Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 20 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 22 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 23 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 26 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 27 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 28 | #include "ctree.h" |
| 29 | #include "disk-io.h" |
| 30 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 31 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 32 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 33 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 34 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 36 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 37 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 38 | u64 bytenr, u64 num_bytes, int alloc); |
| 39 | static int update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 40 | u64 num_bytes, int reserve, int sinfo); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 41 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 42 | struct btrfs_root *root, |
| 43 | u64 bytenr, u64 num_bytes, u64 parent, |
| 44 | u64 root_objectid, u64 owner_objectid, |
| 45 | u64 owner_offset, int refs_to_drop, |
| 46 | struct btrfs_delayed_extent_op *extra_op); |
| 47 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 48 | struct extent_buffer *leaf, |
| 49 | struct btrfs_extent_item *ei); |
| 50 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 51 | struct btrfs_root *root, |
| 52 | u64 parent, u64 root_objectid, |
| 53 | u64 flags, u64 owner, u64 offset, |
| 54 | struct btrfs_key *ins, int ref_mod); |
| 55 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 56 | struct btrfs_root *root, |
| 57 | u64 parent, u64 root_objectid, |
| 58 | u64 flags, struct btrfs_disk_key *key, |
| 59 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 60 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 61 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 62 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 63 | static int find_next_key(struct btrfs_path *path, int level, |
| 64 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 65 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 66 | int dump_block_groups); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 67 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 68 | static noinline int |
| 69 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 70 | { |
| 71 | smp_mb(); |
| 72 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 73 | } |
| 74 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 75 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 76 | { |
| 77 | return (cache->flags & bits) == bits; |
| 78 | } |
| 79 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 80 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
| 81 | { |
| 82 | atomic_inc(&cache->count); |
| 83 | } |
| 84 | |
| 85 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 86 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 87 | if (atomic_dec_and_test(&cache->count)) { |
| 88 | WARN_ON(cache->pinned > 0); |
| 89 | WARN_ON(cache->reserved > 0); |
| 90 | WARN_ON(cache->reserved_pinned > 0); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 91 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 92 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 95 | /* |
| 96 | * this adds the block group to the fs_info rb tree for the block group |
| 97 | * cache |
| 98 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 99 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 100 | struct btrfs_block_group_cache *block_group) |
| 101 | { |
| 102 | struct rb_node **p; |
| 103 | struct rb_node *parent = NULL; |
| 104 | struct btrfs_block_group_cache *cache; |
| 105 | |
| 106 | spin_lock(&info->block_group_cache_lock); |
| 107 | p = &info->block_group_cache_tree.rb_node; |
| 108 | |
| 109 | while (*p) { |
| 110 | parent = *p; |
| 111 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 112 | cache_node); |
| 113 | if (block_group->key.objectid < cache->key.objectid) { |
| 114 | p = &(*p)->rb_left; |
| 115 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 116 | p = &(*p)->rb_right; |
| 117 | } else { |
| 118 | spin_unlock(&info->block_group_cache_lock); |
| 119 | return -EEXIST; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | rb_link_node(&block_group->cache_node, parent, p); |
| 124 | rb_insert_color(&block_group->cache_node, |
| 125 | &info->block_group_cache_tree); |
| 126 | spin_unlock(&info->block_group_cache_lock); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * This will return the block group at or after bytenr if contains is 0, else |
| 133 | * it will return the block group that contains the bytenr |
| 134 | */ |
| 135 | static struct btrfs_block_group_cache * |
| 136 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 137 | int contains) |
| 138 | { |
| 139 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 140 | struct rb_node *n; |
| 141 | u64 end, start; |
| 142 | |
| 143 | spin_lock(&info->block_group_cache_lock); |
| 144 | n = info->block_group_cache_tree.rb_node; |
| 145 | |
| 146 | while (n) { |
| 147 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 148 | cache_node); |
| 149 | end = cache->key.objectid + cache->key.offset - 1; |
| 150 | start = cache->key.objectid; |
| 151 | |
| 152 | if (bytenr < start) { |
| 153 | if (!contains && (!ret || start < ret->key.objectid)) |
| 154 | ret = cache; |
| 155 | n = n->rb_left; |
| 156 | } else if (bytenr > start) { |
| 157 | if (contains && bytenr <= end) { |
| 158 | ret = cache; |
| 159 | break; |
| 160 | } |
| 161 | n = n->rb_right; |
| 162 | } else { |
| 163 | ret = cache; |
| 164 | break; |
| 165 | } |
| 166 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 167 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 168 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 169 | spin_unlock(&info->block_group_cache_lock); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 174 | static int add_excluded_extent(struct btrfs_root *root, |
| 175 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 176 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 177 | u64 end = start + num_bytes - 1; |
| 178 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 179 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 180 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 181 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 182 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 185 | static void free_excluded_extents(struct btrfs_root *root, |
| 186 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 187 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 188 | u64 start, end; |
| 189 | |
| 190 | start = cache->key.objectid; |
| 191 | end = start + cache->key.offset - 1; |
| 192 | |
| 193 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 194 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 195 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 196 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 197 | } |
| 198 | |
| 199 | static int exclude_super_stripes(struct btrfs_root *root, |
| 200 | struct btrfs_block_group_cache *cache) |
| 201 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 202 | u64 bytenr; |
| 203 | u64 *logical; |
| 204 | int stripe_len; |
| 205 | int i, nr, ret; |
| 206 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 207 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 208 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 209 | cache->bytes_super += stripe_len; |
| 210 | ret = add_excluded_extent(root, cache->key.objectid, |
| 211 | stripe_len); |
| 212 | BUG_ON(ret); |
| 213 | } |
| 214 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 215 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 216 | bytenr = btrfs_sb_offset(i); |
| 217 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 218 | cache->key.objectid, bytenr, |
| 219 | 0, &logical, &nr, &stripe_len); |
| 220 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 221 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 222 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 223 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 224 | ret = add_excluded_extent(root, logical[nr], |
| 225 | stripe_len); |
| 226 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 227 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 228 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 229 | kfree(logical); |
| 230 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 234 | static struct btrfs_caching_control * |
| 235 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 236 | { |
| 237 | struct btrfs_caching_control *ctl; |
| 238 | |
| 239 | spin_lock(&cache->lock); |
| 240 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 241 | spin_unlock(&cache->lock); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | ctl = cache->caching_ctl; |
| 246 | atomic_inc(&ctl->count); |
| 247 | spin_unlock(&cache->lock); |
| 248 | return ctl; |
| 249 | } |
| 250 | |
| 251 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 252 | { |
| 253 | if (atomic_dec_and_test(&ctl->count)) |
| 254 | kfree(ctl); |
| 255 | } |
| 256 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 257 | /* |
| 258 | * this is only called by cache_block_group, since we could have freed extents |
| 259 | * we need to check the pinned_extents for any extents that can't be used yet |
| 260 | * since their free space will be released as soon as the transaction commits. |
| 261 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 262 | 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] | 263 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 264 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 265 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 266 | int ret; |
| 267 | |
| 268 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 269 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 270 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 271 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 272 | if (ret) |
| 273 | break; |
| 274 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 275 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 276 | start = extent_end + 1; |
| 277 | } else if (extent_start > start && extent_start < end) { |
| 278 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 279 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 280 | ret = btrfs_add_free_space(block_group, start, |
| 281 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 282 | BUG_ON(ret); |
| 283 | start = extent_end + 1; |
| 284 | } else { |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (start < end) { |
| 290 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 291 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 292 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 293 | BUG_ON(ret); |
| 294 | } |
| 295 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 296 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 299 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 300 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 301 | struct btrfs_block_group_cache *block_group = data; |
| 302 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 303 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 304 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 305 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 306 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 307 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 308 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 309 | u64 last = 0; |
| 310 | u32 nritems; |
| 311 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 312 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 313 | path = btrfs_alloc_path(); |
| 314 | if (!path) |
| 315 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 316 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 317 | exclude_super_stripes(extent_root, block_group); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 318 | spin_lock(&block_group->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 319 | block_group->space_info->bytes_readonly += block_group->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 320 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 321 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 322 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 323 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 324 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 325 | * We don't want to deadlock with somebody trying to allocate a new |
| 326 | * extent for the extent root while also trying to search the extent |
| 327 | * root to add free space. So we skip locking and search the commit |
| 328 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 329 | */ |
| 330 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 331 | path->search_commit_root = 1; |
| 332 | path->reada = 2; |
| 333 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 334 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 335 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 336 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 337 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 338 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 339 | /* need to make sure the commit_root doesn't disappear */ |
| 340 | down_read(&fs_info->extent_commit_sem); |
| 341 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 342 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 343 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 344 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 345 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 346 | leaf = path->nodes[0]; |
| 347 | nritems = btrfs_header_nritems(leaf); |
| 348 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 349 | while (1) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 350 | smp_mb(); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 351 | if (fs_info->closing > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 352 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 353 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 354 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 355 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 356 | if (path->slots[0] < nritems) { |
| 357 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 358 | } else { |
| 359 | ret = find_next_key(path, 0, &key); |
| 360 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 361 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 362 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 363 | caching_ctl->progress = last; |
| 364 | btrfs_release_path(extent_root, path); |
| 365 | up_read(&fs_info->extent_commit_sem); |
| 366 | mutex_unlock(&caching_ctl->mutex); |
| 367 | if (btrfs_transaction_in_commit(fs_info)) |
Chris Mason | f36f304 | 2009-07-30 10:04:48 -0400 | [diff] [blame] | 368 | schedule_timeout(1); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 369 | else |
| 370 | cond_resched(); |
| 371 | goto again; |
| 372 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 373 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 374 | if (key.objectid < block_group->key.objectid) { |
| 375 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 376 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 377 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 378 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 379 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 380 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 381 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 382 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 383 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 384 | total_found += add_new_free_space(block_group, |
| 385 | fs_info, last, |
| 386 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 387 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 388 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 389 | if (total_found > (1024 * 1024 * 2)) { |
| 390 | total_found = 0; |
| 391 | wake_up(&caching_ctl->wait); |
| 392 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 393 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 394 | path->slots[0]++; |
| 395 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 396 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 397 | |
| 398 | total_found += add_new_free_space(block_group, fs_info, last, |
| 399 | block_group->key.objectid + |
| 400 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 401 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 402 | |
| 403 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 404 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 405 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 406 | spin_unlock(&block_group->lock); |
| 407 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 408 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 409 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 410 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 411 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 412 | free_excluded_extents(extent_root, block_group); |
| 413 | |
| 414 | mutex_unlock(&caching_ctl->mutex); |
| 415 | wake_up(&caching_ctl->wait); |
| 416 | |
| 417 | put_caching_control(caching_ctl); |
| 418 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 419 | btrfs_put_block_group(block_group); |
| 420 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int cache_block_group(struct btrfs_block_group_cache *cache) |
| 425 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 426 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 427 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 428 | struct task_struct *tsk; |
| 429 | int ret = 0; |
| 430 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 431 | smp_mb(); |
| 432 | if (cache->cached != BTRFS_CACHE_NO) |
| 433 | return 0; |
| 434 | |
| 435 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_KERNEL); |
| 436 | BUG_ON(!caching_ctl); |
| 437 | |
| 438 | INIT_LIST_HEAD(&caching_ctl->list); |
| 439 | mutex_init(&caching_ctl->mutex); |
| 440 | init_waitqueue_head(&caching_ctl->wait); |
| 441 | caching_ctl->block_group = cache; |
| 442 | caching_ctl->progress = cache->key.objectid; |
| 443 | /* one for caching kthread, one for caching block group list */ |
| 444 | atomic_set(&caching_ctl->count, 2); |
| 445 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 446 | spin_lock(&cache->lock); |
| 447 | if (cache->cached != BTRFS_CACHE_NO) { |
| 448 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 449 | kfree(caching_ctl); |
| 450 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 451 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 452 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 453 | cache->cached = BTRFS_CACHE_STARTED; |
| 454 | spin_unlock(&cache->lock); |
| 455 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 456 | down_write(&fs_info->extent_commit_sem); |
| 457 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 458 | up_write(&fs_info->extent_commit_sem); |
| 459 | |
| 460 | atomic_inc(&cache->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 461 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 462 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 463 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 464 | cache->key.objectid); |
| 465 | if (IS_ERR(tsk)) { |
| 466 | ret = PTR_ERR(tsk); |
| 467 | printk(KERN_ERR "error running thread %d\n", ret); |
| 468 | BUG(); |
| 469 | } |
| 470 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 471 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 474 | /* |
| 475 | * return the block group that starts at or after bytenr |
| 476 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 477 | static struct btrfs_block_group_cache * |
| 478 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 479 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 480 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 481 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 482 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 483 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 484 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 487 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 488 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 489 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 490 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 491 | struct btrfs_fs_info *info, |
| 492 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 493 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 494 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 495 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 496 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 497 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 498 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 499 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 500 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 501 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 502 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 503 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 504 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 505 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 506 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 507 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 508 | BTRFS_BLOCK_GROUP_METADATA; |
| 509 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 510 | rcu_read_lock(); |
| 511 | list_for_each_entry_rcu(found, head, list) { |
| 512 | if (found->flags == flags) { |
| 513 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 514 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 515 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 516 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 517 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 518 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 519 | } |
| 520 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 521 | /* |
| 522 | * after adding space to the filesystem, we need to clear the full flags |
| 523 | * on all the space infos. |
| 524 | */ |
| 525 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 526 | { |
| 527 | struct list_head *head = &info->space_info; |
| 528 | struct btrfs_space_info *found; |
| 529 | |
| 530 | rcu_read_lock(); |
| 531 | list_for_each_entry_rcu(found, head, list) |
| 532 | found->full = 0; |
| 533 | rcu_read_unlock(); |
| 534 | } |
| 535 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 536 | static u64 div_factor(u64 num, int factor) |
| 537 | { |
| 538 | if (factor == 10) |
| 539 | return num; |
| 540 | num *= factor; |
| 541 | do_div(num, 10); |
| 542 | return num; |
| 543 | } |
| 544 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 545 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 546 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 547 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 548 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 549 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 550 | u64 last = max(search_hint, search_start); |
| 551 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 552 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 553 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 554 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 555 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 556 | while (1) { |
| 557 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 558 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 559 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 560 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 561 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 562 | last = cache->key.objectid + cache->key.offset; |
| 563 | used = btrfs_block_group_used(&cache->item); |
| 564 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 565 | if ((full_search || !cache->ro) && |
| 566 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 567 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 568 | div_factor(cache->key.offset, factor)) { |
| 569 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 570 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 571 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 572 | goto found; |
| 573 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 574 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 575 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 576 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 577 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 578 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 579 | if (!wrapped) { |
| 580 | last = search_start; |
| 581 | wrapped = 1; |
| 582 | goto again; |
| 583 | } |
| 584 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 585 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 586 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 587 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 588 | goto again; |
| 589 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 590 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 591 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 592 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 593 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 594 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 595 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 596 | { |
| 597 | int ret; |
| 598 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 599 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 600 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 601 | path = btrfs_alloc_path(); |
| 602 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 603 | key.objectid = start; |
| 604 | key.offset = len; |
| 605 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 606 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 607 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 608 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 609 | return ret; |
| 610 | } |
| 611 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 612 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 613 | * helper function to lookup reference count and flags of extent. |
| 614 | * |
| 615 | * the head node for delayed ref is used to store the sum of all the |
| 616 | * reference count modifications queued up in the rbtree. the head |
| 617 | * node may also store the extent flags to set. This way you can check |
| 618 | * to see what the reference count and extent flags would be if all of |
| 619 | * the delayed refs are not processed. |
| 620 | */ |
| 621 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 622 | struct btrfs_root *root, u64 bytenr, |
| 623 | u64 num_bytes, u64 *refs, u64 *flags) |
| 624 | { |
| 625 | struct btrfs_delayed_ref_head *head; |
| 626 | struct btrfs_delayed_ref_root *delayed_refs; |
| 627 | struct btrfs_path *path; |
| 628 | struct btrfs_extent_item *ei; |
| 629 | struct extent_buffer *leaf; |
| 630 | struct btrfs_key key; |
| 631 | u32 item_size; |
| 632 | u64 num_refs; |
| 633 | u64 extent_flags; |
| 634 | int ret; |
| 635 | |
| 636 | path = btrfs_alloc_path(); |
| 637 | if (!path) |
| 638 | return -ENOMEM; |
| 639 | |
| 640 | key.objectid = bytenr; |
| 641 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 642 | key.offset = num_bytes; |
| 643 | if (!trans) { |
| 644 | path->skip_locking = 1; |
| 645 | path->search_commit_root = 1; |
| 646 | } |
| 647 | again: |
| 648 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 649 | &key, path, 0, 0); |
| 650 | if (ret < 0) |
| 651 | goto out_free; |
| 652 | |
| 653 | if (ret == 0) { |
| 654 | leaf = path->nodes[0]; |
| 655 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 656 | if (item_size >= sizeof(*ei)) { |
| 657 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 658 | struct btrfs_extent_item); |
| 659 | num_refs = btrfs_extent_refs(leaf, ei); |
| 660 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 661 | } else { |
| 662 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 663 | struct btrfs_extent_item_v0 *ei0; |
| 664 | BUG_ON(item_size != sizeof(*ei0)); |
| 665 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 666 | struct btrfs_extent_item_v0); |
| 667 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 668 | /* FIXME: this isn't correct for data */ |
| 669 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 670 | #else |
| 671 | BUG(); |
| 672 | #endif |
| 673 | } |
| 674 | BUG_ON(num_refs == 0); |
| 675 | } else { |
| 676 | num_refs = 0; |
| 677 | extent_flags = 0; |
| 678 | ret = 0; |
| 679 | } |
| 680 | |
| 681 | if (!trans) |
| 682 | goto out; |
| 683 | |
| 684 | delayed_refs = &trans->transaction->delayed_refs; |
| 685 | spin_lock(&delayed_refs->lock); |
| 686 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 687 | if (head) { |
| 688 | if (!mutex_trylock(&head->mutex)) { |
| 689 | atomic_inc(&head->node.refs); |
| 690 | spin_unlock(&delayed_refs->lock); |
| 691 | |
| 692 | btrfs_release_path(root->fs_info->extent_root, path); |
| 693 | |
| 694 | mutex_lock(&head->mutex); |
| 695 | mutex_unlock(&head->mutex); |
| 696 | btrfs_put_delayed_ref(&head->node); |
| 697 | goto again; |
| 698 | } |
| 699 | if (head->extent_op && head->extent_op->update_flags) |
| 700 | extent_flags |= head->extent_op->flags_to_set; |
| 701 | else |
| 702 | BUG_ON(num_refs == 0); |
| 703 | |
| 704 | num_refs += head->node.ref_mod; |
| 705 | mutex_unlock(&head->mutex); |
| 706 | } |
| 707 | spin_unlock(&delayed_refs->lock); |
| 708 | out: |
| 709 | WARN_ON(num_refs == 0); |
| 710 | if (refs) |
| 711 | *refs = num_refs; |
| 712 | if (flags) |
| 713 | *flags = extent_flags; |
| 714 | out_free: |
| 715 | btrfs_free_path(path); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 720 | * Back reference rules. Back refs have three main goals: |
| 721 | * |
| 722 | * 1) differentiate between all holders of references to an extent so that |
| 723 | * when a reference is dropped we can make sure it was a valid reference |
| 724 | * before freeing the extent. |
| 725 | * |
| 726 | * 2) Provide enough information to quickly find the holders of an extent |
| 727 | * if we notice a given block is corrupted or bad. |
| 728 | * |
| 729 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 730 | * maintenance. This is actually the same as #2, but with a slightly |
| 731 | * different use case. |
| 732 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 733 | * There are two kinds of back refs. The implicit back refs is optimized |
| 734 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 735 | * back refs of this kind provide information about the block's owner tree |
| 736 | * and the pointer's key. These information allow us to find the block by |
| 737 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 738 | * referenced by their owner trees. The location of tree block is recorded |
| 739 | * in the back refs. Actually the full back refs is generic, and can be |
| 740 | * used in all cases the implicit back refs is used. The major shortcoming |
| 741 | * of the full back refs is its overhead. Every time a tree block gets |
| 742 | * COWed, we have to update back refs entry for all pointers in it. |
| 743 | * |
| 744 | * For a newly allocated tree block, we use implicit back refs for |
| 745 | * pointers in it. This means most tree related operations only involve |
| 746 | * implicit back refs. For a tree block created in old transaction, the |
| 747 | * only way to drop a reference to it is COW it. So we can detect the |
| 748 | * event that tree block loses its owner tree's reference and do the |
| 749 | * back refs conversion. |
| 750 | * |
| 751 | * When a tree block is COW'd through a tree, there are four cases: |
| 752 | * |
| 753 | * The reference count of the block is one and the tree is the block's |
| 754 | * owner tree. Nothing to do in this case. |
| 755 | * |
| 756 | * The reference count of the block is one and the tree is not the |
| 757 | * block's owner tree. In this case, full back refs is used for pointers |
| 758 | * in the block. Remove these full back refs, add implicit back refs for |
| 759 | * every pointers in the new block. |
| 760 | * |
| 761 | * The reference count of the block is greater than one and the tree is |
| 762 | * the block's owner tree. In this case, implicit back refs is used for |
| 763 | * pointers in the block. Add full back refs for every pointers in the |
| 764 | * block, increase lower level extents' reference counts. The original |
| 765 | * implicit back refs are entailed to the new block. |
| 766 | * |
| 767 | * The reference count of the block is greater than one and the tree is |
| 768 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 769 | * the new block, increase lower level extents' reference count. |
| 770 | * |
| 771 | * Back Reference Key composing: |
| 772 | * |
| 773 | * The key objectid corresponds to the first byte in the extent, |
| 774 | * The key type is used to differentiate between types of back refs. |
| 775 | * There are different meanings of the key offset for different types |
| 776 | * of back refs. |
| 777 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 778 | * File extents can be referenced by: |
| 779 | * |
| 780 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 781 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 782 | * - different offsets inside a file (bookend extents in file.c) |
| 783 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 784 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 785 | * |
| 786 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 787 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 788 | * - original offset in the file |
| 789 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 790 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 791 | * The key offset for the implicit back refs is hash of the first |
| 792 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 793 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 794 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 795 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 796 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 797 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 798 | * The key offset for the implicit back refs is the first byte of |
| 799 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 800 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 801 | * When a file extent is allocated, The implicit back refs is used. |
| 802 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 803 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 804 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 805 | * |
| 806 | * When a file extent is removed file truncation, we find the |
| 807 | * corresponding implicit back refs and check the following fields: |
| 808 | * |
| 809 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 810 | * |
| 811 | * Btree extents can be referenced by: |
| 812 | * |
| 813 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 814 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 815 | * Both the implicit back refs and the full back refs for tree blocks |
| 816 | * only consist of key. The key offset for the implicit back refs is |
| 817 | * objectid of block's owner tree. The key offset for the full back refs |
| 818 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 819 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 820 | * When implicit back refs is used, information about the lowest key and |
| 821 | * level of the tree block are required. These information are stored in |
| 822 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 823 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 824 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 825 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 826 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 827 | struct btrfs_root *root, |
| 828 | struct btrfs_path *path, |
| 829 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 830 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 831 | struct btrfs_extent_item *item; |
| 832 | struct btrfs_extent_item_v0 *ei0; |
| 833 | struct btrfs_extent_ref_v0 *ref0; |
| 834 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 835 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 836 | struct btrfs_key key; |
| 837 | struct btrfs_key found_key; |
| 838 | u32 new_size = sizeof(*item); |
| 839 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 840 | int ret; |
| 841 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 842 | leaf = path->nodes[0]; |
| 843 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 844 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 845 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 846 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 847 | struct btrfs_extent_item_v0); |
| 848 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 849 | |
| 850 | if (owner == (u64)-1) { |
| 851 | while (1) { |
| 852 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 853 | ret = btrfs_next_leaf(root, path); |
| 854 | if (ret < 0) |
| 855 | return ret; |
| 856 | BUG_ON(ret > 0); |
| 857 | leaf = path->nodes[0]; |
| 858 | } |
| 859 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 860 | path->slots[0]); |
| 861 | BUG_ON(key.objectid != found_key.objectid); |
| 862 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 863 | path->slots[0]++; |
| 864 | continue; |
| 865 | } |
| 866 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 867 | struct btrfs_extent_ref_v0); |
| 868 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 869 | break; |
| 870 | } |
| 871 | } |
| 872 | btrfs_release_path(root, path); |
| 873 | |
| 874 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 875 | new_size += sizeof(*bi); |
| 876 | |
| 877 | new_size -= sizeof(*ei0); |
| 878 | ret = btrfs_search_slot(trans, root, &key, path, |
| 879 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 880 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 881 | return ret; |
| 882 | BUG_ON(ret); |
| 883 | |
| 884 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 885 | BUG_ON(ret); |
| 886 | |
| 887 | leaf = path->nodes[0]; |
| 888 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 889 | btrfs_set_extent_refs(leaf, item, refs); |
| 890 | /* FIXME: get real generation */ |
| 891 | btrfs_set_extent_generation(leaf, item, 0); |
| 892 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 893 | btrfs_set_extent_flags(leaf, item, |
| 894 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 895 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 896 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 897 | /* FIXME: get first key of the block */ |
| 898 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 899 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 900 | } else { |
| 901 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 902 | } |
| 903 | btrfs_mark_buffer_dirty(leaf); |
| 904 | return 0; |
| 905 | } |
| 906 | #endif |
| 907 | |
| 908 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 909 | { |
| 910 | u32 high_crc = ~(u32)0; |
| 911 | u32 low_crc = ~(u32)0; |
| 912 | __le64 lenum; |
| 913 | |
| 914 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 915 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 916 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 917 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 918 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 919 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 920 | |
| 921 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 922 | } |
| 923 | |
| 924 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 925 | struct btrfs_extent_data_ref *ref) |
| 926 | { |
| 927 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 928 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 929 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 930 | } |
| 931 | |
| 932 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 933 | struct btrfs_extent_data_ref *ref, |
| 934 | u64 root_objectid, u64 owner, u64 offset) |
| 935 | { |
| 936 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 937 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 938 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 939 | return 0; |
| 940 | return 1; |
| 941 | } |
| 942 | |
| 943 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 944 | struct btrfs_root *root, |
| 945 | struct btrfs_path *path, |
| 946 | u64 bytenr, u64 parent, |
| 947 | u64 root_objectid, |
| 948 | u64 owner, u64 offset) |
| 949 | { |
| 950 | struct btrfs_key key; |
| 951 | struct btrfs_extent_data_ref *ref; |
| 952 | struct extent_buffer *leaf; |
| 953 | u32 nritems; |
| 954 | int ret; |
| 955 | int recow; |
| 956 | int err = -ENOENT; |
| 957 | |
| 958 | key.objectid = bytenr; |
| 959 | if (parent) { |
| 960 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 961 | key.offset = parent; |
| 962 | } else { |
| 963 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 964 | key.offset = hash_extent_data_ref(root_objectid, |
| 965 | owner, offset); |
| 966 | } |
| 967 | again: |
| 968 | recow = 0; |
| 969 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 970 | if (ret < 0) { |
| 971 | err = ret; |
| 972 | goto fail; |
| 973 | } |
| 974 | |
| 975 | if (parent) { |
| 976 | if (!ret) |
| 977 | return 0; |
| 978 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 979 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 980 | btrfs_release_path(root, path); |
| 981 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 982 | if (ret < 0) { |
| 983 | err = ret; |
| 984 | goto fail; |
| 985 | } |
| 986 | if (!ret) |
| 987 | return 0; |
| 988 | #endif |
| 989 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 993 | nritems = btrfs_header_nritems(leaf); |
| 994 | while (1) { |
| 995 | if (path->slots[0] >= nritems) { |
| 996 | ret = btrfs_next_leaf(root, path); |
| 997 | if (ret < 0) |
| 998 | err = ret; |
| 999 | if (ret) |
| 1000 | goto fail; |
| 1001 | |
| 1002 | leaf = path->nodes[0]; |
| 1003 | nritems = btrfs_header_nritems(leaf); |
| 1004 | recow = 1; |
| 1005 | } |
| 1006 | |
| 1007 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1008 | if (key.objectid != bytenr || |
| 1009 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1010 | goto fail; |
| 1011 | |
| 1012 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1013 | struct btrfs_extent_data_ref); |
| 1014 | |
| 1015 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1016 | owner, offset)) { |
| 1017 | if (recow) { |
| 1018 | btrfs_release_path(root, path); |
| 1019 | goto again; |
| 1020 | } |
| 1021 | err = 0; |
| 1022 | break; |
| 1023 | } |
| 1024 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1025 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1026 | fail: |
| 1027 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1030 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1031 | struct btrfs_root *root, |
| 1032 | struct btrfs_path *path, |
| 1033 | u64 bytenr, u64 parent, |
| 1034 | u64 root_objectid, u64 owner, |
| 1035 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1036 | { |
| 1037 | struct btrfs_key key; |
| 1038 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1039 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1040 | u32 num_refs; |
| 1041 | int ret; |
| 1042 | |
| 1043 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1044 | if (parent) { |
| 1045 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1046 | key.offset = parent; |
| 1047 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1048 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1049 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1050 | key.offset = hash_extent_data_ref(root_objectid, |
| 1051 | owner, offset); |
| 1052 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1053 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1054 | |
| 1055 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1056 | if (ret && ret != -EEXIST) |
| 1057 | goto fail; |
| 1058 | |
| 1059 | leaf = path->nodes[0]; |
| 1060 | if (parent) { |
| 1061 | struct btrfs_shared_data_ref *ref; |
| 1062 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1063 | struct btrfs_shared_data_ref); |
| 1064 | if (ret == 0) { |
| 1065 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1066 | } else { |
| 1067 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1068 | num_refs += refs_to_add; |
| 1069 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1070 | } |
| 1071 | } else { |
| 1072 | struct btrfs_extent_data_ref *ref; |
| 1073 | while (ret == -EEXIST) { |
| 1074 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1075 | struct btrfs_extent_data_ref); |
| 1076 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1077 | owner, offset)) |
| 1078 | break; |
| 1079 | btrfs_release_path(root, path); |
| 1080 | key.offset++; |
| 1081 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1082 | size); |
| 1083 | if (ret && ret != -EEXIST) |
| 1084 | goto fail; |
| 1085 | |
| 1086 | leaf = path->nodes[0]; |
| 1087 | } |
| 1088 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1089 | struct btrfs_extent_data_ref); |
| 1090 | if (ret == 0) { |
| 1091 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1092 | root_objectid); |
| 1093 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1094 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1095 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1096 | } else { |
| 1097 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1098 | num_refs += refs_to_add; |
| 1099 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1100 | } |
| 1101 | } |
| 1102 | btrfs_mark_buffer_dirty(leaf); |
| 1103 | ret = 0; |
| 1104 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1105 | btrfs_release_path(root, path); |
| 1106 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1107 | } |
| 1108 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1109 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1110 | struct btrfs_root *root, |
| 1111 | struct btrfs_path *path, |
| 1112 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1113 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1114 | struct btrfs_key key; |
| 1115 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1116 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1117 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1118 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1119 | int ret = 0; |
| 1120 | |
| 1121 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1122 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1123 | |
| 1124 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1125 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1126 | struct btrfs_extent_data_ref); |
| 1127 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1128 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1129 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1130 | struct btrfs_shared_data_ref); |
| 1131 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1132 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1133 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1134 | struct btrfs_extent_ref_v0 *ref0; |
| 1135 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1136 | struct btrfs_extent_ref_v0); |
| 1137 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1138 | #endif |
| 1139 | } else { |
| 1140 | BUG(); |
| 1141 | } |
| 1142 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1143 | BUG_ON(num_refs < refs_to_drop); |
| 1144 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1145 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1146 | if (num_refs == 0) { |
| 1147 | ret = btrfs_del_item(trans, root, path); |
| 1148 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1149 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1150 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1151 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1152 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1153 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1154 | else { |
| 1155 | struct btrfs_extent_ref_v0 *ref0; |
| 1156 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1157 | struct btrfs_extent_ref_v0); |
| 1158 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1159 | } |
| 1160 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1161 | btrfs_mark_buffer_dirty(leaf); |
| 1162 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1163 | return ret; |
| 1164 | } |
| 1165 | |
| 1166 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1167 | struct btrfs_path *path, |
| 1168 | struct btrfs_extent_inline_ref *iref) |
| 1169 | { |
| 1170 | struct btrfs_key key; |
| 1171 | struct extent_buffer *leaf; |
| 1172 | struct btrfs_extent_data_ref *ref1; |
| 1173 | struct btrfs_shared_data_ref *ref2; |
| 1174 | u32 num_refs = 0; |
| 1175 | |
| 1176 | leaf = path->nodes[0]; |
| 1177 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1178 | if (iref) { |
| 1179 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1180 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1181 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1182 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1183 | } else { |
| 1184 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1185 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1186 | } |
| 1187 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1188 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1189 | struct btrfs_extent_data_ref); |
| 1190 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1191 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1192 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1193 | struct btrfs_shared_data_ref); |
| 1194 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1195 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1196 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1197 | struct btrfs_extent_ref_v0 *ref0; |
| 1198 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1199 | struct btrfs_extent_ref_v0); |
| 1200 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1201 | #endif |
| 1202 | } else { |
| 1203 | WARN_ON(1); |
| 1204 | } |
| 1205 | return num_refs; |
| 1206 | } |
| 1207 | |
| 1208 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1209 | struct btrfs_root *root, |
| 1210 | struct btrfs_path *path, |
| 1211 | u64 bytenr, u64 parent, |
| 1212 | u64 root_objectid) |
| 1213 | { |
| 1214 | struct btrfs_key key; |
| 1215 | int ret; |
| 1216 | |
| 1217 | key.objectid = bytenr; |
| 1218 | if (parent) { |
| 1219 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1220 | key.offset = parent; |
| 1221 | } else { |
| 1222 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1223 | key.offset = root_objectid; |
| 1224 | } |
| 1225 | |
| 1226 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1227 | if (ret > 0) |
| 1228 | ret = -ENOENT; |
| 1229 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1230 | if (ret == -ENOENT && parent) { |
| 1231 | btrfs_release_path(root, path); |
| 1232 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1233 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1234 | if (ret > 0) |
| 1235 | ret = -ENOENT; |
| 1236 | } |
| 1237 | #endif |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1242 | struct btrfs_root *root, |
| 1243 | struct btrfs_path *path, |
| 1244 | u64 bytenr, u64 parent, |
| 1245 | u64 root_objectid) |
| 1246 | { |
| 1247 | struct btrfs_key key; |
| 1248 | int ret; |
| 1249 | |
| 1250 | key.objectid = bytenr; |
| 1251 | if (parent) { |
| 1252 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1253 | key.offset = parent; |
| 1254 | } else { |
| 1255 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1256 | key.offset = root_objectid; |
| 1257 | } |
| 1258 | |
| 1259 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1260 | btrfs_release_path(root, path); |
| 1261 | return ret; |
| 1262 | } |
| 1263 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1264 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1265 | { |
| 1266 | int type; |
| 1267 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1268 | if (parent > 0) |
| 1269 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1270 | else |
| 1271 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1272 | } else { |
| 1273 | if (parent > 0) |
| 1274 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1275 | else |
| 1276 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1277 | } |
| 1278 | return type; |
| 1279 | } |
| 1280 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1281 | static int find_next_key(struct btrfs_path *path, int level, |
| 1282 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1283 | |
| 1284 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1285 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1286 | if (!path->nodes[level]) |
| 1287 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1288 | if (path->slots[level] + 1 >= |
| 1289 | btrfs_header_nritems(path->nodes[level])) |
| 1290 | continue; |
| 1291 | if (level == 0) |
| 1292 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1293 | path->slots[level] + 1); |
| 1294 | else |
| 1295 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1296 | path->slots[level] + 1); |
| 1297 | return 0; |
| 1298 | } |
| 1299 | return 1; |
| 1300 | } |
| 1301 | |
| 1302 | /* |
| 1303 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1304 | * to the address of inline back ref, and 0 is returned. |
| 1305 | * |
| 1306 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1307 | * should be inserted, and -ENOENT is returned. |
| 1308 | * |
| 1309 | * if insert is true and there are too many inline back refs, the path |
| 1310 | * points to the extent item, and -EAGAIN is returned. |
| 1311 | * |
| 1312 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1313 | * items in the tree are ordered. |
| 1314 | */ |
| 1315 | static noinline_for_stack |
| 1316 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1317 | struct btrfs_root *root, |
| 1318 | struct btrfs_path *path, |
| 1319 | struct btrfs_extent_inline_ref **ref_ret, |
| 1320 | u64 bytenr, u64 num_bytes, |
| 1321 | u64 parent, u64 root_objectid, |
| 1322 | u64 owner, u64 offset, int insert) |
| 1323 | { |
| 1324 | struct btrfs_key key; |
| 1325 | struct extent_buffer *leaf; |
| 1326 | struct btrfs_extent_item *ei; |
| 1327 | struct btrfs_extent_inline_ref *iref; |
| 1328 | u64 flags; |
| 1329 | u64 item_size; |
| 1330 | unsigned long ptr; |
| 1331 | unsigned long end; |
| 1332 | int extra_size; |
| 1333 | int type; |
| 1334 | int want; |
| 1335 | int ret; |
| 1336 | int err = 0; |
| 1337 | |
| 1338 | key.objectid = bytenr; |
| 1339 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1340 | key.offset = num_bytes; |
| 1341 | |
| 1342 | want = extent_ref_type(parent, owner); |
| 1343 | if (insert) { |
| 1344 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1345 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1346 | } else |
| 1347 | extra_size = -1; |
| 1348 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1349 | if (ret < 0) { |
| 1350 | err = ret; |
| 1351 | goto out; |
| 1352 | } |
| 1353 | BUG_ON(ret); |
| 1354 | |
| 1355 | leaf = path->nodes[0]; |
| 1356 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1357 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1358 | if (item_size < sizeof(*ei)) { |
| 1359 | if (!insert) { |
| 1360 | err = -ENOENT; |
| 1361 | goto out; |
| 1362 | } |
| 1363 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1364 | extra_size); |
| 1365 | if (ret < 0) { |
| 1366 | err = ret; |
| 1367 | goto out; |
| 1368 | } |
| 1369 | leaf = path->nodes[0]; |
| 1370 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1371 | } |
| 1372 | #endif |
| 1373 | BUG_ON(item_size < sizeof(*ei)); |
| 1374 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1375 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1376 | flags = btrfs_extent_flags(leaf, ei); |
| 1377 | |
| 1378 | ptr = (unsigned long)(ei + 1); |
| 1379 | end = (unsigned long)ei + item_size; |
| 1380 | |
| 1381 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1382 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1383 | BUG_ON(ptr > end); |
| 1384 | } else { |
| 1385 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1386 | } |
| 1387 | |
| 1388 | err = -ENOENT; |
| 1389 | while (1) { |
| 1390 | if (ptr >= end) { |
| 1391 | WARN_ON(ptr > end); |
| 1392 | break; |
| 1393 | } |
| 1394 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1395 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1396 | if (want < type) |
| 1397 | break; |
| 1398 | if (want > type) { |
| 1399 | ptr += btrfs_extent_inline_ref_size(type); |
| 1400 | continue; |
| 1401 | } |
| 1402 | |
| 1403 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1404 | struct btrfs_extent_data_ref *dref; |
| 1405 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1406 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1407 | owner, offset)) { |
| 1408 | err = 0; |
| 1409 | break; |
| 1410 | } |
| 1411 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1412 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1413 | break; |
| 1414 | } else { |
| 1415 | u64 ref_offset; |
| 1416 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1417 | if (parent > 0) { |
| 1418 | if (parent == ref_offset) { |
| 1419 | err = 0; |
| 1420 | break; |
| 1421 | } |
| 1422 | if (ref_offset < parent) |
| 1423 | break; |
| 1424 | } else { |
| 1425 | if (root_objectid == ref_offset) { |
| 1426 | err = 0; |
| 1427 | break; |
| 1428 | } |
| 1429 | if (ref_offset < root_objectid) |
| 1430 | break; |
| 1431 | } |
| 1432 | } |
| 1433 | ptr += btrfs_extent_inline_ref_size(type); |
| 1434 | } |
| 1435 | if (err == -ENOENT && insert) { |
| 1436 | if (item_size + extra_size >= |
| 1437 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1438 | err = -EAGAIN; |
| 1439 | goto out; |
| 1440 | } |
| 1441 | /* |
| 1442 | * To add new inline back ref, we have to make sure |
| 1443 | * there is no corresponding back ref item. |
| 1444 | * For simplicity, we just do not add new inline back |
| 1445 | * ref if there is any kind of item for this block |
| 1446 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1447 | if (find_next_key(path, 0, &key) == 0 && |
| 1448 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1449 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1450 | err = -EAGAIN; |
| 1451 | goto out; |
| 1452 | } |
| 1453 | } |
| 1454 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1455 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1456 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1457 | path->keep_locks = 0; |
| 1458 | btrfs_unlock_up_safe(path, 1); |
| 1459 | } |
| 1460 | return err; |
| 1461 | } |
| 1462 | |
| 1463 | /* |
| 1464 | * helper to add new inline back ref |
| 1465 | */ |
| 1466 | static noinline_for_stack |
| 1467 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1468 | struct btrfs_root *root, |
| 1469 | struct btrfs_path *path, |
| 1470 | struct btrfs_extent_inline_ref *iref, |
| 1471 | u64 parent, u64 root_objectid, |
| 1472 | u64 owner, u64 offset, int refs_to_add, |
| 1473 | struct btrfs_delayed_extent_op *extent_op) |
| 1474 | { |
| 1475 | struct extent_buffer *leaf; |
| 1476 | struct btrfs_extent_item *ei; |
| 1477 | unsigned long ptr; |
| 1478 | unsigned long end; |
| 1479 | unsigned long item_offset; |
| 1480 | u64 refs; |
| 1481 | int size; |
| 1482 | int type; |
| 1483 | int ret; |
| 1484 | |
| 1485 | leaf = path->nodes[0]; |
| 1486 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1487 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1488 | |
| 1489 | type = extent_ref_type(parent, owner); |
| 1490 | size = btrfs_extent_inline_ref_size(type); |
| 1491 | |
| 1492 | ret = btrfs_extend_item(trans, root, path, size); |
| 1493 | BUG_ON(ret); |
| 1494 | |
| 1495 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1496 | refs = btrfs_extent_refs(leaf, ei); |
| 1497 | refs += refs_to_add; |
| 1498 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1499 | if (extent_op) |
| 1500 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1501 | |
| 1502 | ptr = (unsigned long)ei + item_offset; |
| 1503 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1504 | if (ptr < end - size) |
| 1505 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1506 | end - size - ptr); |
| 1507 | |
| 1508 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1509 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1510 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1511 | struct btrfs_extent_data_ref *dref; |
| 1512 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1513 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1514 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1515 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1516 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1517 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1518 | struct btrfs_shared_data_ref *sref; |
| 1519 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1520 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1521 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1522 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1523 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1524 | } else { |
| 1525 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1526 | } |
| 1527 | btrfs_mark_buffer_dirty(leaf); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1532 | struct btrfs_root *root, |
| 1533 | struct btrfs_path *path, |
| 1534 | struct btrfs_extent_inline_ref **ref_ret, |
| 1535 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1536 | u64 root_objectid, u64 owner, u64 offset) |
| 1537 | { |
| 1538 | int ret; |
| 1539 | |
| 1540 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1541 | bytenr, num_bytes, parent, |
| 1542 | root_objectid, owner, offset, 0); |
| 1543 | if (ret != -ENOENT) |
| 1544 | return ret; |
| 1545 | |
| 1546 | btrfs_release_path(root, path); |
| 1547 | *ref_ret = NULL; |
| 1548 | |
| 1549 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1550 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1551 | root_objectid); |
| 1552 | } else { |
| 1553 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1554 | root_objectid, owner, offset); |
| 1555 | } |
| 1556 | return ret; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * helper to update/remove inline back ref |
| 1561 | */ |
| 1562 | static noinline_for_stack |
| 1563 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1564 | struct btrfs_root *root, |
| 1565 | struct btrfs_path *path, |
| 1566 | struct btrfs_extent_inline_ref *iref, |
| 1567 | int refs_to_mod, |
| 1568 | struct btrfs_delayed_extent_op *extent_op) |
| 1569 | { |
| 1570 | struct extent_buffer *leaf; |
| 1571 | struct btrfs_extent_item *ei; |
| 1572 | struct btrfs_extent_data_ref *dref = NULL; |
| 1573 | struct btrfs_shared_data_ref *sref = NULL; |
| 1574 | unsigned long ptr; |
| 1575 | unsigned long end; |
| 1576 | u32 item_size; |
| 1577 | int size; |
| 1578 | int type; |
| 1579 | int ret; |
| 1580 | u64 refs; |
| 1581 | |
| 1582 | leaf = path->nodes[0]; |
| 1583 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1584 | refs = btrfs_extent_refs(leaf, ei); |
| 1585 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1586 | refs += refs_to_mod; |
| 1587 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1588 | if (extent_op) |
| 1589 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1590 | |
| 1591 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1592 | |
| 1593 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1594 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1595 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1596 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1597 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1598 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1599 | } else { |
| 1600 | refs = 1; |
| 1601 | BUG_ON(refs_to_mod != -1); |
| 1602 | } |
| 1603 | |
| 1604 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1605 | refs += refs_to_mod; |
| 1606 | |
| 1607 | if (refs > 0) { |
| 1608 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1609 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1610 | else |
| 1611 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1612 | } else { |
| 1613 | size = btrfs_extent_inline_ref_size(type); |
| 1614 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1615 | ptr = (unsigned long)iref; |
| 1616 | end = (unsigned long)ei + item_size; |
| 1617 | if (ptr + size < end) |
| 1618 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1619 | end - ptr - size); |
| 1620 | item_size -= size; |
| 1621 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1622 | BUG_ON(ret); |
| 1623 | } |
| 1624 | btrfs_mark_buffer_dirty(leaf); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | static noinline_for_stack |
| 1629 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1630 | struct btrfs_root *root, |
| 1631 | struct btrfs_path *path, |
| 1632 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1633 | u64 root_objectid, u64 owner, |
| 1634 | u64 offset, int refs_to_add, |
| 1635 | struct btrfs_delayed_extent_op *extent_op) |
| 1636 | { |
| 1637 | struct btrfs_extent_inline_ref *iref; |
| 1638 | int ret; |
| 1639 | |
| 1640 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1641 | bytenr, num_bytes, parent, |
| 1642 | root_objectid, owner, offset, 1); |
| 1643 | if (ret == 0) { |
| 1644 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1645 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1646 | refs_to_add, extent_op); |
| 1647 | } else if (ret == -ENOENT) { |
| 1648 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1649 | parent, root_objectid, |
| 1650 | owner, offset, refs_to_add, |
| 1651 | extent_op); |
| 1652 | } |
| 1653 | return ret; |
| 1654 | } |
| 1655 | |
| 1656 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1657 | struct btrfs_root *root, |
| 1658 | struct btrfs_path *path, |
| 1659 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1660 | u64 owner, u64 offset, int refs_to_add) |
| 1661 | { |
| 1662 | int ret; |
| 1663 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1664 | BUG_ON(refs_to_add != 1); |
| 1665 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1666 | parent, root_objectid); |
| 1667 | } else { |
| 1668 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1669 | parent, root_objectid, |
| 1670 | owner, offset, refs_to_add); |
| 1671 | } |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
| 1675 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1676 | struct btrfs_root *root, |
| 1677 | struct btrfs_path *path, |
| 1678 | struct btrfs_extent_inline_ref *iref, |
| 1679 | int refs_to_drop, int is_data) |
| 1680 | { |
| 1681 | int ret; |
| 1682 | |
| 1683 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1684 | if (iref) { |
| 1685 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1686 | -refs_to_drop, NULL); |
| 1687 | } else if (is_data) { |
| 1688 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1689 | } else { |
| 1690 | ret = btrfs_del_item(trans, root, path); |
| 1691 | } |
| 1692 | return ret; |
| 1693 | } |
| 1694 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1695 | static void btrfs_issue_discard(struct block_device *bdev, |
| 1696 | u64 start, u64 len) |
| 1697 | { |
Christoph Hellwig | dd3932e | 2010-09-16 20:51:46 +0200 | [diff] [blame^] | 1698 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1699 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1700 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1701 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 1702 | u64 num_bytes) |
| 1703 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1704 | int ret; |
| 1705 | u64 map_length = num_bytes; |
| 1706 | struct btrfs_multi_bio *multi = NULL; |
| 1707 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1708 | if (!btrfs_test_opt(root, DISCARD)) |
| 1709 | return 0; |
| 1710 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1711 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1712 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1713 | bytenr, &map_length, &multi, 0); |
| 1714 | if (!ret) { |
| 1715 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1716 | int i; |
| 1717 | |
| 1718 | if (map_length > num_bytes) |
| 1719 | map_length = num_bytes; |
| 1720 | |
| 1721 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1722 | btrfs_issue_discard(stripe->dev->bdev, |
| 1723 | stripe->physical, |
| 1724 | map_length); |
| 1725 | } |
| 1726 | kfree(multi); |
| 1727 | } |
| 1728 | |
| 1729 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1730 | } |
| 1731 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1732 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1733 | struct btrfs_root *root, |
| 1734 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1735 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1736 | { |
| 1737 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1738 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1739 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1740 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1741 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1742 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1743 | parent, root_objectid, (int)owner, |
| 1744 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1745 | } else { |
| 1746 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1747 | parent, root_objectid, owner, offset, |
| 1748 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1749 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1750 | return ret; |
| 1751 | } |
| 1752 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1753 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1754 | struct btrfs_root *root, |
| 1755 | u64 bytenr, u64 num_bytes, |
| 1756 | u64 parent, u64 root_objectid, |
| 1757 | u64 owner, u64 offset, int refs_to_add, |
| 1758 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1759 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1760 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1761 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1762 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1763 | u64 refs; |
| 1764 | int ret; |
| 1765 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1766 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1767 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1768 | if (!path) |
| 1769 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1770 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1771 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1772 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1773 | /* this will setup the path even if it fails to insert the back ref */ |
| 1774 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1775 | path, bytenr, num_bytes, parent, |
| 1776 | root_objectid, owner, offset, |
| 1777 | refs_to_add, extent_op); |
| 1778 | if (ret == 0) |
| 1779 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1780 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1781 | if (ret != -EAGAIN) { |
| 1782 | err = ret; |
| 1783 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1784 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1785 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1786 | leaf = path->nodes[0]; |
| 1787 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1788 | refs = btrfs_extent_refs(leaf, item); |
| 1789 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1790 | if (extent_op) |
| 1791 | __run_delayed_extent_op(extent_op, leaf, item); |
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 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1794 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1795 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1796 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1797 | path->leave_spinning = 1; |
| 1798 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1799 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1800 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1801 | path, bytenr, parent, root_objectid, |
| 1802 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1803 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1804 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1805 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1806 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1807 | } |
| 1808 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1809 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1810 | struct btrfs_root *root, |
| 1811 | struct btrfs_delayed_ref_node *node, |
| 1812 | struct btrfs_delayed_extent_op *extent_op, |
| 1813 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1814 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1815 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1816 | struct btrfs_delayed_data_ref *ref; |
| 1817 | struct btrfs_key ins; |
| 1818 | u64 parent = 0; |
| 1819 | u64 ref_root = 0; |
| 1820 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1821 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1822 | ins.objectid = node->bytenr; |
| 1823 | ins.offset = node->num_bytes; |
| 1824 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1825 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1826 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1827 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1828 | parent = ref->parent; |
| 1829 | else |
| 1830 | ref_root = ref->root; |
| 1831 | |
| 1832 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1833 | if (extent_op) { |
| 1834 | BUG_ON(extent_op->update_key); |
| 1835 | flags |= extent_op->flags_to_set; |
| 1836 | } |
| 1837 | ret = alloc_reserved_file_extent(trans, root, |
| 1838 | parent, ref_root, flags, |
| 1839 | ref->objectid, ref->offset, |
| 1840 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1841 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1842 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1843 | node->num_bytes, parent, |
| 1844 | ref_root, ref->objectid, |
| 1845 | ref->offset, node->ref_mod, |
| 1846 | extent_op); |
| 1847 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1848 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1849 | node->num_bytes, parent, |
| 1850 | ref_root, ref->objectid, |
| 1851 | ref->offset, node->ref_mod, |
| 1852 | extent_op); |
| 1853 | } else { |
| 1854 | BUG(); |
| 1855 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1856 | return ret; |
| 1857 | } |
| 1858 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1859 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1860 | struct extent_buffer *leaf, |
| 1861 | struct btrfs_extent_item *ei) |
| 1862 | { |
| 1863 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1864 | if (extent_op->update_flags) { |
| 1865 | flags |= extent_op->flags_to_set; |
| 1866 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1867 | } |
| 1868 | |
| 1869 | if (extent_op->update_key) { |
| 1870 | struct btrfs_tree_block_info *bi; |
| 1871 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1872 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1873 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1878 | struct btrfs_root *root, |
| 1879 | struct btrfs_delayed_ref_node *node, |
| 1880 | struct btrfs_delayed_extent_op *extent_op) |
| 1881 | { |
| 1882 | struct btrfs_key key; |
| 1883 | struct btrfs_path *path; |
| 1884 | struct btrfs_extent_item *ei; |
| 1885 | struct extent_buffer *leaf; |
| 1886 | u32 item_size; |
| 1887 | int ret; |
| 1888 | int err = 0; |
| 1889 | |
| 1890 | path = btrfs_alloc_path(); |
| 1891 | if (!path) |
| 1892 | return -ENOMEM; |
| 1893 | |
| 1894 | key.objectid = node->bytenr; |
| 1895 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1896 | key.offset = node->num_bytes; |
| 1897 | |
| 1898 | path->reada = 1; |
| 1899 | path->leave_spinning = 1; |
| 1900 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1901 | path, 0, 1); |
| 1902 | if (ret < 0) { |
| 1903 | err = ret; |
| 1904 | goto out; |
| 1905 | } |
| 1906 | if (ret > 0) { |
| 1907 | err = -EIO; |
| 1908 | goto out; |
| 1909 | } |
| 1910 | |
| 1911 | leaf = path->nodes[0]; |
| 1912 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1913 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1914 | if (item_size < sizeof(*ei)) { |
| 1915 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1916 | path, (u64)-1, 0); |
| 1917 | if (ret < 0) { |
| 1918 | err = ret; |
| 1919 | goto out; |
| 1920 | } |
| 1921 | leaf = path->nodes[0]; |
| 1922 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1923 | } |
| 1924 | #endif |
| 1925 | BUG_ON(item_size < sizeof(*ei)); |
| 1926 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1927 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1928 | |
| 1929 | btrfs_mark_buffer_dirty(leaf); |
| 1930 | out: |
| 1931 | btrfs_free_path(path); |
| 1932 | return err; |
| 1933 | } |
| 1934 | |
| 1935 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 1936 | struct btrfs_root *root, |
| 1937 | struct btrfs_delayed_ref_node *node, |
| 1938 | struct btrfs_delayed_extent_op *extent_op, |
| 1939 | int insert_reserved) |
| 1940 | { |
| 1941 | int ret = 0; |
| 1942 | struct btrfs_delayed_tree_ref *ref; |
| 1943 | struct btrfs_key ins; |
| 1944 | u64 parent = 0; |
| 1945 | u64 ref_root = 0; |
| 1946 | |
| 1947 | ins.objectid = node->bytenr; |
| 1948 | ins.offset = node->num_bytes; |
| 1949 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 1950 | |
| 1951 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 1952 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1953 | parent = ref->parent; |
| 1954 | else |
| 1955 | ref_root = ref->root; |
| 1956 | |
| 1957 | BUG_ON(node->ref_mod != 1); |
| 1958 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1959 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 1960 | !extent_op->update_key); |
| 1961 | ret = alloc_reserved_tree_block(trans, root, |
| 1962 | parent, ref_root, |
| 1963 | extent_op->flags_to_set, |
| 1964 | &extent_op->key, |
| 1965 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1966 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1967 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1968 | node->num_bytes, parent, ref_root, |
| 1969 | ref->level, 0, 1, extent_op); |
| 1970 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1971 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1972 | node->num_bytes, parent, ref_root, |
| 1973 | ref->level, 0, 1, extent_op); |
| 1974 | } else { |
| 1975 | BUG(); |
| 1976 | } |
| 1977 | return ret; |
| 1978 | } |
| 1979 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1980 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1981 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 1982 | struct btrfs_root *root, |
| 1983 | struct btrfs_delayed_ref_node *node, |
| 1984 | struct btrfs_delayed_extent_op *extent_op, |
| 1985 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1986 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1987 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1988 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1989 | struct btrfs_delayed_ref_head *head; |
| 1990 | /* |
| 1991 | * we've hit the end of the chain and we were supposed |
| 1992 | * to insert this extent into the tree. But, it got |
| 1993 | * deleted before we ever needed to insert it, so all |
| 1994 | * we have to do is clean up the accounting |
| 1995 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1996 | BUG_ON(extent_op); |
| 1997 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1998 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1999 | btrfs_pin_extent(root, node->bytenr, |
| 2000 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2001 | if (head->is_data) { |
| 2002 | ret = btrfs_del_csums(trans, root, |
| 2003 | node->bytenr, |
| 2004 | node->num_bytes); |
| 2005 | BUG_ON(ret); |
| 2006 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2007 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2008 | mutex_unlock(&head->mutex); |
| 2009 | return 0; |
| 2010 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2011 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2012 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2013 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2014 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2015 | insert_reserved); |
| 2016 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2017 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2018 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2019 | insert_reserved); |
| 2020 | else |
| 2021 | BUG(); |
| 2022 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2023 | } |
| 2024 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2025 | static noinline struct btrfs_delayed_ref_node * |
| 2026 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2027 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2028 | struct rb_node *node; |
| 2029 | struct btrfs_delayed_ref_node *ref; |
| 2030 | int action = BTRFS_ADD_DELAYED_REF; |
| 2031 | again: |
| 2032 | /* |
| 2033 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2034 | * this prevents ref count from going down to zero when |
| 2035 | * there still are pending delayed ref. |
| 2036 | */ |
| 2037 | node = rb_prev(&head->node.rb_node); |
| 2038 | while (1) { |
| 2039 | if (!node) |
| 2040 | break; |
| 2041 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2042 | rb_node); |
| 2043 | if (ref->bytenr != head->node.bytenr) |
| 2044 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2045 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2046 | return ref; |
| 2047 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2048 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2049 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2050 | action = BTRFS_DROP_DELAYED_REF; |
| 2051 | goto again; |
| 2052 | } |
| 2053 | return NULL; |
| 2054 | } |
| 2055 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2056 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2057 | struct btrfs_root *root, |
| 2058 | struct list_head *cluster) |
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 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2061 | struct btrfs_delayed_ref_node *ref; |
| 2062 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2063 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2064 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2065 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2066 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2067 | |
| 2068 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2069 | while (1) { |
| 2070 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2071 | /* pick a new head ref from the cluster list */ |
| 2072 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2073 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2074 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2075 | locked_ref = list_entry(cluster->next, |
| 2076 | struct btrfs_delayed_ref_head, cluster); |
| 2077 | |
| 2078 | /* grab the lock that says we are going to process |
| 2079 | * all the refs for this head */ |
| 2080 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2081 | |
| 2082 | /* |
| 2083 | * we may have dropped the spin lock to get the head |
| 2084 | * mutex lock, and that might have given someone else |
| 2085 | * time to free the head. If that's true, it has been |
| 2086 | * removed from our list and we can move on. |
| 2087 | */ |
| 2088 | if (ret == -EAGAIN) { |
| 2089 | locked_ref = NULL; |
| 2090 | count++; |
| 2091 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | /* |
| 2096 | * record the must insert reserved flag before we |
| 2097 | * drop the spin lock. |
| 2098 | */ |
| 2099 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2100 | locked_ref->must_insert_reserved = 0; |
| 2101 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2102 | extent_op = locked_ref->extent_op; |
| 2103 | locked_ref->extent_op = NULL; |
| 2104 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2105 | /* |
| 2106 | * locked_ref is the head node, so we have to go one |
| 2107 | * node back for any delayed ref updates |
| 2108 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2109 | ref = select_delayed_ref(locked_ref); |
| 2110 | if (!ref) { |
| 2111 | /* All delayed refs have been processed, Go ahead |
| 2112 | * and send the head node to run_one_delayed_ref, |
| 2113 | * so that any accounting fixes can happen |
| 2114 | */ |
| 2115 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2116 | |
| 2117 | if (extent_op && must_insert_reserved) { |
| 2118 | kfree(extent_op); |
| 2119 | extent_op = NULL; |
| 2120 | } |
| 2121 | |
| 2122 | if (extent_op) { |
| 2123 | spin_unlock(&delayed_refs->lock); |
| 2124 | |
| 2125 | ret = run_delayed_extent_op(trans, root, |
| 2126 | ref, extent_op); |
| 2127 | BUG_ON(ret); |
| 2128 | kfree(extent_op); |
| 2129 | |
| 2130 | cond_resched(); |
| 2131 | spin_lock(&delayed_refs->lock); |
| 2132 | continue; |
| 2133 | } |
| 2134 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2135 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2136 | locked_ref = NULL; |
| 2137 | } |
| 2138 | |
| 2139 | ref->in_tree = 0; |
| 2140 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2141 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2142 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2143 | spin_unlock(&delayed_refs->lock); |
| 2144 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2145 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2146 | must_insert_reserved); |
| 2147 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2148 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2149 | btrfs_put_delayed_ref(ref); |
| 2150 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2151 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2152 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2153 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2154 | spin_lock(&delayed_refs->lock); |
| 2155 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2156 | return count; |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * this starts processing the delayed reference count updates and |
| 2161 | * extent insertions we have queued up so far. count can be |
| 2162 | * 0, which means to process everything in the tree at the start |
| 2163 | * of the run (but not newly added entries), or it can be some target |
| 2164 | * number you'd like to process. |
| 2165 | */ |
| 2166 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2167 | struct btrfs_root *root, unsigned long count) |
| 2168 | { |
| 2169 | struct rb_node *node; |
| 2170 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2171 | struct btrfs_delayed_ref_node *ref; |
| 2172 | struct list_head cluster; |
| 2173 | int ret; |
| 2174 | int run_all = count == (unsigned long)-1; |
| 2175 | int run_most = 0; |
| 2176 | |
| 2177 | if (root == root->fs_info->extent_root) |
| 2178 | root = root->fs_info->tree_root; |
| 2179 | |
| 2180 | delayed_refs = &trans->transaction->delayed_refs; |
| 2181 | INIT_LIST_HEAD(&cluster); |
| 2182 | again: |
| 2183 | spin_lock(&delayed_refs->lock); |
| 2184 | if (count == 0) { |
| 2185 | count = delayed_refs->num_entries * 2; |
| 2186 | run_most = 1; |
| 2187 | } |
| 2188 | while (1) { |
| 2189 | if (!(run_all || run_most) && |
| 2190 | delayed_refs->num_heads_ready < 64) |
| 2191 | break; |
| 2192 | |
| 2193 | /* |
| 2194 | * go find something we can process in the rbtree. We start at |
| 2195 | * the beginning of the tree, and then build a cluster |
| 2196 | * of refs to process starting at the first one we are able to |
| 2197 | * lock |
| 2198 | */ |
| 2199 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2200 | delayed_refs->run_delayed_start); |
| 2201 | if (ret) |
| 2202 | break; |
| 2203 | |
| 2204 | ret = run_clustered_refs(trans, root, &cluster); |
| 2205 | BUG_ON(ret < 0); |
| 2206 | |
| 2207 | count -= min_t(unsigned long, ret, count); |
| 2208 | |
| 2209 | if (count == 0) |
| 2210 | break; |
| 2211 | } |
| 2212 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2213 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2214 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2215 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2216 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2217 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2218 | |
| 2219 | while (node) { |
| 2220 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2221 | rb_node); |
| 2222 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2223 | struct btrfs_delayed_ref_head *head; |
| 2224 | |
| 2225 | head = btrfs_delayed_node_to_head(ref); |
| 2226 | atomic_inc(&ref->refs); |
| 2227 | |
| 2228 | spin_unlock(&delayed_refs->lock); |
| 2229 | mutex_lock(&head->mutex); |
| 2230 | mutex_unlock(&head->mutex); |
| 2231 | |
| 2232 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2233 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2234 | goto again; |
| 2235 | } |
| 2236 | node = rb_next(node); |
| 2237 | } |
| 2238 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2239 | schedule_timeout(1); |
| 2240 | goto again; |
| 2241 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2242 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2243 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2244 | return 0; |
| 2245 | } |
| 2246 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2247 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2248 | struct btrfs_root *root, |
| 2249 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2250 | int is_data) |
| 2251 | { |
| 2252 | struct btrfs_delayed_extent_op *extent_op; |
| 2253 | int ret; |
| 2254 | |
| 2255 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2256 | if (!extent_op) |
| 2257 | return -ENOMEM; |
| 2258 | |
| 2259 | extent_op->flags_to_set = flags; |
| 2260 | extent_op->update_flags = 1; |
| 2261 | extent_op->update_key = 0; |
| 2262 | extent_op->is_data = is_data ? 1 : 0; |
| 2263 | |
| 2264 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2265 | if (ret) |
| 2266 | kfree(extent_op); |
| 2267 | return ret; |
| 2268 | } |
| 2269 | |
| 2270 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2271 | struct btrfs_root *root, |
| 2272 | struct btrfs_path *path, |
| 2273 | u64 objectid, u64 offset, u64 bytenr) |
| 2274 | { |
| 2275 | struct btrfs_delayed_ref_head *head; |
| 2276 | struct btrfs_delayed_ref_node *ref; |
| 2277 | struct btrfs_delayed_data_ref *data_ref; |
| 2278 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2279 | struct rb_node *node; |
| 2280 | int ret = 0; |
| 2281 | |
| 2282 | ret = -ENOENT; |
| 2283 | delayed_refs = &trans->transaction->delayed_refs; |
| 2284 | spin_lock(&delayed_refs->lock); |
| 2285 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2286 | if (!head) |
| 2287 | goto out; |
| 2288 | |
| 2289 | if (!mutex_trylock(&head->mutex)) { |
| 2290 | atomic_inc(&head->node.refs); |
| 2291 | spin_unlock(&delayed_refs->lock); |
| 2292 | |
| 2293 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2294 | |
| 2295 | mutex_lock(&head->mutex); |
| 2296 | mutex_unlock(&head->mutex); |
| 2297 | btrfs_put_delayed_ref(&head->node); |
| 2298 | return -EAGAIN; |
| 2299 | } |
| 2300 | |
| 2301 | node = rb_prev(&head->node.rb_node); |
| 2302 | if (!node) |
| 2303 | goto out_unlock; |
| 2304 | |
| 2305 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2306 | |
| 2307 | if (ref->bytenr != bytenr) |
| 2308 | goto out_unlock; |
| 2309 | |
| 2310 | ret = 1; |
| 2311 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2312 | goto out_unlock; |
| 2313 | |
| 2314 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2315 | |
| 2316 | node = rb_prev(node); |
| 2317 | if (node) { |
| 2318 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2319 | if (ref->bytenr == bytenr) |
| 2320 | goto out_unlock; |
| 2321 | } |
| 2322 | |
| 2323 | if (data_ref->root != root->root_key.objectid || |
| 2324 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2325 | goto out_unlock; |
| 2326 | |
| 2327 | ret = 0; |
| 2328 | out_unlock: |
| 2329 | mutex_unlock(&head->mutex); |
| 2330 | out: |
| 2331 | spin_unlock(&delayed_refs->lock); |
| 2332 | return ret; |
| 2333 | } |
| 2334 | |
| 2335 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2336 | struct btrfs_root *root, |
| 2337 | struct btrfs_path *path, |
| 2338 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2339 | { |
| 2340 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2341 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2342 | struct btrfs_extent_data_ref *ref; |
| 2343 | struct btrfs_extent_inline_ref *iref; |
| 2344 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2345 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2346 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2347 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2348 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2349 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2350 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2351 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2352 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2353 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2354 | if (ret < 0) |
| 2355 | goto out; |
| 2356 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2357 | |
| 2358 | ret = -ENOENT; |
| 2359 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2360 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2361 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2362 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2363 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2364 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2365 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2366 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2367 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2368 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2369 | ret = 1; |
| 2370 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2371 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2372 | if (item_size < sizeof(*ei)) { |
| 2373 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2374 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2375 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2376 | #endif |
| 2377 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2378 | |
| 2379 | if (item_size != sizeof(*ei) + |
| 2380 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2381 | goto out; |
| 2382 | |
| 2383 | if (btrfs_extent_generation(leaf, ei) <= |
| 2384 | btrfs_root_last_snapshot(&root->root_item)) |
| 2385 | goto out; |
| 2386 | |
| 2387 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2388 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2389 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2390 | goto out; |
| 2391 | |
| 2392 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2393 | if (btrfs_extent_refs(leaf, ei) != |
| 2394 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2395 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2396 | root->root_key.objectid || |
| 2397 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2398 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2399 | goto out; |
| 2400 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2401 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2402 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2403 | return ret; |
| 2404 | } |
| 2405 | |
| 2406 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2407 | struct btrfs_root *root, |
| 2408 | u64 objectid, u64 offset, u64 bytenr) |
| 2409 | { |
| 2410 | struct btrfs_path *path; |
| 2411 | int ret; |
| 2412 | int ret2; |
| 2413 | |
| 2414 | path = btrfs_alloc_path(); |
| 2415 | if (!path) |
| 2416 | return -ENOENT; |
| 2417 | |
| 2418 | do { |
| 2419 | ret = check_committed_ref(trans, root, path, objectid, |
| 2420 | offset, bytenr); |
| 2421 | if (ret && ret != -ENOENT) |
| 2422 | goto out; |
| 2423 | |
| 2424 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2425 | offset, bytenr); |
| 2426 | } while (ret2 == -EAGAIN); |
| 2427 | |
| 2428 | if (ret2 && ret2 != -ENOENT) { |
| 2429 | ret = ret2; |
| 2430 | goto out; |
| 2431 | } |
| 2432 | |
| 2433 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2434 | ret = 0; |
| 2435 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2436 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2437 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2438 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2439 | return ret; |
| 2440 | } |
| 2441 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2442 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2443 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2444 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2445 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2446 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2447 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2448 | u64 root_gen; |
| 2449 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2450 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2451 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2452 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2453 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2454 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2455 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2456 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2457 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2458 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2459 | shared = 0; |
| 2460 | root_gen = root->root_key.offset; |
| 2461 | } else { |
| 2462 | shared = 1; |
| 2463 | root_gen = trans->transid - 1; |
| 2464 | } |
| 2465 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2466 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2467 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2468 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2469 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2470 | struct btrfs_leaf_ref *ref; |
| 2471 | struct btrfs_extent_info *info; |
| 2472 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2473 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2474 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2475 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2476 | goto out; |
| 2477 | } |
| 2478 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2479 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2480 | ref->bytenr = buf->start; |
| 2481 | ref->owner = btrfs_header_owner(buf); |
| 2482 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2483 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2484 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2485 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2486 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2487 | u64 disk_bytenr; |
| 2488 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2489 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2490 | continue; |
| 2491 | fi = btrfs_item_ptr(buf, i, |
| 2492 | struct btrfs_file_extent_item); |
| 2493 | if (btrfs_file_extent_type(buf, fi) == |
| 2494 | BTRFS_FILE_EXTENT_INLINE) |
| 2495 | continue; |
| 2496 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2497 | if (disk_bytenr == 0) |
| 2498 | continue; |
| 2499 | |
| 2500 | info->bytenr = disk_bytenr; |
| 2501 | info->num_bytes = |
| 2502 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2503 | info->objectid = key.objectid; |
| 2504 | info->offset = key.offset; |
| 2505 | info++; |
| 2506 | } |
| 2507 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2508 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2509 | if (ret == -EEXIST && shared) { |
| 2510 | struct btrfs_leaf_ref *old; |
| 2511 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2512 | BUG_ON(!old); |
| 2513 | btrfs_remove_leaf_ref(root, old); |
| 2514 | btrfs_free_leaf_ref(root, old); |
| 2515 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2516 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2517 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2518 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2519 | } |
| 2520 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2521 | return ret; |
| 2522 | } |
| 2523 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2524 | /* when a block goes through cow, we update the reference counts of |
| 2525 | * everything that block points to. The internal pointers of the block |
| 2526 | * can be in just about any order, and it is likely to have clusters of |
| 2527 | * things that are close together and clusters of things that are not. |
| 2528 | * |
| 2529 | * To help reduce the seeks that come with updating all of these reference |
| 2530 | * counts, sort them by byte number before actual updates are done. |
| 2531 | * |
| 2532 | * struct refsort is used to match byte number to slot in the btree block. |
| 2533 | * we sort based on the byte number and then use the slot to actually |
| 2534 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2535 | * |
| 2536 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2537 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2538 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2539 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2540 | */ |
| 2541 | struct refsort { |
| 2542 | u64 bytenr; |
| 2543 | u32 slot; |
| 2544 | }; |
| 2545 | |
| 2546 | /* |
| 2547 | * for passing into sort() |
| 2548 | */ |
| 2549 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2550 | { |
| 2551 | const struct refsort *a = a_void; |
| 2552 | const struct refsort *b = b_void; |
| 2553 | |
| 2554 | if (a->bytenr < b->bytenr) |
| 2555 | return -1; |
| 2556 | if (a->bytenr > b->bytenr) |
| 2557 | return 1; |
| 2558 | return 0; |
| 2559 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2560 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2561 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2562 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2563 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2564 | struct extent_buffer *buf, |
| 2565 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2566 | { |
| 2567 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2568 | u64 num_bytes; |
| 2569 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2570 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2571 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2572 | struct btrfs_key key; |
| 2573 | struct btrfs_file_extent_item *fi; |
| 2574 | int i; |
| 2575 | int level; |
| 2576 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2577 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2578 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2579 | |
| 2580 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2581 | nritems = btrfs_header_nritems(buf); |
| 2582 | level = btrfs_header_level(buf); |
| 2583 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2584 | if (!root->ref_cows && level == 0) |
| 2585 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2586 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2587 | if (inc) |
| 2588 | process_func = btrfs_inc_extent_ref; |
| 2589 | else |
| 2590 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2591 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2592 | if (full_backref) |
| 2593 | parent = buf->start; |
| 2594 | else |
| 2595 | parent = 0; |
| 2596 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2597 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2598 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2599 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2600 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2601 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2602 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2603 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2604 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2605 | BTRFS_FILE_EXTENT_INLINE) |
| 2606 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2607 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2608 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2609 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2610 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2611 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2612 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2613 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2614 | parent, ref_root, key.objectid, |
| 2615 | key.offset); |
| 2616 | if (ret) |
| 2617 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2618 | } else { |
| 2619 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2620 | num_bytes = btrfs_level_size(root, level - 1); |
| 2621 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2622 | parent, ref_root, level - 1, 0); |
| 2623 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2624 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2625 | } |
| 2626 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2627 | return 0; |
| 2628 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2629 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2630 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2631 | } |
| 2632 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2633 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2634 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2635 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2636 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2637 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2638 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2639 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2640 | struct extent_buffer *buf, int full_backref) |
| 2641 | { |
| 2642 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2643 | } |
| 2644 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2645 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2646 | struct btrfs_root *root, |
| 2647 | struct btrfs_path *path, |
| 2648 | struct btrfs_block_group_cache *cache) |
| 2649 | { |
| 2650 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2651 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2652 | unsigned long bi; |
| 2653 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2654 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2655 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2656 | if (ret < 0) |
| 2657 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2658 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2659 | |
| 2660 | leaf = path->nodes[0]; |
| 2661 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2662 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2663 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2664 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2665 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2666 | if (ret) |
| 2667 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2668 | return 0; |
| 2669 | |
| 2670 | } |
| 2671 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2672 | static struct btrfs_block_group_cache * |
| 2673 | next_block_group(struct btrfs_root *root, |
| 2674 | struct btrfs_block_group_cache *cache) |
| 2675 | { |
| 2676 | struct rb_node *node; |
| 2677 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2678 | node = rb_next(&cache->cache_node); |
| 2679 | btrfs_put_block_group(cache); |
| 2680 | if (node) { |
| 2681 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2682 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2683 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2684 | } else |
| 2685 | cache = NULL; |
| 2686 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2687 | return cache; |
| 2688 | } |
| 2689 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2690 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2691 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2692 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2693 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2694 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2695 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2696 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2697 | |
| 2698 | path = btrfs_alloc_path(); |
| 2699 | if (!path) |
| 2700 | return -ENOMEM; |
| 2701 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2702 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2703 | if (last == 0) { |
| 2704 | err = btrfs_run_delayed_refs(trans, root, |
| 2705 | (unsigned long)-1); |
| 2706 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2707 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2708 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2709 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2710 | while (cache) { |
| 2711 | if (cache->dirty) |
| 2712 | break; |
| 2713 | cache = next_block_group(root, cache); |
| 2714 | } |
| 2715 | if (!cache) { |
| 2716 | if (last == 0) |
| 2717 | break; |
| 2718 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2719 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2720 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2721 | |
| 2722 | cache->dirty = 0; |
| 2723 | last = cache->key.objectid + cache->key.offset; |
| 2724 | |
| 2725 | err = write_one_cache_group(trans, root, path, cache); |
| 2726 | BUG_ON(err); |
| 2727 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2728 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2729 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2730 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2731 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2732 | } |
| 2733 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2734 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2735 | { |
| 2736 | struct btrfs_block_group_cache *block_group; |
| 2737 | int readonly = 0; |
| 2738 | |
| 2739 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2740 | if (!block_group || block_group->ro) |
| 2741 | readonly = 1; |
| 2742 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2743 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2744 | return readonly; |
| 2745 | } |
| 2746 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2747 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2748 | u64 total_bytes, u64 bytes_used, |
| 2749 | struct btrfs_space_info **space_info) |
| 2750 | { |
| 2751 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2752 | int i; |
| 2753 | int factor; |
| 2754 | |
| 2755 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 2756 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2757 | factor = 2; |
| 2758 | else |
| 2759 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2760 | |
| 2761 | found = __find_space_info(info, flags); |
| 2762 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2763 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2764 | found->total_bytes += total_bytes; |
| 2765 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2766 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2767 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2768 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2769 | *space_info = found; |
| 2770 | return 0; |
| 2771 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2772 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2773 | if (!found) |
| 2774 | return -ENOMEM; |
| 2775 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2776 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 2777 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2778 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2779 | spin_lock_init(&found->lock); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2780 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
| 2781 | BTRFS_BLOCK_GROUP_SYSTEM | |
| 2782 | BTRFS_BLOCK_GROUP_METADATA); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2783 | found->total_bytes = total_bytes; |
| 2784 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2785 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2786 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2787 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2788 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2789 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2790 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2791 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2792 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2793 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2794 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2795 | return 0; |
| 2796 | } |
| 2797 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2798 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2799 | { |
| 2800 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2801 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2802 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2803 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2804 | if (extra_flags) { |
| 2805 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2806 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2807 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2808 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2809 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2810 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2811 | } |
| 2812 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2813 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2814 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2815 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2816 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2817 | |
| 2818 | if (num_devices == 1) |
| 2819 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2820 | if (num_devices < 4) |
| 2821 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2822 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2823 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2824 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2825 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2826 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2827 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2828 | |
| 2829 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2830 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2831 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2832 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2833 | |
| 2834 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2835 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2836 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2837 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2838 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2839 | return flags; |
| 2840 | } |
| 2841 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2842 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2843 | { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2844 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2845 | flags |= root->fs_info->avail_data_alloc_bits & |
| 2846 | root->fs_info->data_alloc_profile; |
| 2847 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2848 | flags |= root->fs_info->avail_system_alloc_bits & |
| 2849 | root->fs_info->system_alloc_profile; |
| 2850 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2851 | flags |= root->fs_info->avail_metadata_alloc_bits & |
| 2852 | root->fs_info->metadata_alloc_profile; |
| 2853 | return btrfs_reduce_alloc_profile(root, flags); |
| 2854 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2855 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2856 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
| 2857 | { |
| 2858 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2859 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2860 | if (data) |
| 2861 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 2862 | else if (root == root->fs_info->chunk_root) |
| 2863 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 2864 | else |
| 2865 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 2866 | |
| 2867 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 2871 | { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2872 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2873 | BTRFS_BLOCK_GROUP_DATA); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2874 | } |
| 2875 | |
| 2876 | /* |
| 2877 | * This will check the space that the inode allocates from to make sure we have |
| 2878 | * enough space for bytes. |
| 2879 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2880 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2881 | { |
| 2882 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2883 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 2884 | u64 used; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2885 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2886 | |
| 2887 | /* make sure bytes are sectorsize aligned */ |
| 2888 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2889 | |
| 2890 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 2891 | if (!data_sinfo) |
| 2892 | goto alloc; |
| 2893 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2894 | again: |
| 2895 | /* make sure we have enough space to handle the data first */ |
| 2896 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2897 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 2898 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 2899 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 2900 | |
| 2901 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2902 | struct btrfs_trans_handle *trans; |
| 2903 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2904 | /* |
| 2905 | * if we don't have enough free bytes in this space then we need |
| 2906 | * to alloc a new chunk. |
| 2907 | */ |
| 2908 | if (!data_sinfo->full) { |
| 2909 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2910 | |
| 2911 | data_sinfo->force_alloc = 1; |
| 2912 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 2913 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2914 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2915 | trans = btrfs_join_transaction(root, 1); |
| 2916 | if (IS_ERR(trans)) |
| 2917 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2918 | |
| 2919 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2920 | bytes + 2 * 1024 * 1024, |
| 2921 | alloc_target, 0); |
| 2922 | btrfs_end_transaction(trans, root); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 2923 | if (ret < 0) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2924 | return ret; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 2925 | |
| 2926 | if (!data_sinfo) { |
| 2927 | btrfs_set_inode_space_info(root, inode); |
| 2928 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2929 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2930 | goto again; |
| 2931 | } |
| 2932 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2933 | |
| 2934 | /* commit the current transaction and try again */ |
Sage Weil | dd7e0b7 | 2009-09-29 18:38:44 -0400 | [diff] [blame] | 2935 | if (!committed && !root->fs_info->open_ioctl_trans) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2936 | committed = 1; |
| 2937 | trans = btrfs_join_transaction(root, 1); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 2938 | if (IS_ERR(trans)) |
| 2939 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2940 | ret = btrfs_commit_transaction(trans, root); |
| 2941 | if (ret) |
| 2942 | return ret; |
| 2943 | goto again; |
| 2944 | } |
| 2945 | |
Chris Mason | 933b585 | 2010-05-26 11:31:00 -0400 | [diff] [blame] | 2946 | #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] | 2947 | printk(KERN_ERR "no space left, need %llu, %llu bytes_used, " |
| 2948 | "%llu bytes_reserved, " "%llu bytes_pinned, " |
| 2949 | "%llu bytes_readonly, %llu may use %llu total\n", |
| 2950 | (unsigned long long)bytes, |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 2951 | (unsigned long long)data_sinfo->bytes_used, |
| 2952 | (unsigned long long)data_sinfo->bytes_reserved, |
| 2953 | (unsigned long long)data_sinfo->bytes_pinned, |
| 2954 | (unsigned long long)data_sinfo->bytes_readonly, |
| 2955 | (unsigned long long)data_sinfo->bytes_may_use, |
| 2956 | (unsigned long long)data_sinfo->total_bytes); |
Chris Mason | 933b585 | 2010-05-26 11:31:00 -0400 | [diff] [blame] | 2957 | #endif |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2958 | return -ENOSPC; |
| 2959 | } |
| 2960 | data_sinfo->bytes_may_use += bytes; |
| 2961 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 2962 | spin_unlock(&data_sinfo->lock); |
| 2963 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 2964 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | /* |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2968 | * called when we are clearing an delalloc extent from the |
| 2969 | * inode's io_tree or there was an error for whatever reason |
| 2970 | * after calling btrfs_check_data_free_space |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2971 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2972 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2973 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 2974 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2975 | struct btrfs_space_info *data_sinfo; |
| 2976 | |
| 2977 | /* make sure bytes are sectorsize aligned */ |
| 2978 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2979 | |
| 2980 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2981 | spin_lock(&data_sinfo->lock); |
| 2982 | data_sinfo->bytes_may_use -= bytes; |
| 2983 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 2984 | spin_unlock(&data_sinfo->lock); |
| 2985 | } |
| 2986 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2987 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 2988 | { |
| 2989 | struct list_head *head = &info->space_info; |
| 2990 | struct btrfs_space_info *found; |
| 2991 | |
| 2992 | rcu_read_lock(); |
| 2993 | list_for_each_entry_rcu(found, head, list) { |
| 2994 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2995 | found->force_alloc = 1; |
| 2996 | } |
| 2997 | rcu_read_unlock(); |
| 2998 | } |
| 2999 | |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3000 | static int should_alloc_chunk(struct btrfs_space_info *sinfo, |
| 3001 | u64 alloc_bytes) |
| 3002 | { |
| 3003 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
| 3004 | |
| 3005 | if (sinfo->bytes_used + sinfo->bytes_reserved + |
| 3006 | alloc_bytes + 256 * 1024 * 1024 < num_bytes) |
| 3007 | return 0; |
| 3008 | |
| 3009 | if (sinfo->bytes_used + sinfo->bytes_reserved + |
| 3010 | alloc_bytes < div_factor(num_bytes, 8)) |
| 3011 | return 0; |
| 3012 | |
| 3013 | return 1; |
| 3014 | } |
| 3015 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3016 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3017 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3018 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3019 | { |
| 3020 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3021 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3022 | int ret = 0; |
| 3023 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3024 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3025 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3026 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3027 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3028 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3029 | if (!space_info) { |
| 3030 | ret = update_space_info(extent_root->fs_info, flags, |
| 3031 | 0, 0, &space_info); |
| 3032 | BUG_ON(ret); |
| 3033 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3034 | BUG_ON(!space_info); |
| 3035 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3036 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3037 | if (space_info->force_alloc) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3038 | force = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3039 | if (space_info->full) { |
| 3040 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3041 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3042 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3043 | |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3044 | if (!force && !should_alloc_chunk(space_info, alloc_bytes)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3045 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3046 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3047 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3048 | spin_unlock(&space_info->lock); |
| 3049 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3050 | /* |
| 3051 | * if we're doing a data chunk, go ahead and make sure that |
| 3052 | * we keep a reasonable number of metadata chunks allocated in the |
| 3053 | * FS as well. |
| 3054 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3055 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3056 | fs_info->data_chunk_allocations++; |
| 3057 | if (!(fs_info->data_chunk_allocations % |
| 3058 | fs_info->metadata_ratio)) |
| 3059 | force_metadata_allocation(fs_info); |
| 3060 | } |
| 3061 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3062 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3063 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3064 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3065 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3066 | else |
| 3067 | ret = 1; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3068 | space_info->force_alloc = 0; |
| 3069 | spin_unlock(&space_info->lock); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3070 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3071 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3072 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3073 | } |
| 3074 | |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3075 | static int maybe_allocate_chunk(struct btrfs_trans_handle *trans, |
| 3076 | struct btrfs_root *root, |
| 3077 | struct btrfs_space_info *sinfo, u64 num_bytes) |
| 3078 | { |
| 3079 | int ret; |
| 3080 | int end_trans = 0; |
| 3081 | |
| 3082 | if (sinfo->full) |
| 3083 | return 0; |
| 3084 | |
| 3085 | spin_lock(&sinfo->lock); |
| 3086 | ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024); |
| 3087 | spin_unlock(&sinfo->lock); |
| 3088 | if (!ret) |
| 3089 | return 0; |
| 3090 | |
| 3091 | if (!trans) { |
| 3092 | trans = btrfs_join_transaction(root, 1); |
| 3093 | BUG_ON(IS_ERR(trans)); |
| 3094 | end_trans = 1; |
| 3095 | } |
| 3096 | |
| 3097 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3098 | num_bytes + 2 * 1024 * 1024, |
| 3099 | get_alloc_profile(root, sinfo->flags), 0); |
| 3100 | |
| 3101 | if (end_trans) |
| 3102 | btrfs_end_transaction(trans, root); |
| 3103 | |
| 3104 | return ret == 1 ? 1 : 0; |
| 3105 | } |
| 3106 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3107 | /* |
| 3108 | * shrink metadata reservation for delalloc |
| 3109 | */ |
| 3110 | static int shrink_delalloc(struct btrfs_trans_handle *trans, |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3111 | struct btrfs_root *root, u64 to_reclaim) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3112 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3113 | struct btrfs_block_rsv *block_rsv; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3114 | u64 reserved; |
| 3115 | u64 max_reclaim; |
| 3116 | u64 reclaimed = 0; |
| 3117 | int pause = 1; |
| 3118 | int ret; |
| 3119 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3120 | block_rsv = &root->fs_info->delalloc_block_rsv; |
| 3121 | spin_lock(&block_rsv->lock); |
| 3122 | reserved = block_rsv->reserved; |
| 3123 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3124 | |
| 3125 | if (reserved == 0) |
| 3126 | return 0; |
| 3127 | |
| 3128 | max_reclaim = min(reserved, to_reclaim); |
| 3129 | |
| 3130 | while (1) { |
| 3131 | ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0); |
| 3132 | if (!ret) { |
| 3133 | __set_current_state(TASK_INTERRUPTIBLE); |
| 3134 | schedule_timeout(pause); |
| 3135 | pause <<= 1; |
| 3136 | if (pause > HZ / 10) |
| 3137 | pause = HZ / 10; |
| 3138 | } else { |
| 3139 | pause = 1; |
| 3140 | } |
| 3141 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3142 | spin_lock(&block_rsv->lock); |
| 3143 | if (reserved > block_rsv->reserved) |
| 3144 | reclaimed = reserved - block_rsv->reserved; |
| 3145 | reserved = block_rsv->reserved; |
| 3146 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3147 | |
| 3148 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3149 | break; |
| 3150 | |
| 3151 | if (trans && trans->transaction->blocked) |
| 3152 | return -EAGAIN; |
| 3153 | } |
| 3154 | return reclaimed >= to_reclaim; |
| 3155 | } |
| 3156 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3157 | static int should_retry_reserve(struct btrfs_trans_handle *trans, |
| 3158 | struct btrfs_root *root, |
| 3159 | struct btrfs_block_rsv *block_rsv, |
| 3160 | u64 num_bytes, int *retries) |
| 3161 | { |
| 3162 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3163 | int ret; |
| 3164 | |
| 3165 | if ((*retries) > 2) |
| 3166 | return -ENOSPC; |
| 3167 | |
| 3168 | ret = maybe_allocate_chunk(trans, root, space_info, num_bytes); |
| 3169 | if (ret) |
| 3170 | return 1; |
| 3171 | |
| 3172 | if (trans && trans->transaction->in_commit) |
| 3173 | return -ENOSPC; |
| 3174 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3175 | ret = shrink_delalloc(trans, root, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3176 | if (ret) |
| 3177 | return ret; |
| 3178 | |
| 3179 | spin_lock(&space_info->lock); |
| 3180 | if (space_info->bytes_pinned < num_bytes) |
| 3181 | ret = 1; |
| 3182 | spin_unlock(&space_info->lock); |
| 3183 | if (ret) |
| 3184 | return -ENOSPC; |
| 3185 | |
| 3186 | (*retries)++; |
| 3187 | |
| 3188 | if (trans) |
| 3189 | return -EAGAIN; |
| 3190 | |
| 3191 | trans = btrfs_join_transaction(root, 1); |
| 3192 | BUG_ON(IS_ERR(trans)); |
| 3193 | ret = btrfs_commit_transaction(trans, root); |
| 3194 | BUG_ON(ret); |
| 3195 | |
| 3196 | return 1; |
| 3197 | } |
| 3198 | |
| 3199 | static int reserve_metadata_bytes(struct btrfs_block_rsv *block_rsv, |
| 3200 | u64 num_bytes) |
| 3201 | { |
| 3202 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3203 | u64 unused; |
| 3204 | int ret = -ENOSPC; |
| 3205 | |
| 3206 | spin_lock(&space_info->lock); |
| 3207 | unused = space_info->bytes_used + space_info->bytes_reserved + |
| 3208 | space_info->bytes_pinned + space_info->bytes_readonly; |
| 3209 | |
| 3210 | if (unused < space_info->total_bytes) |
| 3211 | unused = space_info->total_bytes - unused; |
| 3212 | else |
| 3213 | unused = 0; |
| 3214 | |
| 3215 | if (unused >= num_bytes) { |
| 3216 | if (block_rsv->priority >= 10) { |
| 3217 | space_info->bytes_reserved += num_bytes; |
| 3218 | ret = 0; |
| 3219 | } else { |
| 3220 | if ((unused + block_rsv->reserved) * |
| 3221 | block_rsv->priority >= |
| 3222 | (num_bytes + block_rsv->reserved) * 10) { |
| 3223 | space_info->bytes_reserved += num_bytes; |
| 3224 | ret = 0; |
| 3225 | } |
| 3226 | } |
| 3227 | } |
| 3228 | spin_unlock(&space_info->lock); |
| 3229 | |
| 3230 | return ret; |
| 3231 | } |
| 3232 | |
| 3233 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3234 | struct btrfs_root *root) |
| 3235 | { |
| 3236 | struct btrfs_block_rsv *block_rsv; |
| 3237 | if (root->ref_cows) |
| 3238 | block_rsv = trans->block_rsv; |
| 3239 | else |
| 3240 | block_rsv = root->block_rsv; |
| 3241 | |
| 3242 | if (!block_rsv) |
| 3243 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3244 | |
| 3245 | return block_rsv; |
| 3246 | } |
| 3247 | |
| 3248 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3249 | u64 num_bytes) |
| 3250 | { |
| 3251 | int ret = -ENOSPC; |
| 3252 | spin_lock(&block_rsv->lock); |
| 3253 | if (block_rsv->reserved >= num_bytes) { |
| 3254 | block_rsv->reserved -= num_bytes; |
| 3255 | if (block_rsv->reserved < block_rsv->size) |
| 3256 | block_rsv->full = 0; |
| 3257 | ret = 0; |
| 3258 | } |
| 3259 | spin_unlock(&block_rsv->lock); |
| 3260 | return ret; |
| 3261 | } |
| 3262 | |
| 3263 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3264 | u64 num_bytes, int update_size) |
| 3265 | { |
| 3266 | spin_lock(&block_rsv->lock); |
| 3267 | block_rsv->reserved += num_bytes; |
| 3268 | if (update_size) |
| 3269 | block_rsv->size += num_bytes; |
| 3270 | else if (block_rsv->reserved >= block_rsv->size) |
| 3271 | block_rsv->full = 1; |
| 3272 | spin_unlock(&block_rsv->lock); |
| 3273 | } |
| 3274 | |
| 3275 | void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3276 | struct btrfs_block_rsv *dest, u64 num_bytes) |
| 3277 | { |
| 3278 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3279 | |
| 3280 | spin_lock(&block_rsv->lock); |
| 3281 | if (num_bytes == (u64)-1) |
| 3282 | num_bytes = block_rsv->size; |
| 3283 | block_rsv->size -= num_bytes; |
| 3284 | if (block_rsv->reserved >= block_rsv->size) { |
| 3285 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3286 | block_rsv->reserved = block_rsv->size; |
| 3287 | block_rsv->full = 1; |
| 3288 | } else { |
| 3289 | num_bytes = 0; |
| 3290 | } |
| 3291 | spin_unlock(&block_rsv->lock); |
| 3292 | |
| 3293 | if (num_bytes > 0) { |
| 3294 | if (dest) { |
| 3295 | block_rsv_add_bytes(dest, num_bytes, 0); |
| 3296 | } else { |
| 3297 | spin_lock(&space_info->lock); |
| 3298 | space_info->bytes_reserved -= num_bytes; |
| 3299 | spin_unlock(&space_info->lock); |
| 3300 | } |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3305 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3306 | { |
| 3307 | int ret; |
| 3308 | |
| 3309 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3310 | if (ret) |
| 3311 | return ret; |
| 3312 | |
| 3313 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3314 | return 0; |
| 3315 | } |
| 3316 | |
| 3317 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3318 | { |
| 3319 | memset(rsv, 0, sizeof(*rsv)); |
| 3320 | spin_lock_init(&rsv->lock); |
| 3321 | atomic_set(&rsv->usage, 1); |
| 3322 | rsv->priority = 6; |
| 3323 | INIT_LIST_HEAD(&rsv->list); |
| 3324 | } |
| 3325 | |
| 3326 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3327 | { |
| 3328 | struct btrfs_block_rsv *block_rsv; |
| 3329 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3330 | u64 alloc_target; |
| 3331 | |
| 3332 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3333 | if (!block_rsv) |
| 3334 | return NULL; |
| 3335 | |
| 3336 | btrfs_init_block_rsv(block_rsv); |
| 3337 | |
| 3338 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 3339 | block_rsv->space_info = __find_space_info(fs_info, |
| 3340 | BTRFS_BLOCK_GROUP_METADATA); |
| 3341 | |
| 3342 | return block_rsv; |
| 3343 | } |
| 3344 | |
| 3345 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3346 | struct btrfs_block_rsv *rsv) |
| 3347 | { |
| 3348 | if (rsv && atomic_dec_and_test(&rsv->usage)) { |
| 3349 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3350 | if (!rsv->durable) |
| 3351 | kfree(rsv); |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | /* |
| 3356 | * make the block_rsv struct be able to capture freed space. |
| 3357 | * the captured space will re-add to the the block_rsv struct |
| 3358 | * after transaction commit |
| 3359 | */ |
| 3360 | void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info, |
| 3361 | struct btrfs_block_rsv *block_rsv) |
| 3362 | { |
| 3363 | block_rsv->durable = 1; |
| 3364 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 3365 | list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list); |
| 3366 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 3367 | } |
| 3368 | |
| 3369 | int btrfs_block_rsv_add(struct btrfs_trans_handle *trans, |
| 3370 | struct btrfs_root *root, |
| 3371 | struct btrfs_block_rsv *block_rsv, |
| 3372 | u64 num_bytes, int *retries) |
| 3373 | { |
| 3374 | int ret; |
| 3375 | |
| 3376 | if (num_bytes == 0) |
| 3377 | return 0; |
| 3378 | again: |
| 3379 | ret = reserve_metadata_bytes(block_rsv, num_bytes); |
| 3380 | if (!ret) { |
| 3381 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3382 | return 0; |
| 3383 | } |
| 3384 | |
| 3385 | ret = should_retry_reserve(trans, root, block_rsv, num_bytes, retries); |
| 3386 | if (ret > 0) |
| 3387 | goto again; |
| 3388 | |
| 3389 | return ret; |
| 3390 | } |
| 3391 | |
| 3392 | int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, |
| 3393 | struct btrfs_root *root, |
| 3394 | struct btrfs_block_rsv *block_rsv, |
| 3395 | u64 min_reserved, int min_factor) |
| 3396 | { |
| 3397 | u64 num_bytes = 0; |
| 3398 | int commit_trans = 0; |
| 3399 | int ret = -ENOSPC; |
| 3400 | |
| 3401 | if (!block_rsv) |
| 3402 | return 0; |
| 3403 | |
| 3404 | spin_lock(&block_rsv->lock); |
| 3405 | if (min_factor > 0) |
| 3406 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3407 | if (min_reserved > num_bytes) |
| 3408 | num_bytes = min_reserved; |
| 3409 | |
| 3410 | if (block_rsv->reserved >= num_bytes) { |
| 3411 | ret = 0; |
| 3412 | } else { |
| 3413 | num_bytes -= block_rsv->reserved; |
| 3414 | if (block_rsv->durable && |
| 3415 | block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes) |
| 3416 | commit_trans = 1; |
| 3417 | } |
| 3418 | spin_unlock(&block_rsv->lock); |
| 3419 | if (!ret) |
| 3420 | return 0; |
| 3421 | |
| 3422 | if (block_rsv->refill_used) { |
| 3423 | ret = reserve_metadata_bytes(block_rsv, num_bytes); |
| 3424 | if (!ret) { |
| 3425 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3426 | return 0; |
| 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | if (commit_trans) { |
| 3431 | if (trans) |
| 3432 | return -EAGAIN; |
| 3433 | |
| 3434 | trans = btrfs_join_transaction(root, 1); |
| 3435 | BUG_ON(IS_ERR(trans)); |
| 3436 | ret = btrfs_commit_transaction(trans, root); |
| 3437 | return 0; |
| 3438 | } |
| 3439 | |
| 3440 | WARN_ON(1); |
| 3441 | printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n", |
| 3442 | block_rsv->size, block_rsv->reserved, |
| 3443 | block_rsv->freed[0], block_rsv->freed[1]); |
| 3444 | |
| 3445 | return -ENOSPC; |
| 3446 | } |
| 3447 | |
| 3448 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3449 | struct btrfs_block_rsv *dst_rsv, |
| 3450 | u64 num_bytes) |
| 3451 | { |
| 3452 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3453 | } |
| 3454 | |
| 3455 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3456 | struct btrfs_block_rsv *block_rsv, |
| 3457 | u64 num_bytes) |
| 3458 | { |
| 3459 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3460 | if (global_rsv->full || global_rsv == block_rsv || |
| 3461 | block_rsv->space_info != global_rsv->space_info) |
| 3462 | global_rsv = NULL; |
| 3463 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3464 | } |
| 3465 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3466 | /* |
| 3467 | * helper to calculate size of global block reservation. |
| 3468 | * the desired value is sum of space used by extent tree, |
| 3469 | * checksum tree and root tree |
| 3470 | */ |
| 3471 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3472 | { |
| 3473 | struct btrfs_space_info *sinfo; |
| 3474 | u64 num_bytes; |
| 3475 | u64 meta_used; |
| 3476 | u64 data_used; |
| 3477 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
| 3478 | #if 0 |
| 3479 | /* |
| 3480 | * per tree used space accounting can be inaccuracy, so we |
| 3481 | * can't rely on it. |
| 3482 | */ |
| 3483 | spin_lock(&fs_info->extent_root->accounting_lock); |
| 3484 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item); |
| 3485 | spin_unlock(&fs_info->extent_root->accounting_lock); |
| 3486 | |
| 3487 | spin_lock(&fs_info->csum_root->accounting_lock); |
| 3488 | num_bytes += btrfs_root_used(&fs_info->csum_root->root_item); |
| 3489 | spin_unlock(&fs_info->csum_root->accounting_lock); |
| 3490 | |
| 3491 | spin_lock(&fs_info->tree_root->accounting_lock); |
| 3492 | num_bytes += btrfs_root_used(&fs_info->tree_root->root_item); |
| 3493 | spin_unlock(&fs_info->tree_root->accounting_lock); |
| 3494 | #endif |
| 3495 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3496 | spin_lock(&sinfo->lock); |
| 3497 | data_used = sinfo->bytes_used; |
| 3498 | spin_unlock(&sinfo->lock); |
| 3499 | |
| 3500 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3501 | spin_lock(&sinfo->lock); |
| 3502 | meta_used = sinfo->bytes_used; |
| 3503 | spin_unlock(&sinfo->lock); |
| 3504 | |
| 3505 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3506 | csum_size * 2; |
| 3507 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3508 | |
| 3509 | if (num_bytes * 3 > meta_used) |
| 3510 | num_bytes = div64_u64(meta_used, 3); |
| 3511 | |
| 3512 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3513 | } |
| 3514 | |
| 3515 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3516 | { |
| 3517 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3518 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3519 | u64 num_bytes; |
| 3520 | |
| 3521 | num_bytes = calc_global_metadata_size(fs_info); |
| 3522 | |
| 3523 | spin_lock(&block_rsv->lock); |
| 3524 | spin_lock(&sinfo->lock); |
| 3525 | |
| 3526 | block_rsv->size = num_bytes; |
| 3527 | |
| 3528 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
| 3529 | sinfo->bytes_reserved + sinfo->bytes_readonly; |
| 3530 | |
| 3531 | if (sinfo->total_bytes > num_bytes) { |
| 3532 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3533 | block_rsv->reserved += num_bytes; |
| 3534 | sinfo->bytes_reserved += num_bytes; |
| 3535 | } |
| 3536 | |
| 3537 | if (block_rsv->reserved >= block_rsv->size) { |
| 3538 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3539 | sinfo->bytes_reserved -= num_bytes; |
| 3540 | block_rsv->reserved = block_rsv->size; |
| 3541 | block_rsv->full = 1; |
| 3542 | } |
| 3543 | #if 0 |
| 3544 | printk(KERN_INFO"global block rsv size %llu reserved %llu\n", |
| 3545 | block_rsv->size, block_rsv->reserved); |
| 3546 | #endif |
| 3547 | spin_unlock(&sinfo->lock); |
| 3548 | spin_unlock(&block_rsv->lock); |
| 3549 | } |
| 3550 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3551 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3552 | { |
| 3553 | struct btrfs_space_info *space_info; |
| 3554 | |
| 3555 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3556 | fs_info->chunk_block_rsv.space_info = space_info; |
| 3557 | fs_info->chunk_block_rsv.priority = 10; |
| 3558 | |
| 3559 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3560 | fs_info->global_block_rsv.space_info = space_info; |
| 3561 | fs_info->global_block_rsv.priority = 10; |
| 3562 | fs_info->global_block_rsv.refill_used = 1; |
| 3563 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3564 | fs_info->trans_block_rsv.space_info = space_info; |
| 3565 | fs_info->empty_block_rsv.space_info = space_info; |
| 3566 | fs_info->empty_block_rsv.priority = 10; |
| 3567 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3568 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3569 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3570 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3571 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3572 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3573 | |
| 3574 | btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv); |
| 3575 | |
| 3576 | btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv); |
| 3577 | |
| 3578 | update_global_block_rsv(fs_info); |
| 3579 | } |
| 3580 | |
| 3581 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3582 | { |
| 3583 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3584 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3585 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3586 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3587 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3588 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3589 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3590 | } |
| 3591 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3592 | static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items) |
| 3593 | { |
| 3594 | return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) * |
| 3595 | 3 * num_items; |
| 3596 | } |
| 3597 | |
| 3598 | int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3599 | struct btrfs_root *root, |
| 3600 | int num_items, int *retries) |
| 3601 | { |
| 3602 | u64 num_bytes; |
| 3603 | int ret; |
| 3604 | |
| 3605 | if (num_items == 0 || root->fs_info->chunk_root == root) |
| 3606 | return 0; |
| 3607 | |
| 3608 | num_bytes = calc_trans_metadata_size(root, num_items); |
| 3609 | ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv, |
| 3610 | num_bytes, retries); |
| 3611 | if (!ret) { |
| 3612 | trans->bytes_reserved += num_bytes; |
| 3613 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 3614 | } |
| 3615 | return ret; |
| 3616 | } |
| 3617 | |
| 3618 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 3619 | struct btrfs_root *root) |
| 3620 | { |
| 3621 | if (!trans->bytes_reserved) |
| 3622 | return; |
| 3623 | |
| 3624 | BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv); |
| 3625 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3626 | trans->bytes_reserved); |
| 3627 | trans->bytes_reserved = 0; |
| 3628 | } |
| 3629 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3630 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3631 | struct inode *inode) |
| 3632 | { |
| 3633 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3634 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3635 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 3636 | |
| 3637 | /* |
| 3638 | * one for deleting orphan item, one for updating inode and |
| 3639 | * two for calling btrfs_truncate_inode_items. |
| 3640 | * |
| 3641 | * btrfs_truncate_inode_items is a delete operation, it frees |
| 3642 | * more space than it uses in most cases. So two units of |
| 3643 | * metadata space should be enough for calling it many times. |
| 3644 | * If all of the metadata space is used, we can commit |
| 3645 | * transaction and use space it freed. |
| 3646 | */ |
| 3647 | u64 num_bytes = calc_trans_metadata_size(root, 4); |
| 3648 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3649 | } |
| 3650 | |
| 3651 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 3652 | { |
| 3653 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3654 | u64 num_bytes = calc_trans_metadata_size(root, 4); |
| 3655 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 3656 | } |
| 3657 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3658 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3659 | struct btrfs_pending_snapshot *pending) |
| 3660 | { |
| 3661 | struct btrfs_root *root = pending->root; |
| 3662 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3663 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 3664 | /* |
| 3665 | * two for root back/forward refs, two for directory entries |
| 3666 | * and one for root of the snapshot. |
| 3667 | */ |
| 3668 | u64 num_bytes = calc_trans_metadata_size(root, 5); |
| 3669 | dst_rsv->space_info = src_rsv->space_info; |
| 3670 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3671 | } |
| 3672 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3673 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes) |
| 3674 | { |
| 3675 | return num_bytes >>= 3; |
| 3676 | } |
| 3677 | |
| 3678 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 3679 | { |
| 3680 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3681 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
| 3682 | u64 to_reserve; |
| 3683 | int nr_extents; |
| 3684 | int retries = 0; |
| 3685 | int ret; |
| 3686 | |
| 3687 | if (btrfs_transaction_in_commit(root->fs_info)) |
| 3688 | schedule_timeout(1); |
| 3689 | |
| 3690 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
| 3691 | again: |
| 3692 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 3693 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1; |
| 3694 | if (nr_extents > BTRFS_I(inode)->reserved_extents) { |
| 3695 | nr_extents -= BTRFS_I(inode)->reserved_extents; |
| 3696 | to_reserve = calc_trans_metadata_size(root, nr_extents); |
| 3697 | } else { |
| 3698 | nr_extents = 0; |
| 3699 | to_reserve = 0; |
| 3700 | } |
| 3701 | |
| 3702 | to_reserve += calc_csum_metadata_size(inode, num_bytes); |
| 3703 | ret = reserve_metadata_bytes(block_rsv, to_reserve); |
| 3704 | if (ret) { |
| 3705 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 3706 | ret = should_retry_reserve(NULL, root, block_rsv, to_reserve, |
| 3707 | &retries); |
| 3708 | if (ret > 0) |
| 3709 | goto again; |
| 3710 | return ret; |
| 3711 | } |
| 3712 | |
| 3713 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 3714 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
| 3715 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 3716 | |
| 3717 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 3718 | |
| 3719 | if (block_rsv->size > 512 * 1024 * 1024) |
| 3720 | shrink_delalloc(NULL, root, to_reserve); |
| 3721 | |
| 3722 | return 0; |
| 3723 | } |
| 3724 | |
| 3725 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 3726 | { |
| 3727 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3728 | u64 to_free; |
| 3729 | int nr_extents; |
| 3730 | |
| 3731 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
| 3732 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
| 3733 | |
| 3734 | spin_lock(&BTRFS_I(inode)->accounting_lock); |
| 3735 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents); |
| 3736 | if (nr_extents < BTRFS_I(inode)->reserved_extents) { |
| 3737 | nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents; |
| 3738 | BTRFS_I(inode)->reserved_extents -= nr_extents; |
| 3739 | } else { |
| 3740 | nr_extents = 0; |
| 3741 | } |
| 3742 | spin_unlock(&BTRFS_I(inode)->accounting_lock); |
| 3743 | |
| 3744 | to_free = calc_csum_metadata_size(inode, num_bytes); |
| 3745 | if (nr_extents > 0) |
| 3746 | to_free += calc_trans_metadata_size(root, nr_extents); |
| 3747 | |
| 3748 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 3749 | to_free); |
| 3750 | } |
| 3751 | |
| 3752 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 3753 | { |
| 3754 | int ret; |
| 3755 | |
| 3756 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 3757 | if (ret) |
| 3758 | return ret; |
| 3759 | |
| 3760 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 3761 | if (ret) { |
| 3762 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 3763 | return ret; |
| 3764 | } |
| 3765 | |
| 3766 | return 0; |
| 3767 | } |
| 3768 | |
| 3769 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 3770 | { |
| 3771 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 3772 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 3773 | } |
| 3774 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3775 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 3776 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3777 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3778 | { |
| 3779 | struct btrfs_block_group_cache *cache; |
| 3780 | struct btrfs_fs_info *info = root->fs_info; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3781 | int factor; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3782 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3783 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3784 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 3785 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3786 | /* block accounting for super block */ |
| 3787 | spin_lock(&info->delalloc_lock); |
| 3788 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 3789 | if (alloc) |
| 3790 | old_val += num_bytes; |
| 3791 | else |
| 3792 | old_val -= num_bytes; |
| 3793 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3794 | spin_unlock(&info->delalloc_lock); |
| 3795 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3796 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3797 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3798 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3799 | return -1; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3800 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 3801 | BTRFS_BLOCK_GROUP_RAID1 | |
| 3802 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3803 | factor = 2; |
| 3804 | else |
| 3805 | factor = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3806 | byte_in_group = bytenr - cache->key.objectid; |
| 3807 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3808 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3809 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3810 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3811 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3812 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3813 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3814 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3815 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3816 | btrfs_set_block_group_used(&cache->item, old_val); |
| 3817 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3818 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3819 | cache->space_info->bytes_used += num_bytes; |
| 3820 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3821 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3822 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3823 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3824 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3825 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3826 | cache->pinned += num_bytes; |
| 3827 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3828 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3829 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3830 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3831 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3832 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3833 | set_extent_dirty(info->pinned_extents, |
| 3834 | bytenr, bytenr + num_bytes - 1, |
| 3835 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 3836 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3837 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3838 | total -= num_bytes; |
| 3839 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3840 | } |
| 3841 | return 0; |
| 3842 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3843 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3844 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 3845 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3846 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3847 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3848 | |
| 3849 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 3850 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3851 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3852 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3853 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3854 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3855 | |
| 3856 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3857 | } |
| 3858 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3859 | static int pin_down_extent(struct btrfs_root *root, |
| 3860 | struct btrfs_block_group_cache *cache, |
| 3861 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3862 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3863 | spin_lock(&cache->space_info->lock); |
| 3864 | spin_lock(&cache->lock); |
| 3865 | cache->pinned += num_bytes; |
| 3866 | cache->space_info->bytes_pinned += num_bytes; |
| 3867 | if (reserved) { |
| 3868 | cache->reserved -= num_bytes; |
| 3869 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3870 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3871 | spin_unlock(&cache->lock); |
| 3872 | spin_unlock(&cache->space_info->lock); |
| 3873 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3874 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 3875 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 3876 | return 0; |
| 3877 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3878 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3879 | /* |
| 3880 | * this function must be called within transaction |
| 3881 | */ |
| 3882 | int btrfs_pin_extent(struct btrfs_root *root, |
| 3883 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3884 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3885 | struct btrfs_block_group_cache *cache; |
| 3886 | |
| 3887 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 3888 | BUG_ON(!cache); |
| 3889 | |
| 3890 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 3891 | |
| 3892 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3893 | return 0; |
| 3894 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3895 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3896 | /* |
| 3897 | * update size of reserved extents. this function may return -EAGAIN |
| 3898 | * if 'reserve' is true or 'sinfo' is false. |
| 3899 | */ |
| 3900 | static int update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 3901 | u64 num_bytes, int reserve, int sinfo) |
| 3902 | { |
| 3903 | int ret = 0; |
| 3904 | if (sinfo) { |
| 3905 | struct btrfs_space_info *space_info = cache->space_info; |
| 3906 | spin_lock(&space_info->lock); |
| 3907 | spin_lock(&cache->lock); |
| 3908 | if (reserve) { |
| 3909 | if (cache->ro) { |
| 3910 | ret = -EAGAIN; |
| 3911 | } else { |
| 3912 | cache->reserved += num_bytes; |
| 3913 | space_info->bytes_reserved += num_bytes; |
| 3914 | } |
| 3915 | } else { |
| 3916 | if (cache->ro) |
| 3917 | space_info->bytes_readonly += num_bytes; |
| 3918 | cache->reserved -= num_bytes; |
| 3919 | space_info->bytes_reserved -= num_bytes; |
| 3920 | } |
| 3921 | spin_unlock(&cache->lock); |
| 3922 | spin_unlock(&space_info->lock); |
| 3923 | } else { |
| 3924 | spin_lock(&cache->lock); |
| 3925 | if (cache->ro) { |
| 3926 | ret = -EAGAIN; |
| 3927 | } else { |
| 3928 | if (reserve) |
| 3929 | cache->reserved += num_bytes; |
| 3930 | else |
| 3931 | cache->reserved -= num_bytes; |
| 3932 | } |
| 3933 | spin_unlock(&cache->lock); |
| 3934 | } |
| 3935 | return ret; |
| 3936 | } |
| 3937 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3938 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 3939 | struct btrfs_root *root) |
| 3940 | { |
| 3941 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3942 | struct btrfs_caching_control *next; |
| 3943 | struct btrfs_caching_control *caching_ctl; |
| 3944 | struct btrfs_block_group_cache *cache; |
| 3945 | |
| 3946 | down_write(&fs_info->extent_commit_sem); |
| 3947 | |
| 3948 | list_for_each_entry_safe(caching_ctl, next, |
| 3949 | &fs_info->caching_block_groups, list) { |
| 3950 | cache = caching_ctl->block_group; |
| 3951 | if (block_group_cache_done(cache)) { |
| 3952 | cache->last_byte_to_unpin = (u64)-1; |
| 3953 | list_del_init(&caching_ctl->list); |
| 3954 | put_caching_control(caching_ctl); |
| 3955 | } else { |
| 3956 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 3961 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 3962 | else |
| 3963 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 3964 | |
| 3965 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3966 | |
| 3967 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3968 | return 0; |
| 3969 | } |
| 3970 | |
| 3971 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 3972 | { |
| 3973 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3974 | struct btrfs_block_group_cache *cache = NULL; |
| 3975 | u64 len; |
| 3976 | |
| 3977 | while (start <= end) { |
| 3978 | if (!cache || |
| 3979 | start >= cache->key.objectid + cache->key.offset) { |
| 3980 | if (cache) |
| 3981 | btrfs_put_block_group(cache); |
| 3982 | cache = btrfs_lookup_block_group(fs_info, start); |
| 3983 | BUG_ON(!cache); |
| 3984 | } |
| 3985 | |
| 3986 | len = cache->key.objectid + cache->key.offset - start; |
| 3987 | len = min(len, end + 1 - start); |
| 3988 | |
| 3989 | if (start < cache->last_byte_to_unpin) { |
| 3990 | len = min(len, cache->last_byte_to_unpin - start); |
| 3991 | btrfs_add_free_space(cache, start, len); |
| 3992 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3993 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3994 | start += len; |
| 3995 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3996 | spin_lock(&cache->space_info->lock); |
| 3997 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 3998 | cache->pinned -= len; |
| 3999 | cache->space_info->bytes_pinned -= len; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4000 | if (cache->ro) { |
| 4001 | cache->space_info->bytes_readonly += len; |
| 4002 | } else if (cache->reserved_pinned > 0) { |
| 4003 | len = min(len, cache->reserved_pinned); |
| 4004 | cache->reserved_pinned -= len; |
| 4005 | cache->space_info->bytes_reserved += len; |
| 4006 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4007 | spin_unlock(&cache->lock); |
| 4008 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4012 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4013 | return 0; |
| 4014 | } |
| 4015 | |
| 4016 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4017 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4018 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4019 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4020 | struct extent_io_tree *unpin; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4021 | struct btrfs_block_rsv *block_rsv; |
| 4022 | struct btrfs_block_rsv *next_rsv; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4023 | u64 start; |
| 4024 | u64 end; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4025 | int idx; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4026 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4027 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4028 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4029 | unpin = &fs_info->freed_extents[1]; |
| 4030 | else |
| 4031 | unpin = &fs_info->freed_extents[0]; |
| 4032 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4033 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4034 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4035 | EXTENT_DIRTY); |
| 4036 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4037 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4038 | |
| 4039 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 4040 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4041 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4042 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4043 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4044 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4045 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4046 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 4047 | list_for_each_entry_safe(block_rsv, next_rsv, |
| 4048 | &fs_info->durable_block_rsv_list, list) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4049 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4050 | idx = trans->transid & 0x1; |
| 4051 | if (block_rsv->freed[idx] > 0) { |
| 4052 | block_rsv_add_bytes(block_rsv, |
| 4053 | block_rsv->freed[idx], 0); |
| 4054 | block_rsv->freed[idx] = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 4055 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4056 | if (atomic_read(&block_rsv->usage) == 0) { |
| 4057 | btrfs_block_rsv_release(root, block_rsv, (u64)-1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4058 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4059 | if (block_rsv->freed[0] == 0 && |
| 4060 | block_rsv->freed[1] == 0) { |
| 4061 | list_del_init(&block_rsv->list); |
| 4062 | kfree(block_rsv); |
| 4063 | } |
| 4064 | } else { |
| 4065 | btrfs_block_rsv_release(root, block_rsv, 0); |
| 4066 | } |
| 4067 | } |
| 4068 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 4069 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4070 | return 0; |
| 4071 | } |
| 4072 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4073 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4074 | struct btrfs_root *root, |
| 4075 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4076 | u64 root_objectid, u64 owner_objectid, |
| 4077 | u64 owner_offset, int refs_to_drop, |
| 4078 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4079 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4080 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4081 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4082 | struct btrfs_fs_info *info = root->fs_info; |
| 4083 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4084 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4085 | struct btrfs_extent_item *ei; |
| 4086 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4087 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4088 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4089 | int extent_slot = 0; |
| 4090 | int found_extent = 0; |
| 4091 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4092 | u32 item_size; |
| 4093 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4094 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4095 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4096 | if (!path) |
| 4097 | return -ENOMEM; |
| 4098 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4099 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4100 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4101 | |
| 4102 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4103 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4104 | |
| 4105 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4106 | bytenr, num_bytes, parent, |
| 4107 | root_objectid, owner_objectid, |
| 4108 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4109 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4110 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4111 | while (extent_slot >= 0) { |
| 4112 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4113 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4114 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4115 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4116 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4117 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4118 | found_extent = 1; |
| 4119 | break; |
| 4120 | } |
| 4121 | if (path->slots[0] - extent_slot > 5) |
| 4122 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4123 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4124 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4125 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4126 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4127 | if (found_extent && item_size < sizeof(*ei)) |
| 4128 | found_extent = 0; |
| 4129 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4130 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4131 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4132 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4133 | NULL, refs_to_drop, |
| 4134 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4135 | BUG_ON(ret); |
| 4136 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4137 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4138 | |
| 4139 | key.objectid = bytenr; |
| 4140 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4141 | key.offset = num_bytes; |
| 4142 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4143 | ret = btrfs_search_slot(trans, extent_root, |
| 4144 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4145 | if (ret) { |
| 4146 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4147 | ", was looking for %llu\n", ret, |
| 4148 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4149 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4150 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4151 | BUG_ON(ret); |
| 4152 | extent_slot = path->slots[0]; |
| 4153 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4154 | } else { |
| 4155 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4156 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4157 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4158 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4159 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4160 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4161 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4162 | (unsigned long long)owner_objectid, |
| 4163 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4164 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4165 | |
| 4166 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4167 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4168 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4169 | if (item_size < sizeof(*ei)) { |
| 4170 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4171 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4172 | owner_objectid, 0); |
| 4173 | BUG_ON(ret < 0); |
| 4174 | |
| 4175 | btrfs_release_path(extent_root, path); |
| 4176 | path->leave_spinning = 1; |
| 4177 | |
| 4178 | key.objectid = bytenr; |
| 4179 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4180 | key.offset = num_bytes; |
| 4181 | |
| 4182 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4183 | -1, 1); |
| 4184 | if (ret) { |
| 4185 | printk(KERN_ERR "umm, got %d back from search" |
| 4186 | ", was looking for %llu\n", ret, |
| 4187 | (unsigned long long)bytenr); |
| 4188 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4189 | } |
| 4190 | BUG_ON(ret); |
| 4191 | extent_slot = path->slots[0]; |
| 4192 | leaf = path->nodes[0]; |
| 4193 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4194 | } |
| 4195 | #endif |
| 4196 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4197 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4198 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4199 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4200 | struct btrfs_tree_block_info *bi; |
| 4201 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4202 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4203 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4204 | } |
| 4205 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4206 | refs = btrfs_extent_refs(leaf, ei); |
| 4207 | BUG_ON(refs < refs_to_drop); |
| 4208 | refs -= refs_to_drop; |
| 4209 | |
| 4210 | if (refs > 0) { |
| 4211 | if (extent_op) |
| 4212 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4213 | /* |
| 4214 | * In the case of inline back ref, reference count will |
| 4215 | * be updated by remove_extent_backref |
| 4216 | */ |
| 4217 | if (iref) { |
| 4218 | BUG_ON(!found_extent); |
| 4219 | } else { |
| 4220 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4221 | btrfs_mark_buffer_dirty(leaf); |
| 4222 | } |
| 4223 | if (found_extent) { |
| 4224 | ret = remove_extent_backref(trans, extent_root, path, |
| 4225 | iref, refs_to_drop, |
| 4226 | is_data); |
| 4227 | BUG_ON(ret); |
| 4228 | } |
| 4229 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4230 | if (found_extent) { |
| 4231 | BUG_ON(is_data && refs_to_drop != |
| 4232 | extent_data_ref_count(root, path, iref)); |
| 4233 | if (iref) { |
| 4234 | BUG_ON(path->slots[0] != extent_slot); |
| 4235 | } else { |
| 4236 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4237 | path->slots[0] = extent_slot; |
| 4238 | num_to_del = 2; |
| 4239 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4240 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4241 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4242 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4243 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4244 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4245 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4246 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4247 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4248 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4249 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4250 | } else { |
| 4251 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4252 | bytenr >> PAGE_CACHE_SHIFT, |
| 4253 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4254 | } |
| 4255 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4256 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4257 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4258 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4259 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4260 | return ret; |
| 4261 | } |
| 4262 | |
| 4263 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4264 | * 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] | 4265 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4266 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4267 | * removes it from the tree. |
| 4268 | */ |
| 4269 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4270 | struct btrfs_root *root, u64 bytenr) |
| 4271 | { |
| 4272 | struct btrfs_delayed_ref_head *head; |
| 4273 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4274 | struct btrfs_delayed_ref_node *ref; |
| 4275 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4276 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4277 | |
| 4278 | delayed_refs = &trans->transaction->delayed_refs; |
| 4279 | spin_lock(&delayed_refs->lock); |
| 4280 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4281 | if (!head) |
| 4282 | goto out; |
| 4283 | |
| 4284 | node = rb_prev(&head->node.rb_node); |
| 4285 | if (!node) |
| 4286 | goto out; |
| 4287 | |
| 4288 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4289 | |
| 4290 | /* there are still entries for this ref, we can't drop it */ |
| 4291 | if (ref->bytenr == bytenr) |
| 4292 | goto out; |
| 4293 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4294 | if (head->extent_op) { |
| 4295 | if (!head->must_insert_reserved) |
| 4296 | goto out; |
| 4297 | kfree(head->extent_op); |
| 4298 | head->extent_op = NULL; |
| 4299 | } |
| 4300 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4301 | /* |
| 4302 | * waiting for the lock here would deadlock. If someone else has it |
| 4303 | * locked they are already in the process of dropping it anyway |
| 4304 | */ |
| 4305 | if (!mutex_trylock(&head->mutex)) |
| 4306 | goto out; |
| 4307 | |
| 4308 | /* |
| 4309 | * at this point we have a head with no other entries. Go |
| 4310 | * ahead and process it. |
| 4311 | */ |
| 4312 | head->node.in_tree = 0; |
| 4313 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4314 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4315 | delayed_refs->num_entries--; |
| 4316 | |
| 4317 | /* |
| 4318 | * we don't take a ref on the node because we're removing it from the |
| 4319 | * tree, so we just steal the ref the tree was holding. |
| 4320 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4321 | delayed_refs->num_heads--; |
| 4322 | if (list_empty(&head->cluster)) |
| 4323 | delayed_refs->num_heads_ready--; |
| 4324 | |
| 4325 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4326 | spin_unlock(&delayed_refs->lock); |
| 4327 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4328 | BUG_ON(head->extent_op); |
| 4329 | if (head->must_insert_reserved) |
| 4330 | ret = 1; |
| 4331 | |
| 4332 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4333 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4334 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4335 | out: |
| 4336 | spin_unlock(&delayed_refs->lock); |
| 4337 | return 0; |
| 4338 | } |
| 4339 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4340 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4341 | struct btrfs_root *root, |
| 4342 | struct extent_buffer *buf, |
| 4343 | u64 parent, int last_ref) |
| 4344 | { |
| 4345 | struct btrfs_block_rsv *block_rsv; |
| 4346 | struct btrfs_block_group_cache *cache = NULL; |
| 4347 | int ret; |
| 4348 | |
| 4349 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4350 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4351 | parent, root->root_key.objectid, |
| 4352 | btrfs_header_level(buf), |
| 4353 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4354 | BUG_ON(ret); |
| 4355 | } |
| 4356 | |
| 4357 | if (!last_ref) |
| 4358 | return; |
| 4359 | |
| 4360 | block_rsv = get_block_rsv(trans, root); |
| 4361 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | 3bf84a5 | 2010-05-31 09:04:46 +0000 | [diff] [blame] | 4362 | if (block_rsv->space_info != cache->space_info) |
| 4363 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4364 | |
| 4365 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4366 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4367 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4368 | if (!ret) |
| 4369 | goto pin; |
| 4370 | } |
| 4371 | |
| 4372 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4373 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
| 4374 | goto pin; |
| 4375 | } |
| 4376 | |
| 4377 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4378 | |
| 4379 | btrfs_add_free_space(cache, buf->start, buf->len); |
| 4380 | ret = update_reserved_bytes(cache, buf->len, 0, 0); |
| 4381 | if (ret == -EAGAIN) { |
| 4382 | /* block group became read-only */ |
| 4383 | update_reserved_bytes(cache, buf->len, 0, 1); |
| 4384 | goto out; |
| 4385 | } |
| 4386 | |
| 4387 | ret = 1; |
| 4388 | spin_lock(&block_rsv->lock); |
| 4389 | if (block_rsv->reserved < block_rsv->size) { |
| 4390 | block_rsv->reserved += buf->len; |
| 4391 | ret = 0; |
| 4392 | } |
| 4393 | spin_unlock(&block_rsv->lock); |
| 4394 | |
| 4395 | if (ret) { |
| 4396 | spin_lock(&cache->space_info->lock); |
| 4397 | cache->space_info->bytes_reserved -= buf->len; |
| 4398 | spin_unlock(&cache->space_info->lock); |
| 4399 | } |
| 4400 | goto out; |
| 4401 | } |
| 4402 | pin: |
| 4403 | if (block_rsv->durable && !cache->ro) { |
| 4404 | ret = 0; |
| 4405 | spin_lock(&cache->lock); |
| 4406 | if (!cache->ro) { |
| 4407 | cache->reserved_pinned += buf->len; |
| 4408 | ret = 1; |
| 4409 | } |
| 4410 | spin_unlock(&cache->lock); |
| 4411 | |
| 4412 | if (ret) { |
| 4413 | spin_lock(&block_rsv->lock); |
| 4414 | block_rsv->freed[trans->transid & 0x1] += buf->len; |
| 4415 | spin_unlock(&block_rsv->lock); |
| 4416 | } |
| 4417 | } |
| 4418 | out: |
| 4419 | btrfs_put_block_group(cache); |
| 4420 | } |
| 4421 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4422 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4423 | struct btrfs_root *root, |
| 4424 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4425 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4426 | { |
| 4427 | int ret; |
| 4428 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4429 | /* |
| 4430 | * tree log blocks never actually go into the extent allocation |
| 4431 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4432 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4433 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4434 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4435 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4436 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4437 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4438 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4439 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4440 | parent, root_objectid, (int)owner, |
| 4441 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4442 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4443 | } else { |
| 4444 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4445 | parent, root_objectid, owner, |
| 4446 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4447 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4448 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4449 | return ret; |
| 4450 | } |
| 4451 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4452 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4453 | { |
| 4454 | u64 mask = ((u64)root->stripesize - 1); |
| 4455 | u64 ret = (val + mask) & ~mask; |
| 4456 | return ret; |
| 4457 | } |
| 4458 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4459 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4460 | * when we wait for progress in the block group caching, its because |
| 4461 | * our allocation attempt failed at least once. So, we must sleep |
| 4462 | * and let some progress happen before we try again. |
| 4463 | * |
| 4464 | * This function will sleep at least once waiting for new free space to |
| 4465 | * show up, and then it will check the block group free space numbers |
| 4466 | * for our min num_bytes. Another option is to have it go ahead |
| 4467 | * and look in the rbtree for a free extent of a given size, but this |
| 4468 | * is a good start. |
| 4469 | */ |
| 4470 | static noinline int |
| 4471 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4472 | u64 num_bytes) |
| 4473 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4474 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4475 | DEFINE_WAIT(wait); |
| 4476 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4477 | caching_ctl = get_caching_control(cache); |
| 4478 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4479 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4480 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4481 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4482 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4483 | |
| 4484 | put_caching_control(caching_ctl); |
| 4485 | return 0; |
| 4486 | } |
| 4487 | |
| 4488 | static noinline int |
| 4489 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4490 | { |
| 4491 | struct btrfs_caching_control *caching_ctl; |
| 4492 | DEFINE_WAIT(wait); |
| 4493 | |
| 4494 | caching_ctl = get_caching_control(cache); |
| 4495 | if (!caching_ctl) |
| 4496 | return 0; |
| 4497 | |
| 4498 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4499 | |
| 4500 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4501 | return 0; |
| 4502 | } |
| 4503 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4504 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4505 | { |
| 4506 | int index; |
| 4507 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4508 | index = 0; |
| 4509 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4510 | index = 1; |
| 4511 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4512 | index = 2; |
| 4513 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4514 | index = 3; |
| 4515 | else |
| 4516 | index = 4; |
| 4517 | return index; |
| 4518 | } |
| 4519 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4520 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4521 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4522 | LOOP_CACHING_NOWAIT = 1, |
| 4523 | LOOP_CACHING_WAIT = 2, |
| 4524 | LOOP_ALLOC_CHUNK = 3, |
| 4525 | LOOP_NO_EMPTY_SIZE = 4, |
| 4526 | }; |
| 4527 | |
| 4528 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4529 | * walks the btree of allocated extents and find a hole of a given size. |
| 4530 | * The key ins is changed to record the hole: |
| 4531 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4532 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4533 | * ins->offset == number of blocks |
| 4534 | * Any available blocks before search_start are skipped. |
| 4535 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4536 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4537 | struct btrfs_root *orig_root, |
| 4538 | u64 num_bytes, u64 empty_size, |
| 4539 | u64 search_start, u64 search_end, |
| 4540 | u64 hint_byte, struct btrfs_key *ins, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4541 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4542 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4543 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4544 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4545 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4546 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4547 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4548 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4549 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4550 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4551 | int last_ptr_loop = 0; |
| 4552 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4553 | int index = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4554 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4555 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4556 | bool failed_alloc = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4557 | u64 ideal_cache_percent = 0; |
| 4558 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4559 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4560 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4561 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4562 | ins->objectid = 0; |
| 4563 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4564 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4565 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4566 | if (!space_info) { |
| 4567 | printk(KERN_ERR "No space info for %d\n", data); |
| 4568 | return -ENOSPC; |
| 4569 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4570 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4571 | if (orig_root->ref_cows || empty_size) |
| 4572 | allowed_chunk_alloc = 1; |
| 4573 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4574 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4575 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4576 | if (!btrfs_test_opt(root, SSD)) |
| 4577 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4578 | } |
| 4579 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4580 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 4581 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4582 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4583 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4584 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4585 | spin_lock(&last_ptr->lock); |
| 4586 | if (last_ptr->block_group) |
| 4587 | hint_byte = last_ptr->window_start; |
| 4588 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4589 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4590 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4591 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4592 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4593 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4594 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4595 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4596 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4597 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4598 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4599 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4600 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4601 | /* |
| 4602 | * we don't want to use the block group if it doesn't match our |
| 4603 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4604 | * |
| 4605 | * However if we are re-searching with an ideal block group |
| 4606 | * 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] | 4607 | */ |
| 4608 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4609 | (block_group->cached != BTRFS_CACHE_NO || |
| 4610 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4611 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4612 | if (list_empty(&block_group->list) || |
| 4613 | block_group->ro) { |
| 4614 | /* |
| 4615 | * someone is removing this block group, |
| 4616 | * we can't jump into the have_block_group |
| 4617 | * target because our list pointers are not |
| 4618 | * valid |
| 4619 | */ |
| 4620 | btrfs_put_block_group(block_group); |
| 4621 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4622 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4623 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4624 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4625 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4626 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4627 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4628 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4629 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4630 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4631 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4632 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 4633 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4634 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4635 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4636 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4637 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4638 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4639 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4640 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4641 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4642 | u64 free_percent; |
| 4643 | |
| 4644 | free_percent = btrfs_block_group_used(&block_group->item); |
| 4645 | free_percent *= 100; |
| 4646 | free_percent = div64_u64(free_percent, |
| 4647 | block_group->key.offset); |
| 4648 | free_percent = 100 - free_percent; |
| 4649 | if (free_percent > ideal_cache_percent && |
| 4650 | likely(!block_group->ro)) { |
| 4651 | ideal_cache_offset = block_group->key.objectid; |
| 4652 | ideal_cache_percent = free_percent; |
| 4653 | } |
| 4654 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4655 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4656 | * We only want to start kthread caching if we are at |
| 4657 | * the point where we will wait for caching to make |
| 4658 | * progress, or if our ideal search is over and we've |
| 4659 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4660 | */ |
| 4661 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4662 | (loop > LOOP_FIND_IDEAL && |
| 4663 | atomic_read(&space_info->caching_threads) < 2)) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4664 | ret = cache_block_group(block_group); |
| 4665 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4666 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4667 | found_uncached_bg = true; |
| 4668 | |
| 4669 | /* |
| 4670 | * If loop is set for cached only, try the next block |
| 4671 | * group. |
| 4672 | */ |
| 4673 | if (loop == LOOP_FIND_IDEAL) |
| 4674 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4675 | } |
| 4676 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4677 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4678 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4679 | found_uncached_bg = true; |
| 4680 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4681 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4682 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4683 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4684 | /* |
| 4685 | * Ok we want to try and use the cluster allocator, so lets look |
| 4686 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 4687 | * have tried the cluster allocator plenty of times at this |
| 4688 | * point and not have found anything, so we are likely way too |
| 4689 | * fragmented for the clustering stuff to find anything, so lets |
| 4690 | * just skip it and let the allocator find whatever block it can |
| 4691 | * find |
| 4692 | */ |
| 4693 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4694 | /* |
| 4695 | * the refill lock keeps out other |
| 4696 | * people trying to start a new cluster |
| 4697 | */ |
| 4698 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4699 | if (last_ptr->block_group && |
| 4700 | (last_ptr->block_group->ro || |
| 4701 | !block_group_bits(last_ptr->block_group, data))) { |
| 4702 | offset = 0; |
| 4703 | goto refill_cluster; |
| 4704 | } |
| 4705 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4706 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 4707 | num_bytes, search_start); |
| 4708 | if (offset) { |
| 4709 | /* we have a block, we're done */ |
| 4710 | spin_unlock(&last_ptr->refill_lock); |
| 4711 | goto checks; |
| 4712 | } |
| 4713 | |
| 4714 | spin_lock(&last_ptr->lock); |
| 4715 | /* |
| 4716 | * whoops, this cluster doesn't actually point to |
| 4717 | * this block group. Get a ref on the block |
| 4718 | * group is does point to and try again |
| 4719 | */ |
| 4720 | if (!last_ptr_loop && last_ptr->block_group && |
| 4721 | last_ptr->block_group != block_group) { |
| 4722 | |
| 4723 | btrfs_put_block_group(block_group); |
| 4724 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4725 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4726 | spin_unlock(&last_ptr->lock); |
| 4727 | spin_unlock(&last_ptr->refill_lock); |
| 4728 | |
| 4729 | last_ptr_loop = 1; |
| 4730 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4731 | /* |
| 4732 | * we know this block group is properly |
| 4733 | * in the list because |
| 4734 | * btrfs_remove_block_group, drops the |
| 4735 | * cluster before it removes the block |
| 4736 | * group from the list |
| 4737 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4738 | goto have_block_group; |
| 4739 | } |
| 4740 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4741 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4742 | /* |
| 4743 | * this cluster didn't work out, free it and |
| 4744 | * start over |
| 4745 | */ |
| 4746 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 4747 | |
| 4748 | last_ptr_loop = 0; |
| 4749 | |
| 4750 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 4751 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4752 | block_group, last_ptr, |
| 4753 | offset, num_bytes, |
| 4754 | empty_cluster + empty_size); |
| 4755 | if (ret == 0) { |
| 4756 | /* |
| 4757 | * now pull our allocation out of this |
| 4758 | * cluster |
| 4759 | */ |
| 4760 | offset = btrfs_alloc_from_cluster(block_group, |
| 4761 | last_ptr, num_bytes, |
| 4762 | search_start); |
| 4763 | if (offset) { |
| 4764 | /* we found one, proceed */ |
| 4765 | spin_unlock(&last_ptr->refill_lock); |
| 4766 | goto checks; |
| 4767 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4768 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 4769 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4770 | spin_unlock(&last_ptr->refill_lock); |
| 4771 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4772 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4773 | wait_block_group_cache_progress(block_group, |
| 4774 | num_bytes + empty_cluster + empty_size); |
| 4775 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4776 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4777 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4778 | /* |
| 4779 | * at this point we either didn't find a cluster |
| 4780 | * or we weren't able to allocate a block from our |
| 4781 | * cluster. Free the cluster we've been trying |
| 4782 | * to use, and go to the next block group |
| 4783 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4784 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4785 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4786 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4787 | } |
| 4788 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4789 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 4790 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4791 | /* |
| 4792 | * If we didn't find a chunk, and we haven't failed on this |
| 4793 | * block group before, and this block group is in the middle of |
| 4794 | * caching and we are ok with waiting, then go ahead and wait |
| 4795 | * for progress to be made, and set failed_alloc to true. |
| 4796 | * |
| 4797 | * If failed_alloc is true then we've already waited on this |
| 4798 | * block group once and should move on to the next block group. |
| 4799 | */ |
| 4800 | if (!offset && !failed_alloc && !cached && |
| 4801 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4802 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4803 | num_bytes + empty_size); |
| 4804 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4805 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4806 | } else if (!offset) { |
| 4807 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4808 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4809 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4810 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4811 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4812 | if (search_start + num_bytes >= search_end) { |
| 4813 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4814 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4815 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 4816 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4817 | /* move on to the next group */ |
| 4818 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4819 | block_group->key.objectid + block_group->key.offset) { |
| 4820 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4821 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4822 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4823 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4824 | ins->objectid = search_start; |
| 4825 | ins->offset = num_bytes; |
| 4826 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4827 | if (offset < search_start) |
| 4828 | btrfs_add_free_space(block_group, offset, |
| 4829 | search_start - offset); |
| 4830 | BUG_ON(offset > search_start); |
| 4831 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4832 | ret = update_reserved_bytes(block_group, num_bytes, 1, |
| 4833 | (data & BTRFS_BLOCK_GROUP_DATA)); |
| 4834 | if (ret == -EAGAIN) { |
| 4835 | btrfs_add_free_space(block_group, offset, num_bytes); |
| 4836 | goto loop; |
| 4837 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4838 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4839 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4840 | ins->objectid = search_start; |
| 4841 | ins->offset = num_bytes; |
| 4842 | |
| 4843 | if (offset < search_start) |
| 4844 | btrfs_add_free_space(block_group, offset, |
| 4845 | search_start - offset); |
| 4846 | BUG_ON(offset > search_start); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4847 | break; |
| 4848 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4849 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4850 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4851 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4852 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4853 | } |
| 4854 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 4855 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4856 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 4857 | goto search; |
| 4858 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4859 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 4860 | * for them to make caching progress. Also |
| 4861 | * determine the best possible bg to cache |
| 4862 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 4863 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4864 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 4865 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 4866 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 4867 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4868 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4869 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 4870 | (found_uncached_bg || empty_size || empty_cluster || |
| 4871 | allowed_chunk_alloc)) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4872 | index = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4873 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4874 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4875 | loop++; |
| 4876 | if (!ideal_cache_percent && |
| 4877 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4878 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4879 | |
| 4880 | /* |
| 4881 | * 1 of the following 2 things have happened so far |
| 4882 | * |
| 4883 | * 1) We found an ideal block group for caching that |
| 4884 | * is mostly full and will cache quickly, so we might |
| 4885 | * as well wait for it. |
| 4886 | * |
| 4887 | * 2) We searched for cached only and we didn't find |
| 4888 | * anything, and we didn't start any caching kthreads |
| 4889 | * either, so chances are we will loop through and |
| 4890 | * start a couple caching kthreads, and then come back |
| 4891 | * around and just wait for them. This will be slower |
| 4892 | * because we will have 2 caching kthreads reading at |
| 4893 | * the same time when we could have just started one |
| 4894 | * and waited for it to get far enough to give us an |
| 4895 | * allocation, so go ahead and go to the wait caching |
| 4896 | * loop. |
| 4897 | */ |
| 4898 | loop = LOOP_CACHING_WAIT; |
| 4899 | search_start = ideal_cache_offset; |
| 4900 | ideal_cache_percent = 0; |
| 4901 | goto ideal_cache; |
| 4902 | } else if (loop == LOOP_FIND_IDEAL) { |
| 4903 | /* |
| 4904 | * Didn't find a uncached bg, wait on anything we find |
| 4905 | * next. |
| 4906 | */ |
| 4907 | loop = LOOP_CACHING_WAIT; |
| 4908 | goto search; |
| 4909 | } |
| 4910 | |
| 4911 | if (loop < LOOP_CACHING_WAIT) { |
| 4912 | loop++; |
| 4913 | goto search; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4917 | empty_size = 0; |
| 4918 | empty_cluster = 0; |
| 4919 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4920 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4921 | if (allowed_chunk_alloc) { |
| 4922 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 4923 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4924 | allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4925 | done_chunk_alloc = 1; |
| 4926 | } else if (!done_chunk_alloc) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4927 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4928 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4929 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4930 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4931 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4932 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4933 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4934 | ret = -ENOSPC; |
| 4935 | } else if (!ins->objectid) { |
| 4936 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 4937 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4938 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4939 | /* we found what we needed */ |
| 4940 | if (ins->objectid) { |
| 4941 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4942 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4943 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4944 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4945 | ret = 0; |
| 4946 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4947 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 4948 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4949 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4950 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4951 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 4952 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4953 | { |
| 4954 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4955 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4956 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4957 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4958 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 4959 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4960 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4961 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4962 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4963 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 4964 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 4965 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4966 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4967 | (unsigned long long)info->bytes_pinned, |
| 4968 | (unsigned long long)info->bytes_reserved, |
| 4969 | (unsigned long long)info->bytes_may_use, |
| 4970 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4971 | spin_unlock(&info->lock); |
| 4972 | |
| 4973 | if (!dump_block_groups) |
| 4974 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4975 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4976 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4977 | again: |
| 4978 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4979 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4980 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 4981 | "%llu pinned %llu reserved\n", |
| 4982 | (unsigned long long)cache->key.objectid, |
| 4983 | (unsigned long long)cache->key.offset, |
| 4984 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 4985 | (unsigned long long)cache->pinned, |
| 4986 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4987 | btrfs_dump_free_space(cache, bytes); |
| 4988 | spin_unlock(&cache->lock); |
| 4989 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4990 | if (++index < BTRFS_NR_RAID_TYPES) |
| 4991 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4992 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4993 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4994 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4995 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 4996 | struct btrfs_root *root, |
| 4997 | u64 num_bytes, u64 min_alloc_size, |
| 4998 | u64 empty_size, u64 hint_byte, |
| 4999 | u64 search_end, struct btrfs_key *ins, |
| 5000 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5001 | { |
| 5002 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5003 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5004 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5005 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5006 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5007 | /* |
| 5008 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5009 | * is not called recursively on allocations |
| 5010 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5011 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5012 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5013 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5014 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5015 | WARN_ON(num_bytes < root->sectorsize); |
| 5016 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5017 | search_start, search_end, hint_byte, |
| 5018 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5019 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5020 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5021 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5022 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5023 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5024 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5025 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5026 | goto again; |
| 5027 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5028 | if (ret == -ENOSPC) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5029 | struct btrfs_space_info *sinfo; |
| 5030 | |
| 5031 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5032 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5033 | "wanted %llu\n", (unsigned long long)data, |
| 5034 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5035 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5036 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5037 | |
| 5038 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5039 | } |
| 5040 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5041 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 5042 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5043 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5044 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5045 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5046 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5047 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5048 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5049 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5050 | return -ENOSPC; |
| 5051 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5052 | |
| 5053 | ret = btrfs_discard_extent(root, start, len); |
| 5054 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5055 | btrfs_add_free_space(cache, start, len); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5056 | update_reserved_bytes(cache, len, 0, 1); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5057 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5058 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5059 | return ret; |
| 5060 | } |
| 5061 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5062 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5063 | struct btrfs_root *root, |
| 5064 | u64 parent, u64 root_objectid, |
| 5065 | u64 flags, u64 owner, u64 offset, |
| 5066 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5067 | { |
| 5068 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5069 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5070 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5071 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5072 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5073 | struct extent_buffer *leaf; |
| 5074 | int type; |
| 5075 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5076 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5077 | if (parent > 0) |
| 5078 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5079 | else |
| 5080 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5081 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5082 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5083 | |
| 5084 | path = btrfs_alloc_path(); |
| 5085 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5086 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5087 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5088 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5089 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5090 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5091 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5092 | leaf = path->nodes[0]; |
| 5093 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5094 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5095 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5096 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5097 | btrfs_set_extent_flags(leaf, extent_item, |
| 5098 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5099 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5100 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5101 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5102 | if (parent > 0) { |
| 5103 | struct btrfs_shared_data_ref *ref; |
| 5104 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5105 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5106 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5107 | } else { |
| 5108 | struct btrfs_extent_data_ref *ref; |
| 5109 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5110 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5111 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5112 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5113 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5114 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5115 | |
| 5116 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5117 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5118 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5119 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5120 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5121 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5122 | "%llu\n", (unsigned long long)ins->objectid, |
| 5123 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5124 | BUG(); |
| 5125 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5126 | return ret; |
| 5127 | } |
| 5128 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5129 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5130 | struct btrfs_root *root, |
| 5131 | u64 parent, u64 root_objectid, |
| 5132 | u64 flags, struct btrfs_disk_key *key, |
| 5133 | int level, struct btrfs_key *ins) |
| 5134 | { |
| 5135 | int ret; |
| 5136 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5137 | struct btrfs_extent_item *extent_item; |
| 5138 | struct btrfs_tree_block_info *block_info; |
| 5139 | struct btrfs_extent_inline_ref *iref; |
| 5140 | struct btrfs_path *path; |
| 5141 | struct extent_buffer *leaf; |
| 5142 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5143 | |
| 5144 | path = btrfs_alloc_path(); |
| 5145 | BUG_ON(!path); |
| 5146 | |
| 5147 | path->leave_spinning = 1; |
| 5148 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5149 | ins, size); |
| 5150 | BUG_ON(ret); |
| 5151 | |
| 5152 | leaf = path->nodes[0]; |
| 5153 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5154 | struct btrfs_extent_item); |
| 5155 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5156 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5157 | btrfs_set_extent_flags(leaf, extent_item, |
| 5158 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5159 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5160 | |
| 5161 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5162 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5163 | |
| 5164 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5165 | if (parent > 0) { |
| 5166 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5167 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5168 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5169 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5170 | } else { |
| 5171 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5172 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5173 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5174 | } |
| 5175 | |
| 5176 | btrfs_mark_buffer_dirty(leaf); |
| 5177 | btrfs_free_path(path); |
| 5178 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5179 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5180 | if (ret) { |
| 5181 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5182 | "%llu\n", (unsigned long long)ins->objectid, |
| 5183 | (unsigned long long)ins->offset); |
| 5184 | BUG(); |
| 5185 | } |
| 5186 | return ret; |
| 5187 | } |
| 5188 | |
| 5189 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5190 | struct btrfs_root *root, |
| 5191 | u64 root_objectid, u64 owner, |
| 5192 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5193 | { |
| 5194 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5195 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5196 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5197 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5198 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5199 | 0, root_objectid, owner, offset, |
| 5200 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5201 | return ret; |
| 5202 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5203 | |
| 5204 | /* |
| 5205 | * this is used by the tree logging recovery code. It records that |
| 5206 | * an extent has been allocated and makes sure to clear the free |
| 5207 | * space cache bits as well |
| 5208 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5209 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5210 | struct btrfs_root *root, |
| 5211 | u64 root_objectid, u64 owner, u64 offset, |
| 5212 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5213 | { |
| 5214 | int ret; |
| 5215 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5216 | struct btrfs_caching_control *caching_ctl; |
| 5217 | u64 start = ins->objectid; |
| 5218 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5219 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5220 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5221 | cache_block_group(block_group); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5222 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5223 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5224 | if (!caching_ctl) { |
| 5225 | BUG_ON(!block_group_cache_done(block_group)); |
| 5226 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5227 | BUG_ON(ret); |
| 5228 | } else { |
| 5229 | mutex_lock(&caching_ctl->mutex); |
| 5230 | |
| 5231 | if (start >= caching_ctl->progress) { |
| 5232 | ret = add_excluded_extent(root, start, num_bytes); |
| 5233 | BUG_ON(ret); |
| 5234 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5235 | ret = btrfs_remove_free_space(block_group, |
| 5236 | start, num_bytes); |
| 5237 | BUG_ON(ret); |
| 5238 | } else { |
| 5239 | num_bytes = caching_ctl->progress - start; |
| 5240 | ret = btrfs_remove_free_space(block_group, |
| 5241 | start, num_bytes); |
| 5242 | BUG_ON(ret); |
| 5243 | |
| 5244 | start = caching_ctl->progress; |
| 5245 | num_bytes = ins->objectid + ins->offset - |
| 5246 | caching_ctl->progress; |
| 5247 | ret = add_excluded_extent(root, start, num_bytes); |
| 5248 | BUG_ON(ret); |
| 5249 | } |
| 5250 | |
| 5251 | mutex_unlock(&caching_ctl->mutex); |
| 5252 | put_caching_control(caching_ctl); |
| 5253 | } |
| 5254 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5255 | ret = update_reserved_bytes(block_group, ins->offset, 1, 1); |
| 5256 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5257 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5258 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5259 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5260 | return ret; |
| 5261 | } |
| 5262 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5263 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5264 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5265 | u64 bytenr, u32 blocksize, |
| 5266 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5267 | { |
| 5268 | struct extent_buffer *buf; |
| 5269 | |
| 5270 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5271 | if (!buf) |
| 5272 | return ERR_PTR(-ENOMEM); |
| 5273 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5274 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5275 | btrfs_tree_lock(buf); |
| 5276 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5277 | |
| 5278 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5279 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5280 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5281 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5282 | /* |
| 5283 | * we allow two log transactions at a time, use different |
| 5284 | * EXENT bit to differentiate dirty pages. |
| 5285 | */ |
| 5286 | if (root->log_transid % 2 == 0) |
| 5287 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5288 | buf->start + buf->len - 1, GFP_NOFS); |
| 5289 | else |
| 5290 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5291 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5292 | } else { |
| 5293 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5294 | buf->start + buf->len - 1, GFP_NOFS); |
| 5295 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5296 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5297 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5298 | return buf; |
| 5299 | } |
| 5300 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5301 | static struct btrfs_block_rsv * |
| 5302 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5303 | struct btrfs_root *root, u32 blocksize) |
| 5304 | { |
| 5305 | struct btrfs_block_rsv *block_rsv; |
| 5306 | int ret; |
| 5307 | |
| 5308 | block_rsv = get_block_rsv(trans, root); |
| 5309 | |
| 5310 | if (block_rsv->size == 0) { |
| 5311 | ret = reserve_metadata_bytes(block_rsv, blocksize); |
| 5312 | if (ret) |
| 5313 | return ERR_PTR(ret); |
| 5314 | return block_rsv; |
| 5315 | } |
| 5316 | |
| 5317 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5318 | if (!ret) |
| 5319 | return block_rsv; |
| 5320 | |
| 5321 | WARN_ON(1); |
| 5322 | printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n", |
| 5323 | block_rsv->size, block_rsv->reserved, |
| 5324 | block_rsv->freed[0], block_rsv->freed[1]); |
| 5325 | |
| 5326 | return ERR_PTR(-ENOSPC); |
| 5327 | } |
| 5328 | |
| 5329 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5330 | { |
| 5331 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5332 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5333 | } |
| 5334 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5335 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5336 | * finds a free extent and does all the dirty work required for allocation |
| 5337 | * returns the key for the extent through ins, and a tree buffer for |
| 5338 | * the first block of the extent through buf. |
| 5339 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5340 | * returns the tree buffer or NULL. |
| 5341 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5342 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5343 | struct btrfs_root *root, u32 blocksize, |
| 5344 | u64 parent, u64 root_objectid, |
| 5345 | struct btrfs_disk_key *key, int level, |
| 5346 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5347 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5348 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5349 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5350 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5351 | u64 flags = 0; |
| 5352 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5353 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5354 | |
| 5355 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5356 | if (IS_ERR(block_rsv)) |
| 5357 | return ERR_CAST(block_rsv); |
| 5358 | |
| 5359 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5360 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5361 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5362 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5363 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5364 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5365 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5366 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5367 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5368 | BUG_ON(IS_ERR(buf)); |
| 5369 | |
| 5370 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5371 | if (parent == 0) |
| 5372 | parent = ins.objectid; |
| 5373 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5374 | } else |
| 5375 | BUG_ON(parent > 0); |
| 5376 | |
| 5377 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5378 | struct btrfs_delayed_extent_op *extent_op; |
| 5379 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5380 | BUG_ON(!extent_op); |
| 5381 | if (key) |
| 5382 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5383 | else |
| 5384 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5385 | extent_op->flags_to_set = flags; |
| 5386 | extent_op->update_key = 1; |
| 5387 | extent_op->update_flags = 1; |
| 5388 | extent_op->is_data = 0; |
| 5389 | |
| 5390 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5391 | ins.offset, parent, root_objectid, |
| 5392 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5393 | extent_op); |
| 5394 | BUG_ON(ret); |
| 5395 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5396 | return buf; |
| 5397 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5398 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5399 | struct walk_control { |
| 5400 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5401 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5402 | struct btrfs_key update_progress; |
| 5403 | int stage; |
| 5404 | int level; |
| 5405 | int shared_level; |
| 5406 | int update_ref; |
| 5407 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5408 | int reada_slot; |
| 5409 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5410 | }; |
| 5411 | |
| 5412 | #define DROP_REFERENCE 1 |
| 5413 | #define UPDATE_BACKREF 2 |
| 5414 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5415 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5416 | struct btrfs_root *root, |
| 5417 | struct walk_control *wc, |
| 5418 | struct btrfs_path *path) |
| 5419 | { |
| 5420 | u64 bytenr; |
| 5421 | u64 generation; |
| 5422 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5423 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5424 | u64 last = 0; |
| 5425 | u32 nritems; |
| 5426 | u32 blocksize; |
| 5427 | struct btrfs_key key; |
| 5428 | struct extent_buffer *eb; |
| 5429 | int ret; |
| 5430 | int slot; |
| 5431 | int nread = 0; |
| 5432 | |
| 5433 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5434 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5435 | wc->reada_count = max(wc->reada_count, 2); |
| 5436 | } else { |
| 5437 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5438 | wc->reada_count = min_t(int, wc->reada_count, |
| 5439 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5440 | } |
| 5441 | |
| 5442 | eb = path->nodes[wc->level]; |
| 5443 | nritems = btrfs_header_nritems(eb); |
| 5444 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5445 | |
| 5446 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5447 | if (nread >= wc->reada_count) |
| 5448 | break; |
| 5449 | |
| 5450 | cond_resched(); |
| 5451 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5452 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5453 | |
| 5454 | if (slot == path->slots[wc->level]) |
| 5455 | goto reada; |
| 5456 | |
| 5457 | if (wc->stage == UPDATE_BACKREF && |
| 5458 | generation <= root->root_key.offset) |
| 5459 | continue; |
| 5460 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5461 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5462 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5463 | &refs, &flags); |
| 5464 | BUG_ON(ret); |
| 5465 | BUG_ON(refs == 0); |
| 5466 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5467 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5468 | if (refs == 1) |
| 5469 | goto reada; |
| 5470 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5471 | if (wc->level == 1 && |
| 5472 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5473 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5474 | if (!wc->update_ref || |
| 5475 | generation <= root->root_key.offset) |
| 5476 | continue; |
| 5477 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5478 | ret = btrfs_comp_cpu_keys(&key, |
| 5479 | &wc->update_progress); |
| 5480 | if (ret < 0) |
| 5481 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5482 | } else { |
| 5483 | if (wc->level == 1 && |
| 5484 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5485 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5486 | } |
| 5487 | reada: |
| 5488 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5489 | generation); |
| 5490 | if (ret) |
| 5491 | break; |
| 5492 | last = bytenr + blocksize; |
| 5493 | nread++; |
| 5494 | } |
| 5495 | wc->reada_slot = slot; |
| 5496 | } |
| 5497 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5498 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5499 | * hepler to process tree block while walking down the tree. |
| 5500 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5501 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5502 | * back refs for pointers in the block. |
| 5503 | * |
| 5504 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5505 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5506 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5507 | struct btrfs_root *root, |
| 5508 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5509 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5510 | { |
| 5511 | int level = wc->level; |
| 5512 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5513 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5514 | int ret; |
| 5515 | |
| 5516 | if (wc->stage == UPDATE_BACKREF && |
| 5517 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5518 | return 1; |
| 5519 | |
| 5520 | /* |
| 5521 | * when reference count of tree block is 1, it won't increase |
| 5522 | * again. once full backref flag is set, we never clear it. |
| 5523 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5524 | if (lookup_info && |
| 5525 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5526 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5527 | BUG_ON(!path->locks[level]); |
| 5528 | ret = btrfs_lookup_extent_info(trans, root, |
| 5529 | eb->start, eb->len, |
| 5530 | &wc->refs[level], |
| 5531 | &wc->flags[level]); |
| 5532 | BUG_ON(ret); |
| 5533 | BUG_ON(wc->refs[level] == 0); |
| 5534 | } |
| 5535 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5536 | if (wc->stage == DROP_REFERENCE) { |
| 5537 | if (wc->refs[level] > 1) |
| 5538 | return 1; |
| 5539 | |
| 5540 | if (path->locks[level] && !wc->keep_locks) { |
| 5541 | btrfs_tree_unlock(eb); |
| 5542 | path->locks[level] = 0; |
| 5543 | } |
| 5544 | return 0; |
| 5545 | } |
| 5546 | |
| 5547 | /* wc->stage == UPDATE_BACKREF */ |
| 5548 | if (!(wc->flags[level] & flag)) { |
| 5549 | BUG_ON(!path->locks[level]); |
| 5550 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5551 | BUG_ON(ret); |
| 5552 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5553 | BUG_ON(ret); |
| 5554 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5555 | eb->len, flag, 0); |
| 5556 | BUG_ON(ret); |
| 5557 | wc->flags[level] |= flag; |
| 5558 | } |
| 5559 | |
| 5560 | /* |
| 5561 | * the block is shared by multiple trees, so it's not good to |
| 5562 | * keep the tree lock |
| 5563 | */ |
| 5564 | if (path->locks[level] && level > 0) { |
| 5565 | btrfs_tree_unlock(eb); |
| 5566 | path->locks[level] = 0; |
| 5567 | } |
| 5568 | return 0; |
| 5569 | } |
| 5570 | |
| 5571 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5572 | * hepler to process tree block pointer. |
| 5573 | * |
| 5574 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5575 | * reference count of the block pointed to. if the block |
| 5576 | * is shared and we need update back refs for the subtree |
| 5577 | * rooted at the block, this function changes wc->stage to |
| 5578 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5579 | * need to update back, this function drops the reference |
| 5580 | * to the block. |
| 5581 | * |
| 5582 | * NOTE: return value 1 means we should stop walking down. |
| 5583 | */ |
| 5584 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5585 | struct btrfs_root *root, |
| 5586 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5587 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5588 | { |
| 5589 | u64 bytenr; |
| 5590 | u64 generation; |
| 5591 | u64 parent; |
| 5592 | u32 blocksize; |
| 5593 | struct btrfs_key key; |
| 5594 | struct extent_buffer *next; |
| 5595 | int level = wc->level; |
| 5596 | int reada = 0; |
| 5597 | int ret = 0; |
| 5598 | |
| 5599 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5600 | path->slots[level]); |
| 5601 | /* |
| 5602 | * if the lower level block was created before the snapshot |
| 5603 | * was created, we know there is no need to update back refs |
| 5604 | * for the subtree |
| 5605 | */ |
| 5606 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5607 | generation <= root->root_key.offset) { |
| 5608 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5609 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5610 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5611 | |
| 5612 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5613 | blocksize = btrfs_level_size(root, level - 1); |
| 5614 | |
| 5615 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5616 | if (!next) { |
| 5617 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5618 | if (!next) |
| 5619 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5620 | reada = 1; |
| 5621 | } |
| 5622 | btrfs_tree_lock(next); |
| 5623 | btrfs_set_lock_blocking(next); |
| 5624 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5625 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5626 | &wc->refs[level - 1], |
| 5627 | &wc->flags[level - 1]); |
| 5628 | BUG_ON(ret); |
| 5629 | BUG_ON(wc->refs[level - 1] == 0); |
| 5630 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5631 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5632 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5633 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5634 | if (level == 1 && |
| 5635 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5636 | goto skip; |
| 5637 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5638 | if (!wc->update_ref || |
| 5639 | generation <= root->root_key.offset) |
| 5640 | goto skip; |
| 5641 | |
| 5642 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 5643 | path->slots[level]); |
| 5644 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 5645 | if (ret < 0) |
| 5646 | goto skip; |
| 5647 | |
| 5648 | wc->stage = UPDATE_BACKREF; |
| 5649 | wc->shared_level = level - 1; |
| 5650 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5651 | } else { |
| 5652 | if (level == 1 && |
| 5653 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5654 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5655 | } |
| 5656 | |
| 5657 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 5658 | btrfs_tree_unlock(next); |
| 5659 | free_extent_buffer(next); |
| 5660 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5661 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5662 | } |
| 5663 | |
| 5664 | if (!next) { |
| 5665 | if (reada && level == 1) |
| 5666 | reada_walk_down(trans, root, wc, path); |
| 5667 | next = read_tree_block(root, bytenr, blocksize, generation); |
| 5668 | btrfs_tree_lock(next); |
| 5669 | btrfs_set_lock_blocking(next); |
| 5670 | } |
| 5671 | |
| 5672 | level--; |
| 5673 | BUG_ON(level != btrfs_header_level(next)); |
| 5674 | path->nodes[level] = next; |
| 5675 | path->slots[level] = 0; |
| 5676 | path->locks[level] = 1; |
| 5677 | wc->level = level; |
| 5678 | if (wc->level == 1) |
| 5679 | wc->reada_slot = 0; |
| 5680 | return 0; |
| 5681 | skip: |
| 5682 | wc->refs[level - 1] = 0; |
| 5683 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5684 | if (wc->stage == DROP_REFERENCE) { |
| 5685 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 5686 | parent = path->nodes[level]->start; |
| 5687 | } else { |
| 5688 | BUG_ON(root->root_key.objectid != |
| 5689 | btrfs_header_owner(path->nodes[level])); |
| 5690 | parent = 0; |
| 5691 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5692 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5693 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 5694 | root->root_key.objectid, level - 1, 0); |
| 5695 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5696 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5697 | btrfs_tree_unlock(next); |
| 5698 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5699 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5700 | return 1; |
| 5701 | } |
| 5702 | |
| 5703 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5704 | * hepler to process tree block while walking up the tree. |
| 5705 | * |
| 5706 | * when wc->stage == DROP_REFERENCE, this function drops |
| 5707 | * reference count on the block. |
| 5708 | * |
| 5709 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 5710 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 5711 | * to UPDATE_BACKREF previously while processing the block. |
| 5712 | * |
| 5713 | * NOTE: return value 1 means we should stop walking up. |
| 5714 | */ |
| 5715 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 5716 | struct btrfs_root *root, |
| 5717 | struct btrfs_path *path, |
| 5718 | struct walk_control *wc) |
| 5719 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5720 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5721 | int level = wc->level; |
| 5722 | struct extent_buffer *eb = path->nodes[level]; |
| 5723 | u64 parent = 0; |
| 5724 | |
| 5725 | if (wc->stage == UPDATE_BACKREF) { |
| 5726 | BUG_ON(wc->shared_level < level); |
| 5727 | if (level < wc->shared_level) |
| 5728 | goto out; |
| 5729 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5730 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 5731 | if (ret > 0) |
| 5732 | wc->update_ref = 0; |
| 5733 | |
| 5734 | wc->stage = DROP_REFERENCE; |
| 5735 | wc->shared_level = -1; |
| 5736 | path->slots[level] = 0; |
| 5737 | |
| 5738 | /* |
| 5739 | * check reference count again if the block isn't locked. |
| 5740 | * we should start walking down the tree again if reference |
| 5741 | * count is one. |
| 5742 | */ |
| 5743 | if (!path->locks[level]) { |
| 5744 | BUG_ON(level == 0); |
| 5745 | btrfs_tree_lock(eb); |
| 5746 | btrfs_set_lock_blocking(eb); |
| 5747 | path->locks[level] = 1; |
| 5748 | |
| 5749 | ret = btrfs_lookup_extent_info(trans, root, |
| 5750 | eb->start, eb->len, |
| 5751 | &wc->refs[level], |
| 5752 | &wc->flags[level]); |
| 5753 | BUG_ON(ret); |
| 5754 | BUG_ON(wc->refs[level] == 0); |
| 5755 | if (wc->refs[level] == 1) { |
| 5756 | btrfs_tree_unlock(eb); |
| 5757 | path->locks[level] = 0; |
| 5758 | return 1; |
| 5759 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | /* wc->stage == DROP_REFERENCE */ |
| 5764 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 5765 | |
| 5766 | if (wc->refs[level] == 1) { |
| 5767 | if (level == 0) { |
| 5768 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5769 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 5770 | else |
| 5771 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5772 | BUG_ON(ret); |
| 5773 | } |
| 5774 | /* make block locked assertion in clean_tree_block happy */ |
| 5775 | if (!path->locks[level] && |
| 5776 | btrfs_header_generation(eb) == trans->transid) { |
| 5777 | btrfs_tree_lock(eb); |
| 5778 | btrfs_set_lock_blocking(eb); |
| 5779 | path->locks[level] = 1; |
| 5780 | } |
| 5781 | clean_tree_block(trans, root, eb); |
| 5782 | } |
| 5783 | |
| 5784 | if (eb == root->node) { |
| 5785 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5786 | parent = eb->start; |
| 5787 | else |
| 5788 | BUG_ON(root->root_key.objectid != |
| 5789 | btrfs_header_owner(eb)); |
| 5790 | } else { |
| 5791 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 5792 | parent = path->nodes[level + 1]->start; |
| 5793 | else |
| 5794 | BUG_ON(root->root_key.objectid != |
| 5795 | btrfs_header_owner(path->nodes[level + 1])); |
| 5796 | } |
| 5797 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5798 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5799 | out: |
| 5800 | wc->refs[level] = 0; |
| 5801 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5802 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5803 | } |
| 5804 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5805 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 5806 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5807 | struct btrfs_path *path, |
| 5808 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5809 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5810 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5811 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5812 | int ret; |
| 5813 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5814 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5815 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5816 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5817 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5818 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5819 | if (level == 0) |
| 5820 | break; |
| 5821 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 5822 | if (path->slots[level] >= |
| 5823 | btrfs_header_nritems(path->nodes[level])) |
| 5824 | break; |
| 5825 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5826 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5827 | if (ret > 0) { |
| 5828 | path->slots[level]++; |
| 5829 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5830 | } else if (ret < 0) |
| 5831 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5832 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5833 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5834 | return 0; |
| 5835 | } |
| 5836 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5837 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5838 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5839 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5840 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5841 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5842 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5843 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5844 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5845 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 5846 | while (level < max_level && path->nodes[level]) { |
| 5847 | wc->level = level; |
| 5848 | if (path->slots[level] + 1 < |
| 5849 | btrfs_header_nritems(path->nodes[level])) { |
| 5850 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5851 | return 0; |
| 5852 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5853 | ret = walk_up_proc(trans, root, path, wc); |
| 5854 | if (ret > 0) |
| 5855 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5856 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5857 | if (path->locks[level]) { |
| 5858 | btrfs_tree_unlock(path->nodes[level]); |
| 5859 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5860 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5861 | free_extent_buffer(path->nodes[level]); |
| 5862 | path->nodes[level] = NULL; |
| 5863 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5864 | } |
| 5865 | } |
| 5866 | return 1; |
| 5867 | } |
| 5868 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5869 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5870 | * drop a subvolume tree. |
| 5871 | * |
| 5872 | * this function traverses the tree freeing any blocks that only |
| 5873 | * referenced by the tree. |
| 5874 | * |
| 5875 | * when a shared tree block is found. this function decreases its |
| 5876 | * reference count by one. if update_ref is true, this function |
| 5877 | * also make sure backrefs for the shared block and all lower level |
| 5878 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5879 | */ |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5880 | int btrfs_drop_snapshot(struct btrfs_root *root, |
| 5881 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5882 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5883 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5884 | struct btrfs_trans_handle *trans; |
| 5885 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5886 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5887 | struct walk_control *wc; |
| 5888 | struct btrfs_key key; |
| 5889 | int err = 0; |
| 5890 | int ret; |
| 5891 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5892 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5893 | path = btrfs_alloc_path(); |
| 5894 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5895 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5896 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5897 | BUG_ON(!wc); |
| 5898 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5899 | trans = btrfs_start_transaction(tree_root, 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5900 | if (block_rsv) |
| 5901 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5902 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5903 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5904 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5905 | path->nodes[level] = btrfs_lock_root_node(root); |
| 5906 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5907 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5908 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5909 | memset(&wc->update_progress, 0, |
| 5910 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5911 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5912 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5913 | memcpy(&wc->update_progress, &key, |
| 5914 | sizeof(wc->update_progress)); |
| 5915 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5916 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5917 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 5918 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5919 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5920 | path->lowest_level = 0; |
| 5921 | if (ret < 0) { |
| 5922 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 5923 | goto out; |
| 5924 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5925 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5926 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5927 | /* |
| 5928 | * unlock our path, this is safe because only this |
| 5929 | * function is allowed to delete this snapshot |
| 5930 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5931 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5932 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5933 | level = btrfs_header_level(root->node); |
| 5934 | while (1) { |
| 5935 | btrfs_tree_lock(path->nodes[level]); |
| 5936 | btrfs_set_lock_blocking(path->nodes[level]); |
| 5937 | |
| 5938 | ret = btrfs_lookup_extent_info(trans, root, |
| 5939 | path->nodes[level]->start, |
| 5940 | path->nodes[level]->len, |
| 5941 | &wc->refs[level], |
| 5942 | &wc->flags[level]); |
| 5943 | BUG_ON(ret); |
| 5944 | BUG_ON(wc->refs[level] == 0); |
| 5945 | |
| 5946 | if (level == root_item->drop_level) |
| 5947 | break; |
| 5948 | |
| 5949 | btrfs_tree_unlock(path->nodes[level]); |
| 5950 | WARN_ON(wc->refs[level] != 1); |
| 5951 | level--; |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | wc->level = level; |
| 5956 | wc->shared_level = -1; |
| 5957 | wc->stage = DROP_REFERENCE; |
| 5958 | wc->update_ref = update_ref; |
| 5959 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5960 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5961 | |
| 5962 | while (1) { |
| 5963 | ret = walk_down_tree(trans, root, path, wc); |
| 5964 | if (ret < 0) { |
| 5965 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5966 | break; |
| 5967 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5968 | |
| 5969 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 5970 | if (ret < 0) { |
| 5971 | err = ret; |
| 5972 | break; |
| 5973 | } |
| 5974 | |
| 5975 | if (ret > 0) { |
| 5976 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 5977 | break; |
| 5978 | } |
| 5979 | |
| 5980 | if (wc->stage == DROP_REFERENCE) { |
| 5981 | level = wc->level; |
| 5982 | btrfs_node_key(path->nodes[level], |
| 5983 | &root_item->drop_progress, |
| 5984 | path->slots[level]); |
| 5985 | root_item->drop_level = level; |
| 5986 | } |
| 5987 | |
| 5988 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5989 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5990 | ret = btrfs_update_root(trans, tree_root, |
| 5991 | &root->root_key, |
| 5992 | root_item); |
| 5993 | BUG_ON(ret); |
| 5994 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5995 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5996 | trans = btrfs_start_transaction(tree_root, 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 5997 | if (block_rsv) |
| 5998 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 5999 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6000 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6001 | btrfs_release_path(root, path); |
| 6002 | BUG_ON(err); |
| 6003 | |
| 6004 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6005 | BUG_ON(ret); |
| 6006 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6007 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6008 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6009 | NULL, NULL); |
| 6010 | BUG_ON(ret < 0); |
| 6011 | if (ret > 0) { |
| 6012 | ret = btrfs_del_orphan_item(trans, tree_root, |
| 6013 | root->root_key.objectid); |
| 6014 | BUG_ON(ret); |
| 6015 | } |
| 6016 | } |
| 6017 | |
| 6018 | if (root->in_radix) { |
| 6019 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6020 | } else { |
| 6021 | free_extent_buffer(root->node); |
| 6022 | free_extent_buffer(root->commit_root); |
| 6023 | kfree(root); |
| 6024 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6025 | out: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6026 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6027 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6028 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6029 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6030 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6031 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6032 | /* |
| 6033 | * drop subtree rooted at tree block 'node'. |
| 6034 | * |
| 6035 | * NOTE: this function will unlock and release tree block 'node' |
| 6036 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6037 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6038 | struct btrfs_root *root, |
| 6039 | struct extent_buffer *node, |
| 6040 | struct extent_buffer *parent) |
| 6041 | { |
| 6042 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6043 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6044 | int level; |
| 6045 | int parent_level; |
| 6046 | int ret = 0; |
| 6047 | int wret; |
| 6048 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6049 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6050 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6051 | path = btrfs_alloc_path(); |
| 6052 | BUG_ON(!path); |
| 6053 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6054 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 6055 | BUG_ON(!wc); |
| 6056 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6057 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6058 | parent_level = btrfs_header_level(parent); |
| 6059 | extent_buffer_get(parent); |
| 6060 | path->nodes[parent_level] = parent; |
| 6061 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6062 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6063 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6064 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6065 | path->nodes[level] = node; |
| 6066 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6067 | path->locks[level] = 1; |
| 6068 | |
| 6069 | wc->refs[parent_level] = 1; |
| 6070 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6071 | wc->level = level; |
| 6072 | wc->shared_level = -1; |
| 6073 | wc->stage = DROP_REFERENCE; |
| 6074 | wc->update_ref = 0; |
| 6075 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6076 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6077 | |
| 6078 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6079 | wret = walk_down_tree(trans, root, path, wc); |
| 6080 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6081 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6082 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6083 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6084 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6085 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6086 | if (wret < 0) |
| 6087 | ret = wret; |
| 6088 | if (wret != 0) |
| 6089 | break; |
| 6090 | } |
| 6091 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6092 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6093 | btrfs_free_path(path); |
| 6094 | return ret; |
| 6095 | } |
| 6096 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6097 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6098 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 6099 | unsigned long nr) |
| 6100 | { |
| 6101 | return min(last, start + nr - 1); |
| 6102 | } |
| 6103 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6104 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6105 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6106 | { |
| 6107 | u64 page_start; |
| 6108 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6109 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6110 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6111 | unsigned long i; |
| 6112 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6113 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6114 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6115 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6116 | unsigned int total_read = 0; |
| 6117 | unsigned int total_dirty = 0; |
| 6118 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6119 | |
| 6120 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6121 | |
| 6122 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6123 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6124 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 6125 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6126 | /* make sure the dirty trick played by the caller work */ |
| 6127 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6128 | first_index, last_index); |
| 6129 | if (ret) |
| 6130 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6131 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6132 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6133 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6134 | for (i = first_index ; i <= last_index; i++) { |
| 6135 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6136 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6137 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6138 | } |
| 6139 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6140 | again: |
| 6141 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6142 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6143 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6144 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6145 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6146 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6147 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6148 | if (!PageUptodate(page)) { |
| 6149 | btrfs_readpage(NULL, page); |
| 6150 | lock_page(page); |
| 6151 | if (!PageUptodate(page)) { |
| 6152 | unlock_page(page); |
| 6153 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6154 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6155 | goto out_unlock; |
| 6156 | } |
| 6157 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6158 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6159 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6160 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 6161 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6162 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6163 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6164 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6165 | if (ordered) { |
| 6166 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 6167 | unlock_page(page); |
| 6168 | page_cache_release(page); |
| 6169 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6170 | btrfs_put_ordered_extent(ordered); |
| 6171 | goto again; |
| 6172 | } |
| 6173 | set_page_extent_mapped(page); |
| 6174 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6175 | if (i == first_index) |
| 6176 | set_extent_bits(io_tree, page_start, page_end, |
| 6177 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6178 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6179 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6180 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6181 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6182 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6183 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6184 | unlock_page(page); |
| 6185 | page_cache_release(page); |
| 6186 | } |
| 6187 | |
| 6188 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6189 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6190 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6191 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6192 | return ret; |
| 6193 | } |
| 6194 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6195 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6196 | struct btrfs_key *extent_key, |
| 6197 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6198 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6199 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6200 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 6201 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6202 | u64 start = extent_key->objectid - offset; |
| 6203 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6204 | |
| 6205 | em = alloc_extent_map(GFP_NOFS); |
| 6206 | BUG_ON(!em || IS_ERR(em)); |
| 6207 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6208 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6209 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6210 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6211 | em->block_start = extent_key->objectid; |
| 6212 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 6213 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6214 | |
| 6215 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6216 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6217 | while (1) { |
| 6218 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6219 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6220 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6221 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6222 | if (ret != -EEXIST) { |
| 6223 | free_extent_map(em); |
| 6224 | break; |
| 6225 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6226 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6227 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6228 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6229 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6230 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6231 | } |
| 6232 | |
| 6233 | struct btrfs_ref_path { |
| 6234 | u64 extent_start; |
| 6235 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 6236 | u64 root_objectid; |
| 6237 | u64 root_generation; |
| 6238 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6239 | u32 num_refs; |
| 6240 | int lowest_level; |
| 6241 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6242 | int shared_level; |
| 6243 | |
| 6244 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 6245 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6246 | }; |
| 6247 | |
| 6248 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6249 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6250 | u64 disk_bytenr; |
| 6251 | u64 disk_num_bytes; |
| 6252 | u64 offset; |
| 6253 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6254 | u8 compression; |
| 6255 | u8 encryption; |
| 6256 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6257 | }; |
| 6258 | |
| 6259 | static int is_cowonly_root(u64 root_objectid) |
| 6260 | { |
| 6261 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 6262 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 6263 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 6264 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 6265 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 6266 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6267 | return 1; |
| 6268 | return 0; |
| 6269 | } |
| 6270 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6271 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6272 | struct btrfs_root *extent_root, |
| 6273 | struct btrfs_ref_path *ref_path, |
| 6274 | int first_time) |
| 6275 | { |
| 6276 | struct extent_buffer *leaf; |
| 6277 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6278 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6279 | struct btrfs_key key; |
| 6280 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6281 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6282 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6283 | int level; |
| 6284 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6285 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6286 | path = btrfs_alloc_path(); |
| 6287 | if (!path) |
| 6288 | return -ENOMEM; |
| 6289 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6290 | if (first_time) { |
| 6291 | ref_path->lowest_level = -1; |
| 6292 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6293 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6294 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6295 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6296 | walk_down: |
| 6297 | level = ref_path->current_level - 1; |
| 6298 | while (level >= -1) { |
| 6299 | u64 parent; |
| 6300 | if (level < ref_path->lowest_level) |
| 6301 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6302 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6303 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6304 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6305 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6306 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6307 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6308 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6309 | parent = ref_path->nodes[level + 1]; |
| 6310 | ref_path->nodes[level + 1] = 0; |
| 6311 | ref_path->current_level = level; |
| 6312 | BUG_ON(parent == 0); |
| 6313 | |
| 6314 | key.objectid = bytenr; |
| 6315 | key.offset = parent + 1; |
| 6316 | key.type = BTRFS_EXTENT_REF_KEY; |
| 6317 | |
| 6318 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6319 | if (ret < 0) |
| 6320 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6321 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6322 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6323 | leaf = path->nodes[0]; |
| 6324 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6325 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6326 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6327 | if (ret < 0) |
| 6328 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6329 | if (ret > 0) |
| 6330 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6331 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6332 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6333 | |
| 6334 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6335 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6336 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 6337 | if (level < ref_path->shared_level) |
| 6338 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6339 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6340 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6341 | next: |
| 6342 | level--; |
| 6343 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6344 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6345 | } |
| 6346 | /* reached lowest level */ |
| 6347 | ret = 1; |
| 6348 | goto out; |
| 6349 | walk_up: |
| 6350 | level = ref_path->current_level; |
| 6351 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 6352 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6353 | |
| 6354 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6355 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6356 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6357 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6358 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6359 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6360 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6361 | key.objectid = bytenr; |
| 6362 | key.offset = 0; |
| 6363 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6364 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6365 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 6366 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6367 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6368 | |
| 6369 | leaf = path->nodes[0]; |
| 6370 | nritems = btrfs_header_nritems(leaf); |
| 6371 | if (path->slots[0] >= nritems) { |
| 6372 | ret = btrfs_next_leaf(extent_root, path); |
| 6373 | if (ret < 0) |
| 6374 | goto out; |
| 6375 | if (ret > 0) { |
| 6376 | /* the extent was freed by someone */ |
| 6377 | if (ref_path->lowest_level == level) |
| 6378 | goto out; |
| 6379 | btrfs_release_path(extent_root, path); |
| 6380 | goto walk_down; |
| 6381 | } |
| 6382 | leaf = path->nodes[0]; |
| 6383 | } |
| 6384 | |
| 6385 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6386 | if (found_key.objectid != bytenr || |
| 6387 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 6388 | /* the extent was freed by someone */ |
| 6389 | if (ref_path->lowest_level == level) { |
| 6390 | ret = 1; |
| 6391 | goto out; |
| 6392 | } |
| 6393 | btrfs_release_path(extent_root, path); |
| 6394 | goto walk_down; |
| 6395 | } |
| 6396 | found: |
| 6397 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 6398 | struct btrfs_extent_ref); |
| 6399 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 6400 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 6401 | if (first_time) { |
| 6402 | level = (int)ref_objectid; |
| 6403 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 6404 | ref_path->lowest_level = level; |
| 6405 | ref_path->current_level = level; |
| 6406 | ref_path->nodes[level] = bytenr; |
| 6407 | } else { |
| 6408 | WARN_ON(ref_objectid != level); |
| 6409 | } |
| 6410 | } else { |
| 6411 | WARN_ON(level != -1); |
| 6412 | } |
| 6413 | first_time = 0; |
| 6414 | |
| 6415 | if (ref_path->lowest_level == level) { |
| 6416 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6417 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 6418 | } |
| 6419 | |
| 6420 | /* |
| 6421 | * the block is tree root or the block isn't in reference |
| 6422 | * counted tree. |
| 6423 | */ |
| 6424 | if (found_key.objectid == found_key.offset || |
| 6425 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 6426 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6427 | ref_path->root_generation = |
| 6428 | btrfs_ref_generation(leaf, ref); |
| 6429 | if (level < 0) { |
| 6430 | /* special reference from the tree log */ |
| 6431 | ref_path->nodes[0] = found_key.offset; |
| 6432 | ref_path->current_level = 0; |
| 6433 | } |
| 6434 | ret = 0; |
| 6435 | goto out; |
| 6436 | } |
| 6437 | |
| 6438 | level++; |
| 6439 | BUG_ON(ref_path->nodes[level] != 0); |
| 6440 | ref_path->nodes[level] = found_key.offset; |
| 6441 | ref_path->current_level = level; |
| 6442 | |
| 6443 | /* |
| 6444 | * the reference was created in the running transaction, |
| 6445 | * no need to continue walking up. |
| 6446 | */ |
| 6447 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 6448 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6449 | ref_path->root_generation = |
| 6450 | btrfs_ref_generation(leaf, ref); |
| 6451 | ret = 0; |
| 6452 | goto out; |
| 6453 | } |
| 6454 | |
| 6455 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6456 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6457 | } |
| 6458 | /* reached max tree level, but no tree root found. */ |
| 6459 | BUG(); |
| 6460 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6461 | btrfs_free_path(path); |
| 6462 | return ret; |
| 6463 | } |
| 6464 | |
| 6465 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 6466 | struct btrfs_root *extent_root, |
| 6467 | struct btrfs_ref_path *ref_path, |
| 6468 | u64 extent_start) |
| 6469 | { |
| 6470 | memset(ref_path, 0, sizeof(*ref_path)); |
| 6471 | ref_path->extent_start = extent_start; |
| 6472 | |
| 6473 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 6474 | } |
| 6475 | |
| 6476 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 6477 | struct btrfs_root *extent_root, |
| 6478 | struct btrfs_ref_path *ref_path) |
| 6479 | { |
| 6480 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 6481 | } |
| 6482 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6483 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6484 | struct btrfs_key *extent_key, |
| 6485 | u64 offset, int no_fragment, |
| 6486 | struct disk_extent **extents, |
| 6487 | int *nr_extents) |
| 6488 | { |
| 6489 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6490 | struct btrfs_path *path; |
| 6491 | struct btrfs_file_extent_item *fi; |
| 6492 | struct extent_buffer *leaf; |
| 6493 | struct disk_extent *exts = *extents; |
| 6494 | struct btrfs_key found_key; |
| 6495 | u64 cur_pos; |
| 6496 | u64 last_byte; |
| 6497 | u32 nritems; |
| 6498 | int nr = 0; |
| 6499 | int max = *nr_extents; |
| 6500 | int ret; |
| 6501 | |
| 6502 | WARN_ON(!no_fragment && *extents); |
| 6503 | if (!exts) { |
| 6504 | max = 1; |
| 6505 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6506 | if (!exts) |
| 6507 | return -ENOMEM; |
| 6508 | } |
| 6509 | |
| 6510 | path = btrfs_alloc_path(); |
| 6511 | BUG_ON(!path); |
| 6512 | |
| 6513 | cur_pos = extent_key->objectid - offset; |
| 6514 | last_byte = extent_key->objectid + extent_key->offset; |
| 6515 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 6516 | cur_pos, 0); |
| 6517 | if (ret < 0) |
| 6518 | goto out; |
| 6519 | if (ret > 0) { |
| 6520 | ret = -ENOENT; |
| 6521 | goto out; |
| 6522 | } |
| 6523 | |
| 6524 | while (1) { |
| 6525 | leaf = path->nodes[0]; |
| 6526 | nritems = btrfs_header_nritems(leaf); |
| 6527 | if (path->slots[0] >= nritems) { |
| 6528 | ret = btrfs_next_leaf(root, path); |
| 6529 | if (ret < 0) |
| 6530 | goto out; |
| 6531 | if (ret > 0) |
| 6532 | break; |
| 6533 | leaf = path->nodes[0]; |
| 6534 | } |
| 6535 | |
| 6536 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6537 | if (found_key.offset != cur_pos || |
| 6538 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 6539 | found_key.objectid != reloc_inode->i_ino) |
| 6540 | break; |
| 6541 | |
| 6542 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6543 | struct btrfs_file_extent_item); |
| 6544 | if (btrfs_file_extent_type(leaf, fi) != |
| 6545 | BTRFS_FILE_EXTENT_REG || |
| 6546 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6547 | break; |
| 6548 | |
| 6549 | if (nr == max) { |
| 6550 | struct disk_extent *old = exts; |
| 6551 | max *= 2; |
| 6552 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 6553 | memcpy(exts, old, sizeof(*exts) * nr); |
| 6554 | if (old != *extents) |
| 6555 | kfree(old); |
| 6556 | } |
| 6557 | |
| 6558 | exts[nr].disk_bytenr = |
| 6559 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6560 | exts[nr].disk_num_bytes = |
| 6561 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6562 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 6563 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6564 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 6565 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 6566 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 6567 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 6568 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6569 | BUG_ON(exts[nr].offset > 0); |
| 6570 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 6571 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6572 | |
| 6573 | cur_pos += exts[nr].num_bytes; |
| 6574 | nr++; |
| 6575 | |
| 6576 | if (cur_pos + offset >= last_byte) |
| 6577 | break; |
| 6578 | |
| 6579 | if (no_fragment) { |
| 6580 | ret = 1; |
| 6581 | goto out; |
| 6582 | } |
| 6583 | path->slots[0]++; |
| 6584 | } |
| 6585 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6586 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6587 | if (cur_pos + offset < last_byte) { |
| 6588 | ret = -ENOENT; |
| 6589 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6590 | } |
| 6591 | ret = 0; |
| 6592 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6593 | btrfs_free_path(path); |
| 6594 | if (ret) { |
| 6595 | if (exts != *extents) |
| 6596 | kfree(exts); |
| 6597 | } else { |
| 6598 | *extents = exts; |
| 6599 | *nr_extents = nr; |
| 6600 | } |
| 6601 | return ret; |
| 6602 | } |
| 6603 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6604 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6605 | struct btrfs_root *root, |
| 6606 | struct btrfs_path *path, |
| 6607 | struct btrfs_key *extent_key, |
| 6608 | struct btrfs_key *leaf_key, |
| 6609 | struct btrfs_ref_path *ref_path, |
| 6610 | struct disk_extent *new_extents, |
| 6611 | int nr_extents) |
| 6612 | { |
| 6613 | struct extent_buffer *leaf; |
| 6614 | struct btrfs_file_extent_item *fi; |
| 6615 | struct inode *inode = NULL; |
| 6616 | struct btrfs_key key; |
| 6617 | u64 lock_start = 0; |
| 6618 | u64 lock_end = 0; |
| 6619 | u64 num_bytes; |
| 6620 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6621 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6622 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6623 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6624 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6625 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6626 | int ret; |
| 6627 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6628 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6629 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6630 | if (key.objectid < ref_path->owner_objectid || |
| 6631 | (key.objectid == ref_path->owner_objectid && |
| 6632 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 6633 | key.objectid = ref_path->owner_objectid; |
| 6634 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 6635 | key.offset = 0; |
| 6636 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6637 | } |
| 6638 | |
| 6639 | while (1) { |
| 6640 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 6641 | if (ret < 0) |
| 6642 | goto out; |
| 6643 | |
| 6644 | leaf = path->nodes[0]; |
| 6645 | nritems = btrfs_header_nritems(leaf); |
| 6646 | next: |
| 6647 | if (extent_locked && ret > 0) { |
| 6648 | /* |
| 6649 | * the file extent item was modified by someone |
| 6650 | * before the extent got locked. |
| 6651 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6652 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6653 | lock_end, GFP_NOFS); |
| 6654 | extent_locked = 0; |
| 6655 | } |
| 6656 | |
| 6657 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6658 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6659 | break; |
| 6660 | |
| 6661 | BUG_ON(extent_locked); |
| 6662 | ret = btrfs_next_leaf(root, path); |
| 6663 | if (ret < 0) |
| 6664 | goto out; |
| 6665 | if (ret > 0) |
| 6666 | break; |
| 6667 | leaf = path->nodes[0]; |
| 6668 | nritems = btrfs_header_nritems(leaf); |
| 6669 | } |
| 6670 | |
| 6671 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 6672 | |
| 6673 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 6674 | if ((key.objectid > ref_path->owner_objectid) || |
| 6675 | (key.objectid == ref_path->owner_objectid && |
| 6676 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6677 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6678 | break; |
| 6679 | } |
| 6680 | |
| 6681 | if (inode && key.objectid != inode->i_ino) { |
| 6682 | BUG_ON(extent_locked); |
| 6683 | btrfs_release_path(root, path); |
| 6684 | mutex_unlock(&inode->i_mutex); |
| 6685 | iput(inode); |
| 6686 | inode = NULL; |
| 6687 | continue; |
| 6688 | } |
| 6689 | |
| 6690 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 6691 | path->slots[0]++; |
| 6692 | ret = 1; |
| 6693 | goto next; |
| 6694 | } |
| 6695 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6696 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6697 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 6698 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 6699 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6700 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 6701 | extent_key->objectid)) { |
| 6702 | path->slots[0]++; |
| 6703 | ret = 1; |
| 6704 | goto next; |
| 6705 | } |
| 6706 | |
| 6707 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 6708 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 6709 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6710 | if (search_end == (u64)-1) { |
| 6711 | search_end = key.offset - ext_offset + |
| 6712 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 6713 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6714 | |
| 6715 | if (!extent_locked) { |
| 6716 | lock_start = key.offset; |
| 6717 | lock_end = lock_start + num_bytes - 1; |
| 6718 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6719 | if (lock_start > key.offset || |
| 6720 | lock_end + 1 < key.offset + num_bytes) { |
| 6721 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6722 | lock_start, lock_end, GFP_NOFS); |
| 6723 | extent_locked = 0; |
| 6724 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6725 | } |
| 6726 | |
| 6727 | if (!inode) { |
| 6728 | btrfs_release_path(root, path); |
| 6729 | |
| 6730 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 6731 | key.objectid, root); |
| 6732 | if (inode->i_state & I_NEW) { |
| 6733 | BTRFS_I(inode)->root = root; |
| 6734 | BTRFS_I(inode)->location.objectid = |
| 6735 | key.objectid; |
| 6736 | BTRFS_I(inode)->location.type = |
| 6737 | BTRFS_INODE_ITEM_KEY; |
| 6738 | BTRFS_I(inode)->location.offset = 0; |
| 6739 | btrfs_read_locked_inode(inode); |
| 6740 | unlock_new_inode(inode); |
| 6741 | } |
| 6742 | /* |
| 6743 | * some code call btrfs_commit_transaction while |
| 6744 | * holding the i_mutex, so we can't use mutex_lock |
| 6745 | * here. |
| 6746 | */ |
| 6747 | if (is_bad_inode(inode) || |
| 6748 | !mutex_trylock(&inode->i_mutex)) { |
| 6749 | iput(inode); |
| 6750 | inode = NULL; |
| 6751 | key.offset = (u64)-1; |
| 6752 | goto skip; |
| 6753 | } |
| 6754 | } |
| 6755 | |
| 6756 | if (!extent_locked) { |
| 6757 | struct btrfs_ordered_extent *ordered; |
| 6758 | |
| 6759 | btrfs_release_path(root, path); |
| 6760 | |
| 6761 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6762 | lock_end, GFP_NOFS); |
| 6763 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 6764 | lock_end); |
| 6765 | if (ordered && |
| 6766 | ordered->file_offset <= lock_end && |
| 6767 | ordered->file_offset + ordered->len > lock_start) { |
| 6768 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 6769 | lock_start, lock_end, GFP_NOFS); |
| 6770 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6771 | btrfs_put_ordered_extent(ordered); |
| 6772 | key.offset += num_bytes; |
| 6773 | goto skip; |
| 6774 | } |
| 6775 | if (ordered) |
| 6776 | btrfs_put_ordered_extent(ordered); |
| 6777 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6778 | extent_locked = 1; |
| 6779 | continue; |
| 6780 | } |
| 6781 | |
| 6782 | if (nr_extents == 1) { |
| 6783 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6784 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6785 | new_extents[0].disk_bytenr); |
| 6786 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6787 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6788 | btrfs_mark_buffer_dirty(leaf); |
| 6789 | |
| 6790 | btrfs_drop_extent_cache(inode, key.offset, |
| 6791 | key.offset + num_bytes - 1, 0); |
| 6792 | |
| 6793 | ret = btrfs_inc_extent_ref(trans, root, |
| 6794 | new_extents[0].disk_bytenr, |
| 6795 | new_extents[0].disk_num_bytes, |
| 6796 | leaf->start, |
| 6797 | root->root_key.objectid, |
| 6798 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6799 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6800 | BUG_ON(ret); |
| 6801 | |
| 6802 | ret = btrfs_free_extent(trans, root, |
| 6803 | extent_key->objectid, |
| 6804 | extent_key->offset, |
| 6805 | leaf->start, |
| 6806 | btrfs_header_owner(leaf), |
| 6807 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6808 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6809 | BUG_ON(ret); |
| 6810 | |
| 6811 | btrfs_release_path(root, path); |
| 6812 | key.offset += num_bytes; |
| 6813 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6814 | BUG_ON(1); |
| 6815 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6816 | u64 alloc_hint; |
| 6817 | u64 extent_len; |
| 6818 | int i; |
| 6819 | /* |
| 6820 | * drop old extent pointer at first, then insert the |
| 6821 | * new pointers one bye one |
| 6822 | */ |
| 6823 | btrfs_release_path(root, path); |
| 6824 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 6825 | key.offset + num_bytes, |
| 6826 | key.offset, &alloc_hint); |
| 6827 | BUG_ON(ret); |
| 6828 | |
| 6829 | for (i = 0; i < nr_extents; i++) { |
| 6830 | if (ext_offset >= new_extents[i].num_bytes) { |
| 6831 | ext_offset -= new_extents[i].num_bytes; |
| 6832 | continue; |
| 6833 | } |
| 6834 | extent_len = min(new_extents[i].num_bytes - |
| 6835 | ext_offset, num_bytes); |
| 6836 | |
| 6837 | ret = btrfs_insert_empty_item(trans, root, |
| 6838 | path, &key, |
| 6839 | sizeof(*fi)); |
| 6840 | BUG_ON(ret); |
| 6841 | |
| 6842 | leaf = path->nodes[0]; |
| 6843 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 6844 | struct btrfs_file_extent_item); |
| 6845 | btrfs_set_file_extent_generation(leaf, fi, |
| 6846 | trans->transid); |
| 6847 | btrfs_set_file_extent_type(leaf, fi, |
| 6848 | BTRFS_FILE_EXTENT_REG); |
| 6849 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6850 | new_extents[i].disk_bytenr); |
| 6851 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6852 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6853 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 6854 | new_extents[i].ram_bytes); |
| 6855 | |
| 6856 | btrfs_set_file_extent_compression(leaf, fi, |
| 6857 | new_extents[i].compression); |
| 6858 | btrfs_set_file_extent_encryption(leaf, fi, |
| 6859 | new_extents[i].encryption); |
| 6860 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 6861 | new_extents[i].other_encoding); |
| 6862 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6863 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 6864 | extent_len); |
| 6865 | ext_offset += new_extents[i].offset; |
| 6866 | btrfs_set_file_extent_offset(leaf, fi, |
| 6867 | ext_offset); |
| 6868 | btrfs_mark_buffer_dirty(leaf); |
| 6869 | |
| 6870 | btrfs_drop_extent_cache(inode, key.offset, |
| 6871 | key.offset + extent_len - 1, 0); |
| 6872 | |
| 6873 | ret = btrfs_inc_extent_ref(trans, root, |
| 6874 | new_extents[i].disk_bytenr, |
| 6875 | new_extents[i].disk_num_bytes, |
| 6876 | leaf->start, |
| 6877 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6878 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6879 | BUG_ON(ret); |
| 6880 | btrfs_release_path(root, path); |
| 6881 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 6882 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6883 | |
| 6884 | ext_offset = 0; |
| 6885 | num_bytes -= extent_len; |
| 6886 | key.offset += extent_len; |
| 6887 | |
| 6888 | if (num_bytes == 0) |
| 6889 | break; |
| 6890 | } |
| 6891 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6892 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6893 | } |
| 6894 | |
| 6895 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6896 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6897 | lock_end, GFP_NOFS); |
| 6898 | extent_locked = 0; |
| 6899 | } |
| 6900 | skip: |
| 6901 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6902 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6903 | break; |
| 6904 | |
| 6905 | cond_resched(); |
| 6906 | } |
| 6907 | ret = 0; |
| 6908 | out: |
| 6909 | btrfs_release_path(root, path); |
| 6910 | if (inode) { |
| 6911 | mutex_unlock(&inode->i_mutex); |
| 6912 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6913 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 6914 | lock_end, GFP_NOFS); |
| 6915 | } |
| 6916 | iput(inode); |
| 6917 | } |
| 6918 | return ret; |
| 6919 | } |
| 6920 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6921 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 6922 | struct btrfs_root *root, |
| 6923 | struct extent_buffer *buf, u64 orig_start) |
| 6924 | { |
| 6925 | int level; |
| 6926 | int ret; |
| 6927 | |
| 6928 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 6929 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6930 | |
| 6931 | level = btrfs_header_level(buf); |
| 6932 | if (level == 0) { |
| 6933 | struct btrfs_leaf_ref *ref; |
| 6934 | struct btrfs_leaf_ref *orig_ref; |
| 6935 | |
| 6936 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 6937 | if (!orig_ref) |
| 6938 | return -ENOENT; |
| 6939 | |
| 6940 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 6941 | if (!ref) { |
| 6942 | btrfs_free_leaf_ref(root, orig_ref); |
| 6943 | return -ENOMEM; |
| 6944 | } |
| 6945 | |
| 6946 | ref->nritems = orig_ref->nritems; |
| 6947 | memcpy(ref->extents, orig_ref->extents, |
| 6948 | sizeof(ref->extents[0]) * ref->nritems); |
| 6949 | |
| 6950 | btrfs_free_leaf_ref(root, orig_ref); |
| 6951 | |
| 6952 | ref->root_gen = trans->transid; |
| 6953 | ref->bytenr = buf->start; |
| 6954 | ref->owner = btrfs_header_owner(buf); |
| 6955 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6956 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6957 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 6958 | WARN_ON(ret); |
| 6959 | btrfs_free_leaf_ref(root, ref); |
| 6960 | } |
| 6961 | return 0; |
| 6962 | } |
| 6963 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6964 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6965 | struct extent_buffer *leaf, |
| 6966 | struct btrfs_block_group_cache *group, |
| 6967 | struct btrfs_root *target_root) |
| 6968 | { |
| 6969 | struct btrfs_key key; |
| 6970 | struct inode *inode = NULL; |
| 6971 | struct btrfs_file_extent_item *fi; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 6972 | struct extent_state *cached_state = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6973 | u64 num_bytes; |
| 6974 | u64 skip_objectid = 0; |
| 6975 | u32 nritems; |
| 6976 | u32 i; |
| 6977 | |
| 6978 | nritems = btrfs_header_nritems(leaf); |
| 6979 | for (i = 0; i < nritems; i++) { |
| 6980 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6981 | if (key.objectid == skip_objectid || |
| 6982 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 6983 | continue; |
| 6984 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6985 | if (btrfs_file_extent_type(leaf, fi) == |
| 6986 | BTRFS_FILE_EXTENT_INLINE) |
| 6987 | continue; |
| 6988 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 6989 | continue; |
| 6990 | if (!inode || inode->i_ino != key.objectid) { |
| 6991 | iput(inode); |
| 6992 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 6993 | key.objectid, target_root, 1); |
| 6994 | } |
| 6995 | if (!inode) { |
| 6996 | skip_objectid = key.objectid; |
| 6997 | continue; |
| 6998 | } |
| 6999 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 7000 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7001 | lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset, |
| 7002 | key.offset + num_bytes - 1, 0, &cached_state, |
| 7003 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7004 | btrfs_drop_extent_cache(inode, key.offset, |
| 7005 | key.offset + num_bytes - 1, 1); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7006 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset, |
| 7007 | key.offset + num_bytes - 1, &cached_state, |
| 7008 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7009 | cond_resched(); |
| 7010 | } |
| 7011 | iput(inode); |
| 7012 | return 0; |
| 7013 | } |
| 7014 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7015 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7016 | struct btrfs_root *root, |
| 7017 | struct extent_buffer *leaf, |
| 7018 | struct btrfs_block_group_cache *group, |
| 7019 | struct inode *reloc_inode) |
| 7020 | { |
| 7021 | struct btrfs_key key; |
| 7022 | struct btrfs_key extent_key; |
| 7023 | struct btrfs_file_extent_item *fi; |
| 7024 | struct btrfs_leaf_ref *ref; |
| 7025 | struct disk_extent *new_extent; |
| 7026 | u64 bytenr; |
| 7027 | u64 num_bytes; |
| 7028 | u32 nritems; |
| 7029 | u32 i; |
| 7030 | int ext_index; |
| 7031 | int nr_extent; |
| 7032 | int ret; |
| 7033 | |
| 7034 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 7035 | BUG_ON(!new_extent); |
| 7036 | |
| 7037 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 7038 | BUG_ON(!ref); |
| 7039 | |
| 7040 | ext_index = -1; |
| 7041 | nritems = btrfs_header_nritems(leaf); |
| 7042 | for (i = 0; i < nritems; i++) { |
| 7043 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 7044 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 7045 | continue; |
| 7046 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 7047 | if (btrfs_file_extent_type(leaf, fi) == |
| 7048 | BTRFS_FILE_EXTENT_INLINE) |
| 7049 | continue; |
| 7050 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 7051 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7052 | if (bytenr == 0) |
| 7053 | continue; |
| 7054 | |
| 7055 | ext_index++; |
| 7056 | if (bytenr >= group->key.objectid + group->key.offset || |
| 7057 | bytenr + num_bytes <= group->key.objectid) |
| 7058 | continue; |
| 7059 | |
| 7060 | extent_key.objectid = bytenr; |
| 7061 | extent_key.offset = num_bytes; |
| 7062 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 7063 | nr_extent = 1; |
| 7064 | ret = get_new_locations(reloc_inode, &extent_key, |
| 7065 | group->key.objectid, 1, |
| 7066 | &new_extent, &nr_extent); |
| 7067 | if (ret > 0) |
| 7068 | continue; |
| 7069 | BUG_ON(ret < 0); |
| 7070 | |
| 7071 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 7072 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 7073 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 7074 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 7075 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7076 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7077 | new_extent->disk_bytenr); |
| 7078 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7079 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7080 | btrfs_mark_buffer_dirty(leaf); |
| 7081 | |
| 7082 | ret = btrfs_inc_extent_ref(trans, root, |
| 7083 | new_extent->disk_bytenr, |
| 7084 | new_extent->disk_num_bytes, |
| 7085 | leaf->start, |
| 7086 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7087 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7088 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7089 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7090 | ret = btrfs_free_extent(trans, root, |
| 7091 | bytenr, num_bytes, leaf->start, |
| 7092 | btrfs_header_owner(leaf), |
| 7093 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7094 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7095 | BUG_ON(ret); |
| 7096 | cond_resched(); |
| 7097 | } |
| 7098 | kfree(new_extent); |
| 7099 | BUG_ON(ext_index + 1 != ref->nritems); |
| 7100 | btrfs_free_leaf_ref(root, ref); |
| 7101 | return 0; |
| 7102 | } |
| 7103 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7104 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 7105 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7106 | { |
| 7107 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7108 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7109 | |
| 7110 | if (root->reloc_root) { |
| 7111 | reloc_root = root->reloc_root; |
| 7112 | root->reloc_root = NULL; |
| 7113 | list_add(&reloc_root->dead_list, |
| 7114 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7115 | |
| 7116 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 7117 | reloc_root->node->start); |
| 7118 | btrfs_set_root_level(&root->root_item, |
| 7119 | btrfs_header_level(reloc_root->node)); |
| 7120 | memset(&reloc_root->root_item.drop_progress, 0, |
| 7121 | sizeof(struct btrfs_disk_key)); |
| 7122 | reloc_root->root_item.drop_level = 0; |
| 7123 | |
| 7124 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 7125 | &reloc_root->root_key, |
| 7126 | &reloc_root->root_item); |
| 7127 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7128 | } |
| 7129 | return 0; |
| 7130 | } |
| 7131 | |
| 7132 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 7133 | { |
| 7134 | struct btrfs_trans_handle *trans; |
| 7135 | struct btrfs_root *reloc_root; |
| 7136 | struct btrfs_root *prev_root = NULL; |
| 7137 | struct list_head dead_roots; |
| 7138 | int ret; |
| 7139 | unsigned long nr; |
| 7140 | |
| 7141 | INIT_LIST_HEAD(&dead_roots); |
| 7142 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 7143 | |
| 7144 | while (!list_empty(&dead_roots)) { |
| 7145 | reloc_root = list_entry(dead_roots.prev, |
| 7146 | struct btrfs_root, dead_list); |
| 7147 | list_del_init(&reloc_root->dead_list); |
| 7148 | |
| 7149 | BUG_ON(reloc_root->commit_root != NULL); |
| 7150 | while (1) { |
| 7151 | trans = btrfs_join_transaction(root, 1); |
| 7152 | BUG_ON(!trans); |
| 7153 | |
| 7154 | mutex_lock(&root->fs_info->drop_mutex); |
| 7155 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 7156 | if (ret != -EAGAIN) |
| 7157 | break; |
| 7158 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7159 | |
| 7160 | nr = trans->blocks_used; |
| 7161 | ret = btrfs_end_transaction(trans, root); |
| 7162 | BUG_ON(ret); |
| 7163 | btrfs_btree_balance_dirty(root, nr); |
| 7164 | } |
| 7165 | |
| 7166 | free_extent_buffer(reloc_root->node); |
| 7167 | |
| 7168 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 7169 | &reloc_root->root_key); |
| 7170 | BUG_ON(ret); |
| 7171 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7172 | |
| 7173 | nr = trans->blocks_used; |
| 7174 | ret = btrfs_end_transaction(trans, root); |
| 7175 | BUG_ON(ret); |
| 7176 | btrfs_btree_balance_dirty(root, nr); |
| 7177 | |
| 7178 | kfree(prev_root); |
| 7179 | prev_root = reloc_root; |
| 7180 | } |
| 7181 | if (prev_root) { |
| 7182 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 7183 | kfree(prev_root); |
| 7184 | } |
| 7185 | return 0; |
| 7186 | } |
| 7187 | |
| 7188 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 7189 | { |
| 7190 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 7191 | return 0; |
| 7192 | } |
| 7193 | |
| 7194 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 7195 | { |
| 7196 | struct btrfs_root *reloc_root; |
| 7197 | struct btrfs_trans_handle *trans; |
| 7198 | struct btrfs_key location; |
| 7199 | int found; |
| 7200 | int ret; |
| 7201 | |
| 7202 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7203 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 7204 | BUG_ON(ret); |
| 7205 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 7206 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 7207 | |
| 7208 | if (found) { |
| 7209 | trans = btrfs_start_transaction(root, 1); |
| 7210 | BUG_ON(!trans); |
| 7211 | ret = btrfs_commit_transaction(trans, root); |
| 7212 | BUG_ON(ret); |
| 7213 | } |
| 7214 | |
| 7215 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 7216 | location.offset = (u64)-1; |
| 7217 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 7218 | |
| 7219 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 7220 | BUG_ON(!reloc_root); |
| 7221 | btrfs_orphan_cleanup(reloc_root); |
| 7222 | return 0; |
| 7223 | } |
| 7224 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7225 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7226 | struct btrfs_root *root) |
| 7227 | { |
| 7228 | struct btrfs_root *reloc_root; |
| 7229 | struct extent_buffer *eb; |
| 7230 | struct btrfs_root_item *root_item; |
| 7231 | struct btrfs_key root_key; |
| 7232 | int ret; |
| 7233 | |
| 7234 | BUG_ON(!root->ref_cows); |
| 7235 | if (root->reloc_root) |
| 7236 | return 0; |
| 7237 | |
| 7238 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 7239 | BUG_ON(!root_item); |
| 7240 | |
| 7241 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 7242 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 7243 | BUG_ON(ret); |
| 7244 | |
| 7245 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 7246 | root_key.offset = root->root_key.objectid; |
| 7247 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7248 | |
| 7249 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 7250 | btrfs_set_root_refs(root_item, 0); |
| 7251 | btrfs_set_root_bytenr(root_item, eb->start); |
| 7252 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7253 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7254 | |
| 7255 | btrfs_tree_unlock(eb); |
| 7256 | free_extent_buffer(eb); |
| 7257 | |
| 7258 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 7259 | &root_key, root_item); |
| 7260 | BUG_ON(ret); |
| 7261 | kfree(root_item); |
| 7262 | |
| 7263 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 7264 | &root_key); |
| 7265 | BUG_ON(!reloc_root); |
| 7266 | reloc_root->last_trans = trans->transid; |
| 7267 | reloc_root->commit_root = NULL; |
| 7268 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 7269 | |
| 7270 | root->reloc_root = reloc_root; |
| 7271 | return 0; |
| 7272 | } |
| 7273 | |
| 7274 | /* |
| 7275 | * Core function of space balance. |
| 7276 | * |
| 7277 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7278 | * counted roots. There is one reloc tree for each subvol, and all |
| 7279 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 7280 | * of the latest committed roots of subvols (root->commit_root). |
| 7281 | * |
| 7282 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 7283 | * COW the block through subvol's reloc tree, then update block pointer |
| 7284 | * in the subvol to point to the new block. Since all reloc trees share |
| 7285 | * same root key objectid, doing special handing for tree blocks owned |
| 7286 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 7287 | * we can use the resulting new block directly when the same block is |
| 7288 | * required to COW again through other reloc trees. By this way, relocated |
| 7289 | * tree blocks are shared between reloc trees, so they are also shared |
| 7290 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7291 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7292 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7293 | struct btrfs_root *root, |
| 7294 | struct btrfs_path *path, |
| 7295 | struct btrfs_key *first_key, |
| 7296 | struct btrfs_ref_path *ref_path, |
| 7297 | struct btrfs_block_group_cache *group, |
| 7298 | struct inode *reloc_inode) |
| 7299 | { |
| 7300 | struct btrfs_root *reloc_root; |
| 7301 | struct extent_buffer *eb = NULL; |
| 7302 | struct btrfs_key *keys; |
| 7303 | u64 *nodes; |
| 7304 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7305 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7306 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7307 | int ret; |
| 7308 | |
| 7309 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 7310 | lowest_level = ref_path->owner_objectid; |
| 7311 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7312 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7313 | path->lowest_level = lowest_level; |
| 7314 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 7315 | BUG_ON(ret < 0); |
| 7316 | path->lowest_level = 0; |
| 7317 | btrfs_release_path(root, path); |
| 7318 | return 0; |
| 7319 | } |
| 7320 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7321 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7322 | ret = init_reloc_tree(trans, root); |
| 7323 | BUG_ON(ret); |
| 7324 | reloc_root = root->reloc_root; |
| 7325 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7326 | shared_level = ref_path->shared_level; |
| 7327 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7328 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7329 | keys = ref_path->node_keys; |
| 7330 | nodes = ref_path->new_nodes; |
| 7331 | memset(&keys[shared_level + 1], 0, |
| 7332 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 7333 | memset(&nodes[shared_level + 1], 0, |
| 7334 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7335 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7336 | if (nodes[lowest_level] == 0) { |
| 7337 | path->lowest_level = lowest_level; |
| 7338 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7339 | 0, 1); |
| 7340 | BUG_ON(ret); |
| 7341 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 7342 | eb = path->nodes[level]; |
| 7343 | if (!eb || eb == reloc_root->node) |
| 7344 | break; |
| 7345 | nodes[level] = eb->start; |
| 7346 | if (level == 0) |
| 7347 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 7348 | else |
| 7349 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 7350 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7351 | if (nodes[0] && |
| 7352 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7353 | eb = path->nodes[0]; |
| 7354 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 7355 | group, reloc_inode); |
| 7356 | BUG_ON(ret); |
| 7357 | } |
| 7358 | btrfs_release_path(reloc_root, path); |
| 7359 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7360 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7361 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7362 | BUG_ON(ret); |
| 7363 | } |
| 7364 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7365 | /* |
| 7366 | * replace tree blocks in the fs tree with tree blocks in |
| 7367 | * the reloc tree. |
| 7368 | */ |
| 7369 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 7370 | BUG_ON(ret < 0); |
| 7371 | |
| 7372 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7373 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7374 | 0, 0); |
| 7375 | BUG_ON(ret); |
| 7376 | extent_buffer_get(path->nodes[0]); |
| 7377 | eb = path->nodes[0]; |
| 7378 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7379 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 7380 | BUG_ON(ret); |
| 7381 | free_extent_buffer(eb); |
| 7382 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7383 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7384 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7385 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7386 | return 0; |
| 7387 | } |
| 7388 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7389 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7390 | struct btrfs_root *root, |
| 7391 | struct btrfs_path *path, |
| 7392 | struct btrfs_key *first_key, |
| 7393 | struct btrfs_ref_path *ref_path) |
| 7394 | { |
| 7395 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7396 | |
| 7397 | ret = relocate_one_path(trans, root, path, first_key, |
| 7398 | ref_path, NULL, NULL); |
| 7399 | BUG_ON(ret); |
| 7400 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7401 | return 0; |
| 7402 | } |
| 7403 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7404 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7405 | struct btrfs_root *extent_root, |
| 7406 | struct btrfs_path *path, |
| 7407 | struct btrfs_key *extent_key) |
| 7408 | { |
| 7409 | int ret; |
| 7410 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7411 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 7412 | if (ret) |
| 7413 | goto out; |
| 7414 | ret = btrfs_del_item(trans, extent_root, path); |
| 7415 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7416 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7417 | return ret; |
| 7418 | } |
| 7419 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7420 | 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] | 7421 | struct btrfs_ref_path *ref_path) |
| 7422 | { |
| 7423 | struct btrfs_key root_key; |
| 7424 | |
| 7425 | root_key.objectid = ref_path->root_objectid; |
| 7426 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7427 | if (is_cowonly_root(ref_path->root_objectid)) |
| 7428 | root_key.offset = 0; |
| 7429 | else |
| 7430 | root_key.offset = (u64)-1; |
| 7431 | |
| 7432 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 7433 | } |
| 7434 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7435 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7436 | struct btrfs_path *path, |
| 7437 | struct btrfs_key *extent_key, |
| 7438 | struct btrfs_block_group_cache *group, |
| 7439 | struct inode *reloc_inode, int pass) |
| 7440 | { |
| 7441 | struct btrfs_trans_handle *trans; |
| 7442 | struct btrfs_root *found_root; |
| 7443 | struct btrfs_ref_path *ref_path = NULL; |
| 7444 | struct disk_extent *new_extents = NULL; |
| 7445 | int nr_extents = 0; |
| 7446 | int loops; |
| 7447 | int ret; |
| 7448 | int level; |
| 7449 | struct btrfs_key first_key; |
| 7450 | u64 prev_block = 0; |
| 7451 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7452 | |
| 7453 | trans = btrfs_start_transaction(extent_root, 1); |
| 7454 | BUG_ON(!trans); |
| 7455 | |
| 7456 | if (extent_key->objectid == 0) { |
| 7457 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 7458 | goto out; |
| 7459 | } |
| 7460 | |
| 7461 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 7462 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7463 | ret = -ENOMEM; |
| 7464 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7465 | } |
| 7466 | |
| 7467 | for (loops = 0; ; loops++) { |
| 7468 | if (loops == 0) { |
| 7469 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 7470 | extent_key->objectid); |
| 7471 | } else { |
| 7472 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 7473 | } |
| 7474 | if (ret < 0) |
| 7475 | goto out; |
| 7476 | if (ret > 0) |
| 7477 | break; |
| 7478 | |
| 7479 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 7480 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 7481 | continue; |
| 7482 | |
| 7483 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 7484 | BUG_ON(!found_root); |
| 7485 | /* |
| 7486 | * for reference counted tree, only process reference paths |
| 7487 | * rooted at the latest committed root. |
| 7488 | */ |
| 7489 | if (found_root->ref_cows && |
| 7490 | ref_path->root_generation != found_root->root_key.offset) |
| 7491 | continue; |
| 7492 | |
| 7493 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7494 | if (pass == 0) { |
| 7495 | /* |
| 7496 | * copy data extents to new locations |
| 7497 | */ |
| 7498 | u64 group_start = group->key.objectid; |
| 7499 | ret = relocate_data_extent(reloc_inode, |
| 7500 | extent_key, |
| 7501 | group_start); |
| 7502 | if (ret < 0) |
| 7503 | goto out; |
| 7504 | break; |
| 7505 | } |
| 7506 | level = 0; |
| 7507 | } else { |
| 7508 | level = ref_path->owner_objectid; |
| 7509 | } |
| 7510 | |
| 7511 | if (prev_block != ref_path->nodes[level]) { |
| 7512 | struct extent_buffer *eb; |
| 7513 | u64 block_start = ref_path->nodes[level]; |
| 7514 | u64 block_size = btrfs_level_size(found_root, level); |
| 7515 | |
| 7516 | eb = read_tree_block(found_root, block_start, |
| 7517 | block_size, 0); |
| 7518 | btrfs_tree_lock(eb); |
| 7519 | BUG_ON(level != btrfs_header_level(eb)); |
| 7520 | |
| 7521 | if (level == 0) |
| 7522 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 7523 | else |
| 7524 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 7525 | |
| 7526 | btrfs_tree_unlock(eb); |
| 7527 | free_extent_buffer(eb); |
| 7528 | prev_block = block_start; |
| 7529 | } |
| 7530 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7531 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7532 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 7533 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7534 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 7535 | /* |
| 7536 | * try to update data extent references while |
| 7537 | * keeping metadata shared between snapshots. |
| 7538 | */ |
| 7539 | if (pass == 1) { |
| 7540 | ret = relocate_one_path(trans, found_root, |
| 7541 | path, &first_key, ref_path, |
| 7542 | group, reloc_inode); |
| 7543 | if (ret < 0) |
| 7544 | goto out; |
| 7545 | continue; |
| 7546 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7547 | /* |
| 7548 | * use fallback method to process the remaining |
| 7549 | * references. |
| 7550 | */ |
| 7551 | if (!new_extents) { |
| 7552 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7553 | new_extents = kmalloc(sizeof(*new_extents), |
| 7554 | GFP_NOFS); |
| 7555 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7556 | ret = get_new_locations(reloc_inode, |
| 7557 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7558 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7559 | &new_extents, |
| 7560 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7561 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7562 | goto out; |
| 7563 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7564 | ret = replace_one_extent(trans, found_root, |
| 7565 | path, extent_key, |
| 7566 | &first_key, ref_path, |
| 7567 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 7568 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7569 | ret = relocate_tree_block(trans, found_root, path, |
| 7570 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7571 | } |
| 7572 | if (ret < 0) |
| 7573 | goto out; |
| 7574 | } |
| 7575 | ret = 0; |
| 7576 | out: |
| 7577 | btrfs_end_transaction(trans, extent_root); |
| 7578 | kfree(new_extents); |
| 7579 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7580 | return ret; |
| 7581 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7582 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7583 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7584 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 7585 | { |
| 7586 | u64 num_devices; |
| 7587 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 7588 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 7589 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7590 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7591 | if (num_devices == 1) { |
| 7592 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7593 | stripped = flags & ~stripped; |
| 7594 | |
| 7595 | /* turn raid0 into single device chunks */ |
| 7596 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 7597 | return stripped; |
| 7598 | |
| 7599 | /* turn mirroring into duplication */ |
| 7600 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 7601 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7602 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 7603 | return flags; |
| 7604 | } else { |
| 7605 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 7606 | if (flags & stripped) |
| 7607 | return flags; |
| 7608 | |
| 7609 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 7610 | stripped = flags & ~stripped; |
| 7611 | |
| 7612 | /* switch duplicated blocks with raid1 */ |
| 7613 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 7614 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 7615 | |
| 7616 | /* turn single device chunks into raid0 */ |
| 7617 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 7618 | } |
| 7619 | return flags; |
| 7620 | } |
| 7621 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7622 | static int set_block_group_ro(struct btrfs_block_group_cache *cache) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7623 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7624 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7625 | u64 num_bytes; |
| 7626 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7627 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7628 | if (cache->ro) |
| 7629 | return 0; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7630 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7631 | spin_lock(&sinfo->lock); |
| 7632 | spin_lock(&cache->lock); |
| 7633 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7634 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7635 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7636 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
| 7637 | sinfo->bytes_may_use + sinfo->bytes_readonly + |
| 7638 | cache->reserved_pinned + num_bytes < sinfo->total_bytes) { |
| 7639 | sinfo->bytes_readonly += num_bytes; |
| 7640 | sinfo->bytes_reserved += cache->reserved_pinned; |
| 7641 | cache->reserved_pinned = 0; |
| 7642 | cache->ro = 1; |
| 7643 | ret = 0; |
| 7644 | } |
| 7645 | spin_unlock(&cache->lock); |
| 7646 | spin_unlock(&sinfo->lock); |
| 7647 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7648 | } |
| 7649 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7650 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 7651 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7652 | |
| 7653 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7654 | struct btrfs_trans_handle *trans; |
| 7655 | u64 alloc_flags; |
| 7656 | int ret; |
| 7657 | |
| 7658 | BUG_ON(cache->ro); |
| 7659 | |
| 7660 | trans = btrfs_join_transaction(root, 1); |
| 7661 | BUG_ON(IS_ERR(trans)); |
| 7662 | |
| 7663 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 7664 | if (alloc_flags != cache->flags) |
| 7665 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 7666 | |
| 7667 | ret = set_block_group_ro(cache); |
| 7668 | if (!ret) |
| 7669 | goto out; |
| 7670 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
| 7671 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1); |
| 7672 | if (ret < 0) |
| 7673 | goto out; |
| 7674 | ret = set_block_group_ro(cache); |
| 7675 | out: |
| 7676 | btrfs_end_transaction(trans, root); |
| 7677 | return ret; |
| 7678 | } |
| 7679 | |
| 7680 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 7681 | struct btrfs_block_group_cache *cache) |
| 7682 | { |
| 7683 | struct btrfs_space_info *sinfo = cache->space_info; |
| 7684 | u64 num_bytes; |
| 7685 | |
| 7686 | BUG_ON(!cache->ro); |
| 7687 | |
| 7688 | spin_lock(&sinfo->lock); |
| 7689 | spin_lock(&cache->lock); |
| 7690 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 7691 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 7692 | sinfo->bytes_readonly -= num_bytes; |
| 7693 | cache->ro = 0; |
| 7694 | spin_unlock(&cache->lock); |
| 7695 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7696 | return 0; |
| 7697 | } |
| 7698 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7699 | /* |
| 7700 | * checks to see if its even possible to relocate this block group. |
| 7701 | * |
| 7702 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 7703 | * ok to go ahead and try. |
| 7704 | */ |
| 7705 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7706 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7707 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7708 | struct btrfs_space_info *space_info; |
| 7709 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 7710 | struct btrfs_device *device; |
| 7711 | int full = 0; |
| 7712 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7713 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7714 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7715 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7716 | /* odd, couldn't find the block group, leave it alone */ |
| 7717 | if (!block_group) |
| 7718 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7719 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7720 | /* no bytes used, we're good */ |
| 7721 | if (!btrfs_block_group_used(&block_group->item)) |
| 7722 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7723 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7724 | space_info = block_group->space_info; |
| 7725 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 7726 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7727 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7728 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7729 | /* |
| 7730 | * 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] | 7731 | * relocate it unless we're able to allocate a new chunk below. |
| 7732 | * |
| 7733 | * Otherwise, we need to make sure we have room in the space to handle |
| 7734 | * 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] | 7735 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7736 | if ((space_info->total_bytes != block_group->key.offset) && |
| 7737 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7738 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 7739 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 7740 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7741 | spin_unlock(&space_info->lock); |
| 7742 | goto out; |
| 7743 | } |
| 7744 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7745 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7746 | /* |
| 7747 | * ok we don't have enough space, but maybe we have free space on our |
| 7748 | * devices to allocate new chunks for relocation, so loop through our |
| 7749 | * alloc devices and guess if we have enough space. However, if we |
| 7750 | * were marked as full, then we know there aren't enough chunks, and we |
| 7751 | * can just return. |
| 7752 | */ |
| 7753 | ret = -1; |
| 7754 | if (full) |
| 7755 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 7756 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7757 | mutex_lock(&root->fs_info->chunk_mutex); |
| 7758 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 7759 | u64 min_free = btrfs_block_group_used(&block_group->item); |
| 7760 | u64 dev_offset, max_avail; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 7761 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7762 | /* |
| 7763 | * check to make sure we can actually find a chunk with enough |
| 7764 | * space to fit our block group in. |
| 7765 | */ |
| 7766 | if (device->total_bytes > device->bytes_used + min_free) { |
| 7767 | ret = find_free_dev_extent(NULL, device, min_free, |
| 7768 | &dev_offset, &max_avail); |
| 7769 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7770 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7771 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7772 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7773 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7774 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7775 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7776 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7777 | return ret; |
| 7778 | } |
| 7779 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7780 | static int find_first_block_group(struct btrfs_root *root, |
| 7781 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7782 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7783 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7784 | struct btrfs_key found_key; |
| 7785 | struct extent_buffer *leaf; |
| 7786 | int slot; |
| 7787 | |
| 7788 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7789 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7790 | goto out; |
| 7791 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7792 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7793 | slot = path->slots[0]; |
| 7794 | leaf = path->nodes[0]; |
| 7795 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7796 | ret = btrfs_next_leaf(root, path); |
| 7797 | if (ret == 0) |
| 7798 | continue; |
| 7799 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7800 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7801 | break; |
| 7802 | } |
| 7803 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7804 | |
| 7805 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7806 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 7807 | ret = 0; |
| 7808 | goto out; |
| 7809 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7810 | path->slots[0]++; |
| 7811 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7812 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7813 | return ret; |
| 7814 | } |
| 7815 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7816 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7817 | { |
| 7818 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7819 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7820 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7821 | struct rb_node *n; |
| 7822 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7823 | down_write(&info->extent_commit_sem); |
| 7824 | while (!list_empty(&info->caching_block_groups)) { |
| 7825 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7826 | struct btrfs_caching_control, list); |
| 7827 | list_del(&caching_ctl->list); |
| 7828 | put_caching_control(caching_ctl); |
| 7829 | } |
| 7830 | up_write(&info->extent_commit_sem); |
| 7831 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7832 | spin_lock(&info->block_group_cache_lock); |
| 7833 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7834 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7835 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7836 | rb_erase(&block_group->cache_node, |
| 7837 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7838 | spin_unlock(&info->block_group_cache_lock); |
| 7839 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7840 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7841 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7842 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7843 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7844 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7845 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7846 | |
| 7847 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 7848 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7849 | |
| 7850 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7851 | } |
| 7852 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7853 | |
| 7854 | /* now that all the block groups are freed, go through and |
| 7855 | * free all the space_info structs. This is only called during |
| 7856 | * the final stages of unmount, and so we know nobody is |
| 7857 | * using them. We call synchronize_rcu() once before we start, |
| 7858 | * just to be on the safe side. |
| 7859 | */ |
| 7860 | synchronize_rcu(); |
| 7861 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 7862 | release_global_block_rsv(info); |
| 7863 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7864 | while(!list_empty(&info->space_info)) { |
| 7865 | space_info = list_entry(info->space_info.next, |
| 7866 | struct btrfs_space_info, |
| 7867 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7868 | if (space_info->bytes_pinned > 0 || |
| 7869 | space_info->bytes_reserved > 0) { |
| 7870 | WARN_ON(1); |
| 7871 | dump_space_info(space_info, 0, 0); |
| 7872 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7873 | list_del(&space_info->list); |
| 7874 | kfree(space_info); |
| 7875 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7876 | return 0; |
| 7877 | } |
| 7878 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7879 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 7880 | struct btrfs_block_group_cache *cache) |
| 7881 | { |
| 7882 | int index = get_block_group_index(cache); |
| 7883 | |
| 7884 | down_write(&space_info->groups_sem); |
| 7885 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 7886 | up_write(&space_info->groups_sem); |
| 7887 | } |
| 7888 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7889 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7890 | { |
| 7891 | struct btrfs_path *path; |
| 7892 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7893 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7894 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7895 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7896 | struct btrfs_key key; |
| 7897 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7898 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7899 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7900 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7901 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7902 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7903 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7904 | path = btrfs_alloc_path(); |
| 7905 | if (!path) |
| 7906 | return -ENOMEM; |
| 7907 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7908 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7909 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7910 | if (ret > 0) |
| 7911 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7912 | if (ret != 0) |
| 7913 | goto error; |
| 7914 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7915 | leaf = path->nodes[0]; |
| 7916 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7917 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7918 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7919 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7920 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7921 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7922 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7923 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7924 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7925 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7926 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7927 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7928 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7929 | |
| 7930 | /* |
| 7931 | * we only want to have 32k of ram per block group for keeping |
| 7932 | * track of free space, and if we pass 1/2 of that we want to |
| 7933 | * start converting things over to using bitmaps |
| 7934 | */ |
| 7935 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7936 | sizeof(struct btrfs_free_space); |
| 7937 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7938 | read_extent_buffer(leaf, &cache->item, |
| 7939 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7940 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7941 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7942 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7943 | key.objectid = found_key.objectid + found_key.offset; |
| 7944 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7945 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7946 | cache->sectorsize = root->sectorsize; |
| 7947 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7948 | /* |
| 7949 | * check for two cases, either we are full, and therefore |
| 7950 | * don't need to bother with the caching work since we won't |
| 7951 | * find any space, or we are empty, and we can just add all |
| 7952 | * the space in and be done with it. This saves us _alot_ of |
| 7953 | * time, particularly in the full case. |
| 7954 | */ |
| 7955 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7956 | exclude_super_stripes(root, cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7957 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7958 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7959 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7960 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7961 | exclude_super_stripes(root, cache); |
| 7962 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7963 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7964 | add_new_free_space(cache, root->fs_info, |
| 7965 | found_key.objectid, |
| 7966 | found_key.objectid + |
| 7967 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7968 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7969 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7970 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7971 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7972 | btrfs_block_group_used(&cache->item), |
| 7973 | &space_info); |
| 7974 | BUG_ON(ret); |
| 7975 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7976 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7977 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7978 | spin_unlock(&cache->space_info->lock); |
| 7979 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7980 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7981 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7982 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7983 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7984 | |
| 7985 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7986 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7987 | set_block_group_ro(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7988 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7989 | |
| 7990 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 7991 | if (!(get_alloc_profile(root, space_info->flags) & |
| 7992 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 7993 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7994 | BTRFS_BLOCK_GROUP_DUP))) |
| 7995 | continue; |
| 7996 | /* |
| 7997 | * avoid allocating from un-mirrored block group if there are |
| 7998 | * mirrored block groups. |
| 7999 | */ |
| 8000 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8001 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8002 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8003 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8004 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8005 | |
| 8006 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8007 | ret = 0; |
| 8008 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8009 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8010 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8011 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8012 | |
| 8013 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 8014 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8015 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8016 | u64 size) |
| 8017 | { |
| 8018 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8019 | struct btrfs_root *extent_root; |
| 8020 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8021 | |
| 8022 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8023 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8024 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8025 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8026 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8027 | if (!cache) |
| 8028 | return -ENOMEM; |
| 8029 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8030 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8031 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8032 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8033 | cache->sectorsize = root->sectorsize; |
| 8034 | |
| 8035 | /* |
| 8036 | * we only want to have 32k of ram per block group for keeping track |
| 8037 | * of free space, and if we pass 1/2 of that we want to start |
| 8038 | * converting things over to using bitmaps |
| 8039 | */ |
| 8040 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 8041 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8042 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8043 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 8044 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8045 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8046 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8047 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8048 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8049 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 8050 | cache->flags = type; |
| 8051 | btrfs_set_block_group_flags(&cache->item, type); |
| 8052 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8053 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8054 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8055 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8056 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8057 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 8058 | chunk_offset + size); |
| 8059 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8060 | free_excluded_extents(root, cache); |
| 8061 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8062 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 8063 | &cache->space_info); |
| 8064 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8065 | |
| 8066 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8067 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8068 | spin_unlock(&cache->space_info->lock); |
| 8069 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8070 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8071 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8072 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8073 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8074 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8075 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 8076 | sizeof(cache->item)); |
| 8077 | BUG_ON(ret); |
| 8078 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 8079 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8080 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8081 | return 0; |
| 8082 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8083 | |
| 8084 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 8085 | struct btrfs_root *root, u64 group_start) |
| 8086 | { |
| 8087 | struct btrfs_path *path; |
| 8088 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8089 | struct btrfs_free_cluster *cluster; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8090 | struct btrfs_key key; |
| 8091 | int ret; |
| 8092 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8093 | root = root->fs_info->extent_root; |
| 8094 | |
| 8095 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 8096 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8097 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8098 | |
| 8099 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8100 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8101 | /* make sure this block group isn't part of an allocation cluster */ |
| 8102 | cluster = &root->fs_info->data_alloc_cluster; |
| 8103 | spin_lock(&cluster->refill_lock); |
| 8104 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8105 | spin_unlock(&cluster->refill_lock); |
| 8106 | |
| 8107 | /* |
| 8108 | * make sure this block group isn't part of a metadata |
| 8109 | * allocation cluster |
| 8110 | */ |
| 8111 | cluster = &root->fs_info->meta_alloc_cluster; |
| 8112 | spin_lock(&cluster->refill_lock); |
| 8113 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8114 | spin_unlock(&cluster->refill_lock); |
| 8115 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8116 | path = btrfs_alloc_path(); |
| 8117 | BUG_ON(!path); |
| 8118 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8119 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8120 | rb_erase(&block_group->cache_node, |
| 8121 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8122 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8123 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8124 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8125 | /* |
| 8126 | * we must use list_del_init so people can check to see if they |
| 8127 | * are still on the list after taking the semaphore |
| 8128 | */ |
| 8129 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8130 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8131 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8132 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8133 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8134 | |
| 8135 | btrfs_remove_free_space_cache(block_group); |
| 8136 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8137 | spin_lock(&block_group->space_info->lock); |
| 8138 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 8139 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 8140 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8141 | |
| 8142 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8143 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8144 | btrfs_put_block_group(block_group); |
| 8145 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8146 | |
| 8147 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 8148 | if (ret > 0) |
| 8149 | ret = -EIO; |
| 8150 | if (ret < 0) |
| 8151 | goto out; |
| 8152 | |
| 8153 | ret = btrfs_del_item(trans, root, path); |
| 8154 | out: |
| 8155 | btrfs_free_path(path); |
| 8156 | return ret; |
| 8157 | } |