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); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 39 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 40 | struct btrfs_root *root, |
| 41 | u64 bytenr, u64 num_bytes, u64 parent, |
| 42 | u64 root_objectid, u64 owner_objectid, |
| 43 | u64 owner_offset, int refs_to_drop, |
| 44 | struct btrfs_delayed_extent_op *extra_op); |
| 45 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 46 | struct extent_buffer *leaf, |
| 47 | struct btrfs_extent_item *ei); |
| 48 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 49 | struct btrfs_root *root, |
| 50 | u64 parent, u64 root_objectid, |
| 51 | u64 flags, u64 owner, u64 offset, |
| 52 | struct btrfs_key *ins, int ref_mod); |
| 53 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 54 | struct btrfs_root *root, |
| 55 | u64 parent, u64 root_objectid, |
| 56 | u64 flags, struct btrfs_disk_key *key, |
| 57 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 58 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 59 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 60 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 61 | static int find_next_key(struct btrfs_path *path, int level, |
| 62 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 63 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 64 | int dump_block_groups); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 65 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 66 | static noinline int |
| 67 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 68 | { |
| 69 | smp_mb(); |
| 70 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 71 | } |
| 72 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 73 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 74 | { |
| 75 | return (cache->flags & bits) == bits; |
| 76 | } |
| 77 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 78 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
| 79 | { |
| 80 | atomic_inc(&cache->count); |
| 81 | } |
| 82 | |
| 83 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 84 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 85 | if (atomic_dec_and_test(&cache->count)) { |
| 86 | WARN_ON(cache->pinned > 0); |
| 87 | WARN_ON(cache->reserved > 0); |
| 88 | WARN_ON(cache->reserved_pinned > 0); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 89 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 90 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 93 | /* |
| 94 | * this adds the block group to the fs_info rb tree for the block group |
| 95 | * cache |
| 96 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 97 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 98 | struct btrfs_block_group_cache *block_group) |
| 99 | { |
| 100 | struct rb_node **p; |
| 101 | struct rb_node *parent = NULL; |
| 102 | struct btrfs_block_group_cache *cache; |
| 103 | |
| 104 | spin_lock(&info->block_group_cache_lock); |
| 105 | p = &info->block_group_cache_tree.rb_node; |
| 106 | |
| 107 | while (*p) { |
| 108 | parent = *p; |
| 109 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 110 | cache_node); |
| 111 | if (block_group->key.objectid < cache->key.objectid) { |
| 112 | p = &(*p)->rb_left; |
| 113 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 114 | p = &(*p)->rb_right; |
| 115 | } else { |
| 116 | spin_unlock(&info->block_group_cache_lock); |
| 117 | return -EEXIST; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | rb_link_node(&block_group->cache_node, parent, p); |
| 122 | rb_insert_color(&block_group->cache_node, |
| 123 | &info->block_group_cache_tree); |
| 124 | spin_unlock(&info->block_group_cache_lock); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * This will return the block group at or after bytenr if contains is 0, else |
| 131 | * it will return the block group that contains the bytenr |
| 132 | */ |
| 133 | static struct btrfs_block_group_cache * |
| 134 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 135 | int contains) |
| 136 | { |
| 137 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 138 | struct rb_node *n; |
| 139 | u64 end, start; |
| 140 | |
| 141 | spin_lock(&info->block_group_cache_lock); |
| 142 | n = info->block_group_cache_tree.rb_node; |
| 143 | |
| 144 | while (n) { |
| 145 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 146 | cache_node); |
| 147 | end = cache->key.objectid + cache->key.offset - 1; |
| 148 | start = cache->key.objectid; |
| 149 | |
| 150 | if (bytenr < start) { |
| 151 | if (!contains && (!ret || start < ret->key.objectid)) |
| 152 | ret = cache; |
| 153 | n = n->rb_left; |
| 154 | } else if (bytenr > start) { |
| 155 | if (contains && bytenr <= end) { |
| 156 | ret = cache; |
| 157 | break; |
| 158 | } |
| 159 | n = n->rb_right; |
| 160 | } else { |
| 161 | ret = cache; |
| 162 | break; |
| 163 | } |
| 164 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 165 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 166 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 167 | spin_unlock(&info->block_group_cache_lock); |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 172 | static int add_excluded_extent(struct btrfs_root *root, |
| 173 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 174 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 175 | u64 end = start + num_bytes - 1; |
| 176 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 177 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 178 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 179 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 180 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 183 | static void free_excluded_extents(struct btrfs_root *root, |
| 184 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 185 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 186 | u64 start, end; |
| 187 | |
| 188 | start = cache->key.objectid; |
| 189 | end = start + cache->key.offset - 1; |
| 190 | |
| 191 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 192 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 193 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 194 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 195 | } |
| 196 | |
| 197 | static int exclude_super_stripes(struct btrfs_root *root, |
| 198 | struct btrfs_block_group_cache *cache) |
| 199 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 200 | u64 bytenr; |
| 201 | u64 *logical; |
| 202 | int stripe_len; |
| 203 | int i, nr, ret; |
| 204 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 205 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 206 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 207 | cache->bytes_super += stripe_len; |
| 208 | ret = add_excluded_extent(root, cache->key.objectid, |
| 209 | stripe_len); |
| 210 | BUG_ON(ret); |
| 211 | } |
| 212 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 213 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 214 | bytenr = btrfs_sb_offset(i); |
| 215 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 216 | cache->key.objectid, bytenr, |
| 217 | 0, &logical, &nr, &stripe_len); |
| 218 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 219 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 220 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 221 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 222 | ret = add_excluded_extent(root, logical[nr], |
| 223 | stripe_len); |
| 224 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 225 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 226 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 227 | kfree(logical); |
| 228 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 229 | return 0; |
| 230 | } |
| 231 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 232 | static struct btrfs_caching_control * |
| 233 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 234 | { |
| 235 | struct btrfs_caching_control *ctl; |
| 236 | |
| 237 | spin_lock(&cache->lock); |
| 238 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 239 | spin_unlock(&cache->lock); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 243 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 244 | if (!cache->caching_ctl) { |
| 245 | spin_unlock(&cache->lock); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 249 | ctl = cache->caching_ctl; |
| 250 | atomic_inc(&ctl->count); |
| 251 | spin_unlock(&cache->lock); |
| 252 | return ctl; |
| 253 | } |
| 254 | |
| 255 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 256 | { |
| 257 | if (atomic_dec_and_test(&ctl->count)) |
| 258 | kfree(ctl); |
| 259 | } |
| 260 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 261 | /* |
| 262 | * this is only called by cache_block_group, since we could have freed extents |
| 263 | * we need to check the pinned_extents for any extents that can't be used yet |
| 264 | * since their free space will be released as soon as the transaction commits. |
| 265 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 266 | 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] | 267 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 268 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 269 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 270 | int ret; |
| 271 | |
| 272 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 273 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 274 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 275 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 276 | if (ret) |
| 277 | break; |
| 278 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 279 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 280 | start = extent_end + 1; |
| 281 | } else if (extent_start > start && extent_start < end) { |
| 282 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 283 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 284 | ret = btrfs_add_free_space(block_group, start, |
| 285 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 286 | BUG_ON(ret); |
| 287 | start = extent_end + 1; |
| 288 | } else { |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (start < end) { |
| 294 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 295 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 296 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 297 | BUG_ON(ret); |
| 298 | } |
| 299 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 300 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 303 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 304 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 305 | struct btrfs_block_group_cache *block_group = data; |
| 306 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 307 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 308 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 309 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 310 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 311 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 312 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 313 | u64 last = 0; |
| 314 | u32 nritems; |
| 315 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 316 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 317 | path = btrfs_alloc_path(); |
| 318 | if (!path) |
| 319 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 320 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 321 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 322 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 323 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 324 | * We don't want to deadlock with somebody trying to allocate a new |
| 325 | * extent for the extent root while also trying to search the extent |
| 326 | * root to add free space. So we skip locking and search the commit |
| 327 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 328 | */ |
| 329 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 330 | path->search_commit_root = 1; |
| 331 | path->reada = 2; |
| 332 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 333 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 334 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 335 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 336 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 337 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 338 | /* need to make sure the commit_root doesn't disappear */ |
| 339 | down_read(&fs_info->extent_commit_sem); |
| 340 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 341 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 342 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 343 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 344 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 345 | leaf = path->nodes[0]; |
| 346 | nritems = btrfs_header_nritems(leaf); |
| 347 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 348 | while (1) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 349 | smp_mb(); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 350 | if (fs_info->closing > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 351 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 352 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 353 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 354 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 355 | if (path->slots[0] < nritems) { |
| 356 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 357 | } else { |
| 358 | ret = find_next_key(path, 0, &key); |
| 359 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 360 | break; |
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 | caching_ctl->progress = last; |
| 363 | btrfs_release_path(extent_root, path); |
| 364 | up_read(&fs_info->extent_commit_sem); |
| 365 | mutex_unlock(&caching_ctl->mutex); |
| 366 | if (btrfs_transaction_in_commit(fs_info)) |
Chris Mason | f36f304 | 2009-07-30 10:04:48 -0400 | [diff] [blame] | 367 | schedule_timeout(1); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 368 | else |
| 369 | cond_resched(); |
| 370 | goto again; |
| 371 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 372 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 373 | if (key.objectid < block_group->key.objectid) { |
| 374 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 375 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 376 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 377 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 378 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 379 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 380 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 381 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 382 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 383 | total_found += add_new_free_space(block_group, |
| 384 | fs_info, last, |
| 385 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 386 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 387 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 388 | if (total_found > (1024 * 1024 * 2)) { |
| 389 | total_found = 0; |
| 390 | wake_up(&caching_ctl->wait); |
| 391 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 392 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 393 | path->slots[0]++; |
| 394 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 395 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 396 | |
| 397 | total_found += add_new_free_space(block_group, fs_info, last, |
| 398 | block_group->key.objectid + |
| 399 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 400 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 401 | |
| 402 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 403 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 404 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 405 | spin_unlock(&block_group->lock); |
| 406 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 407 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 408 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 409 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 410 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 411 | free_excluded_extents(extent_root, block_group); |
| 412 | |
| 413 | mutex_unlock(&caching_ctl->mutex); |
| 414 | wake_up(&caching_ctl->wait); |
| 415 | |
| 416 | put_caching_control(caching_ctl); |
| 417 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 418 | btrfs_put_block_group(block_group); |
| 419 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 423 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
| 424 | struct btrfs_trans_handle *trans, |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 425 | struct btrfs_root *root, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 426 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 427 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 428 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 429 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 430 | struct task_struct *tsk; |
| 431 | int ret = 0; |
| 432 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 433 | smp_mb(); |
| 434 | if (cache->cached != BTRFS_CACHE_NO) |
| 435 | return 0; |
| 436 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 437 | /* |
| 438 | * 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] | 439 | * to have the normal tree locking. Also if we are currently trying to |
| 440 | * allocate blocks for the tree root we can't do the fast caching since |
| 441 | * we likely hold important locks. |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 442 | */ |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 443 | if (trans && (!trans->transaction->in_commit) && |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 444 | (root && root != root->fs_info->tree_root)) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 445 | spin_lock(&cache->lock); |
| 446 | if (cache->cached != BTRFS_CACHE_NO) { |
| 447 | spin_unlock(&cache->lock); |
| 448 | return 0; |
| 449 | } |
| 450 | cache->cached = BTRFS_CACHE_STARTED; |
| 451 | spin_unlock(&cache->lock); |
| 452 | |
| 453 | ret = load_free_space_cache(fs_info, cache); |
| 454 | |
| 455 | spin_lock(&cache->lock); |
| 456 | if (ret == 1) { |
| 457 | cache->cached = BTRFS_CACHE_FINISHED; |
| 458 | cache->last_byte_to_unpin = (u64)-1; |
| 459 | } else { |
| 460 | cache->cached = BTRFS_CACHE_NO; |
| 461 | } |
| 462 | spin_unlock(&cache->lock); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 463 | if (ret == 1) { |
| 464 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 465 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 466 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | if (load_cache_only) |
| 470 | return 0; |
| 471 | |
Miao Xie | fc0e4a3 | 2011-03-24 11:41:21 +0000 | [diff] [blame] | 472 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 473 | BUG_ON(!caching_ctl); |
| 474 | |
| 475 | INIT_LIST_HEAD(&caching_ctl->list); |
| 476 | mutex_init(&caching_ctl->mutex); |
| 477 | init_waitqueue_head(&caching_ctl->wait); |
| 478 | caching_ctl->block_group = cache; |
| 479 | caching_ctl->progress = cache->key.objectid; |
| 480 | /* one for caching kthread, one for caching block group list */ |
| 481 | atomic_set(&caching_ctl->count, 2); |
| 482 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 483 | spin_lock(&cache->lock); |
| 484 | if (cache->cached != BTRFS_CACHE_NO) { |
| 485 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 486 | kfree(caching_ctl); |
| 487 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 488 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 489 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 490 | cache->cached = BTRFS_CACHE_STARTED; |
| 491 | spin_unlock(&cache->lock); |
| 492 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 493 | down_write(&fs_info->extent_commit_sem); |
| 494 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 495 | up_write(&fs_info->extent_commit_sem); |
| 496 | |
| 497 | atomic_inc(&cache->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 498 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 499 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 500 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 501 | cache->key.objectid); |
| 502 | if (IS_ERR(tsk)) { |
| 503 | ret = PTR_ERR(tsk); |
| 504 | printk(KERN_ERR "error running thread %d\n", ret); |
| 505 | BUG(); |
| 506 | } |
| 507 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 508 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 509 | } |
| 510 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 511 | /* |
| 512 | * return the block group that starts at or after bytenr |
| 513 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 514 | static struct btrfs_block_group_cache * |
| 515 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 516 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 517 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 518 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 519 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 520 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 521 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 524 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 525 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 526 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 527 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 528 | struct btrfs_fs_info *info, |
| 529 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 530 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 531 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 532 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 533 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 534 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 535 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 536 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 537 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 538 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 539 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 540 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 541 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 542 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 543 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 544 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 545 | BTRFS_BLOCK_GROUP_METADATA; |
| 546 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 547 | rcu_read_lock(); |
| 548 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 549 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 550 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 551 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 552 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 553 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 554 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 555 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 558 | /* |
| 559 | * after adding space to the filesystem, we need to clear the full flags |
| 560 | * on all the space infos. |
| 561 | */ |
| 562 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 563 | { |
| 564 | struct list_head *head = &info->space_info; |
| 565 | struct btrfs_space_info *found; |
| 566 | |
| 567 | rcu_read_lock(); |
| 568 | list_for_each_entry_rcu(found, head, list) |
| 569 | found->full = 0; |
| 570 | rcu_read_unlock(); |
| 571 | } |
| 572 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 573 | static u64 div_factor(u64 num, int factor) |
| 574 | { |
| 575 | if (factor == 10) |
| 576 | return num; |
| 577 | num *= factor; |
| 578 | do_div(num, 10); |
| 579 | return num; |
| 580 | } |
| 581 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 582 | static u64 div_factor_fine(u64 num, int factor) |
| 583 | { |
| 584 | if (factor == 100) |
| 585 | return num; |
| 586 | num *= factor; |
| 587 | do_div(num, 100); |
| 588 | return num; |
| 589 | } |
| 590 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 591 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 592 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 593 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 594 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 595 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 596 | u64 last = max(search_hint, search_start); |
| 597 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 598 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 599 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 600 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 601 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 602 | while (1) { |
| 603 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 604 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 605 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 606 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 607 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 608 | last = cache->key.objectid + cache->key.offset; |
| 609 | used = btrfs_block_group_used(&cache->item); |
| 610 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 611 | if ((full_search || !cache->ro) && |
| 612 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 613 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 614 | div_factor(cache->key.offset, factor)) { |
| 615 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 616 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 617 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 618 | goto found; |
| 619 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 620 | } |
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 | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 623 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 624 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 625 | if (!wrapped) { |
| 626 | last = search_start; |
| 627 | wrapped = 1; |
| 628 | goto again; |
| 629 | } |
| 630 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 631 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 632 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 633 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 634 | goto again; |
| 635 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 636 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 637 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 638 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 639 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 640 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 641 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 642 | { |
| 643 | int ret; |
| 644 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 645 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 646 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 647 | path = btrfs_alloc_path(); |
| 648 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 649 | key.objectid = start; |
| 650 | key.offset = len; |
| 651 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 652 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 653 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 654 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 655 | return ret; |
| 656 | } |
| 657 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 658 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 659 | * helper function to lookup reference count and flags of extent. |
| 660 | * |
| 661 | * the head node for delayed ref is used to store the sum of all the |
| 662 | * reference count modifications queued up in the rbtree. the head |
| 663 | * node may also store the extent flags to set. This way you can check |
| 664 | * to see what the reference count and extent flags would be if all of |
| 665 | * the delayed refs are not processed. |
| 666 | */ |
| 667 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 668 | struct btrfs_root *root, u64 bytenr, |
| 669 | u64 num_bytes, u64 *refs, u64 *flags) |
| 670 | { |
| 671 | struct btrfs_delayed_ref_head *head; |
| 672 | struct btrfs_delayed_ref_root *delayed_refs; |
| 673 | struct btrfs_path *path; |
| 674 | struct btrfs_extent_item *ei; |
| 675 | struct extent_buffer *leaf; |
| 676 | struct btrfs_key key; |
| 677 | u32 item_size; |
| 678 | u64 num_refs; |
| 679 | u64 extent_flags; |
| 680 | int ret; |
| 681 | |
| 682 | path = btrfs_alloc_path(); |
| 683 | if (!path) |
| 684 | return -ENOMEM; |
| 685 | |
| 686 | key.objectid = bytenr; |
| 687 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 688 | key.offset = num_bytes; |
| 689 | if (!trans) { |
| 690 | path->skip_locking = 1; |
| 691 | path->search_commit_root = 1; |
| 692 | } |
| 693 | again: |
| 694 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 695 | &key, path, 0, 0); |
| 696 | if (ret < 0) |
| 697 | goto out_free; |
| 698 | |
| 699 | if (ret == 0) { |
| 700 | leaf = path->nodes[0]; |
| 701 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 702 | if (item_size >= sizeof(*ei)) { |
| 703 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 704 | struct btrfs_extent_item); |
| 705 | num_refs = btrfs_extent_refs(leaf, ei); |
| 706 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 707 | } else { |
| 708 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 709 | struct btrfs_extent_item_v0 *ei0; |
| 710 | BUG_ON(item_size != sizeof(*ei0)); |
| 711 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 712 | struct btrfs_extent_item_v0); |
| 713 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 714 | /* FIXME: this isn't correct for data */ |
| 715 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 716 | #else |
| 717 | BUG(); |
| 718 | #endif |
| 719 | } |
| 720 | BUG_ON(num_refs == 0); |
| 721 | } else { |
| 722 | num_refs = 0; |
| 723 | extent_flags = 0; |
| 724 | ret = 0; |
| 725 | } |
| 726 | |
| 727 | if (!trans) |
| 728 | goto out; |
| 729 | |
| 730 | delayed_refs = &trans->transaction->delayed_refs; |
| 731 | spin_lock(&delayed_refs->lock); |
| 732 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 733 | if (head) { |
| 734 | if (!mutex_trylock(&head->mutex)) { |
| 735 | atomic_inc(&head->node.refs); |
| 736 | spin_unlock(&delayed_refs->lock); |
| 737 | |
| 738 | btrfs_release_path(root->fs_info->extent_root, path); |
| 739 | |
| 740 | mutex_lock(&head->mutex); |
| 741 | mutex_unlock(&head->mutex); |
| 742 | btrfs_put_delayed_ref(&head->node); |
| 743 | goto again; |
| 744 | } |
| 745 | if (head->extent_op && head->extent_op->update_flags) |
| 746 | extent_flags |= head->extent_op->flags_to_set; |
| 747 | else |
| 748 | BUG_ON(num_refs == 0); |
| 749 | |
| 750 | num_refs += head->node.ref_mod; |
| 751 | mutex_unlock(&head->mutex); |
| 752 | } |
| 753 | spin_unlock(&delayed_refs->lock); |
| 754 | out: |
| 755 | WARN_ON(num_refs == 0); |
| 756 | if (refs) |
| 757 | *refs = num_refs; |
| 758 | if (flags) |
| 759 | *flags = extent_flags; |
| 760 | out_free: |
| 761 | btrfs_free_path(path); |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 766 | * Back reference rules. Back refs have three main goals: |
| 767 | * |
| 768 | * 1) differentiate between all holders of references to an extent so that |
| 769 | * when a reference is dropped we can make sure it was a valid reference |
| 770 | * before freeing the extent. |
| 771 | * |
| 772 | * 2) Provide enough information to quickly find the holders of an extent |
| 773 | * if we notice a given block is corrupted or bad. |
| 774 | * |
| 775 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 776 | * maintenance. This is actually the same as #2, but with a slightly |
| 777 | * different use case. |
| 778 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 779 | * There are two kinds of back refs. The implicit back refs is optimized |
| 780 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 781 | * back refs of this kind provide information about the block's owner tree |
| 782 | * and the pointer's key. These information allow us to find the block by |
| 783 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 784 | * referenced by their owner trees. The location of tree block is recorded |
| 785 | * in the back refs. Actually the full back refs is generic, and can be |
| 786 | * used in all cases the implicit back refs is used. The major shortcoming |
| 787 | * of the full back refs is its overhead. Every time a tree block gets |
| 788 | * COWed, we have to update back refs entry for all pointers in it. |
| 789 | * |
| 790 | * For a newly allocated tree block, we use implicit back refs for |
| 791 | * pointers in it. This means most tree related operations only involve |
| 792 | * implicit back refs. For a tree block created in old transaction, the |
| 793 | * only way to drop a reference to it is COW it. So we can detect the |
| 794 | * event that tree block loses its owner tree's reference and do the |
| 795 | * back refs conversion. |
| 796 | * |
| 797 | * When a tree block is COW'd through a tree, there are four cases: |
| 798 | * |
| 799 | * The reference count of the block is one and the tree is the block's |
| 800 | * owner tree. Nothing to do in this case. |
| 801 | * |
| 802 | * The reference count of the block is one and the tree is not the |
| 803 | * block's owner tree. In this case, full back refs is used for pointers |
| 804 | * in the block. Remove these full back refs, add implicit back refs for |
| 805 | * every pointers in the new block. |
| 806 | * |
| 807 | * The reference count of the block is greater than one and the tree is |
| 808 | * the block's owner tree. In this case, implicit back refs is used for |
| 809 | * pointers in the block. Add full back refs for every pointers in the |
| 810 | * block, increase lower level extents' reference counts. The original |
| 811 | * implicit back refs are entailed to the new block. |
| 812 | * |
| 813 | * The reference count of the block is greater than one and the tree is |
| 814 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 815 | * the new block, increase lower level extents' reference count. |
| 816 | * |
| 817 | * Back Reference Key composing: |
| 818 | * |
| 819 | * The key objectid corresponds to the first byte in the extent, |
| 820 | * The key type is used to differentiate between types of back refs. |
| 821 | * There are different meanings of the key offset for different types |
| 822 | * of back refs. |
| 823 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 824 | * File extents can be referenced by: |
| 825 | * |
| 826 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 827 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 828 | * - different offsets inside a file (bookend extents in file.c) |
| 829 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 830 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 831 | * |
| 832 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 833 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 834 | * - original offset in the file |
| 835 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 836 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 837 | * The key offset for the implicit back refs is hash of the first |
| 838 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 839 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 840 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 841 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 842 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 843 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 844 | * The key offset for the implicit back refs is the first byte of |
| 845 | * the tree leaf |
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 | * When a file extent is allocated, The implicit back refs is used. |
| 848 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 849 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 850 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 851 | * |
| 852 | * When a file extent is removed file truncation, we find the |
| 853 | * corresponding implicit back refs and check the following fields: |
| 854 | * |
| 855 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 856 | * |
| 857 | * Btree extents can be referenced by: |
| 858 | * |
| 859 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 860 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 861 | * Both the implicit back refs and the full back refs for tree blocks |
| 862 | * only consist of key. The key offset for the implicit back refs is |
| 863 | * objectid of block's owner tree. The key offset for the full back refs |
| 864 | * is the first byte of parent block. |
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 | * When implicit back refs is used, information about the lowest key and |
| 867 | * level of the tree block are required. These information are stored in |
| 868 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 869 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 870 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 871 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 872 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 873 | struct btrfs_root *root, |
| 874 | struct btrfs_path *path, |
| 875 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 876 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 877 | struct btrfs_extent_item *item; |
| 878 | struct btrfs_extent_item_v0 *ei0; |
| 879 | struct btrfs_extent_ref_v0 *ref0; |
| 880 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 881 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 882 | struct btrfs_key key; |
| 883 | struct btrfs_key found_key; |
| 884 | u32 new_size = sizeof(*item); |
| 885 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 886 | int ret; |
| 887 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 888 | leaf = path->nodes[0]; |
| 889 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 890 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 891 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 892 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 893 | struct btrfs_extent_item_v0); |
| 894 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 895 | |
| 896 | if (owner == (u64)-1) { |
| 897 | while (1) { |
| 898 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 899 | ret = btrfs_next_leaf(root, path); |
| 900 | if (ret < 0) |
| 901 | return ret; |
| 902 | BUG_ON(ret > 0); |
| 903 | leaf = path->nodes[0]; |
| 904 | } |
| 905 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 906 | path->slots[0]); |
| 907 | BUG_ON(key.objectid != found_key.objectid); |
| 908 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 909 | path->slots[0]++; |
| 910 | continue; |
| 911 | } |
| 912 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 913 | struct btrfs_extent_ref_v0); |
| 914 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 915 | break; |
| 916 | } |
| 917 | } |
| 918 | btrfs_release_path(root, path); |
| 919 | |
| 920 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 921 | new_size += sizeof(*bi); |
| 922 | |
| 923 | new_size -= sizeof(*ei0); |
| 924 | ret = btrfs_search_slot(trans, root, &key, path, |
| 925 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 926 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 927 | return ret; |
| 928 | BUG_ON(ret); |
| 929 | |
| 930 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 931 | BUG_ON(ret); |
| 932 | |
| 933 | leaf = path->nodes[0]; |
| 934 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 935 | btrfs_set_extent_refs(leaf, item, refs); |
| 936 | /* FIXME: get real generation */ |
| 937 | btrfs_set_extent_generation(leaf, item, 0); |
| 938 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 939 | btrfs_set_extent_flags(leaf, item, |
| 940 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 941 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 942 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 943 | /* FIXME: get first key of the block */ |
| 944 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 945 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 946 | } else { |
| 947 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 948 | } |
| 949 | btrfs_mark_buffer_dirty(leaf); |
| 950 | return 0; |
| 951 | } |
| 952 | #endif |
| 953 | |
| 954 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 955 | { |
| 956 | u32 high_crc = ~(u32)0; |
| 957 | u32 low_crc = ~(u32)0; |
| 958 | __le64 lenum; |
| 959 | |
| 960 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 961 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 962 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 963 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 964 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 965 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 966 | |
| 967 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 968 | } |
| 969 | |
| 970 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 971 | struct btrfs_extent_data_ref *ref) |
| 972 | { |
| 973 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 974 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 975 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 976 | } |
| 977 | |
| 978 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 979 | struct btrfs_extent_data_ref *ref, |
| 980 | u64 root_objectid, u64 owner, u64 offset) |
| 981 | { |
| 982 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 983 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 984 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 985 | return 0; |
| 986 | return 1; |
| 987 | } |
| 988 | |
| 989 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 990 | struct btrfs_root *root, |
| 991 | struct btrfs_path *path, |
| 992 | u64 bytenr, u64 parent, |
| 993 | u64 root_objectid, |
| 994 | u64 owner, u64 offset) |
| 995 | { |
| 996 | struct btrfs_key key; |
| 997 | struct btrfs_extent_data_ref *ref; |
| 998 | struct extent_buffer *leaf; |
| 999 | u32 nritems; |
| 1000 | int ret; |
| 1001 | int recow; |
| 1002 | int err = -ENOENT; |
| 1003 | |
| 1004 | key.objectid = bytenr; |
| 1005 | if (parent) { |
| 1006 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1007 | key.offset = parent; |
| 1008 | } else { |
| 1009 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1010 | key.offset = hash_extent_data_ref(root_objectid, |
| 1011 | owner, offset); |
| 1012 | } |
| 1013 | again: |
| 1014 | recow = 0; |
| 1015 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1016 | if (ret < 0) { |
| 1017 | err = ret; |
| 1018 | goto fail; |
| 1019 | } |
| 1020 | |
| 1021 | if (parent) { |
| 1022 | if (!ret) |
| 1023 | return 0; |
| 1024 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1025 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1026 | btrfs_release_path(root, path); |
| 1027 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1028 | if (ret < 0) { |
| 1029 | err = ret; |
| 1030 | goto fail; |
| 1031 | } |
| 1032 | if (!ret) |
| 1033 | return 0; |
| 1034 | #endif |
| 1035 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1039 | nritems = btrfs_header_nritems(leaf); |
| 1040 | while (1) { |
| 1041 | if (path->slots[0] >= nritems) { |
| 1042 | ret = btrfs_next_leaf(root, path); |
| 1043 | if (ret < 0) |
| 1044 | err = ret; |
| 1045 | if (ret) |
| 1046 | goto fail; |
| 1047 | |
| 1048 | leaf = path->nodes[0]; |
| 1049 | nritems = btrfs_header_nritems(leaf); |
| 1050 | recow = 1; |
| 1051 | } |
| 1052 | |
| 1053 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1054 | if (key.objectid != bytenr || |
| 1055 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1056 | goto fail; |
| 1057 | |
| 1058 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1059 | struct btrfs_extent_data_ref); |
| 1060 | |
| 1061 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1062 | owner, offset)) { |
| 1063 | if (recow) { |
| 1064 | btrfs_release_path(root, path); |
| 1065 | goto again; |
| 1066 | } |
| 1067 | err = 0; |
| 1068 | break; |
| 1069 | } |
| 1070 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1071 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1072 | fail: |
| 1073 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1074 | } |
| 1075 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1076 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1077 | struct btrfs_root *root, |
| 1078 | struct btrfs_path *path, |
| 1079 | u64 bytenr, u64 parent, |
| 1080 | u64 root_objectid, u64 owner, |
| 1081 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1082 | { |
| 1083 | struct btrfs_key key; |
| 1084 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1085 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1086 | u32 num_refs; |
| 1087 | int ret; |
| 1088 | |
| 1089 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1090 | if (parent) { |
| 1091 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1092 | key.offset = parent; |
| 1093 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1094 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1095 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1096 | key.offset = hash_extent_data_ref(root_objectid, |
| 1097 | owner, offset); |
| 1098 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1099 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1100 | |
| 1101 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1102 | if (ret && ret != -EEXIST) |
| 1103 | goto fail; |
| 1104 | |
| 1105 | leaf = path->nodes[0]; |
| 1106 | if (parent) { |
| 1107 | struct btrfs_shared_data_ref *ref; |
| 1108 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1109 | struct btrfs_shared_data_ref); |
| 1110 | if (ret == 0) { |
| 1111 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1112 | } else { |
| 1113 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1114 | num_refs += refs_to_add; |
| 1115 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1116 | } |
| 1117 | } else { |
| 1118 | struct btrfs_extent_data_ref *ref; |
| 1119 | while (ret == -EEXIST) { |
| 1120 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1121 | struct btrfs_extent_data_ref); |
| 1122 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1123 | owner, offset)) |
| 1124 | break; |
| 1125 | btrfs_release_path(root, path); |
| 1126 | key.offset++; |
| 1127 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1128 | size); |
| 1129 | if (ret && ret != -EEXIST) |
| 1130 | goto fail; |
| 1131 | |
| 1132 | leaf = path->nodes[0]; |
| 1133 | } |
| 1134 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1135 | struct btrfs_extent_data_ref); |
| 1136 | if (ret == 0) { |
| 1137 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1138 | root_objectid); |
| 1139 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1140 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1141 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1142 | } else { |
| 1143 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1144 | num_refs += refs_to_add; |
| 1145 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1146 | } |
| 1147 | } |
| 1148 | btrfs_mark_buffer_dirty(leaf); |
| 1149 | ret = 0; |
| 1150 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1151 | btrfs_release_path(root, path); |
| 1152 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1153 | } |
| 1154 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1155 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1156 | struct btrfs_root *root, |
| 1157 | struct btrfs_path *path, |
| 1158 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1159 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1160 | struct btrfs_key key; |
| 1161 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1162 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1163 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1164 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1165 | int ret = 0; |
| 1166 | |
| 1167 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1168 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1169 | |
| 1170 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1171 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1172 | struct btrfs_extent_data_ref); |
| 1173 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1174 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1175 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1176 | struct btrfs_shared_data_ref); |
| 1177 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1178 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1179 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1180 | struct btrfs_extent_ref_v0 *ref0; |
| 1181 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1182 | struct btrfs_extent_ref_v0); |
| 1183 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1184 | #endif |
| 1185 | } else { |
| 1186 | BUG(); |
| 1187 | } |
| 1188 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1189 | BUG_ON(num_refs < refs_to_drop); |
| 1190 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1191 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1192 | if (num_refs == 0) { |
| 1193 | ret = btrfs_del_item(trans, root, path); |
| 1194 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1195 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1196 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1197 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1198 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1199 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1200 | else { |
| 1201 | struct btrfs_extent_ref_v0 *ref0; |
| 1202 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1203 | struct btrfs_extent_ref_v0); |
| 1204 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1205 | } |
| 1206 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1207 | btrfs_mark_buffer_dirty(leaf); |
| 1208 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1209 | return ret; |
| 1210 | } |
| 1211 | |
| 1212 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1213 | struct btrfs_path *path, |
| 1214 | struct btrfs_extent_inline_ref *iref) |
| 1215 | { |
| 1216 | struct btrfs_key key; |
| 1217 | struct extent_buffer *leaf; |
| 1218 | struct btrfs_extent_data_ref *ref1; |
| 1219 | struct btrfs_shared_data_ref *ref2; |
| 1220 | u32 num_refs = 0; |
| 1221 | |
| 1222 | leaf = path->nodes[0]; |
| 1223 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1224 | if (iref) { |
| 1225 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1226 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1227 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1228 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1229 | } else { |
| 1230 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1231 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1232 | } |
| 1233 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1234 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1235 | struct btrfs_extent_data_ref); |
| 1236 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1237 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1238 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1239 | struct btrfs_shared_data_ref); |
| 1240 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1241 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1242 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1243 | struct btrfs_extent_ref_v0 *ref0; |
| 1244 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1245 | struct btrfs_extent_ref_v0); |
| 1246 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1247 | #endif |
| 1248 | } else { |
| 1249 | WARN_ON(1); |
| 1250 | } |
| 1251 | return num_refs; |
| 1252 | } |
| 1253 | |
| 1254 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1255 | struct btrfs_root *root, |
| 1256 | struct btrfs_path *path, |
| 1257 | u64 bytenr, u64 parent, |
| 1258 | u64 root_objectid) |
| 1259 | { |
| 1260 | struct btrfs_key key; |
| 1261 | int ret; |
| 1262 | |
| 1263 | key.objectid = bytenr; |
| 1264 | if (parent) { |
| 1265 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1266 | key.offset = parent; |
| 1267 | } else { |
| 1268 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1269 | key.offset = root_objectid; |
| 1270 | } |
| 1271 | |
| 1272 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1273 | if (ret > 0) |
| 1274 | ret = -ENOENT; |
| 1275 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1276 | if (ret == -ENOENT && parent) { |
| 1277 | btrfs_release_path(root, path); |
| 1278 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1279 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1280 | if (ret > 0) |
| 1281 | ret = -ENOENT; |
| 1282 | } |
| 1283 | #endif |
| 1284 | return ret; |
| 1285 | } |
| 1286 | |
| 1287 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1288 | struct btrfs_root *root, |
| 1289 | struct btrfs_path *path, |
| 1290 | u64 bytenr, u64 parent, |
| 1291 | u64 root_objectid) |
| 1292 | { |
| 1293 | struct btrfs_key key; |
| 1294 | int ret; |
| 1295 | |
| 1296 | key.objectid = bytenr; |
| 1297 | if (parent) { |
| 1298 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1299 | key.offset = parent; |
| 1300 | } else { |
| 1301 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1302 | key.offset = root_objectid; |
| 1303 | } |
| 1304 | |
| 1305 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1306 | btrfs_release_path(root, path); |
| 1307 | return ret; |
| 1308 | } |
| 1309 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1310 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1311 | { |
| 1312 | int type; |
| 1313 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1314 | if (parent > 0) |
| 1315 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1316 | else |
| 1317 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1318 | } else { |
| 1319 | if (parent > 0) |
| 1320 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1321 | else |
| 1322 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1323 | } |
| 1324 | return type; |
| 1325 | } |
| 1326 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1327 | static int find_next_key(struct btrfs_path *path, int level, |
| 1328 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1329 | |
| 1330 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1331 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1332 | if (!path->nodes[level]) |
| 1333 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1334 | if (path->slots[level] + 1 >= |
| 1335 | btrfs_header_nritems(path->nodes[level])) |
| 1336 | continue; |
| 1337 | if (level == 0) |
| 1338 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1339 | path->slots[level] + 1); |
| 1340 | else |
| 1341 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1342 | path->slots[level] + 1); |
| 1343 | return 0; |
| 1344 | } |
| 1345 | return 1; |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1350 | * to the address of inline back ref, and 0 is returned. |
| 1351 | * |
| 1352 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1353 | * should be inserted, and -ENOENT is returned. |
| 1354 | * |
| 1355 | * if insert is true and there are too many inline back refs, the path |
| 1356 | * points to the extent item, and -EAGAIN is returned. |
| 1357 | * |
| 1358 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1359 | * items in the tree are ordered. |
| 1360 | */ |
| 1361 | static noinline_for_stack |
| 1362 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1363 | struct btrfs_root *root, |
| 1364 | struct btrfs_path *path, |
| 1365 | struct btrfs_extent_inline_ref **ref_ret, |
| 1366 | u64 bytenr, u64 num_bytes, |
| 1367 | u64 parent, u64 root_objectid, |
| 1368 | u64 owner, u64 offset, int insert) |
| 1369 | { |
| 1370 | struct btrfs_key key; |
| 1371 | struct extent_buffer *leaf; |
| 1372 | struct btrfs_extent_item *ei; |
| 1373 | struct btrfs_extent_inline_ref *iref; |
| 1374 | u64 flags; |
| 1375 | u64 item_size; |
| 1376 | unsigned long ptr; |
| 1377 | unsigned long end; |
| 1378 | int extra_size; |
| 1379 | int type; |
| 1380 | int want; |
| 1381 | int ret; |
| 1382 | int err = 0; |
| 1383 | |
| 1384 | key.objectid = bytenr; |
| 1385 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1386 | key.offset = num_bytes; |
| 1387 | |
| 1388 | want = extent_ref_type(parent, owner); |
| 1389 | if (insert) { |
| 1390 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1391 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1392 | } else |
| 1393 | extra_size = -1; |
| 1394 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1395 | if (ret < 0) { |
| 1396 | err = ret; |
| 1397 | goto out; |
| 1398 | } |
| 1399 | BUG_ON(ret); |
| 1400 | |
| 1401 | leaf = path->nodes[0]; |
| 1402 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1403 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1404 | if (item_size < sizeof(*ei)) { |
| 1405 | if (!insert) { |
| 1406 | err = -ENOENT; |
| 1407 | goto out; |
| 1408 | } |
| 1409 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1410 | extra_size); |
| 1411 | if (ret < 0) { |
| 1412 | err = ret; |
| 1413 | goto out; |
| 1414 | } |
| 1415 | leaf = path->nodes[0]; |
| 1416 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1417 | } |
| 1418 | #endif |
| 1419 | BUG_ON(item_size < sizeof(*ei)); |
| 1420 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1421 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1422 | flags = btrfs_extent_flags(leaf, ei); |
| 1423 | |
| 1424 | ptr = (unsigned long)(ei + 1); |
| 1425 | end = (unsigned long)ei + item_size; |
| 1426 | |
| 1427 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1428 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1429 | BUG_ON(ptr > end); |
| 1430 | } else { |
| 1431 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1432 | } |
| 1433 | |
| 1434 | err = -ENOENT; |
| 1435 | while (1) { |
| 1436 | if (ptr >= end) { |
| 1437 | WARN_ON(ptr > end); |
| 1438 | break; |
| 1439 | } |
| 1440 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1441 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1442 | if (want < type) |
| 1443 | break; |
| 1444 | if (want > type) { |
| 1445 | ptr += btrfs_extent_inline_ref_size(type); |
| 1446 | continue; |
| 1447 | } |
| 1448 | |
| 1449 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1450 | struct btrfs_extent_data_ref *dref; |
| 1451 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1452 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1453 | owner, offset)) { |
| 1454 | err = 0; |
| 1455 | break; |
| 1456 | } |
| 1457 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1458 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1459 | break; |
| 1460 | } else { |
| 1461 | u64 ref_offset; |
| 1462 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1463 | if (parent > 0) { |
| 1464 | if (parent == ref_offset) { |
| 1465 | err = 0; |
| 1466 | break; |
| 1467 | } |
| 1468 | if (ref_offset < parent) |
| 1469 | break; |
| 1470 | } else { |
| 1471 | if (root_objectid == ref_offset) { |
| 1472 | err = 0; |
| 1473 | break; |
| 1474 | } |
| 1475 | if (ref_offset < root_objectid) |
| 1476 | break; |
| 1477 | } |
| 1478 | } |
| 1479 | ptr += btrfs_extent_inline_ref_size(type); |
| 1480 | } |
| 1481 | if (err == -ENOENT && insert) { |
| 1482 | if (item_size + extra_size >= |
| 1483 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1484 | err = -EAGAIN; |
| 1485 | goto out; |
| 1486 | } |
| 1487 | /* |
| 1488 | * To add new inline back ref, we have to make sure |
| 1489 | * there is no corresponding back ref item. |
| 1490 | * For simplicity, we just do not add new inline back |
| 1491 | * ref if there is any kind of item for this block |
| 1492 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1493 | if (find_next_key(path, 0, &key) == 0 && |
| 1494 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1495 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1496 | err = -EAGAIN; |
| 1497 | goto out; |
| 1498 | } |
| 1499 | } |
| 1500 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1501 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1502 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1503 | path->keep_locks = 0; |
| 1504 | btrfs_unlock_up_safe(path, 1); |
| 1505 | } |
| 1506 | return err; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * helper to add new inline back ref |
| 1511 | */ |
| 1512 | static noinline_for_stack |
| 1513 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1514 | struct btrfs_root *root, |
| 1515 | struct btrfs_path *path, |
| 1516 | struct btrfs_extent_inline_ref *iref, |
| 1517 | u64 parent, u64 root_objectid, |
| 1518 | u64 owner, u64 offset, int refs_to_add, |
| 1519 | struct btrfs_delayed_extent_op *extent_op) |
| 1520 | { |
| 1521 | struct extent_buffer *leaf; |
| 1522 | struct btrfs_extent_item *ei; |
| 1523 | unsigned long ptr; |
| 1524 | unsigned long end; |
| 1525 | unsigned long item_offset; |
| 1526 | u64 refs; |
| 1527 | int size; |
| 1528 | int type; |
| 1529 | int ret; |
| 1530 | |
| 1531 | leaf = path->nodes[0]; |
| 1532 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1533 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1534 | |
| 1535 | type = extent_ref_type(parent, owner); |
| 1536 | size = btrfs_extent_inline_ref_size(type); |
| 1537 | |
| 1538 | ret = btrfs_extend_item(trans, root, path, size); |
| 1539 | BUG_ON(ret); |
| 1540 | |
| 1541 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1542 | refs = btrfs_extent_refs(leaf, ei); |
| 1543 | refs += refs_to_add; |
| 1544 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1545 | if (extent_op) |
| 1546 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1547 | |
| 1548 | ptr = (unsigned long)ei + item_offset; |
| 1549 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1550 | if (ptr < end - size) |
| 1551 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1552 | end - size - ptr); |
| 1553 | |
| 1554 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1555 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1556 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1557 | struct btrfs_extent_data_ref *dref; |
| 1558 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1559 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1560 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1561 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1562 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1563 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1564 | struct btrfs_shared_data_ref *sref; |
| 1565 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1566 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1567 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1568 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1569 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1570 | } else { |
| 1571 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1572 | } |
| 1573 | btrfs_mark_buffer_dirty(leaf); |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
| 1577 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1578 | struct btrfs_root *root, |
| 1579 | struct btrfs_path *path, |
| 1580 | struct btrfs_extent_inline_ref **ref_ret, |
| 1581 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1582 | u64 root_objectid, u64 owner, u64 offset) |
| 1583 | { |
| 1584 | int ret; |
| 1585 | |
| 1586 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1587 | bytenr, num_bytes, parent, |
| 1588 | root_objectid, owner, offset, 0); |
| 1589 | if (ret != -ENOENT) |
| 1590 | return ret; |
| 1591 | |
| 1592 | btrfs_release_path(root, path); |
| 1593 | *ref_ret = NULL; |
| 1594 | |
| 1595 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1596 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1597 | root_objectid); |
| 1598 | } else { |
| 1599 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1600 | root_objectid, owner, offset); |
| 1601 | } |
| 1602 | return ret; |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * helper to update/remove inline back ref |
| 1607 | */ |
| 1608 | static noinline_for_stack |
| 1609 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1610 | struct btrfs_root *root, |
| 1611 | struct btrfs_path *path, |
| 1612 | struct btrfs_extent_inline_ref *iref, |
| 1613 | int refs_to_mod, |
| 1614 | struct btrfs_delayed_extent_op *extent_op) |
| 1615 | { |
| 1616 | struct extent_buffer *leaf; |
| 1617 | struct btrfs_extent_item *ei; |
| 1618 | struct btrfs_extent_data_ref *dref = NULL; |
| 1619 | struct btrfs_shared_data_ref *sref = NULL; |
| 1620 | unsigned long ptr; |
| 1621 | unsigned long end; |
| 1622 | u32 item_size; |
| 1623 | int size; |
| 1624 | int type; |
| 1625 | int ret; |
| 1626 | u64 refs; |
| 1627 | |
| 1628 | leaf = path->nodes[0]; |
| 1629 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1630 | refs = btrfs_extent_refs(leaf, ei); |
| 1631 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1632 | refs += refs_to_mod; |
| 1633 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1634 | if (extent_op) |
| 1635 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1636 | |
| 1637 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1638 | |
| 1639 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1640 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1641 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1642 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1643 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1644 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1645 | } else { |
| 1646 | refs = 1; |
| 1647 | BUG_ON(refs_to_mod != -1); |
| 1648 | } |
| 1649 | |
| 1650 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1651 | refs += refs_to_mod; |
| 1652 | |
| 1653 | if (refs > 0) { |
| 1654 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1655 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1656 | else |
| 1657 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1658 | } else { |
| 1659 | size = btrfs_extent_inline_ref_size(type); |
| 1660 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1661 | ptr = (unsigned long)iref; |
| 1662 | end = (unsigned long)ei + item_size; |
| 1663 | if (ptr + size < end) |
| 1664 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1665 | end - ptr - size); |
| 1666 | item_size -= size; |
| 1667 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1668 | BUG_ON(ret); |
| 1669 | } |
| 1670 | btrfs_mark_buffer_dirty(leaf); |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | static noinline_for_stack |
| 1675 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1676 | struct btrfs_root *root, |
| 1677 | struct btrfs_path *path, |
| 1678 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1679 | u64 root_objectid, u64 owner, |
| 1680 | u64 offset, int refs_to_add, |
| 1681 | struct btrfs_delayed_extent_op *extent_op) |
| 1682 | { |
| 1683 | struct btrfs_extent_inline_ref *iref; |
| 1684 | int ret; |
| 1685 | |
| 1686 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1687 | bytenr, num_bytes, parent, |
| 1688 | root_objectid, owner, offset, 1); |
| 1689 | if (ret == 0) { |
| 1690 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1691 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1692 | refs_to_add, extent_op); |
| 1693 | } else if (ret == -ENOENT) { |
| 1694 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1695 | parent, root_objectid, |
| 1696 | owner, offset, refs_to_add, |
| 1697 | extent_op); |
| 1698 | } |
| 1699 | return ret; |
| 1700 | } |
| 1701 | |
| 1702 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1703 | struct btrfs_root *root, |
| 1704 | struct btrfs_path *path, |
| 1705 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1706 | u64 owner, u64 offset, int refs_to_add) |
| 1707 | { |
| 1708 | int ret; |
| 1709 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1710 | BUG_ON(refs_to_add != 1); |
| 1711 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1712 | parent, root_objectid); |
| 1713 | } else { |
| 1714 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1715 | parent, root_objectid, |
| 1716 | owner, offset, refs_to_add); |
| 1717 | } |
| 1718 | return ret; |
| 1719 | } |
| 1720 | |
| 1721 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1722 | struct btrfs_root *root, |
| 1723 | struct btrfs_path *path, |
| 1724 | struct btrfs_extent_inline_ref *iref, |
| 1725 | int refs_to_drop, int is_data) |
| 1726 | { |
| 1727 | int ret; |
| 1728 | |
| 1729 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1730 | if (iref) { |
| 1731 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1732 | -refs_to_drop, NULL); |
| 1733 | } else if (is_data) { |
| 1734 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1735 | } else { |
| 1736 | ret = btrfs_del_item(trans, root, path); |
| 1737 | } |
| 1738 | return ret; |
| 1739 | } |
| 1740 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1741 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1742 | u64 start, u64 len) |
| 1743 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1744 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1745 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1746 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1747 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1748 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1749 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1750 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1751 | u64 discarded_bytes = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1752 | struct btrfs_multi_bio *multi = NULL; |
| 1753 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1754 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1755 | /* Tell the block device(s) that the sectors can be discarded */ |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1756 | ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, |
| 1757 | bytenr, &num_bytes, &multi, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1758 | if (!ret) { |
| 1759 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1760 | int i; |
| 1761 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1762 | |
| 1763 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1764 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1765 | stripe->physical, |
| 1766 | stripe->length); |
| 1767 | if (!ret) |
| 1768 | discarded_bytes += stripe->length; |
| 1769 | else if (ret != -EOPNOTSUPP) |
| 1770 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1771 | } |
| 1772 | kfree(multi); |
| 1773 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1774 | if (discarded_bytes && ret == -EOPNOTSUPP) |
| 1775 | ret = 0; |
| 1776 | |
| 1777 | if (actual_bytes) |
| 1778 | *actual_bytes = discarded_bytes; |
| 1779 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 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 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3093 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 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; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3348 | long time_left; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3349 | int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3350 | int loops = 0; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3351 | unsigned long progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3352 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3353 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3354 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3355 | |
| 3356 | smp_mb(); |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3357 | reserved = space_info->bytes_reserved; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3358 | progress = space_info->reservation_progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3359 | |
| 3360 | if (reserved == 0) |
| 3361 | return 0; |
| 3362 | |
| 3363 | max_reclaim = min(reserved, to_reclaim); |
| 3364 | |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3365 | while (loops < 1024) { |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3366 | /* have the flusher threads jump in and do some IO */ |
| 3367 | smp_mb(); |
| 3368 | nr_pages = min_t(unsigned long, nr_pages, |
| 3369 | root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); |
| 3370 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3371 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3372 | spin_lock(&space_info->lock); |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3373 | if (reserved > space_info->bytes_reserved) |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3374 | reclaimed += reserved - space_info->bytes_reserved; |
| 3375 | reserved = space_info->bytes_reserved; |
| 3376 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3377 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3378 | loops++; |
| 3379 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3380 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3381 | break; |
| 3382 | |
| 3383 | if (trans && trans->transaction->blocked) |
| 3384 | return -EAGAIN; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3385 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3386 | time_left = schedule_timeout_interruptible(1); |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3387 | |
| 3388 | /* We were interrupted, exit */ |
| 3389 | if (time_left) |
| 3390 | break; |
| 3391 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3392 | /* we've kicked the IO a few times, if anything has been freed, |
| 3393 | * exit. There is no sense in looping here for a long time |
| 3394 | * when we really need to commit the transaction, or there are |
| 3395 | * just too many writers without enough free space |
| 3396 | */ |
| 3397 | |
| 3398 | if (loops > 3) { |
| 3399 | smp_mb(); |
| 3400 | if (progress != space_info->reservation_progress) |
| 3401 | break; |
| 3402 | } |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3403 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3404 | } |
| 3405 | return reclaimed >= to_reclaim; |
| 3406 | } |
| 3407 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3408 | /* |
| 3409 | * Retries tells us how many times we've called reserve_metadata_bytes. The |
| 3410 | * idea is if this is the first call (retries == 0) then we will add to our |
| 3411 | * reserved count if we can't make the allocation in order to hold our place |
| 3412 | * while we go and try and free up space. That way for retries > 1 we don't try |
| 3413 | * and add space, we just check to see if the amount of unused space is >= the |
| 3414 | * total space, meaning that our reservation is valid. |
| 3415 | * |
| 3416 | * However if we don't intend to retry this reservation, pass -1 as retries so |
| 3417 | * that it short circuits this logic. |
| 3418 | */ |
| 3419 | static int reserve_metadata_bytes(struct btrfs_trans_handle *trans, |
| 3420 | struct btrfs_root *root, |
| 3421 | struct btrfs_block_rsv *block_rsv, |
| 3422 | u64 orig_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3423 | { |
| 3424 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3425 | u64 unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3426 | u64 num_bytes = orig_bytes; |
| 3427 | int retries = 0; |
| 3428 | int ret = 0; |
| 3429 | bool reserved = false; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3430 | bool committed = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3431 | |
| 3432 | again: |
| 3433 | ret = -ENOSPC; |
| 3434 | if (reserved) |
| 3435 | num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3436 | |
| 3437 | spin_lock(&space_info->lock); |
| 3438 | unused = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3439 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3440 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3441 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3442 | /* |
| 3443 | * The idea here is that we've not already over-reserved the block group |
| 3444 | * then we can go ahead and save our reservation first and then start |
| 3445 | * flushing if we need to. Otherwise if we've already overcommitted |
| 3446 | * lets start flushing stuff first and then come back and try to make |
| 3447 | * our reservation. |
| 3448 | */ |
| 3449 | if (unused <= space_info->total_bytes) { |
Arne Jansen | 6f33434 | 2010-11-12 23:17:56 +0000 | [diff] [blame] | 3450 | unused = space_info->total_bytes - unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3451 | if (unused >= num_bytes) { |
| 3452 | if (!reserved) |
| 3453 | space_info->bytes_reserved += orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3454 | ret = 0; |
| 3455 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3456 | /* |
| 3457 | * Ok set num_bytes to orig_bytes since we aren't |
| 3458 | * overocmmitted, this way we only try and reclaim what |
| 3459 | * we need. |
| 3460 | */ |
| 3461 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3462 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3463 | } else { |
| 3464 | /* |
| 3465 | * Ok we're over committed, set num_bytes to the overcommitted |
| 3466 | * amount plus the amount of bytes that we need for this |
| 3467 | * reservation. |
| 3468 | */ |
| 3469 | num_bytes = unused - space_info->total_bytes + |
| 3470 | (orig_bytes * (retries + 1)); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3471 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3472 | |
| 3473 | /* |
| 3474 | * Couldn't make our reservation, save our place so while we're trying |
| 3475 | * to reclaim space we can actually use it instead of somebody else |
| 3476 | * stealing it from us. |
| 3477 | */ |
| 3478 | if (ret && !reserved) { |
| 3479 | space_info->bytes_reserved += orig_bytes; |
| 3480 | reserved = true; |
| 3481 | } |
| 3482 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3483 | spin_unlock(&space_info->lock); |
| 3484 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3485 | if (!ret) |
| 3486 | return 0; |
| 3487 | |
| 3488 | if (!flush) |
| 3489 | goto out; |
| 3490 | |
| 3491 | /* |
| 3492 | * We do synchronous shrinking since we don't actually unreserve |
| 3493 | * metadata until after the IO is completed. |
| 3494 | */ |
| 3495 | ret = shrink_delalloc(trans, root, num_bytes, 1); |
| 3496 | if (ret > 0) |
| 3497 | return 0; |
| 3498 | else if (ret < 0) |
| 3499 | goto out; |
| 3500 | |
| 3501 | /* |
| 3502 | * So if we were overcommitted it's possible that somebody else flushed |
| 3503 | * out enough space and we simply didn't have enough space to reclaim, |
| 3504 | * so go back around and try again. |
| 3505 | */ |
| 3506 | if (retries < 2) { |
| 3507 | retries++; |
| 3508 | goto again; |
| 3509 | } |
| 3510 | |
| 3511 | spin_lock(&space_info->lock); |
| 3512 | /* |
| 3513 | * Not enough space to be reclaimed, don't bother committing the |
| 3514 | * transaction. |
| 3515 | */ |
| 3516 | if (space_info->bytes_pinned < orig_bytes) |
| 3517 | ret = -ENOSPC; |
| 3518 | spin_unlock(&space_info->lock); |
| 3519 | if (ret) |
| 3520 | goto out; |
| 3521 | |
| 3522 | ret = -EAGAIN; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3523 | if (trans || committed) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3524 | goto out; |
| 3525 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3526 | ret = -ENOSPC; |
| 3527 | trans = btrfs_join_transaction(root, 1); |
| 3528 | if (IS_ERR(trans)) |
| 3529 | goto out; |
| 3530 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3531 | if (!ret) { |
| 3532 | trans = NULL; |
| 3533 | committed = true; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3534 | goto again; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3535 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3536 | |
| 3537 | out: |
| 3538 | if (reserved) { |
| 3539 | spin_lock(&space_info->lock); |
| 3540 | space_info->bytes_reserved -= orig_bytes; |
| 3541 | spin_unlock(&space_info->lock); |
| 3542 | } |
| 3543 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3544 | return ret; |
| 3545 | } |
| 3546 | |
| 3547 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3548 | struct btrfs_root *root) |
| 3549 | { |
| 3550 | struct btrfs_block_rsv *block_rsv; |
| 3551 | if (root->ref_cows) |
| 3552 | block_rsv = trans->block_rsv; |
| 3553 | else |
| 3554 | block_rsv = root->block_rsv; |
| 3555 | |
| 3556 | if (!block_rsv) |
| 3557 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3558 | |
| 3559 | return block_rsv; |
| 3560 | } |
| 3561 | |
| 3562 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3563 | u64 num_bytes) |
| 3564 | { |
| 3565 | int ret = -ENOSPC; |
| 3566 | spin_lock(&block_rsv->lock); |
| 3567 | if (block_rsv->reserved >= num_bytes) { |
| 3568 | block_rsv->reserved -= num_bytes; |
| 3569 | if (block_rsv->reserved < block_rsv->size) |
| 3570 | block_rsv->full = 0; |
| 3571 | ret = 0; |
| 3572 | } |
| 3573 | spin_unlock(&block_rsv->lock); |
| 3574 | return ret; |
| 3575 | } |
| 3576 | |
| 3577 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3578 | u64 num_bytes, int update_size) |
| 3579 | { |
| 3580 | spin_lock(&block_rsv->lock); |
| 3581 | block_rsv->reserved += num_bytes; |
| 3582 | if (update_size) |
| 3583 | block_rsv->size += num_bytes; |
| 3584 | else if (block_rsv->reserved >= block_rsv->size) |
| 3585 | block_rsv->full = 1; |
| 3586 | spin_unlock(&block_rsv->lock); |
| 3587 | } |
| 3588 | |
| 3589 | void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3590 | struct btrfs_block_rsv *dest, u64 num_bytes) |
| 3591 | { |
| 3592 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3593 | |
| 3594 | spin_lock(&block_rsv->lock); |
| 3595 | if (num_bytes == (u64)-1) |
| 3596 | num_bytes = block_rsv->size; |
| 3597 | block_rsv->size -= num_bytes; |
| 3598 | if (block_rsv->reserved >= block_rsv->size) { |
| 3599 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3600 | block_rsv->reserved = block_rsv->size; |
| 3601 | block_rsv->full = 1; |
| 3602 | } else { |
| 3603 | num_bytes = 0; |
| 3604 | } |
| 3605 | spin_unlock(&block_rsv->lock); |
| 3606 | |
| 3607 | if (num_bytes > 0) { |
| 3608 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 3609 | spin_lock(&dest->lock); |
| 3610 | if (!dest->full) { |
| 3611 | u64 bytes_to_add; |
| 3612 | |
| 3613 | bytes_to_add = dest->size - dest->reserved; |
| 3614 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 3615 | dest->reserved += bytes_to_add; |
| 3616 | if (dest->reserved >= dest->size) |
| 3617 | dest->full = 1; |
| 3618 | num_bytes -= bytes_to_add; |
| 3619 | } |
| 3620 | spin_unlock(&dest->lock); |
| 3621 | } |
| 3622 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3623 | spin_lock(&space_info->lock); |
| 3624 | space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3625 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3626 | spin_unlock(&space_info->lock); |
| 3627 | } |
| 3628 | } |
| 3629 | } |
| 3630 | |
| 3631 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3632 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3633 | { |
| 3634 | int ret; |
| 3635 | |
| 3636 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3637 | if (ret) |
| 3638 | return ret; |
| 3639 | |
| 3640 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3641 | return 0; |
| 3642 | } |
| 3643 | |
| 3644 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3645 | { |
| 3646 | memset(rsv, 0, sizeof(*rsv)); |
| 3647 | spin_lock_init(&rsv->lock); |
| 3648 | atomic_set(&rsv->usage, 1); |
| 3649 | rsv->priority = 6; |
| 3650 | INIT_LIST_HEAD(&rsv->list); |
| 3651 | } |
| 3652 | |
| 3653 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3654 | { |
| 3655 | struct btrfs_block_rsv *block_rsv; |
| 3656 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3657 | |
| 3658 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3659 | if (!block_rsv) |
| 3660 | return NULL; |
| 3661 | |
| 3662 | btrfs_init_block_rsv(block_rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3663 | block_rsv->space_info = __find_space_info(fs_info, |
| 3664 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3665 | return block_rsv; |
| 3666 | } |
| 3667 | |
| 3668 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3669 | struct btrfs_block_rsv *rsv) |
| 3670 | { |
| 3671 | if (rsv && atomic_dec_and_test(&rsv->usage)) { |
| 3672 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3673 | if (!rsv->durable) |
| 3674 | kfree(rsv); |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | /* |
| 3679 | * make the block_rsv struct be able to capture freed space. |
| 3680 | * the captured space will re-add to the the block_rsv struct |
| 3681 | * after transaction commit |
| 3682 | */ |
| 3683 | void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info, |
| 3684 | struct btrfs_block_rsv *block_rsv) |
| 3685 | { |
| 3686 | block_rsv->durable = 1; |
| 3687 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 3688 | list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list); |
| 3689 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 3690 | } |
| 3691 | |
| 3692 | int btrfs_block_rsv_add(struct btrfs_trans_handle *trans, |
| 3693 | struct btrfs_root *root, |
| 3694 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3695 | u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3696 | { |
| 3697 | int ret; |
| 3698 | |
| 3699 | if (num_bytes == 0) |
| 3700 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3701 | |
| 3702 | ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3703 | if (!ret) { |
| 3704 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3705 | return 0; |
| 3706 | } |
| 3707 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3708 | return ret; |
| 3709 | } |
| 3710 | |
| 3711 | int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, |
| 3712 | struct btrfs_root *root, |
| 3713 | struct btrfs_block_rsv *block_rsv, |
| 3714 | u64 min_reserved, int min_factor) |
| 3715 | { |
| 3716 | u64 num_bytes = 0; |
| 3717 | int commit_trans = 0; |
| 3718 | int ret = -ENOSPC; |
| 3719 | |
| 3720 | if (!block_rsv) |
| 3721 | return 0; |
| 3722 | |
| 3723 | spin_lock(&block_rsv->lock); |
| 3724 | if (min_factor > 0) |
| 3725 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3726 | if (min_reserved > num_bytes) |
| 3727 | num_bytes = min_reserved; |
| 3728 | |
| 3729 | if (block_rsv->reserved >= num_bytes) { |
| 3730 | ret = 0; |
| 3731 | } else { |
| 3732 | num_bytes -= block_rsv->reserved; |
| 3733 | if (block_rsv->durable && |
| 3734 | block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes) |
| 3735 | commit_trans = 1; |
| 3736 | } |
| 3737 | spin_unlock(&block_rsv->lock); |
| 3738 | if (!ret) |
| 3739 | return 0; |
| 3740 | |
| 3741 | if (block_rsv->refill_used) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3742 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 3743 | num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3744 | if (!ret) { |
| 3745 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3746 | return 0; |
| 3747 | } |
| 3748 | } |
| 3749 | |
| 3750 | if (commit_trans) { |
| 3751 | if (trans) |
| 3752 | return -EAGAIN; |
| 3753 | |
| 3754 | trans = btrfs_join_transaction(root, 1); |
| 3755 | BUG_ON(IS_ERR(trans)); |
| 3756 | ret = btrfs_commit_transaction(trans, root); |
| 3757 | return 0; |
| 3758 | } |
| 3759 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3760 | return -ENOSPC; |
| 3761 | } |
| 3762 | |
| 3763 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3764 | struct btrfs_block_rsv *dst_rsv, |
| 3765 | u64 num_bytes) |
| 3766 | { |
| 3767 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3768 | } |
| 3769 | |
| 3770 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3771 | struct btrfs_block_rsv *block_rsv, |
| 3772 | u64 num_bytes) |
| 3773 | { |
| 3774 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3775 | if (global_rsv->full || global_rsv == block_rsv || |
| 3776 | block_rsv->space_info != global_rsv->space_info) |
| 3777 | global_rsv = NULL; |
| 3778 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3779 | } |
| 3780 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3781 | /* |
| 3782 | * helper to calculate size of global block reservation. |
| 3783 | * the desired value is sum of space used by extent tree, |
| 3784 | * checksum tree and root tree |
| 3785 | */ |
| 3786 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3787 | { |
| 3788 | struct btrfs_space_info *sinfo; |
| 3789 | u64 num_bytes; |
| 3790 | u64 meta_used; |
| 3791 | u64 data_used; |
| 3792 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
| 3793 | #if 0 |
| 3794 | /* |
| 3795 | * per tree used space accounting can be inaccuracy, so we |
| 3796 | * can't rely on it. |
| 3797 | */ |
| 3798 | spin_lock(&fs_info->extent_root->accounting_lock); |
| 3799 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item); |
| 3800 | spin_unlock(&fs_info->extent_root->accounting_lock); |
| 3801 | |
| 3802 | spin_lock(&fs_info->csum_root->accounting_lock); |
| 3803 | num_bytes += btrfs_root_used(&fs_info->csum_root->root_item); |
| 3804 | spin_unlock(&fs_info->csum_root->accounting_lock); |
| 3805 | |
| 3806 | spin_lock(&fs_info->tree_root->accounting_lock); |
| 3807 | num_bytes += btrfs_root_used(&fs_info->tree_root->root_item); |
| 3808 | spin_unlock(&fs_info->tree_root->accounting_lock); |
| 3809 | #endif |
| 3810 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3811 | spin_lock(&sinfo->lock); |
| 3812 | data_used = sinfo->bytes_used; |
| 3813 | spin_unlock(&sinfo->lock); |
| 3814 | |
| 3815 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3816 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3817 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 3818 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3819 | meta_used = sinfo->bytes_used; |
| 3820 | spin_unlock(&sinfo->lock); |
| 3821 | |
| 3822 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3823 | csum_size * 2; |
| 3824 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3825 | |
| 3826 | if (num_bytes * 3 > meta_used) |
| 3827 | num_bytes = div64_u64(meta_used, 3); |
| 3828 | |
| 3829 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3830 | } |
| 3831 | |
| 3832 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3833 | { |
| 3834 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3835 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3836 | u64 num_bytes; |
| 3837 | |
| 3838 | num_bytes = calc_global_metadata_size(fs_info); |
| 3839 | |
| 3840 | spin_lock(&block_rsv->lock); |
| 3841 | spin_lock(&sinfo->lock); |
| 3842 | |
| 3843 | block_rsv->size = num_bytes; |
| 3844 | |
| 3845 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3846 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 3847 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3848 | |
| 3849 | if (sinfo->total_bytes > num_bytes) { |
| 3850 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3851 | block_rsv->reserved += num_bytes; |
| 3852 | sinfo->bytes_reserved += num_bytes; |
| 3853 | } |
| 3854 | |
| 3855 | if (block_rsv->reserved >= block_rsv->size) { |
| 3856 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3857 | sinfo->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3858 | sinfo->reservation_progress++; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3859 | block_rsv->reserved = block_rsv->size; |
| 3860 | block_rsv->full = 1; |
| 3861 | } |
| 3862 | #if 0 |
| 3863 | printk(KERN_INFO"global block rsv size %llu reserved %llu\n", |
| 3864 | block_rsv->size, block_rsv->reserved); |
| 3865 | #endif |
| 3866 | spin_unlock(&sinfo->lock); |
| 3867 | spin_unlock(&block_rsv->lock); |
| 3868 | } |
| 3869 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3870 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3871 | { |
| 3872 | struct btrfs_space_info *space_info; |
| 3873 | |
| 3874 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3875 | fs_info->chunk_block_rsv.space_info = space_info; |
| 3876 | fs_info->chunk_block_rsv.priority = 10; |
| 3877 | |
| 3878 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3879 | fs_info->global_block_rsv.space_info = space_info; |
| 3880 | fs_info->global_block_rsv.priority = 10; |
| 3881 | fs_info->global_block_rsv.refill_used = 1; |
| 3882 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3883 | fs_info->trans_block_rsv.space_info = space_info; |
| 3884 | fs_info->empty_block_rsv.space_info = space_info; |
| 3885 | fs_info->empty_block_rsv.priority = 10; |
| 3886 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3887 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3888 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3889 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3890 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3891 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3892 | |
| 3893 | btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv); |
| 3894 | |
| 3895 | btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv); |
| 3896 | |
| 3897 | update_global_block_rsv(fs_info); |
| 3898 | } |
| 3899 | |
| 3900 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3901 | { |
| 3902 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3903 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3904 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3905 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3906 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3907 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3908 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3909 | } |
| 3910 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3911 | static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items) |
| 3912 | { |
| 3913 | return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) * |
| 3914 | 3 * num_items; |
| 3915 | } |
| 3916 | |
| 3917 | int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3918 | struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3919 | int num_items) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3920 | { |
| 3921 | u64 num_bytes; |
| 3922 | int ret; |
| 3923 | |
| 3924 | if (num_items == 0 || root->fs_info->chunk_root == root) |
| 3925 | return 0; |
| 3926 | |
| 3927 | num_bytes = calc_trans_metadata_size(root, num_items); |
| 3928 | 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] | 3929 | num_bytes); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3930 | if (!ret) { |
| 3931 | trans->bytes_reserved += num_bytes; |
| 3932 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 3933 | } |
| 3934 | return ret; |
| 3935 | } |
| 3936 | |
| 3937 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 3938 | struct btrfs_root *root) |
| 3939 | { |
| 3940 | if (!trans->bytes_reserved) |
| 3941 | return; |
| 3942 | |
| 3943 | BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv); |
| 3944 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3945 | trans->bytes_reserved); |
| 3946 | trans->bytes_reserved = 0; |
| 3947 | } |
| 3948 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3949 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3950 | struct inode *inode) |
| 3951 | { |
| 3952 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3953 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3954 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 3955 | |
| 3956 | /* |
| 3957 | * one for deleting orphan item, one for updating inode and |
| 3958 | * two for calling btrfs_truncate_inode_items. |
| 3959 | * |
| 3960 | * btrfs_truncate_inode_items is a delete operation, it frees |
| 3961 | * more space than it uses in most cases. So two units of |
| 3962 | * metadata space should be enough for calling it many times. |
| 3963 | * If all of the metadata space is used, we can commit |
| 3964 | * transaction and use space it freed. |
| 3965 | */ |
| 3966 | u64 num_bytes = calc_trans_metadata_size(root, 4); |
| 3967 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3968 | } |
| 3969 | |
| 3970 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 3971 | { |
| 3972 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3973 | u64 num_bytes = calc_trans_metadata_size(root, 4); |
| 3974 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 3975 | } |
| 3976 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3977 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3978 | struct btrfs_pending_snapshot *pending) |
| 3979 | { |
| 3980 | struct btrfs_root *root = pending->root; |
| 3981 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3982 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 3983 | /* |
| 3984 | * two for root back/forward refs, two for directory entries |
| 3985 | * and one for root of the snapshot. |
| 3986 | */ |
| 3987 | u64 num_bytes = calc_trans_metadata_size(root, 5); |
| 3988 | dst_rsv->space_info = src_rsv->space_info; |
| 3989 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3990 | } |
| 3991 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3992 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes) |
| 3993 | { |
| 3994 | return num_bytes >>= 3; |
| 3995 | } |
| 3996 | |
| 3997 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 3998 | { |
| 3999 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4000 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
| 4001 | u64 to_reserve; |
| 4002 | int nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4003 | int reserved_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4004 | int ret; |
| 4005 | |
| 4006 | if (btrfs_transaction_in_commit(root->fs_info)) |
| 4007 | schedule_timeout(1); |
| 4008 | |
| 4009 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4010 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4011 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4012 | reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents); |
| 4013 | |
| 4014 | if (nr_extents > reserved_extents) { |
| 4015 | nr_extents -= reserved_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4016 | to_reserve = calc_trans_metadata_size(root, nr_extents); |
| 4017 | } else { |
| 4018 | nr_extents = 0; |
| 4019 | to_reserve = 0; |
| 4020 | } |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4021 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4022 | to_reserve += calc_csum_metadata_size(inode, num_bytes); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4023 | ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1); |
| 4024 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4025 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4026 | |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4027 | atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4028 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4029 | |
| 4030 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 4031 | |
| 4032 | if (block_rsv->size > 512 * 1024 * 1024) |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4033 | shrink_delalloc(NULL, root, to_reserve, 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4034 | |
| 4035 | return 0; |
| 4036 | } |
| 4037 | |
| 4038 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 4039 | { |
| 4040 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4041 | u64 to_free; |
| 4042 | int nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4043 | int reserved_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4044 | |
| 4045 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
| 4046 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 4047 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4048 | |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4049 | reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents); |
| 4050 | do { |
| 4051 | int old, new; |
| 4052 | |
| 4053 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents); |
| 4054 | if (nr_extents >= reserved_extents) { |
| 4055 | nr_extents = 0; |
| 4056 | break; |
| 4057 | } |
| 4058 | old = reserved_extents; |
| 4059 | nr_extents = reserved_extents - nr_extents; |
| 4060 | new = reserved_extents - nr_extents; |
| 4061 | old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents, |
| 4062 | reserved_extents, new); |
| 4063 | if (likely(old == reserved_extents)) |
| 4064 | break; |
| 4065 | reserved_extents = old; |
| 4066 | } while (1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4067 | |
| 4068 | to_free = calc_csum_metadata_size(inode, num_bytes); |
| 4069 | if (nr_extents > 0) |
| 4070 | to_free += calc_trans_metadata_size(root, nr_extents); |
| 4071 | |
| 4072 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 4073 | to_free); |
| 4074 | } |
| 4075 | |
| 4076 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 4077 | { |
| 4078 | int ret; |
| 4079 | |
| 4080 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 4081 | if (ret) |
| 4082 | return ret; |
| 4083 | |
| 4084 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 4085 | if (ret) { |
| 4086 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4087 | return ret; |
| 4088 | } |
| 4089 | |
| 4090 | return 0; |
| 4091 | } |
| 4092 | |
| 4093 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 4094 | { |
| 4095 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 4096 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4097 | } |
| 4098 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4099 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 4100 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4101 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4102 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4103 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4104 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4105 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4106 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4107 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4108 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 4109 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4110 | /* block accounting for super block */ |
| 4111 | spin_lock(&info->delalloc_lock); |
| 4112 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 4113 | if (alloc) |
| 4114 | old_val += num_bytes; |
| 4115 | else |
| 4116 | old_val -= num_bytes; |
| 4117 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4118 | spin_unlock(&info->delalloc_lock); |
| 4119 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4120 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4121 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4122 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4123 | return -1; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4124 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 4125 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4126 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4127 | factor = 2; |
| 4128 | else |
| 4129 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4130 | /* |
| 4131 | * If this block group has free space cache written out, we |
| 4132 | * need to make sure to load it if we are removing space. This |
| 4133 | * is because we need the unpinning stage to actually add the |
| 4134 | * space back to the block group, otherwise we will leak space. |
| 4135 | */ |
| 4136 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4137 | cache_block_group(cache, trans, NULL, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4138 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4139 | byte_in_group = bytenr - cache->key.objectid; |
| 4140 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4141 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4142 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4143 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4144 | |
| 4145 | if (btrfs_super_cache_generation(&info->super_copy) != 0 && |
| 4146 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 4147 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 4148 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4149 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4150 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4151 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4152 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4153 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4154 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4155 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4156 | cache->space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4157 | cache->space_info->reservation_progress++; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4158 | cache->space_info->bytes_used += num_bytes; |
| 4159 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4160 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4161 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4162 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4163 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4164 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4165 | cache->pinned += num_bytes; |
| 4166 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4167 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4168 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4169 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4170 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4171 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4172 | set_extent_dirty(info->pinned_extents, |
| 4173 | bytenr, bytenr + num_bytes - 1, |
| 4174 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4175 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4176 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4177 | total -= num_bytes; |
| 4178 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4179 | } |
| 4180 | return 0; |
| 4181 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4182 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4183 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 4184 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4185 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4186 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4187 | |
| 4188 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 4189 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4190 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4191 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4192 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4193 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4194 | |
| 4195 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4196 | } |
| 4197 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4198 | static int pin_down_extent(struct btrfs_root *root, |
| 4199 | struct btrfs_block_group_cache *cache, |
| 4200 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4201 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4202 | spin_lock(&cache->space_info->lock); |
| 4203 | spin_lock(&cache->lock); |
| 4204 | cache->pinned += num_bytes; |
| 4205 | cache->space_info->bytes_pinned += num_bytes; |
| 4206 | if (reserved) { |
| 4207 | cache->reserved -= num_bytes; |
| 4208 | cache->space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4209 | cache->space_info->reservation_progress++; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4210 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4211 | spin_unlock(&cache->lock); |
| 4212 | spin_unlock(&cache->space_info->lock); |
| 4213 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4214 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 4215 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4216 | return 0; |
| 4217 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4218 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4219 | /* |
| 4220 | * this function must be called within transaction |
| 4221 | */ |
| 4222 | int btrfs_pin_extent(struct btrfs_root *root, |
| 4223 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4224 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4225 | struct btrfs_block_group_cache *cache; |
| 4226 | |
| 4227 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 4228 | BUG_ON(!cache); |
| 4229 | |
| 4230 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 4231 | |
| 4232 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4233 | return 0; |
| 4234 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4235 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4236 | /* |
| 4237 | * update size of reserved extents. this function may return -EAGAIN |
| 4238 | * if 'reserve' is true or 'sinfo' is false. |
| 4239 | */ |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4240 | int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4241 | u64 num_bytes, int reserve, int sinfo) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4242 | { |
| 4243 | int ret = 0; |
| 4244 | if (sinfo) { |
| 4245 | struct btrfs_space_info *space_info = cache->space_info; |
| 4246 | spin_lock(&space_info->lock); |
| 4247 | spin_lock(&cache->lock); |
| 4248 | if (reserve) { |
| 4249 | if (cache->ro) { |
| 4250 | ret = -EAGAIN; |
| 4251 | } else { |
| 4252 | cache->reserved += num_bytes; |
| 4253 | space_info->bytes_reserved += num_bytes; |
| 4254 | } |
| 4255 | } else { |
| 4256 | if (cache->ro) |
| 4257 | space_info->bytes_readonly += num_bytes; |
| 4258 | cache->reserved -= num_bytes; |
| 4259 | space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4260 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4261 | } |
| 4262 | spin_unlock(&cache->lock); |
| 4263 | spin_unlock(&space_info->lock); |
| 4264 | } else { |
| 4265 | spin_lock(&cache->lock); |
| 4266 | if (cache->ro) { |
| 4267 | ret = -EAGAIN; |
| 4268 | } else { |
| 4269 | if (reserve) |
| 4270 | cache->reserved += num_bytes; |
| 4271 | else |
| 4272 | cache->reserved -= num_bytes; |
| 4273 | } |
| 4274 | spin_unlock(&cache->lock); |
| 4275 | } |
| 4276 | return ret; |
| 4277 | } |
| 4278 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4279 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 4280 | struct btrfs_root *root) |
| 4281 | { |
| 4282 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4283 | struct btrfs_caching_control *next; |
| 4284 | struct btrfs_caching_control *caching_ctl; |
| 4285 | struct btrfs_block_group_cache *cache; |
| 4286 | |
| 4287 | down_write(&fs_info->extent_commit_sem); |
| 4288 | |
| 4289 | list_for_each_entry_safe(caching_ctl, next, |
| 4290 | &fs_info->caching_block_groups, list) { |
| 4291 | cache = caching_ctl->block_group; |
| 4292 | if (block_group_cache_done(cache)) { |
| 4293 | cache->last_byte_to_unpin = (u64)-1; |
| 4294 | list_del_init(&caching_ctl->list); |
| 4295 | put_caching_control(caching_ctl); |
| 4296 | } else { |
| 4297 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4298 | } |
| 4299 | } |
| 4300 | |
| 4301 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4302 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4303 | else |
| 4304 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4305 | |
| 4306 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4307 | |
| 4308 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4309 | return 0; |
| 4310 | } |
| 4311 | |
| 4312 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 4313 | { |
| 4314 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4315 | struct btrfs_block_group_cache *cache = NULL; |
| 4316 | u64 len; |
| 4317 | |
| 4318 | while (start <= end) { |
| 4319 | if (!cache || |
| 4320 | start >= cache->key.objectid + cache->key.offset) { |
| 4321 | if (cache) |
| 4322 | btrfs_put_block_group(cache); |
| 4323 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4324 | BUG_ON(!cache); |
| 4325 | } |
| 4326 | |
| 4327 | len = cache->key.objectid + cache->key.offset - start; |
| 4328 | len = min(len, end + 1 - start); |
| 4329 | |
| 4330 | if (start < cache->last_byte_to_unpin) { |
| 4331 | len = min(len, cache->last_byte_to_unpin - start); |
| 4332 | btrfs_add_free_space(cache, start, len); |
| 4333 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4334 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4335 | start += len; |
| 4336 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4337 | spin_lock(&cache->space_info->lock); |
| 4338 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4339 | cache->pinned -= len; |
| 4340 | cache->space_info->bytes_pinned -= len; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4341 | if (cache->ro) { |
| 4342 | cache->space_info->bytes_readonly += len; |
| 4343 | } else if (cache->reserved_pinned > 0) { |
| 4344 | len = min(len, cache->reserved_pinned); |
| 4345 | cache->reserved_pinned -= len; |
| 4346 | cache->space_info->bytes_reserved += len; |
| 4347 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4348 | spin_unlock(&cache->lock); |
| 4349 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4353 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4354 | return 0; |
| 4355 | } |
| 4356 | |
| 4357 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4358 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4359 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4360 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4361 | struct extent_io_tree *unpin; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4362 | struct btrfs_block_rsv *block_rsv; |
| 4363 | struct btrfs_block_rsv *next_rsv; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4364 | u64 start; |
| 4365 | u64 end; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4366 | int idx; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4367 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4368 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4369 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4370 | unpin = &fs_info->freed_extents[1]; |
| 4371 | else |
| 4372 | unpin = &fs_info->freed_extents[0]; |
| 4373 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4374 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4375 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4376 | EXTENT_DIRTY); |
| 4377 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4378 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4379 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 4380 | if (btrfs_test_opt(root, DISCARD)) |
| 4381 | ret = btrfs_discard_extent(root, start, |
| 4382 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4383 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4384 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4385 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4386 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4387 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4388 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4389 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 4390 | list_for_each_entry_safe(block_rsv, next_rsv, |
| 4391 | &fs_info->durable_block_rsv_list, list) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4392 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4393 | idx = trans->transid & 0x1; |
| 4394 | if (block_rsv->freed[idx] > 0) { |
| 4395 | block_rsv_add_bytes(block_rsv, |
| 4396 | block_rsv->freed[idx], 0); |
| 4397 | block_rsv->freed[idx] = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 4398 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4399 | if (atomic_read(&block_rsv->usage) == 0) { |
| 4400 | btrfs_block_rsv_release(root, block_rsv, (u64)-1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4401 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4402 | if (block_rsv->freed[0] == 0 && |
| 4403 | block_rsv->freed[1] == 0) { |
| 4404 | list_del_init(&block_rsv->list); |
| 4405 | kfree(block_rsv); |
| 4406 | } |
| 4407 | } else { |
| 4408 | btrfs_block_rsv_release(root, block_rsv, 0); |
| 4409 | } |
| 4410 | } |
| 4411 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 4412 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4413 | return 0; |
| 4414 | } |
| 4415 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4416 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4417 | struct btrfs_root *root, |
| 4418 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4419 | u64 root_objectid, u64 owner_objectid, |
| 4420 | u64 owner_offset, int refs_to_drop, |
| 4421 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4422 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4423 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4424 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4425 | struct btrfs_fs_info *info = root->fs_info; |
| 4426 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4427 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4428 | struct btrfs_extent_item *ei; |
| 4429 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4430 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4431 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4432 | int extent_slot = 0; |
| 4433 | int found_extent = 0; |
| 4434 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4435 | u32 item_size; |
| 4436 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4437 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4438 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4439 | if (!path) |
| 4440 | return -ENOMEM; |
| 4441 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4442 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4443 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4444 | |
| 4445 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4446 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4447 | |
| 4448 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4449 | bytenr, num_bytes, parent, |
| 4450 | root_objectid, owner_objectid, |
| 4451 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4452 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4453 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4454 | while (extent_slot >= 0) { |
| 4455 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4456 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4457 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4458 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4459 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4460 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4461 | found_extent = 1; |
| 4462 | break; |
| 4463 | } |
| 4464 | if (path->slots[0] - extent_slot > 5) |
| 4465 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4466 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4467 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4468 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4469 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4470 | if (found_extent && item_size < sizeof(*ei)) |
| 4471 | found_extent = 0; |
| 4472 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4473 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4474 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4475 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4476 | NULL, refs_to_drop, |
| 4477 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4478 | BUG_ON(ret); |
| 4479 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4480 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4481 | |
| 4482 | key.objectid = bytenr; |
| 4483 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4484 | key.offset = num_bytes; |
| 4485 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4486 | ret = btrfs_search_slot(trans, extent_root, |
| 4487 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4488 | if (ret) { |
| 4489 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4490 | ", was looking for %llu\n", ret, |
| 4491 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4492 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4493 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4494 | BUG_ON(ret); |
| 4495 | extent_slot = path->slots[0]; |
| 4496 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4497 | } else { |
| 4498 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4499 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4500 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4501 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4502 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4503 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4504 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4505 | (unsigned long long)owner_objectid, |
| 4506 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4507 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4508 | |
| 4509 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4510 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4511 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4512 | if (item_size < sizeof(*ei)) { |
| 4513 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4514 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4515 | owner_objectid, 0); |
| 4516 | BUG_ON(ret < 0); |
| 4517 | |
| 4518 | btrfs_release_path(extent_root, path); |
| 4519 | path->leave_spinning = 1; |
| 4520 | |
| 4521 | key.objectid = bytenr; |
| 4522 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4523 | key.offset = num_bytes; |
| 4524 | |
| 4525 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4526 | -1, 1); |
| 4527 | if (ret) { |
| 4528 | printk(KERN_ERR "umm, got %d back from search" |
| 4529 | ", was looking for %llu\n", ret, |
| 4530 | (unsigned long long)bytenr); |
| 4531 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4532 | } |
| 4533 | BUG_ON(ret); |
| 4534 | extent_slot = path->slots[0]; |
| 4535 | leaf = path->nodes[0]; |
| 4536 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4537 | } |
| 4538 | #endif |
| 4539 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4540 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4541 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4542 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4543 | struct btrfs_tree_block_info *bi; |
| 4544 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4545 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4546 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4547 | } |
| 4548 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4549 | refs = btrfs_extent_refs(leaf, ei); |
| 4550 | BUG_ON(refs < refs_to_drop); |
| 4551 | refs -= refs_to_drop; |
| 4552 | |
| 4553 | if (refs > 0) { |
| 4554 | if (extent_op) |
| 4555 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4556 | /* |
| 4557 | * In the case of inline back ref, reference count will |
| 4558 | * be updated by remove_extent_backref |
| 4559 | */ |
| 4560 | if (iref) { |
| 4561 | BUG_ON(!found_extent); |
| 4562 | } else { |
| 4563 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4564 | btrfs_mark_buffer_dirty(leaf); |
| 4565 | } |
| 4566 | if (found_extent) { |
| 4567 | ret = remove_extent_backref(trans, extent_root, path, |
| 4568 | iref, refs_to_drop, |
| 4569 | is_data); |
| 4570 | BUG_ON(ret); |
| 4571 | } |
| 4572 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4573 | if (found_extent) { |
| 4574 | BUG_ON(is_data && refs_to_drop != |
| 4575 | extent_data_ref_count(root, path, iref)); |
| 4576 | if (iref) { |
| 4577 | BUG_ON(path->slots[0] != extent_slot); |
| 4578 | } else { |
| 4579 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4580 | path->slots[0] = extent_slot; |
| 4581 | num_to_del = 2; |
| 4582 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4583 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4584 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4585 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4586 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4587 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4588 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4589 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4590 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4591 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4592 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4593 | } else { |
| 4594 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4595 | bytenr >> PAGE_CACHE_SHIFT, |
| 4596 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4597 | } |
| 4598 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4599 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4600 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4601 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4602 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4603 | return ret; |
| 4604 | } |
| 4605 | |
| 4606 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4607 | * 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] | 4608 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4609 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4610 | * removes it from the tree. |
| 4611 | */ |
| 4612 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4613 | struct btrfs_root *root, u64 bytenr) |
| 4614 | { |
| 4615 | struct btrfs_delayed_ref_head *head; |
| 4616 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4617 | struct btrfs_delayed_ref_node *ref; |
| 4618 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4619 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4620 | |
| 4621 | delayed_refs = &trans->transaction->delayed_refs; |
| 4622 | spin_lock(&delayed_refs->lock); |
| 4623 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4624 | if (!head) |
| 4625 | goto out; |
| 4626 | |
| 4627 | node = rb_prev(&head->node.rb_node); |
| 4628 | if (!node) |
| 4629 | goto out; |
| 4630 | |
| 4631 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4632 | |
| 4633 | /* there are still entries for this ref, we can't drop it */ |
| 4634 | if (ref->bytenr == bytenr) |
| 4635 | goto out; |
| 4636 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4637 | if (head->extent_op) { |
| 4638 | if (!head->must_insert_reserved) |
| 4639 | goto out; |
| 4640 | kfree(head->extent_op); |
| 4641 | head->extent_op = NULL; |
| 4642 | } |
| 4643 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4644 | /* |
| 4645 | * waiting for the lock here would deadlock. If someone else has it |
| 4646 | * locked they are already in the process of dropping it anyway |
| 4647 | */ |
| 4648 | if (!mutex_trylock(&head->mutex)) |
| 4649 | goto out; |
| 4650 | |
| 4651 | /* |
| 4652 | * at this point we have a head with no other entries. Go |
| 4653 | * ahead and process it. |
| 4654 | */ |
| 4655 | head->node.in_tree = 0; |
| 4656 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4657 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4658 | delayed_refs->num_entries--; |
| 4659 | |
| 4660 | /* |
| 4661 | * we don't take a ref on the node because we're removing it from the |
| 4662 | * tree, so we just steal the ref the tree was holding. |
| 4663 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4664 | delayed_refs->num_heads--; |
| 4665 | if (list_empty(&head->cluster)) |
| 4666 | delayed_refs->num_heads_ready--; |
| 4667 | |
| 4668 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4669 | spin_unlock(&delayed_refs->lock); |
| 4670 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4671 | BUG_ON(head->extent_op); |
| 4672 | if (head->must_insert_reserved) |
| 4673 | ret = 1; |
| 4674 | |
| 4675 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4676 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4677 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4678 | out: |
| 4679 | spin_unlock(&delayed_refs->lock); |
| 4680 | return 0; |
| 4681 | } |
| 4682 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4683 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4684 | struct btrfs_root *root, |
| 4685 | struct extent_buffer *buf, |
| 4686 | u64 parent, int last_ref) |
| 4687 | { |
| 4688 | struct btrfs_block_rsv *block_rsv; |
| 4689 | struct btrfs_block_group_cache *cache = NULL; |
| 4690 | int ret; |
| 4691 | |
| 4692 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4693 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4694 | parent, root->root_key.objectid, |
| 4695 | btrfs_header_level(buf), |
| 4696 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4697 | BUG_ON(ret); |
| 4698 | } |
| 4699 | |
| 4700 | if (!last_ref) |
| 4701 | return; |
| 4702 | |
| 4703 | block_rsv = get_block_rsv(trans, root); |
| 4704 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | 3bf84a5 | 2010-05-31 09:04:46 +0000 | [diff] [blame] | 4705 | if (block_rsv->space_info != cache->space_info) |
| 4706 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4707 | |
| 4708 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4709 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4710 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4711 | if (!ret) |
| 4712 | goto pin; |
| 4713 | } |
| 4714 | |
| 4715 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4716 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
| 4717 | goto pin; |
| 4718 | } |
| 4719 | |
| 4720 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4721 | |
| 4722 | btrfs_add_free_space(cache, buf->start, buf->len); |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4723 | ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4724 | if (ret == -EAGAIN) { |
| 4725 | /* block group became read-only */ |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4726 | btrfs_update_reserved_bytes(cache, buf->len, 0, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4727 | goto out; |
| 4728 | } |
| 4729 | |
| 4730 | ret = 1; |
| 4731 | spin_lock(&block_rsv->lock); |
| 4732 | if (block_rsv->reserved < block_rsv->size) { |
| 4733 | block_rsv->reserved += buf->len; |
| 4734 | ret = 0; |
| 4735 | } |
| 4736 | spin_unlock(&block_rsv->lock); |
| 4737 | |
| 4738 | if (ret) { |
| 4739 | spin_lock(&cache->space_info->lock); |
| 4740 | cache->space_info->bytes_reserved -= buf->len; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4741 | cache->space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4742 | spin_unlock(&cache->space_info->lock); |
| 4743 | } |
| 4744 | goto out; |
| 4745 | } |
| 4746 | pin: |
| 4747 | if (block_rsv->durable && !cache->ro) { |
| 4748 | ret = 0; |
| 4749 | spin_lock(&cache->lock); |
| 4750 | if (!cache->ro) { |
| 4751 | cache->reserved_pinned += buf->len; |
| 4752 | ret = 1; |
| 4753 | } |
| 4754 | spin_unlock(&cache->lock); |
| 4755 | |
| 4756 | if (ret) { |
| 4757 | spin_lock(&block_rsv->lock); |
| 4758 | block_rsv->freed[trans->transid & 0x1] += buf->len; |
| 4759 | spin_unlock(&block_rsv->lock); |
| 4760 | } |
| 4761 | } |
| 4762 | out: |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 4763 | /* |
| 4764 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 4765 | * anymore. |
| 4766 | */ |
| 4767 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4768 | btrfs_put_block_group(cache); |
| 4769 | } |
| 4770 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4771 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4772 | struct btrfs_root *root, |
| 4773 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4774 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4775 | { |
| 4776 | int ret; |
| 4777 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4778 | /* |
| 4779 | * tree log blocks never actually go into the extent allocation |
| 4780 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4781 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4782 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4783 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4784 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4785 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4786 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4787 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4788 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4789 | parent, root_objectid, (int)owner, |
| 4790 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4791 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4792 | } else { |
| 4793 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4794 | parent, root_objectid, owner, |
| 4795 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4796 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4797 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4798 | return ret; |
| 4799 | } |
| 4800 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4801 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4802 | { |
| 4803 | u64 mask = ((u64)root->stripesize - 1); |
| 4804 | u64 ret = (val + mask) & ~mask; |
| 4805 | return ret; |
| 4806 | } |
| 4807 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4808 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4809 | * when we wait for progress in the block group caching, its because |
| 4810 | * our allocation attempt failed at least once. So, we must sleep |
| 4811 | * and let some progress happen before we try again. |
| 4812 | * |
| 4813 | * This function will sleep at least once waiting for new free space to |
| 4814 | * show up, and then it will check the block group free space numbers |
| 4815 | * for our min num_bytes. Another option is to have it go ahead |
| 4816 | * and look in the rbtree for a free extent of a given size, but this |
| 4817 | * is a good start. |
| 4818 | */ |
| 4819 | static noinline int |
| 4820 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4821 | u64 num_bytes) |
| 4822 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4823 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4824 | DEFINE_WAIT(wait); |
| 4825 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4826 | caching_ctl = get_caching_control(cache); |
| 4827 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4828 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4829 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4830 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4831 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4832 | |
| 4833 | put_caching_control(caching_ctl); |
| 4834 | return 0; |
| 4835 | } |
| 4836 | |
| 4837 | static noinline int |
| 4838 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4839 | { |
| 4840 | struct btrfs_caching_control *caching_ctl; |
| 4841 | DEFINE_WAIT(wait); |
| 4842 | |
| 4843 | caching_ctl = get_caching_control(cache); |
| 4844 | if (!caching_ctl) |
| 4845 | return 0; |
| 4846 | |
| 4847 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4848 | |
| 4849 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4850 | return 0; |
| 4851 | } |
| 4852 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4853 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4854 | { |
| 4855 | int index; |
| 4856 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4857 | index = 0; |
| 4858 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4859 | index = 1; |
| 4860 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4861 | index = 2; |
| 4862 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4863 | index = 3; |
| 4864 | else |
| 4865 | index = 4; |
| 4866 | return index; |
| 4867 | } |
| 4868 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4869 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4870 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4871 | LOOP_CACHING_NOWAIT = 1, |
| 4872 | LOOP_CACHING_WAIT = 2, |
| 4873 | LOOP_ALLOC_CHUNK = 3, |
| 4874 | LOOP_NO_EMPTY_SIZE = 4, |
| 4875 | }; |
| 4876 | |
| 4877 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4878 | * walks the btree of allocated extents and find a hole of a given size. |
| 4879 | * The key ins is changed to record the hole: |
| 4880 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4881 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4882 | * ins->offset == number of blocks |
| 4883 | * Any available blocks before search_start are skipped. |
| 4884 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4885 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4886 | struct btrfs_root *orig_root, |
| 4887 | u64 num_bytes, u64 empty_size, |
| 4888 | u64 search_start, u64 search_end, |
| 4889 | u64 hint_byte, struct btrfs_key *ins, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4890 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4891 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4892 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4893 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4894 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4895 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4896 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4897 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4898 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4899 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4900 | int last_ptr_loop = 0; |
| 4901 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4902 | int index = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4903 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4904 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4905 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4906 | bool use_cluster = true; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4907 | u64 ideal_cache_percent = 0; |
| 4908 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4909 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4910 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4911 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4912 | ins->objectid = 0; |
| 4913 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4914 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4915 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4916 | if (!space_info) { |
| 4917 | printk(KERN_ERR "No space info for %d\n", data); |
| 4918 | return -ENOSPC; |
| 4919 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4920 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4921 | /* |
| 4922 | * If the space info is for both data and metadata it means we have a |
| 4923 | * small filesystem and we can't use the clustering stuff. |
| 4924 | */ |
| 4925 | if (btrfs_mixed_space_info(space_info)) |
| 4926 | use_cluster = false; |
| 4927 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4928 | if (orig_root->ref_cows || empty_size) |
| 4929 | allowed_chunk_alloc = 1; |
| 4930 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4931 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4932 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4933 | if (!btrfs_test_opt(root, SSD)) |
| 4934 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4935 | } |
| 4936 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4937 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
| 4938 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4939 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4940 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4941 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4942 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4943 | spin_lock(&last_ptr->lock); |
| 4944 | if (last_ptr->block_group) |
| 4945 | hint_byte = last_ptr->window_start; |
| 4946 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4947 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4948 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4949 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4950 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4951 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4952 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4953 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4954 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4955 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4956 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4957 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4958 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4959 | /* |
| 4960 | * we don't want to use the block group if it doesn't match our |
| 4961 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4962 | * |
| 4963 | * However if we are re-searching with an ideal block group |
| 4964 | * 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] | 4965 | */ |
| 4966 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4967 | (block_group->cached != BTRFS_CACHE_NO || |
| 4968 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4969 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4970 | if (list_empty(&block_group->list) || |
| 4971 | block_group->ro) { |
| 4972 | /* |
| 4973 | * someone is removing this block group, |
| 4974 | * we can't jump into the have_block_group |
| 4975 | * target because our list pointers are not |
| 4976 | * valid |
| 4977 | */ |
| 4978 | btrfs_put_block_group(block_group); |
| 4979 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4980 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4981 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4982 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4983 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4984 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4985 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4986 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4987 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4988 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4989 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4990 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 4991 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4992 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4993 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4994 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4995 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4996 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4997 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4998 | /* |
| 4999 | * this can happen if we end up cycling through all the |
| 5000 | * raid types, but we want to make sure we only allocate |
| 5001 | * for the proper type. |
| 5002 | */ |
| 5003 | if (!block_group_bits(block_group, data)) { |
| 5004 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 5005 | BTRFS_BLOCK_GROUP_RAID1 | |
| 5006 | BTRFS_BLOCK_GROUP_RAID10; |
| 5007 | |
| 5008 | /* |
| 5009 | * if they asked for extra copies and this block group |
| 5010 | * doesn't provide them, bail. This does allow us to |
| 5011 | * fill raid0 from raid1. |
| 5012 | */ |
| 5013 | if ((data & extra) && !(block_group->flags & extra)) |
| 5014 | goto loop; |
| 5015 | } |
| 5016 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5017 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5018 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5019 | u64 free_percent; |
| 5020 | |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5021 | ret = cache_block_group(block_group, trans, |
| 5022 | orig_root, 1); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 5023 | if (block_group->cached == BTRFS_CACHE_FINISHED) |
| 5024 | goto have_block_group; |
| 5025 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5026 | free_percent = btrfs_block_group_used(&block_group->item); |
| 5027 | free_percent *= 100; |
| 5028 | free_percent = div64_u64(free_percent, |
| 5029 | block_group->key.offset); |
| 5030 | free_percent = 100 - free_percent; |
| 5031 | if (free_percent > ideal_cache_percent && |
| 5032 | likely(!block_group->ro)) { |
| 5033 | ideal_cache_offset = block_group->key.objectid; |
| 5034 | ideal_cache_percent = free_percent; |
| 5035 | } |
| 5036 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5037 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5038 | * We only want to start kthread caching if we are at |
| 5039 | * the point where we will wait for caching to make |
| 5040 | * progress, or if our ideal search is over and we've |
| 5041 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5042 | */ |
| 5043 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5044 | (loop > LOOP_FIND_IDEAL && |
| 5045 | atomic_read(&space_info->caching_threads) < 2)) { |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5046 | ret = cache_block_group(block_group, trans, |
| 5047 | orig_root, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5048 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5049 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5050 | found_uncached_bg = true; |
| 5051 | |
| 5052 | /* |
| 5053 | * If loop is set for cached only, try the next block |
| 5054 | * group. |
| 5055 | */ |
| 5056 | if (loop == LOOP_FIND_IDEAL) |
| 5057 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5058 | } |
| 5059 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5060 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5061 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5062 | found_uncached_bg = true; |
| 5063 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5064 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5065 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5066 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5067 | /* |
| 5068 | * Ok we want to try and use the cluster allocator, so lets look |
| 5069 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 5070 | * have tried the cluster allocator plenty of times at this |
| 5071 | * point and not have found anything, so we are likely way too |
| 5072 | * fragmented for the clustering stuff to find anything, so lets |
| 5073 | * just skip it and let the allocator find whatever block it can |
| 5074 | * find |
| 5075 | */ |
| 5076 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5077 | /* |
| 5078 | * the refill lock keeps out other |
| 5079 | * people trying to start a new cluster |
| 5080 | */ |
| 5081 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5082 | if (last_ptr->block_group && |
| 5083 | (last_ptr->block_group->ro || |
| 5084 | !block_group_bits(last_ptr->block_group, data))) { |
| 5085 | offset = 0; |
| 5086 | goto refill_cluster; |
| 5087 | } |
| 5088 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5089 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 5090 | num_bytes, search_start); |
| 5091 | if (offset) { |
| 5092 | /* we have a block, we're done */ |
| 5093 | spin_unlock(&last_ptr->refill_lock); |
| 5094 | goto checks; |
| 5095 | } |
| 5096 | |
| 5097 | spin_lock(&last_ptr->lock); |
| 5098 | /* |
| 5099 | * whoops, this cluster doesn't actually point to |
| 5100 | * this block group. Get a ref on the block |
| 5101 | * group is does point to and try again |
| 5102 | */ |
| 5103 | if (!last_ptr_loop && last_ptr->block_group && |
| 5104 | last_ptr->block_group != block_group) { |
| 5105 | |
| 5106 | btrfs_put_block_group(block_group); |
| 5107 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5108 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5109 | spin_unlock(&last_ptr->lock); |
| 5110 | spin_unlock(&last_ptr->refill_lock); |
| 5111 | |
| 5112 | last_ptr_loop = 1; |
| 5113 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5114 | /* |
| 5115 | * we know this block group is properly |
| 5116 | * in the list because |
| 5117 | * btrfs_remove_block_group, drops the |
| 5118 | * cluster before it removes the block |
| 5119 | * group from the list |
| 5120 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5121 | goto have_block_group; |
| 5122 | } |
| 5123 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5124 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5125 | /* |
| 5126 | * this cluster didn't work out, free it and |
| 5127 | * start over |
| 5128 | */ |
| 5129 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5130 | |
| 5131 | last_ptr_loop = 0; |
| 5132 | |
| 5133 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 5134 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5135 | block_group, last_ptr, |
| 5136 | offset, num_bytes, |
| 5137 | empty_cluster + empty_size); |
| 5138 | if (ret == 0) { |
| 5139 | /* |
| 5140 | * now pull our allocation out of this |
| 5141 | * cluster |
| 5142 | */ |
| 5143 | offset = btrfs_alloc_from_cluster(block_group, |
| 5144 | last_ptr, num_bytes, |
| 5145 | search_start); |
| 5146 | if (offset) { |
| 5147 | /* we found one, proceed */ |
| 5148 | spin_unlock(&last_ptr->refill_lock); |
| 5149 | goto checks; |
| 5150 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5151 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 5152 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5153 | spin_unlock(&last_ptr->refill_lock); |
| 5154 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5155 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5156 | wait_block_group_cache_progress(block_group, |
| 5157 | num_bytes + empty_cluster + empty_size); |
| 5158 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5159 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5160 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5161 | /* |
| 5162 | * at this point we either didn't find a cluster |
| 5163 | * or we weren't able to allocate a block from our |
| 5164 | * cluster. Free the cluster we've been trying |
| 5165 | * to use, and go to the next block group |
| 5166 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5167 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5168 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5169 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5170 | } |
| 5171 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5172 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 5173 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5174 | /* |
| 5175 | * If we didn't find a chunk, and we haven't failed on this |
| 5176 | * block group before, and this block group is in the middle of |
| 5177 | * caching and we are ok with waiting, then go ahead and wait |
| 5178 | * for progress to be made, and set failed_alloc to true. |
| 5179 | * |
| 5180 | * If failed_alloc is true then we've already waited on this |
| 5181 | * block group once and should move on to the next block group. |
| 5182 | */ |
| 5183 | if (!offset && !failed_alloc && !cached && |
| 5184 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5185 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5186 | num_bytes + empty_size); |
| 5187 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5188 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5189 | } else if (!offset) { |
| 5190 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5191 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5192 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5193 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5194 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5195 | if (search_start + num_bytes >= search_end) { |
| 5196 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5197 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5198 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 5199 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5200 | /* move on to the next group */ |
| 5201 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5202 | block_group->key.objectid + block_group->key.offset) { |
| 5203 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5204 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5205 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5206 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5207 | ins->objectid = search_start; |
| 5208 | ins->offset = num_bytes; |
| 5209 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5210 | if (offset < search_start) |
| 5211 | btrfs_add_free_space(block_group, offset, |
| 5212 | search_start - offset); |
| 5213 | BUG_ON(offset > search_start); |
| 5214 | |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5215 | ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5216 | (data & BTRFS_BLOCK_GROUP_DATA)); |
| 5217 | if (ret == -EAGAIN) { |
| 5218 | btrfs_add_free_space(block_group, offset, num_bytes); |
| 5219 | goto loop; |
| 5220 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5221 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5222 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5223 | ins->objectid = search_start; |
| 5224 | ins->offset = num_bytes; |
| 5225 | |
| 5226 | if (offset < search_start) |
| 5227 | btrfs_add_free_space(block_group, offset, |
| 5228 | search_start - offset); |
| 5229 | BUG_ON(offset > search_start); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5230 | break; |
| 5231 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5232 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5233 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5234 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5235 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5236 | } |
| 5237 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 5238 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5239 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 5240 | goto search; |
| 5241 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5242 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 5243 | * for them to make caching progress. Also |
| 5244 | * determine the best possible bg to cache |
| 5245 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 5246 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5247 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 5248 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 5249 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 5250 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5251 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5252 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 5253 | (found_uncached_bg || empty_size || empty_cluster || |
| 5254 | allowed_chunk_alloc)) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5255 | index = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5256 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5257 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5258 | loop++; |
| 5259 | if (!ideal_cache_percent && |
| 5260 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5261 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5262 | |
| 5263 | /* |
| 5264 | * 1 of the following 2 things have happened so far |
| 5265 | * |
| 5266 | * 1) We found an ideal block group for caching that |
| 5267 | * is mostly full and will cache quickly, so we might |
| 5268 | * as well wait for it. |
| 5269 | * |
| 5270 | * 2) We searched for cached only and we didn't find |
| 5271 | * anything, and we didn't start any caching kthreads |
| 5272 | * either, so chances are we will loop through and |
| 5273 | * start a couple caching kthreads, and then come back |
| 5274 | * around and just wait for them. This will be slower |
| 5275 | * because we will have 2 caching kthreads reading at |
| 5276 | * the same time when we could have just started one |
| 5277 | * and waited for it to get far enough to give us an |
| 5278 | * allocation, so go ahead and go to the wait caching |
| 5279 | * loop. |
| 5280 | */ |
| 5281 | loop = LOOP_CACHING_WAIT; |
| 5282 | search_start = ideal_cache_offset; |
| 5283 | ideal_cache_percent = 0; |
| 5284 | goto ideal_cache; |
| 5285 | } else if (loop == LOOP_FIND_IDEAL) { |
| 5286 | /* |
| 5287 | * Didn't find a uncached bg, wait on anything we find |
| 5288 | * next. |
| 5289 | */ |
| 5290 | loop = LOOP_CACHING_WAIT; |
| 5291 | goto search; |
| 5292 | } |
| 5293 | |
| 5294 | if (loop < LOOP_CACHING_WAIT) { |
| 5295 | loop++; |
| 5296 | goto search; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5300 | empty_size = 0; |
| 5301 | empty_cluster = 0; |
| 5302 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5303 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5304 | if (allowed_chunk_alloc) { |
| 5305 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 5306 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5307 | allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5308 | done_chunk_alloc = 1; |
| 5309 | } else if (!done_chunk_alloc) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5310 | space_info->force_alloc = 1; |
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 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5313 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5314 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5315 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5316 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5317 | ret = -ENOSPC; |
| 5318 | } else if (!ins->objectid) { |
| 5319 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 5320 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5321 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5322 | /* we found what we needed */ |
| 5323 | if (ins->objectid) { |
| 5324 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5325 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5326 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5327 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5328 | ret = 0; |
| 5329 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5330 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5331 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5332 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5333 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5334 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 5335 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5336 | { |
| 5337 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5338 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5339 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5340 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5341 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 5342 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5343 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5344 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5345 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5346 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 5347 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 5348 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5349 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5350 | (unsigned long long)info->bytes_pinned, |
| 5351 | (unsigned long long)info->bytes_reserved, |
| 5352 | (unsigned long long)info->bytes_may_use, |
| 5353 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5354 | spin_unlock(&info->lock); |
| 5355 | |
| 5356 | if (!dump_block_groups) |
| 5357 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5358 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5359 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5360 | again: |
| 5361 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5362 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5363 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 5364 | "%llu pinned %llu reserved\n", |
| 5365 | (unsigned long long)cache->key.objectid, |
| 5366 | (unsigned long long)cache->key.offset, |
| 5367 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 5368 | (unsigned long long)cache->pinned, |
| 5369 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5370 | btrfs_dump_free_space(cache, bytes); |
| 5371 | spin_unlock(&cache->lock); |
| 5372 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5373 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5374 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5375 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5376 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5377 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5378 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5379 | struct btrfs_root *root, |
| 5380 | u64 num_bytes, u64 min_alloc_size, |
| 5381 | u64 empty_size, u64 hint_byte, |
| 5382 | u64 search_end, struct btrfs_key *ins, |
| 5383 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5384 | { |
| 5385 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5386 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5387 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5388 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5389 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5390 | /* |
| 5391 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5392 | * is not called recursively on allocations |
| 5393 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5394 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5395 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5396 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5397 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5398 | WARN_ON(num_bytes < root->sectorsize); |
| 5399 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5400 | search_start, search_end, hint_byte, |
| 5401 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5402 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5403 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5404 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5405 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5406 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5407 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5408 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5409 | goto again; |
| 5410 | } |
Chris Mason | 9143565 | 2011-02-16 13:10:41 -0500 | [diff] [blame] | 5411 | if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5412 | struct btrfs_space_info *sinfo; |
| 5413 | |
| 5414 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5415 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5416 | "wanted %llu\n", (unsigned long long)data, |
| 5417 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5418 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5419 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5420 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5421 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
| 5422 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5423 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5424 | } |
| 5425 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5426 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 5427 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5428 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5429 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5430 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5431 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5432 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5433 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5434 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5435 | return -ENOSPC; |
| 5436 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5437 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5438 | if (btrfs_test_opt(root, DISCARD)) |
| 5439 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5440 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5441 | btrfs_add_free_space(cache, start, len); |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5442 | btrfs_update_reserved_bytes(cache, len, 0, 1); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5443 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5444 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5445 | trace_btrfs_reserved_extent_free(root, start, len); |
| 5446 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5447 | return ret; |
| 5448 | } |
| 5449 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5450 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5451 | struct btrfs_root *root, |
| 5452 | u64 parent, u64 root_objectid, |
| 5453 | u64 flags, u64 owner, u64 offset, |
| 5454 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5455 | { |
| 5456 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5457 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5458 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5459 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5460 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5461 | struct extent_buffer *leaf; |
| 5462 | int type; |
| 5463 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5464 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5465 | if (parent > 0) |
| 5466 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5467 | else |
| 5468 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5469 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5470 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5471 | |
| 5472 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5473 | if (!path) |
| 5474 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5475 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5476 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5477 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5478 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5479 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5480 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5481 | leaf = path->nodes[0]; |
| 5482 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5483 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5484 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5485 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5486 | btrfs_set_extent_flags(leaf, extent_item, |
| 5487 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5488 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5489 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5490 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5491 | if (parent > 0) { |
| 5492 | struct btrfs_shared_data_ref *ref; |
| 5493 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5494 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5495 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5496 | } else { |
| 5497 | struct btrfs_extent_data_ref *ref; |
| 5498 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5499 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5500 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5501 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5502 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5503 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5504 | |
| 5505 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5506 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5507 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5508 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5509 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5510 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5511 | "%llu\n", (unsigned long long)ins->objectid, |
| 5512 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5513 | BUG(); |
| 5514 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5515 | return ret; |
| 5516 | } |
| 5517 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5518 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5519 | struct btrfs_root *root, |
| 5520 | u64 parent, u64 root_objectid, |
| 5521 | u64 flags, struct btrfs_disk_key *key, |
| 5522 | int level, struct btrfs_key *ins) |
| 5523 | { |
| 5524 | int ret; |
| 5525 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5526 | struct btrfs_extent_item *extent_item; |
| 5527 | struct btrfs_tree_block_info *block_info; |
| 5528 | struct btrfs_extent_inline_ref *iref; |
| 5529 | struct btrfs_path *path; |
| 5530 | struct extent_buffer *leaf; |
| 5531 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5532 | |
| 5533 | path = btrfs_alloc_path(); |
| 5534 | BUG_ON(!path); |
| 5535 | |
| 5536 | path->leave_spinning = 1; |
| 5537 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5538 | ins, size); |
| 5539 | BUG_ON(ret); |
| 5540 | |
| 5541 | leaf = path->nodes[0]; |
| 5542 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5543 | struct btrfs_extent_item); |
| 5544 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5545 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5546 | btrfs_set_extent_flags(leaf, extent_item, |
| 5547 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5548 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5549 | |
| 5550 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5551 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5552 | |
| 5553 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5554 | if (parent > 0) { |
| 5555 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5556 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5557 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5558 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5559 | } else { |
| 5560 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5561 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5562 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5563 | } |
| 5564 | |
| 5565 | btrfs_mark_buffer_dirty(leaf); |
| 5566 | btrfs_free_path(path); |
| 5567 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5568 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5569 | if (ret) { |
| 5570 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5571 | "%llu\n", (unsigned long long)ins->objectid, |
| 5572 | (unsigned long long)ins->offset); |
| 5573 | BUG(); |
| 5574 | } |
| 5575 | return ret; |
| 5576 | } |
| 5577 | |
| 5578 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5579 | struct btrfs_root *root, |
| 5580 | u64 root_objectid, u64 owner, |
| 5581 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5582 | { |
| 5583 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5584 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5585 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5586 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5587 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5588 | 0, root_objectid, owner, offset, |
| 5589 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5590 | return ret; |
| 5591 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5592 | |
| 5593 | /* |
| 5594 | * this is used by the tree logging recovery code. It records that |
| 5595 | * an extent has been allocated and makes sure to clear the free |
| 5596 | * space cache bits as well |
| 5597 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5598 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5599 | struct btrfs_root *root, |
| 5600 | u64 root_objectid, u64 owner, u64 offset, |
| 5601 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5602 | { |
| 5603 | int ret; |
| 5604 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5605 | struct btrfs_caching_control *caching_ctl; |
| 5606 | u64 start = ins->objectid; |
| 5607 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5608 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5609 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5610 | cache_block_group(block_group, trans, NULL, 0); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5611 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5612 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5613 | if (!caching_ctl) { |
| 5614 | BUG_ON(!block_group_cache_done(block_group)); |
| 5615 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5616 | BUG_ON(ret); |
| 5617 | } else { |
| 5618 | mutex_lock(&caching_ctl->mutex); |
| 5619 | |
| 5620 | if (start >= caching_ctl->progress) { |
| 5621 | ret = add_excluded_extent(root, start, num_bytes); |
| 5622 | BUG_ON(ret); |
| 5623 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5624 | ret = btrfs_remove_free_space(block_group, |
| 5625 | start, num_bytes); |
| 5626 | BUG_ON(ret); |
| 5627 | } else { |
| 5628 | num_bytes = caching_ctl->progress - start; |
| 5629 | ret = btrfs_remove_free_space(block_group, |
| 5630 | start, num_bytes); |
| 5631 | BUG_ON(ret); |
| 5632 | |
| 5633 | start = caching_ctl->progress; |
| 5634 | num_bytes = ins->objectid + ins->offset - |
| 5635 | caching_ctl->progress; |
| 5636 | ret = add_excluded_extent(root, start, num_bytes); |
| 5637 | BUG_ON(ret); |
| 5638 | } |
| 5639 | |
| 5640 | mutex_unlock(&caching_ctl->mutex); |
| 5641 | put_caching_control(caching_ctl); |
| 5642 | } |
| 5643 | |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5644 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5645 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5646 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5647 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5648 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5649 | return ret; |
| 5650 | } |
| 5651 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5652 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5653 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5654 | u64 bytenr, u32 blocksize, |
| 5655 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5656 | { |
| 5657 | struct extent_buffer *buf; |
| 5658 | |
| 5659 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5660 | if (!buf) |
| 5661 | return ERR_PTR(-ENOMEM); |
| 5662 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5663 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5664 | btrfs_tree_lock(buf); |
| 5665 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5666 | |
| 5667 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5668 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5669 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5670 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5671 | /* |
| 5672 | * we allow two log transactions at a time, use different |
| 5673 | * EXENT bit to differentiate dirty pages. |
| 5674 | */ |
| 5675 | if (root->log_transid % 2 == 0) |
| 5676 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5677 | buf->start + buf->len - 1, GFP_NOFS); |
| 5678 | else |
| 5679 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5680 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5681 | } else { |
| 5682 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5683 | buf->start + buf->len - 1, GFP_NOFS); |
| 5684 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5685 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5686 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5687 | return buf; |
| 5688 | } |
| 5689 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5690 | static struct btrfs_block_rsv * |
| 5691 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5692 | struct btrfs_root *root, u32 blocksize) |
| 5693 | { |
| 5694 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5695 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5696 | int ret; |
| 5697 | |
| 5698 | block_rsv = get_block_rsv(trans, root); |
| 5699 | |
| 5700 | if (block_rsv->size == 0) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5701 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 5702 | blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5703 | /* |
| 5704 | * If we couldn't reserve metadata bytes try and use some from |
| 5705 | * the global reserve. |
| 5706 | */ |
| 5707 | if (ret && block_rsv != global_rsv) { |
| 5708 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5709 | if (!ret) |
| 5710 | return global_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5711 | return ERR_PTR(ret); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5712 | } else if (ret) { |
| 5713 | return ERR_PTR(ret); |
| 5714 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5715 | return block_rsv; |
| 5716 | } |
| 5717 | |
| 5718 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5719 | if (!ret) |
| 5720 | return block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5721 | if (ret) { |
| 5722 | WARN_ON(1); |
| 5723 | ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize, |
| 5724 | 0); |
| 5725 | if (!ret) { |
| 5726 | spin_lock(&block_rsv->lock); |
| 5727 | block_rsv->size += blocksize; |
| 5728 | spin_unlock(&block_rsv->lock); |
| 5729 | return block_rsv; |
| 5730 | } else if (ret && block_rsv != global_rsv) { |
| 5731 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5732 | if (!ret) |
| 5733 | return global_rsv; |
| 5734 | } |
| 5735 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5736 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5737 | return ERR_PTR(-ENOSPC); |
| 5738 | } |
| 5739 | |
| 5740 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5741 | { |
| 5742 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5743 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5744 | } |
| 5745 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5746 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5747 | * finds a free extent and does all the dirty work required for allocation |
| 5748 | * returns the key for the extent through ins, and a tree buffer for |
| 5749 | * the first block of the extent through buf. |
| 5750 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5751 | * returns the tree buffer or NULL. |
| 5752 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5753 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5754 | struct btrfs_root *root, u32 blocksize, |
| 5755 | u64 parent, u64 root_objectid, |
| 5756 | struct btrfs_disk_key *key, int level, |
| 5757 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5758 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5759 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5760 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5761 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5762 | u64 flags = 0; |
| 5763 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5764 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5765 | |
| 5766 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5767 | if (IS_ERR(block_rsv)) |
| 5768 | return ERR_CAST(block_rsv); |
| 5769 | |
| 5770 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5771 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5772 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5773 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5774 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5775 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5776 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5777 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5778 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5779 | BUG_ON(IS_ERR(buf)); |
| 5780 | |
| 5781 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5782 | if (parent == 0) |
| 5783 | parent = ins.objectid; |
| 5784 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5785 | } else |
| 5786 | BUG_ON(parent > 0); |
| 5787 | |
| 5788 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5789 | struct btrfs_delayed_extent_op *extent_op; |
| 5790 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5791 | BUG_ON(!extent_op); |
| 5792 | if (key) |
| 5793 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5794 | else |
| 5795 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5796 | extent_op->flags_to_set = flags; |
| 5797 | extent_op->update_key = 1; |
| 5798 | extent_op->update_flags = 1; |
| 5799 | extent_op->is_data = 0; |
| 5800 | |
| 5801 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5802 | ins.offset, parent, root_objectid, |
| 5803 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5804 | extent_op); |
| 5805 | BUG_ON(ret); |
| 5806 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5807 | return buf; |
| 5808 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5809 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5810 | struct walk_control { |
| 5811 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5812 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5813 | struct btrfs_key update_progress; |
| 5814 | int stage; |
| 5815 | int level; |
| 5816 | int shared_level; |
| 5817 | int update_ref; |
| 5818 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5819 | int reada_slot; |
| 5820 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5821 | }; |
| 5822 | |
| 5823 | #define DROP_REFERENCE 1 |
| 5824 | #define UPDATE_BACKREF 2 |
| 5825 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5826 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5827 | struct btrfs_root *root, |
| 5828 | struct walk_control *wc, |
| 5829 | struct btrfs_path *path) |
| 5830 | { |
| 5831 | u64 bytenr; |
| 5832 | u64 generation; |
| 5833 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5834 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5835 | u32 nritems; |
| 5836 | u32 blocksize; |
| 5837 | struct btrfs_key key; |
| 5838 | struct extent_buffer *eb; |
| 5839 | int ret; |
| 5840 | int slot; |
| 5841 | int nread = 0; |
| 5842 | |
| 5843 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5844 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5845 | wc->reada_count = max(wc->reada_count, 2); |
| 5846 | } else { |
| 5847 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5848 | wc->reada_count = min_t(int, wc->reada_count, |
| 5849 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5850 | } |
| 5851 | |
| 5852 | eb = path->nodes[wc->level]; |
| 5853 | nritems = btrfs_header_nritems(eb); |
| 5854 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5855 | |
| 5856 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5857 | if (nread >= wc->reada_count) |
| 5858 | break; |
| 5859 | |
| 5860 | cond_resched(); |
| 5861 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5862 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5863 | |
| 5864 | if (slot == path->slots[wc->level]) |
| 5865 | goto reada; |
| 5866 | |
| 5867 | if (wc->stage == UPDATE_BACKREF && |
| 5868 | generation <= root->root_key.offset) |
| 5869 | continue; |
| 5870 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5871 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5872 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5873 | &refs, &flags); |
| 5874 | BUG_ON(ret); |
| 5875 | BUG_ON(refs == 0); |
| 5876 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5877 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5878 | if (refs == 1) |
| 5879 | goto reada; |
| 5880 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5881 | if (wc->level == 1 && |
| 5882 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5883 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5884 | if (!wc->update_ref || |
| 5885 | generation <= root->root_key.offset) |
| 5886 | continue; |
| 5887 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5888 | ret = btrfs_comp_cpu_keys(&key, |
| 5889 | &wc->update_progress); |
| 5890 | if (ret < 0) |
| 5891 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5892 | } else { |
| 5893 | if (wc->level == 1 && |
| 5894 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5895 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5896 | } |
| 5897 | reada: |
| 5898 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5899 | generation); |
| 5900 | if (ret) |
| 5901 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5902 | nread++; |
| 5903 | } |
| 5904 | wc->reada_slot = slot; |
| 5905 | } |
| 5906 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5907 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5908 | * hepler to process tree block while walking down the tree. |
| 5909 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5910 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5911 | * back refs for pointers in the block. |
| 5912 | * |
| 5913 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5914 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5915 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5916 | struct btrfs_root *root, |
| 5917 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5918 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5919 | { |
| 5920 | int level = wc->level; |
| 5921 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5922 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5923 | int ret; |
| 5924 | |
| 5925 | if (wc->stage == UPDATE_BACKREF && |
| 5926 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5927 | return 1; |
| 5928 | |
| 5929 | /* |
| 5930 | * when reference count of tree block is 1, it won't increase |
| 5931 | * again. once full backref flag is set, we never clear it. |
| 5932 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5933 | if (lookup_info && |
| 5934 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5935 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5936 | BUG_ON(!path->locks[level]); |
| 5937 | ret = btrfs_lookup_extent_info(trans, root, |
| 5938 | eb->start, eb->len, |
| 5939 | &wc->refs[level], |
| 5940 | &wc->flags[level]); |
| 5941 | BUG_ON(ret); |
| 5942 | BUG_ON(wc->refs[level] == 0); |
| 5943 | } |
| 5944 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5945 | if (wc->stage == DROP_REFERENCE) { |
| 5946 | if (wc->refs[level] > 1) |
| 5947 | return 1; |
| 5948 | |
| 5949 | if (path->locks[level] && !wc->keep_locks) { |
| 5950 | btrfs_tree_unlock(eb); |
| 5951 | path->locks[level] = 0; |
| 5952 | } |
| 5953 | return 0; |
| 5954 | } |
| 5955 | |
| 5956 | /* wc->stage == UPDATE_BACKREF */ |
| 5957 | if (!(wc->flags[level] & flag)) { |
| 5958 | BUG_ON(!path->locks[level]); |
| 5959 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5960 | BUG_ON(ret); |
| 5961 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5962 | BUG_ON(ret); |
| 5963 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5964 | eb->len, flag, 0); |
| 5965 | BUG_ON(ret); |
| 5966 | wc->flags[level] |= flag; |
| 5967 | } |
| 5968 | |
| 5969 | /* |
| 5970 | * the block is shared by multiple trees, so it's not good to |
| 5971 | * keep the tree lock |
| 5972 | */ |
| 5973 | if (path->locks[level] && level > 0) { |
| 5974 | btrfs_tree_unlock(eb); |
| 5975 | path->locks[level] = 0; |
| 5976 | } |
| 5977 | return 0; |
| 5978 | } |
| 5979 | |
| 5980 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5981 | * hepler to process tree block pointer. |
| 5982 | * |
| 5983 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5984 | * reference count of the block pointed to. if the block |
| 5985 | * is shared and we need update back refs for the subtree |
| 5986 | * rooted at the block, this function changes wc->stage to |
| 5987 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5988 | * need to update back, this function drops the reference |
| 5989 | * to the block. |
| 5990 | * |
| 5991 | * NOTE: return value 1 means we should stop walking down. |
| 5992 | */ |
| 5993 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5994 | struct btrfs_root *root, |
| 5995 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5996 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5997 | { |
| 5998 | u64 bytenr; |
| 5999 | u64 generation; |
| 6000 | u64 parent; |
| 6001 | u32 blocksize; |
| 6002 | struct btrfs_key key; |
| 6003 | struct extent_buffer *next; |
| 6004 | int level = wc->level; |
| 6005 | int reada = 0; |
| 6006 | int ret = 0; |
| 6007 | |
| 6008 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 6009 | path->slots[level]); |
| 6010 | /* |
| 6011 | * if the lower level block was created before the snapshot |
| 6012 | * was created, we know there is no need to update back refs |
| 6013 | * for the subtree |
| 6014 | */ |
| 6015 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6016 | generation <= root->root_key.offset) { |
| 6017 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6018 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6019 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6020 | |
| 6021 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 6022 | blocksize = btrfs_level_size(root, level - 1); |
| 6023 | |
| 6024 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 6025 | if (!next) { |
| 6026 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6027 | if (!next) |
| 6028 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6029 | reada = 1; |
| 6030 | } |
| 6031 | btrfs_tree_lock(next); |
| 6032 | btrfs_set_lock_blocking(next); |
| 6033 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6034 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6035 | &wc->refs[level - 1], |
| 6036 | &wc->flags[level - 1]); |
| 6037 | BUG_ON(ret); |
| 6038 | BUG_ON(wc->refs[level - 1] == 0); |
| 6039 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6040 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6041 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6042 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6043 | if (level == 1 && |
| 6044 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6045 | goto skip; |
| 6046 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6047 | if (!wc->update_ref || |
| 6048 | generation <= root->root_key.offset) |
| 6049 | goto skip; |
| 6050 | |
| 6051 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 6052 | path->slots[level]); |
| 6053 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 6054 | if (ret < 0) |
| 6055 | goto skip; |
| 6056 | |
| 6057 | wc->stage = UPDATE_BACKREF; |
| 6058 | wc->shared_level = level - 1; |
| 6059 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6060 | } else { |
| 6061 | if (level == 1 && |
| 6062 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6063 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6064 | } |
| 6065 | |
| 6066 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 6067 | btrfs_tree_unlock(next); |
| 6068 | free_extent_buffer(next); |
| 6069 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6070 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6071 | } |
| 6072 | |
| 6073 | if (!next) { |
| 6074 | if (reada && level == 1) |
| 6075 | reada_walk_down(trans, root, wc, path); |
| 6076 | next = read_tree_block(root, bytenr, blocksize, generation); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 6077 | if (!next) |
| 6078 | return -EIO; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6079 | btrfs_tree_lock(next); |
| 6080 | btrfs_set_lock_blocking(next); |
| 6081 | } |
| 6082 | |
| 6083 | level--; |
| 6084 | BUG_ON(level != btrfs_header_level(next)); |
| 6085 | path->nodes[level] = next; |
| 6086 | path->slots[level] = 0; |
| 6087 | path->locks[level] = 1; |
| 6088 | wc->level = level; |
| 6089 | if (wc->level == 1) |
| 6090 | wc->reada_slot = 0; |
| 6091 | return 0; |
| 6092 | skip: |
| 6093 | wc->refs[level - 1] = 0; |
| 6094 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6095 | if (wc->stage == DROP_REFERENCE) { |
| 6096 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6097 | parent = path->nodes[level]->start; |
| 6098 | } else { |
| 6099 | BUG_ON(root->root_key.objectid != |
| 6100 | btrfs_header_owner(path->nodes[level])); |
| 6101 | parent = 0; |
| 6102 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6103 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6104 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 6105 | root->root_key.objectid, level - 1, 0); |
| 6106 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6107 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6108 | btrfs_tree_unlock(next); |
| 6109 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6110 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6111 | return 1; |
| 6112 | } |
| 6113 | |
| 6114 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6115 | * hepler to process tree block while walking up the tree. |
| 6116 | * |
| 6117 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6118 | * reference count on the block. |
| 6119 | * |
| 6120 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6121 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6122 | * to UPDATE_BACKREF previously while processing the block. |
| 6123 | * |
| 6124 | * NOTE: return value 1 means we should stop walking up. |
| 6125 | */ |
| 6126 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6127 | struct btrfs_root *root, |
| 6128 | struct btrfs_path *path, |
| 6129 | struct walk_control *wc) |
| 6130 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6131 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6132 | int level = wc->level; |
| 6133 | struct extent_buffer *eb = path->nodes[level]; |
| 6134 | u64 parent = 0; |
| 6135 | |
| 6136 | if (wc->stage == UPDATE_BACKREF) { |
| 6137 | BUG_ON(wc->shared_level < level); |
| 6138 | if (level < wc->shared_level) |
| 6139 | goto out; |
| 6140 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6141 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6142 | if (ret > 0) |
| 6143 | wc->update_ref = 0; |
| 6144 | |
| 6145 | wc->stage = DROP_REFERENCE; |
| 6146 | wc->shared_level = -1; |
| 6147 | path->slots[level] = 0; |
| 6148 | |
| 6149 | /* |
| 6150 | * check reference count again if the block isn't locked. |
| 6151 | * we should start walking down the tree again if reference |
| 6152 | * count is one. |
| 6153 | */ |
| 6154 | if (!path->locks[level]) { |
| 6155 | BUG_ON(level == 0); |
| 6156 | btrfs_tree_lock(eb); |
| 6157 | btrfs_set_lock_blocking(eb); |
| 6158 | path->locks[level] = 1; |
| 6159 | |
| 6160 | ret = btrfs_lookup_extent_info(trans, root, |
| 6161 | eb->start, eb->len, |
| 6162 | &wc->refs[level], |
| 6163 | &wc->flags[level]); |
| 6164 | BUG_ON(ret); |
| 6165 | BUG_ON(wc->refs[level] == 0); |
| 6166 | if (wc->refs[level] == 1) { |
| 6167 | btrfs_tree_unlock(eb); |
| 6168 | path->locks[level] = 0; |
| 6169 | return 1; |
| 6170 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6171 | } |
| 6172 | } |
| 6173 | |
| 6174 | /* wc->stage == DROP_REFERENCE */ |
| 6175 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6176 | |
| 6177 | if (wc->refs[level] == 1) { |
| 6178 | if (level == 0) { |
| 6179 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6180 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 6181 | else |
| 6182 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6183 | BUG_ON(ret); |
| 6184 | } |
| 6185 | /* make block locked assertion in clean_tree_block happy */ |
| 6186 | if (!path->locks[level] && |
| 6187 | btrfs_header_generation(eb) == trans->transid) { |
| 6188 | btrfs_tree_lock(eb); |
| 6189 | btrfs_set_lock_blocking(eb); |
| 6190 | path->locks[level] = 1; |
| 6191 | } |
| 6192 | clean_tree_block(trans, root, eb); |
| 6193 | } |
| 6194 | |
| 6195 | if (eb == root->node) { |
| 6196 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6197 | parent = eb->start; |
| 6198 | else |
| 6199 | BUG_ON(root->root_key.objectid != |
| 6200 | btrfs_header_owner(eb)); |
| 6201 | } else { |
| 6202 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6203 | parent = path->nodes[level + 1]->start; |
| 6204 | else |
| 6205 | BUG_ON(root->root_key.objectid != |
| 6206 | btrfs_header_owner(path->nodes[level + 1])); |
| 6207 | } |
| 6208 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6209 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6210 | out: |
| 6211 | wc->refs[level] = 0; |
| 6212 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6213 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6214 | } |
| 6215 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6216 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6217 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6218 | struct btrfs_path *path, |
| 6219 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6220 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6221 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6222 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6223 | int ret; |
| 6224 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6225 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6226 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6227 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6228 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6229 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6230 | if (level == 0) |
| 6231 | break; |
| 6232 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6233 | if (path->slots[level] >= |
| 6234 | btrfs_header_nritems(path->nodes[level])) |
| 6235 | break; |
| 6236 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6237 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6238 | if (ret > 0) { |
| 6239 | path->slots[level]++; |
| 6240 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6241 | } else if (ret < 0) |
| 6242 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6243 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6244 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6245 | return 0; |
| 6246 | } |
| 6247 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6248 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6249 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6250 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6251 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6252 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6253 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6254 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6255 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6256 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6257 | while (level < max_level && path->nodes[level]) { |
| 6258 | wc->level = level; |
| 6259 | if (path->slots[level] + 1 < |
| 6260 | btrfs_header_nritems(path->nodes[level])) { |
| 6261 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6262 | return 0; |
| 6263 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6264 | ret = walk_up_proc(trans, root, path, wc); |
| 6265 | if (ret > 0) |
| 6266 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6267 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6268 | if (path->locks[level]) { |
| 6269 | btrfs_tree_unlock(path->nodes[level]); |
| 6270 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6271 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6272 | free_extent_buffer(path->nodes[level]); |
| 6273 | path->nodes[level] = NULL; |
| 6274 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6275 | } |
| 6276 | } |
| 6277 | return 1; |
| 6278 | } |
| 6279 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6280 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6281 | * drop a subvolume tree. |
| 6282 | * |
| 6283 | * this function traverses the tree freeing any blocks that only |
| 6284 | * referenced by the tree. |
| 6285 | * |
| 6286 | * when a shared tree block is found. this function decreases its |
| 6287 | * reference count by one. if update_ref is true, this function |
| 6288 | * also make sure backrefs for the shared block and all lower level |
| 6289 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6290 | */ |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6291 | int btrfs_drop_snapshot(struct btrfs_root *root, |
| 6292 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6293 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6294 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6295 | struct btrfs_trans_handle *trans; |
| 6296 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6297 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6298 | struct walk_control *wc; |
| 6299 | struct btrfs_key key; |
| 6300 | int err = 0; |
| 6301 | int ret; |
| 6302 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6303 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6304 | path = btrfs_alloc_path(); |
| 6305 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6306 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6307 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 6308 | BUG_ON(!wc); |
| 6309 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6310 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6311 | BUG_ON(IS_ERR(trans)); |
| 6312 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6313 | if (block_rsv) |
| 6314 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6315 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6316 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6317 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6318 | path->nodes[level] = btrfs_lock_root_node(root); |
| 6319 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6320 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6321 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6322 | memset(&wc->update_progress, 0, |
| 6323 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6324 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6325 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6326 | memcpy(&wc->update_progress, &key, |
| 6327 | sizeof(wc->update_progress)); |
| 6328 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6329 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6330 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6331 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6332 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6333 | path->lowest_level = 0; |
| 6334 | if (ret < 0) { |
| 6335 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6336 | goto out; |
| 6337 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6338 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6339 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6340 | /* |
| 6341 | * unlock our path, this is safe because only this |
| 6342 | * function is allowed to delete this snapshot |
| 6343 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6344 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6345 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6346 | level = btrfs_header_level(root->node); |
| 6347 | while (1) { |
| 6348 | btrfs_tree_lock(path->nodes[level]); |
| 6349 | btrfs_set_lock_blocking(path->nodes[level]); |
| 6350 | |
| 6351 | ret = btrfs_lookup_extent_info(trans, root, |
| 6352 | path->nodes[level]->start, |
| 6353 | path->nodes[level]->len, |
| 6354 | &wc->refs[level], |
| 6355 | &wc->flags[level]); |
| 6356 | BUG_ON(ret); |
| 6357 | BUG_ON(wc->refs[level] == 0); |
| 6358 | |
| 6359 | if (level == root_item->drop_level) |
| 6360 | break; |
| 6361 | |
| 6362 | btrfs_tree_unlock(path->nodes[level]); |
| 6363 | WARN_ON(wc->refs[level] != 1); |
| 6364 | level--; |
| 6365 | } |
| 6366 | } |
| 6367 | |
| 6368 | wc->level = level; |
| 6369 | wc->shared_level = -1; |
| 6370 | wc->stage = DROP_REFERENCE; |
| 6371 | wc->update_ref = update_ref; |
| 6372 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6373 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6374 | |
| 6375 | while (1) { |
| 6376 | ret = walk_down_tree(trans, root, path, wc); |
| 6377 | if (ret < 0) { |
| 6378 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6379 | break; |
| 6380 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6381 | |
| 6382 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6383 | if (ret < 0) { |
| 6384 | err = ret; |
| 6385 | break; |
| 6386 | } |
| 6387 | |
| 6388 | if (ret > 0) { |
| 6389 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6390 | break; |
| 6391 | } |
| 6392 | |
| 6393 | if (wc->stage == DROP_REFERENCE) { |
| 6394 | level = wc->level; |
| 6395 | btrfs_node_key(path->nodes[level], |
| 6396 | &root_item->drop_progress, |
| 6397 | path->slots[level]); |
| 6398 | root_item->drop_level = level; |
| 6399 | } |
| 6400 | |
| 6401 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6402 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6403 | ret = btrfs_update_root(trans, tree_root, |
| 6404 | &root->root_key, |
| 6405 | root_item); |
| 6406 | BUG_ON(ret); |
| 6407 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6408 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6409 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6410 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6411 | if (block_rsv) |
| 6412 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6413 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6414 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6415 | btrfs_release_path(root, path); |
| 6416 | BUG_ON(err); |
| 6417 | |
| 6418 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6419 | BUG_ON(ret); |
| 6420 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6421 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6422 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6423 | NULL, NULL); |
| 6424 | BUG_ON(ret < 0); |
| 6425 | if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 6426 | /* if we fail to delete the orphan item this time |
| 6427 | * around, it'll get picked up the next time. |
| 6428 | * |
| 6429 | * The most common failure here is just -ENOENT. |
| 6430 | */ |
| 6431 | btrfs_del_orphan_item(trans, tree_root, |
| 6432 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6433 | } |
| 6434 | } |
| 6435 | |
| 6436 | if (root->in_radix) { |
| 6437 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6438 | } else { |
| 6439 | free_extent_buffer(root->node); |
| 6440 | free_extent_buffer(root->commit_root); |
| 6441 | kfree(root); |
| 6442 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6443 | out: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6444 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6445 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6446 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6447 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6448 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6449 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6450 | /* |
| 6451 | * drop subtree rooted at tree block 'node'. |
| 6452 | * |
| 6453 | * NOTE: this function will unlock and release tree block 'node' |
| 6454 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6455 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6456 | struct btrfs_root *root, |
| 6457 | struct extent_buffer *node, |
| 6458 | struct extent_buffer *parent) |
| 6459 | { |
| 6460 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6461 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6462 | int level; |
| 6463 | int parent_level; |
| 6464 | int ret = 0; |
| 6465 | int wret; |
| 6466 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6467 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6468 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6469 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6470 | if (!path) |
| 6471 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6472 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6473 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6474 | if (!wc) { |
| 6475 | btrfs_free_path(path); |
| 6476 | return -ENOMEM; |
| 6477 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6478 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6479 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6480 | parent_level = btrfs_header_level(parent); |
| 6481 | extent_buffer_get(parent); |
| 6482 | path->nodes[parent_level] = parent; |
| 6483 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6484 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6485 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6486 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6487 | path->nodes[level] = node; |
| 6488 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6489 | path->locks[level] = 1; |
| 6490 | |
| 6491 | wc->refs[parent_level] = 1; |
| 6492 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6493 | wc->level = level; |
| 6494 | wc->shared_level = -1; |
| 6495 | wc->stage = DROP_REFERENCE; |
| 6496 | wc->update_ref = 0; |
| 6497 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6498 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6499 | |
| 6500 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6501 | wret = walk_down_tree(trans, root, path, wc); |
| 6502 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6503 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6504 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6505 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6506 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6507 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6508 | if (wret < 0) |
| 6509 | ret = wret; |
| 6510 | if (wret != 0) |
| 6511 | break; |
| 6512 | } |
| 6513 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6514 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6515 | btrfs_free_path(path); |
| 6516 | return ret; |
| 6517 | } |
| 6518 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6519 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6520 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 6521 | unsigned long nr) |
| 6522 | { |
| 6523 | return min(last, start + nr - 1); |
| 6524 | } |
| 6525 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6526 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6527 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6528 | { |
| 6529 | u64 page_start; |
| 6530 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6531 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6532 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6533 | unsigned long i; |
| 6534 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6535 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6536 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6537 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6538 | unsigned int total_read = 0; |
| 6539 | unsigned int total_dirty = 0; |
| 6540 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6541 | |
| 6542 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 6543 | if (!ra) |
| 6544 | return -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6545 | |
| 6546 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6547 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6548 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 6549 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6550 | /* make sure the dirty trick played by the caller work */ |
| 6551 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6552 | first_index, last_index); |
| 6553 | if (ret) |
| 6554 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6555 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6556 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6557 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6558 | for (i = first_index ; i <= last_index; i++) { |
| 6559 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6560 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6561 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6562 | } |
| 6563 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6564 | again: |
| 6565 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6566 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6567 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6568 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6569 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6570 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6571 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6572 | if (!PageUptodate(page)) { |
| 6573 | btrfs_readpage(NULL, page); |
| 6574 | lock_page(page); |
| 6575 | if (!PageUptodate(page)) { |
| 6576 | unlock_page(page); |
| 6577 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6578 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6579 | goto out_unlock; |
| 6580 | } |
| 6581 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6582 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6583 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6584 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 6585 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6586 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6587 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6588 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6589 | if (ordered) { |
| 6590 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 6591 | unlock_page(page); |
| 6592 | page_cache_release(page); |
| 6593 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6594 | btrfs_put_ordered_extent(ordered); |
| 6595 | goto again; |
| 6596 | } |
| 6597 | set_page_extent_mapped(page); |
| 6598 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6599 | if (i == first_index) |
| 6600 | set_extent_bits(io_tree, page_start, page_end, |
| 6601 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6602 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6603 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6604 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6605 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6606 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6607 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6608 | unlock_page(page); |
| 6609 | page_cache_release(page); |
| 6610 | } |
| 6611 | |
| 6612 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6613 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6614 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6615 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6616 | return ret; |
| 6617 | } |
| 6618 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6619 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6620 | struct btrfs_key *extent_key, |
| 6621 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6622 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6623 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6624 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 6625 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6626 | u64 start = extent_key->objectid - offset; |
| 6627 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6628 | |
| 6629 | em = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 6630 | BUG_ON(!em); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6631 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6632 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6633 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6634 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6635 | em->block_start = extent_key->objectid; |
| 6636 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 6637 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6638 | |
| 6639 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6640 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6641 | while (1) { |
| 6642 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6643 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6644 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6645 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6646 | if (ret != -EEXIST) { |
| 6647 | free_extent_map(em); |
| 6648 | break; |
| 6649 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6650 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6651 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6652 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6653 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6654 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | struct btrfs_ref_path { |
| 6658 | u64 extent_start; |
| 6659 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 6660 | u64 root_objectid; |
| 6661 | u64 root_generation; |
| 6662 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6663 | u32 num_refs; |
| 6664 | int lowest_level; |
| 6665 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6666 | int shared_level; |
| 6667 | |
| 6668 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 6669 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6670 | }; |
| 6671 | |
| 6672 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6673 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6674 | u64 disk_bytenr; |
| 6675 | u64 disk_num_bytes; |
| 6676 | u64 offset; |
| 6677 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6678 | u8 compression; |
| 6679 | u8 encryption; |
| 6680 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6681 | }; |
| 6682 | |
| 6683 | static int is_cowonly_root(u64 root_objectid) |
| 6684 | { |
| 6685 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 6686 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 6687 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 6688 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 6689 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 6690 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6691 | return 1; |
| 6692 | return 0; |
| 6693 | } |
| 6694 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6695 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6696 | struct btrfs_root *extent_root, |
| 6697 | struct btrfs_ref_path *ref_path, |
| 6698 | int first_time) |
| 6699 | { |
| 6700 | struct extent_buffer *leaf; |
| 6701 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6702 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6703 | struct btrfs_key key; |
| 6704 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6705 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6706 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6707 | int level; |
| 6708 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6709 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6710 | path = btrfs_alloc_path(); |
| 6711 | if (!path) |
| 6712 | return -ENOMEM; |
| 6713 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6714 | if (first_time) { |
| 6715 | ref_path->lowest_level = -1; |
| 6716 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6717 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6718 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6719 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6720 | walk_down: |
| 6721 | level = ref_path->current_level - 1; |
| 6722 | while (level >= -1) { |
| 6723 | u64 parent; |
| 6724 | if (level < ref_path->lowest_level) |
| 6725 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6726 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6727 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6728 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6729 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6730 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6731 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6732 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6733 | parent = ref_path->nodes[level + 1]; |
| 6734 | ref_path->nodes[level + 1] = 0; |
| 6735 | ref_path->current_level = level; |
| 6736 | BUG_ON(parent == 0); |
| 6737 | |
| 6738 | key.objectid = bytenr; |
| 6739 | key.offset = parent + 1; |
| 6740 | key.type = BTRFS_EXTENT_REF_KEY; |
| 6741 | |
| 6742 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6743 | if (ret < 0) |
| 6744 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6745 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6746 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6747 | leaf = path->nodes[0]; |
| 6748 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6749 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6750 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6751 | if (ret < 0) |
| 6752 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6753 | if (ret > 0) |
| 6754 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6755 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6756 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6757 | |
| 6758 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6759 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6760 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 6761 | if (level < ref_path->shared_level) |
| 6762 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6763 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6764 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6765 | next: |
| 6766 | level--; |
| 6767 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6768 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6769 | } |
| 6770 | /* reached lowest level */ |
| 6771 | ret = 1; |
| 6772 | goto out; |
| 6773 | walk_up: |
| 6774 | level = ref_path->current_level; |
| 6775 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 6776 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6777 | |
| 6778 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6779 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6780 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6781 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6782 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6783 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6784 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6785 | key.objectid = bytenr; |
| 6786 | key.offset = 0; |
| 6787 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6788 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6789 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 6790 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6791 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6792 | |
| 6793 | leaf = path->nodes[0]; |
| 6794 | nritems = btrfs_header_nritems(leaf); |
| 6795 | if (path->slots[0] >= nritems) { |
| 6796 | ret = btrfs_next_leaf(extent_root, path); |
| 6797 | if (ret < 0) |
| 6798 | goto out; |
| 6799 | if (ret > 0) { |
| 6800 | /* the extent was freed by someone */ |
| 6801 | if (ref_path->lowest_level == level) |
| 6802 | goto out; |
| 6803 | btrfs_release_path(extent_root, path); |
| 6804 | goto walk_down; |
| 6805 | } |
| 6806 | leaf = path->nodes[0]; |
| 6807 | } |
| 6808 | |
| 6809 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6810 | if (found_key.objectid != bytenr || |
| 6811 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 6812 | /* the extent was freed by someone */ |
| 6813 | if (ref_path->lowest_level == level) { |
| 6814 | ret = 1; |
| 6815 | goto out; |
| 6816 | } |
| 6817 | btrfs_release_path(extent_root, path); |
| 6818 | goto walk_down; |
| 6819 | } |
| 6820 | found: |
| 6821 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 6822 | struct btrfs_extent_ref); |
| 6823 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 6824 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 6825 | if (first_time) { |
| 6826 | level = (int)ref_objectid; |
| 6827 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 6828 | ref_path->lowest_level = level; |
| 6829 | ref_path->current_level = level; |
| 6830 | ref_path->nodes[level] = bytenr; |
| 6831 | } else { |
| 6832 | WARN_ON(ref_objectid != level); |
| 6833 | } |
| 6834 | } else { |
| 6835 | WARN_ON(level != -1); |
| 6836 | } |
| 6837 | first_time = 0; |
| 6838 | |
| 6839 | if (ref_path->lowest_level == level) { |
| 6840 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6841 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 6842 | } |
| 6843 | |
| 6844 | /* |
| 6845 | * the block is tree root or the block isn't in reference |
| 6846 | * counted tree. |
| 6847 | */ |
| 6848 | if (found_key.objectid == found_key.offset || |
| 6849 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 6850 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6851 | ref_path->root_generation = |
| 6852 | btrfs_ref_generation(leaf, ref); |
| 6853 | if (level < 0) { |
| 6854 | /* special reference from the tree log */ |
| 6855 | ref_path->nodes[0] = found_key.offset; |
| 6856 | ref_path->current_level = 0; |
| 6857 | } |
| 6858 | ret = 0; |
| 6859 | goto out; |
| 6860 | } |
| 6861 | |
| 6862 | level++; |
| 6863 | BUG_ON(ref_path->nodes[level] != 0); |
| 6864 | ref_path->nodes[level] = found_key.offset; |
| 6865 | ref_path->current_level = level; |
| 6866 | |
| 6867 | /* |
| 6868 | * the reference was created in the running transaction, |
| 6869 | * no need to continue walking up. |
| 6870 | */ |
| 6871 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 6872 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6873 | ref_path->root_generation = |
| 6874 | btrfs_ref_generation(leaf, ref); |
| 6875 | ret = 0; |
| 6876 | goto out; |
| 6877 | } |
| 6878 | |
| 6879 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6880 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6881 | } |
| 6882 | /* reached max tree level, but no tree root found. */ |
| 6883 | BUG(); |
| 6884 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6885 | btrfs_free_path(path); |
| 6886 | return ret; |
| 6887 | } |
| 6888 | |
| 6889 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 6890 | struct btrfs_root *extent_root, |
| 6891 | struct btrfs_ref_path *ref_path, |
| 6892 | u64 extent_start) |
| 6893 | { |
| 6894 | memset(ref_path, 0, sizeof(*ref_path)); |
| 6895 | ref_path->extent_start = extent_start; |
| 6896 | |
| 6897 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 6898 | } |
| 6899 | |
| 6900 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 6901 | struct btrfs_root *extent_root, |
| 6902 | struct btrfs_ref_path *ref_path) |
| 6903 | { |
| 6904 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 6905 | } |
| 6906 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6907 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6908 | struct btrfs_key *extent_key, |
| 6909 | u64 offset, int no_fragment, |
| 6910 | struct disk_extent **extents, |
| 6911 | int *nr_extents) |
| 6912 | { |
| 6913 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6914 | struct btrfs_path *path; |
| 6915 | struct btrfs_file_extent_item *fi; |
| 6916 | struct extent_buffer *leaf; |
| 6917 | struct disk_extent *exts = *extents; |
| 6918 | struct btrfs_key found_key; |
| 6919 | u64 cur_pos; |
| 6920 | u64 last_byte; |
| 6921 | u32 nritems; |
| 6922 | int nr = 0; |
| 6923 | int max = *nr_extents; |
| 6924 | int ret; |
| 6925 | |
| 6926 | WARN_ON(!no_fragment && *extents); |
| 6927 | if (!exts) { |
| 6928 | max = 1; |
| 6929 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6930 | if (!exts) |
| 6931 | return -ENOMEM; |
| 6932 | } |
| 6933 | |
| 6934 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6935 | if (!path) { |
| 6936 | if (exts != *extents) |
| 6937 | kfree(exts); |
| 6938 | return -ENOMEM; |
| 6939 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6940 | |
| 6941 | cur_pos = extent_key->objectid - offset; |
| 6942 | last_byte = extent_key->objectid + extent_key->offset; |
| 6943 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 6944 | cur_pos, 0); |
| 6945 | if (ret < 0) |
| 6946 | goto out; |
| 6947 | if (ret > 0) { |
| 6948 | ret = -ENOENT; |
| 6949 | goto out; |
| 6950 | } |
| 6951 | |
| 6952 | while (1) { |
| 6953 | leaf = path->nodes[0]; |
| 6954 | nritems = btrfs_header_nritems(leaf); |
| 6955 | if (path->slots[0] >= nritems) { |
| 6956 | ret = btrfs_next_leaf(root, path); |
| 6957 | if (ret < 0) |
| 6958 | goto out; |
| 6959 | if (ret > 0) |
| 6960 | break; |
| 6961 | leaf = path->nodes[0]; |
| 6962 | } |
| 6963 | |
| 6964 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6965 | if (found_key.offset != cur_pos || |
| 6966 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 6967 | found_key.objectid != reloc_inode->i_ino) |
| 6968 | break; |
| 6969 | |
| 6970 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6971 | struct btrfs_file_extent_item); |
| 6972 | if (btrfs_file_extent_type(leaf, fi) != |
| 6973 | BTRFS_FILE_EXTENT_REG || |
| 6974 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6975 | break; |
| 6976 | |
| 6977 | if (nr == max) { |
| 6978 | struct disk_extent *old = exts; |
| 6979 | max *= 2; |
| 6980 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
Yoshinori Sano | dac97e5 | 2011-02-15 12:01:42 +0000 | [diff] [blame] | 6981 | if (!exts) { |
| 6982 | ret = -ENOMEM; |
| 6983 | goto out; |
| 6984 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6985 | memcpy(exts, old, sizeof(*exts) * nr); |
| 6986 | if (old != *extents) |
| 6987 | kfree(old); |
| 6988 | } |
| 6989 | |
| 6990 | exts[nr].disk_bytenr = |
| 6991 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6992 | exts[nr].disk_num_bytes = |
| 6993 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6994 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 6995 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6996 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6997 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 6998 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 6999 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 7000 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7001 | BUG_ON(exts[nr].offset > 0); |
| 7002 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 7003 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7004 | |
| 7005 | cur_pos += exts[nr].num_bytes; |
| 7006 | nr++; |
| 7007 | |
| 7008 | if (cur_pos + offset >= last_byte) |
| 7009 | break; |
| 7010 | |
| 7011 | if (no_fragment) { |
| 7012 | ret = 1; |
| 7013 | goto out; |
| 7014 | } |
| 7015 | path->slots[0]++; |
| 7016 | } |
| 7017 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 7018 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7019 | if (cur_pos + offset < last_byte) { |
| 7020 | ret = -ENOENT; |
| 7021 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7022 | } |
| 7023 | ret = 0; |
| 7024 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7025 | btrfs_free_path(path); |
| 7026 | if (ret) { |
| 7027 | if (exts != *extents) |
| 7028 | kfree(exts); |
| 7029 | } else { |
| 7030 | *extents = exts; |
| 7031 | *nr_extents = nr; |
| 7032 | } |
| 7033 | return ret; |
| 7034 | } |
| 7035 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7036 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7037 | struct btrfs_root *root, |
| 7038 | struct btrfs_path *path, |
| 7039 | struct btrfs_key *extent_key, |
| 7040 | struct btrfs_key *leaf_key, |
| 7041 | struct btrfs_ref_path *ref_path, |
| 7042 | struct disk_extent *new_extents, |
| 7043 | int nr_extents) |
| 7044 | { |
| 7045 | struct extent_buffer *leaf; |
| 7046 | struct btrfs_file_extent_item *fi; |
| 7047 | struct inode *inode = NULL; |
| 7048 | struct btrfs_key key; |
| 7049 | u64 lock_start = 0; |
| 7050 | u64 lock_end = 0; |
| 7051 | u64 num_bytes; |
| 7052 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7053 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7054 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7055 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7056 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7057 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7058 | int ret; |
| 7059 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7060 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7061 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7062 | if (key.objectid < ref_path->owner_objectid || |
| 7063 | (key.objectid == ref_path->owner_objectid && |
| 7064 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 7065 | key.objectid = ref_path->owner_objectid; |
| 7066 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 7067 | key.offset = 0; |
| 7068 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7069 | } |
| 7070 | |
| 7071 | while (1) { |
| 7072 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 7073 | if (ret < 0) |
| 7074 | goto out; |
| 7075 | |
| 7076 | leaf = path->nodes[0]; |
| 7077 | nritems = btrfs_header_nritems(leaf); |
| 7078 | next: |
| 7079 | if (extent_locked && ret > 0) { |
| 7080 | /* |
| 7081 | * the file extent item was modified by someone |
| 7082 | * before the extent got locked. |
| 7083 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7084 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7085 | lock_end, GFP_NOFS); |
| 7086 | extent_locked = 0; |
| 7087 | } |
| 7088 | |
| 7089 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7090 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7091 | break; |
| 7092 | |
| 7093 | BUG_ON(extent_locked); |
| 7094 | ret = btrfs_next_leaf(root, path); |
| 7095 | if (ret < 0) |
| 7096 | goto out; |
| 7097 | if (ret > 0) |
| 7098 | break; |
| 7099 | leaf = path->nodes[0]; |
| 7100 | nritems = btrfs_header_nritems(leaf); |
| 7101 | } |
| 7102 | |
| 7103 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 7104 | |
| 7105 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 7106 | if ((key.objectid > ref_path->owner_objectid) || |
| 7107 | (key.objectid == ref_path->owner_objectid && |
| 7108 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7109 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7110 | break; |
| 7111 | } |
| 7112 | |
| 7113 | if (inode && key.objectid != inode->i_ino) { |
| 7114 | BUG_ON(extent_locked); |
| 7115 | btrfs_release_path(root, path); |
| 7116 | mutex_unlock(&inode->i_mutex); |
| 7117 | iput(inode); |
| 7118 | inode = NULL; |
| 7119 | continue; |
| 7120 | } |
| 7121 | |
| 7122 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7123 | path->slots[0]++; |
| 7124 | ret = 1; |
| 7125 | goto next; |
| 7126 | } |
| 7127 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 7128 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7129 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 7130 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 7131 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7132 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 7133 | extent_key->objectid)) { |
| 7134 | path->slots[0]++; |
| 7135 | ret = 1; |
| 7136 | goto next; |
| 7137 | } |
| 7138 | |
| 7139 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 7140 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 7141 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7142 | if (search_end == (u64)-1) { |
| 7143 | search_end = key.offset - ext_offset + |
| 7144 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 7145 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7146 | |
| 7147 | if (!extent_locked) { |
| 7148 | lock_start = key.offset; |
| 7149 | lock_end = lock_start + num_bytes - 1; |
| 7150 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 7151 | if (lock_start > key.offset || |
| 7152 | lock_end + 1 < key.offset + num_bytes) { |
| 7153 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 7154 | lock_start, lock_end, GFP_NOFS); |
| 7155 | extent_locked = 0; |
| 7156 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7157 | } |
| 7158 | |
| 7159 | if (!inode) { |
| 7160 | btrfs_release_path(root, path); |
| 7161 | |
| 7162 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 7163 | key.objectid, root); |
| 7164 | if (inode->i_state & I_NEW) { |
| 7165 | BTRFS_I(inode)->root = root; |
| 7166 | BTRFS_I(inode)->location.objectid = |
| 7167 | key.objectid; |
| 7168 | BTRFS_I(inode)->location.type = |
| 7169 | BTRFS_INODE_ITEM_KEY; |
| 7170 | BTRFS_I(inode)->location.offset = 0; |
| 7171 | btrfs_read_locked_inode(inode); |
| 7172 | unlock_new_inode(inode); |
| 7173 | } |
| 7174 | /* |
| 7175 | * some code call btrfs_commit_transaction while |
| 7176 | * holding the i_mutex, so we can't use mutex_lock |
| 7177 | * here. |
| 7178 | */ |
| 7179 | if (is_bad_inode(inode) || |
| 7180 | !mutex_trylock(&inode->i_mutex)) { |
| 7181 | iput(inode); |
| 7182 | inode = NULL; |
| 7183 | key.offset = (u64)-1; |
| 7184 | goto skip; |
| 7185 | } |
| 7186 | } |
| 7187 | |
| 7188 | if (!extent_locked) { |
| 7189 | struct btrfs_ordered_extent *ordered; |
| 7190 | |
| 7191 | btrfs_release_path(root, path); |
| 7192 | |
| 7193 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7194 | lock_end, GFP_NOFS); |
| 7195 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 7196 | lock_end); |
| 7197 | if (ordered && |
| 7198 | ordered->file_offset <= lock_end && |
| 7199 | ordered->file_offset + ordered->len > lock_start) { |
| 7200 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 7201 | lock_start, lock_end, GFP_NOFS); |
| 7202 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7203 | btrfs_put_ordered_extent(ordered); |
| 7204 | key.offset += num_bytes; |
| 7205 | goto skip; |
| 7206 | } |
| 7207 | if (ordered) |
| 7208 | btrfs_put_ordered_extent(ordered); |
| 7209 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7210 | extent_locked = 1; |
| 7211 | continue; |
| 7212 | } |
| 7213 | |
| 7214 | if (nr_extents == 1) { |
| 7215 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7216 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7217 | new_extents[0].disk_bytenr); |
| 7218 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7219 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7220 | btrfs_mark_buffer_dirty(leaf); |
| 7221 | |
| 7222 | btrfs_drop_extent_cache(inode, key.offset, |
| 7223 | key.offset + num_bytes - 1, 0); |
| 7224 | |
| 7225 | ret = btrfs_inc_extent_ref(trans, root, |
| 7226 | new_extents[0].disk_bytenr, |
| 7227 | new_extents[0].disk_num_bytes, |
| 7228 | leaf->start, |
| 7229 | root->root_key.objectid, |
| 7230 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7231 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7232 | BUG_ON(ret); |
| 7233 | |
| 7234 | ret = btrfs_free_extent(trans, root, |
| 7235 | extent_key->objectid, |
| 7236 | extent_key->offset, |
| 7237 | leaf->start, |
| 7238 | btrfs_header_owner(leaf), |
| 7239 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7240 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7241 | BUG_ON(ret); |
| 7242 | |
| 7243 | btrfs_release_path(root, path); |
| 7244 | key.offset += num_bytes; |
| 7245 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7246 | BUG_ON(1); |
| 7247 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7248 | u64 alloc_hint; |
| 7249 | u64 extent_len; |
| 7250 | int i; |
| 7251 | /* |
| 7252 | * drop old extent pointer at first, then insert the |
| 7253 | * new pointers one bye one |
| 7254 | */ |
| 7255 | btrfs_release_path(root, path); |
| 7256 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 7257 | key.offset + num_bytes, |
| 7258 | key.offset, &alloc_hint); |
| 7259 | BUG_ON(ret); |
| 7260 | |
| 7261 | for (i = 0; i < nr_extents; i++) { |
| 7262 | if (ext_offset >= new_extents[i].num_bytes) { |
| 7263 | ext_offset -= new_extents[i].num_bytes; |
| 7264 | continue; |
| 7265 | } |
| 7266 | extent_len = min(new_extents[i].num_bytes - |
| 7267 | ext_offset, num_bytes); |
| 7268 | |
| 7269 | ret = btrfs_insert_empty_item(trans, root, |
| 7270 | path, &key, |
| 7271 | sizeof(*fi)); |
| 7272 | BUG_ON(ret); |
| 7273 | |
| 7274 | leaf = path->nodes[0]; |
| 7275 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 7276 | struct btrfs_file_extent_item); |
| 7277 | btrfs_set_file_extent_generation(leaf, fi, |
| 7278 | trans->transid); |
| 7279 | btrfs_set_file_extent_type(leaf, fi, |
| 7280 | BTRFS_FILE_EXTENT_REG); |
| 7281 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7282 | new_extents[i].disk_bytenr); |
| 7283 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7284 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7285 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 7286 | new_extents[i].ram_bytes); |
| 7287 | |
| 7288 | btrfs_set_file_extent_compression(leaf, fi, |
| 7289 | new_extents[i].compression); |
| 7290 | btrfs_set_file_extent_encryption(leaf, fi, |
| 7291 | new_extents[i].encryption); |
| 7292 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 7293 | new_extents[i].other_encoding); |
| 7294 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7295 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 7296 | extent_len); |
| 7297 | ext_offset += new_extents[i].offset; |
| 7298 | btrfs_set_file_extent_offset(leaf, fi, |
| 7299 | ext_offset); |
| 7300 | btrfs_mark_buffer_dirty(leaf); |
| 7301 | |
| 7302 | btrfs_drop_extent_cache(inode, key.offset, |
| 7303 | key.offset + extent_len - 1, 0); |
| 7304 | |
| 7305 | ret = btrfs_inc_extent_ref(trans, root, |
| 7306 | new_extents[i].disk_bytenr, |
| 7307 | new_extents[i].disk_num_bytes, |
| 7308 | leaf->start, |
| 7309 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7310 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7311 | BUG_ON(ret); |
| 7312 | btrfs_release_path(root, path); |
| 7313 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 7314 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7315 | |
| 7316 | ext_offset = 0; |
| 7317 | num_bytes -= extent_len; |
| 7318 | key.offset += extent_len; |
| 7319 | |
| 7320 | if (num_bytes == 0) |
| 7321 | break; |
| 7322 | } |
| 7323 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7324 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7325 | } |
| 7326 | |
| 7327 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7328 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7329 | lock_end, GFP_NOFS); |
| 7330 | extent_locked = 0; |
| 7331 | } |
| 7332 | skip: |
| 7333 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7334 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7335 | break; |
| 7336 | |
| 7337 | cond_resched(); |
| 7338 | } |
| 7339 | ret = 0; |
| 7340 | out: |
| 7341 | btrfs_release_path(root, path); |
| 7342 | if (inode) { |
| 7343 | mutex_unlock(&inode->i_mutex); |
| 7344 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7345 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7346 | lock_end, GFP_NOFS); |
| 7347 | } |
| 7348 | iput(inode); |
| 7349 | } |
| 7350 | return ret; |
| 7351 | } |
| 7352 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7353 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 7354 | struct btrfs_root *root, |
| 7355 | struct extent_buffer *buf, u64 orig_start) |
| 7356 | { |
| 7357 | int level; |
| 7358 | int ret; |
| 7359 | |
| 7360 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 7361 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 7362 | |
| 7363 | level = btrfs_header_level(buf); |
| 7364 | if (level == 0) { |
| 7365 | struct btrfs_leaf_ref *ref; |
| 7366 | struct btrfs_leaf_ref *orig_ref; |
| 7367 | |
| 7368 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 7369 | if (!orig_ref) |
| 7370 | return -ENOENT; |
| 7371 | |
| 7372 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 7373 | if (!ref) { |
| 7374 | btrfs_free_leaf_ref(root, orig_ref); |
| 7375 | return -ENOMEM; |
| 7376 | } |
| 7377 | |
| 7378 | ref->nritems = orig_ref->nritems; |
| 7379 | memcpy(ref->extents, orig_ref->extents, |
| 7380 | sizeof(ref->extents[0]) * ref->nritems); |
| 7381 | |
| 7382 | btrfs_free_leaf_ref(root, orig_ref); |
| 7383 | |
| 7384 | ref->root_gen = trans->transid; |
| 7385 | ref->bytenr = buf->start; |
| 7386 | ref->owner = btrfs_header_owner(buf); |
| 7387 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 7388 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7389 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 7390 | WARN_ON(ret); |
| 7391 | btrfs_free_leaf_ref(root, ref); |
| 7392 | } |
| 7393 | return 0; |
| 7394 | } |
| 7395 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7396 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7397 | struct extent_buffer *leaf, |
| 7398 | struct btrfs_block_group_cache *group, |
| 7399 | struct btrfs_root *target_root) |
| 7400 | { |
| 7401 | struct btrfs_key key; |
| 7402 | struct inode *inode = NULL; |
| 7403 | struct btrfs_file_extent_item *fi; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7404 | struct extent_state *cached_state = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7405 | u64 num_bytes; |
| 7406 | u64 skip_objectid = 0; |
| 7407 | u32 nritems; |
| 7408 | u32 i; |
| 7409 | |
| 7410 | nritems = btrfs_header_nritems(leaf); |
| 7411 | for (i = 0; i < nritems; i++) { |
| 7412 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 7413 | if (key.objectid == skip_objectid || |
| 7414 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 7415 | continue; |
| 7416 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 7417 | if (btrfs_file_extent_type(leaf, fi) == |
| 7418 | BTRFS_FILE_EXTENT_INLINE) |
| 7419 | continue; |
| 7420 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 7421 | continue; |
| 7422 | if (!inode || inode->i_ino != key.objectid) { |
| 7423 | iput(inode); |
| 7424 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 7425 | key.objectid, target_root, 1); |
| 7426 | } |
| 7427 | if (!inode) { |
| 7428 | skip_objectid = key.objectid; |
| 7429 | continue; |
| 7430 | } |
| 7431 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 7432 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7433 | lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset, |
| 7434 | key.offset + num_bytes - 1, 0, &cached_state, |
| 7435 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7436 | btrfs_drop_extent_cache(inode, key.offset, |
| 7437 | key.offset + num_bytes - 1, 1); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7438 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset, |
| 7439 | key.offset + num_bytes - 1, &cached_state, |
| 7440 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7441 | cond_resched(); |
| 7442 | } |
| 7443 | iput(inode); |
| 7444 | return 0; |
| 7445 | } |
| 7446 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7447 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7448 | struct btrfs_root *root, |
| 7449 | struct extent_buffer *leaf, |
| 7450 | struct btrfs_block_group_cache *group, |
| 7451 | struct inode *reloc_inode) |
| 7452 | { |
| 7453 | struct btrfs_key key; |
| 7454 | struct btrfs_key extent_key; |
| 7455 | struct btrfs_file_extent_item *fi; |
| 7456 | struct btrfs_leaf_ref *ref; |
| 7457 | struct disk_extent *new_extent; |
| 7458 | u64 bytenr; |
| 7459 | u64 num_bytes; |
| 7460 | u32 nritems; |
| 7461 | u32 i; |
| 7462 | int ext_index; |
| 7463 | int nr_extent; |
| 7464 | int ret; |
| 7465 | |
| 7466 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7467 | if (!new_extent) |
| 7468 | return -ENOMEM; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7469 | |
| 7470 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 7471 | BUG_ON(!ref); |
| 7472 | |
| 7473 | ext_index = -1; |
| 7474 | nritems = btrfs_header_nritems(leaf); |
| 7475 | for (i = 0; i < nritems; i++) { |
| 7476 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 7477 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 7478 | continue; |
| 7479 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 7480 | if (btrfs_file_extent_type(leaf, fi) == |
| 7481 | BTRFS_FILE_EXTENT_INLINE) |
| 7482 | continue; |
| 7483 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 7484 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7485 | if (bytenr == 0) |
| 7486 | continue; |
| 7487 | |
| 7488 | ext_index++; |
| 7489 | if (bytenr >= group->key.objectid + group->key.offset || |
| 7490 | bytenr + num_bytes <= group->key.objectid) |
| 7491 | continue; |
| 7492 | |
| 7493 | extent_key.objectid = bytenr; |
| 7494 | extent_key.offset = num_bytes; |
| 7495 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 7496 | nr_extent = 1; |
| 7497 | ret = get_new_locations(reloc_inode, &extent_key, |
| 7498 | group->key.objectid, 1, |
| 7499 | &new_extent, &nr_extent); |
| 7500 | if (ret > 0) |
| 7501 | continue; |
| 7502 | BUG_ON(ret < 0); |
| 7503 | |
| 7504 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 7505 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 7506 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 7507 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 7508 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7509 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7510 | new_extent->disk_bytenr); |
| 7511 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7512 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7513 | btrfs_mark_buffer_dirty(leaf); |
| 7514 | |
| 7515 | ret = btrfs_inc_extent_ref(trans, root, |
| 7516 | new_extent->disk_bytenr, |
| 7517 | new_extent->disk_num_bytes, |
| 7518 | leaf->start, |
| 7519 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7520 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7521 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7522 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7523 | ret = btrfs_free_extent(trans, root, |
| 7524 | bytenr, num_bytes, leaf->start, |
| 7525 | btrfs_header_owner(leaf), |
| 7526 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7527 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7528 | BUG_ON(ret); |
| 7529 | cond_resched(); |
| 7530 | } |
| 7531 | kfree(new_extent); |
| 7532 | BUG_ON(ext_index + 1 != ref->nritems); |
| 7533 | btrfs_free_leaf_ref(root, ref); |
| 7534 | return 0; |
| 7535 | } |
| 7536 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7537 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 7538 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7539 | { |
| 7540 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7541 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7542 | |
| 7543 | if (root->reloc_root) { |
| 7544 | reloc_root = root->reloc_root; |
| 7545 | root->reloc_root = NULL; |
| 7546 | list_add(&reloc_root->dead_list, |
| 7547 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7548 | |
| 7549 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 7550 | reloc_root->node->start); |
| 7551 | btrfs_set_root_level(&root->root_item, |
| 7552 | btrfs_header_level(reloc_root->node)); |
| 7553 | memset(&reloc_root->root_item.drop_progress, 0, |
| 7554 | sizeof(struct btrfs_disk_key)); |
| 7555 | reloc_root->root_item.drop_level = 0; |
| 7556 | |
| 7557 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 7558 | &reloc_root->root_key, |
| 7559 | &reloc_root->root_item); |
| 7560 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7561 | } |
| 7562 | return 0; |
| 7563 | } |
| 7564 | |
| 7565 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 7566 | { |
| 7567 | struct btrfs_trans_handle *trans; |
| 7568 | struct btrfs_root *reloc_root; |
| 7569 | struct btrfs_root *prev_root = NULL; |
| 7570 | struct list_head dead_roots; |
| 7571 | int ret; |
| 7572 | unsigned long nr; |
| 7573 | |
| 7574 | INIT_LIST_HEAD(&dead_roots); |
| 7575 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 7576 | |
| 7577 | while (!list_empty(&dead_roots)) { |
| 7578 | reloc_root = list_entry(dead_roots.prev, |
| 7579 | struct btrfs_root, dead_list); |
| 7580 | list_del_init(&reloc_root->dead_list); |
| 7581 | |
| 7582 | BUG_ON(reloc_root->commit_root != NULL); |
| 7583 | while (1) { |
| 7584 | trans = btrfs_join_transaction(root, 1); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 7585 | BUG_ON(IS_ERR(trans)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7586 | |
| 7587 | mutex_lock(&root->fs_info->drop_mutex); |
| 7588 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 7589 | if (ret != -EAGAIN) |
| 7590 | break; |
| 7591 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7592 | |
| 7593 | nr = trans->blocks_used; |
| 7594 | ret = btrfs_end_transaction(trans, root); |
| 7595 | BUG_ON(ret); |
| 7596 | btrfs_btree_balance_dirty(root, nr); |
| 7597 | } |
| 7598 | |
| 7599 | free_extent_buffer(reloc_root->node); |
| 7600 | |
| 7601 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 7602 | &reloc_root->root_key); |
| 7603 | BUG_ON(ret); |
| 7604 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7605 | |
| 7606 | nr = trans->blocks_used; |
| 7607 | ret = btrfs_end_transaction(trans, root); |
| 7608 | BUG_ON(ret); |
| 7609 | btrfs_btree_balance_dirty(root, nr); |
| 7610 | |
| 7611 | kfree(prev_root); |
| 7612 | prev_root = reloc_root; |
| 7613 | } |
| 7614 | if (prev_root) { |
| 7615 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 7616 | kfree(prev_root); |
| 7617 | } |
| 7618 | return 0; |
| 7619 | } |
| 7620 | |
| 7621 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 7622 | { |
| 7623 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 7624 | return 0; |
| 7625 | } |
| 7626 | |
| 7627 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 7628 | { |
| 7629 | struct btrfs_root *reloc_root; |
| 7630 | struct btrfs_trans_handle *trans; |
| 7631 | struct btrfs_key location; |
| 7632 | int found; |
| 7633 | int ret; |
| 7634 | |
| 7635 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7636 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 7637 | BUG_ON(ret); |
| 7638 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 7639 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 7640 | |
| 7641 | if (found) { |
| 7642 | trans = btrfs_start_transaction(root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 7643 | BUG_ON(IS_ERR(trans)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7644 | ret = btrfs_commit_transaction(trans, root); |
| 7645 | BUG_ON(ret); |
| 7646 | } |
| 7647 | |
| 7648 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 7649 | location.offset = (u64)-1; |
| 7650 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 7651 | |
| 7652 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 7653 | BUG_ON(!reloc_root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 7654 | ret = btrfs_orphan_cleanup(reloc_root); |
| 7655 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7656 | return 0; |
| 7657 | } |
| 7658 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7659 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7660 | struct btrfs_root *root) |
| 7661 | { |
| 7662 | struct btrfs_root *reloc_root; |
| 7663 | struct extent_buffer *eb; |
| 7664 | struct btrfs_root_item *root_item; |
| 7665 | struct btrfs_key root_key; |
| 7666 | int ret; |
| 7667 | |
| 7668 | BUG_ON(!root->ref_cows); |
| 7669 | if (root->reloc_root) |
| 7670 | return 0; |
| 7671 | |
| 7672 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7673 | if (!root_item) |
| 7674 | return -ENOMEM; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7675 | |
| 7676 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 7677 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 7678 | BUG_ON(ret); |
| 7679 | |
| 7680 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 7681 | root_key.offset = root->root_key.objectid; |
| 7682 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7683 | |
| 7684 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 7685 | btrfs_set_root_refs(root_item, 0); |
| 7686 | btrfs_set_root_bytenr(root_item, eb->start); |
| 7687 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7688 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7689 | |
| 7690 | btrfs_tree_unlock(eb); |
| 7691 | free_extent_buffer(eb); |
| 7692 | |
| 7693 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 7694 | &root_key, root_item); |
| 7695 | BUG_ON(ret); |
| 7696 | kfree(root_item); |
| 7697 | |
| 7698 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 7699 | &root_key); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7700 | BUG_ON(IS_ERR(reloc_root)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7701 | reloc_root->last_trans = trans->transid; |
| 7702 | reloc_root->commit_root = NULL; |
| 7703 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 7704 | |
| 7705 | root->reloc_root = reloc_root; |
| 7706 | return 0; |
| 7707 | } |
| 7708 | |
| 7709 | /* |
| 7710 | * Core function of space balance. |
| 7711 | * |
| 7712 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7713 | * counted roots. There is one reloc tree for each subvol, and all |
| 7714 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 7715 | * of the latest committed roots of subvols (root->commit_root). |
| 7716 | * |
| 7717 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 7718 | * COW the block through subvol's reloc tree, then update block pointer |
| 7719 | * in the subvol to point to the new block. Since all reloc trees share |
| 7720 | * same root key objectid, doing special handing for tree blocks owned |
| 7721 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 7722 | * we can use the resulting new block directly when the same block is |
| 7723 | * required to COW again through other reloc trees. By this way, relocated |
| 7724 | * tree blocks are shared between reloc trees, so they are also shared |
| 7725 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7726 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7727 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7728 | struct btrfs_root *root, |
| 7729 | struct btrfs_path *path, |
| 7730 | struct btrfs_key *first_key, |
| 7731 | struct btrfs_ref_path *ref_path, |
| 7732 | struct btrfs_block_group_cache *group, |
| 7733 | struct inode *reloc_inode) |
| 7734 | { |
| 7735 | struct btrfs_root *reloc_root; |
| 7736 | struct extent_buffer *eb = NULL; |
| 7737 | struct btrfs_key *keys; |
| 7738 | u64 *nodes; |
| 7739 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7740 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7741 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7742 | int ret; |
| 7743 | |
| 7744 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 7745 | lowest_level = ref_path->owner_objectid; |
| 7746 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7747 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7748 | path->lowest_level = lowest_level; |
| 7749 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 7750 | BUG_ON(ret < 0); |
| 7751 | path->lowest_level = 0; |
| 7752 | btrfs_release_path(root, path); |
| 7753 | return 0; |
| 7754 | } |
| 7755 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7756 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7757 | ret = init_reloc_tree(trans, root); |
| 7758 | BUG_ON(ret); |
| 7759 | reloc_root = root->reloc_root; |
| 7760 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7761 | shared_level = ref_path->shared_level; |
| 7762 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7763 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7764 | keys = ref_path->node_keys; |
| 7765 | nodes = ref_path->new_nodes; |
| 7766 | memset(&keys[shared_level + 1], 0, |
| 7767 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 7768 | memset(&nodes[shared_level + 1], 0, |
| 7769 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7770 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7771 | if (nodes[lowest_level] == 0) { |
| 7772 | path->lowest_level = lowest_level; |
| 7773 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7774 | 0, 1); |
| 7775 | BUG_ON(ret); |
| 7776 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 7777 | eb = path->nodes[level]; |
| 7778 | if (!eb || eb == reloc_root->node) |
| 7779 | break; |
| 7780 | nodes[level] = eb->start; |
| 7781 | if (level == 0) |
| 7782 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 7783 | else |
| 7784 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 7785 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7786 | if (nodes[0] && |
| 7787 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7788 | eb = path->nodes[0]; |
| 7789 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 7790 | group, reloc_inode); |
| 7791 | BUG_ON(ret); |
| 7792 | } |
| 7793 | btrfs_release_path(reloc_root, path); |
| 7794 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7795 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7796 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7797 | BUG_ON(ret); |
| 7798 | } |
| 7799 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7800 | /* |
| 7801 | * replace tree blocks in the fs tree with tree blocks in |
| 7802 | * the reloc tree. |
| 7803 | */ |
| 7804 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 7805 | BUG_ON(ret < 0); |
| 7806 | |
| 7807 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7808 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7809 | 0, 0); |
| 7810 | BUG_ON(ret); |
| 7811 | extent_buffer_get(path->nodes[0]); |
| 7812 | eb = path->nodes[0]; |
| 7813 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7814 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 7815 | BUG_ON(ret); |
| 7816 | free_extent_buffer(eb); |
| 7817 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7818 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7819 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7820 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7821 | return 0; |
| 7822 | } |
| 7823 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7824 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7825 | struct btrfs_root *root, |
| 7826 | struct btrfs_path *path, |
| 7827 | struct btrfs_key *first_key, |
| 7828 | struct btrfs_ref_path *ref_path) |
| 7829 | { |
| 7830 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7831 | |
| 7832 | ret = relocate_one_path(trans, root, path, first_key, |
| 7833 | ref_path, NULL, NULL); |
| 7834 | BUG_ON(ret); |
| 7835 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7836 | return 0; |
| 7837 | } |
| 7838 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7839 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7840 | struct btrfs_root *extent_root, |
| 7841 | struct btrfs_path *path, |
| 7842 | struct btrfs_key *extent_key) |
| 7843 | { |
| 7844 | int ret; |
| 7845 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7846 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 7847 | if (ret) |
| 7848 | goto out; |
| 7849 | ret = btrfs_del_item(trans, extent_root, path); |
| 7850 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7851 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7852 | return ret; |
| 7853 | } |
| 7854 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7855 | 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] | 7856 | struct btrfs_ref_path *ref_path) |
| 7857 | { |
| 7858 | struct btrfs_key root_key; |
| 7859 | |
| 7860 | root_key.objectid = ref_path->root_objectid; |
| 7861 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7862 | if (is_cowonly_root(ref_path->root_objectid)) |
| 7863 | root_key.offset = 0; |
| 7864 | else |
| 7865 | root_key.offset = (u64)-1; |
| 7866 | |
| 7867 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 7868 | } |
| 7869 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7870 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7871 | struct btrfs_path *path, |
| 7872 | struct btrfs_key *extent_key, |
| 7873 | struct btrfs_block_group_cache *group, |
| 7874 | struct inode *reloc_inode, int pass) |
| 7875 | { |
| 7876 | struct btrfs_trans_handle *trans; |
| 7877 | struct btrfs_root *found_root; |
| 7878 | struct btrfs_ref_path *ref_path = NULL; |
| 7879 | struct disk_extent *new_extents = NULL; |
| 7880 | int nr_extents = 0; |
| 7881 | int loops; |
| 7882 | int ret; |
| 7883 | int level; |
| 7884 | struct btrfs_key first_key; |
| 7885 | u64 prev_block = 0; |
| 7886 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7887 | |
| 7888 | trans = btrfs_start_transaction(extent_root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 7889 | BUG_ON(IS_ERR(trans)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7890 | |
| 7891 | if (extent_key->objectid == 0) { |
| 7892 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 7893 | goto out; |
| 7894 | } |
| 7895 | |
| 7896 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 7897 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7898 | ret = -ENOMEM; |
| 7899 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7900 | } |
| 7901 | |
| 7902 | for (loops = 0; ; loops++) { |
| 7903 | if (loops == 0) { |
| 7904 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 7905 | extent_key->objectid); |
| 7906 | } else { |
| 7907 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 7908 | } |
| 7909 | if (ret < 0) |
| 7910 | goto out; |
| 7911 | if (ret > 0) |
| 7912 | break; |
| 7913 | |
| 7914 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 7915 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 7916 | continue; |
| 7917 | |
| 7918 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 7919 | BUG_ON(!found_root); |
| 7920 | /* |
| 7921 | * for reference counted tree, only process reference paths |
| 7922 | * rooted at the latest committed root. |
| 7923 | */ |
| 7924 | if (found_root->ref_cows && |
| 7925 | ref_path->root_generation != found_root->root_key.offset) |
| 7926 | continue; |
| 7927 | |
| 7928 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7929 | if (pass == 0) { |
| 7930 | /* |
| 7931 | * copy data extents to new locations |
| 7932 | */ |
| 7933 | u64 group_start = group->key.objectid; |
| 7934 | ret = relocate_data_extent(reloc_inode, |
| 7935 | extent_key, |
| 7936 | group_start); |
| 7937 | if (ret < 0) |
| 7938 | goto out; |
| 7939 | break; |
| 7940 | } |
| 7941 | level = 0; |
| 7942 | } else { |
| 7943 | level = ref_path->owner_objectid; |
| 7944 | } |
| 7945 | |
| 7946 | if (prev_block != ref_path->nodes[level]) { |
| 7947 | struct extent_buffer *eb; |
| 7948 | u64 block_start = ref_path->nodes[level]; |
| 7949 | u64 block_size = btrfs_level_size(found_root, level); |
| 7950 | |
| 7951 | eb = read_tree_block(found_root, block_start, |
| 7952 | block_size, 0); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 7953 | if (!eb) { |
| 7954 | ret = -EIO; |
| 7955 | goto out; |
| 7956 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7957 | btrfs_tree_lock(eb); |
| 7958 | BUG_ON(level != btrfs_header_level(eb)); |
| 7959 | |
| 7960 | if (level == 0) |
| 7961 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 7962 | else |
| 7963 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 7964 | |
| 7965 | btrfs_tree_unlock(eb); |
| 7966 | free_extent_buffer(eb); |
| 7967 | prev_block = block_start; |
| 7968 | } |
| 7969 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7970 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7971 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7972 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7973 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7974 | /* |
| 7975 | * try to update data extent references while |
| 7976 | * keeping metadata shared between snapshots. |
| 7977 | */ |
| 7978 | if (pass == 1) { |
| 7979 | ret = relocate_one_path(trans, found_root, |
| 7980 | path, &first_key, ref_path, |
| 7981 | group, reloc_inode); |
| 7982 | if (ret < 0) |
| 7983 | goto out; |
| 7984 | continue; |
| 7985 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7986 | /* |
| 7987 | * use fallback method to process the remaining |
| 7988 | * references. |
| 7989 | */ |
| 7990 | if (!new_extents) { |
| 7991 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7992 | new_extents = kmalloc(sizeof(*new_extents), |
| 7993 | GFP_NOFS); |
| 7994 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7995 | ret = get_new_locations(reloc_inode, |
| 7996 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7997 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7998 | &new_extents, |
| 7999 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8000 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8001 | goto out; |
| 8002 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8003 | ret = replace_one_extent(trans, found_root, |
| 8004 | path, extent_key, |
| 8005 | &first_key, ref_path, |
| 8006 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 8007 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8008 | ret = relocate_tree_block(trans, found_root, path, |
| 8009 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8010 | } |
| 8011 | if (ret < 0) |
| 8012 | goto out; |
| 8013 | } |
| 8014 | ret = 0; |
| 8015 | out: |
| 8016 | btrfs_end_transaction(trans, extent_root); |
| 8017 | kfree(new_extents); |
| 8018 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8019 | return ret; |
| 8020 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8021 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8022 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8023 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 8024 | { |
| 8025 | u64 num_devices; |
| 8026 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 8027 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 8028 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 8029 | /* |
| 8030 | * we add in the count of missing devices because we want |
| 8031 | * to make sure that any RAID levels on a degraded FS |
| 8032 | * continue to be honored. |
| 8033 | */ |
| 8034 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 8035 | root->fs_info->fs_devices->missing_devices; |
| 8036 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8037 | if (num_devices == 1) { |
| 8038 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 8039 | stripped = flags & ~stripped; |
| 8040 | |
| 8041 | /* turn raid0 into single device chunks */ |
| 8042 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 8043 | return stripped; |
| 8044 | |
| 8045 | /* turn mirroring into duplication */ |
| 8046 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 8047 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8048 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 8049 | return flags; |
| 8050 | } else { |
| 8051 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8052 | if (flags & stripped) |
| 8053 | return flags; |
| 8054 | |
| 8055 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 8056 | stripped = flags & ~stripped; |
| 8057 | |
| 8058 | /* switch duplicated blocks with raid1 */ |
| 8059 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 8060 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 8061 | |
| 8062 | /* turn single device chunks into raid0 */ |
| 8063 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 8064 | } |
| 8065 | return flags; |
| 8066 | } |
| 8067 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8068 | static int set_block_group_ro(struct btrfs_block_group_cache *cache) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8069 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8070 | struct btrfs_space_info *sinfo = cache->space_info; |
| 8071 | u64 num_bytes; |
| 8072 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8073 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8074 | if (cache->ro) |
| 8075 | return 0; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8076 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8077 | spin_lock(&sinfo->lock); |
| 8078 | spin_lock(&cache->lock); |
| 8079 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 8080 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 8081 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8082 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
| 8083 | sinfo->bytes_may_use + sinfo->bytes_readonly + |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 8084 | cache->reserved_pinned + num_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8085 | sinfo->bytes_readonly += num_bytes; |
| 8086 | sinfo->bytes_reserved += cache->reserved_pinned; |
| 8087 | cache->reserved_pinned = 0; |
| 8088 | cache->ro = 1; |
| 8089 | ret = 0; |
| 8090 | } |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 8091 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8092 | spin_unlock(&cache->lock); |
| 8093 | spin_unlock(&sinfo->lock); |
| 8094 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8095 | } |
| 8096 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8097 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 8098 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8099 | |
| 8100 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8101 | struct btrfs_trans_handle *trans; |
| 8102 | u64 alloc_flags; |
| 8103 | int ret; |
| 8104 | |
| 8105 | BUG_ON(cache->ro); |
| 8106 | |
| 8107 | trans = btrfs_join_transaction(root, 1); |
| 8108 | BUG_ON(IS_ERR(trans)); |
| 8109 | |
| 8110 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 8111 | if (alloc_flags != cache->flags) |
| 8112 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 8113 | |
| 8114 | ret = set_block_group_ro(cache); |
| 8115 | if (!ret) |
| 8116 | goto out; |
| 8117 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
| 8118 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 8119 | if (ret < 0) |
| 8120 | goto out; |
| 8121 | ret = set_block_group_ro(cache); |
| 8122 | out: |
| 8123 | btrfs_end_transaction(trans, root); |
| 8124 | return ret; |
| 8125 | } |
| 8126 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 8127 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 8128 | struct btrfs_root *root, u64 type) |
| 8129 | { |
| 8130 | u64 alloc_flags = get_alloc_profile(root, type); |
| 8131 | return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 8132 | } |
| 8133 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 8134 | /* |
| 8135 | * helper to account the unused space of all the readonly block group in the |
| 8136 | * list. takes mirrors into account. |
| 8137 | */ |
| 8138 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 8139 | { |
| 8140 | struct btrfs_block_group_cache *block_group; |
| 8141 | u64 free_bytes = 0; |
| 8142 | int factor; |
| 8143 | |
| 8144 | list_for_each_entry(block_group, groups_list, list) { |
| 8145 | spin_lock(&block_group->lock); |
| 8146 | |
| 8147 | if (!block_group->ro) { |
| 8148 | spin_unlock(&block_group->lock); |
| 8149 | continue; |
| 8150 | } |
| 8151 | |
| 8152 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 8153 | BTRFS_BLOCK_GROUP_RAID10 | |
| 8154 | BTRFS_BLOCK_GROUP_DUP)) |
| 8155 | factor = 2; |
| 8156 | else |
| 8157 | factor = 1; |
| 8158 | |
| 8159 | free_bytes += (block_group->key.offset - |
| 8160 | btrfs_block_group_used(&block_group->item)) * |
| 8161 | factor; |
| 8162 | |
| 8163 | spin_unlock(&block_group->lock); |
| 8164 | } |
| 8165 | |
| 8166 | return free_bytes; |
| 8167 | } |
| 8168 | |
| 8169 | /* |
| 8170 | * helper to account the unused space of all the readonly block group in the |
| 8171 | * space_info. takes mirrors into account. |
| 8172 | */ |
| 8173 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 8174 | { |
| 8175 | int i; |
| 8176 | u64 free_bytes = 0; |
| 8177 | |
| 8178 | spin_lock(&sinfo->lock); |
| 8179 | |
| 8180 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 8181 | if (!list_empty(&sinfo->block_groups[i])) |
| 8182 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 8183 | &sinfo->block_groups[i]); |
| 8184 | |
| 8185 | spin_unlock(&sinfo->lock); |
| 8186 | |
| 8187 | return free_bytes; |
| 8188 | } |
| 8189 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8190 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 8191 | struct btrfs_block_group_cache *cache) |
| 8192 | { |
| 8193 | struct btrfs_space_info *sinfo = cache->space_info; |
| 8194 | u64 num_bytes; |
| 8195 | |
| 8196 | BUG_ON(!cache->ro); |
| 8197 | |
| 8198 | spin_lock(&sinfo->lock); |
| 8199 | spin_lock(&cache->lock); |
| 8200 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 8201 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 8202 | sinfo->bytes_readonly -= num_bytes; |
| 8203 | cache->ro = 0; |
| 8204 | spin_unlock(&cache->lock); |
| 8205 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8206 | return 0; |
| 8207 | } |
| 8208 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8209 | /* |
| 8210 | * checks to see if its even possible to relocate this block group. |
| 8211 | * |
| 8212 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 8213 | * ok to go ahead and try. |
| 8214 | */ |
| 8215 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8216 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8217 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8218 | struct btrfs_space_info *space_info; |
| 8219 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 8220 | struct btrfs_device *device; |
| 8221 | int full = 0; |
| 8222 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8223 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8224 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8225 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8226 | /* odd, couldn't find the block group, leave it alone */ |
| 8227 | if (!block_group) |
| 8228 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8229 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8230 | /* no bytes used, we're good */ |
| 8231 | if (!btrfs_block_group_used(&block_group->item)) |
| 8232 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 8233 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8234 | space_info = block_group->space_info; |
| 8235 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 8236 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8237 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8238 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8239 | /* |
| 8240 | * 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] | 8241 | * relocate it unless we're able to allocate a new chunk below. |
| 8242 | * |
| 8243 | * Otherwise, we need to make sure we have room in the space to handle |
| 8244 | * 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] | 8245 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 8246 | if ((space_info->total_bytes != block_group->key.offset) && |
| 8247 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8248 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 8249 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 8250 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8251 | spin_unlock(&space_info->lock); |
| 8252 | goto out; |
| 8253 | } |
| 8254 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8255 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8256 | /* |
| 8257 | * ok we don't have enough space, but maybe we have free space on our |
| 8258 | * devices to allocate new chunks for relocation, so loop through our |
| 8259 | * alloc devices and guess if we have enough space. However, if we |
| 8260 | * were marked as full, then we know there aren't enough chunks, and we |
| 8261 | * can just return. |
| 8262 | */ |
| 8263 | ret = -1; |
| 8264 | if (full) |
| 8265 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 8266 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8267 | mutex_lock(&root->fs_info->chunk_mutex); |
| 8268 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 8269 | u64 min_free = btrfs_block_group_used(&block_group->item); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 8270 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8271 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8272 | /* |
| 8273 | * check to make sure we can actually find a chunk with enough |
| 8274 | * space to fit our block group in. |
| 8275 | */ |
| 8276 | if (device->total_bytes > device->bytes_used + min_free) { |
| 8277 | ret = find_free_dev_extent(NULL, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 8278 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8279 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8280 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8281 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8282 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8283 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8284 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8285 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8286 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8287 | return ret; |
| 8288 | } |
| 8289 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 8290 | static int find_first_block_group(struct btrfs_root *root, |
| 8291 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8292 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8293 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8294 | struct btrfs_key found_key; |
| 8295 | struct extent_buffer *leaf; |
| 8296 | int slot; |
| 8297 | |
| 8298 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 8299 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8300 | goto out; |
| 8301 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8302 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8303 | slot = path->slots[0]; |
| 8304 | leaf = path->nodes[0]; |
| 8305 | if (slot >= btrfs_header_nritems(leaf)) { |
| 8306 | ret = btrfs_next_leaf(root, path); |
| 8307 | if (ret == 0) |
| 8308 | continue; |
| 8309 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8310 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8311 | break; |
| 8312 | } |
| 8313 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 8314 | |
| 8315 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8316 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 8317 | ret = 0; |
| 8318 | goto out; |
| 8319 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8320 | path->slots[0]++; |
| 8321 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8322 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8323 | return ret; |
| 8324 | } |
| 8325 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8326 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 8327 | { |
| 8328 | struct btrfs_block_group_cache *block_group; |
| 8329 | u64 last = 0; |
| 8330 | |
| 8331 | while (1) { |
| 8332 | struct inode *inode; |
| 8333 | |
| 8334 | block_group = btrfs_lookup_first_block_group(info, last); |
| 8335 | while (block_group) { |
| 8336 | spin_lock(&block_group->lock); |
| 8337 | if (block_group->iref) |
| 8338 | break; |
| 8339 | spin_unlock(&block_group->lock); |
| 8340 | block_group = next_block_group(info->tree_root, |
| 8341 | block_group); |
| 8342 | } |
| 8343 | if (!block_group) { |
| 8344 | if (last == 0) |
| 8345 | break; |
| 8346 | last = 0; |
| 8347 | continue; |
| 8348 | } |
| 8349 | |
| 8350 | inode = block_group->inode; |
| 8351 | block_group->iref = 0; |
| 8352 | block_group->inode = NULL; |
| 8353 | spin_unlock(&block_group->lock); |
| 8354 | iput(inode); |
| 8355 | last = block_group->key.objectid + block_group->key.offset; |
| 8356 | btrfs_put_block_group(block_group); |
| 8357 | } |
| 8358 | } |
| 8359 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8360 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 8361 | { |
| 8362 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8363 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8364 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8365 | struct rb_node *n; |
| 8366 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8367 | down_write(&info->extent_commit_sem); |
| 8368 | while (!list_empty(&info->caching_block_groups)) { |
| 8369 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 8370 | struct btrfs_caching_control, list); |
| 8371 | list_del(&caching_ctl->list); |
| 8372 | put_caching_control(caching_ctl); |
| 8373 | } |
| 8374 | up_write(&info->extent_commit_sem); |
| 8375 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8376 | spin_lock(&info->block_group_cache_lock); |
| 8377 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 8378 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 8379 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8380 | rb_erase(&block_group->cache_node, |
| 8381 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8382 | spin_unlock(&info->block_group_cache_lock); |
| 8383 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8384 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8385 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8386 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8387 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8388 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8389 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8390 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8391 | /* |
| 8392 | * We haven't cached this block group, which means we could |
| 8393 | * possibly have excluded extents on this block group. |
| 8394 | */ |
| 8395 | if (block_group->cached == BTRFS_CACHE_NO) |
| 8396 | free_excluded_extents(info->extent_root, block_group); |
| 8397 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8398 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 8399 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8400 | |
| 8401 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8402 | } |
| 8403 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8404 | |
| 8405 | /* now that all the block groups are freed, go through and |
| 8406 | * free all the space_info structs. This is only called during |
| 8407 | * the final stages of unmount, and so we know nobody is |
| 8408 | * using them. We call synchronize_rcu() once before we start, |
| 8409 | * just to be on the safe side. |
| 8410 | */ |
| 8411 | synchronize_rcu(); |
| 8412 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 8413 | release_global_block_rsv(info); |
| 8414 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8415 | while(!list_empty(&info->space_info)) { |
| 8416 | space_info = list_entry(info->space_info.next, |
| 8417 | struct btrfs_space_info, |
| 8418 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8419 | if (space_info->bytes_pinned > 0 || |
| 8420 | space_info->bytes_reserved > 0) { |
| 8421 | WARN_ON(1); |
| 8422 | dump_space_info(space_info, 0, 0); |
| 8423 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8424 | list_del(&space_info->list); |
| 8425 | kfree(space_info); |
| 8426 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8427 | return 0; |
| 8428 | } |
| 8429 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8430 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 8431 | struct btrfs_block_group_cache *cache) |
| 8432 | { |
| 8433 | int index = get_block_group_index(cache); |
| 8434 | |
| 8435 | down_write(&space_info->groups_sem); |
| 8436 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 8437 | up_write(&space_info->groups_sem); |
| 8438 | } |
| 8439 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8440 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 8441 | { |
| 8442 | struct btrfs_path *path; |
| 8443 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8444 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8445 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8446 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8447 | struct btrfs_key key; |
| 8448 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8449 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8450 | int need_clear = 0; |
| 8451 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8452 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8453 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8454 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8455 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8456 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8457 | path = btrfs_alloc_path(); |
| 8458 | if (!path) |
| 8459 | return -ENOMEM; |
| 8460 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8461 | cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); |
| 8462 | if (cache_gen != 0 && |
| 8463 | btrfs_super_generation(&root->fs_info->super_copy) != cache_gen) |
| 8464 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 8465 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 8466 | need_clear = 1; |
Josef Bacik | 8216ef8 | 2010-10-28 16:55:47 -0400 | [diff] [blame] | 8467 | if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen) |
| 8468 | printk(KERN_INFO "btrfs: disk space caching is enabled\n"); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8469 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8470 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8471 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8472 | if (ret > 0) |
| 8473 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8474 | if (ret != 0) |
| 8475 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8476 | leaf = path->nodes[0]; |
| 8477 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8478 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8479 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8480 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8481 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8482 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 8483 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8484 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8485 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 8486 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8487 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8488 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8489 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8490 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8491 | if (need_clear) |
| 8492 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 8493 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8494 | /* |
| 8495 | * we only want to have 32k of ram per block group for keeping |
| 8496 | * track of free space, and if we pass 1/2 of that we want to |
| 8497 | * start converting things over to using bitmaps |
| 8498 | */ |
| 8499 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 8500 | sizeof(struct btrfs_free_space); |
| 8501 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8502 | read_extent_buffer(leaf, &cache->item, |
| 8503 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 8504 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8505 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8506 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8507 | key.objectid = found_key.objectid + found_key.offset; |
| 8508 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8509 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8510 | cache->sectorsize = root->sectorsize; |
| 8511 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8512 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8513 | * We need to exclude the super stripes now so that the space |
| 8514 | * info has super bytes accounted for, otherwise we'll think |
| 8515 | * we have more space than we actually do. |
| 8516 | */ |
| 8517 | exclude_super_stripes(root, cache); |
| 8518 | |
| 8519 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8520 | * check for two cases, either we are full, and therefore |
| 8521 | * don't need to bother with the caching work since we won't |
| 8522 | * find any space, or we are empty, and we can just add all |
| 8523 | * the space in and be done with it. This saves us _alot_ of |
| 8524 | * time, particularly in the full case. |
| 8525 | */ |
| 8526 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8527 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8528 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8529 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8530 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8531 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8532 | cache->cached = BTRFS_CACHE_FINISHED; |
| 8533 | add_new_free_space(cache, root->fs_info, |
| 8534 | found_key.objectid, |
| 8535 | found_key.objectid + |
| 8536 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8537 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8538 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8539 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8540 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 8541 | btrfs_block_group_used(&cache->item), |
| 8542 | &space_info); |
| 8543 | BUG_ON(ret); |
| 8544 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8545 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8546 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8547 | spin_unlock(&cache->space_info->lock); |
| 8548 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8549 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8550 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8551 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8552 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 8553 | |
| 8554 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 8555 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8556 | set_block_group_ro(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8557 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8558 | |
| 8559 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 8560 | if (!(get_alloc_profile(root, space_info->flags) & |
| 8561 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 8562 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8563 | BTRFS_BLOCK_GROUP_DUP))) |
| 8564 | continue; |
| 8565 | /* |
| 8566 | * avoid allocating from un-mirrored block group if there are |
| 8567 | * mirrored block groups. |
| 8568 | */ |
| 8569 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8570 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8571 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8572 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8573 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8574 | |
| 8575 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8576 | ret = 0; |
| 8577 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8578 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8579 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8580 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8581 | |
| 8582 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 8583 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8584 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8585 | u64 size) |
| 8586 | { |
| 8587 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8588 | struct btrfs_root *extent_root; |
| 8589 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8590 | |
| 8591 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8592 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8593 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8594 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8595 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8596 | if (!cache) |
| 8597 | return -ENOMEM; |
| 8598 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8599 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8600 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8601 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8602 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8603 | cache->fs_info = root->fs_info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8604 | |
| 8605 | /* |
| 8606 | * we only want to have 32k of ram per block group for keeping track |
| 8607 | * of free space, and if we pass 1/2 of that we want to start |
| 8608 | * converting things over to using bitmaps |
| 8609 | */ |
| 8610 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 8611 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8612 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8613 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 8614 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8615 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8616 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8617 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8618 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8619 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 8620 | cache->flags = type; |
| 8621 | btrfs_set_block_group_flags(&cache->item, type); |
| 8622 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8623 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8624 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8625 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8626 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8627 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 8628 | chunk_offset + size); |
| 8629 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8630 | free_excluded_extents(root, cache); |
| 8631 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8632 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 8633 | &cache->space_info); |
| 8634 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8635 | |
| 8636 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8637 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8638 | spin_unlock(&cache->space_info->lock); |
| 8639 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8640 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8641 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8642 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8643 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8644 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8645 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 8646 | sizeof(cache->item)); |
| 8647 | BUG_ON(ret); |
| 8648 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 8649 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8650 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8651 | return 0; |
| 8652 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8653 | |
| 8654 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 8655 | struct btrfs_root *root, u64 group_start) |
| 8656 | { |
| 8657 | struct btrfs_path *path; |
| 8658 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8659 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8660 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8661 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8662 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8663 | int ret; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8664 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8665 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8666 | root = root->fs_info->extent_root; |
| 8667 | |
| 8668 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 8669 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8670 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8671 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 8672 | /* |
| 8673 | * Free the reserved super bytes from this block group before |
| 8674 | * remove it. |
| 8675 | */ |
| 8676 | free_excluded_extents(root, block_group); |
| 8677 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8678 | memcpy(&key, &block_group->key, sizeof(key)); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8679 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 8680 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8681 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8682 | factor = 2; |
| 8683 | else |
| 8684 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8685 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8686 | /* make sure this block group isn't part of an allocation cluster */ |
| 8687 | cluster = &root->fs_info->data_alloc_cluster; |
| 8688 | spin_lock(&cluster->refill_lock); |
| 8689 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8690 | spin_unlock(&cluster->refill_lock); |
| 8691 | |
| 8692 | /* |
| 8693 | * make sure this block group isn't part of a metadata |
| 8694 | * allocation cluster |
| 8695 | */ |
| 8696 | cluster = &root->fs_info->meta_alloc_cluster; |
| 8697 | spin_lock(&cluster->refill_lock); |
| 8698 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8699 | spin_unlock(&cluster->refill_lock); |
| 8700 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8701 | path = btrfs_alloc_path(); |
| 8702 | BUG_ON(!path); |
| 8703 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8704 | inode = lookup_free_space_inode(root, block_group, path); |
| 8705 | if (!IS_ERR(inode)) { |
| 8706 | btrfs_orphan_add(trans, inode); |
| 8707 | clear_nlink(inode); |
| 8708 | /* One for the block groups ref */ |
| 8709 | spin_lock(&block_group->lock); |
| 8710 | if (block_group->iref) { |
| 8711 | block_group->iref = 0; |
| 8712 | block_group->inode = NULL; |
| 8713 | spin_unlock(&block_group->lock); |
| 8714 | iput(inode); |
| 8715 | } else { |
| 8716 | spin_unlock(&block_group->lock); |
| 8717 | } |
| 8718 | /* One for our lookup ref */ |
| 8719 | iput(inode); |
| 8720 | } |
| 8721 | |
| 8722 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 8723 | key.offset = block_group->key.objectid; |
| 8724 | key.type = 0; |
| 8725 | |
| 8726 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 8727 | if (ret < 0) |
| 8728 | goto out; |
| 8729 | if (ret > 0) |
| 8730 | btrfs_release_path(tree_root, path); |
| 8731 | if (ret == 0) { |
| 8732 | ret = btrfs_del_item(trans, tree_root, path); |
| 8733 | if (ret) |
| 8734 | goto out; |
| 8735 | btrfs_release_path(tree_root, path); |
| 8736 | } |
| 8737 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8738 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8739 | rb_erase(&block_group->cache_node, |
| 8740 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8741 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8742 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8743 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8744 | /* |
| 8745 | * we must use list_del_init so people can check to see if they |
| 8746 | * are still on the list after taking the semaphore |
| 8747 | */ |
| 8748 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8749 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8750 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8751 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8752 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8753 | |
| 8754 | btrfs_remove_free_space_cache(block_group); |
| 8755 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8756 | spin_lock(&block_group->space_info->lock); |
| 8757 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 8758 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8759 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8760 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8761 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8762 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8763 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8764 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8765 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8766 | btrfs_put_block_group(block_group); |
| 8767 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8768 | |
| 8769 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 8770 | if (ret > 0) |
| 8771 | ret = -EIO; |
| 8772 | if (ret < 0) |
| 8773 | goto out; |
| 8774 | |
| 8775 | ret = btrfs_del_item(trans, root, path); |
| 8776 | out: |
| 8777 | btrfs_free_path(path); |
| 8778 | return ret; |
| 8779 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8780 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8781 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 8782 | { |
| 8783 | struct btrfs_space_info *space_info; |
| 8784 | int ret; |
| 8785 | |
| 8786 | ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0, |
| 8787 | &space_info); |
| 8788 | if (ret) |
| 8789 | return ret; |
| 8790 | |
| 8791 | ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0, |
| 8792 | &space_info); |
| 8793 | if (ret) |
| 8794 | return ret; |
| 8795 | |
| 8796 | ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0, |
| 8797 | &space_info); |
| 8798 | if (ret) |
| 8799 | return ret; |
| 8800 | |
| 8801 | return ret; |
| 8802 | } |
| 8803 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8804 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 8805 | { |
| 8806 | return unpin_extent_range(root, start, end); |
| 8807 | } |
| 8808 | |
| 8809 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 8810 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8811 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 8812 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8813 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8814 | |
| 8815 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 8816 | { |
| 8817 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 8818 | struct btrfs_block_group_cache *cache = NULL; |
| 8819 | u64 group_trimmed; |
| 8820 | u64 start; |
| 8821 | u64 end; |
| 8822 | u64 trimmed = 0; |
| 8823 | int ret = 0; |
| 8824 | |
| 8825 | cache = btrfs_lookup_block_group(fs_info, range->start); |
| 8826 | |
| 8827 | while (cache) { |
| 8828 | if (cache->key.objectid >= (range->start + range->len)) { |
| 8829 | btrfs_put_block_group(cache); |
| 8830 | break; |
| 8831 | } |
| 8832 | |
| 8833 | start = max(range->start, cache->key.objectid); |
| 8834 | end = min(range->start + range->len, |
| 8835 | cache->key.objectid + cache->key.offset); |
| 8836 | |
| 8837 | if (end - start >= range->minlen) { |
| 8838 | if (!block_group_cache_done(cache)) { |
| 8839 | ret = cache_block_group(cache, NULL, root, 0); |
| 8840 | if (!ret) |
| 8841 | wait_block_group_cache_done(cache); |
| 8842 | } |
| 8843 | ret = btrfs_trim_block_group(cache, |
| 8844 | &group_trimmed, |
| 8845 | start, |
| 8846 | end, |
| 8847 | range->minlen); |
| 8848 | |
| 8849 | trimmed += group_trimmed; |
| 8850 | if (ret) { |
| 8851 | btrfs_put_block_group(cache); |
| 8852 | break; |
| 8853 | } |
| 8854 | } |
| 8855 | |
| 8856 | cache = next_block_group(fs_info->tree_root, cache); |
| 8857 | } |
| 8858 | |
| 8859 | range->len = trimmed; |
| 8860 | return ret; |
| 8861 | } |