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> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 24 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 25 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 26 | #include "ctree.h" |
| 27 | #include "disk-io.h" |
| 28 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 29 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 30 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 31 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 32 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 33 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 34 | static int update_reserved_extents(struct btrfs_root *root, |
| 35 | u64 bytenr, u64 num, int reserve); |
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, |
| 38 | u64 bytenr, u64 num_bytes, int alloc, |
| 39 | int mark_free); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 40 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 41 | struct btrfs_root *root, |
| 42 | u64 bytenr, u64 num_bytes, u64 parent, |
| 43 | u64 root_objectid, u64 owner_objectid, |
| 44 | u64 owner_offset, int refs_to_drop, |
| 45 | struct btrfs_delayed_extent_op *extra_op); |
| 46 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 47 | struct extent_buffer *leaf, |
| 48 | struct btrfs_extent_item *ei); |
| 49 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 50 | struct btrfs_root *root, |
| 51 | u64 parent, u64 root_objectid, |
| 52 | u64 flags, u64 owner, u64 offset, |
| 53 | struct btrfs_key *ins, int ref_mod); |
| 54 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 55 | struct btrfs_root *root, |
| 56 | u64 parent, u64 root_objectid, |
| 57 | u64 flags, struct btrfs_disk_key *key, |
| 58 | int level, struct btrfs_key *ins); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 59 | |
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); |
| 63 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 64 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 65 | { |
| 66 | return (cache->flags & bits) == bits; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * this adds the block group to the fs_info rb tree for the block group |
| 71 | * cache |
| 72 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 73 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 74 | struct btrfs_block_group_cache *block_group) |
| 75 | { |
| 76 | struct rb_node **p; |
| 77 | struct rb_node *parent = NULL; |
| 78 | struct btrfs_block_group_cache *cache; |
| 79 | |
| 80 | spin_lock(&info->block_group_cache_lock); |
| 81 | p = &info->block_group_cache_tree.rb_node; |
| 82 | |
| 83 | while (*p) { |
| 84 | parent = *p; |
| 85 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 86 | cache_node); |
| 87 | if (block_group->key.objectid < cache->key.objectid) { |
| 88 | p = &(*p)->rb_left; |
| 89 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 90 | p = &(*p)->rb_right; |
| 91 | } else { |
| 92 | spin_unlock(&info->block_group_cache_lock); |
| 93 | return -EEXIST; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | rb_link_node(&block_group->cache_node, parent, p); |
| 98 | rb_insert_color(&block_group->cache_node, |
| 99 | &info->block_group_cache_tree); |
| 100 | spin_unlock(&info->block_group_cache_lock); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * This will return the block group at or after bytenr if contains is 0, else |
| 107 | * it will return the block group that contains the bytenr |
| 108 | */ |
| 109 | static struct btrfs_block_group_cache * |
| 110 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 111 | int contains) |
| 112 | { |
| 113 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 114 | struct rb_node *n; |
| 115 | u64 end, start; |
| 116 | |
| 117 | spin_lock(&info->block_group_cache_lock); |
| 118 | n = info->block_group_cache_tree.rb_node; |
| 119 | |
| 120 | while (n) { |
| 121 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 122 | cache_node); |
| 123 | end = cache->key.objectid + cache->key.offset - 1; |
| 124 | start = cache->key.objectid; |
| 125 | |
| 126 | if (bytenr < start) { |
| 127 | if (!contains && (!ret || start < ret->key.objectid)) |
| 128 | ret = cache; |
| 129 | n = n->rb_left; |
| 130 | } else if (bytenr > start) { |
| 131 | if (contains && bytenr <= end) { |
| 132 | ret = cache; |
| 133 | break; |
| 134 | } |
| 135 | n = n->rb_right; |
| 136 | } else { |
| 137 | ret = cache; |
| 138 | break; |
| 139 | } |
| 140 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 141 | if (ret) |
| 142 | atomic_inc(&ret->count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 143 | spin_unlock(&info->block_group_cache_lock); |
| 144 | |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * this is only called by cache_block_group, since we could have freed extents |
| 150 | * we need to check the pinned_extents for any extents that can't be used yet |
| 151 | * since their free space will be released as soon as the transaction commits. |
| 152 | */ |
| 153 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 154 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 155 | { |
| 156 | u64 extent_start, extent_end, size; |
| 157 | int ret; |
| 158 | |
| 159 | while (start < end) { |
| 160 | ret = find_first_extent_bit(&info->pinned_extents, start, |
| 161 | &extent_start, &extent_end, |
| 162 | EXTENT_DIRTY); |
| 163 | if (ret) |
| 164 | break; |
| 165 | |
| 166 | if (extent_start == start) { |
| 167 | start = extent_end + 1; |
| 168 | } else if (extent_start > start && extent_start < end) { |
| 169 | size = extent_start - start; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 170 | ret = btrfs_add_free_space(block_group, start, |
| 171 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 172 | BUG_ON(ret); |
| 173 | start = extent_end + 1; |
| 174 | } else { |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (start < end) { |
| 180 | size = end - start; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 181 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 182 | BUG_ON(ret); |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 188 | static int remove_sb_from_cache(struct btrfs_root *root, |
| 189 | struct btrfs_block_group_cache *cache) |
| 190 | { |
| 191 | u64 bytenr; |
| 192 | u64 *logical; |
| 193 | int stripe_len; |
| 194 | int i, nr, ret; |
| 195 | |
| 196 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 197 | bytenr = btrfs_sb_offset(i); |
| 198 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 199 | cache->key.objectid, bytenr, 0, |
| 200 | &logical, &nr, &stripe_len); |
| 201 | BUG_ON(ret); |
| 202 | while (nr--) { |
| 203 | btrfs_remove_free_space(cache, logical[nr], |
| 204 | stripe_len); |
| 205 | } |
| 206 | kfree(logical); |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 211 | static int cache_block_group(struct btrfs_root *root, |
| 212 | struct btrfs_block_group_cache *block_group) |
| 213 | { |
| 214 | struct btrfs_path *path; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 215 | int ret = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 216 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 217 | struct extent_buffer *leaf; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 218 | int slot; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 219 | u64 last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 220 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 221 | if (!block_group) |
| 222 | return 0; |
| 223 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 224 | root = root->fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 225 | |
| 226 | if (block_group->cached) |
| 227 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 228 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 229 | path = btrfs_alloc_path(); |
| 230 | if (!path) |
| 231 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 232 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 233 | path->reada = 2; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 234 | /* |
| 235 | * we get into deadlocks with paths held by callers of this function. |
| 236 | * since the alloc_mutex is protecting things right now, just |
| 237 | * skip the locking here |
| 238 | */ |
| 239 | path->skip_locking = 1; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 240 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
| 241 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 242 | key.offset = 0; |
| 243 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 244 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 245 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 246 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 247 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 248 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 249 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 250 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 251 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 252 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 253 | if (ret < 0) |
| 254 | goto err; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 255 | if (ret == 0) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 256 | continue; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 257 | else |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 258 | break; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 259 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 260 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 261 | if (key.objectid < block_group->key.objectid) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 262 | goto next; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 263 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 264 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 265 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 266 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 267 | |
| 268 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 269 | add_new_free_space(block_group, root->fs_info, last, |
| 270 | key.objectid); |
| 271 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 272 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 273 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 274 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 275 | path->slots[0]++; |
| 276 | } |
| 277 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 278 | add_new_free_space(block_group, root->fs_info, last, |
| 279 | block_group->key.objectid + |
| 280 | block_group->key.offset); |
| 281 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 282 | block_group->cached = 1; |
Josef Bacik | 70cb074 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 283 | remove_sb_from_cache(root, block_group); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 284 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 285 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 286 | btrfs_free_path(path); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 287 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 290 | /* |
| 291 | * return the block group that starts at or after bytenr |
| 292 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 293 | static struct btrfs_block_group_cache * |
| 294 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 295 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 296 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 297 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 298 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 299 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 300 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 303 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 304 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 305 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 306 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 307 | struct btrfs_fs_info *info, |
| 308 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 309 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 310 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 311 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 312 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 313 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 314 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 315 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 316 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 317 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 318 | { |
| 319 | if (atomic_dec_and_test(&cache->count)) |
| 320 | kfree(cache); |
| 321 | } |
| 322 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 323 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 324 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 325 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 326 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 327 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 328 | |
| 329 | rcu_read_lock(); |
| 330 | list_for_each_entry_rcu(found, head, list) { |
| 331 | if (found->flags == flags) { |
| 332 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 333 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 334 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 335 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 336 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 337 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 338 | } |
| 339 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 340 | /* |
| 341 | * after adding space to the filesystem, we need to clear the full flags |
| 342 | * on all the space infos. |
| 343 | */ |
| 344 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 345 | { |
| 346 | struct list_head *head = &info->space_info; |
| 347 | struct btrfs_space_info *found; |
| 348 | |
| 349 | rcu_read_lock(); |
| 350 | list_for_each_entry_rcu(found, head, list) |
| 351 | found->full = 0; |
| 352 | rcu_read_unlock(); |
| 353 | } |
| 354 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 355 | static u64 div_factor(u64 num, int factor) |
| 356 | { |
| 357 | if (factor == 10) |
| 358 | return num; |
| 359 | num *= factor; |
| 360 | do_div(num, 10); |
| 361 | return num; |
| 362 | } |
| 363 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 364 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 365 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 366 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 367 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 368 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 369 | u64 last = max(search_hint, search_start); |
| 370 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 371 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 372 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 373 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 374 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 375 | while (1) { |
| 376 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 377 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 378 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 379 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 380 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 381 | last = cache->key.objectid + cache->key.offset; |
| 382 | used = btrfs_block_group_used(&cache->item); |
| 383 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 384 | if ((full_search || !cache->ro) && |
| 385 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 386 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 387 | div_factor(cache->key.offset, factor)) { |
| 388 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 389 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 390 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 391 | goto found; |
| 392 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 393 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 394 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 395 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 396 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 397 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 398 | if (!wrapped) { |
| 399 | last = search_start; |
| 400 | wrapped = 1; |
| 401 | goto again; |
| 402 | } |
| 403 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 404 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 405 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 406 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 407 | goto again; |
| 408 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 409 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 410 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 411 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 412 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 413 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 414 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 415 | { |
| 416 | int ret; |
| 417 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 418 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 419 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 420 | path = btrfs_alloc_path(); |
| 421 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 422 | key.objectid = start; |
| 423 | key.offset = len; |
| 424 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 425 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 426 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 427 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 428 | return ret; |
| 429 | } |
| 430 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 431 | /* |
| 432 | * Back reference rules. Back refs have three main goals: |
| 433 | * |
| 434 | * 1) differentiate between all holders of references to an extent so that |
| 435 | * when a reference is dropped we can make sure it was a valid reference |
| 436 | * before freeing the extent. |
| 437 | * |
| 438 | * 2) Provide enough information to quickly find the holders of an extent |
| 439 | * if we notice a given block is corrupted or bad. |
| 440 | * |
| 441 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 442 | * maintenance. This is actually the same as #2, but with a slightly |
| 443 | * different use case. |
| 444 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 445 | * There are two kinds of back refs. The implicit back refs is optimized |
| 446 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 447 | * back refs of this kind provide information about the block's owner tree |
| 448 | * and the pointer's key. These information allow us to find the block by |
| 449 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 450 | * referenced by their owner trees. The location of tree block is recorded |
| 451 | * in the back refs. Actually the full back refs is generic, and can be |
| 452 | * used in all cases the implicit back refs is used. The major shortcoming |
| 453 | * of the full back refs is its overhead. Every time a tree block gets |
| 454 | * COWed, we have to update back refs entry for all pointers in it. |
| 455 | * |
| 456 | * For a newly allocated tree block, we use implicit back refs for |
| 457 | * pointers in it. This means most tree related operations only involve |
| 458 | * implicit back refs. For a tree block created in old transaction, the |
| 459 | * only way to drop a reference to it is COW it. So we can detect the |
| 460 | * event that tree block loses its owner tree's reference and do the |
| 461 | * back refs conversion. |
| 462 | * |
| 463 | * When a tree block is COW'd through a tree, there are four cases: |
| 464 | * |
| 465 | * The reference count of the block is one and the tree is the block's |
| 466 | * owner tree. Nothing to do in this case. |
| 467 | * |
| 468 | * The reference count of the block is one and the tree is not the |
| 469 | * block's owner tree. In this case, full back refs is used for pointers |
| 470 | * in the block. Remove these full back refs, add implicit back refs for |
| 471 | * every pointers in the new block. |
| 472 | * |
| 473 | * The reference count of the block is greater than one and the tree is |
| 474 | * the block's owner tree. In this case, implicit back refs is used for |
| 475 | * pointers in the block. Add full back refs for every pointers in the |
| 476 | * block, increase lower level extents' reference counts. The original |
| 477 | * implicit back refs are entailed to the new block. |
| 478 | * |
| 479 | * The reference count of the block is greater than one and the tree is |
| 480 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 481 | * the new block, increase lower level extents' reference count. |
| 482 | * |
| 483 | * Back Reference Key composing: |
| 484 | * |
| 485 | * The key objectid corresponds to the first byte in the extent, |
| 486 | * The key type is used to differentiate between types of back refs. |
| 487 | * There are different meanings of the key offset for different types |
| 488 | * of back refs. |
| 489 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 490 | * File extents can be referenced by: |
| 491 | * |
| 492 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 493 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 494 | * - different offsets inside a file (bookend extents in file.c) |
| 495 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 496 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 497 | * |
| 498 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 499 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 500 | * - original offset in the file |
| 501 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 502 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 503 | * The key offset for the implicit back refs is hash of the first |
| 504 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 505 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 506 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 507 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 508 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 509 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 510 | * The key offset for the implicit back refs is the first byte of |
| 511 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 512 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 513 | * When a file extent is allocated, The implicit back refs is used. |
| 514 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 515 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 516 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 517 | * |
| 518 | * When a file extent is removed file truncation, we find the |
| 519 | * corresponding implicit back refs and check the following fields: |
| 520 | * |
| 521 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 522 | * |
| 523 | * Btree extents can be referenced by: |
| 524 | * |
| 525 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 526 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 527 | * Both the implicit back refs and the full back refs for tree blocks |
| 528 | * only consist of key. The key offset for the implicit back refs is |
| 529 | * objectid of block's owner tree. The key offset for the full back refs |
| 530 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 531 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 532 | * When implicit back refs is used, information about the lowest key and |
| 533 | * level of the tree block are required. These information are stored in |
| 534 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 535 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 536 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 537 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 538 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 539 | struct btrfs_root *root, |
| 540 | struct btrfs_path *path, |
| 541 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 542 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 543 | struct btrfs_extent_item *item; |
| 544 | struct btrfs_extent_item_v0 *ei0; |
| 545 | struct btrfs_extent_ref_v0 *ref0; |
| 546 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 547 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 548 | struct btrfs_key key; |
| 549 | struct btrfs_key found_key; |
| 550 | u32 new_size = sizeof(*item); |
| 551 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 552 | int ret; |
| 553 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 554 | leaf = path->nodes[0]; |
| 555 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 556 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 557 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 558 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 559 | struct btrfs_extent_item_v0); |
| 560 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 561 | |
| 562 | if (owner == (u64)-1) { |
| 563 | while (1) { |
| 564 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 565 | ret = btrfs_next_leaf(root, path); |
| 566 | if (ret < 0) |
| 567 | return ret; |
| 568 | BUG_ON(ret > 0); |
| 569 | leaf = path->nodes[0]; |
| 570 | } |
| 571 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 572 | path->slots[0]); |
| 573 | BUG_ON(key.objectid != found_key.objectid); |
| 574 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 575 | path->slots[0]++; |
| 576 | continue; |
| 577 | } |
| 578 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 579 | struct btrfs_extent_ref_v0); |
| 580 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | btrfs_release_path(root, path); |
| 585 | |
| 586 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 587 | new_size += sizeof(*bi); |
| 588 | |
| 589 | new_size -= sizeof(*ei0); |
| 590 | ret = btrfs_search_slot(trans, root, &key, path, |
| 591 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 592 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 593 | return ret; |
| 594 | BUG_ON(ret); |
| 595 | |
| 596 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 597 | BUG_ON(ret); |
| 598 | |
| 599 | leaf = path->nodes[0]; |
| 600 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 601 | btrfs_set_extent_refs(leaf, item, refs); |
| 602 | /* FIXME: get real generation */ |
| 603 | btrfs_set_extent_generation(leaf, item, 0); |
| 604 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 605 | btrfs_set_extent_flags(leaf, item, |
| 606 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 607 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 608 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 609 | /* FIXME: get first key of the block */ |
| 610 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 611 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 612 | } else { |
| 613 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 614 | } |
| 615 | btrfs_mark_buffer_dirty(leaf); |
| 616 | return 0; |
| 617 | } |
| 618 | #endif |
| 619 | |
| 620 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 621 | { |
| 622 | u32 high_crc = ~(u32)0; |
| 623 | u32 low_crc = ~(u32)0; |
| 624 | __le64 lenum; |
| 625 | |
| 626 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 627 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 628 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 629 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 630 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 631 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 632 | |
| 633 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 634 | } |
| 635 | |
| 636 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 637 | struct btrfs_extent_data_ref *ref) |
| 638 | { |
| 639 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 640 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 641 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 642 | } |
| 643 | |
| 644 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 645 | struct btrfs_extent_data_ref *ref, |
| 646 | u64 root_objectid, u64 owner, u64 offset) |
| 647 | { |
| 648 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 649 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 650 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 651 | return 0; |
| 652 | return 1; |
| 653 | } |
| 654 | |
| 655 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 656 | struct btrfs_root *root, |
| 657 | struct btrfs_path *path, |
| 658 | u64 bytenr, u64 parent, |
| 659 | u64 root_objectid, |
| 660 | u64 owner, u64 offset) |
| 661 | { |
| 662 | struct btrfs_key key; |
| 663 | struct btrfs_extent_data_ref *ref; |
| 664 | struct extent_buffer *leaf; |
| 665 | u32 nritems; |
| 666 | int ret; |
| 667 | int recow; |
| 668 | int err = -ENOENT; |
| 669 | |
| 670 | key.objectid = bytenr; |
| 671 | if (parent) { |
| 672 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 673 | key.offset = parent; |
| 674 | } else { |
| 675 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 676 | key.offset = hash_extent_data_ref(root_objectid, |
| 677 | owner, offset); |
| 678 | } |
| 679 | again: |
| 680 | recow = 0; |
| 681 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 682 | if (ret < 0) { |
| 683 | err = ret; |
| 684 | goto fail; |
| 685 | } |
| 686 | |
| 687 | if (parent) { |
| 688 | if (!ret) |
| 689 | return 0; |
| 690 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 691 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 692 | btrfs_release_path(root, path); |
| 693 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 694 | if (ret < 0) { |
| 695 | err = ret; |
| 696 | goto fail; |
| 697 | } |
| 698 | if (!ret) |
| 699 | return 0; |
| 700 | #endif |
| 701 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 705 | nritems = btrfs_header_nritems(leaf); |
| 706 | while (1) { |
| 707 | if (path->slots[0] >= nritems) { |
| 708 | ret = btrfs_next_leaf(root, path); |
| 709 | if (ret < 0) |
| 710 | err = ret; |
| 711 | if (ret) |
| 712 | goto fail; |
| 713 | |
| 714 | leaf = path->nodes[0]; |
| 715 | nritems = btrfs_header_nritems(leaf); |
| 716 | recow = 1; |
| 717 | } |
| 718 | |
| 719 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 720 | if (key.objectid != bytenr || |
| 721 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 722 | goto fail; |
| 723 | |
| 724 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 725 | struct btrfs_extent_data_ref); |
| 726 | |
| 727 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 728 | owner, offset)) { |
| 729 | if (recow) { |
| 730 | btrfs_release_path(root, path); |
| 731 | goto again; |
| 732 | } |
| 733 | err = 0; |
| 734 | break; |
| 735 | } |
| 736 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 737 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 738 | fail: |
| 739 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 740 | } |
| 741 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 742 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 743 | struct btrfs_root *root, |
| 744 | struct btrfs_path *path, |
| 745 | u64 bytenr, u64 parent, |
| 746 | u64 root_objectid, u64 owner, |
| 747 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 748 | { |
| 749 | struct btrfs_key key; |
| 750 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 751 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 752 | u32 num_refs; |
| 753 | int ret; |
| 754 | |
| 755 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 756 | if (parent) { |
| 757 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 758 | key.offset = parent; |
| 759 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 760 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 761 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 762 | key.offset = hash_extent_data_ref(root_objectid, |
| 763 | owner, offset); |
| 764 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 765 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 766 | |
| 767 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 768 | if (ret && ret != -EEXIST) |
| 769 | goto fail; |
| 770 | |
| 771 | leaf = path->nodes[0]; |
| 772 | if (parent) { |
| 773 | struct btrfs_shared_data_ref *ref; |
| 774 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 775 | struct btrfs_shared_data_ref); |
| 776 | if (ret == 0) { |
| 777 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 778 | } else { |
| 779 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 780 | num_refs += refs_to_add; |
| 781 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 782 | } |
| 783 | } else { |
| 784 | struct btrfs_extent_data_ref *ref; |
| 785 | while (ret == -EEXIST) { |
| 786 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 787 | struct btrfs_extent_data_ref); |
| 788 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 789 | owner, offset)) |
| 790 | break; |
| 791 | btrfs_release_path(root, path); |
| 792 | key.offset++; |
| 793 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 794 | size); |
| 795 | if (ret && ret != -EEXIST) |
| 796 | goto fail; |
| 797 | |
| 798 | leaf = path->nodes[0]; |
| 799 | } |
| 800 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 801 | struct btrfs_extent_data_ref); |
| 802 | if (ret == 0) { |
| 803 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 804 | root_objectid); |
| 805 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 806 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 807 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 808 | } else { |
| 809 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 810 | num_refs += refs_to_add; |
| 811 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 812 | } |
| 813 | } |
| 814 | btrfs_mark_buffer_dirty(leaf); |
| 815 | ret = 0; |
| 816 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 817 | btrfs_release_path(root, path); |
| 818 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 819 | } |
| 820 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 821 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 822 | struct btrfs_root *root, |
| 823 | struct btrfs_path *path, |
| 824 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 825 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 826 | struct btrfs_key key; |
| 827 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 828 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 829 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 830 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 831 | int ret = 0; |
| 832 | |
| 833 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 834 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 835 | |
| 836 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 837 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 838 | struct btrfs_extent_data_ref); |
| 839 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 840 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 841 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 842 | struct btrfs_shared_data_ref); |
| 843 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 844 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 845 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 846 | struct btrfs_extent_ref_v0 *ref0; |
| 847 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 848 | struct btrfs_extent_ref_v0); |
| 849 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 850 | #endif |
| 851 | } else { |
| 852 | BUG(); |
| 853 | } |
| 854 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 855 | BUG_ON(num_refs < refs_to_drop); |
| 856 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 857 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 858 | if (num_refs == 0) { |
| 859 | ret = btrfs_del_item(trans, root, path); |
| 860 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 861 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 862 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 863 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 864 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 865 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 866 | else { |
| 867 | struct btrfs_extent_ref_v0 *ref0; |
| 868 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 869 | struct btrfs_extent_ref_v0); |
| 870 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 871 | } |
| 872 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 873 | btrfs_mark_buffer_dirty(leaf); |
| 874 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 875 | return ret; |
| 876 | } |
| 877 | |
| 878 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 879 | struct btrfs_path *path, |
| 880 | struct btrfs_extent_inline_ref *iref) |
| 881 | { |
| 882 | struct btrfs_key key; |
| 883 | struct extent_buffer *leaf; |
| 884 | struct btrfs_extent_data_ref *ref1; |
| 885 | struct btrfs_shared_data_ref *ref2; |
| 886 | u32 num_refs = 0; |
| 887 | |
| 888 | leaf = path->nodes[0]; |
| 889 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 890 | if (iref) { |
| 891 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 892 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 893 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 894 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 895 | } else { |
| 896 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 897 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 898 | } |
| 899 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 900 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 901 | struct btrfs_extent_data_ref); |
| 902 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 903 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 904 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 905 | struct btrfs_shared_data_ref); |
| 906 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 907 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 908 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 909 | struct btrfs_extent_ref_v0 *ref0; |
| 910 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 911 | struct btrfs_extent_ref_v0); |
| 912 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 913 | #endif |
| 914 | } else { |
| 915 | WARN_ON(1); |
| 916 | } |
| 917 | return num_refs; |
| 918 | } |
| 919 | |
| 920 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 921 | struct btrfs_root *root, |
| 922 | struct btrfs_path *path, |
| 923 | u64 bytenr, u64 parent, |
| 924 | u64 root_objectid) |
| 925 | { |
| 926 | struct btrfs_key key; |
| 927 | int ret; |
| 928 | |
| 929 | key.objectid = bytenr; |
| 930 | if (parent) { |
| 931 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 932 | key.offset = parent; |
| 933 | } else { |
| 934 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 935 | key.offset = root_objectid; |
| 936 | } |
| 937 | |
| 938 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 939 | if (ret > 0) |
| 940 | ret = -ENOENT; |
| 941 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 942 | if (ret == -ENOENT && parent) { |
| 943 | btrfs_release_path(root, path); |
| 944 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 945 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 946 | if (ret > 0) |
| 947 | ret = -ENOENT; |
| 948 | } |
| 949 | #endif |
| 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 954 | struct btrfs_root *root, |
| 955 | struct btrfs_path *path, |
| 956 | u64 bytenr, u64 parent, |
| 957 | u64 root_objectid) |
| 958 | { |
| 959 | struct btrfs_key key; |
| 960 | int ret; |
| 961 | |
| 962 | key.objectid = bytenr; |
| 963 | if (parent) { |
| 964 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 965 | key.offset = parent; |
| 966 | } else { |
| 967 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 968 | key.offset = root_objectid; |
| 969 | } |
| 970 | |
| 971 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 972 | btrfs_release_path(root, path); |
| 973 | return ret; |
| 974 | } |
| 975 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 976 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 977 | { |
| 978 | int type; |
| 979 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 980 | if (parent > 0) |
| 981 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 982 | else |
| 983 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 984 | } else { |
| 985 | if (parent > 0) |
| 986 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 987 | else |
| 988 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 989 | } |
| 990 | return type; |
| 991 | } |
| 992 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 993 | static int find_next_key(struct btrfs_path *path, int level, |
| 994 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 995 | |
| 996 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 997 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 998 | if (!path->nodes[level]) |
| 999 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1000 | if (path->slots[level] + 1 >= |
| 1001 | btrfs_header_nritems(path->nodes[level])) |
| 1002 | continue; |
| 1003 | if (level == 0) |
| 1004 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1005 | path->slots[level] + 1); |
| 1006 | else |
| 1007 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1008 | path->slots[level] + 1); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1016 | * to the address of inline back ref, and 0 is returned. |
| 1017 | * |
| 1018 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1019 | * should be inserted, and -ENOENT is returned. |
| 1020 | * |
| 1021 | * if insert is true and there are too many inline back refs, the path |
| 1022 | * points to the extent item, and -EAGAIN is returned. |
| 1023 | * |
| 1024 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1025 | * items in the tree are ordered. |
| 1026 | */ |
| 1027 | static noinline_for_stack |
| 1028 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1029 | struct btrfs_root *root, |
| 1030 | struct btrfs_path *path, |
| 1031 | struct btrfs_extent_inline_ref **ref_ret, |
| 1032 | u64 bytenr, u64 num_bytes, |
| 1033 | u64 parent, u64 root_objectid, |
| 1034 | u64 owner, u64 offset, int insert) |
| 1035 | { |
| 1036 | struct btrfs_key key; |
| 1037 | struct extent_buffer *leaf; |
| 1038 | struct btrfs_extent_item *ei; |
| 1039 | struct btrfs_extent_inline_ref *iref; |
| 1040 | u64 flags; |
| 1041 | u64 item_size; |
| 1042 | unsigned long ptr; |
| 1043 | unsigned long end; |
| 1044 | int extra_size; |
| 1045 | int type; |
| 1046 | int want; |
| 1047 | int ret; |
| 1048 | int err = 0; |
| 1049 | |
| 1050 | key.objectid = bytenr; |
| 1051 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1052 | key.offset = num_bytes; |
| 1053 | |
| 1054 | want = extent_ref_type(parent, owner); |
| 1055 | if (insert) { |
| 1056 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1057 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1058 | } else |
| 1059 | extra_size = -1; |
| 1060 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1061 | if (ret < 0) { |
| 1062 | err = ret; |
| 1063 | goto out; |
| 1064 | } |
| 1065 | BUG_ON(ret); |
| 1066 | |
| 1067 | leaf = path->nodes[0]; |
| 1068 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1069 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1070 | if (item_size < sizeof(*ei)) { |
| 1071 | if (!insert) { |
| 1072 | err = -ENOENT; |
| 1073 | goto out; |
| 1074 | } |
| 1075 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1076 | extra_size); |
| 1077 | if (ret < 0) { |
| 1078 | err = ret; |
| 1079 | goto out; |
| 1080 | } |
| 1081 | leaf = path->nodes[0]; |
| 1082 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1083 | } |
| 1084 | #endif |
| 1085 | BUG_ON(item_size < sizeof(*ei)); |
| 1086 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1087 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1088 | flags = btrfs_extent_flags(leaf, ei); |
| 1089 | |
| 1090 | ptr = (unsigned long)(ei + 1); |
| 1091 | end = (unsigned long)ei + item_size; |
| 1092 | |
| 1093 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1094 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1095 | BUG_ON(ptr > end); |
| 1096 | } else { |
| 1097 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1098 | } |
| 1099 | |
| 1100 | err = -ENOENT; |
| 1101 | while (1) { |
| 1102 | if (ptr >= end) { |
| 1103 | WARN_ON(ptr > end); |
| 1104 | break; |
| 1105 | } |
| 1106 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1107 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1108 | if (want < type) |
| 1109 | break; |
| 1110 | if (want > type) { |
| 1111 | ptr += btrfs_extent_inline_ref_size(type); |
| 1112 | continue; |
| 1113 | } |
| 1114 | |
| 1115 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1116 | struct btrfs_extent_data_ref *dref; |
| 1117 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1118 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1119 | owner, offset)) { |
| 1120 | err = 0; |
| 1121 | break; |
| 1122 | } |
| 1123 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1124 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1125 | break; |
| 1126 | } else { |
| 1127 | u64 ref_offset; |
| 1128 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1129 | if (parent > 0) { |
| 1130 | if (parent == ref_offset) { |
| 1131 | err = 0; |
| 1132 | break; |
| 1133 | } |
| 1134 | if (ref_offset < parent) |
| 1135 | break; |
| 1136 | } else { |
| 1137 | if (root_objectid == ref_offset) { |
| 1138 | err = 0; |
| 1139 | break; |
| 1140 | } |
| 1141 | if (ref_offset < root_objectid) |
| 1142 | break; |
| 1143 | } |
| 1144 | } |
| 1145 | ptr += btrfs_extent_inline_ref_size(type); |
| 1146 | } |
| 1147 | if (err == -ENOENT && insert) { |
| 1148 | if (item_size + extra_size >= |
| 1149 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1150 | err = -EAGAIN; |
| 1151 | goto out; |
| 1152 | } |
| 1153 | /* |
| 1154 | * To add new inline back ref, we have to make sure |
| 1155 | * there is no corresponding back ref item. |
| 1156 | * For simplicity, we just do not add new inline back |
| 1157 | * ref if there is any kind of item for this block |
| 1158 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1159 | if (find_next_key(path, 0, &key) == 0 && |
| 1160 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1161 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1162 | err = -EAGAIN; |
| 1163 | goto out; |
| 1164 | } |
| 1165 | } |
| 1166 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1167 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1168 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1169 | path->keep_locks = 0; |
| 1170 | btrfs_unlock_up_safe(path, 1); |
| 1171 | } |
| 1172 | return err; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * helper to add new inline back ref |
| 1177 | */ |
| 1178 | static noinline_for_stack |
| 1179 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1180 | struct btrfs_root *root, |
| 1181 | struct btrfs_path *path, |
| 1182 | struct btrfs_extent_inline_ref *iref, |
| 1183 | u64 parent, u64 root_objectid, |
| 1184 | u64 owner, u64 offset, int refs_to_add, |
| 1185 | struct btrfs_delayed_extent_op *extent_op) |
| 1186 | { |
| 1187 | struct extent_buffer *leaf; |
| 1188 | struct btrfs_extent_item *ei; |
| 1189 | unsigned long ptr; |
| 1190 | unsigned long end; |
| 1191 | unsigned long item_offset; |
| 1192 | u64 refs; |
| 1193 | int size; |
| 1194 | int type; |
| 1195 | int ret; |
| 1196 | |
| 1197 | leaf = path->nodes[0]; |
| 1198 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1199 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1200 | |
| 1201 | type = extent_ref_type(parent, owner); |
| 1202 | size = btrfs_extent_inline_ref_size(type); |
| 1203 | |
| 1204 | ret = btrfs_extend_item(trans, root, path, size); |
| 1205 | BUG_ON(ret); |
| 1206 | |
| 1207 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1208 | refs = btrfs_extent_refs(leaf, ei); |
| 1209 | refs += refs_to_add; |
| 1210 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1211 | if (extent_op) |
| 1212 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1213 | |
| 1214 | ptr = (unsigned long)ei + item_offset; |
| 1215 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1216 | if (ptr < end - size) |
| 1217 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1218 | end - size - ptr); |
| 1219 | |
| 1220 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1221 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1222 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1223 | struct btrfs_extent_data_ref *dref; |
| 1224 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1225 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1226 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1227 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1228 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1229 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1230 | struct btrfs_shared_data_ref *sref; |
| 1231 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1232 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1233 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1234 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1235 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1236 | } else { |
| 1237 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1238 | } |
| 1239 | btrfs_mark_buffer_dirty(leaf); |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1244 | struct btrfs_root *root, |
| 1245 | struct btrfs_path *path, |
| 1246 | struct btrfs_extent_inline_ref **ref_ret, |
| 1247 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1248 | u64 root_objectid, u64 owner, u64 offset) |
| 1249 | { |
| 1250 | int ret; |
| 1251 | |
| 1252 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1253 | bytenr, num_bytes, parent, |
| 1254 | root_objectid, owner, offset, 0); |
| 1255 | if (ret != -ENOENT) |
| 1256 | return ret; |
| 1257 | |
| 1258 | btrfs_release_path(root, path); |
| 1259 | *ref_ret = NULL; |
| 1260 | |
| 1261 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1262 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1263 | root_objectid); |
| 1264 | } else { |
| 1265 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1266 | root_objectid, owner, offset); |
| 1267 | } |
| 1268 | return ret; |
| 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * helper to update/remove inline back ref |
| 1273 | */ |
| 1274 | static noinline_for_stack |
| 1275 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1276 | struct btrfs_root *root, |
| 1277 | struct btrfs_path *path, |
| 1278 | struct btrfs_extent_inline_ref *iref, |
| 1279 | int refs_to_mod, |
| 1280 | struct btrfs_delayed_extent_op *extent_op) |
| 1281 | { |
| 1282 | struct extent_buffer *leaf; |
| 1283 | struct btrfs_extent_item *ei; |
| 1284 | struct btrfs_extent_data_ref *dref = NULL; |
| 1285 | struct btrfs_shared_data_ref *sref = NULL; |
| 1286 | unsigned long ptr; |
| 1287 | unsigned long end; |
| 1288 | u32 item_size; |
| 1289 | int size; |
| 1290 | int type; |
| 1291 | int ret; |
| 1292 | u64 refs; |
| 1293 | |
| 1294 | leaf = path->nodes[0]; |
| 1295 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1296 | refs = btrfs_extent_refs(leaf, ei); |
| 1297 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1298 | refs += refs_to_mod; |
| 1299 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1300 | if (extent_op) |
| 1301 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1302 | |
| 1303 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1304 | |
| 1305 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1306 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1307 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1308 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1309 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1310 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1311 | } else { |
| 1312 | refs = 1; |
| 1313 | BUG_ON(refs_to_mod != -1); |
| 1314 | } |
| 1315 | |
| 1316 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1317 | refs += refs_to_mod; |
| 1318 | |
| 1319 | if (refs > 0) { |
| 1320 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1321 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1322 | else |
| 1323 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1324 | } else { |
| 1325 | size = btrfs_extent_inline_ref_size(type); |
| 1326 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1327 | ptr = (unsigned long)iref; |
| 1328 | end = (unsigned long)ei + item_size; |
| 1329 | if (ptr + size < end) |
| 1330 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1331 | end - ptr - size); |
| 1332 | item_size -= size; |
| 1333 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1334 | BUG_ON(ret); |
| 1335 | } |
| 1336 | btrfs_mark_buffer_dirty(leaf); |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | static noinline_for_stack |
| 1341 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1342 | struct btrfs_root *root, |
| 1343 | struct btrfs_path *path, |
| 1344 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1345 | u64 root_objectid, u64 owner, |
| 1346 | u64 offset, int refs_to_add, |
| 1347 | struct btrfs_delayed_extent_op *extent_op) |
| 1348 | { |
| 1349 | struct btrfs_extent_inline_ref *iref; |
| 1350 | int ret; |
| 1351 | |
| 1352 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1353 | bytenr, num_bytes, parent, |
| 1354 | root_objectid, owner, offset, 1); |
| 1355 | if (ret == 0) { |
| 1356 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1357 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1358 | refs_to_add, extent_op); |
| 1359 | } else if (ret == -ENOENT) { |
| 1360 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1361 | parent, root_objectid, |
| 1362 | owner, offset, refs_to_add, |
| 1363 | extent_op); |
| 1364 | } |
| 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1369 | struct btrfs_root *root, |
| 1370 | struct btrfs_path *path, |
| 1371 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1372 | u64 owner, u64 offset, int refs_to_add) |
| 1373 | { |
| 1374 | int ret; |
| 1375 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1376 | BUG_ON(refs_to_add != 1); |
| 1377 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1378 | parent, root_objectid); |
| 1379 | } else { |
| 1380 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1381 | parent, root_objectid, |
| 1382 | owner, offset, refs_to_add); |
| 1383 | } |
| 1384 | return ret; |
| 1385 | } |
| 1386 | |
| 1387 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1388 | struct btrfs_root *root, |
| 1389 | struct btrfs_path *path, |
| 1390 | struct btrfs_extent_inline_ref *iref, |
| 1391 | int refs_to_drop, int is_data) |
| 1392 | { |
| 1393 | int ret; |
| 1394 | |
| 1395 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1396 | if (iref) { |
| 1397 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1398 | -refs_to_drop, NULL); |
| 1399 | } else if (is_data) { |
| 1400 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1401 | } else { |
| 1402 | ret = btrfs_del_item(trans, root, path); |
| 1403 | } |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 1407 | #ifdef BIO_RW_DISCARD |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1408 | static void btrfs_issue_discard(struct block_device *bdev, |
| 1409 | u64 start, u64 len) |
| 1410 | { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1411 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1412 | } |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 1413 | #endif |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1414 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1415 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 1416 | u64 num_bytes) |
| 1417 | { |
| 1418 | #ifdef BIO_RW_DISCARD |
| 1419 | int ret; |
| 1420 | u64 map_length = num_bytes; |
| 1421 | struct btrfs_multi_bio *multi = NULL; |
| 1422 | |
| 1423 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1424 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1425 | bytenr, &map_length, &multi, 0); |
| 1426 | if (!ret) { |
| 1427 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1428 | int i; |
| 1429 | |
| 1430 | if (map_length > num_bytes) |
| 1431 | map_length = num_bytes; |
| 1432 | |
| 1433 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1434 | btrfs_issue_discard(stripe->dev->bdev, |
| 1435 | stripe->physical, |
| 1436 | map_length); |
| 1437 | } |
| 1438 | kfree(multi); |
| 1439 | } |
| 1440 | |
| 1441 | return ret; |
| 1442 | #else |
| 1443 | return 0; |
| 1444 | #endif |
| 1445 | } |
| 1446 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1447 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1448 | struct btrfs_root *root, |
| 1449 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1450 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1451 | { |
| 1452 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1453 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1454 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1455 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1456 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1457 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1458 | parent, root_objectid, (int)owner, |
| 1459 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1460 | } else { |
| 1461 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1462 | parent, root_objectid, owner, offset, |
| 1463 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1464 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1465 | return ret; |
| 1466 | } |
| 1467 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1468 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1469 | struct btrfs_root *root, |
| 1470 | u64 bytenr, u64 num_bytes, |
| 1471 | u64 parent, u64 root_objectid, |
| 1472 | u64 owner, u64 offset, int refs_to_add, |
| 1473 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1474 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1475 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1476 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1477 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1478 | u64 refs; |
| 1479 | int ret; |
| 1480 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1481 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1482 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1483 | if (!path) |
| 1484 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1485 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1486 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1487 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1488 | /* this will setup the path even if it fails to insert the back ref */ |
| 1489 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1490 | path, bytenr, num_bytes, parent, |
| 1491 | root_objectid, owner, offset, |
| 1492 | refs_to_add, extent_op); |
| 1493 | if (ret == 0) |
| 1494 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1495 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1496 | if (ret != -EAGAIN) { |
| 1497 | err = ret; |
| 1498 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1499 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1500 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1501 | leaf = path->nodes[0]; |
| 1502 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1503 | refs = btrfs_extent_refs(leaf, item); |
| 1504 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1505 | if (extent_op) |
| 1506 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1507 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1508 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1509 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1510 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1511 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1512 | path->leave_spinning = 1; |
| 1513 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1514 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1515 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1516 | path, bytenr, parent, root_objectid, |
| 1517 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1518 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1519 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1520 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1521 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1522 | } |
| 1523 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1524 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1525 | struct btrfs_root *root, |
| 1526 | struct btrfs_delayed_ref_node *node, |
| 1527 | struct btrfs_delayed_extent_op *extent_op, |
| 1528 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1529 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1530 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1531 | struct btrfs_delayed_data_ref *ref; |
| 1532 | struct btrfs_key ins; |
| 1533 | u64 parent = 0; |
| 1534 | u64 ref_root = 0; |
| 1535 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1536 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1537 | ins.objectid = node->bytenr; |
| 1538 | ins.offset = node->num_bytes; |
| 1539 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1540 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1541 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1542 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1543 | parent = ref->parent; |
| 1544 | else |
| 1545 | ref_root = ref->root; |
| 1546 | |
| 1547 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1548 | if (extent_op) { |
| 1549 | BUG_ON(extent_op->update_key); |
| 1550 | flags |= extent_op->flags_to_set; |
| 1551 | } |
| 1552 | ret = alloc_reserved_file_extent(trans, root, |
| 1553 | parent, ref_root, flags, |
| 1554 | ref->objectid, ref->offset, |
| 1555 | &ins, node->ref_mod); |
| 1556 | update_reserved_extents(root, ins.objectid, ins.offset, 0); |
| 1557 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1558 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1559 | node->num_bytes, parent, |
| 1560 | ref_root, ref->objectid, |
| 1561 | ref->offset, node->ref_mod, |
| 1562 | extent_op); |
| 1563 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1564 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1565 | node->num_bytes, parent, |
| 1566 | ref_root, ref->objectid, |
| 1567 | ref->offset, node->ref_mod, |
| 1568 | extent_op); |
| 1569 | } else { |
| 1570 | BUG(); |
| 1571 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1572 | return ret; |
| 1573 | } |
| 1574 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1575 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1576 | struct extent_buffer *leaf, |
| 1577 | struct btrfs_extent_item *ei) |
| 1578 | { |
| 1579 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1580 | if (extent_op->update_flags) { |
| 1581 | flags |= extent_op->flags_to_set; |
| 1582 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1583 | } |
| 1584 | |
| 1585 | if (extent_op->update_key) { |
| 1586 | struct btrfs_tree_block_info *bi; |
| 1587 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1588 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1589 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1594 | struct btrfs_root *root, |
| 1595 | struct btrfs_delayed_ref_node *node, |
| 1596 | struct btrfs_delayed_extent_op *extent_op) |
| 1597 | { |
| 1598 | struct btrfs_key key; |
| 1599 | struct btrfs_path *path; |
| 1600 | struct btrfs_extent_item *ei; |
| 1601 | struct extent_buffer *leaf; |
| 1602 | u32 item_size; |
| 1603 | int ret; |
| 1604 | int err = 0; |
| 1605 | |
| 1606 | path = btrfs_alloc_path(); |
| 1607 | if (!path) |
| 1608 | return -ENOMEM; |
| 1609 | |
| 1610 | key.objectid = node->bytenr; |
| 1611 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1612 | key.offset = node->num_bytes; |
| 1613 | |
| 1614 | path->reada = 1; |
| 1615 | path->leave_spinning = 1; |
| 1616 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1617 | path, 0, 1); |
| 1618 | if (ret < 0) { |
| 1619 | err = ret; |
| 1620 | goto out; |
| 1621 | } |
| 1622 | if (ret > 0) { |
| 1623 | err = -EIO; |
| 1624 | goto out; |
| 1625 | } |
| 1626 | |
| 1627 | leaf = path->nodes[0]; |
| 1628 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1629 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1630 | if (item_size < sizeof(*ei)) { |
| 1631 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1632 | path, (u64)-1, 0); |
| 1633 | if (ret < 0) { |
| 1634 | err = ret; |
| 1635 | goto out; |
| 1636 | } |
| 1637 | leaf = path->nodes[0]; |
| 1638 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1639 | } |
| 1640 | #endif |
| 1641 | BUG_ON(item_size < sizeof(*ei)); |
| 1642 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1643 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1644 | |
| 1645 | btrfs_mark_buffer_dirty(leaf); |
| 1646 | out: |
| 1647 | btrfs_free_path(path); |
| 1648 | return err; |
| 1649 | } |
| 1650 | |
| 1651 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 1652 | struct btrfs_root *root, |
| 1653 | struct btrfs_delayed_ref_node *node, |
| 1654 | struct btrfs_delayed_extent_op *extent_op, |
| 1655 | int insert_reserved) |
| 1656 | { |
| 1657 | int ret = 0; |
| 1658 | struct btrfs_delayed_tree_ref *ref; |
| 1659 | struct btrfs_key ins; |
| 1660 | u64 parent = 0; |
| 1661 | u64 ref_root = 0; |
| 1662 | |
| 1663 | ins.objectid = node->bytenr; |
| 1664 | ins.offset = node->num_bytes; |
| 1665 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 1666 | |
| 1667 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 1668 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1669 | parent = ref->parent; |
| 1670 | else |
| 1671 | ref_root = ref->root; |
| 1672 | |
| 1673 | BUG_ON(node->ref_mod != 1); |
| 1674 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1675 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 1676 | !extent_op->update_key); |
| 1677 | ret = alloc_reserved_tree_block(trans, root, |
| 1678 | parent, ref_root, |
| 1679 | extent_op->flags_to_set, |
| 1680 | &extent_op->key, |
| 1681 | ref->level, &ins); |
| 1682 | update_reserved_extents(root, ins.objectid, ins.offset, 0); |
| 1683 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1684 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1685 | node->num_bytes, parent, ref_root, |
| 1686 | ref->level, 0, 1, extent_op); |
| 1687 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1688 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1689 | node->num_bytes, parent, ref_root, |
| 1690 | ref->level, 0, 1, extent_op); |
| 1691 | } else { |
| 1692 | BUG(); |
| 1693 | } |
| 1694 | return ret; |
| 1695 | } |
| 1696 | |
| 1697 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1698 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1699 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 1700 | struct btrfs_root *root, |
| 1701 | struct btrfs_delayed_ref_node *node, |
| 1702 | struct btrfs_delayed_extent_op *extent_op, |
| 1703 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1704 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1705 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1706 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1707 | struct btrfs_delayed_ref_head *head; |
| 1708 | /* |
| 1709 | * we've hit the end of the chain and we were supposed |
| 1710 | * to insert this extent into the tree. But, it got |
| 1711 | * deleted before we ever needed to insert it, so all |
| 1712 | * we have to do is clean up the accounting |
| 1713 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1714 | BUG_ON(extent_op); |
| 1715 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1716 | if (insert_reserved) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1717 | if (head->is_data) { |
| 1718 | ret = btrfs_del_csums(trans, root, |
| 1719 | node->bytenr, |
| 1720 | node->num_bytes); |
| 1721 | BUG_ON(ret); |
| 1722 | } |
| 1723 | btrfs_update_pinned_extents(root, node->bytenr, |
| 1724 | node->num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1725 | update_reserved_extents(root, node->bytenr, |
| 1726 | node->num_bytes, 0); |
| 1727 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1728 | mutex_unlock(&head->mutex); |
| 1729 | return 0; |
| 1730 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1731 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1732 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 1733 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 1734 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 1735 | insert_reserved); |
| 1736 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 1737 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1738 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 1739 | insert_reserved); |
| 1740 | else |
| 1741 | BUG(); |
| 1742 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1743 | } |
| 1744 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1745 | static noinline struct btrfs_delayed_ref_node * |
| 1746 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1747 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1748 | struct rb_node *node; |
| 1749 | struct btrfs_delayed_ref_node *ref; |
| 1750 | int action = BTRFS_ADD_DELAYED_REF; |
| 1751 | again: |
| 1752 | /* |
| 1753 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 1754 | * this prevents ref count from going down to zero when |
| 1755 | * there still are pending delayed ref. |
| 1756 | */ |
| 1757 | node = rb_prev(&head->node.rb_node); |
| 1758 | while (1) { |
| 1759 | if (!node) |
| 1760 | break; |
| 1761 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 1762 | rb_node); |
| 1763 | if (ref->bytenr != head->node.bytenr) |
| 1764 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1765 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1766 | return ref; |
| 1767 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1768 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1769 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 1770 | action = BTRFS_DROP_DELAYED_REF; |
| 1771 | goto again; |
| 1772 | } |
| 1773 | return NULL; |
| 1774 | } |
| 1775 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1776 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 1777 | struct btrfs_root *root, |
| 1778 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1779 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1780 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1781 | struct btrfs_delayed_ref_node *ref; |
| 1782 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1783 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1784 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1785 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1786 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1787 | |
| 1788 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1789 | while (1) { |
| 1790 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1791 | /* pick a new head ref from the cluster list */ |
| 1792 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1793 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1794 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1795 | locked_ref = list_entry(cluster->next, |
| 1796 | struct btrfs_delayed_ref_head, cluster); |
| 1797 | |
| 1798 | /* grab the lock that says we are going to process |
| 1799 | * all the refs for this head */ |
| 1800 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 1801 | |
| 1802 | /* |
| 1803 | * we may have dropped the spin lock to get the head |
| 1804 | * mutex lock, and that might have given someone else |
| 1805 | * time to free the head. If that's true, it has been |
| 1806 | * removed from our list and we can move on. |
| 1807 | */ |
| 1808 | if (ret == -EAGAIN) { |
| 1809 | locked_ref = NULL; |
| 1810 | count++; |
| 1811 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | /* |
| 1816 | * record the must insert reserved flag before we |
| 1817 | * drop the spin lock. |
| 1818 | */ |
| 1819 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 1820 | locked_ref->must_insert_reserved = 0; |
| 1821 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1822 | extent_op = locked_ref->extent_op; |
| 1823 | locked_ref->extent_op = NULL; |
| 1824 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1825 | /* |
| 1826 | * locked_ref is the head node, so we have to go one |
| 1827 | * node back for any delayed ref updates |
| 1828 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1829 | ref = select_delayed_ref(locked_ref); |
| 1830 | if (!ref) { |
| 1831 | /* All delayed refs have been processed, Go ahead |
| 1832 | * and send the head node to run_one_delayed_ref, |
| 1833 | * so that any accounting fixes can happen |
| 1834 | */ |
| 1835 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1836 | |
| 1837 | if (extent_op && must_insert_reserved) { |
| 1838 | kfree(extent_op); |
| 1839 | extent_op = NULL; |
| 1840 | } |
| 1841 | |
| 1842 | if (extent_op) { |
| 1843 | spin_unlock(&delayed_refs->lock); |
| 1844 | |
| 1845 | ret = run_delayed_extent_op(trans, root, |
| 1846 | ref, extent_op); |
| 1847 | BUG_ON(ret); |
| 1848 | kfree(extent_op); |
| 1849 | |
| 1850 | cond_resched(); |
| 1851 | spin_lock(&delayed_refs->lock); |
| 1852 | continue; |
| 1853 | } |
| 1854 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1855 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1856 | locked_ref = NULL; |
| 1857 | } |
| 1858 | |
| 1859 | ref->in_tree = 0; |
| 1860 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 1861 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1862 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1863 | spin_unlock(&delayed_refs->lock); |
| 1864 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1865 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1866 | must_insert_reserved); |
| 1867 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1868 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1869 | btrfs_put_delayed_ref(ref); |
| 1870 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1871 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1872 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 1873 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1874 | spin_lock(&delayed_refs->lock); |
| 1875 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1876 | return count; |
| 1877 | } |
| 1878 | |
| 1879 | /* |
| 1880 | * this starts processing the delayed reference count updates and |
| 1881 | * extent insertions we have queued up so far. count can be |
| 1882 | * 0, which means to process everything in the tree at the start |
| 1883 | * of the run (but not newly added entries), or it can be some target |
| 1884 | * number you'd like to process. |
| 1885 | */ |
| 1886 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 1887 | struct btrfs_root *root, unsigned long count) |
| 1888 | { |
| 1889 | struct rb_node *node; |
| 1890 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1891 | struct btrfs_delayed_ref_node *ref; |
| 1892 | struct list_head cluster; |
| 1893 | int ret; |
| 1894 | int run_all = count == (unsigned long)-1; |
| 1895 | int run_most = 0; |
| 1896 | |
| 1897 | if (root == root->fs_info->extent_root) |
| 1898 | root = root->fs_info->tree_root; |
| 1899 | |
| 1900 | delayed_refs = &trans->transaction->delayed_refs; |
| 1901 | INIT_LIST_HEAD(&cluster); |
| 1902 | again: |
| 1903 | spin_lock(&delayed_refs->lock); |
| 1904 | if (count == 0) { |
| 1905 | count = delayed_refs->num_entries * 2; |
| 1906 | run_most = 1; |
| 1907 | } |
| 1908 | while (1) { |
| 1909 | if (!(run_all || run_most) && |
| 1910 | delayed_refs->num_heads_ready < 64) |
| 1911 | break; |
| 1912 | |
| 1913 | /* |
| 1914 | * go find something we can process in the rbtree. We start at |
| 1915 | * the beginning of the tree, and then build a cluster |
| 1916 | * of refs to process starting at the first one we are able to |
| 1917 | * lock |
| 1918 | */ |
| 1919 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 1920 | delayed_refs->run_delayed_start); |
| 1921 | if (ret) |
| 1922 | break; |
| 1923 | |
| 1924 | ret = run_clustered_refs(trans, root, &cluster); |
| 1925 | BUG_ON(ret < 0); |
| 1926 | |
| 1927 | count -= min_t(unsigned long, ret, count); |
| 1928 | |
| 1929 | if (count == 0) |
| 1930 | break; |
| 1931 | } |
| 1932 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1933 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1934 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1935 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1936 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1937 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1938 | |
| 1939 | while (node) { |
| 1940 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 1941 | rb_node); |
| 1942 | if (btrfs_delayed_ref_is_head(ref)) { |
| 1943 | struct btrfs_delayed_ref_head *head; |
| 1944 | |
| 1945 | head = btrfs_delayed_node_to_head(ref); |
| 1946 | atomic_inc(&ref->refs); |
| 1947 | |
| 1948 | spin_unlock(&delayed_refs->lock); |
| 1949 | mutex_lock(&head->mutex); |
| 1950 | mutex_unlock(&head->mutex); |
| 1951 | |
| 1952 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 1953 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1954 | goto again; |
| 1955 | } |
| 1956 | node = rb_next(node); |
| 1957 | } |
| 1958 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1959 | schedule_timeout(1); |
| 1960 | goto again; |
| 1961 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1962 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1963 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1964 | return 0; |
| 1965 | } |
| 1966 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1967 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 1968 | struct btrfs_root *root, |
| 1969 | u64 bytenr, u64 num_bytes, u64 flags, |
| 1970 | int is_data) |
| 1971 | { |
| 1972 | struct btrfs_delayed_extent_op *extent_op; |
| 1973 | int ret; |
| 1974 | |
| 1975 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 1976 | if (!extent_op) |
| 1977 | return -ENOMEM; |
| 1978 | |
| 1979 | extent_op->flags_to_set = flags; |
| 1980 | extent_op->update_flags = 1; |
| 1981 | extent_op->update_key = 0; |
| 1982 | extent_op->is_data = is_data ? 1 : 0; |
| 1983 | |
| 1984 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 1985 | if (ret) |
| 1986 | kfree(extent_op); |
| 1987 | return ret; |
| 1988 | } |
| 1989 | |
| 1990 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 1991 | struct btrfs_root *root, |
| 1992 | struct btrfs_path *path, |
| 1993 | u64 objectid, u64 offset, u64 bytenr) |
| 1994 | { |
| 1995 | struct btrfs_delayed_ref_head *head; |
| 1996 | struct btrfs_delayed_ref_node *ref; |
| 1997 | struct btrfs_delayed_data_ref *data_ref; |
| 1998 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1999 | struct rb_node *node; |
| 2000 | int ret = 0; |
| 2001 | |
| 2002 | ret = -ENOENT; |
| 2003 | delayed_refs = &trans->transaction->delayed_refs; |
| 2004 | spin_lock(&delayed_refs->lock); |
| 2005 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2006 | if (!head) |
| 2007 | goto out; |
| 2008 | |
| 2009 | if (!mutex_trylock(&head->mutex)) { |
| 2010 | atomic_inc(&head->node.refs); |
| 2011 | spin_unlock(&delayed_refs->lock); |
| 2012 | |
| 2013 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2014 | |
| 2015 | mutex_lock(&head->mutex); |
| 2016 | mutex_unlock(&head->mutex); |
| 2017 | btrfs_put_delayed_ref(&head->node); |
| 2018 | return -EAGAIN; |
| 2019 | } |
| 2020 | |
| 2021 | node = rb_prev(&head->node.rb_node); |
| 2022 | if (!node) |
| 2023 | goto out_unlock; |
| 2024 | |
| 2025 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2026 | |
| 2027 | if (ref->bytenr != bytenr) |
| 2028 | goto out_unlock; |
| 2029 | |
| 2030 | ret = 1; |
| 2031 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2032 | goto out_unlock; |
| 2033 | |
| 2034 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2035 | |
| 2036 | node = rb_prev(node); |
| 2037 | if (node) { |
| 2038 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2039 | if (ref->bytenr == bytenr) |
| 2040 | goto out_unlock; |
| 2041 | } |
| 2042 | |
| 2043 | if (data_ref->root != root->root_key.objectid || |
| 2044 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2045 | goto out_unlock; |
| 2046 | |
| 2047 | ret = 0; |
| 2048 | out_unlock: |
| 2049 | mutex_unlock(&head->mutex); |
| 2050 | out: |
| 2051 | spin_unlock(&delayed_refs->lock); |
| 2052 | return ret; |
| 2053 | } |
| 2054 | |
| 2055 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2056 | struct btrfs_root *root, |
| 2057 | struct btrfs_path *path, |
| 2058 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2059 | { |
| 2060 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2061 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2062 | struct btrfs_extent_data_ref *ref; |
| 2063 | struct btrfs_extent_inline_ref *iref; |
| 2064 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2065 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2066 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2067 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2068 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2069 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2070 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2071 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2072 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2073 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2074 | if (ret < 0) |
| 2075 | goto out; |
| 2076 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2077 | |
| 2078 | ret = -ENOENT; |
| 2079 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2080 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2081 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2082 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2083 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2084 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2085 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2086 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2087 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2088 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2089 | ret = 1; |
| 2090 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2091 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2092 | if (item_size < sizeof(*ei)) { |
| 2093 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2094 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2095 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2096 | #endif |
| 2097 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2098 | |
| 2099 | if (item_size != sizeof(*ei) + |
| 2100 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2101 | goto out; |
| 2102 | |
| 2103 | if (btrfs_extent_generation(leaf, ei) <= |
| 2104 | btrfs_root_last_snapshot(&root->root_item)) |
| 2105 | goto out; |
| 2106 | |
| 2107 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2108 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2109 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2110 | goto out; |
| 2111 | |
| 2112 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2113 | if (btrfs_extent_refs(leaf, ei) != |
| 2114 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2115 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2116 | root->root_key.objectid || |
| 2117 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2118 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2119 | goto out; |
| 2120 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2121 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2122 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2123 | return ret; |
| 2124 | } |
| 2125 | |
| 2126 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2127 | struct btrfs_root *root, |
| 2128 | u64 objectid, u64 offset, u64 bytenr) |
| 2129 | { |
| 2130 | struct btrfs_path *path; |
| 2131 | int ret; |
| 2132 | int ret2; |
| 2133 | |
| 2134 | path = btrfs_alloc_path(); |
| 2135 | if (!path) |
| 2136 | return -ENOENT; |
| 2137 | |
| 2138 | do { |
| 2139 | ret = check_committed_ref(trans, root, path, objectid, |
| 2140 | offset, bytenr); |
| 2141 | if (ret && ret != -ENOENT) |
| 2142 | goto out; |
| 2143 | |
| 2144 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2145 | offset, bytenr); |
| 2146 | } while (ret2 == -EAGAIN); |
| 2147 | |
| 2148 | if (ret2 && ret2 != -ENOENT) { |
| 2149 | ret = ret2; |
| 2150 | goto out; |
| 2151 | } |
| 2152 | |
| 2153 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2154 | ret = 0; |
| 2155 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2156 | btrfs_free_path(path); |
| 2157 | return ret; |
| 2158 | } |
| 2159 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2160 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2161 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2162 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2163 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2164 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2165 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2166 | u64 root_gen; |
| 2167 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2168 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2169 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2170 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2171 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2172 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2173 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2174 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2175 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2176 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2177 | shared = 0; |
| 2178 | root_gen = root->root_key.offset; |
| 2179 | } else { |
| 2180 | shared = 1; |
| 2181 | root_gen = trans->transid - 1; |
| 2182 | } |
| 2183 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2184 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2185 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2186 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2187 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2188 | struct btrfs_leaf_ref *ref; |
| 2189 | struct btrfs_extent_info *info; |
| 2190 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2191 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2192 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2193 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2194 | goto out; |
| 2195 | } |
| 2196 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2197 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2198 | ref->bytenr = buf->start; |
| 2199 | ref->owner = btrfs_header_owner(buf); |
| 2200 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2201 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2202 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2203 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2204 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2205 | u64 disk_bytenr; |
| 2206 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2207 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2208 | continue; |
| 2209 | fi = btrfs_item_ptr(buf, i, |
| 2210 | struct btrfs_file_extent_item); |
| 2211 | if (btrfs_file_extent_type(buf, fi) == |
| 2212 | BTRFS_FILE_EXTENT_INLINE) |
| 2213 | continue; |
| 2214 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2215 | if (disk_bytenr == 0) |
| 2216 | continue; |
| 2217 | |
| 2218 | info->bytenr = disk_bytenr; |
| 2219 | info->num_bytes = |
| 2220 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2221 | info->objectid = key.objectid; |
| 2222 | info->offset = key.offset; |
| 2223 | info++; |
| 2224 | } |
| 2225 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2226 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2227 | if (ret == -EEXIST && shared) { |
| 2228 | struct btrfs_leaf_ref *old; |
| 2229 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2230 | BUG_ON(!old); |
| 2231 | btrfs_remove_leaf_ref(root, old); |
| 2232 | btrfs_free_leaf_ref(root, old); |
| 2233 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2234 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2235 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2236 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2237 | } |
| 2238 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2239 | return ret; |
| 2240 | } |
| 2241 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2242 | /* when a block goes through cow, we update the reference counts of |
| 2243 | * everything that block points to. The internal pointers of the block |
| 2244 | * can be in just about any order, and it is likely to have clusters of |
| 2245 | * things that are close together and clusters of things that are not. |
| 2246 | * |
| 2247 | * To help reduce the seeks that come with updating all of these reference |
| 2248 | * counts, sort them by byte number before actual updates are done. |
| 2249 | * |
| 2250 | * struct refsort is used to match byte number to slot in the btree block. |
| 2251 | * we sort based on the byte number and then use the slot to actually |
| 2252 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2253 | * |
| 2254 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2255 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2256 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2257 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2258 | */ |
| 2259 | struct refsort { |
| 2260 | u64 bytenr; |
| 2261 | u32 slot; |
| 2262 | }; |
| 2263 | |
| 2264 | /* |
| 2265 | * for passing into sort() |
| 2266 | */ |
| 2267 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2268 | { |
| 2269 | const struct refsort *a = a_void; |
| 2270 | const struct refsort *b = b_void; |
| 2271 | |
| 2272 | if (a->bytenr < b->bytenr) |
| 2273 | return -1; |
| 2274 | if (a->bytenr > b->bytenr) |
| 2275 | return 1; |
| 2276 | return 0; |
| 2277 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2278 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2279 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2280 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2281 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2282 | struct extent_buffer *buf, |
| 2283 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2284 | { |
| 2285 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2286 | u64 num_bytes; |
| 2287 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2288 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2289 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2290 | struct btrfs_key key; |
| 2291 | struct btrfs_file_extent_item *fi; |
| 2292 | int i; |
| 2293 | int level; |
| 2294 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2295 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2296 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2297 | |
| 2298 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2299 | nritems = btrfs_header_nritems(buf); |
| 2300 | level = btrfs_header_level(buf); |
| 2301 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2302 | if (!root->ref_cows && level == 0) |
| 2303 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2304 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2305 | if (inc) |
| 2306 | process_func = btrfs_inc_extent_ref; |
| 2307 | else |
| 2308 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2309 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2310 | if (full_backref) |
| 2311 | parent = buf->start; |
| 2312 | else |
| 2313 | parent = 0; |
| 2314 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2315 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2316 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2317 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2318 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2319 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2320 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2321 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2322 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2323 | BTRFS_FILE_EXTENT_INLINE) |
| 2324 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2325 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2326 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2327 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2328 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2329 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2330 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2331 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2332 | parent, ref_root, key.objectid, |
| 2333 | key.offset); |
| 2334 | if (ret) |
| 2335 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2336 | } else { |
| 2337 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2338 | num_bytes = btrfs_level_size(root, level - 1); |
| 2339 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2340 | parent, ref_root, level - 1, 0); |
| 2341 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2342 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2343 | } |
| 2344 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2345 | return 0; |
| 2346 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2347 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2348 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2349 | } |
| 2350 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2351 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2352 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2353 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2354 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2355 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2356 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2357 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2358 | struct extent_buffer *buf, int full_backref) |
| 2359 | { |
| 2360 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2361 | } |
| 2362 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2363 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2364 | struct btrfs_root *root, |
| 2365 | struct btrfs_path *path, |
| 2366 | struct btrfs_block_group_cache *cache) |
| 2367 | { |
| 2368 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2369 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2370 | unsigned long bi; |
| 2371 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2372 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2373 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2374 | if (ret < 0) |
| 2375 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2376 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2377 | |
| 2378 | leaf = path->nodes[0]; |
| 2379 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2380 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2381 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2382 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2383 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2384 | if (ret) |
| 2385 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2386 | return 0; |
| 2387 | |
| 2388 | } |
| 2389 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2390 | static struct btrfs_block_group_cache * |
| 2391 | next_block_group(struct btrfs_root *root, |
| 2392 | struct btrfs_block_group_cache *cache) |
| 2393 | { |
| 2394 | struct rb_node *node; |
| 2395 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2396 | node = rb_next(&cache->cache_node); |
| 2397 | btrfs_put_block_group(cache); |
| 2398 | if (node) { |
| 2399 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2400 | cache_node); |
| 2401 | atomic_inc(&cache->count); |
| 2402 | } else |
| 2403 | cache = NULL; |
| 2404 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2405 | return cache; |
| 2406 | } |
| 2407 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2408 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2409 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2410 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2411 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2412 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2413 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2414 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2415 | |
| 2416 | path = btrfs_alloc_path(); |
| 2417 | if (!path) |
| 2418 | return -ENOMEM; |
| 2419 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2420 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2421 | if (last == 0) { |
| 2422 | err = btrfs_run_delayed_refs(trans, root, |
| 2423 | (unsigned long)-1); |
| 2424 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2425 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2426 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2427 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2428 | while (cache) { |
| 2429 | if (cache->dirty) |
| 2430 | break; |
| 2431 | cache = next_block_group(root, cache); |
| 2432 | } |
| 2433 | if (!cache) { |
| 2434 | if (last == 0) |
| 2435 | break; |
| 2436 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2437 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2438 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2439 | |
| 2440 | cache->dirty = 0; |
| 2441 | last = cache->key.objectid + cache->key.offset; |
| 2442 | |
| 2443 | err = write_one_cache_group(trans, root, path, cache); |
| 2444 | BUG_ON(err); |
| 2445 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2446 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2447 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2448 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2449 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2450 | } |
| 2451 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2452 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2453 | { |
| 2454 | struct btrfs_block_group_cache *block_group; |
| 2455 | int readonly = 0; |
| 2456 | |
| 2457 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2458 | if (!block_group || block_group->ro) |
| 2459 | readonly = 1; |
| 2460 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2461 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2462 | return readonly; |
| 2463 | } |
| 2464 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2465 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2466 | u64 total_bytes, u64 bytes_used, |
| 2467 | struct btrfs_space_info **space_info) |
| 2468 | { |
| 2469 | struct btrfs_space_info *found; |
| 2470 | |
| 2471 | found = __find_space_info(info, flags); |
| 2472 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2473 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2474 | found->total_bytes += total_bytes; |
| 2475 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2476 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2477 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2478 | *space_info = found; |
| 2479 | return 0; |
| 2480 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2481 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2482 | if (!found) |
| 2483 | return -ENOMEM; |
| 2484 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2485 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2486 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2487 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2488 | found->flags = flags; |
| 2489 | found->total_bytes = total_bytes; |
| 2490 | found->bytes_used = bytes_used; |
| 2491 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2492 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2493 | found->bytes_readonly = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2494 | found->bytes_delalloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2495 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2496 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2497 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2498 | list_add_rcu(&found->list, &info->space_info); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2499 | return 0; |
| 2500 | } |
| 2501 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2502 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2503 | { |
| 2504 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2505 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2506 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2507 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2508 | if (extra_flags) { |
| 2509 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2510 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2511 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2512 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2513 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2514 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2515 | } |
| 2516 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2517 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2518 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 2519 | { |
| 2520 | spin_lock(&cache->space_info->lock); |
| 2521 | spin_lock(&cache->lock); |
| 2522 | if (!cache->ro) { |
| 2523 | cache->space_info->bytes_readonly += cache->key.offset - |
| 2524 | btrfs_block_group_used(&cache->item); |
| 2525 | cache->ro = 1; |
| 2526 | } |
| 2527 | spin_unlock(&cache->lock); |
| 2528 | spin_unlock(&cache->space_info->lock); |
| 2529 | } |
| 2530 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2531 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2532 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2533 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2534 | |
| 2535 | if (num_devices == 1) |
| 2536 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2537 | if (num_devices < 4) |
| 2538 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2539 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2540 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2541 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2542 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2543 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2544 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2545 | |
| 2546 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2547 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2548 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2549 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2550 | |
| 2551 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2552 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2553 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2554 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2555 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2556 | return flags; |
| 2557 | } |
| 2558 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2559 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) |
| 2560 | { |
| 2561 | struct btrfs_fs_info *info = root->fs_info; |
| 2562 | u64 alloc_profile; |
| 2563 | |
| 2564 | if (data) { |
| 2565 | alloc_profile = info->avail_data_alloc_bits & |
| 2566 | info->data_alloc_profile; |
| 2567 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
| 2568 | } else if (root == root->fs_info->chunk_root) { |
| 2569 | alloc_profile = info->avail_system_alloc_bits & |
| 2570 | info->system_alloc_profile; |
| 2571 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
| 2572 | } else { |
| 2573 | alloc_profile = info->avail_metadata_alloc_bits & |
| 2574 | info->metadata_alloc_profile; |
| 2575 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
| 2576 | } |
| 2577 | |
| 2578 | return btrfs_reduce_alloc_profile(root, data); |
| 2579 | } |
| 2580 | |
| 2581 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 2582 | { |
| 2583 | u64 alloc_target; |
| 2584 | |
| 2585 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2586 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
| 2587 | alloc_target); |
| 2588 | } |
| 2589 | |
| 2590 | /* |
| 2591 | * for now this just makes sure we have at least 5% of our metadata space free |
| 2592 | * for use. |
| 2593 | */ |
| 2594 | int btrfs_check_metadata_free_space(struct btrfs_root *root) |
| 2595 | { |
| 2596 | struct btrfs_fs_info *info = root->fs_info; |
| 2597 | struct btrfs_space_info *meta_sinfo; |
| 2598 | u64 alloc_target, thresh; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2599 | int committed = 0, ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2600 | |
| 2601 | /* get the space info for where the metadata will live */ |
| 2602 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 2603 | meta_sinfo = __find_space_info(info, alloc_target); |
| 2604 | |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2605 | again: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2606 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2607 | if (!meta_sinfo->full) |
| 2608 | thresh = meta_sinfo->total_bytes * 80; |
| 2609 | else |
| 2610 | thresh = meta_sinfo->total_bytes * 95; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2611 | |
| 2612 | do_div(thresh, 100); |
| 2613 | |
| 2614 | if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 2615 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2616 | struct btrfs_trans_handle *trans; |
| 2617 | if (!meta_sinfo->full) { |
| 2618 | meta_sinfo->force_alloc = 1; |
| 2619 | spin_unlock(&meta_sinfo->lock); |
| 2620 | |
| 2621 | trans = btrfs_start_transaction(root, 1); |
| 2622 | if (!trans) |
| 2623 | return -ENOMEM; |
| 2624 | |
| 2625 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2626 | 2 * 1024 * 1024, alloc_target, 0); |
| 2627 | btrfs_end_transaction(trans, root); |
| 2628 | goto again; |
| 2629 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2630 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2631 | |
| 2632 | if (!committed) { |
| 2633 | committed = 1; |
| 2634 | trans = btrfs_join_transaction(root, 1); |
| 2635 | if (!trans) |
| 2636 | return -ENOMEM; |
| 2637 | ret = btrfs_commit_transaction(trans, root); |
| 2638 | if (ret) |
| 2639 | return ret; |
| 2640 | goto again; |
| 2641 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2642 | return -ENOSPC; |
| 2643 | } |
| 2644 | spin_unlock(&meta_sinfo->lock); |
| 2645 | |
| 2646 | return 0; |
| 2647 | } |
| 2648 | |
| 2649 | /* |
| 2650 | * This will check the space that the inode allocates from to make sure we have |
| 2651 | * enough space for bytes. |
| 2652 | */ |
| 2653 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, |
| 2654 | u64 bytes) |
| 2655 | { |
| 2656 | struct btrfs_space_info *data_sinfo; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2657 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2658 | |
| 2659 | /* make sure bytes are sectorsize aligned */ |
| 2660 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2661 | |
| 2662 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2663 | again: |
| 2664 | /* make sure we have enough space to handle the data first */ |
| 2665 | spin_lock(&data_sinfo->lock); |
| 2666 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - |
| 2667 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - |
| 2668 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - |
| 2669 | data_sinfo->bytes_may_use < bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2670 | struct btrfs_trans_handle *trans; |
| 2671 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2672 | /* |
| 2673 | * if we don't have enough free bytes in this space then we need |
| 2674 | * to alloc a new chunk. |
| 2675 | */ |
| 2676 | if (!data_sinfo->full) { |
| 2677 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2678 | |
| 2679 | data_sinfo->force_alloc = 1; |
| 2680 | spin_unlock(&data_sinfo->lock); |
| 2681 | |
| 2682 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2683 | trans = btrfs_start_transaction(root, 1); |
| 2684 | if (!trans) |
| 2685 | return -ENOMEM; |
| 2686 | |
| 2687 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2688 | bytes + 2 * 1024 * 1024, |
| 2689 | alloc_target, 0); |
| 2690 | btrfs_end_transaction(trans, root); |
| 2691 | if (ret) |
| 2692 | return ret; |
| 2693 | goto again; |
| 2694 | } |
| 2695 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 2696 | |
| 2697 | /* commit the current transaction and try again */ |
| 2698 | if (!committed) { |
| 2699 | committed = 1; |
| 2700 | trans = btrfs_join_transaction(root, 1); |
| 2701 | if (!trans) |
| 2702 | return -ENOMEM; |
| 2703 | ret = btrfs_commit_transaction(trans, root); |
| 2704 | if (ret) |
| 2705 | return ret; |
| 2706 | goto again; |
| 2707 | } |
| 2708 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2709 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" |
| 2710 | ", %llu bytes_used, %llu bytes_reserved, " |
Hu Tao | 68f5a38 | 2009-07-02 13:55:45 -0400 | [diff] [blame] | 2711 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use " |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 2712 | "%llu total\n", (unsigned long long)bytes, |
| 2713 | (unsigned long long)data_sinfo->bytes_delalloc, |
| 2714 | (unsigned long long)data_sinfo->bytes_used, |
| 2715 | (unsigned long long)data_sinfo->bytes_reserved, |
| 2716 | (unsigned long long)data_sinfo->bytes_pinned, |
| 2717 | (unsigned long long)data_sinfo->bytes_readonly, |
| 2718 | (unsigned long long)data_sinfo->bytes_may_use, |
| 2719 | (unsigned long long)data_sinfo->total_bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2720 | return -ENOSPC; |
| 2721 | } |
| 2722 | data_sinfo->bytes_may_use += bytes; |
| 2723 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 2724 | spin_unlock(&data_sinfo->lock); |
| 2725 | |
| 2726 | return btrfs_check_metadata_free_space(root); |
| 2727 | } |
| 2728 | |
| 2729 | /* |
| 2730 | * if there was an error for whatever reason after calling |
| 2731 | * btrfs_check_data_free_space, call this so we can cleanup the counters. |
| 2732 | */ |
| 2733 | void btrfs_free_reserved_data_space(struct btrfs_root *root, |
| 2734 | struct inode *inode, u64 bytes) |
| 2735 | { |
| 2736 | struct btrfs_space_info *data_sinfo; |
| 2737 | |
| 2738 | /* make sure bytes are sectorsize aligned */ |
| 2739 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2740 | |
| 2741 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2742 | spin_lock(&data_sinfo->lock); |
| 2743 | data_sinfo->bytes_may_use -= bytes; |
| 2744 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 2745 | spin_unlock(&data_sinfo->lock); |
| 2746 | } |
| 2747 | |
| 2748 | /* called when we are adding a delalloc extent to the inode's io_tree */ |
| 2749 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, |
| 2750 | u64 bytes) |
| 2751 | { |
| 2752 | struct btrfs_space_info *data_sinfo; |
| 2753 | |
| 2754 | /* get the space info for where this inode will be storing its data */ |
| 2755 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2756 | |
| 2757 | /* make sure we have enough space to handle the data first */ |
| 2758 | spin_lock(&data_sinfo->lock); |
| 2759 | data_sinfo->bytes_delalloc += bytes; |
| 2760 | |
| 2761 | /* |
| 2762 | * we are adding a delalloc extent without calling |
| 2763 | * btrfs_check_data_free_space first. This happens on a weird |
| 2764 | * writepage condition, but shouldn't hurt our accounting |
| 2765 | */ |
| 2766 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { |
| 2767 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; |
| 2768 | BTRFS_I(inode)->reserved_bytes = 0; |
| 2769 | } else { |
| 2770 | data_sinfo->bytes_may_use -= bytes; |
| 2771 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 2772 | } |
| 2773 | |
| 2774 | spin_unlock(&data_sinfo->lock); |
| 2775 | } |
| 2776 | |
| 2777 | /* called when we are clearing an delalloc extent from the inode's io_tree */ |
| 2778 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, |
| 2779 | u64 bytes) |
| 2780 | { |
| 2781 | struct btrfs_space_info *info; |
| 2782 | |
| 2783 | info = BTRFS_I(inode)->space_info; |
| 2784 | |
| 2785 | spin_lock(&info->lock); |
| 2786 | info->bytes_delalloc -= bytes; |
| 2787 | spin_unlock(&info->lock); |
| 2788 | } |
| 2789 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2790 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 2791 | { |
| 2792 | struct list_head *head = &info->space_info; |
| 2793 | struct btrfs_space_info *found; |
| 2794 | |
| 2795 | rcu_read_lock(); |
| 2796 | list_for_each_entry_rcu(found, head, list) { |
| 2797 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2798 | found->force_alloc = 1; |
| 2799 | } |
| 2800 | rcu_read_unlock(); |
| 2801 | } |
| 2802 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2803 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 2804 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2805 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2806 | { |
| 2807 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2808 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2809 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2810 | int ret = 0; |
| 2811 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2812 | mutex_lock(&fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2813 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2814 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2815 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2816 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2817 | if (!space_info) { |
| 2818 | ret = update_space_info(extent_root->fs_info, flags, |
| 2819 | 0, 0, &space_info); |
| 2820 | BUG_ON(ret); |
| 2821 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2822 | BUG_ON(!space_info); |
| 2823 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2824 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2825 | if (space_info->force_alloc) { |
| 2826 | force = 1; |
| 2827 | space_info->force_alloc = 0; |
| 2828 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2829 | if (space_info->full) { |
| 2830 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2831 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2832 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2833 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2834 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
| 2835 | thresh = div_factor(thresh, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2836 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2837 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2838 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 2839 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2840 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2841 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2842 | spin_unlock(&space_info->lock); |
| 2843 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 2844 | /* |
| 2845 | * if we're doing a data chunk, go ahead and make sure that |
| 2846 | * we keep a reasonable number of metadata chunks allocated in the |
| 2847 | * FS as well. |
| 2848 | */ |
| 2849 | if (flags & BTRFS_BLOCK_GROUP_DATA) { |
| 2850 | fs_info->data_chunk_allocations++; |
| 2851 | if (!(fs_info->data_chunk_allocations % |
| 2852 | fs_info->metadata_ratio)) |
| 2853 | force_metadata_allocation(fs_info); |
| 2854 | } |
| 2855 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2856 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2857 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2858 | space_info->full = 1; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2859 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2860 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2861 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2862 | } |
| 2863 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2864 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 2865 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2866 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2867 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2868 | { |
| 2869 | struct btrfs_block_group_cache *cache; |
| 2870 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2871 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2872 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2873 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 2874 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2875 | /* block accounting for super block */ |
| 2876 | spin_lock(&info->delalloc_lock); |
| 2877 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 2878 | if (alloc) |
| 2879 | old_val += num_bytes; |
| 2880 | else |
| 2881 | old_val -= num_bytes; |
| 2882 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
| 2883 | |
| 2884 | /* block accounting for root item */ |
| 2885 | old_val = btrfs_root_used(&root->root_item); |
| 2886 | if (alloc) |
| 2887 | old_val += num_bytes; |
| 2888 | else |
| 2889 | old_val -= num_bytes; |
| 2890 | btrfs_set_root_used(&root->root_item, old_val); |
| 2891 | spin_unlock(&info->delalloc_lock); |
| 2892 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2893 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2894 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2895 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2896 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2897 | byte_in_group = bytenr - cache->key.objectid; |
| 2898 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2899 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2900 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2901 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2902 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2903 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2904 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2905 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2906 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2907 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 2908 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2909 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2910 | btrfs_set_block_group_used(&cache->item, old_val); |
| 2911 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2912 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2913 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2914 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2915 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2916 | if (cache->ro) |
| 2917 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2918 | btrfs_set_block_group_used(&cache->item, old_val); |
| 2919 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2920 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2921 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2922 | int ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2923 | |
| 2924 | ret = btrfs_discard_extent(root, bytenr, |
| 2925 | num_bytes); |
| 2926 | WARN_ON(ret); |
| 2927 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2928 | ret = btrfs_add_free_space(cache, bytenr, |
| 2929 | num_bytes); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2930 | WARN_ON(ret); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2931 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2932 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2933 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2934 | total -= num_bytes; |
| 2935 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2936 | } |
| 2937 | return 0; |
| 2938 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2939 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2940 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 2941 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2942 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2943 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2944 | |
| 2945 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 2946 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2947 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2948 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2949 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2950 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2951 | |
| 2952 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2953 | } |
| 2954 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2955 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2956 | u64 bytenr, u64 num, int pin) |
| 2957 | { |
| 2958 | u64 len; |
| 2959 | struct btrfs_block_group_cache *cache; |
| 2960 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2961 | |
| 2962 | if (pin) { |
| 2963 | set_extent_dirty(&fs_info->pinned_extents, |
| 2964 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2965 | } else { |
| 2966 | clear_extent_dirty(&fs_info->pinned_extents, |
| 2967 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2968 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2969 | |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2970 | while (num > 0) { |
| 2971 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2972 | BUG_ON(!cache); |
| 2973 | len = min(num, cache->key.offset - |
| 2974 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2975 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2976 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2977 | spin_lock(&cache->lock); |
| 2978 | cache->pinned += len; |
| 2979 | cache->space_info->bytes_pinned += len; |
| 2980 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2981 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2982 | fs_info->total_pinned += len; |
| 2983 | } else { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2984 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2985 | spin_lock(&cache->lock); |
| 2986 | cache->pinned -= len; |
| 2987 | cache->space_info->bytes_pinned -= len; |
| 2988 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2989 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2990 | fs_info->total_pinned -= len; |
Josef Bacik | 07103a3 | 2008-11-19 15:17:55 -0500 | [diff] [blame] | 2991 | if (cache->cached) |
| 2992 | btrfs_add_free_space(cache, bytenr, len); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2993 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2994 | btrfs_put_block_group(cache); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2995 | bytenr += len; |
| 2996 | num -= len; |
| 2997 | } |
| 2998 | return 0; |
| 2999 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3000 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3001 | static int update_reserved_extents(struct btrfs_root *root, |
| 3002 | u64 bytenr, u64 num, int reserve) |
| 3003 | { |
| 3004 | u64 len; |
| 3005 | struct btrfs_block_group_cache *cache; |
| 3006 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3007 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3008 | while (num > 0) { |
| 3009 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 3010 | BUG_ON(!cache); |
| 3011 | len = min(num, cache->key.offset - |
| 3012 | (bytenr - cache->key.objectid)); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3013 | |
| 3014 | spin_lock(&cache->space_info->lock); |
| 3015 | spin_lock(&cache->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3016 | if (reserve) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3017 | cache->reserved += len; |
| 3018 | cache->space_info->bytes_reserved += len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3019 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3020 | cache->reserved -= len; |
| 3021 | cache->space_info->bytes_reserved -= len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3022 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3023 | spin_unlock(&cache->lock); |
| 3024 | spin_unlock(&cache->space_info->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3025 | btrfs_put_block_group(cache); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3026 | bytenr += len; |
| 3027 | num -= len; |
| 3028 | } |
| 3029 | return 0; |
| 3030 | } |
| 3031 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3032 | int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3033 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3034 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3035 | u64 start; |
| 3036 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3037 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3038 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3039 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3040 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3041 | ret = find_first_extent_bit(pinned_extents, last, |
| 3042 | &start, &end, EXTENT_DIRTY); |
| 3043 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3044 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3045 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 3046 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3047 | } |
| 3048 | return 0; |
| 3049 | } |
| 3050 | |
| 3051 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 3052 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3053 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3054 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3055 | u64 start; |
| 3056 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3057 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3058 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3059 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3060 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 3061 | EXTENT_DIRTY); |
| 3062 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3063 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3064 | |
| 3065 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 3066 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3067 | /* unlocks the pinned mutex */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3068 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3069 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3070 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3071 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3072 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3073 | return ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3074 | } |
| 3075 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3076 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 3077 | struct btrfs_root *root, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3078 | struct btrfs_path *path, |
| 3079 | u64 bytenr, u64 num_bytes, int is_data, |
| 3080 | struct extent_buffer **must_clean) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3081 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3082 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3083 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3084 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3085 | if (is_data) |
| 3086 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3087 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3088 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 3089 | if (!buf) |
| 3090 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 3091 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3092 | /* we can reuse a block if it hasn't been written |
| 3093 | * and it is from this transaction. We can't |
| 3094 | * reuse anything from the tree log root because |
| 3095 | * it has tiny sub-transactions. |
| 3096 | */ |
| 3097 | if (btrfs_buffer_uptodate(buf, 0) && |
| 3098 | btrfs_try_tree_lock(buf)) { |
| 3099 | u64 header_owner = btrfs_header_owner(buf); |
| 3100 | u64 header_transid = btrfs_header_generation(buf); |
| 3101 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
| 3102 | header_transid == trans->transid && |
| 3103 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3104 | *must_clean = buf; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3105 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 3106 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3107 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 3108 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3109 | free_extent_buffer(buf); |
| 3110 | pinit: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3111 | btrfs_set_path_blocking(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3112 | /* unlocks the pinned mutex */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3113 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 3114 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 3115 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 3116 | return 0; |
| 3117 | } |
| 3118 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3119 | |
| 3120 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 3121 | struct btrfs_root *root, |
| 3122 | u64 bytenr, u64 num_bytes, u64 parent, |
| 3123 | u64 root_objectid, u64 owner_objectid, |
| 3124 | u64 owner_offset, int refs_to_drop, |
| 3125 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3126 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3127 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3128 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3129 | struct btrfs_fs_info *info = root->fs_info; |
| 3130 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3131 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3132 | struct btrfs_extent_item *ei; |
| 3133 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3134 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3135 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3136 | int extent_slot = 0; |
| 3137 | int found_extent = 0; |
| 3138 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3139 | u32 item_size; |
| 3140 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 3141 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3142 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3143 | if (!path) |
| 3144 | return -ENOMEM; |
| 3145 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 3146 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3147 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3148 | |
| 3149 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 3150 | BUG_ON(!is_data && refs_to_drop != 1); |
| 3151 | |
| 3152 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 3153 | bytenr, num_bytes, parent, |
| 3154 | root_objectid, owner_objectid, |
| 3155 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3156 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3157 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3158 | while (extent_slot >= 0) { |
| 3159 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3160 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3161 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3162 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3163 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 3164 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3165 | found_extent = 1; |
| 3166 | break; |
| 3167 | } |
| 3168 | if (path->slots[0] - extent_slot > 5) |
| 3169 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3170 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3171 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3172 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3173 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 3174 | if (found_extent && item_size < sizeof(*ei)) |
| 3175 | found_extent = 0; |
| 3176 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3177 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3178 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3179 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3180 | NULL, refs_to_drop, |
| 3181 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3182 | BUG_ON(ret); |
| 3183 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3184 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3185 | |
| 3186 | key.objectid = bytenr; |
| 3187 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3188 | key.offset = num_bytes; |
| 3189 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3190 | ret = btrfs_search_slot(trans, extent_root, |
| 3191 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3192 | if (ret) { |
| 3193 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3194 | ", was looking for %llu\n", ret, |
| 3195 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3196 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3197 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3198 | BUG_ON(ret); |
| 3199 | extent_slot = path->slots[0]; |
| 3200 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3201 | } else { |
| 3202 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3203 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3204 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3205 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3206 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3207 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3208 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3209 | (unsigned long long)owner_objectid, |
| 3210 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3211 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3212 | |
| 3213 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3214 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3215 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 3216 | if (item_size < sizeof(*ei)) { |
| 3217 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 3218 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 3219 | owner_objectid, 0); |
| 3220 | BUG_ON(ret < 0); |
| 3221 | |
| 3222 | btrfs_release_path(extent_root, path); |
| 3223 | path->leave_spinning = 1; |
| 3224 | |
| 3225 | key.objectid = bytenr; |
| 3226 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 3227 | key.offset = num_bytes; |
| 3228 | |
| 3229 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 3230 | -1, 1); |
| 3231 | if (ret) { |
| 3232 | printk(KERN_ERR "umm, got %d back from search" |
| 3233 | ", was looking for %llu\n", ret, |
| 3234 | (unsigned long long)bytenr); |
| 3235 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 3236 | } |
| 3237 | BUG_ON(ret); |
| 3238 | extent_slot = path->slots[0]; |
| 3239 | leaf = path->nodes[0]; |
| 3240 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 3241 | } |
| 3242 | #endif |
| 3243 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3244 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 3245 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3246 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 3247 | struct btrfs_tree_block_info *bi; |
| 3248 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 3249 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 3250 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3251 | } |
| 3252 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3253 | refs = btrfs_extent_refs(leaf, ei); |
| 3254 | BUG_ON(refs < refs_to_drop); |
| 3255 | refs -= refs_to_drop; |
| 3256 | |
| 3257 | if (refs > 0) { |
| 3258 | if (extent_op) |
| 3259 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 3260 | /* |
| 3261 | * In the case of inline back ref, reference count will |
| 3262 | * be updated by remove_extent_backref |
| 3263 | */ |
| 3264 | if (iref) { |
| 3265 | BUG_ON(!found_extent); |
| 3266 | } else { |
| 3267 | btrfs_set_extent_refs(leaf, ei, refs); |
| 3268 | btrfs_mark_buffer_dirty(leaf); |
| 3269 | } |
| 3270 | if (found_extent) { |
| 3271 | ret = remove_extent_backref(trans, extent_root, path, |
| 3272 | iref, refs_to_drop, |
| 3273 | is_data); |
| 3274 | BUG_ON(ret); |
| 3275 | } |
| 3276 | } else { |
| 3277 | int mark_free = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3278 | struct extent_buffer *must_clean = NULL; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3279 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3280 | if (found_extent) { |
| 3281 | BUG_ON(is_data && refs_to_drop != |
| 3282 | extent_data_ref_count(root, path, iref)); |
| 3283 | if (iref) { |
| 3284 | BUG_ON(path->slots[0] != extent_slot); |
| 3285 | } else { |
| 3286 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 3287 | path->slots[0] = extent_slot; |
| 3288 | num_to_del = 2; |
| 3289 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 3290 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3291 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3292 | ret = pin_down_bytes(trans, root, path, bytenr, |
| 3293 | num_bytes, is_data, &must_clean); |
| 3294 | if (ret > 0) |
| 3295 | mark_free = 1; |
| 3296 | BUG_ON(ret < 0); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3297 | /* |
| 3298 | * it is going to be very rare for someone to be waiting |
| 3299 | * on the block we're freeing. del_items might need to |
| 3300 | * schedule, so rather than get fancy, just force it |
| 3301 | * to blocking here |
| 3302 | */ |
| 3303 | if (must_clean) |
| 3304 | btrfs_set_lock_blocking(must_clean); |
| 3305 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 3306 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 3307 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3308 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3309 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 3310 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3311 | if (must_clean) { |
| 3312 | clean_tree_block(NULL, root, must_clean); |
| 3313 | btrfs_tree_unlock(must_clean); |
| 3314 | free_extent_buffer(must_clean); |
| 3315 | } |
| 3316 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3317 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3318 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 3319 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 3320 | } else { |
| 3321 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 3322 | bytenr >> PAGE_CACHE_SHIFT, |
| 3323 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 3324 | } |
| 3325 | |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 3326 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
| 3327 | mark_free); |
| 3328 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3329 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3330 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3331 | return ret; |
| 3332 | } |
| 3333 | |
| 3334 | /* |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3335 | * when we free an extent, it is possible (and likely) that we free the last |
| 3336 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 3337 | * a given extent, and if there are no other delayed refs to be processed, it |
| 3338 | * removes it from the tree. |
| 3339 | */ |
| 3340 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 3341 | struct btrfs_root *root, u64 bytenr) |
| 3342 | { |
| 3343 | struct btrfs_delayed_ref_head *head; |
| 3344 | struct btrfs_delayed_ref_root *delayed_refs; |
| 3345 | struct btrfs_delayed_ref_node *ref; |
| 3346 | struct rb_node *node; |
| 3347 | int ret; |
| 3348 | |
| 3349 | delayed_refs = &trans->transaction->delayed_refs; |
| 3350 | spin_lock(&delayed_refs->lock); |
| 3351 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 3352 | if (!head) |
| 3353 | goto out; |
| 3354 | |
| 3355 | node = rb_prev(&head->node.rb_node); |
| 3356 | if (!node) |
| 3357 | goto out; |
| 3358 | |
| 3359 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 3360 | |
| 3361 | /* there are still entries for this ref, we can't drop it */ |
| 3362 | if (ref->bytenr == bytenr) |
| 3363 | goto out; |
| 3364 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3365 | if (head->extent_op) { |
| 3366 | if (!head->must_insert_reserved) |
| 3367 | goto out; |
| 3368 | kfree(head->extent_op); |
| 3369 | head->extent_op = NULL; |
| 3370 | } |
| 3371 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3372 | /* |
| 3373 | * waiting for the lock here would deadlock. If someone else has it |
| 3374 | * locked they are already in the process of dropping it anyway |
| 3375 | */ |
| 3376 | if (!mutex_trylock(&head->mutex)) |
| 3377 | goto out; |
| 3378 | |
| 3379 | /* |
| 3380 | * at this point we have a head with no other entries. Go |
| 3381 | * ahead and process it. |
| 3382 | */ |
| 3383 | head->node.in_tree = 0; |
| 3384 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3385 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3386 | delayed_refs->num_entries--; |
| 3387 | |
| 3388 | /* |
| 3389 | * we don't take a ref on the node because we're removing it from the |
| 3390 | * tree, so we just steal the ref the tree was holding. |
| 3391 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3392 | delayed_refs->num_heads--; |
| 3393 | if (list_empty(&head->cluster)) |
| 3394 | delayed_refs->num_heads_ready--; |
| 3395 | |
| 3396 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3397 | spin_unlock(&delayed_refs->lock); |
| 3398 | |
| 3399 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3400 | &head->node, head->extent_op, |
| 3401 | head->must_insert_reserved); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3402 | BUG_ON(ret); |
| 3403 | btrfs_put_delayed_ref(&head->node); |
| 3404 | return 0; |
| 3405 | out: |
| 3406 | spin_unlock(&delayed_refs->lock); |
| 3407 | return 0; |
| 3408 | } |
| 3409 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3410 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3411 | struct btrfs_root *root, |
| 3412 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3413 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3414 | { |
| 3415 | int ret; |
| 3416 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3417 | /* |
| 3418 | * tree log blocks never actually go into the extent allocation |
| 3419 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3420 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3421 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 3422 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3423 | /* unlocks the pinned mutex */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3424 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3425 | update_reserved_extents(root, bytenr, num_bytes, 0); |
| 3426 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3427 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 3428 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 3429 | parent, root_objectid, (int)owner, |
| 3430 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 3431 | BUG_ON(ret); |
| 3432 | ret = check_ref_cleanup(trans, root, bytenr); |
| 3433 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3434 | } else { |
| 3435 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 3436 | parent, root_objectid, owner, |
| 3437 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 3438 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3439 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3440 | return ret; |
| 3441 | } |
| 3442 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 3443 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 3444 | { |
| 3445 | u64 mask = ((u64)root->stripesize - 1); |
| 3446 | u64 ret = (val + mask) & ~mask; |
| 3447 | return ret; |
| 3448 | } |
| 3449 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3450 | /* |
| 3451 | * walks the btree of allocated extents and find a hole of a given size. |
| 3452 | * The key ins is changed to record the hole: |
| 3453 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3454 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3455 | * ins->offset == number of blocks |
| 3456 | * Any available blocks before search_start are skipped. |
| 3457 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3458 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3459 | struct btrfs_root *orig_root, |
| 3460 | u64 num_bytes, u64 empty_size, |
| 3461 | u64 search_start, u64 search_end, |
| 3462 | u64 hint_byte, struct btrfs_key *ins, |
| 3463 | u64 exclude_start, u64 exclude_nr, |
| 3464 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3465 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3466 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3467 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3468 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3469 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3470 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3471 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3472 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3473 | int last_ptr_loop = 0; |
| 3474 | int loop = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3475 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3476 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 3477 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3478 | ins->objectid = 0; |
| 3479 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 3480 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3481 | space_info = __find_space_info(root->fs_info, data); |
| 3482 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3483 | if (orig_root->ref_cows || empty_size) |
| 3484 | allowed_chunk_alloc = 1; |
| 3485 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3486 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3487 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 3488 | if (!btrfs_test_opt(root, SSD)) |
| 3489 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3490 | } |
| 3491 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3492 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 3493 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 3494 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3495 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3496 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3497 | spin_lock(&last_ptr->lock); |
| 3498 | if (last_ptr->block_group) |
| 3499 | hint_byte = last_ptr->window_start; |
| 3500 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3501 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3502 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3503 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3504 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3505 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3506 | if (!last_ptr) { |
| 3507 | empty_cluster = 0; |
| 3508 | loop = 1; |
| 3509 | } |
| 3510 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3511 | if (search_start == hint_byte) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3512 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 3513 | search_start); |
| 3514 | if (block_group && block_group_bits(block_group, data)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3515 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3516 | if (list_empty(&block_group->list) || |
| 3517 | block_group->ro) { |
| 3518 | /* |
| 3519 | * someone is removing this block group, |
| 3520 | * we can't jump into the have_block_group |
| 3521 | * target because our list pointers are not |
| 3522 | * valid |
| 3523 | */ |
| 3524 | btrfs_put_block_group(block_group); |
| 3525 | up_read(&space_info->groups_sem); |
| 3526 | } else |
| 3527 | goto have_block_group; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3528 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3529 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3530 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3531 | } |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3532 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3533 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3534 | down_read(&space_info->groups_sem); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3535 | list_for_each_entry(block_group, &space_info->block_groups, list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3536 | u64 offset; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 3537 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3538 | atomic_inc(&block_group->count); |
| 3539 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3540 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3541 | have_block_group: |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3542 | if (unlikely(!block_group->cached)) { |
| 3543 | mutex_lock(&block_group->cache_mutex); |
| 3544 | ret = cache_block_group(root, block_group); |
| 3545 | mutex_unlock(&block_group->cache_mutex); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3546 | if (ret) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3547 | btrfs_put_block_group(block_group); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3548 | break; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3549 | } |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3550 | } |
| 3551 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3552 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3553 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3554 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3555 | if (last_ptr) { |
| 3556 | /* |
| 3557 | * the refill lock keeps out other |
| 3558 | * people trying to start a new cluster |
| 3559 | */ |
| 3560 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3561 | if (last_ptr->block_group && |
| 3562 | (last_ptr->block_group->ro || |
| 3563 | !block_group_bits(last_ptr->block_group, data))) { |
| 3564 | offset = 0; |
| 3565 | goto refill_cluster; |
| 3566 | } |
| 3567 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3568 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 3569 | num_bytes, search_start); |
| 3570 | if (offset) { |
| 3571 | /* we have a block, we're done */ |
| 3572 | spin_unlock(&last_ptr->refill_lock); |
| 3573 | goto checks; |
| 3574 | } |
| 3575 | |
| 3576 | spin_lock(&last_ptr->lock); |
| 3577 | /* |
| 3578 | * whoops, this cluster doesn't actually point to |
| 3579 | * this block group. Get a ref on the block |
| 3580 | * group is does point to and try again |
| 3581 | */ |
| 3582 | if (!last_ptr_loop && last_ptr->block_group && |
| 3583 | last_ptr->block_group != block_group) { |
| 3584 | |
| 3585 | btrfs_put_block_group(block_group); |
| 3586 | block_group = last_ptr->block_group; |
| 3587 | atomic_inc(&block_group->count); |
| 3588 | spin_unlock(&last_ptr->lock); |
| 3589 | spin_unlock(&last_ptr->refill_lock); |
| 3590 | |
| 3591 | last_ptr_loop = 1; |
| 3592 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3593 | /* |
| 3594 | * we know this block group is properly |
| 3595 | * in the list because |
| 3596 | * btrfs_remove_block_group, drops the |
| 3597 | * cluster before it removes the block |
| 3598 | * group from the list |
| 3599 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3600 | goto have_block_group; |
| 3601 | } |
| 3602 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 3603 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3604 | /* |
| 3605 | * this cluster didn't work out, free it and |
| 3606 | * start over |
| 3607 | */ |
| 3608 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 3609 | |
| 3610 | last_ptr_loop = 0; |
| 3611 | |
| 3612 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 3613 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3614 | block_group, last_ptr, |
| 3615 | offset, num_bytes, |
| 3616 | empty_cluster + empty_size); |
| 3617 | if (ret == 0) { |
| 3618 | /* |
| 3619 | * now pull our allocation out of this |
| 3620 | * cluster |
| 3621 | */ |
| 3622 | offset = btrfs_alloc_from_cluster(block_group, |
| 3623 | last_ptr, num_bytes, |
| 3624 | search_start); |
| 3625 | if (offset) { |
| 3626 | /* we found one, proceed */ |
| 3627 | spin_unlock(&last_ptr->refill_lock); |
| 3628 | goto checks; |
| 3629 | } |
| 3630 | } |
| 3631 | /* |
| 3632 | * at this point we either didn't find a cluster |
| 3633 | * or we weren't able to allocate a block from our |
| 3634 | * cluster. Free the cluster we've been trying |
| 3635 | * to use, and go to the next block group |
| 3636 | */ |
| 3637 | if (loop < 2) { |
| 3638 | btrfs_return_cluster_to_free_space(NULL, |
| 3639 | last_ptr); |
| 3640 | spin_unlock(&last_ptr->refill_lock); |
| 3641 | goto loop; |
| 3642 | } |
| 3643 | spin_unlock(&last_ptr->refill_lock); |
| 3644 | } |
| 3645 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3646 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 3647 | num_bytes, empty_size); |
| 3648 | if (!offset) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3649 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3650 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3651 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3652 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3653 | if (search_start + num_bytes >= search_end) { |
| 3654 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3655 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3656 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3657 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3658 | /* move on to the next group */ |
| 3659 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3660 | block_group->key.objectid + block_group->key.offset) { |
| 3661 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3662 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3663 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3664 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3665 | if (exclude_nr > 0 && |
| 3666 | (search_start + num_bytes > exclude_start && |
| 3667 | search_start < exclude_start + exclude_nr)) { |
| 3668 | search_start = exclude_start + exclude_nr; |
| 3669 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3670 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3671 | /* |
| 3672 | * if search_start is still in this block group |
| 3673 | * then we just re-search this block group |
| 3674 | */ |
| 3675 | if (search_start >= block_group->key.objectid && |
| 3676 | search_start < (block_group->key.objectid + |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3677 | block_group->key.offset)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3678 | goto have_block_group; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3679 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3680 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3681 | |
| 3682 | ins->objectid = search_start; |
| 3683 | ins->offset = num_bytes; |
| 3684 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 3685 | if (offset < search_start) |
| 3686 | btrfs_add_free_space(block_group, offset, |
| 3687 | search_start - offset); |
| 3688 | BUG_ON(offset > search_start); |
| 3689 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3690 | /* we are all good, lets return */ |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3691 | break; |
| 3692 | loop: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3693 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3694 | } |
| 3695 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3696 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3697 | /* loop == 0, try to find a clustered alloc in every block group |
| 3698 | * loop == 1, try again after forcing a chunk allocation |
| 3699 | * loop == 2, set empty_size and empty_cluster to 0 and try again |
| 3700 | */ |
| 3701 | if (!ins->objectid && loop < 3 && |
| 3702 | (empty_size || empty_cluster || allowed_chunk_alloc)) { |
| 3703 | if (loop >= 2) { |
| 3704 | empty_size = 0; |
| 3705 | empty_cluster = 0; |
| 3706 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3707 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3708 | if (allowed_chunk_alloc) { |
| 3709 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 3710 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3711 | allowed_chunk_alloc = 0; |
| 3712 | } else { |
| 3713 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3714 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3715 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3716 | if (loop < 3) { |
| 3717 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3718 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3719 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3720 | ret = -ENOSPC; |
| 3721 | } else if (!ins->objectid) { |
| 3722 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 3723 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3724 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3725 | /* we found what we needed */ |
| 3726 | if (ins->objectid) { |
| 3727 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3728 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3729 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3730 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 3731 | ret = 0; |
| 3732 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3733 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 3734 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3735 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3736 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3737 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 3738 | { |
| 3739 | struct btrfs_block_group_cache *cache; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3740 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3741 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 3742 | (unsigned long long)(info->total_bytes - info->bytes_used - |
| 3743 | info->bytes_pinned - info->bytes_reserved), |
| 3744 | (info->full) ? "" : "not "); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3745 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 3746 | " may_use=%llu, used=%llu\n", |
| 3747 | (unsigned long long)info->total_bytes, |
| 3748 | (unsigned long long)info->bytes_pinned, |
| 3749 | (unsigned long long)info->bytes_delalloc, |
| 3750 | (unsigned long long)info->bytes_may_use, |
| 3751 | (unsigned long long)info->bytes_used); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3752 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3753 | down_read(&info->groups_sem); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3754 | list_for_each_entry(cache, &info->block_groups, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3755 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3756 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 3757 | "%llu pinned %llu reserved\n", |
| 3758 | (unsigned long long)cache->key.objectid, |
| 3759 | (unsigned long long)cache->key.offset, |
| 3760 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 3761 | (unsigned long long)cache->pinned, |
| 3762 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3763 | btrfs_dump_free_space(cache, bytes); |
| 3764 | spin_unlock(&cache->lock); |
| 3765 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3766 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3767 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3768 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3769 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3770 | struct btrfs_root *root, |
| 3771 | u64 num_bytes, u64 min_alloc_size, |
| 3772 | u64 empty_size, u64 hint_byte, |
| 3773 | u64 search_end, struct btrfs_key *ins, |
| 3774 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3775 | { |
| 3776 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 3777 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3778 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3779 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3780 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3781 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3782 | /* |
| 3783 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 3784 | * is not called recursively on allocations |
| 3785 | */ |
| 3786 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3787 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3788 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3789 | 2 * 1024 * 1024, |
| 3790 | BTRFS_BLOCK_GROUP_METADATA | |
| 3791 | (info->metadata_alloc_profile & |
| 3792 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3793 | } |
| 3794 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3795 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3796 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3797 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3798 | WARN_ON(num_bytes < root->sectorsize); |
| 3799 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 3800 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3801 | trans->alloc_exclude_start, |
| 3802 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3803 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3804 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 3805 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3806 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3807 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3808 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3809 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3810 | goto again; |
| 3811 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3812 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3813 | struct btrfs_space_info *sinfo; |
| 3814 | |
| 3815 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3816 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 3817 | "wanted %llu\n", (unsigned long long)data, |
| 3818 | (unsigned long long)num_bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3819 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3820 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3821 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3822 | |
| 3823 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3824 | } |
| 3825 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3826 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 3827 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3828 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3829 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3830 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3831 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 3832 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3833 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 3834 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3835 | return -ENOSPC; |
| 3836 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3837 | |
| 3838 | ret = btrfs_discard_extent(root, start, len); |
| 3839 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3840 | btrfs_add_free_space(cache, start, len); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3841 | btrfs_put_block_group(cache); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3842 | update_reserved_extents(root, start, len, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3843 | |
| 3844 | return ret; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3845 | } |
| 3846 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3847 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3848 | struct btrfs_root *root, |
| 3849 | u64 num_bytes, u64 min_alloc_size, |
| 3850 | u64 empty_size, u64 hint_byte, |
| 3851 | u64 search_end, struct btrfs_key *ins, |
| 3852 | u64 data) |
| 3853 | { |
| 3854 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3855 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 3856 | empty_size, hint_byte, search_end, ins, |
| 3857 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3858 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3859 | return ret; |
| 3860 | } |
| 3861 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3862 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 3863 | struct btrfs_root *root, |
| 3864 | u64 parent, u64 root_objectid, |
| 3865 | u64 flags, u64 owner, u64 offset, |
| 3866 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3867 | { |
| 3868 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3869 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3870 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3871 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3872 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3873 | struct extent_buffer *leaf; |
| 3874 | int type; |
| 3875 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 3876 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3877 | if (parent > 0) |
| 3878 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 3879 | else |
| 3880 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3881 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3882 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3883 | |
| 3884 | path = btrfs_alloc_path(); |
| 3885 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3886 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 3887 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3888 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 3889 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3890 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3891 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3892 | leaf = path->nodes[0]; |
| 3893 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3894 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3895 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 3896 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 3897 | btrfs_set_extent_flags(leaf, extent_item, |
| 3898 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3899 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3900 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 3901 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 3902 | if (parent > 0) { |
| 3903 | struct btrfs_shared_data_ref *ref; |
| 3904 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 3905 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 3906 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 3907 | } else { |
| 3908 | struct btrfs_extent_data_ref *ref; |
| 3909 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 3910 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 3911 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 3912 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 3913 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 3914 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3915 | |
| 3916 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3917 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3918 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3919 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 3920 | 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 3921 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3922 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 3923 | "%llu\n", (unsigned long long)ins->objectid, |
| 3924 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 3925 | BUG(); |
| 3926 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3927 | return ret; |
| 3928 | } |
| 3929 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3930 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 3931 | struct btrfs_root *root, |
| 3932 | u64 parent, u64 root_objectid, |
| 3933 | u64 flags, struct btrfs_disk_key *key, |
| 3934 | int level, struct btrfs_key *ins) |
| 3935 | { |
| 3936 | int ret; |
| 3937 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3938 | struct btrfs_extent_item *extent_item; |
| 3939 | struct btrfs_tree_block_info *block_info; |
| 3940 | struct btrfs_extent_inline_ref *iref; |
| 3941 | struct btrfs_path *path; |
| 3942 | struct extent_buffer *leaf; |
| 3943 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 3944 | |
| 3945 | path = btrfs_alloc_path(); |
| 3946 | BUG_ON(!path); |
| 3947 | |
| 3948 | path->leave_spinning = 1; |
| 3949 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 3950 | ins, size); |
| 3951 | BUG_ON(ret); |
| 3952 | |
| 3953 | leaf = path->nodes[0]; |
| 3954 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 3955 | struct btrfs_extent_item); |
| 3956 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 3957 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 3958 | btrfs_set_extent_flags(leaf, extent_item, |
| 3959 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 3960 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 3961 | |
| 3962 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 3963 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 3964 | |
| 3965 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 3966 | if (parent > 0) { |
| 3967 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 3968 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 3969 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 3970 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 3971 | } else { |
| 3972 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 3973 | BTRFS_TREE_BLOCK_REF_KEY); |
| 3974 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 3975 | } |
| 3976 | |
| 3977 | btrfs_mark_buffer_dirty(leaf); |
| 3978 | btrfs_free_path(path); |
| 3979 | |
| 3980 | ret = update_block_group(trans, root, ins->objectid, ins->offset, |
| 3981 | 1, 0); |
| 3982 | if (ret) { |
| 3983 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 3984 | "%llu\n", (unsigned long long)ins->objectid, |
| 3985 | (unsigned long long)ins->offset); |
| 3986 | BUG(); |
| 3987 | } |
| 3988 | return ret; |
| 3989 | } |
| 3990 | |
| 3991 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 3992 | struct btrfs_root *root, |
| 3993 | u64 root_objectid, u64 owner, |
| 3994 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3995 | { |
| 3996 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 3997 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3998 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3999 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4000 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 4001 | 0, root_objectid, owner, offset, |
| 4002 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4003 | return ret; |
| 4004 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4005 | |
| 4006 | /* |
| 4007 | * this is used by the tree logging recovery code. It records that |
| 4008 | * an extent has been allocated and makes sure to clear the free |
| 4009 | * space cache bits as well |
| 4010 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4011 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 4012 | struct btrfs_root *root, |
| 4013 | u64 root_objectid, u64 owner, u64 offset, |
| 4014 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4015 | { |
| 4016 | int ret; |
| 4017 | struct btrfs_block_group_cache *block_group; |
| 4018 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4019 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4020 | mutex_lock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4021 | cache_block_group(root, block_group); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4022 | mutex_unlock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4023 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 4024 | ret = btrfs_remove_free_space(block_group, ins->objectid, |
| 4025 | ins->offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4026 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4027 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4028 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 4029 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4030 | return ret; |
| 4031 | } |
| 4032 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4033 | /* |
| 4034 | * finds a free extent and does all the dirty work required for allocation |
| 4035 | * returns the key for the extent through ins, and a tree buffer for |
| 4036 | * the first block of the extent through buf. |
| 4037 | * |
| 4038 | * returns 0 if everything worked, non-zero otherwise. |
| 4039 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4040 | static int alloc_tree_block(struct btrfs_trans_handle *trans, |
| 4041 | struct btrfs_root *root, |
| 4042 | u64 num_bytes, u64 parent, u64 root_objectid, |
| 4043 | struct btrfs_disk_key *key, int level, |
| 4044 | u64 empty_size, u64 hint_byte, u64 search_end, |
| 4045 | struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4046 | { |
| 4047 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4048 | u64 flags = 0; |
| 4049 | |
| 4050 | ret = __btrfs_reserve_extent(trans, root, num_bytes, num_bytes, |
| 4051 | empty_size, hint_byte, search_end, |
| 4052 | ins, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 4053 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4054 | |
| 4055 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 4056 | if (parent == 0) |
| 4057 | parent = ins->objectid; |
| 4058 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4059 | } else |
| 4060 | BUG_ON(parent > 0); |
| 4061 | |
| 4062 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4063 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4064 | struct btrfs_delayed_extent_op *extent_op; |
| 4065 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 4066 | BUG_ON(!extent_op); |
| 4067 | if (key) |
| 4068 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 4069 | else |
| 4070 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 4071 | extent_op->flags_to_set = flags; |
| 4072 | extent_op->update_key = 1; |
| 4073 | extent_op->update_flags = 1; |
| 4074 | extent_op->is_data = 0; |
| 4075 | |
| 4076 | ret = btrfs_add_delayed_tree_ref(trans, ins->objectid, |
| 4077 | ins->offset, parent, root_objectid, |
| 4078 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 4079 | extent_op); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4080 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 4081 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4082 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4083 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4084 | |
| 4085 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 4086 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4087 | u64 bytenr, u32 blocksize, |
| 4088 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4089 | { |
| 4090 | struct extent_buffer *buf; |
| 4091 | |
| 4092 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 4093 | if (!buf) |
| 4094 | return ERR_PTR(-ENOMEM); |
| 4095 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4096 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4097 | btrfs_tree_lock(buf); |
| 4098 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4099 | |
| 4100 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4101 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4102 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4103 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4104 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4105 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 4106 | } else { |
| 4107 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 4108 | buf->start + buf->len - 1, GFP_NOFS); |
| 4109 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4110 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4111 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 4112 | return buf; |
| 4113 | } |
| 4114 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4115 | /* |
| 4116 | * helper function to allocate a block for a given tree |
| 4117 | * returns the tree buffer or NULL. |
| 4118 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4119 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4120 | struct btrfs_root *root, u32 blocksize, |
| 4121 | u64 parent, u64 root_objectid, |
| 4122 | struct btrfs_disk_key *key, int level, |
| 4123 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4124 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4125 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4126 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4127 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4128 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4129 | ret = alloc_tree_block(trans, root, blocksize, parent, root_objectid, |
| 4130 | key, level, empty_size, hint, (u64)-1, &ins); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4131 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4132 | BUG_ON(ret > 0); |
| 4133 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4134 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 4135 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 4136 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 4137 | blocksize, level); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4138 | return buf; |
| 4139 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4140 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4141 | #if 0 |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4142 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 4143 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4144 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4145 | u64 disk_bytenr; |
| 4146 | u64 num_bytes; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4147 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4148 | struct btrfs_file_extent_item *fi; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4149 | u32 nritems; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4150 | int i; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4151 | int ret; |
| 4152 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4153 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 4154 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4155 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4156 | for (i = 0; i < nritems; i++) { |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 4157 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4158 | btrfs_item_key_to_cpu(leaf, &key, i); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4159 | |
| 4160 | /* only extents have references, skip everything else */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4161 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4162 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4163 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4164 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4165 | |
| 4166 | /* inline extents live in the btree, they don't have refs */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4167 | if (btrfs_file_extent_type(leaf, fi) == |
| 4168 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 4169 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4170 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4171 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4172 | |
| 4173 | /* holes don't have refs */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4174 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 4175 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 4176 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4177 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4178 | ret = btrfs_free_extent(trans, root, disk_bytenr, num_bytes, |
| 4179 | leaf->start, 0, key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4180 | BUG_ON(ret); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4181 | } |
| 4182 | return 0; |
| 4183 | } |
| 4184 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4185 | static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4186 | struct btrfs_root *root, |
| 4187 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4188 | { |
| 4189 | int i; |
| 4190 | int ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4191 | struct btrfs_extent_info *info; |
| 4192 | struct refsort *sorted; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4193 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4194 | if (ref->nritems == 0) |
| 4195 | return 0; |
| 4196 | |
| 4197 | sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4198 | for (i = 0; i < ref->nritems; i++) { |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4199 | sorted[i].bytenr = ref->extents[i].bytenr; |
| 4200 | sorted[i].slot = i; |
| 4201 | } |
| 4202 | sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL); |
| 4203 | |
| 4204 | /* |
| 4205 | * the items in the ref were sorted when the ref was inserted |
| 4206 | * into the ref cache, so this is already in order |
| 4207 | */ |
| 4208 | for (i = 0; i < ref->nritems; i++) { |
| 4209 | info = ref->extents + sorted[i].slot; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4210 | ret = btrfs_free_extent(trans, root, info->bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4211 | info->num_bytes, ref->bytenr, |
| 4212 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4213 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 4214 | |
| 4215 | atomic_inc(&root->fs_info->throttle_gen); |
| 4216 | wake_up(&root->fs_info->transaction_throttle); |
| 4217 | cond_resched(); |
| 4218 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4219 | BUG_ON(ret); |
| 4220 | info++; |
| 4221 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4222 | |
Chris Mason | 806638b | 2009-02-05 09:08:14 -0500 | [diff] [blame] | 4223 | kfree(sorted); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4224 | return 0; |
| 4225 | } |
| 4226 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4227 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4228 | static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans, |
| 4229 | struct btrfs_root *root, u64 start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4230 | u64 len, u32 *refs) |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4231 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 4232 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4233 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4234 | ret = btrfs_lookup_extent_refs(trans, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4235 | BUG_ON(ret); |
| 4236 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4237 | #if 0 /* some debugging code in case we see problems here */ |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4238 | /* if the refs count is one, it won't get increased again. But |
| 4239 | * if the ref count is > 1, someone may be decreasing it at |
| 4240 | * the same time we are. |
| 4241 | */ |
| 4242 | if (*refs != 1) { |
| 4243 | struct extent_buffer *eb = NULL; |
| 4244 | eb = btrfs_find_create_tree_block(root, start, len); |
| 4245 | if (eb) |
| 4246 | btrfs_tree_lock(eb); |
| 4247 | |
| 4248 | mutex_lock(&root->fs_info->alloc_mutex); |
| 4249 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 4250 | BUG_ON(ret); |
| 4251 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 4252 | |
| 4253 | if (eb) { |
| 4254 | btrfs_tree_unlock(eb); |
| 4255 | free_extent_buffer(eb); |
| 4256 | } |
| 4257 | if (*refs == 1) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4258 | printk(KERN_ERR "btrfs block %llu went down to one " |
| 4259 | "during drop_snap\n", (unsigned long long)start); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4260 | } |
| 4261 | |
| 4262 | } |
| 4263 | #endif |
| 4264 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4265 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 4266 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4267 | } |
| 4268 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4269 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4270 | /* |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4271 | * this is used while deleting old snapshots, and it drops the refs |
| 4272 | * on a whole subtree starting from a level 1 node. |
| 4273 | * |
| 4274 | * The idea is to sort all the leaf pointers, and then drop the |
| 4275 | * ref on all the leaves in order. Most of the time the leaves |
| 4276 | * will have ref cache entries, so no leaf IOs will be required to |
| 4277 | * find the extents they have references on. |
| 4278 | * |
| 4279 | * For each leaf, any references it has are also dropped in order |
| 4280 | * |
| 4281 | * This ends up dropping the references in something close to optimal |
| 4282 | * order for reading and modifying the extent allocation tree. |
| 4283 | */ |
| 4284 | static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans, |
| 4285 | struct btrfs_root *root, |
| 4286 | struct btrfs_path *path) |
| 4287 | { |
| 4288 | u64 bytenr; |
| 4289 | u64 root_owner; |
| 4290 | u64 root_gen; |
| 4291 | struct extent_buffer *eb = path->nodes[1]; |
| 4292 | struct extent_buffer *leaf; |
| 4293 | struct btrfs_leaf_ref *ref; |
| 4294 | struct refsort *sorted = NULL; |
| 4295 | int nritems = btrfs_header_nritems(eb); |
| 4296 | int ret; |
| 4297 | int i; |
| 4298 | int refi = 0; |
| 4299 | int slot = path->slots[1]; |
| 4300 | u32 blocksize = btrfs_level_size(root, 0); |
| 4301 | u32 refs; |
| 4302 | |
| 4303 | if (nritems == 0) |
| 4304 | goto out; |
| 4305 | |
| 4306 | root_owner = btrfs_header_owner(eb); |
| 4307 | root_gen = btrfs_header_generation(eb); |
| 4308 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); |
| 4309 | |
| 4310 | /* |
| 4311 | * step one, sort all the leaf pointers so we don't scribble |
| 4312 | * randomly into the extent allocation tree |
| 4313 | */ |
| 4314 | for (i = slot; i < nritems; i++) { |
| 4315 | sorted[refi].bytenr = btrfs_node_blockptr(eb, i); |
| 4316 | sorted[refi].slot = i; |
| 4317 | refi++; |
| 4318 | } |
| 4319 | |
| 4320 | /* |
| 4321 | * nritems won't be zero, but if we're picking up drop_snapshot |
| 4322 | * after a crash, slot might be > 0, so double check things |
| 4323 | * just in case. |
| 4324 | */ |
| 4325 | if (refi == 0) |
| 4326 | goto out; |
| 4327 | |
| 4328 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 4329 | |
| 4330 | /* |
| 4331 | * the first loop frees everything the leaves point to |
| 4332 | */ |
| 4333 | for (i = 0; i < refi; i++) { |
| 4334 | u64 ptr_gen; |
| 4335 | |
| 4336 | bytenr = sorted[i].bytenr; |
| 4337 | |
| 4338 | /* |
| 4339 | * check the reference count on this leaf. If it is > 1 |
| 4340 | * we just decrement it below and don't update any |
| 4341 | * of the refs the leaf points to. |
| 4342 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4343 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
| 4344 | blocksize, &refs); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4345 | BUG_ON(ret); |
| 4346 | if (refs != 1) |
| 4347 | continue; |
| 4348 | |
| 4349 | ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot); |
| 4350 | |
| 4351 | /* |
| 4352 | * the leaf only had one reference, which means the |
| 4353 | * only thing pointing to this leaf is the snapshot |
| 4354 | * we're deleting. It isn't possible for the reference |
| 4355 | * count to increase again later |
| 4356 | * |
| 4357 | * The reference cache is checked for the leaf, |
| 4358 | * and if found we'll be able to drop any refs held by |
| 4359 | * the leaf without needing to read it in. |
| 4360 | */ |
| 4361 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
| 4362 | if (ref && ref->generation != ptr_gen) { |
| 4363 | btrfs_free_leaf_ref(root, ref); |
| 4364 | ref = NULL; |
| 4365 | } |
| 4366 | if (ref) { |
| 4367 | ret = cache_drop_leaf_ref(trans, root, ref); |
| 4368 | BUG_ON(ret); |
| 4369 | btrfs_remove_leaf_ref(root, ref); |
| 4370 | btrfs_free_leaf_ref(root, ref); |
| 4371 | } else { |
| 4372 | /* |
| 4373 | * the leaf wasn't in the reference cache, so |
| 4374 | * we have to read it. |
| 4375 | */ |
| 4376 | leaf = read_tree_block(root, bytenr, blocksize, |
| 4377 | ptr_gen); |
| 4378 | ret = btrfs_drop_leaf_ref(trans, root, leaf); |
| 4379 | BUG_ON(ret); |
| 4380 | free_extent_buffer(leaf); |
| 4381 | } |
| 4382 | atomic_inc(&root->fs_info->throttle_gen); |
| 4383 | wake_up(&root->fs_info->transaction_throttle); |
| 4384 | cond_resched(); |
| 4385 | } |
| 4386 | |
| 4387 | /* |
| 4388 | * run through the loop again to free the refs on the leaves. |
| 4389 | * This is faster than doing it in the loop above because |
| 4390 | * the leaves are likely to be clustered together. We end up |
| 4391 | * working in nice chunks on the extent allocation tree. |
| 4392 | */ |
| 4393 | for (i = 0; i < refi; i++) { |
| 4394 | bytenr = sorted[i].bytenr; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4395 | ret = btrfs_free_extent(trans, root, bytenr, |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4396 | blocksize, eb->start, |
| 4397 | root_owner, root_gen, 0, 1); |
| 4398 | BUG_ON(ret); |
| 4399 | |
| 4400 | atomic_inc(&root->fs_info->throttle_gen); |
| 4401 | wake_up(&root->fs_info->transaction_throttle); |
| 4402 | cond_resched(); |
| 4403 | } |
| 4404 | out: |
| 4405 | kfree(sorted); |
| 4406 | |
| 4407 | /* |
| 4408 | * update the path to show we've processed the entire level 1 |
| 4409 | * node. This will get saved into the root's drop_snapshot_progress |
| 4410 | * field so these drops are not repeated again if this transaction |
| 4411 | * commits. |
| 4412 | */ |
| 4413 | path->slots[1] = nritems; |
| 4414 | return 0; |
| 4415 | } |
| 4416 | |
| 4417 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4418 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 4419 | * counts as it goes. |
| 4420 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4421 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4422 | struct btrfs_root *root, |
| 4423 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4424 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4425 | u64 root_owner; |
| 4426 | u64 root_gen; |
| 4427 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 4428 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4429 | struct extent_buffer *next; |
| 4430 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4431 | struct extent_buffer *parent; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4432 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4433 | int ret; |
| 4434 | u32 refs; |
| 4435 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4436 | WARN_ON(*level < 0); |
| 4437 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4438 | ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4439 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4440 | BUG_ON(ret); |
| 4441 | if (refs > 1) |
| 4442 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4443 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4444 | /* |
| 4445 | * walk down to the last node level and free all the leaves |
| 4446 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4447 | while (*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4448 | WARN_ON(*level < 0); |
| 4449 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4450 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4451 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4452 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4453 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4454 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4455 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4456 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4457 | break; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4458 | |
| 4459 | /* the new code goes down to level 1 and does all the |
| 4460 | * leaves pointed to that node in bulk. So, this check |
| 4461 | * for level 0 will always be false. |
| 4462 | * |
| 4463 | * But, the disk format allows the drop_snapshot_progress |
| 4464 | * field in the root to leave things in a state where |
| 4465 | * a leaf will need cleaning up here. If someone crashes |
| 4466 | * with the old code and then boots with the new code, |
| 4467 | * we might find a leaf here. |
| 4468 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4469 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4470 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4471 | BUG_ON(ret); |
| 4472 | break; |
| 4473 | } |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4474 | |
| 4475 | /* |
| 4476 | * once we get to level one, process the whole node |
| 4477 | * at once, including everything below it. |
| 4478 | */ |
| 4479 | if (*level == 1) { |
| 4480 | ret = drop_level_one_refs(trans, root, path); |
| 4481 | BUG_ON(ret); |
| 4482 | break; |
| 4483 | } |
| 4484 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4485 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 4486 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4487 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4488 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4489 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
| 4490 | blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4491 | BUG_ON(ret); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4492 | |
| 4493 | /* |
| 4494 | * if there is more than one reference, we don't need |
| 4495 | * to read that node to drop any references it has. We |
| 4496 | * just drop the ref we hold on that node and move on to the |
| 4497 | * next slot in this level. |
| 4498 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4499 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4500 | parent = path->nodes[*level]; |
| 4501 | root_owner = btrfs_header_owner(parent); |
| 4502 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4503 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4504 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4505 | ret = btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4506 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4507 | root_owner, root_gen, |
| 4508 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4509 | BUG_ON(ret); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 4510 | |
| 4511 | atomic_inc(&root->fs_info->throttle_gen); |
| 4512 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 4513 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 4514 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4515 | continue; |
| 4516 | } |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4517 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4518 | /* |
| 4519 | * we need to keep freeing things in the next level down. |
| 4520 | * read the block and loop around to process it |
| 4521 | */ |
| 4522 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4523 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 4524 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4525 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4526 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4527 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4528 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 4529 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4530 | } |
| 4531 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4532 | WARN_ON(*level < 0); |
| 4533 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4534 | |
| 4535 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4536 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4537 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4538 | } else { |
| 4539 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4540 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4541 | } |
| 4542 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4543 | blocksize = btrfs_level_size(root, *level); |
| 4544 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4545 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4546 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4547 | /* |
| 4548 | * cleanup and free the reference on the last node |
| 4549 | * we processed |
| 4550 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4551 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4552 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4553 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4554 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4555 | path->nodes[*level] = NULL; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4556 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4557 | *level += 1; |
| 4558 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4559 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4560 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4561 | return 0; |
| 4562 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4563 | #endif |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4564 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4565 | struct walk_control { |
| 4566 | u64 refs[BTRFS_MAX_LEVEL]; |
| 4567 | u64 flags[BTRFS_MAX_LEVEL]; |
| 4568 | struct btrfs_key update_progress; |
| 4569 | int stage; |
| 4570 | int level; |
| 4571 | int shared_level; |
| 4572 | int update_ref; |
| 4573 | int keep_locks; |
| 4574 | }; |
| 4575 | |
| 4576 | #define DROP_REFERENCE 1 |
| 4577 | #define UPDATE_BACKREF 2 |
| 4578 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4579 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4580 | * hepler to process tree block while walking down the tree. |
| 4581 | * |
| 4582 | * when wc->stage == DROP_REFERENCE, this function checks |
| 4583 | * reference count of the block. if the block is shared and |
| 4584 | * we need update back refs for the subtree rooted at the |
| 4585 | * block, this function changes wc->stage to UPDATE_BACKREF |
| 4586 | * |
| 4587 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 4588 | * back refs for pointers in the block. |
| 4589 | * |
| 4590 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4591 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4592 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 4593 | struct btrfs_root *root, |
| 4594 | struct btrfs_path *path, |
| 4595 | struct walk_control *wc) |
| 4596 | { |
| 4597 | int level = wc->level; |
| 4598 | struct extent_buffer *eb = path->nodes[level]; |
| 4599 | struct btrfs_key key; |
| 4600 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 4601 | int ret; |
| 4602 | |
| 4603 | if (wc->stage == UPDATE_BACKREF && |
| 4604 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 4605 | return 1; |
| 4606 | |
| 4607 | /* |
| 4608 | * when reference count of tree block is 1, it won't increase |
| 4609 | * again. once full backref flag is set, we never clear it. |
| 4610 | */ |
| 4611 | if ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 4612 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag))) { |
| 4613 | BUG_ON(!path->locks[level]); |
| 4614 | ret = btrfs_lookup_extent_info(trans, root, |
| 4615 | eb->start, eb->len, |
| 4616 | &wc->refs[level], |
| 4617 | &wc->flags[level]); |
| 4618 | BUG_ON(ret); |
| 4619 | BUG_ON(wc->refs[level] == 0); |
| 4620 | } |
| 4621 | |
| 4622 | if (wc->stage == DROP_REFERENCE && |
| 4623 | wc->update_ref && wc->refs[level] > 1) { |
| 4624 | BUG_ON(eb == root->node); |
| 4625 | BUG_ON(path->slots[level] > 0); |
| 4626 | if (level == 0) |
| 4627 | btrfs_item_key_to_cpu(eb, &key, path->slots[level]); |
| 4628 | else |
| 4629 | btrfs_node_key_to_cpu(eb, &key, path->slots[level]); |
| 4630 | if (btrfs_header_owner(eb) == root->root_key.objectid && |
| 4631 | btrfs_comp_cpu_keys(&key, &wc->update_progress) >= 0) { |
| 4632 | wc->stage = UPDATE_BACKREF; |
| 4633 | wc->shared_level = level; |
| 4634 | } |
| 4635 | } |
| 4636 | |
| 4637 | if (wc->stage == DROP_REFERENCE) { |
| 4638 | if (wc->refs[level] > 1) |
| 4639 | return 1; |
| 4640 | |
| 4641 | if (path->locks[level] && !wc->keep_locks) { |
| 4642 | btrfs_tree_unlock(eb); |
| 4643 | path->locks[level] = 0; |
| 4644 | } |
| 4645 | return 0; |
| 4646 | } |
| 4647 | |
| 4648 | /* wc->stage == UPDATE_BACKREF */ |
| 4649 | if (!(wc->flags[level] & flag)) { |
| 4650 | BUG_ON(!path->locks[level]); |
| 4651 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 4652 | BUG_ON(ret); |
| 4653 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 4654 | BUG_ON(ret); |
| 4655 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 4656 | eb->len, flag, 0); |
| 4657 | BUG_ON(ret); |
| 4658 | wc->flags[level] |= flag; |
| 4659 | } |
| 4660 | |
| 4661 | /* |
| 4662 | * the block is shared by multiple trees, so it's not good to |
| 4663 | * keep the tree lock |
| 4664 | */ |
| 4665 | if (path->locks[level] && level > 0) { |
| 4666 | btrfs_tree_unlock(eb); |
| 4667 | path->locks[level] = 0; |
| 4668 | } |
| 4669 | return 0; |
| 4670 | } |
| 4671 | |
| 4672 | /* |
| 4673 | * hepler to process tree block while walking up the tree. |
| 4674 | * |
| 4675 | * when wc->stage == DROP_REFERENCE, this function drops |
| 4676 | * reference count on the block. |
| 4677 | * |
| 4678 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 4679 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 4680 | * to UPDATE_BACKREF previously while processing the block. |
| 4681 | * |
| 4682 | * NOTE: return value 1 means we should stop walking up. |
| 4683 | */ |
| 4684 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 4685 | struct btrfs_root *root, |
| 4686 | struct btrfs_path *path, |
| 4687 | struct walk_control *wc) |
| 4688 | { |
| 4689 | int ret = 0; |
| 4690 | int level = wc->level; |
| 4691 | struct extent_buffer *eb = path->nodes[level]; |
| 4692 | u64 parent = 0; |
| 4693 | |
| 4694 | if (wc->stage == UPDATE_BACKREF) { |
| 4695 | BUG_ON(wc->shared_level < level); |
| 4696 | if (level < wc->shared_level) |
| 4697 | goto out; |
| 4698 | |
| 4699 | BUG_ON(wc->refs[level] <= 1); |
| 4700 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 4701 | if (ret > 0) |
| 4702 | wc->update_ref = 0; |
| 4703 | |
| 4704 | wc->stage = DROP_REFERENCE; |
| 4705 | wc->shared_level = -1; |
| 4706 | path->slots[level] = 0; |
| 4707 | |
| 4708 | /* |
| 4709 | * check reference count again if the block isn't locked. |
| 4710 | * we should start walking down the tree again if reference |
| 4711 | * count is one. |
| 4712 | */ |
| 4713 | if (!path->locks[level]) { |
| 4714 | BUG_ON(level == 0); |
| 4715 | btrfs_tree_lock(eb); |
| 4716 | btrfs_set_lock_blocking(eb); |
| 4717 | path->locks[level] = 1; |
| 4718 | |
| 4719 | ret = btrfs_lookup_extent_info(trans, root, |
| 4720 | eb->start, eb->len, |
| 4721 | &wc->refs[level], |
| 4722 | &wc->flags[level]); |
| 4723 | BUG_ON(ret); |
| 4724 | BUG_ON(wc->refs[level] == 0); |
| 4725 | if (wc->refs[level] == 1) { |
| 4726 | btrfs_tree_unlock(eb); |
| 4727 | path->locks[level] = 0; |
| 4728 | return 1; |
| 4729 | } |
| 4730 | } else { |
| 4731 | BUG_ON(level != 0); |
| 4732 | } |
| 4733 | } |
| 4734 | |
| 4735 | /* wc->stage == DROP_REFERENCE */ |
| 4736 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 4737 | |
| 4738 | if (wc->refs[level] == 1) { |
| 4739 | if (level == 0) { |
| 4740 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 4741 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 4742 | else |
| 4743 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 4744 | BUG_ON(ret); |
| 4745 | } |
| 4746 | /* make block locked assertion in clean_tree_block happy */ |
| 4747 | if (!path->locks[level] && |
| 4748 | btrfs_header_generation(eb) == trans->transid) { |
| 4749 | btrfs_tree_lock(eb); |
| 4750 | btrfs_set_lock_blocking(eb); |
| 4751 | path->locks[level] = 1; |
| 4752 | } |
| 4753 | clean_tree_block(trans, root, eb); |
| 4754 | } |
| 4755 | |
| 4756 | if (eb == root->node) { |
| 4757 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 4758 | parent = eb->start; |
| 4759 | else |
| 4760 | BUG_ON(root->root_key.objectid != |
| 4761 | btrfs_header_owner(eb)); |
| 4762 | } else { |
| 4763 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 4764 | parent = path->nodes[level + 1]->start; |
| 4765 | else |
| 4766 | BUG_ON(root->root_key.objectid != |
| 4767 | btrfs_header_owner(path->nodes[level + 1])); |
| 4768 | } |
| 4769 | |
| 4770 | ret = btrfs_free_extent(trans, root, eb->start, eb->len, parent, |
| 4771 | root->root_key.objectid, level, 0); |
| 4772 | BUG_ON(ret); |
| 4773 | out: |
| 4774 | wc->refs[level] = 0; |
| 4775 | wc->flags[level] = 0; |
| 4776 | return ret; |
| 4777 | } |
| 4778 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4779 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 4780 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4781 | struct btrfs_path *path, |
| 4782 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4783 | { |
| 4784 | struct extent_buffer *next; |
| 4785 | struct extent_buffer *cur; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4786 | u64 bytenr; |
| 4787 | u64 ptr_gen; |
| 4788 | u32 blocksize; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4789 | int level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4790 | int ret; |
| 4791 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4792 | while (level >= 0) { |
| 4793 | cur = path->nodes[level]; |
| 4794 | BUG_ON(path->slots[level] >= btrfs_header_nritems(cur)); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4795 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4796 | ret = walk_down_proc(trans, root, path, wc); |
| 4797 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4798 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4799 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4800 | if (level == 0) |
| 4801 | break; |
| 4802 | |
| 4803 | bytenr = btrfs_node_blockptr(cur, path->slots[level]); |
| 4804 | blocksize = btrfs_level_size(root, level - 1); |
| 4805 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[level]); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4806 | |
| 4807 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
| 4808 | btrfs_tree_lock(next); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4809 | btrfs_set_lock_blocking(next); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4810 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4811 | level--; |
| 4812 | BUG_ON(level != btrfs_header_level(next)); |
| 4813 | path->nodes[level] = next; |
| 4814 | path->slots[level] = 0; |
| 4815 | path->locks[level] = 1; |
| 4816 | wc->level = level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4817 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4818 | return 0; |
| 4819 | } |
| 4820 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4821 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4822 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4823 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4824 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4825 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4826 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4827 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4828 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4829 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 4830 | while (level < max_level && path->nodes[level]) { |
| 4831 | wc->level = level; |
| 4832 | if (path->slots[level] + 1 < |
| 4833 | btrfs_header_nritems(path->nodes[level])) { |
| 4834 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4835 | return 0; |
| 4836 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4837 | ret = walk_up_proc(trans, root, path, wc); |
| 4838 | if (ret > 0) |
| 4839 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4840 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4841 | if (path->locks[level]) { |
| 4842 | btrfs_tree_unlock(path->nodes[level]); |
| 4843 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4844 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4845 | free_extent_buffer(path->nodes[level]); |
| 4846 | path->nodes[level] = NULL; |
| 4847 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4848 | } |
| 4849 | } |
| 4850 | return 1; |
| 4851 | } |
| 4852 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4853 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4854 | * drop a subvolume tree. |
| 4855 | * |
| 4856 | * this function traverses the tree freeing any blocks that only |
| 4857 | * referenced by the tree. |
| 4858 | * |
| 4859 | * when a shared tree block is found. this function decreases its |
| 4860 | * reference count by one. if update_ref is true, this function |
| 4861 | * also make sure backrefs for the shared block and all lower level |
| 4862 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4863 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4864 | int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4865 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4866 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4867 | struct btrfs_trans_handle *trans; |
| 4868 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4869 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4870 | struct walk_control *wc; |
| 4871 | struct btrfs_key key; |
| 4872 | int err = 0; |
| 4873 | int ret; |
| 4874 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4875 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4876 | path = btrfs_alloc_path(); |
| 4877 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4878 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4879 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 4880 | BUG_ON(!wc); |
| 4881 | |
| 4882 | trans = btrfs_start_transaction(tree_root, 1); |
| 4883 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4884 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4885 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4886 | path->nodes[level] = btrfs_lock_root_node(root); |
| 4887 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4888 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4889 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4890 | memset(&wc->update_progress, 0, |
| 4891 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4892 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4893 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4894 | memcpy(&wc->update_progress, &key, |
| 4895 | sizeof(wc->update_progress)); |
| 4896 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 4897 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4898 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 4899 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4900 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4901 | path->lowest_level = 0; |
| 4902 | if (ret < 0) { |
| 4903 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4904 | goto out; |
| 4905 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4906 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 4907 | path->slots[level]); |
| 4908 | WARN_ON(memcmp(&key, &wc->update_progress, sizeof(key))); |
| 4909 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4910 | /* |
| 4911 | * unlock our path, this is safe because only this |
| 4912 | * function is allowed to delete this snapshot |
| 4913 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4914 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4915 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4916 | level = btrfs_header_level(root->node); |
| 4917 | while (1) { |
| 4918 | btrfs_tree_lock(path->nodes[level]); |
| 4919 | btrfs_set_lock_blocking(path->nodes[level]); |
| 4920 | |
| 4921 | ret = btrfs_lookup_extent_info(trans, root, |
| 4922 | path->nodes[level]->start, |
| 4923 | path->nodes[level]->len, |
| 4924 | &wc->refs[level], |
| 4925 | &wc->flags[level]); |
| 4926 | BUG_ON(ret); |
| 4927 | BUG_ON(wc->refs[level] == 0); |
| 4928 | |
| 4929 | if (level == root_item->drop_level) |
| 4930 | break; |
| 4931 | |
| 4932 | btrfs_tree_unlock(path->nodes[level]); |
| 4933 | WARN_ON(wc->refs[level] != 1); |
| 4934 | level--; |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | wc->level = level; |
| 4939 | wc->shared_level = -1; |
| 4940 | wc->stage = DROP_REFERENCE; |
| 4941 | wc->update_ref = update_ref; |
| 4942 | wc->keep_locks = 0; |
| 4943 | |
| 4944 | while (1) { |
| 4945 | ret = walk_down_tree(trans, root, path, wc); |
| 4946 | if (ret < 0) { |
| 4947 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4948 | break; |
| 4949 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4950 | |
| 4951 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 4952 | if (ret < 0) { |
| 4953 | err = ret; |
| 4954 | break; |
| 4955 | } |
| 4956 | |
| 4957 | if (ret > 0) { |
| 4958 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 4959 | break; |
| 4960 | } |
| 4961 | |
| 4962 | if (wc->stage == DROP_REFERENCE) { |
| 4963 | level = wc->level; |
| 4964 | btrfs_node_key(path->nodes[level], |
| 4965 | &root_item->drop_progress, |
| 4966 | path->slots[level]); |
| 4967 | root_item->drop_level = level; |
| 4968 | } |
| 4969 | |
| 4970 | BUG_ON(wc->level == 0); |
| 4971 | if (trans->transaction->in_commit || |
| 4972 | trans->transaction->delayed_refs.flushing) { |
| 4973 | ret = btrfs_update_root(trans, tree_root, |
| 4974 | &root->root_key, |
| 4975 | root_item); |
| 4976 | BUG_ON(ret); |
| 4977 | |
| 4978 | btrfs_end_transaction(trans, tree_root); |
| 4979 | trans = btrfs_start_transaction(tree_root, 1); |
| 4980 | } else { |
| 4981 | unsigned long update; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4982 | update = trans->delayed_ref_updates; |
| 4983 | trans->delayed_ref_updates = 0; |
| 4984 | if (update) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4985 | btrfs_run_delayed_refs(trans, tree_root, |
| 4986 | update); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4987 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4988 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4989 | btrfs_release_path(root, path); |
| 4990 | BUG_ON(err); |
| 4991 | |
| 4992 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 4993 | BUG_ON(ret); |
| 4994 | |
| 4995 | free_extent_buffer(root->node); |
| 4996 | free_extent_buffer(root->commit_root); |
| 4997 | kfree(root); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4998 | out: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 4999 | btrfs_end_transaction(trans, tree_root); |
| 5000 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5001 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5002 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 5003 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5004 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5005 | /* |
| 5006 | * drop subtree rooted at tree block 'node'. |
| 5007 | * |
| 5008 | * NOTE: this function will unlock and release tree block 'node' |
| 5009 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5010 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 5011 | struct btrfs_root *root, |
| 5012 | struct extent_buffer *node, |
| 5013 | struct extent_buffer *parent) |
| 5014 | { |
| 5015 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5016 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5017 | int level; |
| 5018 | int parent_level; |
| 5019 | int ret = 0; |
| 5020 | int wret; |
| 5021 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5022 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5023 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5024 | path = btrfs_alloc_path(); |
| 5025 | BUG_ON(!path); |
| 5026 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5027 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 5028 | BUG_ON(!wc); |
| 5029 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5030 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5031 | parent_level = btrfs_header_level(parent); |
| 5032 | extent_buffer_get(parent); |
| 5033 | path->nodes[parent_level] = parent; |
| 5034 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 5035 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 5036 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5037 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5038 | path->nodes[level] = node; |
| 5039 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5040 | path->locks[level] = 1; |
| 5041 | |
| 5042 | wc->refs[parent_level] = 1; |
| 5043 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5044 | wc->level = level; |
| 5045 | wc->shared_level = -1; |
| 5046 | wc->stage = DROP_REFERENCE; |
| 5047 | wc->update_ref = 0; |
| 5048 | wc->keep_locks = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5049 | |
| 5050 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5051 | wret = walk_down_tree(trans, root, path, wc); |
| 5052 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5053 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5054 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5055 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5056 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5057 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5058 | if (wret < 0) |
| 5059 | ret = wret; |
| 5060 | if (wret != 0) |
| 5061 | break; |
| 5062 | } |
| 5063 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5064 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5065 | btrfs_free_path(path); |
| 5066 | return ret; |
| 5067 | } |
| 5068 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5069 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5070 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 5071 | unsigned long nr) |
| 5072 | { |
| 5073 | return min(last, start + nr - 1); |
| 5074 | } |
| 5075 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5076 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5077 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5078 | { |
| 5079 | u64 page_start; |
| 5080 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5081 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5082 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5083 | unsigned long i; |
| 5084 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5085 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5086 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5087 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5088 | unsigned int total_read = 0; |
| 5089 | unsigned int total_dirty = 0; |
| 5090 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5091 | |
| 5092 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5093 | |
| 5094 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5095 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5096 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 5097 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5098 | /* make sure the dirty trick played by the caller work */ |
| 5099 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 5100 | first_index, last_index); |
| 5101 | if (ret) |
| 5102 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5103 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5104 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5105 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5106 | for (i = first_index ; i <= last_index; i++) { |
| 5107 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5108 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5109 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 5110 | } |
| 5111 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5112 | again: |
| 5113 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5114 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5115 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5116 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5117 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5118 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5119 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5120 | if (!PageUptodate(page)) { |
| 5121 | btrfs_readpage(NULL, page); |
| 5122 | lock_page(page); |
| 5123 | if (!PageUptodate(page)) { |
| 5124 | unlock_page(page); |
| 5125 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5126 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5127 | goto out_unlock; |
| 5128 | } |
| 5129 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5130 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5131 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5132 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 5133 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5134 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5135 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 5136 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 5137 | if (ordered) { |
| 5138 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 5139 | unlock_page(page); |
| 5140 | page_cache_release(page); |
| 5141 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5142 | btrfs_put_ordered_extent(ordered); |
| 5143 | goto again; |
| 5144 | } |
| 5145 | set_page_extent_mapped(page); |
| 5146 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5147 | if (i == first_index) |
| 5148 | set_extent_bits(io_tree, page_start, page_end, |
| 5149 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 5150 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5151 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5152 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5153 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5154 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 5155 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5156 | unlock_page(page); |
| 5157 | page_cache_release(page); |
| 5158 | } |
| 5159 | |
| 5160 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5161 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5162 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5163 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5164 | return ret; |
| 5165 | } |
| 5166 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5167 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5168 | struct btrfs_key *extent_key, |
| 5169 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5170 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5171 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 5172 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 5173 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5174 | u64 start = extent_key->objectid - offset; |
| 5175 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5176 | |
| 5177 | em = alloc_extent_map(GFP_NOFS); |
| 5178 | BUG_ON(!em || IS_ERR(em)); |
| 5179 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5180 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5181 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5182 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5183 | em->block_start = extent_key->objectid; |
| 5184 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 5185 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 5186 | |
| 5187 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5188 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5189 | while (1) { |
| 5190 | int ret; |
| 5191 | spin_lock(&em_tree->lock); |
| 5192 | ret = add_extent_mapping(em_tree, em); |
| 5193 | spin_unlock(&em_tree->lock); |
| 5194 | if (ret != -EEXIST) { |
| 5195 | free_extent_map(em); |
| 5196 | break; |
| 5197 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5198 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5199 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5200 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5201 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5202 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | struct btrfs_ref_path { |
| 5206 | u64 extent_start; |
| 5207 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 5208 | u64 root_objectid; |
| 5209 | u64 root_generation; |
| 5210 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5211 | u32 num_refs; |
| 5212 | int lowest_level; |
| 5213 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5214 | int shared_level; |
| 5215 | |
| 5216 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 5217 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5218 | }; |
| 5219 | |
| 5220 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5221 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5222 | u64 disk_bytenr; |
| 5223 | u64 disk_num_bytes; |
| 5224 | u64 offset; |
| 5225 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5226 | u8 compression; |
| 5227 | u8 encryption; |
| 5228 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5229 | }; |
| 5230 | |
| 5231 | static int is_cowonly_root(u64 root_objectid) |
| 5232 | { |
| 5233 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 5234 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 5235 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 5236 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 5237 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5238 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5239 | return 1; |
| 5240 | return 0; |
| 5241 | } |
| 5242 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5243 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5244 | struct btrfs_root *extent_root, |
| 5245 | struct btrfs_ref_path *ref_path, |
| 5246 | int first_time) |
| 5247 | { |
| 5248 | struct extent_buffer *leaf; |
| 5249 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5250 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5251 | struct btrfs_key key; |
| 5252 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5253 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5254 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5255 | int level; |
| 5256 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5257 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5258 | path = btrfs_alloc_path(); |
| 5259 | if (!path) |
| 5260 | return -ENOMEM; |
| 5261 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5262 | if (first_time) { |
| 5263 | ref_path->lowest_level = -1; |
| 5264 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5265 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5266 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5267 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5268 | walk_down: |
| 5269 | level = ref_path->current_level - 1; |
| 5270 | while (level >= -1) { |
| 5271 | u64 parent; |
| 5272 | if (level < ref_path->lowest_level) |
| 5273 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5274 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5275 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5276 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5277 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5278 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5279 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5280 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5281 | parent = ref_path->nodes[level + 1]; |
| 5282 | ref_path->nodes[level + 1] = 0; |
| 5283 | ref_path->current_level = level; |
| 5284 | BUG_ON(parent == 0); |
| 5285 | |
| 5286 | key.objectid = bytenr; |
| 5287 | key.offset = parent + 1; |
| 5288 | key.type = BTRFS_EXTENT_REF_KEY; |
| 5289 | |
| 5290 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5291 | if (ret < 0) |
| 5292 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5293 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5294 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5295 | leaf = path->nodes[0]; |
| 5296 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5297 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5298 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5299 | if (ret < 0) |
| 5300 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5301 | if (ret > 0) |
| 5302 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 5303 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5304 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5305 | |
| 5306 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5307 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5308 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 5309 | if (level < ref_path->shared_level) |
| 5310 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5311 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5312 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5313 | next: |
| 5314 | level--; |
| 5315 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5316 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5317 | } |
| 5318 | /* reached lowest level */ |
| 5319 | ret = 1; |
| 5320 | goto out; |
| 5321 | walk_up: |
| 5322 | level = ref_path->current_level; |
| 5323 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 5324 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5325 | |
| 5326 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5327 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5328 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5329 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5330 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5331 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5332 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5333 | key.objectid = bytenr; |
| 5334 | key.offset = 0; |
| 5335 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5336 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5337 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 5338 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5339 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5340 | |
| 5341 | leaf = path->nodes[0]; |
| 5342 | nritems = btrfs_header_nritems(leaf); |
| 5343 | if (path->slots[0] >= nritems) { |
| 5344 | ret = btrfs_next_leaf(extent_root, path); |
| 5345 | if (ret < 0) |
| 5346 | goto out; |
| 5347 | if (ret > 0) { |
| 5348 | /* the extent was freed by someone */ |
| 5349 | if (ref_path->lowest_level == level) |
| 5350 | goto out; |
| 5351 | btrfs_release_path(extent_root, path); |
| 5352 | goto walk_down; |
| 5353 | } |
| 5354 | leaf = path->nodes[0]; |
| 5355 | } |
| 5356 | |
| 5357 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5358 | if (found_key.objectid != bytenr || |
| 5359 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 5360 | /* the extent was freed by someone */ |
| 5361 | if (ref_path->lowest_level == level) { |
| 5362 | ret = 1; |
| 5363 | goto out; |
| 5364 | } |
| 5365 | btrfs_release_path(extent_root, path); |
| 5366 | goto walk_down; |
| 5367 | } |
| 5368 | found: |
| 5369 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 5370 | struct btrfs_extent_ref); |
| 5371 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 5372 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5373 | if (first_time) { |
| 5374 | level = (int)ref_objectid; |
| 5375 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 5376 | ref_path->lowest_level = level; |
| 5377 | ref_path->current_level = level; |
| 5378 | ref_path->nodes[level] = bytenr; |
| 5379 | } else { |
| 5380 | WARN_ON(ref_objectid != level); |
| 5381 | } |
| 5382 | } else { |
| 5383 | WARN_ON(level != -1); |
| 5384 | } |
| 5385 | first_time = 0; |
| 5386 | |
| 5387 | if (ref_path->lowest_level == level) { |
| 5388 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5389 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 5390 | } |
| 5391 | |
| 5392 | /* |
| 5393 | * the block is tree root or the block isn't in reference |
| 5394 | * counted tree. |
| 5395 | */ |
| 5396 | if (found_key.objectid == found_key.offset || |
| 5397 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 5398 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 5399 | ref_path->root_generation = |
| 5400 | btrfs_ref_generation(leaf, ref); |
| 5401 | if (level < 0) { |
| 5402 | /* special reference from the tree log */ |
| 5403 | ref_path->nodes[0] = found_key.offset; |
| 5404 | ref_path->current_level = 0; |
| 5405 | } |
| 5406 | ret = 0; |
| 5407 | goto out; |
| 5408 | } |
| 5409 | |
| 5410 | level++; |
| 5411 | BUG_ON(ref_path->nodes[level] != 0); |
| 5412 | ref_path->nodes[level] = found_key.offset; |
| 5413 | ref_path->current_level = level; |
| 5414 | |
| 5415 | /* |
| 5416 | * the reference was created in the running transaction, |
| 5417 | * no need to continue walking up. |
| 5418 | */ |
| 5419 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 5420 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 5421 | ref_path->root_generation = |
| 5422 | btrfs_ref_generation(leaf, ref); |
| 5423 | ret = 0; |
| 5424 | goto out; |
| 5425 | } |
| 5426 | |
| 5427 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5428 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5429 | } |
| 5430 | /* reached max tree level, but no tree root found. */ |
| 5431 | BUG(); |
| 5432 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5433 | btrfs_free_path(path); |
| 5434 | return ret; |
| 5435 | } |
| 5436 | |
| 5437 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 5438 | struct btrfs_root *extent_root, |
| 5439 | struct btrfs_ref_path *ref_path, |
| 5440 | u64 extent_start) |
| 5441 | { |
| 5442 | memset(ref_path, 0, sizeof(*ref_path)); |
| 5443 | ref_path->extent_start = extent_start; |
| 5444 | |
| 5445 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 5446 | } |
| 5447 | |
| 5448 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 5449 | struct btrfs_root *extent_root, |
| 5450 | struct btrfs_ref_path *ref_path) |
| 5451 | { |
| 5452 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 5453 | } |
| 5454 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5455 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5456 | struct btrfs_key *extent_key, |
| 5457 | u64 offset, int no_fragment, |
| 5458 | struct disk_extent **extents, |
| 5459 | int *nr_extents) |
| 5460 | { |
| 5461 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 5462 | struct btrfs_path *path; |
| 5463 | struct btrfs_file_extent_item *fi; |
| 5464 | struct extent_buffer *leaf; |
| 5465 | struct disk_extent *exts = *extents; |
| 5466 | struct btrfs_key found_key; |
| 5467 | u64 cur_pos; |
| 5468 | u64 last_byte; |
| 5469 | u32 nritems; |
| 5470 | int nr = 0; |
| 5471 | int max = *nr_extents; |
| 5472 | int ret; |
| 5473 | |
| 5474 | WARN_ON(!no_fragment && *extents); |
| 5475 | if (!exts) { |
| 5476 | max = 1; |
| 5477 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 5478 | if (!exts) |
| 5479 | return -ENOMEM; |
| 5480 | } |
| 5481 | |
| 5482 | path = btrfs_alloc_path(); |
| 5483 | BUG_ON(!path); |
| 5484 | |
| 5485 | cur_pos = extent_key->objectid - offset; |
| 5486 | last_byte = extent_key->objectid + extent_key->offset; |
| 5487 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 5488 | cur_pos, 0); |
| 5489 | if (ret < 0) |
| 5490 | goto out; |
| 5491 | if (ret > 0) { |
| 5492 | ret = -ENOENT; |
| 5493 | goto out; |
| 5494 | } |
| 5495 | |
| 5496 | while (1) { |
| 5497 | leaf = path->nodes[0]; |
| 5498 | nritems = btrfs_header_nritems(leaf); |
| 5499 | if (path->slots[0] >= nritems) { |
| 5500 | ret = btrfs_next_leaf(root, path); |
| 5501 | if (ret < 0) |
| 5502 | goto out; |
| 5503 | if (ret > 0) |
| 5504 | break; |
| 5505 | leaf = path->nodes[0]; |
| 5506 | } |
| 5507 | |
| 5508 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5509 | if (found_key.offset != cur_pos || |
| 5510 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 5511 | found_key.objectid != reloc_inode->i_ino) |
| 5512 | break; |
| 5513 | |
| 5514 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 5515 | struct btrfs_file_extent_item); |
| 5516 | if (btrfs_file_extent_type(leaf, fi) != |
| 5517 | BTRFS_FILE_EXTENT_REG || |
| 5518 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 5519 | break; |
| 5520 | |
| 5521 | if (nr == max) { |
| 5522 | struct disk_extent *old = exts; |
| 5523 | max *= 2; |
| 5524 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 5525 | memcpy(exts, old, sizeof(*exts) * nr); |
| 5526 | if (old != *extents) |
| 5527 | kfree(old); |
| 5528 | } |
| 5529 | |
| 5530 | exts[nr].disk_bytenr = |
| 5531 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5532 | exts[nr].disk_num_bytes = |
| 5533 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 5534 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 5535 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5536 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 5537 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 5538 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 5539 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 5540 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5541 | BUG_ON(exts[nr].offset > 0); |
| 5542 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 5543 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5544 | |
| 5545 | cur_pos += exts[nr].num_bytes; |
| 5546 | nr++; |
| 5547 | |
| 5548 | if (cur_pos + offset >= last_byte) |
| 5549 | break; |
| 5550 | |
| 5551 | if (no_fragment) { |
| 5552 | ret = 1; |
| 5553 | goto out; |
| 5554 | } |
| 5555 | path->slots[0]++; |
| 5556 | } |
| 5557 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 5558 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5559 | if (cur_pos + offset < last_byte) { |
| 5560 | ret = -ENOENT; |
| 5561 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5562 | } |
| 5563 | ret = 0; |
| 5564 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5565 | btrfs_free_path(path); |
| 5566 | if (ret) { |
| 5567 | if (exts != *extents) |
| 5568 | kfree(exts); |
| 5569 | } else { |
| 5570 | *extents = exts; |
| 5571 | *nr_extents = nr; |
| 5572 | } |
| 5573 | return ret; |
| 5574 | } |
| 5575 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5576 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5577 | struct btrfs_root *root, |
| 5578 | struct btrfs_path *path, |
| 5579 | struct btrfs_key *extent_key, |
| 5580 | struct btrfs_key *leaf_key, |
| 5581 | struct btrfs_ref_path *ref_path, |
| 5582 | struct disk_extent *new_extents, |
| 5583 | int nr_extents) |
| 5584 | { |
| 5585 | struct extent_buffer *leaf; |
| 5586 | struct btrfs_file_extent_item *fi; |
| 5587 | struct inode *inode = NULL; |
| 5588 | struct btrfs_key key; |
| 5589 | u64 lock_start = 0; |
| 5590 | u64 lock_end = 0; |
| 5591 | u64 num_bytes; |
| 5592 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5593 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5594 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5595 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5596 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5597 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5598 | int ret; |
| 5599 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5600 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5601 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5602 | if (key.objectid < ref_path->owner_objectid || |
| 5603 | (key.objectid == ref_path->owner_objectid && |
| 5604 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 5605 | key.objectid = ref_path->owner_objectid; |
| 5606 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 5607 | key.offset = 0; |
| 5608 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | while (1) { |
| 5612 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 5613 | if (ret < 0) |
| 5614 | goto out; |
| 5615 | |
| 5616 | leaf = path->nodes[0]; |
| 5617 | nritems = btrfs_header_nritems(leaf); |
| 5618 | next: |
| 5619 | if (extent_locked && ret > 0) { |
| 5620 | /* |
| 5621 | * the file extent item was modified by someone |
| 5622 | * before the extent got locked. |
| 5623 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5624 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 5625 | lock_end, GFP_NOFS); |
| 5626 | extent_locked = 0; |
| 5627 | } |
| 5628 | |
| 5629 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5630 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5631 | break; |
| 5632 | |
| 5633 | BUG_ON(extent_locked); |
| 5634 | ret = btrfs_next_leaf(root, path); |
| 5635 | if (ret < 0) |
| 5636 | goto out; |
| 5637 | if (ret > 0) |
| 5638 | break; |
| 5639 | leaf = path->nodes[0]; |
| 5640 | nritems = btrfs_header_nritems(leaf); |
| 5641 | } |
| 5642 | |
| 5643 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 5644 | |
| 5645 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 5646 | if ((key.objectid > ref_path->owner_objectid) || |
| 5647 | (key.objectid == ref_path->owner_objectid && |
| 5648 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5649 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5650 | break; |
| 5651 | } |
| 5652 | |
| 5653 | if (inode && key.objectid != inode->i_ino) { |
| 5654 | BUG_ON(extent_locked); |
| 5655 | btrfs_release_path(root, path); |
| 5656 | mutex_unlock(&inode->i_mutex); |
| 5657 | iput(inode); |
| 5658 | inode = NULL; |
| 5659 | continue; |
| 5660 | } |
| 5661 | |
| 5662 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5663 | path->slots[0]++; |
| 5664 | ret = 1; |
| 5665 | goto next; |
| 5666 | } |
| 5667 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 5668 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5669 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 5670 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 5671 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5672 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 5673 | extent_key->objectid)) { |
| 5674 | path->slots[0]++; |
| 5675 | ret = 1; |
| 5676 | goto next; |
| 5677 | } |
| 5678 | |
| 5679 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 5680 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 5681 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5682 | if (search_end == (u64)-1) { |
| 5683 | search_end = key.offset - ext_offset + |
| 5684 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 5685 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5686 | |
| 5687 | if (!extent_locked) { |
| 5688 | lock_start = key.offset; |
| 5689 | lock_end = lock_start + num_bytes - 1; |
| 5690 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5691 | if (lock_start > key.offset || |
| 5692 | lock_end + 1 < key.offset + num_bytes) { |
| 5693 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 5694 | lock_start, lock_end, GFP_NOFS); |
| 5695 | extent_locked = 0; |
| 5696 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5697 | } |
| 5698 | |
| 5699 | if (!inode) { |
| 5700 | btrfs_release_path(root, path); |
| 5701 | |
| 5702 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 5703 | key.objectid, root); |
| 5704 | if (inode->i_state & I_NEW) { |
| 5705 | BTRFS_I(inode)->root = root; |
| 5706 | BTRFS_I(inode)->location.objectid = |
| 5707 | key.objectid; |
| 5708 | BTRFS_I(inode)->location.type = |
| 5709 | BTRFS_INODE_ITEM_KEY; |
| 5710 | BTRFS_I(inode)->location.offset = 0; |
| 5711 | btrfs_read_locked_inode(inode); |
| 5712 | unlock_new_inode(inode); |
| 5713 | } |
| 5714 | /* |
| 5715 | * some code call btrfs_commit_transaction while |
| 5716 | * holding the i_mutex, so we can't use mutex_lock |
| 5717 | * here. |
| 5718 | */ |
| 5719 | if (is_bad_inode(inode) || |
| 5720 | !mutex_trylock(&inode->i_mutex)) { |
| 5721 | iput(inode); |
| 5722 | inode = NULL; |
| 5723 | key.offset = (u64)-1; |
| 5724 | goto skip; |
| 5725 | } |
| 5726 | } |
| 5727 | |
| 5728 | if (!extent_locked) { |
| 5729 | struct btrfs_ordered_extent *ordered; |
| 5730 | |
| 5731 | btrfs_release_path(root, path); |
| 5732 | |
| 5733 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 5734 | lock_end, GFP_NOFS); |
| 5735 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 5736 | lock_end); |
| 5737 | if (ordered && |
| 5738 | ordered->file_offset <= lock_end && |
| 5739 | ordered->file_offset + ordered->len > lock_start) { |
| 5740 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 5741 | lock_start, lock_end, GFP_NOFS); |
| 5742 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5743 | btrfs_put_ordered_extent(ordered); |
| 5744 | key.offset += num_bytes; |
| 5745 | goto skip; |
| 5746 | } |
| 5747 | if (ordered) |
| 5748 | btrfs_put_ordered_extent(ordered); |
| 5749 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5750 | extent_locked = 1; |
| 5751 | continue; |
| 5752 | } |
| 5753 | |
| 5754 | if (nr_extents == 1) { |
| 5755 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5756 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 5757 | new_extents[0].disk_bytenr); |
| 5758 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 5759 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5760 | btrfs_mark_buffer_dirty(leaf); |
| 5761 | |
| 5762 | btrfs_drop_extent_cache(inode, key.offset, |
| 5763 | key.offset + num_bytes - 1, 0); |
| 5764 | |
| 5765 | ret = btrfs_inc_extent_ref(trans, root, |
| 5766 | new_extents[0].disk_bytenr, |
| 5767 | new_extents[0].disk_num_bytes, |
| 5768 | leaf->start, |
| 5769 | root->root_key.objectid, |
| 5770 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5771 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5772 | BUG_ON(ret); |
| 5773 | |
| 5774 | ret = btrfs_free_extent(trans, root, |
| 5775 | extent_key->objectid, |
| 5776 | extent_key->offset, |
| 5777 | leaf->start, |
| 5778 | btrfs_header_owner(leaf), |
| 5779 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5780 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5781 | BUG_ON(ret); |
| 5782 | |
| 5783 | btrfs_release_path(root, path); |
| 5784 | key.offset += num_bytes; |
| 5785 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5786 | BUG_ON(1); |
| 5787 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5788 | u64 alloc_hint; |
| 5789 | u64 extent_len; |
| 5790 | int i; |
| 5791 | /* |
| 5792 | * drop old extent pointer at first, then insert the |
| 5793 | * new pointers one bye one |
| 5794 | */ |
| 5795 | btrfs_release_path(root, path); |
| 5796 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 5797 | key.offset + num_bytes, |
| 5798 | key.offset, &alloc_hint); |
| 5799 | BUG_ON(ret); |
| 5800 | |
| 5801 | for (i = 0; i < nr_extents; i++) { |
| 5802 | if (ext_offset >= new_extents[i].num_bytes) { |
| 5803 | ext_offset -= new_extents[i].num_bytes; |
| 5804 | continue; |
| 5805 | } |
| 5806 | extent_len = min(new_extents[i].num_bytes - |
| 5807 | ext_offset, num_bytes); |
| 5808 | |
| 5809 | ret = btrfs_insert_empty_item(trans, root, |
| 5810 | path, &key, |
| 5811 | sizeof(*fi)); |
| 5812 | BUG_ON(ret); |
| 5813 | |
| 5814 | leaf = path->nodes[0]; |
| 5815 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 5816 | struct btrfs_file_extent_item); |
| 5817 | btrfs_set_file_extent_generation(leaf, fi, |
| 5818 | trans->transid); |
| 5819 | btrfs_set_file_extent_type(leaf, fi, |
| 5820 | BTRFS_FILE_EXTENT_REG); |
| 5821 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 5822 | new_extents[i].disk_bytenr); |
| 5823 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 5824 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5825 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 5826 | new_extents[i].ram_bytes); |
| 5827 | |
| 5828 | btrfs_set_file_extent_compression(leaf, fi, |
| 5829 | new_extents[i].compression); |
| 5830 | btrfs_set_file_extent_encryption(leaf, fi, |
| 5831 | new_extents[i].encryption); |
| 5832 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 5833 | new_extents[i].other_encoding); |
| 5834 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5835 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 5836 | extent_len); |
| 5837 | ext_offset += new_extents[i].offset; |
| 5838 | btrfs_set_file_extent_offset(leaf, fi, |
| 5839 | ext_offset); |
| 5840 | btrfs_mark_buffer_dirty(leaf); |
| 5841 | |
| 5842 | btrfs_drop_extent_cache(inode, key.offset, |
| 5843 | key.offset + extent_len - 1, 0); |
| 5844 | |
| 5845 | ret = btrfs_inc_extent_ref(trans, root, |
| 5846 | new_extents[i].disk_bytenr, |
| 5847 | new_extents[i].disk_num_bytes, |
| 5848 | leaf->start, |
| 5849 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5850 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5851 | BUG_ON(ret); |
| 5852 | btrfs_release_path(root, path); |
| 5853 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5854 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5855 | |
| 5856 | ext_offset = 0; |
| 5857 | num_bytes -= extent_len; |
| 5858 | key.offset += extent_len; |
| 5859 | |
| 5860 | if (num_bytes == 0) |
| 5861 | break; |
| 5862 | } |
| 5863 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5864 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5868 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 5869 | lock_end, GFP_NOFS); |
| 5870 | extent_locked = 0; |
| 5871 | } |
| 5872 | skip: |
| 5873 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5874 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5875 | break; |
| 5876 | |
| 5877 | cond_resched(); |
| 5878 | } |
| 5879 | ret = 0; |
| 5880 | out: |
| 5881 | btrfs_release_path(root, path); |
| 5882 | if (inode) { |
| 5883 | mutex_unlock(&inode->i_mutex); |
| 5884 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5885 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 5886 | lock_end, GFP_NOFS); |
| 5887 | } |
| 5888 | iput(inode); |
| 5889 | } |
| 5890 | return ret; |
| 5891 | } |
| 5892 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5893 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 5894 | struct btrfs_root *root, |
| 5895 | struct extent_buffer *buf, u64 orig_start) |
| 5896 | { |
| 5897 | int level; |
| 5898 | int ret; |
| 5899 | |
| 5900 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 5901 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5902 | |
| 5903 | level = btrfs_header_level(buf); |
| 5904 | if (level == 0) { |
| 5905 | struct btrfs_leaf_ref *ref; |
| 5906 | struct btrfs_leaf_ref *orig_ref; |
| 5907 | |
| 5908 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 5909 | if (!orig_ref) |
| 5910 | return -ENOENT; |
| 5911 | |
| 5912 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 5913 | if (!ref) { |
| 5914 | btrfs_free_leaf_ref(root, orig_ref); |
| 5915 | return -ENOMEM; |
| 5916 | } |
| 5917 | |
| 5918 | ref->nritems = orig_ref->nritems; |
| 5919 | memcpy(ref->extents, orig_ref->extents, |
| 5920 | sizeof(ref->extents[0]) * ref->nritems); |
| 5921 | |
| 5922 | btrfs_free_leaf_ref(root, orig_ref); |
| 5923 | |
| 5924 | ref->root_gen = trans->transid; |
| 5925 | ref->bytenr = buf->start; |
| 5926 | ref->owner = btrfs_header_owner(buf); |
| 5927 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5928 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5929 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 5930 | WARN_ON(ret); |
| 5931 | btrfs_free_leaf_ref(root, ref); |
| 5932 | } |
| 5933 | return 0; |
| 5934 | } |
| 5935 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5936 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5937 | struct extent_buffer *leaf, |
| 5938 | struct btrfs_block_group_cache *group, |
| 5939 | struct btrfs_root *target_root) |
| 5940 | { |
| 5941 | struct btrfs_key key; |
| 5942 | struct inode *inode = NULL; |
| 5943 | struct btrfs_file_extent_item *fi; |
| 5944 | u64 num_bytes; |
| 5945 | u64 skip_objectid = 0; |
| 5946 | u32 nritems; |
| 5947 | u32 i; |
| 5948 | |
| 5949 | nritems = btrfs_header_nritems(leaf); |
| 5950 | for (i = 0; i < nritems; i++) { |
| 5951 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 5952 | if (key.objectid == skip_objectid || |
| 5953 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 5954 | continue; |
| 5955 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 5956 | if (btrfs_file_extent_type(leaf, fi) == |
| 5957 | BTRFS_FILE_EXTENT_INLINE) |
| 5958 | continue; |
| 5959 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 5960 | continue; |
| 5961 | if (!inode || inode->i_ino != key.objectid) { |
| 5962 | iput(inode); |
| 5963 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 5964 | key.objectid, target_root, 1); |
| 5965 | } |
| 5966 | if (!inode) { |
| 5967 | skip_objectid = key.objectid; |
| 5968 | continue; |
| 5969 | } |
| 5970 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 5971 | |
| 5972 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 5973 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5974 | btrfs_drop_extent_cache(inode, key.offset, |
| 5975 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5976 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 5977 | key.offset + num_bytes - 1, GFP_NOFS); |
| 5978 | cond_resched(); |
| 5979 | } |
| 5980 | iput(inode); |
| 5981 | return 0; |
| 5982 | } |
| 5983 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5984 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5985 | struct btrfs_root *root, |
| 5986 | struct extent_buffer *leaf, |
| 5987 | struct btrfs_block_group_cache *group, |
| 5988 | struct inode *reloc_inode) |
| 5989 | { |
| 5990 | struct btrfs_key key; |
| 5991 | struct btrfs_key extent_key; |
| 5992 | struct btrfs_file_extent_item *fi; |
| 5993 | struct btrfs_leaf_ref *ref; |
| 5994 | struct disk_extent *new_extent; |
| 5995 | u64 bytenr; |
| 5996 | u64 num_bytes; |
| 5997 | u32 nritems; |
| 5998 | u32 i; |
| 5999 | int ext_index; |
| 6000 | int nr_extent; |
| 6001 | int ret; |
| 6002 | |
| 6003 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 6004 | BUG_ON(!new_extent); |
| 6005 | |
| 6006 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 6007 | BUG_ON(!ref); |
| 6008 | |
| 6009 | ext_index = -1; |
| 6010 | nritems = btrfs_header_nritems(leaf); |
| 6011 | for (i = 0; i < nritems; i++) { |
| 6012 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 6013 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 6014 | continue; |
| 6015 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 6016 | if (btrfs_file_extent_type(leaf, fi) == |
| 6017 | BTRFS_FILE_EXTENT_INLINE) |
| 6018 | continue; |
| 6019 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 6020 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 6021 | if (bytenr == 0) |
| 6022 | continue; |
| 6023 | |
| 6024 | ext_index++; |
| 6025 | if (bytenr >= group->key.objectid + group->key.offset || |
| 6026 | bytenr + num_bytes <= group->key.objectid) |
| 6027 | continue; |
| 6028 | |
| 6029 | extent_key.objectid = bytenr; |
| 6030 | extent_key.offset = num_bytes; |
| 6031 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 6032 | nr_extent = 1; |
| 6033 | ret = get_new_locations(reloc_inode, &extent_key, |
| 6034 | group->key.objectid, 1, |
| 6035 | &new_extent, &nr_extent); |
| 6036 | if (ret > 0) |
| 6037 | continue; |
| 6038 | BUG_ON(ret < 0); |
| 6039 | |
| 6040 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 6041 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 6042 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 6043 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 6044 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6045 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 6046 | new_extent->disk_bytenr); |
| 6047 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 6048 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6049 | btrfs_mark_buffer_dirty(leaf); |
| 6050 | |
| 6051 | ret = btrfs_inc_extent_ref(trans, root, |
| 6052 | new_extent->disk_bytenr, |
| 6053 | new_extent->disk_num_bytes, |
| 6054 | leaf->start, |
| 6055 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6056 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6057 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6058 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6059 | ret = btrfs_free_extent(trans, root, |
| 6060 | bytenr, num_bytes, leaf->start, |
| 6061 | btrfs_header_owner(leaf), |
| 6062 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 6063 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6064 | BUG_ON(ret); |
| 6065 | cond_resched(); |
| 6066 | } |
| 6067 | kfree(new_extent); |
| 6068 | BUG_ON(ext_index + 1 != ref->nritems); |
| 6069 | btrfs_free_leaf_ref(root, ref); |
| 6070 | return 0; |
| 6071 | } |
| 6072 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6073 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 6074 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6075 | { |
| 6076 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6077 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6078 | |
| 6079 | if (root->reloc_root) { |
| 6080 | reloc_root = root->reloc_root; |
| 6081 | root->reloc_root = NULL; |
| 6082 | list_add(&reloc_root->dead_list, |
| 6083 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6084 | |
| 6085 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 6086 | reloc_root->node->start); |
| 6087 | btrfs_set_root_level(&root->root_item, |
| 6088 | btrfs_header_level(reloc_root->node)); |
| 6089 | memset(&reloc_root->root_item.drop_progress, 0, |
| 6090 | sizeof(struct btrfs_disk_key)); |
| 6091 | reloc_root->root_item.drop_level = 0; |
| 6092 | |
| 6093 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 6094 | &reloc_root->root_key, |
| 6095 | &reloc_root->root_item); |
| 6096 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6097 | } |
| 6098 | return 0; |
| 6099 | } |
| 6100 | |
| 6101 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 6102 | { |
| 6103 | struct btrfs_trans_handle *trans; |
| 6104 | struct btrfs_root *reloc_root; |
| 6105 | struct btrfs_root *prev_root = NULL; |
| 6106 | struct list_head dead_roots; |
| 6107 | int ret; |
| 6108 | unsigned long nr; |
| 6109 | |
| 6110 | INIT_LIST_HEAD(&dead_roots); |
| 6111 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 6112 | |
| 6113 | while (!list_empty(&dead_roots)) { |
| 6114 | reloc_root = list_entry(dead_roots.prev, |
| 6115 | struct btrfs_root, dead_list); |
| 6116 | list_del_init(&reloc_root->dead_list); |
| 6117 | |
| 6118 | BUG_ON(reloc_root->commit_root != NULL); |
| 6119 | while (1) { |
| 6120 | trans = btrfs_join_transaction(root, 1); |
| 6121 | BUG_ON(!trans); |
| 6122 | |
| 6123 | mutex_lock(&root->fs_info->drop_mutex); |
| 6124 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 6125 | if (ret != -EAGAIN) |
| 6126 | break; |
| 6127 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6128 | |
| 6129 | nr = trans->blocks_used; |
| 6130 | ret = btrfs_end_transaction(trans, root); |
| 6131 | BUG_ON(ret); |
| 6132 | btrfs_btree_balance_dirty(root, nr); |
| 6133 | } |
| 6134 | |
| 6135 | free_extent_buffer(reloc_root->node); |
| 6136 | |
| 6137 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 6138 | &reloc_root->root_key); |
| 6139 | BUG_ON(ret); |
| 6140 | mutex_unlock(&root->fs_info->drop_mutex); |
| 6141 | |
| 6142 | nr = trans->blocks_used; |
| 6143 | ret = btrfs_end_transaction(trans, root); |
| 6144 | BUG_ON(ret); |
| 6145 | btrfs_btree_balance_dirty(root, nr); |
| 6146 | |
| 6147 | kfree(prev_root); |
| 6148 | prev_root = reloc_root; |
| 6149 | } |
| 6150 | if (prev_root) { |
| 6151 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 6152 | kfree(prev_root); |
| 6153 | } |
| 6154 | return 0; |
| 6155 | } |
| 6156 | |
| 6157 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 6158 | { |
| 6159 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 6160 | return 0; |
| 6161 | } |
| 6162 | |
| 6163 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 6164 | { |
| 6165 | struct btrfs_root *reloc_root; |
| 6166 | struct btrfs_trans_handle *trans; |
| 6167 | struct btrfs_key location; |
| 6168 | int found; |
| 6169 | int ret; |
| 6170 | |
| 6171 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6172 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 6173 | BUG_ON(ret); |
| 6174 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 6175 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 6176 | |
| 6177 | if (found) { |
| 6178 | trans = btrfs_start_transaction(root, 1); |
| 6179 | BUG_ON(!trans); |
| 6180 | ret = btrfs_commit_transaction(trans, root); |
| 6181 | BUG_ON(ret); |
| 6182 | } |
| 6183 | |
| 6184 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 6185 | location.offset = (u64)-1; |
| 6186 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 6187 | |
| 6188 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 6189 | BUG_ON(!reloc_root); |
| 6190 | btrfs_orphan_cleanup(reloc_root); |
| 6191 | return 0; |
| 6192 | } |
| 6193 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6194 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6195 | struct btrfs_root *root) |
| 6196 | { |
| 6197 | struct btrfs_root *reloc_root; |
| 6198 | struct extent_buffer *eb; |
| 6199 | struct btrfs_root_item *root_item; |
| 6200 | struct btrfs_key root_key; |
| 6201 | int ret; |
| 6202 | |
| 6203 | BUG_ON(!root->ref_cows); |
| 6204 | if (root->reloc_root) |
| 6205 | return 0; |
| 6206 | |
| 6207 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 6208 | BUG_ON(!root_item); |
| 6209 | |
| 6210 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 6211 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 6212 | BUG_ON(ret); |
| 6213 | |
| 6214 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 6215 | root_key.offset = root->root_key.objectid; |
| 6216 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6217 | |
| 6218 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 6219 | btrfs_set_root_refs(root_item, 0); |
| 6220 | btrfs_set_root_bytenr(root_item, eb->start); |
| 6221 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6222 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6223 | |
| 6224 | btrfs_tree_unlock(eb); |
| 6225 | free_extent_buffer(eb); |
| 6226 | |
| 6227 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 6228 | &root_key, root_item); |
| 6229 | BUG_ON(ret); |
| 6230 | kfree(root_item); |
| 6231 | |
| 6232 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 6233 | &root_key); |
| 6234 | BUG_ON(!reloc_root); |
| 6235 | reloc_root->last_trans = trans->transid; |
| 6236 | reloc_root->commit_root = NULL; |
| 6237 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 6238 | |
| 6239 | root->reloc_root = reloc_root; |
| 6240 | return 0; |
| 6241 | } |
| 6242 | |
| 6243 | /* |
| 6244 | * Core function of space balance. |
| 6245 | * |
| 6246 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6247 | * counted roots. There is one reloc tree for each subvol, and all |
| 6248 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 6249 | * of the latest committed roots of subvols (root->commit_root). |
| 6250 | * |
| 6251 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 6252 | * COW the block through subvol's reloc tree, then update block pointer |
| 6253 | * in the subvol to point to the new block. Since all reloc trees share |
| 6254 | * same root key objectid, doing special handing for tree blocks owned |
| 6255 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 6256 | * we can use the resulting new block directly when the same block is |
| 6257 | * required to COW again through other reloc trees. By this way, relocated |
| 6258 | * tree blocks are shared between reloc trees, so they are also shared |
| 6259 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6260 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6261 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6262 | struct btrfs_root *root, |
| 6263 | struct btrfs_path *path, |
| 6264 | struct btrfs_key *first_key, |
| 6265 | struct btrfs_ref_path *ref_path, |
| 6266 | struct btrfs_block_group_cache *group, |
| 6267 | struct inode *reloc_inode) |
| 6268 | { |
| 6269 | struct btrfs_root *reloc_root; |
| 6270 | struct extent_buffer *eb = NULL; |
| 6271 | struct btrfs_key *keys; |
| 6272 | u64 *nodes; |
| 6273 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6274 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6275 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6276 | int ret; |
| 6277 | |
| 6278 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 6279 | lowest_level = ref_path->owner_objectid; |
| 6280 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6281 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6282 | path->lowest_level = lowest_level; |
| 6283 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 6284 | BUG_ON(ret < 0); |
| 6285 | path->lowest_level = 0; |
| 6286 | btrfs_release_path(root, path); |
| 6287 | return 0; |
| 6288 | } |
| 6289 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6290 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 6291 | ret = init_reloc_tree(trans, root); |
| 6292 | BUG_ON(ret); |
| 6293 | reloc_root = root->reloc_root; |
| 6294 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6295 | shared_level = ref_path->shared_level; |
| 6296 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6297 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6298 | keys = ref_path->node_keys; |
| 6299 | nodes = ref_path->new_nodes; |
| 6300 | memset(&keys[shared_level + 1], 0, |
| 6301 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 6302 | memset(&nodes[shared_level + 1], 0, |
| 6303 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6304 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6305 | if (nodes[lowest_level] == 0) { |
| 6306 | path->lowest_level = lowest_level; |
| 6307 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6308 | 0, 1); |
| 6309 | BUG_ON(ret); |
| 6310 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 6311 | eb = path->nodes[level]; |
| 6312 | if (!eb || eb == reloc_root->node) |
| 6313 | break; |
| 6314 | nodes[level] = eb->start; |
| 6315 | if (level == 0) |
| 6316 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 6317 | else |
| 6318 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 6319 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6320 | if (nodes[0] && |
| 6321 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6322 | eb = path->nodes[0]; |
| 6323 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 6324 | group, reloc_inode); |
| 6325 | BUG_ON(ret); |
| 6326 | } |
| 6327 | btrfs_release_path(reloc_root, path); |
| 6328 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6329 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6330 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6331 | BUG_ON(ret); |
| 6332 | } |
| 6333 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6334 | /* |
| 6335 | * replace tree blocks in the fs tree with tree blocks in |
| 6336 | * the reloc tree. |
| 6337 | */ |
| 6338 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 6339 | BUG_ON(ret < 0); |
| 6340 | |
| 6341 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6342 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 6343 | 0, 0); |
| 6344 | BUG_ON(ret); |
| 6345 | extent_buffer_get(path->nodes[0]); |
| 6346 | eb = path->nodes[0]; |
| 6347 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6348 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 6349 | BUG_ON(ret); |
| 6350 | free_extent_buffer(eb); |
| 6351 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6352 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6353 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6354 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6355 | return 0; |
| 6356 | } |
| 6357 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6358 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6359 | struct btrfs_root *root, |
| 6360 | struct btrfs_path *path, |
| 6361 | struct btrfs_key *first_key, |
| 6362 | struct btrfs_ref_path *ref_path) |
| 6363 | { |
| 6364 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6365 | |
| 6366 | ret = relocate_one_path(trans, root, path, first_key, |
| 6367 | ref_path, NULL, NULL); |
| 6368 | BUG_ON(ret); |
| 6369 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6370 | return 0; |
| 6371 | } |
| 6372 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6373 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6374 | struct btrfs_root *extent_root, |
| 6375 | struct btrfs_path *path, |
| 6376 | struct btrfs_key *extent_key) |
| 6377 | { |
| 6378 | int ret; |
| 6379 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6380 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 6381 | if (ret) |
| 6382 | goto out; |
| 6383 | ret = btrfs_del_item(trans, extent_root, path); |
| 6384 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6385 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6386 | return ret; |
| 6387 | } |
| 6388 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6389 | 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] | 6390 | struct btrfs_ref_path *ref_path) |
| 6391 | { |
| 6392 | struct btrfs_key root_key; |
| 6393 | |
| 6394 | root_key.objectid = ref_path->root_objectid; |
| 6395 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6396 | if (is_cowonly_root(ref_path->root_objectid)) |
| 6397 | root_key.offset = 0; |
| 6398 | else |
| 6399 | root_key.offset = (u64)-1; |
| 6400 | |
| 6401 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 6402 | } |
| 6403 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6404 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6405 | struct btrfs_path *path, |
| 6406 | struct btrfs_key *extent_key, |
| 6407 | struct btrfs_block_group_cache *group, |
| 6408 | struct inode *reloc_inode, int pass) |
| 6409 | { |
| 6410 | struct btrfs_trans_handle *trans; |
| 6411 | struct btrfs_root *found_root; |
| 6412 | struct btrfs_ref_path *ref_path = NULL; |
| 6413 | struct disk_extent *new_extents = NULL; |
| 6414 | int nr_extents = 0; |
| 6415 | int loops; |
| 6416 | int ret; |
| 6417 | int level; |
| 6418 | struct btrfs_key first_key; |
| 6419 | u64 prev_block = 0; |
| 6420 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6421 | |
| 6422 | trans = btrfs_start_transaction(extent_root, 1); |
| 6423 | BUG_ON(!trans); |
| 6424 | |
| 6425 | if (extent_key->objectid == 0) { |
| 6426 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 6427 | goto out; |
| 6428 | } |
| 6429 | |
| 6430 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 6431 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6432 | ret = -ENOMEM; |
| 6433 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6434 | } |
| 6435 | |
| 6436 | for (loops = 0; ; loops++) { |
| 6437 | if (loops == 0) { |
| 6438 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 6439 | extent_key->objectid); |
| 6440 | } else { |
| 6441 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 6442 | } |
| 6443 | if (ret < 0) |
| 6444 | goto out; |
| 6445 | if (ret > 0) |
| 6446 | break; |
| 6447 | |
| 6448 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 6449 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 6450 | continue; |
| 6451 | |
| 6452 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 6453 | BUG_ON(!found_root); |
| 6454 | /* |
| 6455 | * for reference counted tree, only process reference paths |
| 6456 | * rooted at the latest committed root. |
| 6457 | */ |
| 6458 | if (found_root->ref_cows && |
| 6459 | ref_path->root_generation != found_root->root_key.offset) |
| 6460 | continue; |
| 6461 | |
| 6462 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 6463 | if (pass == 0) { |
| 6464 | /* |
| 6465 | * copy data extents to new locations |
| 6466 | */ |
| 6467 | u64 group_start = group->key.objectid; |
| 6468 | ret = relocate_data_extent(reloc_inode, |
| 6469 | extent_key, |
| 6470 | group_start); |
| 6471 | if (ret < 0) |
| 6472 | goto out; |
| 6473 | break; |
| 6474 | } |
| 6475 | level = 0; |
| 6476 | } else { |
| 6477 | level = ref_path->owner_objectid; |
| 6478 | } |
| 6479 | |
| 6480 | if (prev_block != ref_path->nodes[level]) { |
| 6481 | struct extent_buffer *eb; |
| 6482 | u64 block_start = ref_path->nodes[level]; |
| 6483 | u64 block_size = btrfs_level_size(found_root, level); |
| 6484 | |
| 6485 | eb = read_tree_block(found_root, block_start, |
| 6486 | block_size, 0); |
| 6487 | btrfs_tree_lock(eb); |
| 6488 | BUG_ON(level != btrfs_header_level(eb)); |
| 6489 | |
| 6490 | if (level == 0) |
| 6491 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 6492 | else |
| 6493 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 6494 | |
| 6495 | btrfs_tree_unlock(eb); |
| 6496 | free_extent_buffer(eb); |
| 6497 | prev_block = block_start; |
| 6498 | } |
| 6499 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 6500 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6501 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 6502 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6503 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 6504 | /* |
| 6505 | * try to update data extent references while |
| 6506 | * keeping metadata shared between snapshots. |
| 6507 | */ |
| 6508 | if (pass == 1) { |
| 6509 | ret = relocate_one_path(trans, found_root, |
| 6510 | path, &first_key, ref_path, |
| 6511 | group, reloc_inode); |
| 6512 | if (ret < 0) |
| 6513 | goto out; |
| 6514 | continue; |
| 6515 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6516 | /* |
| 6517 | * use fallback method to process the remaining |
| 6518 | * references. |
| 6519 | */ |
| 6520 | if (!new_extents) { |
| 6521 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6522 | new_extents = kmalloc(sizeof(*new_extents), |
| 6523 | GFP_NOFS); |
| 6524 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6525 | ret = get_new_locations(reloc_inode, |
| 6526 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6527 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6528 | &new_extents, |
| 6529 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6530 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6531 | goto out; |
| 6532 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6533 | ret = replace_one_extent(trans, found_root, |
| 6534 | path, extent_key, |
| 6535 | &first_key, ref_path, |
| 6536 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 6537 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6538 | ret = relocate_tree_block(trans, found_root, path, |
| 6539 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6540 | } |
| 6541 | if (ret < 0) |
| 6542 | goto out; |
| 6543 | } |
| 6544 | ret = 0; |
| 6545 | out: |
| 6546 | btrfs_end_transaction(trans, extent_root); |
| 6547 | kfree(new_extents); |
| 6548 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6549 | return ret; |
| 6550 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6551 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6552 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6553 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 6554 | { |
| 6555 | u64 num_devices; |
| 6556 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 6557 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 6558 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6559 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6560 | if (num_devices == 1) { |
| 6561 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6562 | stripped = flags & ~stripped; |
| 6563 | |
| 6564 | /* turn raid0 into single device chunks */ |
| 6565 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 6566 | return stripped; |
| 6567 | |
| 6568 | /* turn mirroring into duplication */ |
| 6569 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6570 | BTRFS_BLOCK_GROUP_RAID10)) |
| 6571 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 6572 | return flags; |
| 6573 | } else { |
| 6574 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6575 | if (flags & stripped) |
| 6576 | return flags; |
| 6577 | |
| 6578 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6579 | stripped = flags & ~stripped; |
| 6580 | |
| 6581 | /* switch duplicated blocks with raid1 */ |
| 6582 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 6583 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 6584 | |
| 6585 | /* turn single device chunks into raid0 */ |
| 6586 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 6587 | } |
| 6588 | return flags; |
| 6589 | } |
| 6590 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 6591 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6592 | struct btrfs_block_group_cache *shrink_block_group, |
| 6593 | int force) |
| 6594 | { |
| 6595 | struct btrfs_trans_handle *trans; |
| 6596 | u64 new_alloc_flags; |
| 6597 | u64 calc; |
| 6598 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6599 | spin_lock(&shrink_block_group->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6600 | if (btrfs_block_group_used(&shrink_block_group->item) + |
| 6601 | shrink_block_group->reserved > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6602 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6603 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6604 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6605 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6606 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6607 | new_alloc_flags = update_block_group_flags(root, |
| 6608 | shrink_block_group->flags); |
| 6609 | if (new_alloc_flags != shrink_block_group->flags) { |
| 6610 | calc = |
| 6611 | btrfs_block_group_used(&shrink_block_group->item); |
| 6612 | } else { |
| 6613 | calc = shrink_block_group->key.offset; |
| 6614 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6615 | spin_unlock(&shrink_block_group->lock); |
| 6616 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6617 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 6618 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6619 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6620 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6621 | } else |
| 6622 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6623 | return 0; |
| 6624 | } |
| 6625 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6626 | |
| 6627 | int btrfs_prepare_block_group_relocation(struct btrfs_root *root, |
| 6628 | struct btrfs_block_group_cache *group) |
| 6629 | |
| 6630 | { |
| 6631 | __alloc_chunk_for_shrink(root, group, 1); |
| 6632 | set_block_group_readonly(group); |
| 6633 | return 0; |
| 6634 | } |
| 6635 | |
| 6636 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6637 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 6638 | struct btrfs_root *root, |
| 6639 | u64 objectid, u64 size) |
| 6640 | { |
| 6641 | struct btrfs_path *path; |
| 6642 | struct btrfs_inode_item *item; |
| 6643 | struct extent_buffer *leaf; |
| 6644 | int ret; |
| 6645 | |
| 6646 | path = btrfs_alloc_path(); |
| 6647 | if (!path) |
| 6648 | return -ENOMEM; |
| 6649 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6650 | path->leave_spinning = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6651 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 6652 | if (ret) |
| 6653 | goto out; |
| 6654 | |
| 6655 | leaf = path->nodes[0]; |
| 6656 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 6657 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 6658 | btrfs_set_inode_generation(leaf, item, 1); |
| 6659 | btrfs_set_inode_size(leaf, item, size); |
| 6660 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 6661 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6662 | btrfs_mark_buffer_dirty(leaf); |
| 6663 | btrfs_release_path(root, path); |
| 6664 | out: |
| 6665 | btrfs_free_path(path); |
| 6666 | return ret; |
| 6667 | } |
| 6668 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6669 | static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6670 | struct btrfs_block_group_cache *group) |
| 6671 | { |
| 6672 | struct inode *inode = NULL; |
| 6673 | struct btrfs_trans_handle *trans; |
| 6674 | struct btrfs_root *root; |
| 6675 | struct btrfs_key root_key; |
| 6676 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 6677 | int err = 0; |
| 6678 | |
| 6679 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 6680 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6681 | root_key.offset = (u64)-1; |
| 6682 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 6683 | if (IS_ERR(root)) |
| 6684 | return ERR_CAST(root); |
| 6685 | |
| 6686 | trans = btrfs_start_transaction(root, 1); |
| 6687 | BUG_ON(!trans); |
| 6688 | |
| 6689 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 6690 | if (err) |
| 6691 | goto out; |
| 6692 | |
| 6693 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 6694 | BUG_ON(err); |
| 6695 | |
| 6696 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6697 | group->key.offset, 0, group->key.offset, |
| 6698 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6699 | BUG_ON(err); |
| 6700 | |
| 6701 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 6702 | if (inode->i_state & I_NEW) { |
| 6703 | BTRFS_I(inode)->root = root; |
| 6704 | BTRFS_I(inode)->location.objectid = objectid; |
| 6705 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 6706 | BTRFS_I(inode)->location.offset = 0; |
| 6707 | btrfs_read_locked_inode(inode); |
| 6708 | unlock_new_inode(inode); |
| 6709 | BUG_ON(is_bad_inode(inode)); |
| 6710 | } else { |
| 6711 | BUG_ON(1); |
| 6712 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 6713 | BTRFS_I(inode)->index_cnt = group->key.objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6714 | |
| 6715 | err = btrfs_orphan_add(trans, inode); |
| 6716 | out: |
| 6717 | btrfs_end_transaction(trans, root); |
| 6718 | if (err) { |
| 6719 | if (inode) |
| 6720 | iput(inode); |
| 6721 | inode = ERR_PTR(err); |
| 6722 | } |
| 6723 | return inode; |
| 6724 | } |
| 6725 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 6726 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) |
| 6727 | { |
| 6728 | |
| 6729 | struct btrfs_ordered_sum *sums; |
| 6730 | struct btrfs_sector_sum *sector_sum; |
| 6731 | struct btrfs_ordered_extent *ordered; |
| 6732 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6733 | struct list_head list; |
| 6734 | size_t offset; |
| 6735 | int ret; |
| 6736 | u64 disk_bytenr; |
| 6737 | |
| 6738 | INIT_LIST_HEAD(&list); |
| 6739 | |
| 6740 | ordered = btrfs_lookup_ordered_extent(inode, file_pos); |
| 6741 | BUG_ON(ordered->file_offset != file_pos || ordered->len != len); |
| 6742 | |
| 6743 | disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 6744 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 6745 | disk_bytenr + len - 1, &list); |
| 6746 | |
| 6747 | while (!list_empty(&list)) { |
| 6748 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 6749 | list_del_init(&sums->list); |
| 6750 | |
| 6751 | sector_sum = sums->sums; |
| 6752 | sums->bytenr = ordered->start; |
| 6753 | |
| 6754 | offset = 0; |
| 6755 | while (offset < sums->len) { |
| 6756 | sector_sum->bytenr += ordered->start - disk_bytenr; |
| 6757 | sector_sum++; |
| 6758 | offset += root->sectorsize; |
| 6759 | } |
| 6760 | |
| 6761 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 6762 | } |
| 6763 | btrfs_put_ordered_extent(ordered); |
| 6764 | return 0; |
| 6765 | } |
| 6766 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6767 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6768 | { |
| 6769 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6770 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6771 | struct btrfs_fs_info *info = root->fs_info; |
| 6772 | struct extent_buffer *leaf; |
| 6773 | struct inode *reloc_inode; |
| 6774 | struct btrfs_block_group_cache *block_group; |
| 6775 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6776 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6777 | u64 cur_byte; |
| 6778 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6779 | u32 nritems; |
| 6780 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6781 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6782 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6783 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6784 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6785 | |
| 6786 | block_group = btrfs_lookup_block_group(info, group_start); |
| 6787 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6788 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6789 | printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6790 | (unsigned long long)block_group->key.objectid, |
| 6791 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6792 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6793 | path = btrfs_alloc_path(); |
| 6794 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6795 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6796 | reloc_inode = create_reloc_inode(info, block_group); |
| 6797 | BUG_ON(IS_ERR(reloc_inode)); |
| 6798 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6799 | __alloc_chunk_for_shrink(root, block_group, 1); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6800 | set_block_group_readonly(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6801 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6802 | btrfs_start_delalloc_inodes(info->tree_root); |
| 6803 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6804 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6805 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6806 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6807 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6808 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6809 | key.offset = 0; |
| 6810 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6811 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6812 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6813 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 6814 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6815 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6816 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 6817 | btrfs_clean_old_snapshots(info->tree_root); |
| 6818 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 6819 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6820 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6821 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 6822 | btrfs_commit_transaction(trans, info->tree_root); |
| 6823 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6824 | while (1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6825 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6826 | if (ret < 0) |
| 6827 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6828 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6829 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6830 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6831 | if (path->slots[0] >= nritems) { |
| 6832 | ret = btrfs_next_leaf(root, path); |
| 6833 | if (ret < 0) |
| 6834 | goto out; |
| 6835 | if (ret == 1) { |
| 6836 | ret = 0; |
| 6837 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6838 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6839 | leaf = path->nodes[0]; |
| 6840 | nritems = btrfs_header_nritems(leaf); |
| 6841 | } |
| 6842 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6843 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6844 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6845 | if (key.objectid >= block_group->key.objectid + |
| 6846 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 6847 | break; |
| 6848 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6849 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6850 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6851 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6852 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6853 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6854 | } |
| 6855 | progress = 1; |
| 6856 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6857 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 6858 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6859 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6860 | goto next; |
| 6861 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6862 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6863 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6864 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6865 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6866 | |
| 6867 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 6868 | ret = relocate_one_extent(root, path, &key, block_group, |
| 6869 | reloc_inode, pass); |
| 6870 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6871 | if (ret > 0) |
| 6872 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6873 | |
| 6874 | key.objectid = cur_byte; |
| 6875 | key.type = 0; |
| 6876 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6877 | } |
| 6878 | |
| 6879 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6880 | |
| 6881 | if (pass == 0) { |
| 6882 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 6883 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6884 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6885 | |
| 6886 | if (total_found > 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6887 | printk(KERN_INFO "btrfs found %llu extents in pass %d\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6888 | (unsigned long long)total_found, pass); |
| 6889 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6890 | if (total_found == skipped && pass > 2) { |
| 6891 | iput(reloc_inode); |
| 6892 | reloc_inode = create_reloc_inode(info, block_group); |
| 6893 | pass = 0; |
| 6894 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6895 | goto again; |
| 6896 | } |
| 6897 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6898 | /* delete reloc_inode */ |
| 6899 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6900 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6901 | /* unpin extents in this range */ |
| 6902 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 6903 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6904 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6905 | spin_lock(&block_group->lock); |
| 6906 | WARN_ON(block_group->pinned > 0); |
| 6907 | WARN_ON(block_group->reserved > 0); |
| 6908 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 6909 | spin_unlock(&block_group->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6910 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6911 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6912 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6913 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6914 | return ret; |
| 6915 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6916 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6917 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 6918 | static int find_first_block_group(struct btrfs_root *root, |
| 6919 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6920 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6921 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6922 | struct btrfs_key found_key; |
| 6923 | struct extent_buffer *leaf; |
| 6924 | int slot; |
| 6925 | |
| 6926 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 6927 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6928 | goto out; |
| 6929 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6930 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6931 | slot = path->slots[0]; |
| 6932 | leaf = path->nodes[0]; |
| 6933 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6934 | ret = btrfs_next_leaf(root, path); |
| 6935 | if (ret == 0) |
| 6936 | continue; |
| 6937 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6938 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6939 | break; |
| 6940 | } |
| 6941 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6942 | |
| 6943 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6944 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 6945 | ret = 0; |
| 6946 | goto out; |
| 6947 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6948 | path->slots[0]++; |
| 6949 | } |
| 6950 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6951 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6952 | return ret; |
| 6953 | } |
| 6954 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6955 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 6956 | { |
| 6957 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6958 | struct btrfs_space_info *space_info; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6959 | struct rb_node *n; |
| 6960 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6961 | spin_lock(&info->block_group_cache_lock); |
| 6962 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 6963 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 6964 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6965 | rb_erase(&block_group->cache_node, |
| 6966 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6967 | spin_unlock(&info->block_group_cache_lock); |
| 6968 | |
| 6969 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6970 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6971 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6972 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6973 | |
| 6974 | WARN_ON(atomic_read(&block_group->count) != 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6975 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6976 | |
| 6977 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6978 | } |
| 6979 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6980 | |
| 6981 | /* now that all the block groups are freed, go through and |
| 6982 | * free all the space_info structs. This is only called during |
| 6983 | * the final stages of unmount, and so we know nobody is |
| 6984 | * using them. We call synchronize_rcu() once before we start, |
| 6985 | * just to be on the safe side. |
| 6986 | */ |
| 6987 | synchronize_rcu(); |
| 6988 | |
| 6989 | while(!list_empty(&info->space_info)) { |
| 6990 | space_info = list_entry(info->space_info.next, |
| 6991 | struct btrfs_space_info, |
| 6992 | list); |
| 6993 | |
| 6994 | list_del(&space_info->list); |
| 6995 | kfree(space_info); |
| 6996 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6997 | return 0; |
| 6998 | } |
| 6999 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7000 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7001 | { |
| 7002 | struct btrfs_path *path; |
| 7003 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7004 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7005 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7006 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7007 | struct btrfs_key key; |
| 7008 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7009 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7010 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7011 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7012 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7013 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7014 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7015 | path = btrfs_alloc_path(); |
| 7016 | if (!path) |
| 7017 | return -ENOMEM; |
| 7018 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7019 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7020 | ret = find_first_block_group(root, path, &key); |
| 7021 | if (ret > 0) { |
| 7022 | ret = 0; |
| 7023 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7024 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7025 | if (ret != 0) |
| 7026 | goto error; |
| 7027 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7028 | leaf = path->nodes[0]; |
| 7029 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7030 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7031 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7032 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7033 | break; |
| 7034 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7035 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7036 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7037 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7038 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7039 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7040 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7041 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame^] | 7042 | cache->sectorsize = root->sectorsize; |
| 7043 | |
| 7044 | /* |
| 7045 | * we only want to have 32k of ram per block group for keeping |
| 7046 | * track of free space, and if we pass 1/2 of that we want to |
| 7047 | * start converting things over to using bitmaps |
| 7048 | */ |
| 7049 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7050 | sizeof(struct btrfs_free_space); |
| 7051 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7052 | read_extent_buffer(leaf, &cache->item, |
| 7053 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7054 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7055 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7056 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7057 | key.objectid = found_key.objectid + found_key.offset; |
| 7058 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7059 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7060 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7061 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7062 | btrfs_block_group_used(&cache->item), |
| 7063 | &space_info); |
| 7064 | BUG_ON(ret); |
| 7065 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7066 | down_write(&space_info->groups_sem); |
| 7067 | list_add_tail(&cache->list, &space_info->block_groups); |
| 7068 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7069 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7070 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7071 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7072 | |
| 7073 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7074 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 7075 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7076 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7077 | ret = 0; |
| 7078 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7079 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7080 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7081 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7082 | |
| 7083 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7084 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7085 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7086 | u64 size) |
| 7087 | { |
| 7088 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7089 | struct btrfs_root *extent_root; |
| 7090 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7091 | |
| 7092 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7093 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7094 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7095 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7096 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7097 | if (!cache) |
| 7098 | return -ENOMEM; |
| 7099 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7100 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7101 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7102 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame^] | 7103 | cache->sectorsize = root->sectorsize; |
| 7104 | |
| 7105 | /* |
| 7106 | * we only want to have 32k of ram per block group for keeping track |
| 7107 | * of free space, and if we pass 1/2 of that we want to start |
| 7108 | * converting things over to using bitmaps |
| 7109 | */ |
| 7110 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 7111 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7112 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7113 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 7114 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 7115 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7116 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7117 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7118 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7119 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7120 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7121 | cache->flags = type; |
| 7122 | btrfs_set_block_group_flags(&cache->item, type); |
| 7123 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame^] | 7124 | cache->cached = 1; |
| 7125 | ret = btrfs_add_free_space(cache, chunk_offset, size); |
| 7126 | BUG_ON(ret); |
| 7127 | remove_sb_from_cache(root, cache); |
| 7128 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7129 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7130 | &cache->space_info); |
| 7131 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7132 | down_write(&cache->space_info->groups_sem); |
| 7133 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 7134 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7135 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7136 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7137 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7138 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7139 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7140 | sizeof(cache->item)); |
| 7141 | BUG_ON(ret); |
| 7142 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7143 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7144 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7145 | return 0; |
| 7146 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7147 | |
| 7148 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7149 | struct btrfs_root *root, u64 group_start) |
| 7150 | { |
| 7151 | struct btrfs_path *path; |
| 7152 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7153 | struct btrfs_free_cluster *cluster; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7154 | struct btrfs_key key; |
| 7155 | int ret; |
| 7156 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7157 | root = root->fs_info->extent_root; |
| 7158 | |
| 7159 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7160 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7161 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7162 | |
| 7163 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7164 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7165 | /* make sure this block group isn't part of an allocation cluster */ |
| 7166 | cluster = &root->fs_info->data_alloc_cluster; |
| 7167 | spin_lock(&cluster->refill_lock); |
| 7168 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7169 | spin_unlock(&cluster->refill_lock); |
| 7170 | |
| 7171 | /* |
| 7172 | * make sure this block group isn't part of a metadata |
| 7173 | * allocation cluster |
| 7174 | */ |
| 7175 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7176 | spin_lock(&cluster->refill_lock); |
| 7177 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7178 | spin_unlock(&cluster->refill_lock); |
| 7179 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7180 | path = btrfs_alloc_path(); |
| 7181 | BUG_ON(!path); |
| 7182 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7183 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7184 | rb_erase(&block_group->cache_node, |
| 7185 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7186 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 7187 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7188 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7189 | /* |
| 7190 | * we must use list_del_init so people can check to see if they |
| 7191 | * are still on the list after taking the semaphore |
| 7192 | */ |
| 7193 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7194 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7195 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7196 | spin_lock(&block_group->space_info->lock); |
| 7197 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7198 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 7199 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7200 | block_group->space_info->full = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7201 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7202 | btrfs_put_block_group(block_group); |
| 7203 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7204 | |
| 7205 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7206 | if (ret > 0) |
| 7207 | ret = -EIO; |
| 7208 | if (ret < 0) |
| 7209 | goto out; |
| 7210 | |
| 7211 | ret = btrfs_del_item(trans, root, path); |
| 7212 | out: |
| 7213 | btrfs_free_path(path); |
| 7214 | return ret; |
| 7215 | } |