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" |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 26 | #include "crc32c.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 27 | #include "ctree.h" |
| 28 | #include "disk-io.h" |
| 29 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 30 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 31 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 32 | #include "locking.h" |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 33 | #include "ref-cache.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 34 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 36 | #define PENDING_EXTENT_INSERT 0 |
| 37 | #define PENDING_EXTENT_DELETE 1 |
| 38 | #define PENDING_BACKREF_UPDATE 2 |
| 39 | |
| 40 | struct pending_extent_op { |
| 41 | int type; |
| 42 | u64 bytenr; |
| 43 | u64 num_bytes; |
| 44 | u64 parent; |
| 45 | u64 orig_parent; |
| 46 | u64 generation; |
| 47 | u64 orig_generation; |
| 48 | int level; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 49 | struct list_head list; |
| 50 | int del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 51 | }; |
| 52 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 53 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
| 54 | struct btrfs_root *root, u64 parent, |
| 55 | u64 root_objectid, u64 ref_generation, |
| 56 | u64 owner, struct btrfs_key *ins, |
| 57 | int ref_mod); |
| 58 | static int update_reserved_extents(struct btrfs_root *root, |
| 59 | u64 bytenr, u64 num, int reserve); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 60 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 61 | struct btrfs_root *root, |
| 62 | u64 bytenr, u64 num_bytes, int alloc, |
| 63 | int mark_free); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 64 | static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 65 | struct btrfs_root *root, |
| 66 | u64 bytenr, u64 num_bytes, u64 parent, |
| 67 | u64 root_objectid, u64 ref_generation, |
| 68 | u64 owner_objectid, int pin, |
| 69 | int ref_to_drop); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 70 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 71 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 72 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 73 | u64 flags, int force); |
| 74 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 75 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 76 | { |
| 77 | return (cache->flags & bits) == bits; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * this adds the block group to the fs_info rb tree for the block group |
| 82 | * cache |
| 83 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 84 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 85 | struct btrfs_block_group_cache *block_group) |
| 86 | { |
| 87 | struct rb_node **p; |
| 88 | struct rb_node *parent = NULL; |
| 89 | struct btrfs_block_group_cache *cache; |
| 90 | |
| 91 | spin_lock(&info->block_group_cache_lock); |
| 92 | p = &info->block_group_cache_tree.rb_node; |
| 93 | |
| 94 | while (*p) { |
| 95 | parent = *p; |
| 96 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 97 | cache_node); |
| 98 | if (block_group->key.objectid < cache->key.objectid) { |
| 99 | p = &(*p)->rb_left; |
| 100 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 101 | p = &(*p)->rb_right; |
| 102 | } else { |
| 103 | spin_unlock(&info->block_group_cache_lock); |
| 104 | return -EEXIST; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | rb_link_node(&block_group->cache_node, parent, p); |
| 109 | rb_insert_color(&block_group->cache_node, |
| 110 | &info->block_group_cache_tree); |
| 111 | spin_unlock(&info->block_group_cache_lock); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * This will return the block group at or after bytenr if contains is 0, else |
| 118 | * it will return the block group that contains the bytenr |
| 119 | */ |
| 120 | static struct btrfs_block_group_cache * |
| 121 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 122 | int contains) |
| 123 | { |
| 124 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 125 | struct rb_node *n; |
| 126 | u64 end, start; |
| 127 | |
| 128 | spin_lock(&info->block_group_cache_lock); |
| 129 | n = info->block_group_cache_tree.rb_node; |
| 130 | |
| 131 | while (n) { |
| 132 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 133 | cache_node); |
| 134 | end = cache->key.objectid + cache->key.offset - 1; |
| 135 | start = cache->key.objectid; |
| 136 | |
| 137 | if (bytenr < start) { |
| 138 | if (!contains && (!ret || start < ret->key.objectid)) |
| 139 | ret = cache; |
| 140 | n = n->rb_left; |
| 141 | } else if (bytenr > start) { |
| 142 | if (contains && bytenr <= end) { |
| 143 | ret = cache; |
| 144 | break; |
| 145 | } |
| 146 | n = n->rb_right; |
| 147 | } else { |
| 148 | ret = cache; |
| 149 | break; |
| 150 | } |
| 151 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 152 | if (ret) |
| 153 | atomic_inc(&ret->count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 154 | spin_unlock(&info->block_group_cache_lock); |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * this is only called by cache_block_group, since we could have freed extents |
| 161 | * we need to check the pinned_extents for any extents that can't be used yet |
| 162 | * since their free space will be released as soon as the transaction commits. |
| 163 | */ |
| 164 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 165 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 166 | { |
| 167 | u64 extent_start, extent_end, size; |
| 168 | int ret; |
| 169 | |
| 170 | while (start < end) { |
| 171 | ret = find_first_extent_bit(&info->pinned_extents, start, |
| 172 | &extent_start, &extent_end, |
| 173 | EXTENT_DIRTY); |
| 174 | if (ret) |
| 175 | break; |
| 176 | |
| 177 | if (extent_start == start) { |
| 178 | start = extent_end + 1; |
| 179 | } else if (extent_start > start && extent_start < end) { |
| 180 | size = extent_start - start; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 181 | ret = btrfs_add_free_space(block_group, start, |
| 182 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 183 | BUG_ON(ret); |
| 184 | start = extent_end + 1; |
| 185 | } else { |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (start < end) { |
| 191 | size = end - start; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 192 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 193 | BUG_ON(ret); |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 199 | static int remove_sb_from_cache(struct btrfs_root *root, |
| 200 | struct btrfs_block_group_cache *cache) |
| 201 | { |
| 202 | u64 bytenr; |
| 203 | u64 *logical; |
| 204 | int stripe_len; |
| 205 | int i, nr, ret; |
| 206 | |
| 207 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 208 | bytenr = btrfs_sb_offset(i); |
| 209 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 210 | cache->key.objectid, bytenr, 0, |
| 211 | &logical, &nr, &stripe_len); |
| 212 | BUG_ON(ret); |
| 213 | while (nr--) { |
| 214 | btrfs_remove_free_space(cache, logical[nr], |
| 215 | stripe_len); |
| 216 | } |
| 217 | kfree(logical); |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 222 | static int cache_block_group(struct btrfs_root *root, |
| 223 | struct btrfs_block_group_cache *block_group) |
| 224 | { |
| 225 | struct btrfs_path *path; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 226 | int ret = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 227 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 228 | struct extent_buffer *leaf; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 229 | int slot; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 230 | u64 last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 231 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 232 | if (!block_group) |
| 233 | return 0; |
| 234 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 235 | root = root->fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 236 | |
| 237 | if (block_group->cached) |
| 238 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 239 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 240 | path = btrfs_alloc_path(); |
| 241 | if (!path) |
| 242 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 243 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 244 | path->reada = 2; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 245 | /* |
| 246 | * we get into deadlocks with paths held by callers of this function. |
| 247 | * since the alloc_mutex is protecting things right now, just |
| 248 | * skip the locking here |
| 249 | */ |
| 250 | path->skip_locking = 1; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 251 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
| 252 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 253 | key.offset = 0; |
| 254 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 255 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 256 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 257 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 258 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 259 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 260 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 261 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 262 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 263 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 264 | if (ret < 0) |
| 265 | goto err; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 266 | if (ret == 0) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 267 | continue; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 268 | else |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 269 | break; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 270 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 271 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 272 | if (key.objectid < block_group->key.objectid) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 273 | goto next; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 274 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 275 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 276 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 277 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 278 | |
| 279 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 280 | add_new_free_space(block_group, root->fs_info, last, |
| 281 | key.objectid); |
| 282 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 283 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 284 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 285 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 286 | path->slots[0]++; |
| 287 | } |
| 288 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 289 | add_new_free_space(block_group, root->fs_info, last, |
| 290 | block_group->key.objectid + |
| 291 | block_group->key.offset); |
| 292 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 293 | block_group->cached = 1; |
Josef Bacik | 70cb074 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 294 | remove_sb_from_cache(root, block_group); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 295 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 296 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 297 | btrfs_free_path(path); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 298 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 301 | /* |
| 302 | * return the block group that starts at or after bytenr |
| 303 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 304 | static struct btrfs_block_group_cache * |
| 305 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 306 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 307 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 308 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 309 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 310 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 311 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 314 | /* |
| 315 | * return the block group that contains teh given bytenr |
| 316 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 317 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 318 | struct btrfs_fs_info *info, |
| 319 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 320 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 321 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 322 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 323 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 324 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 325 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 326 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 327 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 328 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 329 | { |
| 330 | if (atomic_dec_and_test(&cache->count)) |
| 331 | kfree(cache); |
| 332 | } |
| 333 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 334 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 335 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 336 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 337 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 338 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 339 | |
| 340 | rcu_read_lock(); |
| 341 | list_for_each_entry_rcu(found, head, list) { |
| 342 | if (found->flags == flags) { |
| 343 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 344 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 345 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 346 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 347 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 348 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 351 | /* |
| 352 | * after adding space to the filesystem, we need to clear the full flags |
| 353 | * on all the space infos. |
| 354 | */ |
| 355 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 356 | { |
| 357 | struct list_head *head = &info->space_info; |
| 358 | struct btrfs_space_info *found; |
| 359 | |
| 360 | rcu_read_lock(); |
| 361 | list_for_each_entry_rcu(found, head, list) |
| 362 | found->full = 0; |
| 363 | rcu_read_unlock(); |
| 364 | } |
| 365 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 366 | static u64 div_factor(u64 num, int factor) |
| 367 | { |
| 368 | if (factor == 10) |
| 369 | return num; |
| 370 | num *= factor; |
| 371 | do_div(num, 10); |
| 372 | return num; |
| 373 | } |
| 374 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 375 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 376 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 377 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 378 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 379 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 380 | u64 last = max(search_hint, search_start); |
| 381 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 382 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 383 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 384 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 385 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 386 | while (1) { |
| 387 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 388 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 389 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 390 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 391 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 392 | last = cache->key.objectid + cache->key.offset; |
| 393 | used = btrfs_block_group_used(&cache->item); |
| 394 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 395 | if ((full_search || !cache->ro) && |
| 396 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 397 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 398 | div_factor(cache->key.offset, factor)) { |
| 399 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 400 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 401 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 402 | goto found; |
| 403 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 404 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 405 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 406 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 407 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 408 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 409 | if (!wrapped) { |
| 410 | last = search_start; |
| 411 | wrapped = 1; |
| 412 | goto again; |
| 413 | } |
| 414 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 415 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 416 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 417 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 418 | goto again; |
| 419 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 420 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 421 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 422 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 423 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 424 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 425 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 426 | { |
| 427 | int ret; |
| 428 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 429 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 430 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 431 | path = btrfs_alloc_path(); |
| 432 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 433 | key.objectid = start; |
| 434 | key.offset = len; |
| 435 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 436 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 437 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 438 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 439 | return ret; |
| 440 | } |
| 441 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 442 | /* |
| 443 | * Back reference rules. Back refs have three main goals: |
| 444 | * |
| 445 | * 1) differentiate between all holders of references to an extent so that |
| 446 | * when a reference is dropped we can make sure it was a valid reference |
| 447 | * before freeing the extent. |
| 448 | * |
| 449 | * 2) Provide enough information to quickly find the holders of an extent |
| 450 | * if we notice a given block is corrupted or bad. |
| 451 | * |
| 452 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 453 | * maintenance. This is actually the same as #2, but with a slightly |
| 454 | * different use case. |
| 455 | * |
| 456 | * File extents can be referenced by: |
| 457 | * |
| 458 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 459 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 460 | * - different offsets inside a file (bookend extents in file.c) |
| 461 | * |
| 462 | * The extent ref structure has fields for: |
| 463 | * |
| 464 | * - Objectid of the subvolume root |
| 465 | * - Generation number of the tree holding the reference |
| 466 | * - objectid of the file holding the reference |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 467 | * - number of references holding by parent node (alway 1 for tree blocks) |
| 468 | * |
| 469 | * Btree leaf may hold multiple references to a file extent. In most cases, |
| 470 | * these references are from same file and the corresponding offsets inside |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 471 | * the file are close together. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 472 | * |
| 473 | * When a file extent is allocated the fields are filled in: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 474 | * (root_key.objectid, trans->transid, inode objectid, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 475 | * |
| 476 | * When a leaf is cow'd new references are added for every file extent found |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 477 | * in the leaf. It looks similar to the create case, but trans->transid will |
| 478 | * be different when the block is cow'd. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 479 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 480 | * (root_key.objectid, trans->transid, inode objectid, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 481 | * number of references in the leaf) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 482 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 483 | * When a file extent is removed either during snapshot deletion or |
| 484 | * file truncation, we find the corresponding back reference and check |
| 485 | * the following fields: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 486 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 487 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 488 | * inode objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 489 | * |
| 490 | * Btree extents can be referenced by: |
| 491 | * |
| 492 | * - Different subvolumes |
| 493 | * - Different generations of the same subvolume |
| 494 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 495 | * When a tree block is created, back references are inserted: |
| 496 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 497 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 498 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 499 | * When a tree block is cow'd, new back references are added for all the |
| 500 | * blocks it points to. If the tree block isn't in reference counted root, |
| 501 | * the old back references are removed. These new back references are of |
| 502 | * the form (trans->transid will have increased since creation): |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 503 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 504 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 505 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 506 | * When a backref is in deleting, the following fields are checked: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 507 | * |
| 508 | * if backref was for a tree root: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 509 | * (btrfs_header_owner(itself), btrfs_header_generation(itself), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 510 | * else |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 511 | * (btrfs_header_owner(parent), btrfs_header_generation(parent), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 512 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 513 | * Back Reference Key composing: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 514 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 515 | * The key objectid corresponds to the first byte in the extent, the key |
| 516 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first |
| 517 | * byte of parent extent. If a extent is tree root, the key offset is set |
| 518 | * to the key objectid. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 519 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 520 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 521 | static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 522 | struct btrfs_root *root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 523 | struct btrfs_path *path, |
| 524 | u64 bytenr, u64 parent, |
| 525 | u64 ref_root, u64 ref_generation, |
| 526 | u64 owner_objectid, int del) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 527 | { |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 528 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 529 | struct btrfs_extent_ref *ref; |
| 530 | struct extent_buffer *leaf; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 531 | u64 ref_objectid; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 532 | int ret; |
| 533 | |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 534 | key.objectid = bytenr; |
| 535 | key.type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 536 | key.offset = parent; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 537 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 538 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); |
| 539 | if (ret < 0) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 540 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 541 | if (ret > 0) { |
| 542 | ret = -ENOENT; |
| 543 | goto out; |
| 544 | } |
| 545 | |
| 546 | leaf = path->nodes[0]; |
| 547 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 548 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 549 | if (btrfs_ref_root(leaf, ref) != ref_root || |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 550 | btrfs_ref_generation(leaf, ref) != ref_generation || |
| 551 | (ref_objectid != owner_objectid && |
| 552 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 553 | ret = -EIO; |
| 554 | WARN_ON(1); |
| 555 | goto out; |
| 556 | } |
| 557 | ret = 0; |
| 558 | out: |
| 559 | return ret; |
| 560 | } |
| 561 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 562 | static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 563 | struct btrfs_root *root, |
| 564 | struct btrfs_path *path, |
| 565 | u64 bytenr, u64 parent, |
| 566 | u64 ref_root, u64 ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 567 | u64 owner_objectid, |
| 568 | int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 569 | { |
| 570 | struct btrfs_key key; |
| 571 | struct extent_buffer *leaf; |
| 572 | struct btrfs_extent_ref *ref; |
| 573 | u32 num_refs; |
| 574 | int ret; |
| 575 | |
| 576 | key.objectid = bytenr; |
| 577 | key.type = BTRFS_EXTENT_REF_KEY; |
| 578 | key.offset = parent; |
| 579 | |
| 580 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
| 581 | if (ret == 0) { |
| 582 | leaf = path->nodes[0]; |
| 583 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 584 | struct btrfs_extent_ref); |
| 585 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 586 | btrfs_set_ref_generation(leaf, ref, ref_generation); |
| 587 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 588 | btrfs_set_ref_num_refs(leaf, ref, refs_to_add); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 589 | } else if (ret == -EEXIST) { |
| 590 | u64 existing_owner; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 591 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 592 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); |
| 593 | leaf = path->nodes[0]; |
| 594 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 595 | struct btrfs_extent_ref); |
| 596 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 597 | btrfs_ref_generation(leaf, ref) != ref_generation) { |
| 598 | ret = -EIO; |
| 599 | WARN_ON(1); |
| 600 | goto out; |
| 601 | } |
| 602 | |
| 603 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 604 | BUG_ON(num_refs == 0); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 605 | btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 606 | |
| 607 | existing_owner = btrfs_ref_objectid(leaf, ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 608 | if (existing_owner != owner_objectid && |
| 609 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 610 | btrfs_set_ref_objectid(leaf, ref, |
| 611 | BTRFS_MULTIPLE_OBJECTIDS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 612 | } |
| 613 | ret = 0; |
| 614 | } else { |
| 615 | goto out; |
| 616 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 617 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 618 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 619 | out: |
| 620 | btrfs_release_path(root, path); |
| 621 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 622 | } |
| 623 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 624 | static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 625 | struct btrfs_root *root, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 626 | struct btrfs_path *path, |
| 627 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 628 | { |
| 629 | struct extent_buffer *leaf; |
| 630 | struct btrfs_extent_ref *ref; |
| 631 | u32 num_refs; |
| 632 | int ret = 0; |
| 633 | |
| 634 | leaf = path->nodes[0]; |
| 635 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 636 | num_refs = btrfs_ref_num_refs(leaf, ref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 637 | BUG_ON(num_refs < refs_to_drop); |
| 638 | num_refs -= refs_to_drop; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 639 | if (num_refs == 0) { |
| 640 | ret = btrfs_del_item(trans, root, path); |
| 641 | } else { |
| 642 | btrfs_set_ref_num_refs(leaf, ref, num_refs); |
| 643 | btrfs_mark_buffer_dirty(leaf); |
| 644 | } |
| 645 | btrfs_release_path(root, path); |
| 646 | return ret; |
| 647 | } |
| 648 | |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 649 | #ifdef BIO_RW_DISCARD |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 650 | static void btrfs_issue_discard(struct block_device *bdev, |
| 651 | u64 start, u64 len) |
| 652 | { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 653 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 654 | } |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 655 | #endif |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 656 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 657 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 658 | u64 num_bytes) |
| 659 | { |
| 660 | #ifdef BIO_RW_DISCARD |
| 661 | int ret; |
| 662 | u64 map_length = num_bytes; |
| 663 | struct btrfs_multi_bio *multi = NULL; |
| 664 | |
| 665 | /* Tell the block device(s) that the sectors can be discarded */ |
| 666 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 667 | bytenr, &map_length, &multi, 0); |
| 668 | if (!ret) { |
| 669 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 670 | int i; |
| 671 | |
| 672 | if (map_length > num_bytes) |
| 673 | map_length = num_bytes; |
| 674 | |
| 675 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 676 | btrfs_issue_discard(stripe->dev->bdev, |
| 677 | stripe->physical, |
| 678 | map_length); |
| 679 | } |
| 680 | kfree(multi); |
| 681 | } |
| 682 | |
| 683 | return ret; |
| 684 | #else |
| 685 | return 0; |
| 686 | #endif |
| 687 | } |
| 688 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 689 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 690 | struct btrfs_root *root, u64 bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 691 | u64 num_bytes, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 692 | u64 orig_parent, u64 parent, |
| 693 | u64 orig_root, u64 ref_root, |
| 694 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 695 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 696 | { |
| 697 | int ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 698 | int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 699 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 700 | ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes, |
| 701 | orig_parent, parent, orig_root, |
| 702 | ref_root, orig_generation, |
| 703 | ref_generation, owner_objectid, pin); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 704 | BUG_ON(ret); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 709 | struct btrfs_root *root, u64 bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 710 | u64 num_bytes, u64 orig_parent, u64 parent, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 711 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 712 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 713 | { |
| 714 | int ret; |
| 715 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 716 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 717 | return 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 718 | |
| 719 | ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes, |
| 720 | orig_parent, parent, ref_root, |
| 721 | ref_root, ref_generation, |
| 722 | ref_generation, owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 723 | return ret; |
| 724 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 725 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 726 | struct btrfs_root *root, u64 bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 727 | u64 num_bytes, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 728 | u64 orig_parent, u64 parent, |
| 729 | u64 orig_root, u64 ref_root, |
| 730 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 731 | u64 owner_objectid) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 732 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 733 | int ret; |
| 734 | |
| 735 | ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root, |
| 736 | ref_generation, owner_objectid, |
| 737 | BTRFS_ADD_DELAYED_REF, 0); |
| 738 | BUG_ON(ret); |
| 739 | return ret; |
| 740 | } |
| 741 | |
| 742 | static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans, |
| 743 | struct btrfs_root *root, u64 bytenr, |
| 744 | u64 num_bytes, u64 parent, u64 ref_root, |
| 745 | u64 ref_generation, u64 owner_objectid, |
| 746 | int refs_to_add) |
| 747 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 748 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 749 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 750 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 751 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 752 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 753 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 754 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 755 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 756 | if (!path) |
| 757 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 758 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 759 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 760 | path->leave_spinning = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 761 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 762 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 763 | key.offset = num_bytes; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 764 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 765 | /* first find the extent item and update its reference count */ |
| 766 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 767 | path, 0, 1); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 768 | if (ret < 0) { |
| 769 | btrfs_set_path_blocking(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 770 | return ret; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 771 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 772 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 773 | if (ret > 0) { |
| 774 | WARN_ON(1); |
| 775 | btrfs_free_path(path); |
| 776 | return -EIO; |
| 777 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 778 | l = path->nodes[0]; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 779 | |
| 780 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 781 | if (key.objectid != bytenr) { |
| 782 | btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 783 | printk(KERN_ERR "btrfs wanted %llu found %llu\n", |
| 784 | (unsigned long long)bytenr, |
| 785 | (unsigned long long)key.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 786 | BUG(); |
| 787 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 788 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
| 789 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 790 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 791 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 792 | refs = btrfs_extent_refs(l, item); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 793 | btrfs_set_extent_refs(l, item, refs + refs_to_add); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 794 | btrfs_unlock_up_safe(path, 1); |
| 795 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 796 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 797 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 798 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 799 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 800 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 801 | path->leave_spinning = 1; |
| 802 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 803 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 804 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
| 805 | path, bytenr, parent, |
| 806 | ref_root, ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 807 | owner_objectid, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 808 | BUG_ON(ret); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 809 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 810 | return 0; |
| 811 | } |
| 812 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 813 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 814 | struct btrfs_root *root, |
| 815 | u64 bytenr, u64 num_bytes, u64 parent, |
| 816 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 817 | u64 owner_objectid) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 818 | { |
| 819 | int ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 820 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 821 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 822 | return 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 823 | |
| 824 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 825 | 0, ref_root, 0, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 826 | owner_objectid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 827 | return ret; |
| 828 | } |
| 829 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 830 | static int drop_delayed_ref(struct btrfs_trans_handle *trans, |
| 831 | struct btrfs_root *root, |
| 832 | struct btrfs_delayed_ref_node *node) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 833 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 834 | int ret = 0; |
| 835 | struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node); |
| 836 | |
| 837 | BUG_ON(node->ref_mod == 0); |
| 838 | ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes, |
| 839 | node->parent, ref->root, ref->generation, |
| 840 | ref->owner_objectid, ref->pin, node->ref_mod); |
| 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | /* helper function to actually process a single delayed ref entry */ |
| 846 | static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 847 | struct btrfs_root *root, |
| 848 | struct btrfs_delayed_ref_node *node, |
| 849 | int insert_reserved) |
| 850 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 851 | int ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 852 | struct btrfs_delayed_ref *ref; |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 853 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 854 | if (node->parent == (u64)-1) { |
| 855 | struct btrfs_delayed_ref_head *head; |
| 856 | /* |
| 857 | * we've hit the end of the chain and we were supposed |
| 858 | * to insert this extent into the tree. But, it got |
| 859 | * deleted before we ever needed to insert it, so all |
| 860 | * we have to do is clean up the accounting |
| 861 | */ |
| 862 | if (insert_reserved) { |
| 863 | update_reserved_extents(root, node->bytenr, |
| 864 | node->num_bytes, 0); |
| 865 | } |
| 866 | head = btrfs_delayed_node_to_head(node); |
| 867 | mutex_unlock(&head->mutex); |
| 868 | return 0; |
| 869 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 870 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 871 | ref = btrfs_delayed_node_to_ref(node); |
| 872 | if (ref->action == BTRFS_ADD_DELAYED_REF) { |
| 873 | if (insert_reserved) { |
| 874 | struct btrfs_key ins; |
| 875 | |
| 876 | ins.objectid = node->bytenr; |
| 877 | ins.offset = node->num_bytes; |
| 878 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 879 | |
| 880 | /* record the full extent allocation */ |
| 881 | ret = __btrfs_alloc_reserved_extent(trans, root, |
| 882 | node->parent, ref->root, |
| 883 | ref->generation, ref->owner_objectid, |
| 884 | &ins, node->ref_mod); |
| 885 | update_reserved_extents(root, node->bytenr, |
| 886 | node->num_bytes, 0); |
| 887 | } else { |
| 888 | /* just add one backref */ |
| 889 | ret = add_extent_ref(trans, root, node->bytenr, |
| 890 | node->num_bytes, |
| 891 | node->parent, ref->root, ref->generation, |
| 892 | ref->owner_objectid, node->ref_mod); |
| 893 | } |
| 894 | BUG_ON(ret); |
| 895 | } else if (ref->action == BTRFS_DROP_DELAYED_REF) { |
| 896 | WARN_ON(insert_reserved); |
| 897 | ret = drop_delayed_ref(trans, root, node); |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 898 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 899 | return 0; |
| 900 | } |
| 901 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 902 | static noinline struct btrfs_delayed_ref_node * |
| 903 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 904 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 905 | struct rb_node *node; |
| 906 | struct btrfs_delayed_ref_node *ref; |
| 907 | int action = BTRFS_ADD_DELAYED_REF; |
| 908 | again: |
| 909 | /* |
| 910 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 911 | * this prevents ref count from going down to zero when |
| 912 | * there still are pending delayed ref. |
| 913 | */ |
| 914 | node = rb_prev(&head->node.rb_node); |
| 915 | while (1) { |
| 916 | if (!node) |
| 917 | break; |
| 918 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 919 | rb_node); |
| 920 | if (ref->bytenr != head->node.bytenr) |
| 921 | break; |
| 922 | if (btrfs_delayed_node_to_ref(ref)->action == action) |
| 923 | return ref; |
| 924 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 925 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 926 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 927 | action = BTRFS_DROP_DELAYED_REF; |
| 928 | goto again; |
| 929 | } |
| 930 | return NULL; |
| 931 | } |
| 932 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 933 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 934 | struct btrfs_root *root, |
| 935 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 936 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 937 | struct btrfs_delayed_ref_root *delayed_refs; |
| 938 | struct btrfs_delayed_ref_node *ref; |
| 939 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
| 940 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 941 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 942 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 943 | |
| 944 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 945 | while (1) { |
| 946 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 947 | /* pick a new head ref from the cluster list */ |
| 948 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 949 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 950 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 951 | locked_ref = list_entry(cluster->next, |
| 952 | struct btrfs_delayed_ref_head, cluster); |
| 953 | |
| 954 | /* grab the lock that says we are going to process |
| 955 | * all the refs for this head */ |
| 956 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 957 | |
| 958 | /* |
| 959 | * we may have dropped the spin lock to get the head |
| 960 | * mutex lock, and that might have given someone else |
| 961 | * time to free the head. If that's true, it has been |
| 962 | * removed from our list and we can move on. |
| 963 | */ |
| 964 | if (ret == -EAGAIN) { |
| 965 | locked_ref = NULL; |
| 966 | count++; |
| 967 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * record the must insert reserved flag before we |
| 973 | * drop the spin lock. |
| 974 | */ |
| 975 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 976 | locked_ref->must_insert_reserved = 0; |
| 977 | |
| 978 | /* |
| 979 | * locked_ref is the head node, so we have to go one |
| 980 | * node back for any delayed ref updates |
| 981 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 982 | ref = select_delayed_ref(locked_ref); |
| 983 | if (!ref) { |
| 984 | /* All delayed refs have been processed, Go ahead |
| 985 | * and send the head node to run_one_delayed_ref, |
| 986 | * so that any accounting fixes can happen |
| 987 | */ |
| 988 | ref = &locked_ref->node; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 989 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 990 | locked_ref = NULL; |
| 991 | } |
| 992 | |
| 993 | ref->in_tree = 0; |
| 994 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 995 | delayed_refs->num_entries--; |
| 996 | spin_unlock(&delayed_refs->lock); |
| 997 | |
| 998 | ret = run_one_delayed_ref(trans, root, ref, |
| 999 | must_insert_reserved); |
| 1000 | BUG_ON(ret); |
| 1001 | btrfs_put_delayed_ref(ref); |
| 1002 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1003 | count++; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 1004 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1005 | spin_lock(&delayed_refs->lock); |
| 1006 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1007 | return count; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * this starts processing the delayed reference count updates and |
| 1012 | * extent insertions we have queued up so far. count can be |
| 1013 | * 0, which means to process everything in the tree at the start |
| 1014 | * of the run (but not newly added entries), or it can be some target |
| 1015 | * number you'd like to process. |
| 1016 | */ |
| 1017 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 1018 | struct btrfs_root *root, unsigned long count) |
| 1019 | { |
| 1020 | struct rb_node *node; |
| 1021 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1022 | struct btrfs_delayed_ref_node *ref; |
| 1023 | struct list_head cluster; |
| 1024 | int ret; |
| 1025 | int run_all = count == (unsigned long)-1; |
| 1026 | int run_most = 0; |
| 1027 | |
| 1028 | if (root == root->fs_info->extent_root) |
| 1029 | root = root->fs_info->tree_root; |
| 1030 | |
| 1031 | delayed_refs = &trans->transaction->delayed_refs; |
| 1032 | INIT_LIST_HEAD(&cluster); |
| 1033 | again: |
| 1034 | spin_lock(&delayed_refs->lock); |
| 1035 | if (count == 0) { |
| 1036 | count = delayed_refs->num_entries * 2; |
| 1037 | run_most = 1; |
| 1038 | } |
| 1039 | while (1) { |
| 1040 | if (!(run_all || run_most) && |
| 1041 | delayed_refs->num_heads_ready < 64) |
| 1042 | break; |
| 1043 | |
| 1044 | /* |
| 1045 | * go find something we can process in the rbtree. We start at |
| 1046 | * the beginning of the tree, and then build a cluster |
| 1047 | * of refs to process starting at the first one we are able to |
| 1048 | * lock |
| 1049 | */ |
| 1050 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 1051 | delayed_refs->run_delayed_start); |
| 1052 | if (ret) |
| 1053 | break; |
| 1054 | |
| 1055 | ret = run_clustered_refs(trans, root, &cluster); |
| 1056 | BUG_ON(ret < 0); |
| 1057 | |
| 1058 | count -= min_t(unsigned long, ret, count); |
| 1059 | |
| 1060 | if (count == 0) |
| 1061 | break; |
| 1062 | } |
| 1063 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1064 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1065 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1066 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1067 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1068 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1069 | |
| 1070 | while (node) { |
| 1071 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 1072 | rb_node); |
| 1073 | if (btrfs_delayed_ref_is_head(ref)) { |
| 1074 | struct btrfs_delayed_ref_head *head; |
| 1075 | |
| 1076 | head = btrfs_delayed_node_to_head(ref); |
| 1077 | atomic_inc(&ref->refs); |
| 1078 | |
| 1079 | spin_unlock(&delayed_refs->lock); |
| 1080 | mutex_lock(&head->mutex); |
| 1081 | mutex_unlock(&head->mutex); |
| 1082 | |
| 1083 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 1084 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1085 | goto again; |
| 1086 | } |
| 1087 | node = rb_next(node); |
| 1088 | } |
| 1089 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1090 | schedule_timeout(1); |
| 1091 | goto again; |
| 1092 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1093 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1094 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1095 | return 0; |
| 1096 | } |
| 1097 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1098 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1099 | struct btrfs_root *root, u64 objectid, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1100 | { |
| 1101 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1102 | struct btrfs_path *path; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1103 | struct extent_buffer *leaf; |
| 1104 | struct btrfs_extent_ref *ref_item; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1105 | struct btrfs_key key; |
| 1106 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1107 | u64 ref_root; |
| 1108 | u64 last_snapshot; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1109 | u32 nritems; |
| 1110 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1111 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1112 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1113 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1114 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1115 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1116 | path = btrfs_alloc_path(); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1117 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 1118 | if (ret < 0) |
| 1119 | goto out; |
| 1120 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1121 | |
| 1122 | ret = -ENOENT; |
| 1123 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1124 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1125 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1126 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1127 | leaf = path->nodes[0]; |
| 1128 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1129 | |
| 1130 | if (found_key.objectid != bytenr || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1131 | found_key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1132 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1133 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1134 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1135 | while (1) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1136 | leaf = path->nodes[0]; |
| 1137 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1138 | if (path->slots[0] >= nritems) { |
| 1139 | ret = btrfs_next_leaf(extent_root, path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1140 | if (ret < 0) |
| 1141 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1142 | if (ret == 0) |
| 1143 | continue; |
| 1144 | break; |
| 1145 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1146 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1147 | if (found_key.objectid != bytenr) |
| 1148 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 1149 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1150 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 1151 | path->slots[0]++; |
| 1152 | continue; |
| 1153 | } |
| 1154 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1155 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1156 | struct btrfs_extent_ref); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1157 | ref_root = btrfs_ref_root(leaf, ref_item); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1158 | if ((ref_root != root->root_key.objectid && |
| 1159 | ref_root != BTRFS_TREE_LOG_OBJECTID) || |
| 1160 | objectid != btrfs_ref_objectid(leaf, ref_item)) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1161 | ret = 1; |
| 1162 | goto out; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1163 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1164 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { |
| 1165 | ret = 1; |
| 1166 | goto out; |
| 1167 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1168 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1169 | path->slots[0]++; |
| 1170 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1171 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1172 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1173 | btrfs_free_path(path); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1177 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1178 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1179 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1180 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1181 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1182 | u64 root_gen; |
| 1183 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1184 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1185 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1186 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1187 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1188 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1189 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1190 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1191 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1192 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 1193 | shared = 0; |
| 1194 | root_gen = root->root_key.offset; |
| 1195 | } else { |
| 1196 | shared = 1; |
| 1197 | root_gen = trans->transid - 1; |
| 1198 | } |
| 1199 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1200 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1201 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1202 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1203 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1204 | struct btrfs_leaf_ref *ref; |
| 1205 | struct btrfs_extent_info *info; |
| 1206 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1207 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1208 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1209 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1210 | goto out; |
| 1211 | } |
| 1212 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1213 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1214 | ref->bytenr = buf->start; |
| 1215 | ref->owner = btrfs_header_owner(buf); |
| 1216 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1217 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1218 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1219 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1220 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1221 | u64 disk_bytenr; |
| 1222 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1223 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1224 | continue; |
| 1225 | fi = btrfs_item_ptr(buf, i, |
| 1226 | struct btrfs_file_extent_item); |
| 1227 | if (btrfs_file_extent_type(buf, fi) == |
| 1228 | BTRFS_FILE_EXTENT_INLINE) |
| 1229 | continue; |
| 1230 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1231 | if (disk_bytenr == 0) |
| 1232 | continue; |
| 1233 | |
| 1234 | info->bytenr = disk_bytenr; |
| 1235 | info->num_bytes = |
| 1236 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 1237 | info->objectid = key.objectid; |
| 1238 | info->offset = key.offset; |
| 1239 | info++; |
| 1240 | } |
| 1241 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1242 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1243 | if (ret == -EEXIST && shared) { |
| 1244 | struct btrfs_leaf_ref *old; |
| 1245 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 1246 | BUG_ON(!old); |
| 1247 | btrfs_remove_leaf_ref(root, old); |
| 1248 | btrfs_free_leaf_ref(root, old); |
| 1249 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 1250 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1251 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1252 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1253 | } |
| 1254 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1255 | return ret; |
| 1256 | } |
| 1257 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1258 | /* when a block goes through cow, we update the reference counts of |
| 1259 | * everything that block points to. The internal pointers of the block |
| 1260 | * can be in just about any order, and it is likely to have clusters of |
| 1261 | * things that are close together and clusters of things that are not. |
| 1262 | * |
| 1263 | * To help reduce the seeks that come with updating all of these reference |
| 1264 | * counts, sort them by byte number before actual updates are done. |
| 1265 | * |
| 1266 | * struct refsort is used to match byte number to slot in the btree block. |
| 1267 | * we sort based on the byte number and then use the slot to actually |
| 1268 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 1269 | * |
| 1270 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 1271 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 1272 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 1273 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1274 | */ |
| 1275 | struct refsort { |
| 1276 | u64 bytenr; |
| 1277 | u32 slot; |
| 1278 | }; |
| 1279 | |
| 1280 | /* |
| 1281 | * for passing into sort() |
| 1282 | */ |
| 1283 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 1284 | { |
| 1285 | const struct refsort *a = a_void; |
| 1286 | const struct refsort *b = b_void; |
| 1287 | |
| 1288 | if (a->bytenr < b->bytenr) |
| 1289 | return -1; |
| 1290 | if (a->bytenr > b->bytenr) |
| 1291 | return 1; |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans, |
| 1297 | struct btrfs_root *root, |
| 1298 | struct extent_buffer *orig_buf, |
| 1299 | struct extent_buffer *buf, u32 *nr_extents) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1300 | { |
| 1301 | u64 bytenr; |
| 1302 | u64 ref_root; |
| 1303 | u64 orig_root; |
| 1304 | u64 ref_generation; |
| 1305 | u64 orig_generation; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1306 | struct refsort *sorted; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1307 | u32 nritems; |
| 1308 | u32 nr_file_extents = 0; |
| 1309 | struct btrfs_key key; |
| 1310 | struct btrfs_file_extent_item *fi; |
| 1311 | int i; |
| 1312 | int level; |
| 1313 | int ret = 0; |
| 1314 | int faili = 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1315 | int refi = 0; |
| 1316 | int slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1317 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1318 | u64, u64, u64, u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1319 | |
| 1320 | ref_root = btrfs_header_owner(buf); |
| 1321 | ref_generation = btrfs_header_generation(buf); |
| 1322 | orig_root = btrfs_header_owner(orig_buf); |
| 1323 | orig_generation = btrfs_header_generation(orig_buf); |
| 1324 | |
| 1325 | nritems = btrfs_header_nritems(buf); |
| 1326 | level = btrfs_header_level(buf); |
| 1327 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1328 | sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS); |
| 1329 | BUG_ON(!sorted); |
| 1330 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1331 | if (root->ref_cows) { |
| 1332 | process_func = __btrfs_inc_extent_ref; |
| 1333 | } else { |
| 1334 | if (level == 0 && |
| 1335 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1336 | goto out; |
| 1337 | if (level != 0 && |
| 1338 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1339 | goto out; |
| 1340 | process_func = __btrfs_update_extent_ref; |
| 1341 | } |
| 1342 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1343 | /* |
| 1344 | * we make two passes through the items. In the first pass we |
| 1345 | * only record the byte number and slot. Then we sort based on |
| 1346 | * byte number and do the actual work based on the sorted results |
| 1347 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1348 | for (i = 0; i < nritems; i++) { |
| 1349 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1350 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1351 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1352 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1353 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1354 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1355 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1356 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1357 | BTRFS_FILE_EXTENT_INLINE) |
| 1358 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1359 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1360 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1361 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1362 | |
| 1363 | nr_file_extents++; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1364 | sorted[refi].bytenr = bytenr; |
| 1365 | sorted[refi].slot = i; |
| 1366 | refi++; |
| 1367 | } else { |
| 1368 | bytenr = btrfs_node_blockptr(buf, i); |
| 1369 | sorted[refi].bytenr = bytenr; |
| 1370 | sorted[refi].slot = i; |
| 1371 | refi++; |
| 1372 | } |
| 1373 | } |
| 1374 | /* |
| 1375 | * if refi == 0, we didn't actually put anything into the sorted |
| 1376 | * array and we're done |
| 1377 | */ |
| 1378 | if (refi == 0) |
| 1379 | goto out; |
| 1380 | |
| 1381 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 1382 | |
| 1383 | for (i = 0; i < refi; i++) { |
| 1384 | cond_resched(); |
| 1385 | slot = sorted[i].slot; |
| 1386 | bytenr = sorted[i].bytenr; |
| 1387 | |
| 1388 | if (level == 0) { |
| 1389 | btrfs_item_key_to_cpu(buf, &key, slot); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1390 | fi = btrfs_item_ptr(buf, slot, |
| 1391 | struct btrfs_file_extent_item); |
| 1392 | |
| 1393 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1394 | if (bytenr == 0) |
| 1395 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1396 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1397 | ret = process_func(trans, root, bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1398 | btrfs_file_extent_disk_num_bytes(buf, fi), |
| 1399 | orig_buf->start, buf->start, |
| 1400 | orig_root, ref_root, |
| 1401 | orig_generation, ref_generation, |
| 1402 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1403 | |
| 1404 | if (ret) { |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1405 | faili = slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1406 | WARN_ON(1); |
| 1407 | goto fail; |
| 1408 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1409 | } else { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1410 | ret = process_func(trans, root, bytenr, buf->len, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1411 | orig_buf->start, buf->start, |
| 1412 | orig_root, ref_root, |
| 1413 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1414 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1415 | if (ret) { |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1416 | faili = slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1417 | WARN_ON(1); |
| 1418 | goto fail; |
| 1419 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1420 | } |
| 1421 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1422 | out: |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1423 | kfree(sorted); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1424 | if (nr_extents) { |
| 1425 | if (level == 0) |
| 1426 | *nr_extents = nr_file_extents; |
| 1427 | else |
| 1428 | *nr_extents = nritems; |
| 1429 | } |
| 1430 | return 0; |
| 1431 | fail: |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1432 | kfree(sorted); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1433 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1434 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1435 | } |
| 1436 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1437 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
| 1438 | struct btrfs_root *root, struct extent_buffer *orig_buf, |
| 1439 | struct extent_buffer *buf, int start_slot, int nr) |
| 1440 | |
| 1441 | { |
| 1442 | u64 bytenr; |
| 1443 | u64 ref_root; |
| 1444 | u64 orig_root; |
| 1445 | u64 ref_generation; |
| 1446 | u64 orig_generation; |
| 1447 | struct btrfs_key key; |
| 1448 | struct btrfs_file_extent_item *fi; |
| 1449 | int i; |
| 1450 | int ret; |
| 1451 | int slot; |
| 1452 | int level; |
| 1453 | |
| 1454 | BUG_ON(start_slot < 0); |
| 1455 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); |
| 1456 | |
| 1457 | ref_root = btrfs_header_owner(buf); |
| 1458 | ref_generation = btrfs_header_generation(buf); |
| 1459 | orig_root = btrfs_header_owner(orig_buf); |
| 1460 | orig_generation = btrfs_header_generation(orig_buf); |
| 1461 | level = btrfs_header_level(buf); |
| 1462 | |
| 1463 | if (!root->ref_cows) { |
| 1464 | if (level == 0 && |
| 1465 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1466 | return 0; |
| 1467 | if (level != 0 && |
| 1468 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { |
| 1473 | cond_resched(); |
| 1474 | if (level == 0) { |
| 1475 | btrfs_item_key_to_cpu(buf, &key, slot); |
| 1476 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1477 | continue; |
| 1478 | fi = btrfs_item_ptr(buf, slot, |
| 1479 | struct btrfs_file_extent_item); |
| 1480 | if (btrfs_file_extent_type(buf, fi) == |
| 1481 | BTRFS_FILE_EXTENT_INLINE) |
| 1482 | continue; |
| 1483 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1484 | if (bytenr == 0) |
| 1485 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1486 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1487 | btrfs_file_extent_disk_num_bytes(buf, fi), |
| 1488 | orig_buf->start, buf->start, |
| 1489 | orig_root, ref_root, orig_generation, |
| 1490 | ref_generation, key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1491 | if (ret) |
| 1492 | goto fail; |
| 1493 | } else { |
| 1494 | bytenr = btrfs_node_blockptr(buf, slot); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1495 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1496 | buf->len, orig_buf->start, |
| 1497 | buf->start, orig_root, ref_root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1498 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1499 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1500 | if (ret) |
| 1501 | goto fail; |
| 1502 | } |
| 1503 | } |
| 1504 | return 0; |
| 1505 | fail: |
| 1506 | WARN_ON(1); |
| 1507 | return -1; |
| 1508 | } |
| 1509 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1510 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1511 | struct btrfs_root *root, |
| 1512 | struct btrfs_path *path, |
| 1513 | struct btrfs_block_group_cache *cache) |
| 1514 | { |
| 1515 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1516 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1517 | unsigned long bi; |
| 1518 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1519 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1520 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1521 | if (ret < 0) |
| 1522 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1523 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1524 | |
| 1525 | leaf = path->nodes[0]; |
| 1526 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1527 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1528 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1529 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1530 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1531 | if (ret) |
| 1532 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1533 | return 0; |
| 1534 | |
| 1535 | } |
| 1536 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1537 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1538 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1539 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1540 | struct btrfs_block_group_cache *cache, *entry; |
| 1541 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1542 | int err = 0; |
| 1543 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1544 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1545 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1546 | |
| 1547 | path = btrfs_alloc_path(); |
| 1548 | if (!path) |
| 1549 | return -ENOMEM; |
| 1550 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1551 | while (1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1552 | cache = NULL; |
| 1553 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1554 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1555 | n; n = rb_next(n)) { |
| 1556 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1557 | cache_node); |
| 1558 | if (entry->dirty) { |
| 1559 | cache = entry; |
| 1560 | break; |
| 1561 | } |
| 1562 | } |
| 1563 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1564 | |
| 1565 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1566 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1567 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1568 | cache->dirty = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1569 | last += cache->key.offset; |
| 1570 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1571 | err = write_one_cache_group(trans, root, |
| 1572 | path, cache); |
| 1573 | /* |
| 1574 | * if we fail to write the cache group, we want |
| 1575 | * to keep it marked dirty in hopes that a later |
| 1576 | * write will work |
| 1577 | */ |
| 1578 | if (err) { |
| 1579 | werr = err; |
| 1580 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | btrfs_free_path(path); |
| 1584 | return werr; |
| 1585 | } |
| 1586 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1587 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 1588 | { |
| 1589 | struct btrfs_block_group_cache *block_group; |
| 1590 | int readonly = 0; |
| 1591 | |
| 1592 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 1593 | if (!block_group || block_group->ro) |
| 1594 | readonly = 1; |
| 1595 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 1596 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1597 | return readonly; |
| 1598 | } |
| 1599 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1600 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1601 | u64 total_bytes, u64 bytes_used, |
| 1602 | struct btrfs_space_info **space_info) |
| 1603 | { |
| 1604 | struct btrfs_space_info *found; |
| 1605 | |
| 1606 | found = __find_space_info(info, flags); |
| 1607 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1608 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1609 | found->total_bytes += total_bytes; |
| 1610 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1611 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1612 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1613 | *space_info = found; |
| 1614 | return 0; |
| 1615 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1616 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1617 | if (!found) |
| 1618 | return -ENOMEM; |
| 1619 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1620 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1621 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1622 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1623 | found->flags = flags; |
| 1624 | found->total_bytes = total_bytes; |
| 1625 | found->bytes_used = bytes_used; |
| 1626 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1627 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1628 | found->bytes_readonly = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1629 | found->bytes_delalloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1630 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1631 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1632 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 1633 | list_add_rcu(&found->list, &info->space_info); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
| 1636 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1637 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1638 | { |
| 1639 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1640 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1641 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1642 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1643 | if (extra_flags) { |
| 1644 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1645 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1646 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1647 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1648 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1649 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1650 | } |
| 1651 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1652 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1653 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 1654 | { |
| 1655 | spin_lock(&cache->space_info->lock); |
| 1656 | spin_lock(&cache->lock); |
| 1657 | if (!cache->ro) { |
| 1658 | cache->space_info->bytes_readonly += cache->key.offset - |
| 1659 | btrfs_block_group_used(&cache->item); |
| 1660 | cache->ro = 1; |
| 1661 | } |
| 1662 | spin_unlock(&cache->lock); |
| 1663 | spin_unlock(&cache->space_info->lock); |
| 1664 | } |
| 1665 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1666 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1667 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1668 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1669 | |
| 1670 | if (num_devices == 1) |
| 1671 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1672 | if (num_devices < 4) |
| 1673 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1674 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1675 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1676 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1677 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1678 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1679 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1680 | |
| 1681 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1682 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1683 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1684 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1685 | |
| 1686 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1687 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1688 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1689 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1690 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1691 | return flags; |
| 1692 | } |
| 1693 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1694 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) |
| 1695 | { |
| 1696 | struct btrfs_fs_info *info = root->fs_info; |
| 1697 | u64 alloc_profile; |
| 1698 | |
| 1699 | if (data) { |
| 1700 | alloc_profile = info->avail_data_alloc_bits & |
| 1701 | info->data_alloc_profile; |
| 1702 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
| 1703 | } else if (root == root->fs_info->chunk_root) { |
| 1704 | alloc_profile = info->avail_system_alloc_bits & |
| 1705 | info->system_alloc_profile; |
| 1706 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
| 1707 | } else { |
| 1708 | alloc_profile = info->avail_metadata_alloc_bits & |
| 1709 | info->metadata_alloc_profile; |
| 1710 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
| 1711 | } |
| 1712 | |
| 1713 | return btrfs_reduce_alloc_profile(root, data); |
| 1714 | } |
| 1715 | |
| 1716 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 1717 | { |
| 1718 | u64 alloc_target; |
| 1719 | |
| 1720 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 1721 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
| 1722 | alloc_target); |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * for now this just makes sure we have at least 5% of our metadata space free |
| 1727 | * for use. |
| 1728 | */ |
| 1729 | int btrfs_check_metadata_free_space(struct btrfs_root *root) |
| 1730 | { |
| 1731 | struct btrfs_fs_info *info = root->fs_info; |
| 1732 | struct btrfs_space_info *meta_sinfo; |
| 1733 | u64 alloc_target, thresh; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1734 | int committed = 0, ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1735 | |
| 1736 | /* get the space info for where the metadata will live */ |
| 1737 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 1738 | meta_sinfo = __find_space_info(info, alloc_target); |
| 1739 | |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1740 | again: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1741 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1742 | if (!meta_sinfo->full) |
| 1743 | thresh = meta_sinfo->total_bytes * 80; |
| 1744 | else |
| 1745 | thresh = meta_sinfo->total_bytes * 95; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1746 | |
| 1747 | do_div(thresh, 100); |
| 1748 | |
| 1749 | if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 1750 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1751 | struct btrfs_trans_handle *trans; |
| 1752 | if (!meta_sinfo->full) { |
| 1753 | meta_sinfo->force_alloc = 1; |
| 1754 | spin_unlock(&meta_sinfo->lock); |
| 1755 | |
| 1756 | trans = btrfs_start_transaction(root, 1); |
| 1757 | if (!trans) |
| 1758 | return -ENOMEM; |
| 1759 | |
| 1760 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 1761 | 2 * 1024 * 1024, alloc_target, 0); |
| 1762 | btrfs_end_transaction(trans, root); |
| 1763 | goto again; |
| 1764 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1765 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1766 | |
| 1767 | if (!committed) { |
| 1768 | committed = 1; |
| 1769 | trans = btrfs_join_transaction(root, 1); |
| 1770 | if (!trans) |
| 1771 | return -ENOMEM; |
| 1772 | ret = btrfs_commit_transaction(trans, root); |
| 1773 | if (ret) |
| 1774 | return ret; |
| 1775 | goto again; |
| 1776 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1777 | return -ENOSPC; |
| 1778 | } |
| 1779 | spin_unlock(&meta_sinfo->lock); |
| 1780 | |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | /* |
| 1785 | * This will check the space that the inode allocates from to make sure we have |
| 1786 | * enough space for bytes. |
| 1787 | */ |
| 1788 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, |
| 1789 | u64 bytes) |
| 1790 | { |
| 1791 | struct btrfs_space_info *data_sinfo; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1792 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1793 | |
| 1794 | /* make sure bytes are sectorsize aligned */ |
| 1795 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 1796 | |
| 1797 | data_sinfo = BTRFS_I(inode)->space_info; |
| 1798 | again: |
| 1799 | /* make sure we have enough space to handle the data first */ |
| 1800 | spin_lock(&data_sinfo->lock); |
| 1801 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - |
| 1802 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - |
| 1803 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - |
| 1804 | data_sinfo->bytes_may_use < bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1805 | struct btrfs_trans_handle *trans; |
| 1806 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1807 | /* |
| 1808 | * if we don't have enough free bytes in this space then we need |
| 1809 | * to alloc a new chunk. |
| 1810 | */ |
| 1811 | if (!data_sinfo->full) { |
| 1812 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1813 | |
| 1814 | data_sinfo->force_alloc = 1; |
| 1815 | spin_unlock(&data_sinfo->lock); |
| 1816 | |
| 1817 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 1818 | trans = btrfs_start_transaction(root, 1); |
| 1819 | if (!trans) |
| 1820 | return -ENOMEM; |
| 1821 | |
| 1822 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 1823 | bytes + 2 * 1024 * 1024, |
| 1824 | alloc_target, 0); |
| 1825 | btrfs_end_transaction(trans, root); |
| 1826 | if (ret) |
| 1827 | return ret; |
| 1828 | goto again; |
| 1829 | } |
| 1830 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1831 | |
| 1832 | /* commit the current transaction and try again */ |
| 1833 | if (!committed) { |
| 1834 | committed = 1; |
| 1835 | trans = btrfs_join_transaction(root, 1); |
| 1836 | if (!trans) |
| 1837 | return -ENOMEM; |
| 1838 | ret = btrfs_commit_transaction(trans, root); |
| 1839 | if (ret) |
| 1840 | return ret; |
| 1841 | goto again; |
| 1842 | } |
| 1843 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1844 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" |
| 1845 | ", %llu bytes_used, %llu bytes_reserved, " |
| 1846 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use" |
| 1847 | "%llu total\n", bytes, data_sinfo->bytes_delalloc, |
| 1848 | data_sinfo->bytes_used, data_sinfo->bytes_reserved, |
| 1849 | data_sinfo->bytes_pinned, data_sinfo->bytes_readonly, |
| 1850 | data_sinfo->bytes_may_use, data_sinfo->total_bytes); |
| 1851 | return -ENOSPC; |
| 1852 | } |
| 1853 | data_sinfo->bytes_may_use += bytes; |
| 1854 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 1855 | spin_unlock(&data_sinfo->lock); |
| 1856 | |
| 1857 | return btrfs_check_metadata_free_space(root); |
| 1858 | } |
| 1859 | |
| 1860 | /* |
| 1861 | * if there was an error for whatever reason after calling |
| 1862 | * btrfs_check_data_free_space, call this so we can cleanup the counters. |
| 1863 | */ |
| 1864 | void btrfs_free_reserved_data_space(struct btrfs_root *root, |
| 1865 | struct inode *inode, u64 bytes) |
| 1866 | { |
| 1867 | struct btrfs_space_info *data_sinfo; |
| 1868 | |
| 1869 | /* make sure bytes are sectorsize aligned */ |
| 1870 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 1871 | |
| 1872 | data_sinfo = BTRFS_I(inode)->space_info; |
| 1873 | spin_lock(&data_sinfo->lock); |
| 1874 | data_sinfo->bytes_may_use -= bytes; |
| 1875 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 1876 | spin_unlock(&data_sinfo->lock); |
| 1877 | } |
| 1878 | |
| 1879 | /* called when we are adding a delalloc extent to the inode's io_tree */ |
| 1880 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, |
| 1881 | u64 bytes) |
| 1882 | { |
| 1883 | struct btrfs_space_info *data_sinfo; |
| 1884 | |
| 1885 | /* get the space info for where this inode will be storing its data */ |
| 1886 | data_sinfo = BTRFS_I(inode)->space_info; |
| 1887 | |
| 1888 | /* make sure we have enough space to handle the data first */ |
| 1889 | spin_lock(&data_sinfo->lock); |
| 1890 | data_sinfo->bytes_delalloc += bytes; |
| 1891 | |
| 1892 | /* |
| 1893 | * we are adding a delalloc extent without calling |
| 1894 | * btrfs_check_data_free_space first. This happens on a weird |
| 1895 | * writepage condition, but shouldn't hurt our accounting |
| 1896 | */ |
| 1897 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { |
| 1898 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; |
| 1899 | BTRFS_I(inode)->reserved_bytes = 0; |
| 1900 | } else { |
| 1901 | data_sinfo->bytes_may_use -= bytes; |
| 1902 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 1903 | } |
| 1904 | |
| 1905 | spin_unlock(&data_sinfo->lock); |
| 1906 | } |
| 1907 | |
| 1908 | /* called when we are clearing an delalloc extent from the inode's io_tree */ |
| 1909 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, |
| 1910 | u64 bytes) |
| 1911 | { |
| 1912 | struct btrfs_space_info *info; |
| 1913 | |
| 1914 | info = BTRFS_I(inode)->space_info; |
| 1915 | |
| 1916 | spin_lock(&info->lock); |
| 1917 | info->bytes_delalloc -= bytes; |
| 1918 | spin_unlock(&info->lock); |
| 1919 | } |
| 1920 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1921 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1922 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1923 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1924 | { |
| 1925 | struct btrfs_space_info *space_info; |
| 1926 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1927 | int ret = 0; |
| 1928 | |
| 1929 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1930 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1931 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1932 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1933 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1934 | if (!space_info) { |
| 1935 | ret = update_space_info(extent_root->fs_info, flags, |
| 1936 | 0, 0, &space_info); |
| 1937 | BUG_ON(ret); |
| 1938 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1939 | BUG_ON(!space_info); |
| 1940 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1941 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1942 | if (space_info->force_alloc) { |
| 1943 | force = 1; |
| 1944 | space_info->force_alloc = 0; |
| 1945 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1946 | if (space_info->full) { |
| 1947 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1948 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1949 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1950 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1951 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
| 1952 | thresh = div_factor(thresh, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1953 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1954 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1955 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 1956 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1957 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1958 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1959 | spin_unlock(&space_info->lock); |
| 1960 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1961 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1962 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1963 | space_info->full = 1; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1964 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1965 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1966 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1967 | } |
| 1968 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1969 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1970 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1971 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1972 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1973 | { |
| 1974 | struct btrfs_block_group_cache *cache; |
| 1975 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1976 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1977 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1978 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1979 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1980 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1981 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1982 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1983 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1984 | byte_in_group = bytenr - cache->key.objectid; |
| 1985 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1986 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1987 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1988 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1989 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1990 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1991 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1992 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1993 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1994 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 1995 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1996 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1997 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1998 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1999 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2000 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2001 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2002 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2003 | if (cache->ro) |
| 2004 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2005 | btrfs_set_block_group_used(&cache->item, old_val); |
| 2006 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2007 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2008 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2009 | int ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2010 | |
| 2011 | ret = btrfs_discard_extent(root, bytenr, |
| 2012 | num_bytes); |
| 2013 | WARN_ON(ret); |
| 2014 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2015 | ret = btrfs_add_free_space(cache, bytenr, |
| 2016 | num_bytes); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2017 | WARN_ON(ret); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2018 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2019 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2020 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2021 | total -= num_bytes; |
| 2022 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2023 | } |
| 2024 | return 0; |
| 2025 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2026 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2027 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 2028 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2029 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2030 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2031 | |
| 2032 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 2033 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2034 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2035 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2036 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2037 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2038 | |
| 2039 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2040 | } |
| 2041 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2042 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2043 | u64 bytenr, u64 num, int pin) |
| 2044 | { |
| 2045 | u64 len; |
| 2046 | struct btrfs_block_group_cache *cache; |
| 2047 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2048 | |
| 2049 | if (pin) { |
| 2050 | set_extent_dirty(&fs_info->pinned_extents, |
| 2051 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2052 | } else { |
| 2053 | clear_extent_dirty(&fs_info->pinned_extents, |
| 2054 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2055 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2056 | |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2057 | while (num > 0) { |
| 2058 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2059 | BUG_ON(!cache); |
| 2060 | len = min(num, cache->key.offset - |
| 2061 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2062 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2063 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2064 | spin_lock(&cache->lock); |
| 2065 | cache->pinned += len; |
| 2066 | cache->space_info->bytes_pinned += len; |
| 2067 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2068 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2069 | fs_info->total_pinned += len; |
| 2070 | } else { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2071 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2072 | spin_lock(&cache->lock); |
| 2073 | cache->pinned -= len; |
| 2074 | cache->space_info->bytes_pinned -= len; |
| 2075 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2076 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2077 | fs_info->total_pinned -= len; |
Josef Bacik | 07103a3 | 2008-11-19 15:17:55 -0500 | [diff] [blame] | 2078 | if (cache->cached) |
| 2079 | btrfs_add_free_space(cache, bytenr, len); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2080 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2081 | btrfs_put_block_group(cache); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2082 | bytenr += len; |
| 2083 | num -= len; |
| 2084 | } |
| 2085 | return 0; |
| 2086 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2087 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2088 | static int update_reserved_extents(struct btrfs_root *root, |
| 2089 | u64 bytenr, u64 num, int reserve) |
| 2090 | { |
| 2091 | u64 len; |
| 2092 | struct btrfs_block_group_cache *cache; |
| 2093 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2094 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2095 | while (num > 0) { |
| 2096 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 2097 | BUG_ON(!cache); |
| 2098 | len = min(num, cache->key.offset - |
| 2099 | (bytenr - cache->key.objectid)); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2100 | |
| 2101 | spin_lock(&cache->space_info->lock); |
| 2102 | spin_lock(&cache->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2103 | if (reserve) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2104 | cache->reserved += len; |
| 2105 | cache->space_info->bytes_reserved += len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2106 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2107 | cache->reserved -= len; |
| 2108 | cache->space_info->bytes_reserved -= len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2109 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2110 | spin_unlock(&cache->lock); |
| 2111 | spin_unlock(&cache->space_info->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2112 | btrfs_put_block_group(cache); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2113 | bytenr += len; |
| 2114 | num -= len; |
| 2115 | } |
| 2116 | return 0; |
| 2117 | } |
| 2118 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2119 | 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] | 2120 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2121 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2122 | u64 start; |
| 2123 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2124 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2125 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2126 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2127 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2128 | ret = find_first_extent_bit(pinned_extents, last, |
| 2129 | &start, &end, EXTENT_DIRTY); |
| 2130 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2131 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2132 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 2133 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2134 | } |
| 2135 | return 0; |
| 2136 | } |
| 2137 | |
| 2138 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 2139 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2140 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2141 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2142 | u64 start; |
| 2143 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2144 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2145 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2146 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2147 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 2148 | EXTENT_DIRTY); |
| 2149 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2150 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2151 | |
| 2152 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 2153 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2154 | /* unlocks the pinned mutex */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2155 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2156 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2157 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2158 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2159 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2160 | return ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2161 | } |
| 2162 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2163 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 2164 | struct btrfs_root *root, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2165 | struct btrfs_path *path, |
| 2166 | u64 bytenr, u64 num_bytes, int is_data, |
| 2167 | struct extent_buffer **must_clean) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2168 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2169 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2170 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2171 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2172 | if (is_data) |
| 2173 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2174 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2175 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 2176 | if (!buf) |
| 2177 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2178 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2179 | /* we can reuse a block if it hasn't been written |
| 2180 | * and it is from this transaction. We can't |
| 2181 | * reuse anything from the tree log root because |
| 2182 | * it has tiny sub-transactions. |
| 2183 | */ |
| 2184 | if (btrfs_buffer_uptodate(buf, 0) && |
| 2185 | btrfs_try_tree_lock(buf)) { |
| 2186 | u64 header_owner = btrfs_header_owner(buf); |
| 2187 | u64 header_transid = btrfs_header_generation(buf); |
| 2188 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2189 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2190 | header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID && |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2191 | header_transid == trans->transid && |
| 2192 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2193 | *must_clean = buf; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2194 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2195 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2196 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2197 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2198 | free_extent_buffer(buf); |
| 2199 | pinit: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2200 | btrfs_set_path_blocking(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2201 | /* unlocks the pinned mutex */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2202 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 2203 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2204 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2205 | return 0; |
| 2206 | } |
| 2207 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2208 | /* |
| 2209 | * remove an extent from the root, returns 0 on success |
| 2210 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2211 | static int __free_extent(struct btrfs_trans_handle *trans, |
| 2212 | struct btrfs_root *root, |
| 2213 | u64 bytenr, u64 num_bytes, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2214 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2215 | u64 owner_objectid, int pin, int mark_free, |
| 2216 | int refs_to_drop) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2217 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2218 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2219 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2220 | struct btrfs_fs_info *info = root->fs_info; |
| 2221 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2222 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2223 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2224 | int extent_slot = 0; |
| 2225 | int found_extent = 0; |
| 2226 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2227 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2228 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2229 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2230 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2231 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2232 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2233 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2234 | if (!path) |
| 2235 | return -ENOMEM; |
| 2236 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2237 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2238 | path->leave_spinning = 1; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2239 | ret = lookup_extent_backref(trans, extent_root, path, |
| 2240 | bytenr, parent, root_objectid, |
| 2241 | ref_generation, owner_objectid, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2242 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2243 | struct btrfs_key found_key; |
| 2244 | extent_slot = path->slots[0]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2245 | while (extent_slot > 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2246 | extent_slot--; |
| 2247 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2248 | extent_slot); |
| 2249 | if (found_key.objectid != bytenr) |
| 2250 | break; |
| 2251 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2252 | found_key.offset == num_bytes) { |
| 2253 | found_extent = 1; |
| 2254 | break; |
| 2255 | } |
| 2256 | if (path->slots[0] - extent_slot > 5) |
| 2257 | break; |
| 2258 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2259 | if (!found_extent) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2260 | ret = remove_extent_backref(trans, extent_root, path, |
| 2261 | refs_to_drop); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2262 | BUG_ON(ret); |
| 2263 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2264 | path->leave_spinning = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2265 | ret = btrfs_search_slot(trans, extent_root, |
| 2266 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2267 | if (ret) { |
| 2268 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2269 | ", was looking for %llu\n", ret, |
| 2270 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2271 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2272 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2273 | BUG_ON(ret); |
| 2274 | extent_slot = path->slots[0]; |
| 2275 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2276 | } else { |
| 2277 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2278 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2279 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2280 | "parent %llu root %llu gen %llu owner %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2281 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2282 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2283 | (unsigned long long)root_objectid, |
| 2284 | (unsigned long long)ref_generation, |
| 2285 | (unsigned long long)owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2286 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2287 | |
| 2288 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2289 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2290 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2291 | refs = btrfs_extent_refs(leaf, ei); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2292 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2293 | /* |
| 2294 | * we're not allowed to delete the extent item if there |
| 2295 | * are other delayed ref updates pending |
| 2296 | */ |
| 2297 | |
| 2298 | BUG_ON(refs < refs_to_drop); |
| 2299 | refs -= refs_to_drop; |
| 2300 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2301 | btrfs_mark_buffer_dirty(leaf); |
| 2302 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2303 | if (refs == 0 && found_extent && |
| 2304 | path->slots[0] == extent_slot + 1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2305 | struct btrfs_extent_ref *ref; |
| 2306 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 2307 | struct btrfs_extent_ref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2308 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2309 | /* if the back ref and the extent are next to each other |
| 2310 | * they get deleted below in one shot |
| 2311 | */ |
| 2312 | path->slots[0] = extent_slot; |
| 2313 | num_to_del = 2; |
| 2314 | } else if (found_extent) { |
| 2315 | /* otherwise delete the extent back ref */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2316 | ret = remove_extent_backref(trans, extent_root, path, |
| 2317 | refs_to_drop); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2318 | BUG_ON(ret); |
| 2319 | /* if refs are 0, we need to setup the path for deletion */ |
| 2320 | if (refs == 0) { |
| 2321 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2322 | path->leave_spinning = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2323 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 2324 | -1, 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2325 | BUG_ON(ret); |
| 2326 | } |
| 2327 | } |
| 2328 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2329 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2330 | u64 super_used; |
| 2331 | u64 root_used; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2332 | struct extent_buffer *must_clean = NULL; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2333 | |
| 2334 | if (pin) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2335 | ret = pin_down_bytes(trans, root, path, |
| 2336 | bytenr, num_bytes, |
| 2337 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID, |
| 2338 | &must_clean); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 2339 | if (ret > 0) |
| 2340 | mark_free = 1; |
| 2341 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2342 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2343 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2344 | /* block accounting for super block */ |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2345 | spin_lock(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2346 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2347 | btrfs_set_super_bytes_used(&info->super_copy, |
| 2348 | super_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2349 | |
| 2350 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2351 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2352 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2353 | root_used - num_bytes); |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 2354 | spin_unlock(&info->delalloc_lock); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2355 | |
| 2356 | /* |
| 2357 | * it is going to be very rare for someone to be waiting |
| 2358 | * on the block we're freeing. del_items might need to |
| 2359 | * schedule, so rather than get fancy, just force it |
| 2360 | * to blocking here |
| 2361 | */ |
| 2362 | if (must_clean) |
| 2363 | btrfs_set_lock_blocking(must_clean); |
| 2364 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2365 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 2366 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2367 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2368 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2369 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2370 | if (must_clean) { |
| 2371 | clean_tree_block(NULL, root, must_clean); |
| 2372 | btrfs_tree_unlock(must_clean); |
| 2373 | free_extent_buffer(must_clean); |
| 2374 | } |
| 2375 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2376 | if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 2377 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 2378 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 2379 | } else { |
| 2380 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 2381 | bytenr >> PAGE_CACHE_SHIFT, |
| 2382 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2383 | } |
| 2384 | |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 2385 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
| 2386 | mark_free); |
| 2387 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2388 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2389 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2390 | return ret; |
| 2391 | } |
| 2392 | |
| 2393 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2394 | * remove an extent from the root, returns 0 on success |
| 2395 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2396 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2397 | struct btrfs_root *root, |
| 2398 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2399 | u64 root_objectid, u64 ref_generation, |
| 2400 | u64 owner_objectid, int pin, |
| 2401 | int refs_to_drop) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2402 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2403 | WARN_ON(num_bytes < root->sectorsize); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2404 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2405 | /* |
| 2406 | * if metadata always pin |
| 2407 | * if data pin when any transaction has committed this |
| 2408 | */ |
| 2409 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID || |
| 2410 | ref_generation != trans->transid) |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2411 | pin = 1; |
| 2412 | |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2413 | if (ref_generation != trans->transid) |
| 2414 | pin = 1; |
| 2415 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2416 | return __free_extent(trans, root, bytenr, num_bytes, parent, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2417 | root_objectid, ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2418 | owner_objectid, pin, pin == 0, refs_to_drop); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2419 | } |
| 2420 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2421 | /* |
| 2422 | * when we free an extent, it is possible (and likely) that we free the last |
| 2423 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 2424 | * a given extent, and if there are no other delayed refs to be processed, it |
| 2425 | * removes it from the tree. |
| 2426 | */ |
| 2427 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 2428 | struct btrfs_root *root, u64 bytenr) |
| 2429 | { |
| 2430 | struct btrfs_delayed_ref_head *head; |
| 2431 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2432 | struct btrfs_delayed_ref_node *ref; |
| 2433 | struct rb_node *node; |
| 2434 | int ret; |
| 2435 | |
| 2436 | delayed_refs = &trans->transaction->delayed_refs; |
| 2437 | spin_lock(&delayed_refs->lock); |
| 2438 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2439 | if (!head) |
| 2440 | goto out; |
| 2441 | |
| 2442 | node = rb_prev(&head->node.rb_node); |
| 2443 | if (!node) |
| 2444 | goto out; |
| 2445 | |
| 2446 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2447 | |
| 2448 | /* there are still entries for this ref, we can't drop it */ |
| 2449 | if (ref->bytenr == bytenr) |
| 2450 | goto out; |
| 2451 | |
| 2452 | /* |
| 2453 | * waiting for the lock here would deadlock. If someone else has it |
| 2454 | * locked they are already in the process of dropping it anyway |
| 2455 | */ |
| 2456 | if (!mutex_trylock(&head->mutex)) |
| 2457 | goto out; |
| 2458 | |
| 2459 | /* |
| 2460 | * at this point we have a head with no other entries. Go |
| 2461 | * ahead and process it. |
| 2462 | */ |
| 2463 | head->node.in_tree = 0; |
| 2464 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2465 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2466 | delayed_refs->num_entries--; |
| 2467 | |
| 2468 | /* |
| 2469 | * we don't take a ref on the node because we're removing it from the |
| 2470 | * tree, so we just steal the ref the tree was holding. |
| 2471 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2472 | delayed_refs->num_heads--; |
| 2473 | if (list_empty(&head->cluster)) |
| 2474 | delayed_refs->num_heads_ready--; |
| 2475 | |
| 2476 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2477 | spin_unlock(&delayed_refs->lock); |
| 2478 | |
| 2479 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, |
| 2480 | &head->node, head->must_insert_reserved); |
| 2481 | BUG_ON(ret); |
| 2482 | btrfs_put_delayed_ref(&head->node); |
| 2483 | return 0; |
| 2484 | out: |
| 2485 | spin_unlock(&delayed_refs->lock); |
| 2486 | return 0; |
| 2487 | } |
| 2488 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2489 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2490 | struct btrfs_root *root, |
| 2491 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2492 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2493 | u64 owner_objectid, int pin) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2494 | { |
| 2495 | int ret; |
| 2496 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2497 | /* |
| 2498 | * tree log blocks never actually go into the extent allocation |
| 2499 | * tree, just update pinning info and exit early. |
| 2500 | * |
| 2501 | * data extents referenced by the tree log do need to have |
| 2502 | * their reference counts bumped. |
| 2503 | */ |
| 2504 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID && |
| 2505 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2506 | /* unlocks the pinned mutex */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2507 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2508 | update_reserved_extents(root, bytenr, num_bytes, 0); |
| 2509 | ret = 0; |
| 2510 | } else { |
| 2511 | ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, |
| 2512 | root_objectid, ref_generation, |
| 2513 | owner_objectid, |
| 2514 | BTRFS_DROP_DELAYED_REF, 1); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2515 | BUG_ON(ret); |
| 2516 | ret = check_ref_cleanup(trans, root, bytenr); |
| 2517 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2518 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2519 | return ret; |
| 2520 | } |
| 2521 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2522 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 2523 | { |
| 2524 | u64 mask = ((u64)root->stripesize - 1); |
| 2525 | u64 ret = (val + mask) & ~mask; |
| 2526 | return ret; |
| 2527 | } |
| 2528 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2529 | /* |
| 2530 | * walks the btree of allocated extents and find a hole of a given size. |
| 2531 | * The key ins is changed to record the hole: |
| 2532 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2533 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2534 | * ins->offset == number of blocks |
| 2535 | * Any available blocks before search_start are skipped. |
| 2536 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2537 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2538 | struct btrfs_root *orig_root, |
| 2539 | u64 num_bytes, u64 empty_size, |
| 2540 | u64 search_start, u64 search_end, |
| 2541 | u64 hint_byte, struct btrfs_key *ins, |
| 2542 | u64 exclude_start, u64 exclude_nr, |
| 2543 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2544 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2545 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2546 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2547 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2548 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2549 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2550 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2551 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2552 | int last_ptr_loop = 0; |
| 2553 | int loop = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2554 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2555 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2556 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2557 | ins->objectid = 0; |
| 2558 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2559 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2560 | space_info = __find_space_info(root->fs_info, data); |
| 2561 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2562 | if (orig_root->ref_cows || empty_size) |
| 2563 | allowed_chunk_alloc = 1; |
| 2564 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2565 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2566 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 2567 | if (!btrfs_test_opt(root, SSD)) |
| 2568 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2569 | } |
| 2570 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2571 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 2572 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 2573 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2574 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2575 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2576 | spin_lock(&last_ptr->lock); |
| 2577 | if (last_ptr->block_group) |
| 2578 | hint_byte = last_ptr->window_start; |
| 2579 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2580 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2581 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2582 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2583 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2584 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2585 | if (!last_ptr) { |
| 2586 | empty_cluster = 0; |
| 2587 | loop = 1; |
| 2588 | } |
| 2589 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2590 | if (search_start == hint_byte) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2591 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 2592 | search_start); |
| 2593 | if (block_group && block_group_bits(block_group, data)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2594 | down_read(&space_info->groups_sem); |
| 2595 | goto have_block_group; |
| 2596 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2597 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2598 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2599 | } |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2600 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2601 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2602 | down_read(&space_info->groups_sem); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2603 | list_for_each_entry(block_group, &space_info->block_groups, list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2604 | u64 offset; |
Chris Mason | 8a1413a2 | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2605 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2606 | atomic_inc(&block_group->count); |
| 2607 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2608 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2609 | have_block_group: |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 2610 | if (unlikely(!block_group->cached)) { |
| 2611 | mutex_lock(&block_group->cache_mutex); |
| 2612 | ret = cache_block_group(root, block_group); |
| 2613 | mutex_unlock(&block_group->cache_mutex); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2614 | if (ret) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2615 | btrfs_put_block_group(block_group); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 2616 | break; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2617 | } |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 2618 | } |
| 2619 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 2620 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2621 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2622 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2623 | if (last_ptr) { |
| 2624 | /* |
| 2625 | * the refill lock keeps out other |
| 2626 | * people trying to start a new cluster |
| 2627 | */ |
| 2628 | spin_lock(&last_ptr->refill_lock); |
| 2629 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 2630 | num_bytes, search_start); |
| 2631 | if (offset) { |
| 2632 | /* we have a block, we're done */ |
| 2633 | spin_unlock(&last_ptr->refill_lock); |
| 2634 | goto checks; |
| 2635 | } |
| 2636 | |
| 2637 | spin_lock(&last_ptr->lock); |
| 2638 | /* |
| 2639 | * whoops, this cluster doesn't actually point to |
| 2640 | * this block group. Get a ref on the block |
| 2641 | * group is does point to and try again |
| 2642 | */ |
| 2643 | if (!last_ptr_loop && last_ptr->block_group && |
| 2644 | last_ptr->block_group != block_group) { |
| 2645 | |
| 2646 | btrfs_put_block_group(block_group); |
| 2647 | block_group = last_ptr->block_group; |
| 2648 | atomic_inc(&block_group->count); |
| 2649 | spin_unlock(&last_ptr->lock); |
| 2650 | spin_unlock(&last_ptr->refill_lock); |
| 2651 | |
| 2652 | last_ptr_loop = 1; |
| 2653 | search_start = block_group->key.objectid; |
| 2654 | goto have_block_group; |
| 2655 | } |
| 2656 | spin_unlock(&last_ptr->lock); |
| 2657 | |
| 2658 | /* |
| 2659 | * this cluster didn't work out, free it and |
| 2660 | * start over |
| 2661 | */ |
| 2662 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 2663 | |
| 2664 | last_ptr_loop = 0; |
| 2665 | |
| 2666 | /* allocate a cluster in this block group */ |
| 2667 | ret = btrfs_find_space_cluster(trans, |
| 2668 | block_group, last_ptr, |
| 2669 | offset, num_bytes, |
| 2670 | empty_cluster + empty_size); |
| 2671 | if (ret == 0) { |
| 2672 | /* |
| 2673 | * now pull our allocation out of this |
| 2674 | * cluster |
| 2675 | */ |
| 2676 | offset = btrfs_alloc_from_cluster(block_group, |
| 2677 | last_ptr, num_bytes, |
| 2678 | search_start); |
| 2679 | if (offset) { |
| 2680 | /* we found one, proceed */ |
| 2681 | spin_unlock(&last_ptr->refill_lock); |
| 2682 | goto checks; |
| 2683 | } |
| 2684 | } |
| 2685 | /* |
| 2686 | * at this point we either didn't find a cluster |
| 2687 | * or we weren't able to allocate a block from our |
| 2688 | * cluster. Free the cluster we've been trying |
| 2689 | * to use, and go to the next block group |
| 2690 | */ |
| 2691 | if (loop < 2) { |
| 2692 | btrfs_return_cluster_to_free_space(NULL, |
| 2693 | last_ptr); |
| 2694 | spin_unlock(&last_ptr->refill_lock); |
| 2695 | goto loop; |
| 2696 | } |
| 2697 | spin_unlock(&last_ptr->refill_lock); |
| 2698 | } |
| 2699 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2700 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 2701 | num_bytes, empty_size); |
| 2702 | if (!offset) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2703 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2704 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2705 | search_start = stripe_align(root, offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2706 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2707 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2708 | if (search_start + num_bytes >= search_end) { |
| 2709 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2710 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2711 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2712 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2713 | /* move on to the next group */ |
| 2714 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2715 | block_group->key.objectid + block_group->key.offset) { |
| 2716 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2717 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2718 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2719 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2720 | if (exclude_nr > 0 && |
| 2721 | (search_start + num_bytes > exclude_start && |
| 2722 | search_start < exclude_start + exclude_nr)) { |
| 2723 | search_start = exclude_start + exclude_nr; |
| 2724 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2725 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2726 | /* |
| 2727 | * if search_start is still in this block group |
| 2728 | * then we just re-search this block group |
| 2729 | */ |
| 2730 | if (search_start >= block_group->key.objectid && |
| 2731 | search_start < (block_group->key.objectid + |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2732 | block_group->key.offset)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2733 | goto have_block_group; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2734 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2735 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2736 | |
| 2737 | ins->objectid = search_start; |
| 2738 | ins->offset = num_bytes; |
| 2739 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2740 | if (offset < search_start) |
| 2741 | btrfs_add_free_space(block_group, offset, |
| 2742 | search_start - offset); |
| 2743 | BUG_ON(offset > search_start); |
| 2744 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2745 | /* we are all good, lets return */ |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2746 | break; |
| 2747 | loop: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2748 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2749 | } |
| 2750 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2751 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2752 | /* loop == 0, try to find a clustered alloc in every block group |
| 2753 | * loop == 1, try again after forcing a chunk allocation |
| 2754 | * loop == 2, set empty_size and empty_cluster to 0 and try again |
| 2755 | */ |
| 2756 | if (!ins->objectid && loop < 3 && |
| 2757 | (empty_size || empty_cluster || allowed_chunk_alloc)) { |
| 2758 | if (loop >= 2) { |
| 2759 | empty_size = 0; |
| 2760 | empty_cluster = 0; |
| 2761 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2762 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2763 | if (allowed_chunk_alloc) { |
| 2764 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 2765 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2766 | allowed_chunk_alloc = 0; |
| 2767 | } else { |
| 2768 | space_info->force_alloc = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2769 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2770 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2771 | if (loop < 3) { |
| 2772 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2773 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2774 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2775 | ret = -ENOSPC; |
| 2776 | } else if (!ins->objectid) { |
| 2777 | ret = -ENOSPC; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 2778 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2779 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2780 | /* we found what we needed */ |
| 2781 | if (ins->objectid) { |
| 2782 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2783 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2784 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2785 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 2786 | ret = 0; |
| 2787 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2788 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2789 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2790 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2791 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2792 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 2793 | { |
| 2794 | struct btrfs_block_group_cache *cache; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2795 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2796 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 2797 | (unsigned long long)(info->total_bytes - info->bytes_used - |
| 2798 | info->bytes_pinned - info->bytes_reserved), |
| 2799 | (info->full) ? "" : "not "); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2800 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," |
| 2801 | " may_use=%llu, used=%llu\n", info->total_bytes, |
| 2802 | info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use, |
| 2803 | info->bytes_used); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2804 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2805 | down_read(&info->groups_sem); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2806 | list_for_each_entry(cache, &info->block_groups, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2807 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2808 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 2809 | "%llu pinned %llu reserved\n", |
| 2810 | (unsigned long long)cache->key.objectid, |
| 2811 | (unsigned long long)cache->key.offset, |
| 2812 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 2813 | (unsigned long long)cache->pinned, |
| 2814 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2815 | btrfs_dump_free_space(cache, bytes); |
| 2816 | spin_unlock(&cache->lock); |
| 2817 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2818 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2819 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2820 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2821 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2822 | struct btrfs_root *root, |
| 2823 | u64 num_bytes, u64 min_alloc_size, |
| 2824 | u64 empty_size, u64 hint_byte, |
| 2825 | u64 search_end, struct btrfs_key *ins, |
| 2826 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2827 | { |
| 2828 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 2829 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2830 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2831 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2832 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2833 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2834 | /* |
| 2835 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 2836 | * is not called recursively on allocations |
| 2837 | */ |
| 2838 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2839 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2840 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2841 | 2 * 1024 * 1024, |
| 2842 | BTRFS_BLOCK_GROUP_METADATA | |
| 2843 | (info->metadata_alloc_profile & |
| 2844 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2845 | } |
| 2846 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2847 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2848 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2849 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2850 | WARN_ON(num_bytes < root->sectorsize); |
| 2851 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 2852 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2853 | trans->alloc_exclude_start, |
| 2854 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2855 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2856 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 2857 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2858 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2859 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2860 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2861 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2862 | goto again; |
| 2863 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2864 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2865 | struct btrfs_space_info *sinfo; |
| 2866 | |
| 2867 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2868 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 2869 | "wanted %llu\n", (unsigned long long)data, |
| 2870 | (unsigned long long)num_bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2871 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2872 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2873 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2874 | |
| 2875 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2876 | } |
| 2877 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2878 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 2879 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2880 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2881 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2882 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2883 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2884 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2885 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 2886 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2887 | return -ENOSPC; |
| 2888 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2889 | |
| 2890 | ret = btrfs_discard_extent(root, start, len); |
| 2891 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2892 | btrfs_add_free_space(cache, start, len); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 2893 | btrfs_put_block_group(cache); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2894 | update_reserved_extents(root, start, len, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2895 | |
| 2896 | return ret; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2897 | } |
| 2898 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2899 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2900 | struct btrfs_root *root, |
| 2901 | u64 num_bytes, u64 min_alloc_size, |
| 2902 | u64 empty_size, u64 hint_byte, |
| 2903 | u64 search_end, struct btrfs_key *ins, |
| 2904 | u64 data) |
| 2905 | { |
| 2906 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2907 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 2908 | empty_size, hint_byte, search_end, ins, |
| 2909 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2910 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2911 | return ret; |
| 2912 | } |
| 2913 | |
| 2914 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2915 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2916 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2917 | u64 owner, struct btrfs_key *ins, |
| 2918 | int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2919 | { |
| 2920 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2921 | u64 super_used; |
| 2922 | u64 root_used; |
| 2923 | u64 num_bytes = ins->offset; |
| 2924 | u32 sizes[2]; |
| 2925 | struct btrfs_fs_info *info = root->fs_info; |
| 2926 | struct btrfs_root *extent_root = info->extent_root; |
| 2927 | struct btrfs_extent_item *extent_item; |
| 2928 | struct btrfs_extent_ref *ref; |
| 2929 | struct btrfs_path *path; |
| 2930 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2931 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2932 | if (parent == 0) |
| 2933 | parent = ins->objectid; |
| 2934 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2935 | /* block accounting for super block */ |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2936 | spin_lock(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2937 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2938 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2939 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2940 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2941 | root_used = btrfs_root_used(&root->root_item); |
| 2942 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 2943 | spin_unlock(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2944 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2945 | memcpy(&keys[0], ins, sizeof(*ins)); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2946 | keys[1].objectid = ins->objectid; |
| 2947 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2948 | keys[1].offset = parent; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2949 | sizes[0] = sizeof(*extent_item); |
| 2950 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2951 | |
| 2952 | path = btrfs_alloc_path(); |
| 2953 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2954 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2955 | path->leave_spinning = 1; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2956 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 2957 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2958 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2959 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2960 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2961 | struct btrfs_extent_item); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2962 | btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2963 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 2964 | struct btrfs_extent_ref); |
| 2965 | |
| 2966 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 2967 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 2968 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2969 | btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2970 | |
| 2971 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2972 | |
| 2973 | trans->alloc_exclude_start = 0; |
| 2974 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2975 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2976 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2977 | if (ret) |
| 2978 | goto out; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2979 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2980 | ret = update_block_group(trans, root, ins->objectid, |
| 2981 | ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 2982 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2983 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 2984 | "%llu\n", (unsigned long long)ins->objectid, |
| 2985 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 2986 | BUG(); |
| 2987 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2988 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2989 | return ret; |
| 2990 | } |
| 2991 | |
| 2992 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2993 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2994 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2995 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2996 | { |
| 2997 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 2998 | |
| 2999 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 3000 | return 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3001 | |
| 3002 | ret = btrfs_add_delayed_ref(trans, ins->objectid, |
| 3003 | ins->offset, parent, root_objectid, |
| 3004 | ref_generation, owner, |
| 3005 | BTRFS_ADD_DELAYED_EXTENT, 0); |
| 3006 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3007 | return ret; |
| 3008 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3009 | |
| 3010 | /* |
| 3011 | * this is used by the tree logging recovery code. It records that |
| 3012 | * an extent has been allocated and makes sure to clear the free |
| 3013 | * space cache bits as well |
| 3014 | */ |
| 3015 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3016 | struct btrfs_root *root, u64 parent, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3017 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3018 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3019 | { |
| 3020 | int ret; |
| 3021 | struct btrfs_block_group_cache *block_group; |
| 3022 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3023 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3024 | mutex_lock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3025 | cache_block_group(root, block_group); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3026 | mutex_unlock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3027 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3028 | ret = btrfs_remove_free_space(block_group, ins->objectid, |
| 3029 | ins->offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3030 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 3031 | btrfs_put_block_group(block_group); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3032 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3033 | ref_generation, owner, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3034 | return ret; |
| 3035 | } |
| 3036 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3037 | /* |
| 3038 | * finds a free extent and does all the dirty work required for allocation |
| 3039 | * returns the key for the extent through ins, and a tree buffer for |
| 3040 | * the first block of the extent through buf. |
| 3041 | * |
| 3042 | * returns 0 if everything worked, non-zero otherwise. |
| 3043 | */ |
| 3044 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 3045 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3046 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3047 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3048 | u64 owner_objectid, u64 empty_size, u64 hint_byte, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3049 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 3050 | { |
| 3051 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3052 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 3053 | min_alloc_size, empty_size, hint_byte, |
| 3054 | search_end, ins, data); |
| 3055 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3056 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3057 | ret = btrfs_add_delayed_ref(trans, ins->objectid, |
| 3058 | ins->offset, parent, root_objectid, |
| 3059 | ref_generation, owner_objectid, |
| 3060 | BTRFS_ADD_DELAYED_EXTENT, 0); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3061 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3062 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3063 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3064 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3065 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3066 | |
| 3067 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 3068 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3069 | u64 bytenr, u32 blocksize, |
| 3070 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3071 | { |
| 3072 | struct extent_buffer *buf; |
| 3073 | |
| 3074 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 3075 | if (!buf) |
| 3076 | return ERR_PTR(-ENOMEM); |
| 3077 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3078 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3079 | btrfs_tree_lock(buf); |
| 3080 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3081 | |
| 3082 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3083 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3084 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3085 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 3086 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3087 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3088 | } else { |
| 3089 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 3090 | buf->start + buf->len - 1, GFP_NOFS); |
| 3091 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3092 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3093 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3094 | return buf; |
| 3095 | } |
| 3096 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3097 | /* |
| 3098 | * helper function to allocate a block for a given tree |
| 3099 | * returns the tree buffer or NULL. |
| 3100 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3101 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3102 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3103 | u32 blocksize, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3104 | u64 root_objectid, |
| 3105 | u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3106 | int level, |
| 3107 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3108 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3109 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3110 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3111 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3112 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3113 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3114 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3115 | root_objectid, ref_generation, level, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3116 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3117 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3118 | BUG_ON(ret > 0); |
| 3119 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3120 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 3121 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3122 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 3123 | blocksize, level); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3124 | return buf; |
| 3125 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3126 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3127 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3128 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3129 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3130 | u64 leaf_owner; |
| 3131 | u64 leaf_generation; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3132 | struct refsort *sorted; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3133 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3134 | struct btrfs_file_extent_item *fi; |
| 3135 | int i; |
| 3136 | int nritems; |
| 3137 | int ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3138 | int refi = 0; |
| 3139 | int slot; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3140 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3141 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 3142 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3143 | leaf_owner = btrfs_header_owner(leaf); |
| 3144 | leaf_generation = btrfs_header_generation(leaf); |
| 3145 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3146 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); |
| 3147 | /* we do this loop twice. The first time we build a list |
| 3148 | * of the extents we have a reference on, then we sort the list |
| 3149 | * by bytenr. The second time around we actually do the |
| 3150 | * extent freeing. |
| 3151 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3152 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3153 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 3154 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3155 | |
| 3156 | btrfs_item_key_to_cpu(leaf, &key, i); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3157 | |
| 3158 | /* only extents have references, skip everything else */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3159 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3160 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3161 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3162 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3163 | |
| 3164 | /* inline extents live in the btree, they don't have refs */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3165 | if (btrfs_file_extent_type(leaf, fi) == |
| 3166 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 3167 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3168 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3169 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3170 | |
| 3171 | /* holes don't have refs */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3172 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 3173 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3174 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3175 | sorted[refi].bytenr = disk_bytenr; |
| 3176 | sorted[refi].slot = i; |
| 3177 | refi++; |
| 3178 | } |
| 3179 | |
| 3180 | if (refi == 0) |
| 3181 | goto out; |
| 3182 | |
| 3183 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 3184 | |
| 3185 | for (i = 0; i < refi; i++) { |
| 3186 | u64 disk_bytenr; |
| 3187 | |
| 3188 | disk_bytenr = sorted[i].bytenr; |
| 3189 | slot = sorted[i].slot; |
| 3190 | |
| 3191 | cond_resched(); |
| 3192 | |
| 3193 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 3194 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 3195 | continue; |
| 3196 | |
| 3197 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 3198 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3199 | ret = btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3200 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3201 | leaf->start, leaf_owner, leaf_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3202 | key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3203 | BUG_ON(ret); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3204 | |
| 3205 | atomic_inc(&root->fs_info->throttle_gen); |
| 3206 | wake_up(&root->fs_info->transaction_throttle); |
| 3207 | cond_resched(); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3208 | } |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3209 | out: |
| 3210 | kfree(sorted); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3211 | return 0; |
| 3212 | } |
| 3213 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3214 | static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3215 | struct btrfs_root *root, |
| 3216 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3217 | { |
| 3218 | int i; |
| 3219 | int ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3220 | struct btrfs_extent_info *info; |
| 3221 | struct refsort *sorted; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3222 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3223 | if (ref->nritems == 0) |
| 3224 | return 0; |
| 3225 | |
| 3226 | sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3227 | for (i = 0; i < ref->nritems; i++) { |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3228 | sorted[i].bytenr = ref->extents[i].bytenr; |
| 3229 | sorted[i].slot = i; |
| 3230 | } |
| 3231 | sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL); |
| 3232 | |
| 3233 | /* |
| 3234 | * the items in the ref were sorted when the ref was inserted |
| 3235 | * into the ref cache, so this is already in order |
| 3236 | */ |
| 3237 | for (i = 0; i < ref->nritems; i++) { |
| 3238 | info = ref->extents + sorted[i].slot; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3239 | ret = btrfs_free_extent(trans, root, info->bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3240 | info->num_bytes, ref->bytenr, |
| 3241 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3242 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3243 | |
| 3244 | atomic_inc(&root->fs_info->throttle_gen); |
| 3245 | wake_up(&root->fs_info->transaction_throttle); |
| 3246 | cond_resched(); |
| 3247 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3248 | BUG_ON(ret); |
| 3249 | info++; |
| 3250 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3251 | |
Chris Mason | 806638b | 2009-02-05 09:08:14 -0500 | [diff] [blame] | 3252 | kfree(sorted); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3253 | return 0; |
| 3254 | } |
| 3255 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3256 | static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans, |
| 3257 | struct btrfs_root *root, u64 start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3258 | u64 len, u32 *refs) |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3259 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3260 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3261 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3262 | ret = btrfs_lookup_extent_ref(trans, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3263 | BUG_ON(ret); |
| 3264 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3265 | #if 0 /* some debugging code in case we see problems here */ |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3266 | /* if the refs count is one, it won't get increased again. But |
| 3267 | * if the ref count is > 1, someone may be decreasing it at |
| 3268 | * the same time we are. |
| 3269 | */ |
| 3270 | if (*refs != 1) { |
| 3271 | struct extent_buffer *eb = NULL; |
| 3272 | eb = btrfs_find_create_tree_block(root, start, len); |
| 3273 | if (eb) |
| 3274 | btrfs_tree_lock(eb); |
| 3275 | |
| 3276 | mutex_lock(&root->fs_info->alloc_mutex); |
| 3277 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 3278 | BUG_ON(ret); |
| 3279 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 3280 | |
| 3281 | if (eb) { |
| 3282 | btrfs_tree_unlock(eb); |
| 3283 | free_extent_buffer(eb); |
| 3284 | } |
| 3285 | if (*refs == 1) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3286 | printk(KERN_ERR "btrfs block %llu went down to one " |
| 3287 | "during drop_snap\n", (unsigned long long)start); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | } |
| 3291 | #endif |
| 3292 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3293 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3294 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | /* |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3298 | * this is used while deleting old snapshots, and it drops the refs |
| 3299 | * on a whole subtree starting from a level 1 node. |
| 3300 | * |
| 3301 | * The idea is to sort all the leaf pointers, and then drop the |
| 3302 | * ref on all the leaves in order. Most of the time the leaves |
| 3303 | * will have ref cache entries, so no leaf IOs will be required to |
| 3304 | * find the extents they have references on. |
| 3305 | * |
| 3306 | * For each leaf, any references it has are also dropped in order |
| 3307 | * |
| 3308 | * This ends up dropping the references in something close to optimal |
| 3309 | * order for reading and modifying the extent allocation tree. |
| 3310 | */ |
| 3311 | static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans, |
| 3312 | struct btrfs_root *root, |
| 3313 | struct btrfs_path *path) |
| 3314 | { |
| 3315 | u64 bytenr; |
| 3316 | u64 root_owner; |
| 3317 | u64 root_gen; |
| 3318 | struct extent_buffer *eb = path->nodes[1]; |
| 3319 | struct extent_buffer *leaf; |
| 3320 | struct btrfs_leaf_ref *ref; |
| 3321 | struct refsort *sorted = NULL; |
| 3322 | int nritems = btrfs_header_nritems(eb); |
| 3323 | int ret; |
| 3324 | int i; |
| 3325 | int refi = 0; |
| 3326 | int slot = path->slots[1]; |
| 3327 | u32 blocksize = btrfs_level_size(root, 0); |
| 3328 | u32 refs; |
| 3329 | |
| 3330 | if (nritems == 0) |
| 3331 | goto out; |
| 3332 | |
| 3333 | root_owner = btrfs_header_owner(eb); |
| 3334 | root_gen = btrfs_header_generation(eb); |
| 3335 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); |
| 3336 | |
| 3337 | /* |
| 3338 | * step one, sort all the leaf pointers so we don't scribble |
| 3339 | * randomly into the extent allocation tree |
| 3340 | */ |
| 3341 | for (i = slot; i < nritems; i++) { |
| 3342 | sorted[refi].bytenr = btrfs_node_blockptr(eb, i); |
| 3343 | sorted[refi].slot = i; |
| 3344 | refi++; |
| 3345 | } |
| 3346 | |
| 3347 | /* |
| 3348 | * nritems won't be zero, but if we're picking up drop_snapshot |
| 3349 | * after a crash, slot might be > 0, so double check things |
| 3350 | * just in case. |
| 3351 | */ |
| 3352 | if (refi == 0) |
| 3353 | goto out; |
| 3354 | |
| 3355 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 3356 | |
| 3357 | /* |
| 3358 | * the first loop frees everything the leaves point to |
| 3359 | */ |
| 3360 | for (i = 0; i < refi; i++) { |
| 3361 | u64 ptr_gen; |
| 3362 | |
| 3363 | bytenr = sorted[i].bytenr; |
| 3364 | |
| 3365 | /* |
| 3366 | * check the reference count on this leaf. If it is > 1 |
| 3367 | * we just decrement it below and don't update any |
| 3368 | * of the refs the leaf points to. |
| 3369 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3370 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
| 3371 | blocksize, &refs); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3372 | BUG_ON(ret); |
| 3373 | if (refs != 1) |
| 3374 | continue; |
| 3375 | |
| 3376 | ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot); |
| 3377 | |
| 3378 | /* |
| 3379 | * the leaf only had one reference, which means the |
| 3380 | * only thing pointing to this leaf is the snapshot |
| 3381 | * we're deleting. It isn't possible for the reference |
| 3382 | * count to increase again later |
| 3383 | * |
| 3384 | * The reference cache is checked for the leaf, |
| 3385 | * and if found we'll be able to drop any refs held by |
| 3386 | * the leaf without needing to read it in. |
| 3387 | */ |
| 3388 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
| 3389 | if (ref && ref->generation != ptr_gen) { |
| 3390 | btrfs_free_leaf_ref(root, ref); |
| 3391 | ref = NULL; |
| 3392 | } |
| 3393 | if (ref) { |
| 3394 | ret = cache_drop_leaf_ref(trans, root, ref); |
| 3395 | BUG_ON(ret); |
| 3396 | btrfs_remove_leaf_ref(root, ref); |
| 3397 | btrfs_free_leaf_ref(root, ref); |
| 3398 | } else { |
| 3399 | /* |
| 3400 | * the leaf wasn't in the reference cache, so |
| 3401 | * we have to read it. |
| 3402 | */ |
| 3403 | leaf = read_tree_block(root, bytenr, blocksize, |
| 3404 | ptr_gen); |
| 3405 | ret = btrfs_drop_leaf_ref(trans, root, leaf); |
| 3406 | BUG_ON(ret); |
| 3407 | free_extent_buffer(leaf); |
| 3408 | } |
| 3409 | atomic_inc(&root->fs_info->throttle_gen); |
| 3410 | wake_up(&root->fs_info->transaction_throttle); |
| 3411 | cond_resched(); |
| 3412 | } |
| 3413 | |
| 3414 | /* |
| 3415 | * run through the loop again to free the refs on the leaves. |
| 3416 | * This is faster than doing it in the loop above because |
| 3417 | * the leaves are likely to be clustered together. We end up |
| 3418 | * working in nice chunks on the extent allocation tree. |
| 3419 | */ |
| 3420 | for (i = 0; i < refi; i++) { |
| 3421 | bytenr = sorted[i].bytenr; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3422 | ret = btrfs_free_extent(trans, root, bytenr, |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3423 | blocksize, eb->start, |
| 3424 | root_owner, root_gen, 0, 1); |
| 3425 | BUG_ON(ret); |
| 3426 | |
| 3427 | atomic_inc(&root->fs_info->throttle_gen); |
| 3428 | wake_up(&root->fs_info->transaction_throttle); |
| 3429 | cond_resched(); |
| 3430 | } |
| 3431 | out: |
| 3432 | kfree(sorted); |
| 3433 | |
| 3434 | /* |
| 3435 | * update the path to show we've processed the entire level 1 |
| 3436 | * node. This will get saved into the root's drop_snapshot_progress |
| 3437 | * field so these drops are not repeated again if this transaction |
| 3438 | * commits. |
| 3439 | */ |
| 3440 | path->slots[1] = nritems; |
| 3441 | return 0; |
| 3442 | } |
| 3443 | |
| 3444 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3445 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 3446 | * counts as it goes. |
| 3447 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3448 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3449 | struct btrfs_root *root, |
| 3450 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3451 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3452 | u64 root_owner; |
| 3453 | u64 root_gen; |
| 3454 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3455 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3456 | struct extent_buffer *next; |
| 3457 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3458 | struct extent_buffer *parent; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3459 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3460 | int ret; |
| 3461 | u32 refs; |
| 3462 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3463 | WARN_ON(*level < 0); |
| 3464 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3465 | ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3466 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3467 | BUG_ON(ret); |
| 3468 | if (refs > 1) |
| 3469 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3470 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3471 | /* |
| 3472 | * walk down to the last node level and free all the leaves |
| 3473 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3474 | while (*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3475 | WARN_ON(*level < 0); |
| 3476 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3477 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3478 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3479 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3480 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3481 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3482 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3483 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3484 | break; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3485 | |
| 3486 | /* the new code goes down to level 1 and does all the |
| 3487 | * leaves pointed to that node in bulk. So, this check |
| 3488 | * for level 0 will always be false. |
| 3489 | * |
| 3490 | * But, the disk format allows the drop_snapshot_progress |
| 3491 | * field in the root to leave things in a state where |
| 3492 | * a leaf will need cleaning up here. If someone crashes |
| 3493 | * with the old code and then boots with the new code, |
| 3494 | * we might find a leaf here. |
| 3495 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3496 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3497 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3498 | BUG_ON(ret); |
| 3499 | break; |
| 3500 | } |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3501 | |
| 3502 | /* |
| 3503 | * once we get to level one, process the whole node |
| 3504 | * at once, including everything below it. |
| 3505 | */ |
| 3506 | if (*level == 1) { |
| 3507 | ret = drop_level_one_refs(trans, root, path); |
| 3508 | BUG_ON(ret); |
| 3509 | break; |
| 3510 | } |
| 3511 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3512 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3513 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3514 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3515 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3516 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
| 3517 | blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3518 | BUG_ON(ret); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3519 | |
| 3520 | /* |
| 3521 | * if there is more than one reference, we don't need |
| 3522 | * to read that node to drop any references it has. We |
| 3523 | * just drop the ref we hold on that node and move on to the |
| 3524 | * next slot in this level. |
| 3525 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3526 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3527 | parent = path->nodes[*level]; |
| 3528 | root_owner = btrfs_header_owner(parent); |
| 3529 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3530 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3531 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3532 | ret = btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3533 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3534 | root_owner, root_gen, |
| 3535 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3536 | BUG_ON(ret); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3537 | |
| 3538 | atomic_inc(&root->fs_info->throttle_gen); |
| 3539 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3540 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3541 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3542 | continue; |
| 3543 | } |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3544 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3545 | /* |
| 3546 | * we need to keep freeing things in the next level down. |
| 3547 | * read the block and loop around to process it |
| 3548 | */ |
| 3549 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3550 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3551 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3552 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3553 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3554 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3555 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3556 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3557 | } |
| 3558 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3559 | WARN_ON(*level < 0); |
| 3560 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3561 | |
| 3562 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3563 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3564 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3565 | } else { |
| 3566 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3567 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3568 | } |
| 3569 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3570 | blocksize = btrfs_level_size(root, *level); |
| 3571 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3572 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3573 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3574 | /* |
| 3575 | * cleanup and free the reference on the last node |
| 3576 | * we processed |
| 3577 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3578 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3579 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3580 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3581 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3582 | path->nodes[*level] = NULL; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3583 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3584 | *level += 1; |
| 3585 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3586 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3587 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3588 | return 0; |
| 3589 | } |
| 3590 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3591 | /* |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3592 | * helper function for drop_subtree, this function is similar to |
| 3593 | * walk_down_tree. The main difference is that it checks reference |
| 3594 | * counts while tree blocks are locked. |
| 3595 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3596 | static noinline int walk_down_subtree(struct btrfs_trans_handle *trans, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3597 | struct btrfs_root *root, |
| 3598 | struct btrfs_path *path, int *level) |
| 3599 | { |
| 3600 | struct extent_buffer *next; |
| 3601 | struct extent_buffer *cur; |
| 3602 | struct extent_buffer *parent; |
| 3603 | u64 bytenr; |
| 3604 | u64 ptr_gen; |
| 3605 | u32 blocksize; |
| 3606 | u32 refs; |
| 3607 | int ret; |
| 3608 | |
| 3609 | cur = path->nodes[*level]; |
| 3610 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, |
| 3611 | &refs); |
| 3612 | BUG_ON(ret); |
| 3613 | if (refs > 1) |
| 3614 | goto out; |
| 3615 | |
| 3616 | while (*level >= 0) { |
| 3617 | cur = path->nodes[*level]; |
| 3618 | if (*level == 0) { |
| 3619 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
| 3620 | BUG_ON(ret); |
| 3621 | clean_tree_block(trans, root, cur); |
| 3622 | break; |
| 3623 | } |
| 3624 | if (path->slots[*level] >= btrfs_header_nritems(cur)) { |
| 3625 | clean_tree_block(trans, root, cur); |
| 3626 | break; |
| 3627 | } |
| 3628 | |
| 3629 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 3630 | blocksize = btrfs_level_size(root, *level - 1); |
| 3631 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
| 3632 | |
| 3633 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
| 3634 | btrfs_tree_lock(next); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3635 | btrfs_set_lock_blocking(next); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3636 | |
| 3637 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, |
| 3638 | &refs); |
| 3639 | BUG_ON(ret); |
| 3640 | if (refs > 1) { |
| 3641 | parent = path->nodes[*level]; |
| 3642 | ret = btrfs_free_extent(trans, root, bytenr, |
| 3643 | blocksize, parent->start, |
| 3644 | btrfs_header_owner(parent), |
| 3645 | btrfs_header_generation(parent), |
| 3646 | *level - 1, 1); |
| 3647 | BUG_ON(ret); |
| 3648 | path->slots[*level]++; |
| 3649 | btrfs_tree_unlock(next); |
| 3650 | free_extent_buffer(next); |
| 3651 | continue; |
| 3652 | } |
| 3653 | |
| 3654 | *level = btrfs_header_level(next); |
| 3655 | path->nodes[*level] = next; |
| 3656 | path->slots[*level] = 0; |
| 3657 | path->locks[*level] = 1; |
| 3658 | cond_resched(); |
| 3659 | } |
| 3660 | out: |
| 3661 | parent = path->nodes[*level + 1]; |
| 3662 | bytenr = path->nodes[*level]->start; |
| 3663 | blocksize = path->nodes[*level]->len; |
| 3664 | |
| 3665 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
| 3666 | parent->start, btrfs_header_owner(parent), |
| 3667 | btrfs_header_generation(parent), *level, 1); |
| 3668 | BUG_ON(ret); |
| 3669 | |
| 3670 | if (path->locks[*level]) { |
| 3671 | btrfs_tree_unlock(path->nodes[*level]); |
| 3672 | path->locks[*level] = 0; |
| 3673 | } |
| 3674 | free_extent_buffer(path->nodes[*level]); |
| 3675 | path->nodes[*level] = NULL; |
| 3676 | *level += 1; |
| 3677 | cond_resched(); |
| 3678 | return 0; |
| 3679 | } |
| 3680 | |
| 3681 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3682 | * helper for dropping snapshots. This walks back up the tree in the path |
| 3683 | * to find the first node higher up where we haven't yet gone through |
| 3684 | * all the slots |
| 3685 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3686 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3687 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3688 | struct btrfs_path *path, |
| 3689 | int *level, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3690 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3691 | u64 root_owner; |
| 3692 | u64 root_gen; |
| 3693 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3694 | int i; |
| 3695 | int slot; |
| 3696 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3697 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3698 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3699 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3700 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 3701 | struct extent_buffer *node; |
| 3702 | struct btrfs_disk_key disk_key; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3703 | |
| 3704 | /* |
| 3705 | * there is more work to do in this level. |
| 3706 | * Update the drop_progress marker to reflect |
| 3707 | * the work we've done so far, and then bump |
| 3708 | * the slot number |
| 3709 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3710 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3711 | path->slots[i]++; |
| 3712 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3713 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3714 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3715 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3716 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3717 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3718 | return 0; |
| 3719 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3720 | struct extent_buffer *parent; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3721 | |
| 3722 | /* |
| 3723 | * this whole node is done, free our reference |
| 3724 | * on it and go up one level |
| 3725 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3726 | if (path->nodes[*level] == root->node) |
| 3727 | parent = path->nodes[*level]; |
| 3728 | else |
| 3729 | parent = path->nodes[*level + 1]; |
| 3730 | |
| 3731 | root_owner = btrfs_header_owner(parent); |
| 3732 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3733 | |
| 3734 | clean_tree_block(trans, root, path->nodes[*level]); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3735 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3736 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3737 | path->nodes[*level]->len, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3738 | parent->start, root_owner, |
| 3739 | root_gen, *level, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3740 | BUG_ON(ret); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3741 | if (path->locks[*level]) { |
| 3742 | btrfs_tree_unlock(path->nodes[*level]); |
| 3743 | path->locks[*level] = 0; |
| 3744 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3745 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3746 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3747 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3748 | } |
| 3749 | } |
| 3750 | return 1; |
| 3751 | } |
| 3752 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3753 | /* |
| 3754 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 3755 | * the tree freeing any blocks that have a ref count of zero after being |
| 3756 | * decremented. |
| 3757 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3758 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3759 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3760 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 3761 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3762 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3763 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3764 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3765 | int i; |
| 3766 | int orig_level; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3767 | int update_count; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3768 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3769 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3770 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3771 | path = btrfs_alloc_path(); |
| 3772 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3773 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3774 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3775 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3776 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 3777 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3778 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3779 | path->slots[level] = 0; |
| 3780 | } else { |
| 3781 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3782 | struct btrfs_disk_key found_key; |
| 3783 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3784 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3785 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3786 | level = root_item->drop_level; |
| 3787 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3788 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3789 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3790 | ret = wret; |
| 3791 | goto out; |
| 3792 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3793 | node = path->nodes[level]; |
| 3794 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 3795 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 3796 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3797 | /* |
| 3798 | * unlock our path, this is safe because only this |
| 3799 | * function is allowed to delete this snapshot |
| 3800 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3801 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 3802 | if (path->nodes[i] && path->locks[i]) { |
| 3803 | path->locks[i] = 0; |
| 3804 | btrfs_tree_unlock(path->nodes[i]); |
| 3805 | } |
| 3806 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3807 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3808 | while (1) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3809 | unsigned long update; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3810 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3811 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3812 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3813 | if (wret < 0) |
| 3814 | ret = wret; |
| 3815 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3816 | wret = walk_up_tree(trans, root, path, &level, |
| 3817 | BTRFS_MAX_LEVEL); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3818 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3819 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3820 | if (wret < 0) |
| 3821 | ret = wret; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 3822 | if (trans->transaction->in_commit || |
| 3823 | trans->transaction->delayed_refs.flushing) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3824 | ret = -EAGAIN; |
| 3825 | break; |
| 3826 | } |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3827 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3828 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3829 | for (update_count = 0; update_count < 16; update_count++) { |
| 3830 | update = trans->delayed_ref_updates; |
| 3831 | trans->delayed_ref_updates = 0; |
| 3832 | if (update) |
| 3833 | btrfs_run_delayed_refs(trans, root, update); |
| 3834 | else |
| 3835 | break; |
| 3836 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3837 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3838 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3839 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3840 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 3841 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3842 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3843 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3844 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3845 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3846 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3847 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3848 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3849 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 3850 | struct btrfs_root *root, |
| 3851 | struct extent_buffer *node, |
| 3852 | struct extent_buffer *parent) |
| 3853 | { |
| 3854 | struct btrfs_path *path; |
| 3855 | int level; |
| 3856 | int parent_level; |
| 3857 | int ret = 0; |
| 3858 | int wret; |
| 3859 | |
| 3860 | path = btrfs_alloc_path(); |
| 3861 | BUG_ON(!path); |
| 3862 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 3863 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3864 | parent_level = btrfs_header_level(parent); |
| 3865 | extent_buffer_get(parent); |
| 3866 | path->nodes[parent_level] = parent; |
| 3867 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 3868 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 3869 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3870 | level = btrfs_header_level(node); |
| 3871 | extent_buffer_get(node); |
| 3872 | path->nodes[level] = node; |
| 3873 | path->slots[level] = 0; |
| 3874 | |
| 3875 | while (1) { |
| 3876 | wret = walk_down_subtree(trans, root, path, &level); |
| 3877 | if (wret < 0) |
| 3878 | ret = wret; |
| 3879 | if (wret != 0) |
| 3880 | break; |
| 3881 | |
| 3882 | wret = walk_up_tree(trans, root, path, &level, parent_level); |
| 3883 | if (wret < 0) |
| 3884 | ret = wret; |
| 3885 | if (wret != 0) |
| 3886 | break; |
| 3887 | } |
| 3888 | |
| 3889 | btrfs_free_path(path); |
| 3890 | return ret; |
| 3891 | } |
| 3892 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3893 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 3894 | unsigned long nr) |
| 3895 | { |
| 3896 | return min(last, start + nr - 1); |
| 3897 | } |
| 3898 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3899 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3900 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3901 | { |
| 3902 | u64 page_start; |
| 3903 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3904 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3905 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3906 | unsigned long i; |
| 3907 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3908 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3909 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3910 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3911 | unsigned int total_read = 0; |
| 3912 | unsigned int total_dirty = 0; |
| 3913 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3914 | |
| 3915 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3916 | |
| 3917 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3918 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3919 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 3920 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3921 | /* make sure the dirty trick played by the caller work */ |
| 3922 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 3923 | first_index, last_index); |
| 3924 | if (ret) |
| 3925 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3926 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3927 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3928 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3929 | for (i = first_index ; i <= last_index; i++) { |
| 3930 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3931 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3932 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3933 | } |
| 3934 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3935 | again: |
| 3936 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3937 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3938 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3939 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3940 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3941 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3942 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3943 | if (!PageUptodate(page)) { |
| 3944 | btrfs_readpage(NULL, page); |
| 3945 | lock_page(page); |
| 3946 | if (!PageUptodate(page)) { |
| 3947 | unlock_page(page); |
| 3948 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3949 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3950 | goto out_unlock; |
| 3951 | } |
| 3952 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3953 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3954 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3955 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 3956 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3957 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3958 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3959 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3960 | if (ordered) { |
| 3961 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3962 | unlock_page(page); |
| 3963 | page_cache_release(page); |
| 3964 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3965 | btrfs_put_ordered_extent(ordered); |
| 3966 | goto again; |
| 3967 | } |
| 3968 | set_page_extent_mapped(page); |
| 3969 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3970 | if (i == first_index) |
| 3971 | set_extent_bits(io_tree, page_start, page_end, |
| 3972 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 3973 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3974 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3975 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3976 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3977 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3978 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3979 | unlock_page(page); |
| 3980 | page_cache_release(page); |
| 3981 | } |
| 3982 | |
| 3983 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3984 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3985 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3986 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3987 | return ret; |
| 3988 | } |
| 3989 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3990 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3991 | struct btrfs_key *extent_key, |
| 3992 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3993 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3994 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 3995 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 3996 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3997 | u64 start = extent_key->objectid - offset; |
| 3998 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3999 | |
| 4000 | em = alloc_extent_map(GFP_NOFS); |
| 4001 | BUG_ON(!em || IS_ERR(em)); |
| 4002 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4003 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4004 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4005 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4006 | em->block_start = extent_key->objectid; |
| 4007 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4008 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4009 | |
| 4010 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4011 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4012 | while (1) { |
| 4013 | int ret; |
| 4014 | spin_lock(&em_tree->lock); |
| 4015 | ret = add_extent_mapping(em_tree, em); |
| 4016 | spin_unlock(&em_tree->lock); |
| 4017 | if (ret != -EEXIST) { |
| 4018 | free_extent_map(em); |
| 4019 | break; |
| 4020 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4021 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4022 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4023 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4024 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4025 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4026 | } |
| 4027 | |
| 4028 | struct btrfs_ref_path { |
| 4029 | u64 extent_start; |
| 4030 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 4031 | u64 root_objectid; |
| 4032 | u64 root_generation; |
| 4033 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4034 | u32 num_refs; |
| 4035 | int lowest_level; |
| 4036 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4037 | int shared_level; |
| 4038 | |
| 4039 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 4040 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4041 | }; |
| 4042 | |
| 4043 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4044 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4045 | u64 disk_bytenr; |
| 4046 | u64 disk_num_bytes; |
| 4047 | u64 offset; |
| 4048 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4049 | u8 compression; |
| 4050 | u8 encryption; |
| 4051 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4052 | }; |
| 4053 | |
| 4054 | static int is_cowonly_root(u64 root_objectid) |
| 4055 | { |
| 4056 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 4057 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 4058 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 4059 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 4060 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 4061 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4062 | return 1; |
| 4063 | return 0; |
| 4064 | } |
| 4065 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4066 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4067 | struct btrfs_root *extent_root, |
| 4068 | struct btrfs_ref_path *ref_path, |
| 4069 | int first_time) |
| 4070 | { |
| 4071 | struct extent_buffer *leaf; |
| 4072 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4073 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4074 | struct btrfs_key key; |
| 4075 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4076 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4077 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4078 | int level; |
| 4079 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4080 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4081 | path = btrfs_alloc_path(); |
| 4082 | if (!path) |
| 4083 | return -ENOMEM; |
| 4084 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4085 | if (first_time) { |
| 4086 | ref_path->lowest_level = -1; |
| 4087 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4088 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4089 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4090 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4091 | walk_down: |
| 4092 | level = ref_path->current_level - 1; |
| 4093 | while (level >= -1) { |
| 4094 | u64 parent; |
| 4095 | if (level < ref_path->lowest_level) |
| 4096 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4097 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4098 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4099 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4100 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4101 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4102 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4103 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4104 | parent = ref_path->nodes[level + 1]; |
| 4105 | ref_path->nodes[level + 1] = 0; |
| 4106 | ref_path->current_level = level; |
| 4107 | BUG_ON(parent == 0); |
| 4108 | |
| 4109 | key.objectid = bytenr; |
| 4110 | key.offset = parent + 1; |
| 4111 | key.type = BTRFS_EXTENT_REF_KEY; |
| 4112 | |
| 4113 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4114 | if (ret < 0) |
| 4115 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4116 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4117 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4118 | leaf = path->nodes[0]; |
| 4119 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4120 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4121 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4122 | if (ret < 0) |
| 4123 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4124 | if (ret > 0) |
| 4125 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 4126 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4127 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4128 | |
| 4129 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4130 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4131 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 4132 | if (level < ref_path->shared_level) |
| 4133 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4134 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4135 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4136 | next: |
| 4137 | level--; |
| 4138 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4139 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4140 | } |
| 4141 | /* reached lowest level */ |
| 4142 | ret = 1; |
| 4143 | goto out; |
| 4144 | walk_up: |
| 4145 | level = ref_path->current_level; |
| 4146 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 4147 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4148 | |
| 4149 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4150 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4151 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4152 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4153 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4154 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4155 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4156 | key.objectid = bytenr; |
| 4157 | key.offset = 0; |
| 4158 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4159 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4160 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 4161 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4162 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4163 | |
| 4164 | leaf = path->nodes[0]; |
| 4165 | nritems = btrfs_header_nritems(leaf); |
| 4166 | if (path->slots[0] >= nritems) { |
| 4167 | ret = btrfs_next_leaf(extent_root, path); |
| 4168 | if (ret < 0) |
| 4169 | goto out; |
| 4170 | if (ret > 0) { |
| 4171 | /* the extent was freed by someone */ |
| 4172 | if (ref_path->lowest_level == level) |
| 4173 | goto out; |
| 4174 | btrfs_release_path(extent_root, path); |
| 4175 | goto walk_down; |
| 4176 | } |
| 4177 | leaf = path->nodes[0]; |
| 4178 | } |
| 4179 | |
| 4180 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4181 | if (found_key.objectid != bytenr || |
| 4182 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 4183 | /* the extent was freed by someone */ |
| 4184 | if (ref_path->lowest_level == level) { |
| 4185 | ret = 1; |
| 4186 | goto out; |
| 4187 | } |
| 4188 | btrfs_release_path(extent_root, path); |
| 4189 | goto walk_down; |
| 4190 | } |
| 4191 | found: |
| 4192 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 4193 | struct btrfs_extent_ref); |
| 4194 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 4195 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4196 | if (first_time) { |
| 4197 | level = (int)ref_objectid; |
| 4198 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 4199 | ref_path->lowest_level = level; |
| 4200 | ref_path->current_level = level; |
| 4201 | ref_path->nodes[level] = bytenr; |
| 4202 | } else { |
| 4203 | WARN_ON(ref_objectid != level); |
| 4204 | } |
| 4205 | } else { |
| 4206 | WARN_ON(level != -1); |
| 4207 | } |
| 4208 | first_time = 0; |
| 4209 | |
| 4210 | if (ref_path->lowest_level == level) { |
| 4211 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4212 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 4213 | } |
| 4214 | |
| 4215 | /* |
| 4216 | * the block is tree root or the block isn't in reference |
| 4217 | * counted tree. |
| 4218 | */ |
| 4219 | if (found_key.objectid == found_key.offset || |
| 4220 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 4221 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4222 | ref_path->root_generation = |
| 4223 | btrfs_ref_generation(leaf, ref); |
| 4224 | if (level < 0) { |
| 4225 | /* special reference from the tree log */ |
| 4226 | ref_path->nodes[0] = found_key.offset; |
| 4227 | ref_path->current_level = 0; |
| 4228 | } |
| 4229 | ret = 0; |
| 4230 | goto out; |
| 4231 | } |
| 4232 | |
| 4233 | level++; |
| 4234 | BUG_ON(ref_path->nodes[level] != 0); |
| 4235 | ref_path->nodes[level] = found_key.offset; |
| 4236 | ref_path->current_level = level; |
| 4237 | |
| 4238 | /* |
| 4239 | * the reference was created in the running transaction, |
| 4240 | * no need to continue walking up. |
| 4241 | */ |
| 4242 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 4243 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4244 | ref_path->root_generation = |
| 4245 | btrfs_ref_generation(leaf, ref); |
| 4246 | ret = 0; |
| 4247 | goto out; |
| 4248 | } |
| 4249 | |
| 4250 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4251 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4252 | } |
| 4253 | /* reached max tree level, but no tree root found. */ |
| 4254 | BUG(); |
| 4255 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4256 | btrfs_free_path(path); |
| 4257 | return ret; |
| 4258 | } |
| 4259 | |
| 4260 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 4261 | struct btrfs_root *extent_root, |
| 4262 | struct btrfs_ref_path *ref_path, |
| 4263 | u64 extent_start) |
| 4264 | { |
| 4265 | memset(ref_path, 0, sizeof(*ref_path)); |
| 4266 | ref_path->extent_start = extent_start; |
| 4267 | |
| 4268 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 4269 | } |
| 4270 | |
| 4271 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 4272 | struct btrfs_root *extent_root, |
| 4273 | struct btrfs_ref_path *ref_path) |
| 4274 | { |
| 4275 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 4276 | } |
| 4277 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4278 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4279 | struct btrfs_key *extent_key, |
| 4280 | u64 offset, int no_fragment, |
| 4281 | struct disk_extent **extents, |
| 4282 | int *nr_extents) |
| 4283 | { |
| 4284 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4285 | struct btrfs_path *path; |
| 4286 | struct btrfs_file_extent_item *fi; |
| 4287 | struct extent_buffer *leaf; |
| 4288 | struct disk_extent *exts = *extents; |
| 4289 | struct btrfs_key found_key; |
| 4290 | u64 cur_pos; |
| 4291 | u64 last_byte; |
| 4292 | u32 nritems; |
| 4293 | int nr = 0; |
| 4294 | int max = *nr_extents; |
| 4295 | int ret; |
| 4296 | |
| 4297 | WARN_ON(!no_fragment && *extents); |
| 4298 | if (!exts) { |
| 4299 | max = 1; |
| 4300 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4301 | if (!exts) |
| 4302 | return -ENOMEM; |
| 4303 | } |
| 4304 | |
| 4305 | path = btrfs_alloc_path(); |
| 4306 | BUG_ON(!path); |
| 4307 | |
| 4308 | cur_pos = extent_key->objectid - offset; |
| 4309 | last_byte = extent_key->objectid + extent_key->offset; |
| 4310 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 4311 | cur_pos, 0); |
| 4312 | if (ret < 0) |
| 4313 | goto out; |
| 4314 | if (ret > 0) { |
| 4315 | ret = -ENOENT; |
| 4316 | goto out; |
| 4317 | } |
| 4318 | |
| 4319 | while (1) { |
| 4320 | leaf = path->nodes[0]; |
| 4321 | nritems = btrfs_header_nritems(leaf); |
| 4322 | if (path->slots[0] >= nritems) { |
| 4323 | ret = btrfs_next_leaf(root, path); |
| 4324 | if (ret < 0) |
| 4325 | goto out; |
| 4326 | if (ret > 0) |
| 4327 | break; |
| 4328 | leaf = path->nodes[0]; |
| 4329 | } |
| 4330 | |
| 4331 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4332 | if (found_key.offset != cur_pos || |
| 4333 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 4334 | found_key.objectid != reloc_inode->i_ino) |
| 4335 | break; |
| 4336 | |
| 4337 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4338 | struct btrfs_file_extent_item); |
| 4339 | if (btrfs_file_extent_type(leaf, fi) != |
| 4340 | BTRFS_FILE_EXTENT_REG || |
| 4341 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4342 | break; |
| 4343 | |
| 4344 | if (nr == max) { |
| 4345 | struct disk_extent *old = exts; |
| 4346 | max *= 2; |
| 4347 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4348 | memcpy(exts, old, sizeof(*exts) * nr); |
| 4349 | if (old != *extents) |
| 4350 | kfree(old); |
| 4351 | } |
| 4352 | |
| 4353 | exts[nr].disk_bytenr = |
| 4354 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4355 | exts[nr].disk_num_bytes = |
| 4356 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4357 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 4358 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4359 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 4360 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 4361 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 4362 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 4363 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4364 | BUG_ON(exts[nr].offset > 0); |
| 4365 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 4366 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4367 | |
| 4368 | cur_pos += exts[nr].num_bytes; |
| 4369 | nr++; |
| 4370 | |
| 4371 | if (cur_pos + offset >= last_byte) |
| 4372 | break; |
| 4373 | |
| 4374 | if (no_fragment) { |
| 4375 | ret = 1; |
| 4376 | goto out; |
| 4377 | } |
| 4378 | path->slots[0]++; |
| 4379 | } |
| 4380 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 4381 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4382 | if (cur_pos + offset < last_byte) { |
| 4383 | ret = -ENOENT; |
| 4384 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4385 | } |
| 4386 | ret = 0; |
| 4387 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4388 | btrfs_free_path(path); |
| 4389 | if (ret) { |
| 4390 | if (exts != *extents) |
| 4391 | kfree(exts); |
| 4392 | } else { |
| 4393 | *extents = exts; |
| 4394 | *nr_extents = nr; |
| 4395 | } |
| 4396 | return ret; |
| 4397 | } |
| 4398 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4399 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4400 | struct btrfs_root *root, |
| 4401 | struct btrfs_path *path, |
| 4402 | struct btrfs_key *extent_key, |
| 4403 | struct btrfs_key *leaf_key, |
| 4404 | struct btrfs_ref_path *ref_path, |
| 4405 | struct disk_extent *new_extents, |
| 4406 | int nr_extents) |
| 4407 | { |
| 4408 | struct extent_buffer *leaf; |
| 4409 | struct btrfs_file_extent_item *fi; |
| 4410 | struct inode *inode = NULL; |
| 4411 | struct btrfs_key key; |
| 4412 | u64 lock_start = 0; |
| 4413 | u64 lock_end = 0; |
| 4414 | u64 num_bytes; |
| 4415 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4416 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4417 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4418 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4419 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4420 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4421 | int ret; |
| 4422 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4423 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4424 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4425 | if (key.objectid < ref_path->owner_objectid || |
| 4426 | (key.objectid == ref_path->owner_objectid && |
| 4427 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 4428 | key.objectid = ref_path->owner_objectid; |
| 4429 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4430 | key.offset = 0; |
| 4431 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | while (1) { |
| 4435 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 4436 | if (ret < 0) |
| 4437 | goto out; |
| 4438 | |
| 4439 | leaf = path->nodes[0]; |
| 4440 | nritems = btrfs_header_nritems(leaf); |
| 4441 | next: |
| 4442 | if (extent_locked && ret > 0) { |
| 4443 | /* |
| 4444 | * the file extent item was modified by someone |
| 4445 | * before the extent got locked. |
| 4446 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4447 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4448 | lock_end, GFP_NOFS); |
| 4449 | extent_locked = 0; |
| 4450 | } |
| 4451 | |
| 4452 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4453 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4454 | break; |
| 4455 | |
| 4456 | BUG_ON(extent_locked); |
| 4457 | ret = btrfs_next_leaf(root, path); |
| 4458 | if (ret < 0) |
| 4459 | goto out; |
| 4460 | if (ret > 0) |
| 4461 | break; |
| 4462 | leaf = path->nodes[0]; |
| 4463 | nritems = btrfs_header_nritems(leaf); |
| 4464 | } |
| 4465 | |
| 4466 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4467 | |
| 4468 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 4469 | if ((key.objectid > ref_path->owner_objectid) || |
| 4470 | (key.objectid == ref_path->owner_objectid && |
| 4471 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4472 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4473 | break; |
| 4474 | } |
| 4475 | |
| 4476 | if (inode && key.objectid != inode->i_ino) { |
| 4477 | BUG_ON(extent_locked); |
| 4478 | btrfs_release_path(root, path); |
| 4479 | mutex_unlock(&inode->i_mutex); |
| 4480 | iput(inode); |
| 4481 | inode = NULL; |
| 4482 | continue; |
| 4483 | } |
| 4484 | |
| 4485 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4486 | path->slots[0]++; |
| 4487 | ret = 1; |
| 4488 | goto next; |
| 4489 | } |
| 4490 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4491 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4492 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4493 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 4494 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4495 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 4496 | extent_key->objectid)) { |
| 4497 | path->slots[0]++; |
| 4498 | ret = 1; |
| 4499 | goto next; |
| 4500 | } |
| 4501 | |
| 4502 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4503 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 4504 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4505 | if (search_end == (u64)-1) { |
| 4506 | search_end = key.offset - ext_offset + |
| 4507 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 4508 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4509 | |
| 4510 | if (!extent_locked) { |
| 4511 | lock_start = key.offset; |
| 4512 | lock_end = lock_start + num_bytes - 1; |
| 4513 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4514 | if (lock_start > key.offset || |
| 4515 | lock_end + 1 < key.offset + num_bytes) { |
| 4516 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4517 | lock_start, lock_end, GFP_NOFS); |
| 4518 | extent_locked = 0; |
| 4519 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | if (!inode) { |
| 4523 | btrfs_release_path(root, path); |
| 4524 | |
| 4525 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 4526 | key.objectid, root); |
| 4527 | if (inode->i_state & I_NEW) { |
| 4528 | BTRFS_I(inode)->root = root; |
| 4529 | BTRFS_I(inode)->location.objectid = |
| 4530 | key.objectid; |
| 4531 | BTRFS_I(inode)->location.type = |
| 4532 | BTRFS_INODE_ITEM_KEY; |
| 4533 | BTRFS_I(inode)->location.offset = 0; |
| 4534 | btrfs_read_locked_inode(inode); |
| 4535 | unlock_new_inode(inode); |
| 4536 | } |
| 4537 | /* |
| 4538 | * some code call btrfs_commit_transaction while |
| 4539 | * holding the i_mutex, so we can't use mutex_lock |
| 4540 | * here. |
| 4541 | */ |
| 4542 | if (is_bad_inode(inode) || |
| 4543 | !mutex_trylock(&inode->i_mutex)) { |
| 4544 | iput(inode); |
| 4545 | inode = NULL; |
| 4546 | key.offset = (u64)-1; |
| 4547 | goto skip; |
| 4548 | } |
| 4549 | } |
| 4550 | |
| 4551 | if (!extent_locked) { |
| 4552 | struct btrfs_ordered_extent *ordered; |
| 4553 | |
| 4554 | btrfs_release_path(root, path); |
| 4555 | |
| 4556 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4557 | lock_end, GFP_NOFS); |
| 4558 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 4559 | lock_end); |
| 4560 | if (ordered && |
| 4561 | ordered->file_offset <= lock_end && |
| 4562 | ordered->file_offset + ordered->len > lock_start) { |
| 4563 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4564 | lock_start, lock_end, GFP_NOFS); |
| 4565 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 4566 | btrfs_put_ordered_extent(ordered); |
| 4567 | key.offset += num_bytes; |
| 4568 | goto skip; |
| 4569 | } |
| 4570 | if (ordered) |
| 4571 | btrfs_put_ordered_extent(ordered); |
| 4572 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4573 | extent_locked = 1; |
| 4574 | continue; |
| 4575 | } |
| 4576 | |
| 4577 | if (nr_extents == 1) { |
| 4578 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4579 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4580 | new_extents[0].disk_bytenr); |
| 4581 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4582 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4583 | btrfs_mark_buffer_dirty(leaf); |
| 4584 | |
| 4585 | btrfs_drop_extent_cache(inode, key.offset, |
| 4586 | key.offset + num_bytes - 1, 0); |
| 4587 | |
| 4588 | ret = btrfs_inc_extent_ref(trans, root, |
| 4589 | new_extents[0].disk_bytenr, |
| 4590 | new_extents[0].disk_num_bytes, |
| 4591 | leaf->start, |
| 4592 | root->root_key.objectid, |
| 4593 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4594 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4595 | BUG_ON(ret); |
| 4596 | |
| 4597 | ret = btrfs_free_extent(trans, root, |
| 4598 | extent_key->objectid, |
| 4599 | extent_key->offset, |
| 4600 | leaf->start, |
| 4601 | btrfs_header_owner(leaf), |
| 4602 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4603 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4604 | BUG_ON(ret); |
| 4605 | |
| 4606 | btrfs_release_path(root, path); |
| 4607 | key.offset += num_bytes; |
| 4608 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4609 | BUG_ON(1); |
| 4610 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4611 | u64 alloc_hint; |
| 4612 | u64 extent_len; |
| 4613 | int i; |
| 4614 | /* |
| 4615 | * drop old extent pointer at first, then insert the |
| 4616 | * new pointers one bye one |
| 4617 | */ |
| 4618 | btrfs_release_path(root, path); |
| 4619 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 4620 | key.offset + num_bytes, |
| 4621 | key.offset, &alloc_hint); |
| 4622 | BUG_ON(ret); |
| 4623 | |
| 4624 | for (i = 0; i < nr_extents; i++) { |
| 4625 | if (ext_offset >= new_extents[i].num_bytes) { |
| 4626 | ext_offset -= new_extents[i].num_bytes; |
| 4627 | continue; |
| 4628 | } |
| 4629 | extent_len = min(new_extents[i].num_bytes - |
| 4630 | ext_offset, num_bytes); |
| 4631 | |
| 4632 | ret = btrfs_insert_empty_item(trans, root, |
| 4633 | path, &key, |
| 4634 | sizeof(*fi)); |
| 4635 | BUG_ON(ret); |
| 4636 | |
| 4637 | leaf = path->nodes[0]; |
| 4638 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4639 | struct btrfs_file_extent_item); |
| 4640 | btrfs_set_file_extent_generation(leaf, fi, |
| 4641 | trans->transid); |
| 4642 | btrfs_set_file_extent_type(leaf, fi, |
| 4643 | BTRFS_FILE_EXTENT_REG); |
| 4644 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4645 | new_extents[i].disk_bytenr); |
| 4646 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4647 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4648 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 4649 | new_extents[i].ram_bytes); |
| 4650 | |
| 4651 | btrfs_set_file_extent_compression(leaf, fi, |
| 4652 | new_extents[i].compression); |
| 4653 | btrfs_set_file_extent_encryption(leaf, fi, |
| 4654 | new_extents[i].encryption); |
| 4655 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 4656 | new_extents[i].other_encoding); |
| 4657 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4658 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4659 | extent_len); |
| 4660 | ext_offset += new_extents[i].offset; |
| 4661 | btrfs_set_file_extent_offset(leaf, fi, |
| 4662 | ext_offset); |
| 4663 | btrfs_mark_buffer_dirty(leaf); |
| 4664 | |
| 4665 | btrfs_drop_extent_cache(inode, key.offset, |
| 4666 | key.offset + extent_len - 1, 0); |
| 4667 | |
| 4668 | ret = btrfs_inc_extent_ref(trans, root, |
| 4669 | new_extents[i].disk_bytenr, |
| 4670 | new_extents[i].disk_num_bytes, |
| 4671 | leaf->start, |
| 4672 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4673 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4674 | BUG_ON(ret); |
| 4675 | btrfs_release_path(root, path); |
| 4676 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4677 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4678 | |
| 4679 | ext_offset = 0; |
| 4680 | num_bytes -= extent_len; |
| 4681 | key.offset += extent_len; |
| 4682 | |
| 4683 | if (num_bytes == 0) |
| 4684 | break; |
| 4685 | } |
| 4686 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4687 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4691 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4692 | lock_end, GFP_NOFS); |
| 4693 | extent_locked = 0; |
| 4694 | } |
| 4695 | skip: |
| 4696 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4697 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4698 | break; |
| 4699 | |
| 4700 | cond_resched(); |
| 4701 | } |
| 4702 | ret = 0; |
| 4703 | out: |
| 4704 | btrfs_release_path(root, path); |
| 4705 | if (inode) { |
| 4706 | mutex_unlock(&inode->i_mutex); |
| 4707 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4708 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4709 | lock_end, GFP_NOFS); |
| 4710 | } |
| 4711 | iput(inode); |
| 4712 | } |
| 4713 | return ret; |
| 4714 | } |
| 4715 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4716 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 4717 | struct btrfs_root *root, |
| 4718 | struct extent_buffer *buf, u64 orig_start) |
| 4719 | { |
| 4720 | int level; |
| 4721 | int ret; |
| 4722 | |
| 4723 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 4724 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 4725 | |
| 4726 | level = btrfs_header_level(buf); |
| 4727 | if (level == 0) { |
| 4728 | struct btrfs_leaf_ref *ref; |
| 4729 | struct btrfs_leaf_ref *orig_ref; |
| 4730 | |
| 4731 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 4732 | if (!orig_ref) |
| 4733 | return -ENOENT; |
| 4734 | |
| 4735 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 4736 | if (!ref) { |
| 4737 | btrfs_free_leaf_ref(root, orig_ref); |
| 4738 | return -ENOMEM; |
| 4739 | } |
| 4740 | |
| 4741 | ref->nritems = orig_ref->nritems; |
| 4742 | memcpy(ref->extents, orig_ref->extents, |
| 4743 | sizeof(ref->extents[0]) * ref->nritems); |
| 4744 | |
| 4745 | btrfs_free_leaf_ref(root, orig_ref); |
| 4746 | |
| 4747 | ref->root_gen = trans->transid; |
| 4748 | ref->bytenr = buf->start; |
| 4749 | ref->owner = btrfs_header_owner(buf); |
| 4750 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4751 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4752 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 4753 | WARN_ON(ret); |
| 4754 | btrfs_free_leaf_ref(root, ref); |
| 4755 | } |
| 4756 | return 0; |
| 4757 | } |
| 4758 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4759 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4760 | struct extent_buffer *leaf, |
| 4761 | struct btrfs_block_group_cache *group, |
| 4762 | struct btrfs_root *target_root) |
| 4763 | { |
| 4764 | struct btrfs_key key; |
| 4765 | struct inode *inode = NULL; |
| 4766 | struct btrfs_file_extent_item *fi; |
| 4767 | u64 num_bytes; |
| 4768 | u64 skip_objectid = 0; |
| 4769 | u32 nritems; |
| 4770 | u32 i; |
| 4771 | |
| 4772 | nritems = btrfs_header_nritems(leaf); |
| 4773 | for (i = 0; i < nritems; i++) { |
| 4774 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4775 | if (key.objectid == skip_objectid || |
| 4776 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 4777 | continue; |
| 4778 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4779 | if (btrfs_file_extent_type(leaf, fi) == |
| 4780 | BTRFS_FILE_EXTENT_INLINE) |
| 4781 | continue; |
| 4782 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4783 | continue; |
| 4784 | if (!inode || inode->i_ino != key.objectid) { |
| 4785 | iput(inode); |
| 4786 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 4787 | key.objectid, target_root, 1); |
| 4788 | } |
| 4789 | if (!inode) { |
| 4790 | skip_objectid = key.objectid; |
| 4791 | continue; |
| 4792 | } |
| 4793 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4794 | |
| 4795 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4796 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4797 | btrfs_drop_extent_cache(inode, key.offset, |
| 4798 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4799 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4800 | key.offset + num_bytes - 1, GFP_NOFS); |
| 4801 | cond_resched(); |
| 4802 | } |
| 4803 | iput(inode); |
| 4804 | return 0; |
| 4805 | } |
| 4806 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4807 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4808 | struct btrfs_root *root, |
| 4809 | struct extent_buffer *leaf, |
| 4810 | struct btrfs_block_group_cache *group, |
| 4811 | struct inode *reloc_inode) |
| 4812 | { |
| 4813 | struct btrfs_key key; |
| 4814 | struct btrfs_key extent_key; |
| 4815 | struct btrfs_file_extent_item *fi; |
| 4816 | struct btrfs_leaf_ref *ref; |
| 4817 | struct disk_extent *new_extent; |
| 4818 | u64 bytenr; |
| 4819 | u64 num_bytes; |
| 4820 | u32 nritems; |
| 4821 | u32 i; |
| 4822 | int ext_index; |
| 4823 | int nr_extent; |
| 4824 | int ret; |
| 4825 | |
| 4826 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 4827 | BUG_ON(!new_extent); |
| 4828 | |
| 4829 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 4830 | BUG_ON(!ref); |
| 4831 | |
| 4832 | ext_index = -1; |
| 4833 | nritems = btrfs_header_nritems(leaf); |
| 4834 | for (i = 0; i < nritems; i++) { |
| 4835 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4836 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 4837 | continue; |
| 4838 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4839 | if (btrfs_file_extent_type(leaf, fi) == |
| 4840 | BTRFS_FILE_EXTENT_INLINE) |
| 4841 | continue; |
| 4842 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4843 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4844 | if (bytenr == 0) |
| 4845 | continue; |
| 4846 | |
| 4847 | ext_index++; |
| 4848 | if (bytenr >= group->key.objectid + group->key.offset || |
| 4849 | bytenr + num_bytes <= group->key.objectid) |
| 4850 | continue; |
| 4851 | |
| 4852 | extent_key.objectid = bytenr; |
| 4853 | extent_key.offset = num_bytes; |
| 4854 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4855 | nr_extent = 1; |
| 4856 | ret = get_new_locations(reloc_inode, &extent_key, |
| 4857 | group->key.objectid, 1, |
| 4858 | &new_extent, &nr_extent); |
| 4859 | if (ret > 0) |
| 4860 | continue; |
| 4861 | BUG_ON(ret < 0); |
| 4862 | |
| 4863 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 4864 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 4865 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 4866 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 4867 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4868 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4869 | new_extent->disk_bytenr); |
| 4870 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4871 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4872 | btrfs_mark_buffer_dirty(leaf); |
| 4873 | |
| 4874 | ret = btrfs_inc_extent_ref(trans, root, |
| 4875 | new_extent->disk_bytenr, |
| 4876 | new_extent->disk_num_bytes, |
| 4877 | leaf->start, |
| 4878 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4879 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4880 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4881 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4882 | ret = btrfs_free_extent(trans, root, |
| 4883 | bytenr, num_bytes, leaf->start, |
| 4884 | btrfs_header_owner(leaf), |
| 4885 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4886 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4887 | BUG_ON(ret); |
| 4888 | cond_resched(); |
| 4889 | } |
| 4890 | kfree(new_extent); |
| 4891 | BUG_ON(ext_index + 1 != ref->nritems); |
| 4892 | btrfs_free_leaf_ref(root, ref); |
| 4893 | return 0; |
| 4894 | } |
| 4895 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4896 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 4897 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4898 | { |
| 4899 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4900 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4901 | |
| 4902 | if (root->reloc_root) { |
| 4903 | reloc_root = root->reloc_root; |
| 4904 | root->reloc_root = NULL; |
| 4905 | list_add(&reloc_root->dead_list, |
| 4906 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4907 | |
| 4908 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 4909 | reloc_root->node->start); |
| 4910 | btrfs_set_root_level(&root->root_item, |
| 4911 | btrfs_header_level(reloc_root->node)); |
| 4912 | memset(&reloc_root->root_item.drop_progress, 0, |
| 4913 | sizeof(struct btrfs_disk_key)); |
| 4914 | reloc_root->root_item.drop_level = 0; |
| 4915 | |
| 4916 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 4917 | &reloc_root->root_key, |
| 4918 | &reloc_root->root_item); |
| 4919 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4920 | } |
| 4921 | return 0; |
| 4922 | } |
| 4923 | |
| 4924 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 4925 | { |
| 4926 | struct btrfs_trans_handle *trans; |
| 4927 | struct btrfs_root *reloc_root; |
| 4928 | struct btrfs_root *prev_root = NULL; |
| 4929 | struct list_head dead_roots; |
| 4930 | int ret; |
| 4931 | unsigned long nr; |
| 4932 | |
| 4933 | INIT_LIST_HEAD(&dead_roots); |
| 4934 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 4935 | |
| 4936 | while (!list_empty(&dead_roots)) { |
| 4937 | reloc_root = list_entry(dead_roots.prev, |
| 4938 | struct btrfs_root, dead_list); |
| 4939 | list_del_init(&reloc_root->dead_list); |
| 4940 | |
| 4941 | BUG_ON(reloc_root->commit_root != NULL); |
| 4942 | while (1) { |
| 4943 | trans = btrfs_join_transaction(root, 1); |
| 4944 | BUG_ON(!trans); |
| 4945 | |
| 4946 | mutex_lock(&root->fs_info->drop_mutex); |
| 4947 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 4948 | if (ret != -EAGAIN) |
| 4949 | break; |
| 4950 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4951 | |
| 4952 | nr = trans->blocks_used; |
| 4953 | ret = btrfs_end_transaction(trans, root); |
| 4954 | BUG_ON(ret); |
| 4955 | btrfs_btree_balance_dirty(root, nr); |
| 4956 | } |
| 4957 | |
| 4958 | free_extent_buffer(reloc_root->node); |
| 4959 | |
| 4960 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 4961 | &reloc_root->root_key); |
| 4962 | BUG_ON(ret); |
| 4963 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4964 | |
| 4965 | nr = trans->blocks_used; |
| 4966 | ret = btrfs_end_transaction(trans, root); |
| 4967 | BUG_ON(ret); |
| 4968 | btrfs_btree_balance_dirty(root, nr); |
| 4969 | |
| 4970 | kfree(prev_root); |
| 4971 | prev_root = reloc_root; |
| 4972 | } |
| 4973 | if (prev_root) { |
| 4974 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 4975 | kfree(prev_root); |
| 4976 | } |
| 4977 | return 0; |
| 4978 | } |
| 4979 | |
| 4980 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 4981 | { |
| 4982 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 4983 | return 0; |
| 4984 | } |
| 4985 | |
| 4986 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 4987 | { |
| 4988 | struct btrfs_root *reloc_root; |
| 4989 | struct btrfs_trans_handle *trans; |
| 4990 | struct btrfs_key location; |
| 4991 | int found; |
| 4992 | int ret; |
| 4993 | |
| 4994 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 4995 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 4996 | BUG_ON(ret); |
| 4997 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 4998 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 4999 | |
| 5000 | if (found) { |
| 5001 | trans = btrfs_start_transaction(root, 1); |
| 5002 | BUG_ON(!trans); |
| 5003 | ret = btrfs_commit_transaction(trans, root); |
| 5004 | BUG_ON(ret); |
| 5005 | } |
| 5006 | |
| 5007 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5008 | location.offset = (u64)-1; |
| 5009 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 5010 | |
| 5011 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 5012 | BUG_ON(!reloc_root); |
| 5013 | btrfs_orphan_cleanup(reloc_root); |
| 5014 | return 0; |
| 5015 | } |
| 5016 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5017 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5018 | struct btrfs_root *root) |
| 5019 | { |
| 5020 | struct btrfs_root *reloc_root; |
| 5021 | struct extent_buffer *eb; |
| 5022 | struct btrfs_root_item *root_item; |
| 5023 | struct btrfs_key root_key; |
| 5024 | int ret; |
| 5025 | |
| 5026 | BUG_ON(!root->ref_cows); |
| 5027 | if (root->reloc_root) |
| 5028 | return 0; |
| 5029 | |
| 5030 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 5031 | BUG_ON(!root_item); |
| 5032 | |
| 5033 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 5034 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 5035 | BUG_ON(ret); |
| 5036 | |
| 5037 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 5038 | root_key.offset = root->root_key.objectid; |
| 5039 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5040 | |
| 5041 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 5042 | btrfs_set_root_refs(root_item, 0); |
| 5043 | btrfs_set_root_bytenr(root_item, eb->start); |
| 5044 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5045 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5046 | |
| 5047 | btrfs_tree_unlock(eb); |
| 5048 | free_extent_buffer(eb); |
| 5049 | |
| 5050 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 5051 | &root_key, root_item); |
| 5052 | BUG_ON(ret); |
| 5053 | kfree(root_item); |
| 5054 | |
| 5055 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 5056 | &root_key); |
| 5057 | BUG_ON(!reloc_root); |
| 5058 | reloc_root->last_trans = trans->transid; |
| 5059 | reloc_root->commit_root = NULL; |
| 5060 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 5061 | |
| 5062 | root->reloc_root = reloc_root; |
| 5063 | return 0; |
| 5064 | } |
| 5065 | |
| 5066 | /* |
| 5067 | * Core function of space balance. |
| 5068 | * |
| 5069 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5070 | * counted roots. There is one reloc tree for each subvol, and all |
| 5071 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 5072 | * of the latest committed roots of subvols (root->commit_root). |
| 5073 | * |
| 5074 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 5075 | * COW the block through subvol's reloc tree, then update block pointer |
| 5076 | * in the subvol to point to the new block. Since all reloc trees share |
| 5077 | * same root key objectid, doing special handing for tree blocks owned |
| 5078 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 5079 | * we can use the resulting new block directly when the same block is |
| 5080 | * required to COW again through other reloc trees. By this way, relocated |
| 5081 | * tree blocks are shared between reloc trees, so they are also shared |
| 5082 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5083 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5084 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5085 | struct btrfs_root *root, |
| 5086 | struct btrfs_path *path, |
| 5087 | struct btrfs_key *first_key, |
| 5088 | struct btrfs_ref_path *ref_path, |
| 5089 | struct btrfs_block_group_cache *group, |
| 5090 | struct inode *reloc_inode) |
| 5091 | { |
| 5092 | struct btrfs_root *reloc_root; |
| 5093 | struct extent_buffer *eb = NULL; |
| 5094 | struct btrfs_key *keys; |
| 5095 | u64 *nodes; |
| 5096 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5097 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5098 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5099 | int ret; |
| 5100 | |
| 5101 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 5102 | lowest_level = ref_path->owner_objectid; |
| 5103 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5104 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5105 | path->lowest_level = lowest_level; |
| 5106 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 5107 | BUG_ON(ret < 0); |
| 5108 | path->lowest_level = 0; |
| 5109 | btrfs_release_path(root, path); |
| 5110 | return 0; |
| 5111 | } |
| 5112 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5113 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5114 | ret = init_reloc_tree(trans, root); |
| 5115 | BUG_ON(ret); |
| 5116 | reloc_root = root->reloc_root; |
| 5117 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5118 | shared_level = ref_path->shared_level; |
| 5119 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5120 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5121 | keys = ref_path->node_keys; |
| 5122 | nodes = ref_path->new_nodes; |
| 5123 | memset(&keys[shared_level + 1], 0, |
| 5124 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 5125 | memset(&nodes[shared_level + 1], 0, |
| 5126 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5127 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5128 | if (nodes[lowest_level] == 0) { |
| 5129 | path->lowest_level = lowest_level; |
| 5130 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5131 | 0, 1); |
| 5132 | BUG_ON(ret); |
| 5133 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 5134 | eb = path->nodes[level]; |
| 5135 | if (!eb || eb == reloc_root->node) |
| 5136 | break; |
| 5137 | nodes[level] = eb->start; |
| 5138 | if (level == 0) |
| 5139 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 5140 | else |
| 5141 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 5142 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5143 | if (nodes[0] && |
| 5144 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5145 | eb = path->nodes[0]; |
| 5146 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 5147 | group, reloc_inode); |
| 5148 | BUG_ON(ret); |
| 5149 | } |
| 5150 | btrfs_release_path(reloc_root, path); |
| 5151 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5152 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5153 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5154 | BUG_ON(ret); |
| 5155 | } |
| 5156 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5157 | /* |
| 5158 | * replace tree blocks in the fs tree with tree blocks in |
| 5159 | * the reloc tree. |
| 5160 | */ |
| 5161 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 5162 | BUG_ON(ret < 0); |
| 5163 | |
| 5164 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5165 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5166 | 0, 0); |
| 5167 | BUG_ON(ret); |
| 5168 | extent_buffer_get(path->nodes[0]); |
| 5169 | eb = path->nodes[0]; |
| 5170 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5171 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 5172 | BUG_ON(ret); |
| 5173 | free_extent_buffer(eb); |
| 5174 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5175 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5176 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5177 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5178 | return 0; |
| 5179 | } |
| 5180 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5181 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5182 | struct btrfs_root *root, |
| 5183 | struct btrfs_path *path, |
| 5184 | struct btrfs_key *first_key, |
| 5185 | struct btrfs_ref_path *ref_path) |
| 5186 | { |
| 5187 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5188 | |
| 5189 | ret = relocate_one_path(trans, root, path, first_key, |
| 5190 | ref_path, NULL, NULL); |
| 5191 | BUG_ON(ret); |
| 5192 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5193 | return 0; |
| 5194 | } |
| 5195 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5196 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5197 | struct btrfs_root *extent_root, |
| 5198 | struct btrfs_path *path, |
| 5199 | struct btrfs_key *extent_key) |
| 5200 | { |
| 5201 | int ret; |
| 5202 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5203 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 5204 | if (ret) |
| 5205 | goto out; |
| 5206 | ret = btrfs_del_item(trans, extent_root, path); |
| 5207 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5208 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5209 | return ret; |
| 5210 | } |
| 5211 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5212 | 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] | 5213 | struct btrfs_ref_path *ref_path) |
| 5214 | { |
| 5215 | struct btrfs_key root_key; |
| 5216 | |
| 5217 | root_key.objectid = ref_path->root_objectid; |
| 5218 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5219 | if (is_cowonly_root(ref_path->root_objectid)) |
| 5220 | root_key.offset = 0; |
| 5221 | else |
| 5222 | root_key.offset = (u64)-1; |
| 5223 | |
| 5224 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5225 | } |
| 5226 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5227 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5228 | struct btrfs_path *path, |
| 5229 | struct btrfs_key *extent_key, |
| 5230 | struct btrfs_block_group_cache *group, |
| 5231 | struct inode *reloc_inode, int pass) |
| 5232 | { |
| 5233 | struct btrfs_trans_handle *trans; |
| 5234 | struct btrfs_root *found_root; |
| 5235 | struct btrfs_ref_path *ref_path = NULL; |
| 5236 | struct disk_extent *new_extents = NULL; |
| 5237 | int nr_extents = 0; |
| 5238 | int loops; |
| 5239 | int ret; |
| 5240 | int level; |
| 5241 | struct btrfs_key first_key; |
| 5242 | u64 prev_block = 0; |
| 5243 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5244 | |
| 5245 | trans = btrfs_start_transaction(extent_root, 1); |
| 5246 | BUG_ON(!trans); |
| 5247 | |
| 5248 | if (extent_key->objectid == 0) { |
| 5249 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 5250 | goto out; |
| 5251 | } |
| 5252 | |
| 5253 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 5254 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5255 | ret = -ENOMEM; |
| 5256 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5257 | } |
| 5258 | |
| 5259 | for (loops = 0; ; loops++) { |
| 5260 | if (loops == 0) { |
| 5261 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 5262 | extent_key->objectid); |
| 5263 | } else { |
| 5264 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 5265 | } |
| 5266 | if (ret < 0) |
| 5267 | goto out; |
| 5268 | if (ret > 0) |
| 5269 | break; |
| 5270 | |
| 5271 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5272 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 5273 | continue; |
| 5274 | |
| 5275 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 5276 | BUG_ON(!found_root); |
| 5277 | /* |
| 5278 | * for reference counted tree, only process reference paths |
| 5279 | * rooted at the latest committed root. |
| 5280 | */ |
| 5281 | if (found_root->ref_cows && |
| 5282 | ref_path->root_generation != found_root->root_key.offset) |
| 5283 | continue; |
| 5284 | |
| 5285 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5286 | if (pass == 0) { |
| 5287 | /* |
| 5288 | * copy data extents to new locations |
| 5289 | */ |
| 5290 | u64 group_start = group->key.objectid; |
| 5291 | ret = relocate_data_extent(reloc_inode, |
| 5292 | extent_key, |
| 5293 | group_start); |
| 5294 | if (ret < 0) |
| 5295 | goto out; |
| 5296 | break; |
| 5297 | } |
| 5298 | level = 0; |
| 5299 | } else { |
| 5300 | level = ref_path->owner_objectid; |
| 5301 | } |
| 5302 | |
| 5303 | if (prev_block != ref_path->nodes[level]) { |
| 5304 | struct extent_buffer *eb; |
| 5305 | u64 block_start = ref_path->nodes[level]; |
| 5306 | u64 block_size = btrfs_level_size(found_root, level); |
| 5307 | |
| 5308 | eb = read_tree_block(found_root, block_start, |
| 5309 | block_size, 0); |
| 5310 | btrfs_tree_lock(eb); |
| 5311 | BUG_ON(level != btrfs_header_level(eb)); |
| 5312 | |
| 5313 | if (level == 0) |
| 5314 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 5315 | else |
| 5316 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 5317 | |
| 5318 | btrfs_tree_unlock(eb); |
| 5319 | free_extent_buffer(eb); |
| 5320 | prev_block = block_start; |
| 5321 | } |
| 5322 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 5323 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5324 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 5325 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5326 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5327 | /* |
| 5328 | * try to update data extent references while |
| 5329 | * keeping metadata shared between snapshots. |
| 5330 | */ |
| 5331 | if (pass == 1) { |
| 5332 | ret = relocate_one_path(trans, found_root, |
| 5333 | path, &first_key, ref_path, |
| 5334 | group, reloc_inode); |
| 5335 | if (ret < 0) |
| 5336 | goto out; |
| 5337 | continue; |
| 5338 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5339 | /* |
| 5340 | * use fallback method to process the remaining |
| 5341 | * references. |
| 5342 | */ |
| 5343 | if (!new_extents) { |
| 5344 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5345 | new_extents = kmalloc(sizeof(*new_extents), |
| 5346 | GFP_NOFS); |
| 5347 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5348 | ret = get_new_locations(reloc_inode, |
| 5349 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5350 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5351 | &new_extents, |
| 5352 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5353 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5354 | goto out; |
| 5355 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5356 | ret = replace_one_extent(trans, found_root, |
| 5357 | path, extent_key, |
| 5358 | &first_key, ref_path, |
| 5359 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5360 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5361 | ret = relocate_tree_block(trans, found_root, path, |
| 5362 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5363 | } |
| 5364 | if (ret < 0) |
| 5365 | goto out; |
| 5366 | } |
| 5367 | ret = 0; |
| 5368 | out: |
| 5369 | btrfs_end_transaction(trans, extent_root); |
| 5370 | kfree(new_extents); |
| 5371 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5372 | return ret; |
| 5373 | } |
| 5374 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5375 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 5376 | { |
| 5377 | u64 num_devices; |
| 5378 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 5379 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 5380 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5381 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5382 | if (num_devices == 1) { |
| 5383 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5384 | stripped = flags & ~stripped; |
| 5385 | |
| 5386 | /* turn raid0 into single device chunks */ |
| 5387 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 5388 | return stripped; |
| 5389 | |
| 5390 | /* turn mirroring into duplication */ |
| 5391 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5392 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5393 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 5394 | return flags; |
| 5395 | } else { |
| 5396 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5397 | if (flags & stripped) |
| 5398 | return flags; |
| 5399 | |
| 5400 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5401 | stripped = flags & ~stripped; |
| 5402 | |
| 5403 | /* switch duplicated blocks with raid1 */ |
| 5404 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 5405 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 5406 | |
| 5407 | /* turn single device chunks into raid0 */ |
| 5408 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 5409 | } |
| 5410 | return flags; |
| 5411 | } |
| 5412 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 5413 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5414 | struct btrfs_block_group_cache *shrink_block_group, |
| 5415 | int force) |
| 5416 | { |
| 5417 | struct btrfs_trans_handle *trans; |
| 5418 | u64 new_alloc_flags; |
| 5419 | u64 calc; |
| 5420 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5421 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5422 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5423 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5424 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5425 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5426 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5427 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5428 | new_alloc_flags = update_block_group_flags(root, |
| 5429 | shrink_block_group->flags); |
| 5430 | if (new_alloc_flags != shrink_block_group->flags) { |
| 5431 | calc = |
| 5432 | btrfs_block_group_used(&shrink_block_group->item); |
| 5433 | } else { |
| 5434 | calc = shrink_block_group->key.offset; |
| 5435 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5436 | spin_unlock(&shrink_block_group->lock); |
| 5437 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5438 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5439 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5440 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5441 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5442 | } else |
| 5443 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5444 | return 0; |
| 5445 | } |
| 5446 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5447 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 5448 | struct btrfs_root *root, |
| 5449 | u64 objectid, u64 size) |
| 5450 | { |
| 5451 | struct btrfs_path *path; |
| 5452 | struct btrfs_inode_item *item; |
| 5453 | struct extent_buffer *leaf; |
| 5454 | int ret; |
| 5455 | |
| 5456 | path = btrfs_alloc_path(); |
| 5457 | if (!path) |
| 5458 | return -ENOMEM; |
| 5459 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5460 | path->leave_spinning = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5461 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 5462 | if (ret) |
| 5463 | goto out; |
| 5464 | |
| 5465 | leaf = path->nodes[0]; |
| 5466 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 5467 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 5468 | btrfs_set_inode_generation(leaf, item, 1); |
| 5469 | btrfs_set_inode_size(leaf, item, size); |
| 5470 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 5471 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5472 | btrfs_mark_buffer_dirty(leaf); |
| 5473 | btrfs_release_path(root, path); |
| 5474 | out: |
| 5475 | btrfs_free_path(path); |
| 5476 | return ret; |
| 5477 | } |
| 5478 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5479 | 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] | 5480 | struct btrfs_block_group_cache *group) |
| 5481 | { |
| 5482 | struct inode *inode = NULL; |
| 5483 | struct btrfs_trans_handle *trans; |
| 5484 | struct btrfs_root *root; |
| 5485 | struct btrfs_key root_key; |
| 5486 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 5487 | int err = 0; |
| 5488 | |
| 5489 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5490 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5491 | root_key.offset = (u64)-1; |
| 5492 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5493 | if (IS_ERR(root)) |
| 5494 | return ERR_CAST(root); |
| 5495 | |
| 5496 | trans = btrfs_start_transaction(root, 1); |
| 5497 | BUG_ON(!trans); |
| 5498 | |
| 5499 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 5500 | if (err) |
| 5501 | goto out; |
| 5502 | |
| 5503 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 5504 | BUG_ON(err); |
| 5505 | |
| 5506 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5507 | group->key.offset, 0, group->key.offset, |
| 5508 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5509 | BUG_ON(err); |
| 5510 | |
| 5511 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 5512 | if (inode->i_state & I_NEW) { |
| 5513 | BTRFS_I(inode)->root = root; |
| 5514 | BTRFS_I(inode)->location.objectid = objectid; |
| 5515 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5516 | BTRFS_I(inode)->location.offset = 0; |
| 5517 | btrfs_read_locked_inode(inode); |
| 5518 | unlock_new_inode(inode); |
| 5519 | BUG_ON(is_bad_inode(inode)); |
| 5520 | } else { |
| 5521 | BUG_ON(1); |
| 5522 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 5523 | BTRFS_I(inode)->index_cnt = group->key.objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5524 | |
| 5525 | err = btrfs_orphan_add(trans, inode); |
| 5526 | out: |
| 5527 | btrfs_end_transaction(trans, root); |
| 5528 | if (err) { |
| 5529 | if (inode) |
| 5530 | iput(inode); |
| 5531 | inode = ERR_PTR(err); |
| 5532 | } |
| 5533 | return inode; |
| 5534 | } |
| 5535 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 5536 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) |
| 5537 | { |
| 5538 | |
| 5539 | struct btrfs_ordered_sum *sums; |
| 5540 | struct btrfs_sector_sum *sector_sum; |
| 5541 | struct btrfs_ordered_extent *ordered; |
| 5542 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5543 | struct list_head list; |
| 5544 | size_t offset; |
| 5545 | int ret; |
| 5546 | u64 disk_bytenr; |
| 5547 | |
| 5548 | INIT_LIST_HEAD(&list); |
| 5549 | |
| 5550 | ordered = btrfs_lookup_ordered_extent(inode, file_pos); |
| 5551 | BUG_ON(ordered->file_offset != file_pos || ordered->len != len); |
| 5552 | |
| 5553 | disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5554 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 5555 | disk_bytenr + len - 1, &list); |
| 5556 | |
| 5557 | while (!list_empty(&list)) { |
| 5558 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 5559 | list_del_init(&sums->list); |
| 5560 | |
| 5561 | sector_sum = sums->sums; |
| 5562 | sums->bytenr = ordered->start; |
| 5563 | |
| 5564 | offset = 0; |
| 5565 | while (offset < sums->len) { |
| 5566 | sector_sum->bytenr += ordered->start - disk_bytenr; |
| 5567 | sector_sum++; |
| 5568 | offset += root->sectorsize; |
| 5569 | } |
| 5570 | |
| 5571 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 5572 | } |
| 5573 | btrfs_put_ordered_extent(ordered); |
| 5574 | return 0; |
| 5575 | } |
| 5576 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5577 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5578 | { |
| 5579 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5580 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5581 | struct btrfs_fs_info *info = root->fs_info; |
| 5582 | struct extent_buffer *leaf; |
| 5583 | struct inode *reloc_inode; |
| 5584 | struct btrfs_block_group_cache *block_group; |
| 5585 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5586 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5587 | u64 cur_byte; |
| 5588 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5589 | u32 nritems; |
| 5590 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5591 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5592 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5593 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5594 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5595 | |
| 5596 | block_group = btrfs_lookup_block_group(info, group_start); |
| 5597 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5598 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5599 | printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5600 | (unsigned long long)block_group->key.objectid, |
| 5601 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5602 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5603 | path = btrfs_alloc_path(); |
| 5604 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5605 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5606 | reloc_inode = create_reloc_inode(info, block_group); |
| 5607 | BUG_ON(IS_ERR(reloc_inode)); |
| 5608 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5609 | __alloc_chunk_for_shrink(root, block_group, 1); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5610 | set_block_group_readonly(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5611 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5612 | btrfs_start_delalloc_inodes(info->tree_root); |
| 5613 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5614 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5615 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5616 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5617 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5618 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5619 | key.offset = 0; |
| 5620 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5621 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5622 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5623 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5624 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5625 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5626 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 5627 | btrfs_clean_old_snapshots(info->tree_root); |
| 5628 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 5629 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5630 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5631 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5632 | btrfs_commit_transaction(trans, info->tree_root); |
| 5633 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5634 | while (1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5635 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5636 | if (ret < 0) |
| 5637 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5638 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5639 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5640 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5641 | if (path->slots[0] >= nritems) { |
| 5642 | ret = btrfs_next_leaf(root, path); |
| 5643 | if (ret < 0) |
| 5644 | goto out; |
| 5645 | if (ret == 1) { |
| 5646 | ret = 0; |
| 5647 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5648 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5649 | leaf = path->nodes[0]; |
| 5650 | nritems = btrfs_header_nritems(leaf); |
| 5651 | } |
| 5652 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5653 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5654 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5655 | if (key.objectid >= block_group->key.objectid + |
| 5656 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5657 | break; |
| 5658 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5659 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5660 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5661 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5662 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5663 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5664 | } |
| 5665 | progress = 1; |
| 5666 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5667 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 5668 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5669 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5670 | goto next; |
| 5671 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5672 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5673 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5674 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5675 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5676 | |
| 5677 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 5678 | ret = relocate_one_extent(root, path, &key, block_group, |
| 5679 | reloc_inode, pass); |
| 5680 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5681 | if (ret > 0) |
| 5682 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5683 | |
| 5684 | key.objectid = cur_byte; |
| 5685 | key.type = 0; |
| 5686 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5687 | } |
| 5688 | |
| 5689 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5690 | |
| 5691 | if (pass == 0) { |
| 5692 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 5693 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5694 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5695 | |
| 5696 | if (total_found > 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5697 | printk(KERN_INFO "btrfs found %llu extents in pass %d\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5698 | (unsigned long long)total_found, pass); |
| 5699 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5700 | if (total_found == skipped && pass > 2) { |
| 5701 | iput(reloc_inode); |
| 5702 | reloc_inode = create_reloc_inode(info, block_group); |
| 5703 | pass = 0; |
| 5704 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5705 | goto again; |
| 5706 | } |
| 5707 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5708 | /* delete reloc_inode */ |
| 5709 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5710 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5711 | /* unpin extents in this range */ |
| 5712 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5713 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5714 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5715 | spin_lock(&block_group->lock); |
| 5716 | WARN_ON(block_group->pinned > 0); |
| 5717 | WARN_ON(block_group->reserved > 0); |
| 5718 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 5719 | spin_unlock(&block_group->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 5720 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5721 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5722 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5723 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5724 | return ret; |
| 5725 | } |
| 5726 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 5727 | static int find_first_block_group(struct btrfs_root *root, |
| 5728 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5729 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5730 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5731 | struct btrfs_key found_key; |
| 5732 | struct extent_buffer *leaf; |
| 5733 | int slot; |
| 5734 | |
| 5735 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 5736 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5737 | goto out; |
| 5738 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5739 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5740 | slot = path->slots[0]; |
| 5741 | leaf = path->nodes[0]; |
| 5742 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5743 | ret = btrfs_next_leaf(root, path); |
| 5744 | if (ret == 0) |
| 5745 | continue; |
| 5746 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5747 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5748 | break; |
| 5749 | } |
| 5750 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5751 | |
| 5752 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5753 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 5754 | ret = 0; |
| 5755 | goto out; |
| 5756 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5757 | path->slots[0]++; |
| 5758 | } |
| 5759 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5760 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5761 | return ret; |
| 5762 | } |
| 5763 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5764 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 5765 | { |
| 5766 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 5767 | struct btrfs_space_info *space_info; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5768 | struct rb_node *n; |
| 5769 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5770 | spin_lock(&info->block_group_cache_lock); |
| 5771 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 5772 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 5773 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5774 | rb_erase(&block_group->cache_node, |
| 5775 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5776 | spin_unlock(&info->block_group_cache_lock); |
| 5777 | |
| 5778 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5779 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5780 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5781 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5782 | |
| 5783 | WARN_ON(atomic_read(&block_group->count) != 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5784 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5785 | |
| 5786 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5787 | } |
| 5788 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 5789 | |
| 5790 | /* now that all the block groups are freed, go through and |
| 5791 | * free all the space_info structs. This is only called during |
| 5792 | * the final stages of unmount, and so we know nobody is |
| 5793 | * using them. We call synchronize_rcu() once before we start, |
| 5794 | * just to be on the safe side. |
| 5795 | */ |
| 5796 | synchronize_rcu(); |
| 5797 | |
| 5798 | while(!list_empty(&info->space_info)) { |
| 5799 | space_info = list_entry(info->space_info.next, |
| 5800 | struct btrfs_space_info, |
| 5801 | list); |
| 5802 | |
| 5803 | list_del(&space_info->list); |
| 5804 | kfree(space_info); |
| 5805 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5806 | return 0; |
| 5807 | } |
| 5808 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5809 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 5810 | { |
| 5811 | struct btrfs_path *path; |
| 5812 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5813 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5814 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5815 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5816 | struct btrfs_key key; |
| 5817 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5818 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5819 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5820 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5821 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5822 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5823 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5824 | path = btrfs_alloc_path(); |
| 5825 | if (!path) |
| 5826 | return -ENOMEM; |
| 5827 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5828 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5829 | ret = find_first_block_group(root, path, &key); |
| 5830 | if (ret > 0) { |
| 5831 | ret = 0; |
| 5832 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5833 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5834 | if (ret != 0) |
| 5835 | goto error; |
| 5836 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5837 | leaf = path->nodes[0]; |
| 5838 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5839 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5840 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5841 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5842 | break; |
| 5843 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5844 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5845 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5846 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5847 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5848 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5849 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 5850 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5851 | read_extent_buffer(leaf, &cache->item, |
| 5852 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 5853 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5854 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5855 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5856 | key.objectid = found_key.objectid + found_key.offset; |
| 5857 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5858 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5859 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5860 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 5861 | btrfs_block_group_used(&cache->item), |
| 5862 | &space_info); |
| 5863 | BUG_ON(ret); |
| 5864 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5865 | down_write(&space_info->groups_sem); |
| 5866 | list_add_tail(&cache->list, &space_info->block_groups); |
| 5867 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5868 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5869 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5870 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 5871 | |
| 5872 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5873 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 5874 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5875 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5876 | ret = 0; |
| 5877 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5878 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5879 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5880 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5881 | |
| 5882 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 5883 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5884 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5885 | u64 size) |
| 5886 | { |
| 5887 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5888 | struct btrfs_root *extent_root; |
| 5889 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5890 | |
| 5891 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5892 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 5893 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5894 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5895 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5896 | if (!cache) |
| 5897 | return -ENOMEM; |
| 5898 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5899 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5900 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5901 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 5902 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5903 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5904 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5905 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5906 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 5907 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5908 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5909 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5910 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 5911 | cache->flags = type; |
| 5912 | btrfs_set_block_group_flags(&cache->item, type); |
| 5913 | |
| 5914 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 5915 | &cache->space_info); |
| 5916 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5917 | down_write(&cache->space_info->groups_sem); |
| 5918 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 5919 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5920 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5921 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5922 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5923 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5924 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 5925 | sizeof(cache->item)); |
| 5926 | BUG_ON(ret); |
| 5927 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 5928 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5929 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5930 | return 0; |
| 5931 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5932 | |
| 5933 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 5934 | struct btrfs_root *root, u64 group_start) |
| 5935 | { |
| 5936 | struct btrfs_path *path; |
| 5937 | struct btrfs_block_group_cache *block_group; |
| 5938 | struct btrfs_key key; |
| 5939 | int ret; |
| 5940 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5941 | root = root->fs_info->extent_root; |
| 5942 | |
| 5943 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 5944 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5945 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5946 | |
| 5947 | memcpy(&key, &block_group->key, sizeof(key)); |
| 5948 | |
| 5949 | path = btrfs_alloc_path(); |
| 5950 | BUG_ON(!path); |
| 5951 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5952 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5953 | rb_erase(&block_group->cache_node, |
| 5954 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5955 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 5956 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5957 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5958 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5959 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5960 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5961 | spin_lock(&block_group->space_info->lock); |
| 5962 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 5963 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 5964 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5965 | block_group->space_info->full = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5966 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame^] | 5967 | btrfs_put_block_group(block_group); |
| 5968 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5969 | |
| 5970 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 5971 | if (ret > 0) |
| 5972 | ret = -EIO; |
| 5973 | if (ret < 0) |
| 5974 | goto out; |
| 5975 | |
| 5976 | ret = btrfs_del_item(trans, root, path); |
| 5977 | out: |
| 5978 | btrfs_free_path(path); |
| 5979 | return ret; |
| 5980 | } |