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 | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 34 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 35 | #define PENDING_EXTENT_INSERT 0 |
| 36 | #define PENDING_EXTENT_DELETE 1 |
| 37 | #define PENDING_BACKREF_UPDATE 2 |
| 38 | |
| 39 | struct pending_extent_op { |
| 40 | int type; |
| 41 | u64 bytenr; |
| 42 | u64 num_bytes; |
| 43 | u64 parent; |
| 44 | u64 orig_parent; |
| 45 | u64 generation; |
| 46 | u64 orig_generation; |
| 47 | int level; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 48 | struct list_head list; |
| 49 | int del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 52 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
| 53 | struct btrfs_root *root, u64 parent, |
| 54 | u64 root_objectid, u64 ref_generation, |
| 55 | u64 owner, struct btrfs_key *ins, |
| 56 | int ref_mod); |
| 57 | static int update_reserved_extents(struct btrfs_root *root, |
| 58 | u64 bytenr, u64 num, int reserve); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 59 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 60 | struct btrfs_root *root, |
| 61 | u64 bytenr, u64 num_bytes, int alloc, |
| 62 | int mark_free); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 63 | static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 64 | struct btrfs_root *root, |
| 65 | u64 bytenr, u64 num_bytes, u64 parent, |
| 66 | u64 root_objectid, u64 ref_generation, |
| 67 | u64 owner_objectid, int pin, |
| 68 | int ref_to_drop); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 69 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 70 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 71 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 72 | u64 flags, int force); |
| 73 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 74 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 75 | { |
| 76 | return (cache->flags & bits) == bits; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * this adds the block group to the fs_info rb tree for the block group |
| 81 | * cache |
| 82 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 83 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 84 | struct btrfs_block_group_cache *block_group) |
| 85 | { |
| 86 | struct rb_node **p; |
| 87 | struct rb_node *parent = NULL; |
| 88 | struct btrfs_block_group_cache *cache; |
| 89 | |
| 90 | spin_lock(&info->block_group_cache_lock); |
| 91 | p = &info->block_group_cache_tree.rb_node; |
| 92 | |
| 93 | while (*p) { |
| 94 | parent = *p; |
| 95 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 96 | cache_node); |
| 97 | if (block_group->key.objectid < cache->key.objectid) { |
| 98 | p = &(*p)->rb_left; |
| 99 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 100 | p = &(*p)->rb_right; |
| 101 | } else { |
| 102 | spin_unlock(&info->block_group_cache_lock); |
| 103 | return -EEXIST; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | rb_link_node(&block_group->cache_node, parent, p); |
| 108 | rb_insert_color(&block_group->cache_node, |
| 109 | &info->block_group_cache_tree); |
| 110 | spin_unlock(&info->block_group_cache_lock); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * This will return the block group at or after bytenr if contains is 0, else |
| 117 | * it will return the block group that contains the bytenr |
| 118 | */ |
| 119 | static struct btrfs_block_group_cache * |
| 120 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 121 | int contains) |
| 122 | { |
| 123 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 124 | struct rb_node *n; |
| 125 | u64 end, start; |
| 126 | |
| 127 | spin_lock(&info->block_group_cache_lock); |
| 128 | n = info->block_group_cache_tree.rb_node; |
| 129 | |
| 130 | while (n) { |
| 131 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 132 | cache_node); |
| 133 | end = cache->key.objectid + cache->key.offset - 1; |
| 134 | start = cache->key.objectid; |
| 135 | |
| 136 | if (bytenr < start) { |
| 137 | if (!contains && (!ret || start < ret->key.objectid)) |
| 138 | ret = cache; |
| 139 | n = n->rb_left; |
| 140 | } else if (bytenr > start) { |
| 141 | if (contains && bytenr <= end) { |
| 142 | ret = cache; |
| 143 | break; |
| 144 | } |
| 145 | n = n->rb_right; |
| 146 | } else { |
| 147 | ret = cache; |
| 148 | break; |
| 149 | } |
| 150 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 151 | if (ret) |
| 152 | atomic_inc(&ret->count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 153 | spin_unlock(&info->block_group_cache_lock); |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * this is only called by cache_block_group, since we could have freed extents |
| 160 | * we need to check the pinned_extents for any extents that can't be used yet |
| 161 | * since their free space will be released as soon as the transaction commits. |
| 162 | */ |
| 163 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 164 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 165 | { |
| 166 | u64 extent_start, extent_end, size; |
| 167 | int ret; |
| 168 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 169 | mutex_lock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 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 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 195 | mutex_unlock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 200 | static int remove_sb_from_cache(struct btrfs_root *root, |
| 201 | struct btrfs_block_group_cache *cache) |
| 202 | { |
| 203 | u64 bytenr; |
| 204 | u64 *logical; |
| 205 | int stripe_len; |
| 206 | int i, nr, ret; |
| 207 | |
| 208 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 209 | bytenr = btrfs_sb_offset(i); |
| 210 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 211 | cache->key.objectid, bytenr, 0, |
| 212 | &logical, &nr, &stripe_len); |
| 213 | BUG_ON(ret); |
| 214 | while (nr--) { |
| 215 | btrfs_remove_free_space(cache, logical[nr], |
| 216 | stripe_len); |
| 217 | } |
| 218 | kfree(logical); |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 223 | static int cache_block_group(struct btrfs_root *root, |
| 224 | struct btrfs_block_group_cache *block_group) |
| 225 | { |
| 226 | struct btrfs_path *path; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 227 | int ret = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 228 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 229 | struct extent_buffer *leaf; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 230 | int slot; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 231 | u64 last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 232 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 233 | if (!block_group) |
| 234 | return 0; |
| 235 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 236 | root = root->fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 237 | |
| 238 | if (block_group->cached) |
| 239 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 240 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 241 | path = btrfs_alloc_path(); |
| 242 | if (!path) |
| 243 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 244 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 245 | path->reada = 2; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 246 | /* |
| 247 | * we get into deadlocks with paths held by callers of this function. |
| 248 | * since the alloc_mutex is protecting things right now, just |
| 249 | * skip the locking here |
| 250 | */ |
| 251 | path->skip_locking = 1; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 252 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
| 253 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 254 | key.offset = 0; |
| 255 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 256 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 257 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 258 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 259 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 260 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 261 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 262 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 263 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 264 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 265 | if (ret < 0) |
| 266 | goto err; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 267 | if (ret == 0) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 268 | continue; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 269 | else |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 270 | break; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 271 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 272 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 273 | if (key.objectid < block_group->key.objectid) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 274 | goto next; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 275 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 276 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 277 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 278 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 279 | |
| 280 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 281 | add_new_free_space(block_group, root->fs_info, last, |
| 282 | key.objectid); |
| 283 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 284 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 285 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 286 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 287 | path->slots[0]++; |
| 288 | } |
| 289 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 290 | add_new_free_space(block_group, root->fs_info, last, |
| 291 | block_group->key.objectid + |
| 292 | block_group->key.offset); |
| 293 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 294 | remove_sb_from_cache(root, block_group); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 295 | block_group->cached = 1; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 296 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 297 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 298 | btrfs_free_path(path); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 299 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 302 | /* |
| 303 | * return the block group that starts at or after bytenr |
| 304 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 305 | static struct btrfs_block_group_cache * |
| 306 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 307 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 308 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 309 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 310 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 311 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 312 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 315 | /* |
| 316 | * return the block group that contains teh given bytenr |
| 317 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 318 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 319 | struct btrfs_fs_info *info, |
| 320 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 321 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 322 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 323 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 324 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 325 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 326 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 327 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 328 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 329 | static inline void put_block_group(struct btrfs_block_group_cache *cache) |
| 330 | { |
| 331 | if (atomic_dec_and_test(&cache->count)) |
| 332 | kfree(cache); |
| 333 | } |
| 334 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 335 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 336 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 337 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 338 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 339 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 340 | |
| 341 | rcu_read_lock(); |
| 342 | list_for_each_entry_rcu(found, head, list) { |
| 343 | if (found->flags == flags) { |
| 344 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 345 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 346 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 347 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 348 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 349 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 352 | /* |
| 353 | * after adding space to the filesystem, we need to clear the full flags |
| 354 | * on all the space infos. |
| 355 | */ |
| 356 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 357 | { |
| 358 | struct list_head *head = &info->space_info; |
| 359 | struct btrfs_space_info *found; |
| 360 | |
| 361 | rcu_read_lock(); |
| 362 | list_for_each_entry_rcu(found, head, list) |
| 363 | found->full = 0; |
| 364 | rcu_read_unlock(); |
| 365 | } |
| 366 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 367 | static u64 div_factor(u64 num, int factor) |
| 368 | { |
| 369 | if (factor == 10) |
| 370 | return num; |
| 371 | num *= factor; |
| 372 | do_div(num, 10); |
| 373 | return num; |
| 374 | } |
| 375 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 376 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 377 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 378 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 379 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 380 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 381 | u64 last = max(search_hint, search_start); |
| 382 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 383 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 384 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 385 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 386 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 387 | while (1) { |
| 388 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 389 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 390 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 391 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 392 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 393 | last = cache->key.objectid + cache->key.offset; |
| 394 | used = btrfs_block_group_used(&cache->item); |
| 395 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 396 | if ((full_search || !cache->ro) && |
| 397 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 398 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 399 | div_factor(cache->key.offset, factor)) { |
| 400 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 401 | spin_unlock(&cache->lock); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 402 | put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 403 | goto found; |
| 404 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 405 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 406 | spin_unlock(&cache->lock); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 407 | put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 408 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 409 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 410 | if (!wrapped) { |
| 411 | last = search_start; |
| 412 | wrapped = 1; |
| 413 | goto again; |
| 414 | } |
| 415 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 416 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 417 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 418 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 419 | goto again; |
| 420 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 421 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 422 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 423 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 424 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 425 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 426 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 427 | { |
| 428 | int ret; |
| 429 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 430 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 431 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 432 | path = btrfs_alloc_path(); |
| 433 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 434 | key.objectid = start; |
| 435 | key.offset = len; |
| 436 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 437 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 438 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 439 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 440 | return ret; |
| 441 | } |
| 442 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 443 | /* |
| 444 | * Back reference rules. Back refs have three main goals: |
| 445 | * |
| 446 | * 1) differentiate between all holders of references to an extent so that |
| 447 | * when a reference is dropped we can make sure it was a valid reference |
| 448 | * before freeing the extent. |
| 449 | * |
| 450 | * 2) Provide enough information to quickly find the holders of an extent |
| 451 | * if we notice a given block is corrupted or bad. |
| 452 | * |
| 453 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 454 | * maintenance. This is actually the same as #2, but with a slightly |
| 455 | * different use case. |
| 456 | * |
| 457 | * File extents can be referenced by: |
| 458 | * |
| 459 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 460 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 461 | * - different offsets inside a file (bookend extents in file.c) |
| 462 | * |
| 463 | * The extent ref structure has fields for: |
| 464 | * |
| 465 | * - Objectid of the subvolume root |
| 466 | * - Generation number of the tree holding the reference |
| 467 | * - objectid of the file holding the reference |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 468 | * - number of references holding by parent node (alway 1 for tree blocks) |
| 469 | * |
| 470 | * Btree leaf may hold multiple references to a file extent. In most cases, |
| 471 | * these references are from same file and the corresponding offsets inside |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 472 | * the file are close together. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 473 | * |
| 474 | * When a file extent is allocated the fields are filled in: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 475 | * (root_key.objectid, trans->transid, inode objectid, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 476 | * |
| 477 | * 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] | 478 | * in the leaf. It looks similar to the create case, but trans->transid will |
| 479 | * be different when the block is cow'd. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 480 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 481 | * (root_key.objectid, trans->transid, inode objectid, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 482 | * number of references in the leaf) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 483 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 484 | * When a file extent is removed either during snapshot deletion or |
| 485 | * file truncation, we find the corresponding back reference and check |
| 486 | * the following fields: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 487 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 488 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 489 | * inode objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 490 | * |
| 491 | * Btree extents can be referenced by: |
| 492 | * |
| 493 | * - Different subvolumes |
| 494 | * - Different generations of the same subvolume |
| 495 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 496 | * When a tree block is created, back references are inserted: |
| 497 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 498 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 499 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 500 | * When a tree block is cow'd, new back references are added for all the |
| 501 | * blocks it points to. If the tree block isn't in reference counted root, |
| 502 | * the old back references are removed. These new back references are of |
| 503 | * the form (trans->transid will have increased since creation): |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 504 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 505 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 506 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 507 | * When a backref is in deleting, the following fields are checked: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 508 | * |
| 509 | * if backref was for a tree root: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 510 | * (btrfs_header_owner(itself), btrfs_header_generation(itself), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 511 | * else |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 512 | * (btrfs_header_owner(parent), btrfs_header_generation(parent), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 513 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 514 | * Back Reference Key composing: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 515 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 516 | * The key objectid corresponds to the first byte in the extent, the key |
| 517 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first |
| 518 | * byte of parent extent. If a extent is tree root, the key offset is set |
| 519 | * to the key objectid. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 520 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 521 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 522 | static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 523 | struct btrfs_root *root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 524 | struct btrfs_path *path, |
| 525 | u64 bytenr, u64 parent, |
| 526 | u64 ref_root, u64 ref_generation, |
| 527 | u64 owner_objectid, int del) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 528 | { |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 529 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 530 | struct btrfs_extent_ref *ref; |
| 531 | struct extent_buffer *leaf; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 532 | u64 ref_objectid; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 533 | int ret; |
| 534 | |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 535 | key.objectid = bytenr; |
| 536 | key.type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 537 | key.offset = parent; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 538 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 539 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); |
| 540 | if (ret < 0) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 541 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 542 | if (ret > 0) { |
| 543 | ret = -ENOENT; |
| 544 | goto out; |
| 545 | } |
| 546 | |
| 547 | leaf = path->nodes[0]; |
| 548 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 549 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 550 | if (btrfs_ref_root(leaf, ref) != ref_root || |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 551 | btrfs_ref_generation(leaf, ref) != ref_generation || |
| 552 | (ref_objectid != owner_objectid && |
| 553 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 554 | ret = -EIO; |
| 555 | WARN_ON(1); |
| 556 | goto out; |
| 557 | } |
| 558 | ret = 0; |
| 559 | out: |
| 560 | return ret; |
| 561 | } |
| 562 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 563 | static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 564 | struct btrfs_root *root, |
| 565 | struct btrfs_path *path, |
| 566 | u64 bytenr, u64 parent, |
| 567 | u64 ref_root, u64 ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 568 | u64 owner_objectid, |
| 569 | int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 570 | { |
| 571 | struct btrfs_key key; |
| 572 | struct extent_buffer *leaf; |
| 573 | struct btrfs_extent_ref *ref; |
| 574 | u32 num_refs; |
| 575 | int ret; |
| 576 | |
| 577 | key.objectid = bytenr; |
| 578 | key.type = BTRFS_EXTENT_REF_KEY; |
| 579 | key.offset = parent; |
| 580 | |
| 581 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
| 582 | if (ret == 0) { |
| 583 | leaf = path->nodes[0]; |
| 584 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 585 | struct btrfs_extent_ref); |
| 586 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 587 | btrfs_set_ref_generation(leaf, ref, ref_generation); |
| 588 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 589 | btrfs_set_ref_num_refs(leaf, ref, refs_to_add); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 590 | } else if (ret == -EEXIST) { |
| 591 | u64 existing_owner; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 592 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 593 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); |
| 594 | leaf = path->nodes[0]; |
| 595 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 596 | struct btrfs_extent_ref); |
| 597 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 598 | btrfs_ref_generation(leaf, ref) != ref_generation) { |
| 599 | ret = -EIO; |
| 600 | WARN_ON(1); |
| 601 | goto out; |
| 602 | } |
| 603 | |
| 604 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 605 | BUG_ON(num_refs == 0); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 606 | btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 607 | |
| 608 | existing_owner = btrfs_ref_objectid(leaf, ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 609 | if (existing_owner != owner_objectid && |
| 610 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 611 | btrfs_set_ref_objectid(leaf, ref, |
| 612 | BTRFS_MULTIPLE_OBJECTIDS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 613 | } |
| 614 | ret = 0; |
| 615 | } else { |
| 616 | goto out; |
| 617 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 618 | btrfs_unlock_up_safe(path, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 619 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 620 | out: |
| 621 | btrfs_release_path(root, path); |
| 622 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 623 | } |
| 624 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 625 | static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 626 | struct btrfs_root *root, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 627 | struct btrfs_path *path, |
| 628 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 629 | { |
| 630 | struct extent_buffer *leaf; |
| 631 | struct btrfs_extent_ref *ref; |
| 632 | u32 num_refs; |
| 633 | int ret = 0; |
| 634 | |
| 635 | leaf = path->nodes[0]; |
| 636 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 637 | num_refs = btrfs_ref_num_refs(leaf, ref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 638 | BUG_ON(num_refs < refs_to_drop); |
| 639 | num_refs -= refs_to_drop; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 640 | if (num_refs == 0) { |
| 641 | ret = btrfs_del_item(trans, root, path); |
| 642 | } else { |
| 643 | btrfs_set_ref_num_refs(leaf, ref, num_refs); |
| 644 | btrfs_mark_buffer_dirty(leaf); |
| 645 | } |
| 646 | btrfs_release_path(root, path); |
| 647 | return ret; |
| 648 | } |
| 649 | |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 650 | #ifdef BIO_RW_DISCARD |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 651 | static void btrfs_issue_discard(struct block_device *bdev, |
| 652 | u64 start, u64 len) |
| 653 | { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 654 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 655 | } |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 656 | #endif |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 657 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 658 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 659 | u64 num_bytes) |
| 660 | { |
| 661 | #ifdef BIO_RW_DISCARD |
| 662 | int ret; |
| 663 | u64 map_length = num_bytes; |
| 664 | struct btrfs_multi_bio *multi = NULL; |
| 665 | |
| 666 | /* Tell the block device(s) that the sectors can be discarded */ |
| 667 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 668 | bytenr, &map_length, &multi, 0); |
| 669 | if (!ret) { |
| 670 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 671 | int i; |
| 672 | |
| 673 | if (map_length > num_bytes) |
| 674 | map_length = num_bytes; |
| 675 | |
| 676 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 677 | btrfs_issue_discard(stripe->dev->bdev, |
| 678 | stripe->physical, |
| 679 | map_length); |
| 680 | } |
| 681 | kfree(multi); |
| 682 | } |
| 683 | |
| 684 | return ret; |
| 685 | #else |
| 686 | return 0; |
| 687 | #endif |
| 688 | } |
| 689 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 690 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 691 | struct btrfs_root *root, u64 bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 692 | u64 num_bytes, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 693 | u64 orig_parent, u64 parent, |
| 694 | u64 orig_root, u64 ref_root, |
| 695 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 696 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 697 | { |
| 698 | int ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 699 | int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 700 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 701 | ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes, |
| 702 | orig_parent, parent, orig_root, |
| 703 | ref_root, orig_generation, |
| 704 | ref_generation, owner_objectid, pin); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 705 | BUG_ON(ret); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 710 | struct btrfs_root *root, u64 bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 711 | u64 num_bytes, u64 orig_parent, u64 parent, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 712 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 713 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 714 | { |
| 715 | int ret; |
| 716 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 717 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 718 | return 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 719 | |
| 720 | ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes, |
| 721 | orig_parent, parent, ref_root, |
| 722 | ref_root, ref_generation, |
| 723 | ref_generation, owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 724 | return ret; |
| 725 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 726 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 727 | struct btrfs_root *root, u64 bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 728 | u64 num_bytes, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 729 | u64 orig_parent, u64 parent, |
| 730 | u64 orig_root, u64 ref_root, |
| 731 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 732 | u64 owner_objectid) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 733 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 734 | int ret; |
| 735 | |
| 736 | ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root, |
| 737 | ref_generation, owner_objectid, |
| 738 | BTRFS_ADD_DELAYED_REF, 0); |
| 739 | BUG_ON(ret); |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans, |
| 744 | struct btrfs_root *root, u64 bytenr, |
| 745 | u64 num_bytes, u64 parent, u64 ref_root, |
| 746 | u64 ref_generation, u64 owner_objectid, |
| 747 | int refs_to_add) |
| 748 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 749 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 750 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 751 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 752 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 753 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 754 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 755 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 756 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 757 | if (!path) |
| 758 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 759 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 760 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 761 | path->leave_spinning = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 762 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 763 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 764 | key.offset = num_bytes; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 765 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 766 | /* first find the extent item and update its reference count */ |
| 767 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 768 | path, 0, 1); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 769 | if (ret < 0) { |
| 770 | btrfs_set_path_blocking(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 771 | return ret; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 772 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 773 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 774 | if (ret > 0) { |
| 775 | WARN_ON(1); |
| 776 | btrfs_free_path(path); |
| 777 | return -EIO; |
| 778 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 779 | l = path->nodes[0]; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 780 | |
| 781 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 782 | if (key.objectid != bytenr) { |
| 783 | btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 784 | printk(KERN_ERR "btrfs wanted %llu found %llu\n", |
| 785 | (unsigned long long)bytenr, |
| 786 | (unsigned long long)key.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 787 | BUG(); |
| 788 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 789 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
| 790 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 791 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 792 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 793 | refs = btrfs_extent_refs(l, item); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 794 | btrfs_set_extent_refs(l, item, refs + refs_to_add); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 795 | btrfs_unlock_up_safe(path, 1); |
| 796 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 797 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 798 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 799 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 800 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 801 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 802 | path->leave_spinning = 1; |
| 803 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 804 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 805 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
| 806 | path, bytenr, parent, |
| 807 | ref_root, ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 808 | owner_objectid, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 809 | BUG_ON(ret); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 810 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 811 | return 0; |
| 812 | } |
| 813 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 814 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 815 | struct btrfs_root *root, |
| 816 | u64 bytenr, u64 num_bytes, u64 parent, |
| 817 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 818 | u64 owner_objectid) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 819 | { |
| 820 | int ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 821 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 822 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 823 | return 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 824 | |
| 825 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 826 | 0, ref_root, 0, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 827 | owner_objectid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 828 | return ret; |
| 829 | } |
| 830 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 831 | static int drop_delayed_ref(struct btrfs_trans_handle *trans, |
| 832 | struct btrfs_root *root, |
| 833 | struct btrfs_delayed_ref_node *node) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 834 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 835 | int ret = 0; |
| 836 | struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node); |
| 837 | |
| 838 | BUG_ON(node->ref_mod == 0); |
| 839 | ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes, |
| 840 | node->parent, ref->root, ref->generation, |
| 841 | ref->owner_objectid, ref->pin, node->ref_mod); |
| 842 | |
| 843 | return ret; |
| 844 | } |
| 845 | |
| 846 | /* helper function to actually process a single delayed ref entry */ |
| 847 | static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 848 | struct btrfs_root *root, |
| 849 | struct btrfs_delayed_ref_node *node, |
| 850 | int insert_reserved) |
| 851 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 852 | int ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 853 | struct btrfs_delayed_ref *ref; |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 854 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 855 | if (node->parent == (u64)-1) { |
| 856 | struct btrfs_delayed_ref_head *head; |
| 857 | /* |
| 858 | * we've hit the end of the chain and we were supposed |
| 859 | * to insert this extent into the tree. But, it got |
| 860 | * deleted before we ever needed to insert it, so all |
| 861 | * we have to do is clean up the accounting |
| 862 | */ |
| 863 | if (insert_reserved) { |
| 864 | update_reserved_extents(root, node->bytenr, |
| 865 | node->num_bytes, 0); |
| 866 | } |
| 867 | head = btrfs_delayed_node_to_head(node); |
| 868 | mutex_unlock(&head->mutex); |
| 869 | return 0; |
| 870 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 871 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 872 | ref = btrfs_delayed_node_to_ref(node); |
| 873 | if (ref->action == BTRFS_ADD_DELAYED_REF) { |
| 874 | if (insert_reserved) { |
| 875 | struct btrfs_key ins; |
| 876 | |
| 877 | ins.objectid = node->bytenr; |
| 878 | ins.offset = node->num_bytes; |
| 879 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 880 | |
| 881 | /* record the full extent allocation */ |
| 882 | ret = __btrfs_alloc_reserved_extent(trans, root, |
| 883 | node->parent, ref->root, |
| 884 | ref->generation, ref->owner_objectid, |
| 885 | &ins, node->ref_mod); |
| 886 | update_reserved_extents(root, node->bytenr, |
| 887 | node->num_bytes, 0); |
| 888 | } else { |
| 889 | /* just add one backref */ |
| 890 | ret = add_extent_ref(trans, root, node->bytenr, |
| 891 | node->num_bytes, |
| 892 | node->parent, ref->root, ref->generation, |
| 893 | ref->owner_objectid, node->ref_mod); |
| 894 | } |
| 895 | BUG_ON(ret); |
| 896 | } else if (ref->action == BTRFS_DROP_DELAYED_REF) { |
| 897 | WARN_ON(insert_reserved); |
| 898 | ret = drop_delayed_ref(trans, root, node); |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 899 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 900 | return 0; |
| 901 | } |
| 902 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 903 | static noinline struct btrfs_delayed_ref_node * |
| 904 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 905 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 906 | struct rb_node *node; |
| 907 | struct btrfs_delayed_ref_node *ref; |
| 908 | int action = BTRFS_ADD_DELAYED_REF; |
| 909 | again: |
| 910 | /* |
| 911 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 912 | * this prevents ref count from going down to zero when |
| 913 | * there still are pending delayed ref. |
| 914 | */ |
| 915 | node = rb_prev(&head->node.rb_node); |
| 916 | while (1) { |
| 917 | if (!node) |
| 918 | break; |
| 919 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 920 | rb_node); |
| 921 | if (ref->bytenr != head->node.bytenr) |
| 922 | break; |
| 923 | if (btrfs_delayed_node_to_ref(ref)->action == action) |
| 924 | return ref; |
| 925 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 926 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 927 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 928 | action = BTRFS_DROP_DELAYED_REF; |
| 929 | goto again; |
| 930 | } |
| 931 | return NULL; |
| 932 | } |
| 933 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 934 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 935 | struct btrfs_root *root, |
| 936 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 937 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 938 | struct btrfs_delayed_ref_root *delayed_refs; |
| 939 | struct btrfs_delayed_ref_node *ref; |
| 940 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
| 941 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 942 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 943 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 944 | |
| 945 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 946 | while (1) { |
| 947 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 948 | /* pick a new head ref from the cluster list */ |
| 949 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 950 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 951 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 952 | locked_ref = list_entry(cluster->next, |
| 953 | struct btrfs_delayed_ref_head, cluster); |
| 954 | |
| 955 | /* grab the lock that says we are going to process |
| 956 | * all the refs for this head */ |
| 957 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 958 | |
| 959 | /* |
| 960 | * we may have dropped the spin lock to get the head |
| 961 | * mutex lock, and that might have given someone else |
| 962 | * time to free the head. If that's true, it has been |
| 963 | * removed from our list and we can move on. |
| 964 | */ |
| 965 | if (ret == -EAGAIN) { |
| 966 | locked_ref = NULL; |
| 967 | count++; |
| 968 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * record the must insert reserved flag before we |
| 974 | * drop the spin lock. |
| 975 | */ |
| 976 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 977 | locked_ref->must_insert_reserved = 0; |
| 978 | |
| 979 | /* |
| 980 | * locked_ref is the head node, so we have to go one |
| 981 | * node back for any delayed ref updates |
| 982 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 983 | ref = select_delayed_ref(locked_ref); |
| 984 | if (!ref) { |
| 985 | /* All delayed refs have been processed, Go ahead |
| 986 | * and send the head node to run_one_delayed_ref, |
| 987 | * so that any accounting fixes can happen |
| 988 | */ |
| 989 | ref = &locked_ref->node; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 990 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 991 | locked_ref = NULL; |
| 992 | } |
| 993 | |
| 994 | ref->in_tree = 0; |
| 995 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 996 | delayed_refs->num_entries--; |
| 997 | spin_unlock(&delayed_refs->lock); |
| 998 | |
| 999 | ret = run_one_delayed_ref(trans, root, ref, |
| 1000 | must_insert_reserved); |
| 1001 | BUG_ON(ret); |
| 1002 | btrfs_put_delayed_ref(ref); |
| 1003 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1004 | count++; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 1005 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1006 | spin_lock(&delayed_refs->lock); |
| 1007 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1008 | return count; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * this starts processing the delayed reference count updates and |
| 1013 | * extent insertions we have queued up so far. count can be |
| 1014 | * 0, which means to process everything in the tree at the start |
| 1015 | * of the run (but not newly added entries), or it can be some target |
| 1016 | * number you'd like to process. |
| 1017 | */ |
| 1018 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 1019 | struct btrfs_root *root, unsigned long count) |
| 1020 | { |
| 1021 | struct rb_node *node; |
| 1022 | struct btrfs_delayed_ref_root *delayed_refs; |
| 1023 | struct btrfs_delayed_ref_node *ref; |
| 1024 | struct list_head cluster; |
| 1025 | int ret; |
| 1026 | int run_all = count == (unsigned long)-1; |
| 1027 | int run_most = 0; |
| 1028 | |
| 1029 | if (root == root->fs_info->extent_root) |
| 1030 | root = root->fs_info->tree_root; |
| 1031 | |
| 1032 | delayed_refs = &trans->transaction->delayed_refs; |
| 1033 | INIT_LIST_HEAD(&cluster); |
| 1034 | again: |
| 1035 | spin_lock(&delayed_refs->lock); |
| 1036 | if (count == 0) { |
| 1037 | count = delayed_refs->num_entries * 2; |
| 1038 | run_most = 1; |
| 1039 | } |
| 1040 | while (1) { |
| 1041 | if (!(run_all || run_most) && |
| 1042 | delayed_refs->num_heads_ready < 64) |
| 1043 | break; |
| 1044 | |
| 1045 | /* |
| 1046 | * go find something we can process in the rbtree. We start at |
| 1047 | * the beginning of the tree, and then build a cluster |
| 1048 | * of refs to process starting at the first one we are able to |
| 1049 | * lock |
| 1050 | */ |
| 1051 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 1052 | delayed_refs->run_delayed_start); |
| 1053 | if (ret) |
| 1054 | break; |
| 1055 | |
| 1056 | ret = run_clustered_refs(trans, root, &cluster); |
| 1057 | BUG_ON(ret < 0); |
| 1058 | |
| 1059 | count -= min_t(unsigned long, ret, count); |
| 1060 | |
| 1061 | if (count == 0) |
| 1062 | break; |
| 1063 | } |
| 1064 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1065 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1066 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1067 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1068 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1069 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1070 | |
| 1071 | while (node) { |
| 1072 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 1073 | rb_node); |
| 1074 | if (btrfs_delayed_ref_is_head(ref)) { |
| 1075 | struct btrfs_delayed_ref_head *head; |
| 1076 | |
| 1077 | head = btrfs_delayed_node_to_head(ref); |
| 1078 | atomic_inc(&ref->refs); |
| 1079 | |
| 1080 | spin_unlock(&delayed_refs->lock); |
| 1081 | mutex_lock(&head->mutex); |
| 1082 | mutex_unlock(&head->mutex); |
| 1083 | |
| 1084 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 1085 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1086 | goto again; |
| 1087 | } |
| 1088 | node = rb_next(node); |
| 1089 | } |
| 1090 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1091 | schedule_timeout(1); |
| 1092 | goto again; |
| 1093 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1094 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 1095 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1096 | return 0; |
| 1097 | } |
| 1098 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1099 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1100 | struct btrfs_root *root, u64 objectid, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1101 | { |
| 1102 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1103 | struct btrfs_path *path; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1104 | struct extent_buffer *leaf; |
| 1105 | struct btrfs_extent_ref *ref_item; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1106 | struct btrfs_key key; |
| 1107 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1108 | u64 ref_root; |
| 1109 | u64 last_snapshot; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1110 | u32 nritems; |
| 1111 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1112 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1113 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1114 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1115 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1116 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1117 | path = btrfs_alloc_path(); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1118 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 1119 | if (ret < 0) |
| 1120 | goto out; |
| 1121 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1122 | |
| 1123 | ret = -ENOENT; |
| 1124 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1125 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1126 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1127 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1128 | leaf = path->nodes[0]; |
| 1129 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1130 | |
| 1131 | if (found_key.objectid != bytenr || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1132 | found_key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1133 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1134 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1135 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1136 | while (1) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1137 | leaf = path->nodes[0]; |
| 1138 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1139 | if (path->slots[0] >= nritems) { |
| 1140 | ret = btrfs_next_leaf(extent_root, path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1141 | if (ret < 0) |
| 1142 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1143 | if (ret == 0) |
| 1144 | continue; |
| 1145 | break; |
| 1146 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1147 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1148 | if (found_key.objectid != bytenr) |
| 1149 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 1150 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1151 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 1152 | path->slots[0]++; |
| 1153 | continue; |
| 1154 | } |
| 1155 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1156 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1157 | struct btrfs_extent_ref); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1158 | ref_root = btrfs_ref_root(leaf, ref_item); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1159 | if ((ref_root != root->root_key.objectid && |
| 1160 | ref_root != BTRFS_TREE_LOG_OBJECTID) || |
| 1161 | objectid != btrfs_ref_objectid(leaf, ref_item)) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1162 | ret = 1; |
| 1163 | goto out; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1164 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1165 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { |
| 1166 | ret = 1; |
| 1167 | goto out; |
| 1168 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1169 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1170 | path->slots[0]++; |
| 1171 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1172 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1173 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1174 | btrfs_free_path(path); |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1178 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1179 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1180 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1181 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1182 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1183 | u64 root_gen; |
| 1184 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1185 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1186 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1187 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1188 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1189 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1190 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1191 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1192 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1193 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 1194 | shared = 0; |
| 1195 | root_gen = root->root_key.offset; |
| 1196 | } else { |
| 1197 | shared = 1; |
| 1198 | root_gen = trans->transid - 1; |
| 1199 | } |
| 1200 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1201 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1202 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1203 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1204 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1205 | struct btrfs_leaf_ref *ref; |
| 1206 | struct btrfs_extent_info *info; |
| 1207 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1208 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1209 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1210 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1211 | goto out; |
| 1212 | } |
| 1213 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1214 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1215 | ref->bytenr = buf->start; |
| 1216 | ref->owner = btrfs_header_owner(buf); |
| 1217 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1218 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1219 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1220 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1221 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1222 | u64 disk_bytenr; |
| 1223 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1224 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1225 | continue; |
| 1226 | fi = btrfs_item_ptr(buf, i, |
| 1227 | struct btrfs_file_extent_item); |
| 1228 | if (btrfs_file_extent_type(buf, fi) == |
| 1229 | BTRFS_FILE_EXTENT_INLINE) |
| 1230 | continue; |
| 1231 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1232 | if (disk_bytenr == 0) |
| 1233 | continue; |
| 1234 | |
| 1235 | info->bytenr = disk_bytenr; |
| 1236 | info->num_bytes = |
| 1237 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 1238 | info->objectid = key.objectid; |
| 1239 | info->offset = key.offset; |
| 1240 | info++; |
| 1241 | } |
| 1242 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1243 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1244 | if (ret == -EEXIST && shared) { |
| 1245 | struct btrfs_leaf_ref *old; |
| 1246 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 1247 | BUG_ON(!old); |
| 1248 | btrfs_remove_leaf_ref(root, old); |
| 1249 | btrfs_free_leaf_ref(root, old); |
| 1250 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 1251 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1252 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1253 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1254 | } |
| 1255 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1256 | return ret; |
| 1257 | } |
| 1258 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1259 | /* when a block goes through cow, we update the reference counts of |
| 1260 | * everything that block points to. The internal pointers of the block |
| 1261 | * can be in just about any order, and it is likely to have clusters of |
| 1262 | * things that are close together and clusters of things that are not. |
| 1263 | * |
| 1264 | * To help reduce the seeks that come with updating all of these reference |
| 1265 | * counts, sort them by byte number before actual updates are done. |
| 1266 | * |
| 1267 | * struct refsort is used to match byte number to slot in the btree block. |
| 1268 | * we sort based on the byte number and then use the slot to actually |
| 1269 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 1270 | * |
| 1271 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 1272 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 1273 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 1274 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1275 | */ |
| 1276 | struct refsort { |
| 1277 | u64 bytenr; |
| 1278 | u32 slot; |
| 1279 | }; |
| 1280 | |
| 1281 | /* |
| 1282 | * for passing into sort() |
| 1283 | */ |
| 1284 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 1285 | { |
| 1286 | const struct refsort *a = a_void; |
| 1287 | const struct refsort *b = b_void; |
| 1288 | |
| 1289 | if (a->bytenr < b->bytenr) |
| 1290 | return -1; |
| 1291 | if (a->bytenr > b->bytenr) |
| 1292 | return 1; |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans, |
| 1298 | struct btrfs_root *root, |
| 1299 | struct extent_buffer *orig_buf, |
| 1300 | struct extent_buffer *buf, u32 *nr_extents) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1301 | { |
| 1302 | u64 bytenr; |
| 1303 | u64 ref_root; |
| 1304 | u64 orig_root; |
| 1305 | u64 ref_generation; |
| 1306 | u64 orig_generation; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1307 | struct refsort *sorted; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1308 | u32 nritems; |
| 1309 | u32 nr_file_extents = 0; |
| 1310 | struct btrfs_key key; |
| 1311 | struct btrfs_file_extent_item *fi; |
| 1312 | int i; |
| 1313 | int level; |
| 1314 | int ret = 0; |
| 1315 | int faili = 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1316 | int refi = 0; |
| 1317 | int slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1318 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1319 | u64, u64, u64, u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1320 | |
| 1321 | ref_root = btrfs_header_owner(buf); |
| 1322 | ref_generation = btrfs_header_generation(buf); |
| 1323 | orig_root = btrfs_header_owner(orig_buf); |
| 1324 | orig_generation = btrfs_header_generation(orig_buf); |
| 1325 | |
| 1326 | nritems = btrfs_header_nritems(buf); |
| 1327 | level = btrfs_header_level(buf); |
| 1328 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1329 | sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS); |
| 1330 | BUG_ON(!sorted); |
| 1331 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1332 | if (root->ref_cows) { |
| 1333 | process_func = __btrfs_inc_extent_ref; |
| 1334 | } else { |
| 1335 | if (level == 0 && |
| 1336 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1337 | goto out; |
| 1338 | if (level != 0 && |
| 1339 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1340 | goto out; |
| 1341 | process_func = __btrfs_update_extent_ref; |
| 1342 | } |
| 1343 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1344 | /* |
| 1345 | * we make two passes through the items. In the first pass we |
| 1346 | * only record the byte number and slot. Then we sort based on |
| 1347 | * byte number and do the actual work based on the sorted results |
| 1348 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1349 | for (i = 0; i < nritems; i++) { |
| 1350 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1351 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1352 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1353 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1354 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1355 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1356 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1357 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1358 | BTRFS_FILE_EXTENT_INLINE) |
| 1359 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1360 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1361 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1362 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1363 | |
| 1364 | nr_file_extents++; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1365 | sorted[refi].bytenr = bytenr; |
| 1366 | sorted[refi].slot = i; |
| 1367 | refi++; |
| 1368 | } else { |
| 1369 | bytenr = btrfs_node_blockptr(buf, i); |
| 1370 | sorted[refi].bytenr = bytenr; |
| 1371 | sorted[refi].slot = i; |
| 1372 | refi++; |
| 1373 | } |
| 1374 | } |
| 1375 | /* |
| 1376 | * if refi == 0, we didn't actually put anything into the sorted |
| 1377 | * array and we're done |
| 1378 | */ |
| 1379 | if (refi == 0) |
| 1380 | goto out; |
| 1381 | |
| 1382 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 1383 | |
| 1384 | for (i = 0; i < refi; i++) { |
| 1385 | cond_resched(); |
| 1386 | slot = sorted[i].slot; |
| 1387 | bytenr = sorted[i].bytenr; |
| 1388 | |
| 1389 | if (level == 0) { |
| 1390 | btrfs_item_key_to_cpu(buf, &key, slot); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1391 | fi = btrfs_item_ptr(buf, slot, |
| 1392 | struct btrfs_file_extent_item); |
| 1393 | |
| 1394 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1395 | if (bytenr == 0) |
| 1396 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1397 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1398 | ret = process_func(trans, root, bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1399 | btrfs_file_extent_disk_num_bytes(buf, fi), |
| 1400 | orig_buf->start, buf->start, |
| 1401 | orig_root, ref_root, |
| 1402 | orig_generation, ref_generation, |
| 1403 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1404 | |
| 1405 | if (ret) { |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1406 | faili = slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1407 | WARN_ON(1); |
| 1408 | goto fail; |
| 1409 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1410 | } else { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1411 | ret = process_func(trans, root, bytenr, buf->len, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1412 | orig_buf->start, buf->start, |
| 1413 | orig_root, ref_root, |
| 1414 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1415 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1416 | if (ret) { |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1417 | faili = slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1418 | WARN_ON(1); |
| 1419 | goto fail; |
| 1420 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1421 | } |
| 1422 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1423 | out: |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1424 | kfree(sorted); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1425 | if (nr_extents) { |
| 1426 | if (level == 0) |
| 1427 | *nr_extents = nr_file_extents; |
| 1428 | else |
| 1429 | *nr_extents = nritems; |
| 1430 | } |
| 1431 | return 0; |
| 1432 | fail: |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1433 | kfree(sorted); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1434 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1435 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1436 | } |
| 1437 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1438 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
| 1439 | struct btrfs_root *root, struct extent_buffer *orig_buf, |
| 1440 | struct extent_buffer *buf, int start_slot, int nr) |
| 1441 | |
| 1442 | { |
| 1443 | u64 bytenr; |
| 1444 | u64 ref_root; |
| 1445 | u64 orig_root; |
| 1446 | u64 ref_generation; |
| 1447 | u64 orig_generation; |
| 1448 | struct btrfs_key key; |
| 1449 | struct btrfs_file_extent_item *fi; |
| 1450 | int i; |
| 1451 | int ret; |
| 1452 | int slot; |
| 1453 | int level; |
| 1454 | |
| 1455 | BUG_ON(start_slot < 0); |
| 1456 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); |
| 1457 | |
| 1458 | ref_root = btrfs_header_owner(buf); |
| 1459 | ref_generation = btrfs_header_generation(buf); |
| 1460 | orig_root = btrfs_header_owner(orig_buf); |
| 1461 | orig_generation = btrfs_header_generation(orig_buf); |
| 1462 | level = btrfs_header_level(buf); |
| 1463 | |
| 1464 | if (!root->ref_cows) { |
| 1465 | if (level == 0 && |
| 1466 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1467 | return 0; |
| 1468 | if (level != 0 && |
| 1469 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { |
| 1474 | cond_resched(); |
| 1475 | if (level == 0) { |
| 1476 | btrfs_item_key_to_cpu(buf, &key, slot); |
| 1477 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1478 | continue; |
| 1479 | fi = btrfs_item_ptr(buf, slot, |
| 1480 | struct btrfs_file_extent_item); |
| 1481 | if (btrfs_file_extent_type(buf, fi) == |
| 1482 | BTRFS_FILE_EXTENT_INLINE) |
| 1483 | continue; |
| 1484 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1485 | if (bytenr == 0) |
| 1486 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1487 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1488 | btrfs_file_extent_disk_num_bytes(buf, fi), |
| 1489 | orig_buf->start, buf->start, |
| 1490 | orig_root, ref_root, orig_generation, |
| 1491 | ref_generation, key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1492 | if (ret) |
| 1493 | goto fail; |
| 1494 | } else { |
| 1495 | bytenr = btrfs_node_blockptr(buf, slot); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1496 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1497 | buf->len, orig_buf->start, |
| 1498 | buf->start, orig_root, ref_root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1499 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1500 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1501 | if (ret) |
| 1502 | goto fail; |
| 1503 | } |
| 1504 | } |
| 1505 | return 0; |
| 1506 | fail: |
| 1507 | WARN_ON(1); |
| 1508 | return -1; |
| 1509 | } |
| 1510 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1511 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1512 | struct btrfs_root *root, |
| 1513 | struct btrfs_path *path, |
| 1514 | struct btrfs_block_group_cache *cache) |
| 1515 | { |
| 1516 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1517 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1518 | unsigned long bi; |
| 1519 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1520 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1521 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1522 | if (ret < 0) |
| 1523 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1524 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1525 | |
| 1526 | leaf = path->nodes[0]; |
| 1527 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1528 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1529 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1530 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1531 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1532 | if (ret) |
| 1533 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1534 | return 0; |
| 1535 | |
| 1536 | } |
| 1537 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1538 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1539 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1540 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1541 | struct btrfs_block_group_cache *cache, *entry; |
| 1542 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1543 | int err = 0; |
| 1544 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1545 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1546 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1547 | |
| 1548 | path = btrfs_alloc_path(); |
| 1549 | if (!path) |
| 1550 | return -ENOMEM; |
| 1551 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1552 | while (1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1553 | cache = NULL; |
| 1554 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1555 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1556 | n; n = rb_next(n)) { |
| 1557 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1558 | cache_node); |
| 1559 | if (entry->dirty) { |
| 1560 | cache = entry; |
| 1561 | break; |
| 1562 | } |
| 1563 | } |
| 1564 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1565 | |
| 1566 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1567 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1568 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1569 | cache->dirty = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1570 | last += cache->key.offset; |
| 1571 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1572 | err = write_one_cache_group(trans, root, |
| 1573 | path, cache); |
| 1574 | /* |
| 1575 | * if we fail to write the cache group, we want |
| 1576 | * to keep it marked dirty in hopes that a later |
| 1577 | * write will work |
| 1578 | */ |
| 1579 | if (err) { |
| 1580 | werr = err; |
| 1581 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | btrfs_free_path(path); |
| 1585 | return werr; |
| 1586 | } |
| 1587 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1588 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 1589 | { |
| 1590 | struct btrfs_block_group_cache *block_group; |
| 1591 | int readonly = 0; |
| 1592 | |
| 1593 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 1594 | if (!block_group || block_group->ro) |
| 1595 | readonly = 1; |
| 1596 | if (block_group) |
| 1597 | put_block_group(block_group); |
| 1598 | return readonly; |
| 1599 | } |
| 1600 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1601 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1602 | u64 total_bytes, u64 bytes_used, |
| 1603 | struct btrfs_space_info **space_info) |
| 1604 | { |
| 1605 | struct btrfs_space_info *found; |
| 1606 | |
| 1607 | found = __find_space_info(info, flags); |
| 1608 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1609 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1610 | found->total_bytes += total_bytes; |
| 1611 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1612 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1613 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1614 | *space_info = found; |
| 1615 | return 0; |
| 1616 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1617 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1618 | if (!found) |
| 1619 | return -ENOMEM; |
| 1620 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1621 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1622 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1623 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1624 | found->flags = flags; |
| 1625 | found->total_bytes = total_bytes; |
| 1626 | found->bytes_used = bytes_used; |
| 1627 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1628 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1629 | found->bytes_readonly = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1630 | found->bytes_delalloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1631 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1632 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1633 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 1634 | list_add_rcu(&found->list, &info->space_info); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1635 | return 0; |
| 1636 | } |
| 1637 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1638 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1639 | { |
| 1640 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1641 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1642 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1643 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1644 | if (extra_flags) { |
| 1645 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1646 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1647 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1648 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1649 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1650 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1651 | } |
| 1652 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1653 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1654 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 1655 | { |
| 1656 | spin_lock(&cache->space_info->lock); |
| 1657 | spin_lock(&cache->lock); |
| 1658 | if (!cache->ro) { |
| 1659 | cache->space_info->bytes_readonly += cache->key.offset - |
| 1660 | btrfs_block_group_used(&cache->item); |
| 1661 | cache->ro = 1; |
| 1662 | } |
| 1663 | spin_unlock(&cache->lock); |
| 1664 | spin_unlock(&cache->space_info->lock); |
| 1665 | } |
| 1666 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1667 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1668 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1669 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1670 | |
| 1671 | if (num_devices == 1) |
| 1672 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1673 | if (num_devices < 4) |
| 1674 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1675 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1676 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1677 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1678 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1679 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1680 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1681 | |
| 1682 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1683 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1684 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1685 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1686 | |
| 1687 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1688 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1689 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1690 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1691 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1692 | return flags; |
| 1693 | } |
| 1694 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1695 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) |
| 1696 | { |
| 1697 | struct btrfs_fs_info *info = root->fs_info; |
| 1698 | u64 alloc_profile; |
| 1699 | |
| 1700 | if (data) { |
| 1701 | alloc_profile = info->avail_data_alloc_bits & |
| 1702 | info->data_alloc_profile; |
| 1703 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
| 1704 | } else if (root == root->fs_info->chunk_root) { |
| 1705 | alloc_profile = info->avail_system_alloc_bits & |
| 1706 | info->system_alloc_profile; |
| 1707 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
| 1708 | } else { |
| 1709 | alloc_profile = info->avail_metadata_alloc_bits & |
| 1710 | info->metadata_alloc_profile; |
| 1711 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
| 1712 | } |
| 1713 | |
| 1714 | return btrfs_reduce_alloc_profile(root, data); |
| 1715 | } |
| 1716 | |
| 1717 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 1718 | { |
| 1719 | u64 alloc_target; |
| 1720 | |
| 1721 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 1722 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
| 1723 | alloc_target); |
| 1724 | } |
| 1725 | |
| 1726 | /* |
| 1727 | * for now this just makes sure we have at least 5% of our metadata space free |
| 1728 | * for use. |
| 1729 | */ |
| 1730 | int btrfs_check_metadata_free_space(struct btrfs_root *root) |
| 1731 | { |
| 1732 | struct btrfs_fs_info *info = root->fs_info; |
| 1733 | struct btrfs_space_info *meta_sinfo; |
| 1734 | u64 alloc_target, thresh; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1735 | int committed = 0, ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1736 | |
| 1737 | /* get the space info for where the metadata will live */ |
| 1738 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 1739 | meta_sinfo = __find_space_info(info, alloc_target); |
| 1740 | |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1741 | again: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1742 | spin_lock(&meta_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1743 | if (!meta_sinfo->full) |
| 1744 | thresh = meta_sinfo->total_bytes * 80; |
| 1745 | else |
| 1746 | thresh = meta_sinfo->total_bytes * 95; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1747 | |
| 1748 | do_div(thresh, 100); |
| 1749 | |
| 1750 | if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 1751 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1752 | struct btrfs_trans_handle *trans; |
| 1753 | if (!meta_sinfo->full) { |
| 1754 | meta_sinfo->force_alloc = 1; |
| 1755 | spin_unlock(&meta_sinfo->lock); |
| 1756 | |
| 1757 | trans = btrfs_start_transaction(root, 1); |
| 1758 | if (!trans) |
| 1759 | return -ENOMEM; |
| 1760 | |
| 1761 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 1762 | 2 * 1024 * 1024, alloc_target, 0); |
| 1763 | btrfs_end_transaction(trans, root); |
| 1764 | goto again; |
| 1765 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1766 | spin_unlock(&meta_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1767 | |
| 1768 | if (!committed) { |
| 1769 | committed = 1; |
| 1770 | trans = btrfs_join_transaction(root, 1); |
| 1771 | if (!trans) |
| 1772 | return -ENOMEM; |
| 1773 | ret = btrfs_commit_transaction(trans, root); |
| 1774 | if (ret) |
| 1775 | return ret; |
| 1776 | goto again; |
| 1777 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1778 | return -ENOSPC; |
| 1779 | } |
| 1780 | spin_unlock(&meta_sinfo->lock); |
| 1781 | |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | /* |
| 1786 | * This will check the space that the inode allocates from to make sure we have |
| 1787 | * enough space for bytes. |
| 1788 | */ |
| 1789 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, |
| 1790 | u64 bytes) |
| 1791 | { |
| 1792 | struct btrfs_space_info *data_sinfo; |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1793 | int ret = 0, committed = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1794 | |
| 1795 | /* make sure bytes are sectorsize aligned */ |
| 1796 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 1797 | |
| 1798 | data_sinfo = BTRFS_I(inode)->space_info; |
| 1799 | again: |
| 1800 | /* make sure we have enough space to handle the data first */ |
| 1801 | spin_lock(&data_sinfo->lock); |
| 1802 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - |
| 1803 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - |
| 1804 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - |
| 1805 | data_sinfo->bytes_may_use < bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1806 | struct btrfs_trans_handle *trans; |
| 1807 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1808 | /* |
| 1809 | * if we don't have enough free bytes in this space then we need |
| 1810 | * to alloc a new chunk. |
| 1811 | */ |
| 1812 | if (!data_sinfo->full) { |
| 1813 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1814 | |
| 1815 | data_sinfo->force_alloc = 1; |
| 1816 | spin_unlock(&data_sinfo->lock); |
| 1817 | |
| 1818 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 1819 | trans = btrfs_start_transaction(root, 1); |
| 1820 | if (!trans) |
| 1821 | return -ENOMEM; |
| 1822 | |
| 1823 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 1824 | bytes + 2 * 1024 * 1024, |
| 1825 | alloc_target, 0); |
| 1826 | btrfs_end_transaction(trans, root); |
| 1827 | if (ret) |
| 1828 | return ret; |
| 1829 | goto again; |
| 1830 | } |
| 1831 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 1832 | |
| 1833 | /* commit the current transaction and try again */ |
| 1834 | if (!committed) { |
| 1835 | committed = 1; |
| 1836 | trans = btrfs_join_transaction(root, 1); |
| 1837 | if (!trans) |
| 1838 | return -ENOMEM; |
| 1839 | ret = btrfs_commit_transaction(trans, root); |
| 1840 | if (ret) |
| 1841 | return ret; |
| 1842 | goto again; |
| 1843 | } |
| 1844 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 1845 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" |
| 1846 | ", %llu bytes_used, %llu bytes_reserved, " |
| 1847 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use" |
| 1848 | "%llu total\n", bytes, data_sinfo->bytes_delalloc, |
| 1849 | data_sinfo->bytes_used, data_sinfo->bytes_reserved, |
| 1850 | data_sinfo->bytes_pinned, data_sinfo->bytes_readonly, |
| 1851 | data_sinfo->bytes_may_use, data_sinfo->total_bytes); |
| 1852 | return -ENOSPC; |
| 1853 | } |
| 1854 | data_sinfo->bytes_may_use += bytes; |
| 1855 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 1856 | spin_unlock(&data_sinfo->lock); |
| 1857 | |
| 1858 | return btrfs_check_metadata_free_space(root); |
| 1859 | } |
| 1860 | |
| 1861 | /* |
| 1862 | * if there was an error for whatever reason after calling |
| 1863 | * btrfs_check_data_free_space, call this so we can cleanup the counters. |
| 1864 | */ |
| 1865 | void btrfs_free_reserved_data_space(struct btrfs_root *root, |
| 1866 | struct inode *inode, u64 bytes) |
| 1867 | { |
| 1868 | struct btrfs_space_info *data_sinfo; |
| 1869 | |
| 1870 | /* make sure bytes are sectorsize aligned */ |
| 1871 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 1872 | |
| 1873 | data_sinfo = BTRFS_I(inode)->space_info; |
| 1874 | spin_lock(&data_sinfo->lock); |
| 1875 | data_sinfo->bytes_may_use -= bytes; |
| 1876 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 1877 | spin_unlock(&data_sinfo->lock); |
| 1878 | } |
| 1879 | |
| 1880 | /* called when we are adding a delalloc extent to the inode's io_tree */ |
| 1881 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, |
| 1882 | u64 bytes) |
| 1883 | { |
| 1884 | struct btrfs_space_info *data_sinfo; |
| 1885 | |
| 1886 | /* get the space info for where this inode will be storing its data */ |
| 1887 | data_sinfo = BTRFS_I(inode)->space_info; |
| 1888 | |
| 1889 | /* make sure we have enough space to handle the data first */ |
| 1890 | spin_lock(&data_sinfo->lock); |
| 1891 | data_sinfo->bytes_delalloc += bytes; |
| 1892 | |
| 1893 | /* |
| 1894 | * we are adding a delalloc extent without calling |
| 1895 | * btrfs_check_data_free_space first. This happens on a weird |
| 1896 | * writepage condition, but shouldn't hurt our accounting |
| 1897 | */ |
| 1898 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { |
| 1899 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; |
| 1900 | BTRFS_I(inode)->reserved_bytes = 0; |
| 1901 | } else { |
| 1902 | data_sinfo->bytes_may_use -= bytes; |
| 1903 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 1904 | } |
| 1905 | |
| 1906 | spin_unlock(&data_sinfo->lock); |
| 1907 | } |
| 1908 | |
| 1909 | /* called when we are clearing an delalloc extent from the inode's io_tree */ |
| 1910 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, |
| 1911 | u64 bytes) |
| 1912 | { |
| 1913 | struct btrfs_space_info *info; |
| 1914 | |
| 1915 | info = BTRFS_I(inode)->space_info; |
| 1916 | |
| 1917 | spin_lock(&info->lock); |
| 1918 | info->bytes_delalloc -= bytes; |
| 1919 | spin_unlock(&info->lock); |
| 1920 | } |
| 1921 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1922 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1923 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1924 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1925 | { |
| 1926 | struct btrfs_space_info *space_info; |
| 1927 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1928 | int ret = 0; |
| 1929 | |
| 1930 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1931 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1932 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1933 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1934 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1935 | if (!space_info) { |
| 1936 | ret = update_space_info(extent_root->fs_info, flags, |
| 1937 | 0, 0, &space_info); |
| 1938 | BUG_ON(ret); |
| 1939 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1940 | BUG_ON(!space_info); |
| 1941 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1942 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1943 | if (space_info->force_alloc) { |
| 1944 | force = 1; |
| 1945 | space_info->force_alloc = 0; |
| 1946 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1947 | if (space_info->full) { |
| 1948 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1949 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1950 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1951 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1952 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
| 1953 | thresh = div_factor(thresh, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1954 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1955 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1956 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 1957 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1958 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1959 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1960 | spin_unlock(&space_info->lock); |
| 1961 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1962 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1963 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1964 | space_info->full = 1; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1965 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1966 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1967 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1968 | } |
| 1969 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1970 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1971 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1972 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1973 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1974 | { |
| 1975 | struct btrfs_block_group_cache *cache; |
| 1976 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1977 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1978 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1979 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1980 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1981 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1982 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1983 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1984 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1985 | byte_in_group = bytenr - cache->key.objectid; |
| 1986 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1987 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1988 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1989 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1990 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1991 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1992 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1993 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1994 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1995 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 1996 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1997 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1998 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1999 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2000 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2001 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2002 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2003 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2004 | if (cache->ro) |
| 2005 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2006 | btrfs_set_block_group_used(&cache->item, old_val); |
| 2007 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2008 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2009 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2010 | int ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2011 | |
| 2012 | ret = btrfs_discard_extent(root, bytenr, |
| 2013 | num_bytes); |
| 2014 | WARN_ON(ret); |
| 2015 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2016 | ret = btrfs_add_free_space(cache, bytenr, |
| 2017 | num_bytes); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2018 | WARN_ON(ret); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2019 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2020 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2021 | put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2022 | total -= num_bytes; |
| 2023 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2024 | } |
| 2025 | return 0; |
| 2026 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2027 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2028 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 2029 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2030 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2031 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2032 | |
| 2033 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 2034 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2035 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2036 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2037 | bytenr = cache->key.objectid; |
| 2038 | put_block_group(cache); |
| 2039 | |
| 2040 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2041 | } |
| 2042 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2043 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2044 | u64 bytenr, u64 num, int pin) |
| 2045 | { |
| 2046 | u64 len; |
| 2047 | struct btrfs_block_group_cache *cache; |
| 2048 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2049 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2050 | WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2051 | if (pin) { |
| 2052 | set_extent_dirty(&fs_info->pinned_extents, |
| 2053 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2054 | } else { |
| 2055 | clear_extent_dirty(&fs_info->pinned_extents, |
| 2056 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2057 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2058 | mutex_unlock(&root->fs_info->pinned_mutex); |
| 2059 | |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2060 | while (num > 0) { |
| 2061 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2062 | BUG_ON(!cache); |
| 2063 | len = min(num, cache->key.offset - |
| 2064 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2065 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2066 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2067 | spin_lock(&cache->lock); |
| 2068 | cache->pinned += len; |
| 2069 | cache->space_info->bytes_pinned += len; |
| 2070 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2071 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2072 | fs_info->total_pinned += len; |
| 2073 | } else { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2074 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2075 | spin_lock(&cache->lock); |
| 2076 | cache->pinned -= len; |
| 2077 | cache->space_info->bytes_pinned -= len; |
| 2078 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2079 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2080 | fs_info->total_pinned -= len; |
Josef Bacik | 07103a3 | 2008-11-19 15:17:55 -0500 | [diff] [blame] | 2081 | if (cache->cached) |
| 2082 | btrfs_add_free_space(cache, bytenr, len); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2083 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2084 | put_block_group(cache); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2085 | bytenr += len; |
| 2086 | num -= len; |
| 2087 | } |
| 2088 | return 0; |
| 2089 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2090 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2091 | static int update_reserved_extents(struct btrfs_root *root, |
| 2092 | u64 bytenr, u64 num, int reserve) |
| 2093 | { |
| 2094 | u64 len; |
| 2095 | struct btrfs_block_group_cache *cache; |
| 2096 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2097 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2098 | while (num > 0) { |
| 2099 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 2100 | BUG_ON(!cache); |
| 2101 | len = min(num, cache->key.offset - |
| 2102 | (bytenr - cache->key.objectid)); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2103 | |
| 2104 | spin_lock(&cache->space_info->lock); |
| 2105 | spin_lock(&cache->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2106 | if (reserve) { |
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 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2110 | cache->reserved -= len; |
| 2111 | cache->space_info->bytes_reserved -= len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2112 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2113 | spin_unlock(&cache->lock); |
| 2114 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2115 | put_block_group(cache); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2116 | bytenr += len; |
| 2117 | num -= len; |
| 2118 | } |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2122 | 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] | 2123 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2124 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2125 | u64 start; |
| 2126 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2127 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2128 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2129 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2130 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2131 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2132 | ret = find_first_extent_bit(pinned_extents, last, |
| 2133 | &start, &end, EXTENT_DIRTY); |
| 2134 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2135 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2136 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 2137 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2138 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2139 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2140 | return 0; |
| 2141 | } |
| 2142 | |
| 2143 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 2144 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2145 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2146 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2147 | u64 start; |
| 2148 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2149 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2150 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2151 | while (1) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2152 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2153 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 2154 | EXTENT_DIRTY); |
| 2155 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2156 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2157 | |
| 2158 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 2159 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2160 | /* unlocks the pinned mutex */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2161 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2162 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2163 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2164 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2165 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2166 | mutex_unlock(&root->fs_info->pinned_mutex); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2167 | return ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2168 | } |
| 2169 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2170 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 2171 | struct btrfs_root *root, |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2172 | struct btrfs_path *path, |
| 2173 | u64 bytenr, u64 num_bytes, int is_data, |
| 2174 | struct extent_buffer **must_clean) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2175 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2176 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2177 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2178 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2179 | if (is_data) |
| 2180 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2181 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2182 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 2183 | if (!buf) |
| 2184 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2185 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2186 | /* we can reuse a block if it hasn't been written |
| 2187 | * and it is from this transaction. We can't |
| 2188 | * reuse anything from the tree log root because |
| 2189 | * it has tiny sub-transactions. |
| 2190 | */ |
| 2191 | if (btrfs_buffer_uptodate(buf, 0) && |
| 2192 | btrfs_try_tree_lock(buf)) { |
| 2193 | u64 header_owner = btrfs_header_owner(buf); |
| 2194 | u64 header_transid = btrfs_header_generation(buf); |
| 2195 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2196 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2197 | header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID && |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2198 | header_transid == trans->transid && |
| 2199 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2200 | *must_clean = buf; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2201 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2202 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2203 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2204 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2205 | free_extent_buffer(buf); |
| 2206 | pinit: |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2207 | btrfs_set_path_blocking(path); |
| 2208 | mutex_lock(&root->fs_info->pinned_mutex); |
| 2209 | /* unlocks the pinned mutex */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2210 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 2211 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2212 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2213 | return 0; |
| 2214 | } |
| 2215 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2216 | /* |
| 2217 | * remove an extent from the root, returns 0 on success |
| 2218 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2219 | static int __free_extent(struct btrfs_trans_handle *trans, |
| 2220 | struct btrfs_root *root, |
| 2221 | u64 bytenr, u64 num_bytes, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2222 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2223 | u64 owner_objectid, int pin, int mark_free, |
| 2224 | int refs_to_drop) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2225 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2226 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2227 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2228 | struct btrfs_fs_info *info = root->fs_info; |
| 2229 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2230 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2231 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2232 | int extent_slot = 0; |
| 2233 | int found_extent = 0; |
| 2234 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2235 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2236 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2237 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2238 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2239 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2240 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2241 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2242 | if (!path) |
| 2243 | return -ENOMEM; |
| 2244 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2245 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2246 | path->leave_spinning = 1; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2247 | ret = lookup_extent_backref(trans, extent_root, path, |
| 2248 | bytenr, parent, root_objectid, |
| 2249 | ref_generation, owner_objectid, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2250 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2251 | struct btrfs_key found_key; |
| 2252 | extent_slot = path->slots[0]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2253 | while (extent_slot > 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2254 | extent_slot--; |
| 2255 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2256 | extent_slot); |
| 2257 | if (found_key.objectid != bytenr) |
| 2258 | break; |
| 2259 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2260 | found_key.offset == num_bytes) { |
| 2261 | found_extent = 1; |
| 2262 | break; |
| 2263 | } |
| 2264 | if (path->slots[0] - extent_slot > 5) |
| 2265 | break; |
| 2266 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2267 | if (!found_extent) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2268 | ret = remove_extent_backref(trans, extent_root, path, |
| 2269 | refs_to_drop); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2270 | BUG_ON(ret); |
| 2271 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2272 | path->leave_spinning = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2273 | ret = btrfs_search_slot(trans, extent_root, |
| 2274 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2275 | if (ret) { |
| 2276 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2277 | ", was looking for %llu\n", ret, |
| 2278 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2279 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2280 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2281 | BUG_ON(ret); |
| 2282 | extent_slot = path->slots[0]; |
| 2283 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2284 | } else { |
| 2285 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2286 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2287 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2288 | "parent %llu root %llu gen %llu owner %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2289 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2290 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2291 | (unsigned long long)root_objectid, |
| 2292 | (unsigned long long)ref_generation, |
| 2293 | (unsigned long long)owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2294 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2295 | |
| 2296 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2297 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2298 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2299 | refs = btrfs_extent_refs(leaf, ei); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2300 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2301 | /* |
| 2302 | * we're not allowed to delete the extent item if there |
| 2303 | * are other delayed ref updates pending |
| 2304 | */ |
| 2305 | |
| 2306 | BUG_ON(refs < refs_to_drop); |
| 2307 | refs -= refs_to_drop; |
| 2308 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2309 | btrfs_mark_buffer_dirty(leaf); |
| 2310 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2311 | if (refs == 0 && found_extent && |
| 2312 | path->slots[0] == extent_slot + 1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2313 | struct btrfs_extent_ref *ref; |
| 2314 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 2315 | struct btrfs_extent_ref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2316 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2317 | /* if the back ref and the extent are next to each other |
| 2318 | * they get deleted below in one shot |
| 2319 | */ |
| 2320 | path->slots[0] = extent_slot; |
| 2321 | num_to_del = 2; |
| 2322 | } else if (found_extent) { |
| 2323 | /* otherwise delete the extent back ref */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2324 | ret = remove_extent_backref(trans, extent_root, path, |
| 2325 | refs_to_drop); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2326 | BUG_ON(ret); |
| 2327 | /* if refs are 0, we need to setup the path for deletion */ |
| 2328 | if (refs == 0) { |
| 2329 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2330 | path->leave_spinning = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2331 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 2332 | -1, 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2333 | BUG_ON(ret); |
| 2334 | } |
| 2335 | } |
| 2336 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2337 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2338 | u64 super_used; |
| 2339 | u64 root_used; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2340 | struct extent_buffer *must_clean = NULL; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2341 | |
| 2342 | if (pin) { |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2343 | ret = pin_down_bytes(trans, root, path, |
| 2344 | bytenr, num_bytes, |
| 2345 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID, |
| 2346 | &must_clean); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 2347 | if (ret > 0) |
| 2348 | mark_free = 1; |
| 2349 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2350 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2351 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2352 | /* block accounting for super block */ |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2353 | spin_lock(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2354 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2355 | btrfs_set_super_bytes_used(&info->super_copy, |
| 2356 | super_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2357 | |
| 2358 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2359 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2360 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2361 | root_used - num_bytes); |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 2362 | spin_unlock(&info->delalloc_lock); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2363 | |
| 2364 | /* |
| 2365 | * it is going to be very rare for someone to be waiting |
| 2366 | * on the block we're freeing. del_items might need to |
| 2367 | * schedule, so rather than get fancy, just force it |
| 2368 | * to blocking here |
| 2369 | */ |
| 2370 | if (must_clean) |
| 2371 | btrfs_set_lock_blocking(must_clean); |
| 2372 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2373 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 2374 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2375 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2376 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2377 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2378 | if (must_clean) { |
| 2379 | clean_tree_block(NULL, root, must_clean); |
| 2380 | btrfs_tree_unlock(must_clean); |
| 2381 | free_extent_buffer(must_clean); |
| 2382 | } |
| 2383 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2384 | if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 2385 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 2386 | BUG_ON(ret); |
| 2387 | } |
| 2388 | |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 2389 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
| 2390 | mark_free); |
| 2391 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2392 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2393 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2394 | return ret; |
| 2395 | } |
| 2396 | |
| 2397 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2398 | * remove an extent from the root, returns 0 on success |
| 2399 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2400 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2401 | struct btrfs_root *root, |
| 2402 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2403 | u64 root_objectid, u64 ref_generation, |
| 2404 | u64 owner_objectid, int pin, |
| 2405 | int refs_to_drop) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2406 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2407 | WARN_ON(num_bytes < root->sectorsize); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2408 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2409 | /* |
| 2410 | * if metadata always pin |
| 2411 | * if data pin when any transaction has committed this |
| 2412 | */ |
| 2413 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID || |
| 2414 | ref_generation != trans->transid) |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2415 | pin = 1; |
| 2416 | |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2417 | if (ref_generation != trans->transid) |
| 2418 | pin = 1; |
| 2419 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2420 | return __free_extent(trans, root, bytenr, num_bytes, parent, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2421 | root_objectid, ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2422 | owner_objectid, pin, pin == 0, refs_to_drop); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2423 | } |
| 2424 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2425 | /* |
| 2426 | * when we free an extent, it is possible (and likely) that we free the last |
| 2427 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 2428 | * a given extent, and if there are no other delayed refs to be processed, it |
| 2429 | * removes it from the tree. |
| 2430 | */ |
| 2431 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 2432 | struct btrfs_root *root, u64 bytenr) |
| 2433 | { |
| 2434 | struct btrfs_delayed_ref_head *head; |
| 2435 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2436 | struct btrfs_delayed_ref_node *ref; |
| 2437 | struct rb_node *node; |
| 2438 | int ret; |
| 2439 | |
| 2440 | delayed_refs = &trans->transaction->delayed_refs; |
| 2441 | spin_lock(&delayed_refs->lock); |
| 2442 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2443 | if (!head) |
| 2444 | goto out; |
| 2445 | |
| 2446 | node = rb_prev(&head->node.rb_node); |
| 2447 | if (!node) |
| 2448 | goto out; |
| 2449 | |
| 2450 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2451 | |
| 2452 | /* there are still entries for this ref, we can't drop it */ |
| 2453 | if (ref->bytenr == bytenr) |
| 2454 | goto out; |
| 2455 | |
| 2456 | /* |
| 2457 | * waiting for the lock here would deadlock. If someone else has it |
| 2458 | * locked they are already in the process of dropping it anyway |
| 2459 | */ |
| 2460 | if (!mutex_trylock(&head->mutex)) |
| 2461 | goto out; |
| 2462 | |
| 2463 | /* |
| 2464 | * at this point we have a head with no other entries. Go |
| 2465 | * ahead and process it. |
| 2466 | */ |
| 2467 | head->node.in_tree = 0; |
| 2468 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2469 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2470 | delayed_refs->num_entries--; |
| 2471 | |
| 2472 | /* |
| 2473 | * we don't take a ref on the node because we're removing it from the |
| 2474 | * tree, so we just steal the ref the tree was holding. |
| 2475 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2476 | delayed_refs->num_heads--; |
| 2477 | if (list_empty(&head->cluster)) |
| 2478 | delayed_refs->num_heads_ready--; |
| 2479 | |
| 2480 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2481 | spin_unlock(&delayed_refs->lock); |
| 2482 | |
| 2483 | ret = run_one_delayed_ref(trans, root->fs_info->tree_root, |
| 2484 | &head->node, head->must_insert_reserved); |
| 2485 | BUG_ON(ret); |
| 2486 | btrfs_put_delayed_ref(&head->node); |
| 2487 | return 0; |
| 2488 | out: |
| 2489 | spin_unlock(&delayed_refs->lock); |
| 2490 | return 0; |
| 2491 | } |
| 2492 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2493 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2494 | struct btrfs_root *root, |
| 2495 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2496 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2497 | u64 owner_objectid, int pin) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2498 | { |
| 2499 | int ret; |
| 2500 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2501 | /* |
| 2502 | * tree log blocks never actually go into the extent allocation |
| 2503 | * tree, just update pinning info and exit early. |
| 2504 | * |
| 2505 | * data extents referenced by the tree log do need to have |
| 2506 | * their reference counts bumped. |
| 2507 | */ |
| 2508 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID && |
| 2509 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2510 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2511 | |
| 2512 | /* unlocks the pinned mutex */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2513 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2514 | update_reserved_extents(root, bytenr, num_bytes, 0); |
| 2515 | ret = 0; |
| 2516 | } else { |
| 2517 | ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, |
| 2518 | root_objectid, ref_generation, |
| 2519 | owner_objectid, |
| 2520 | BTRFS_DROP_DELAYED_REF, 1); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2521 | BUG_ON(ret); |
| 2522 | ret = check_ref_cleanup(trans, root, bytenr); |
| 2523 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2524 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2525 | return ret; |
| 2526 | } |
| 2527 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2528 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 2529 | { |
| 2530 | u64 mask = ((u64)root->stripesize - 1); |
| 2531 | u64 ret = (val + mask) & ~mask; |
| 2532 | return ret; |
| 2533 | } |
| 2534 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2535 | /* |
| 2536 | * walks the btree of allocated extents and find a hole of a given size. |
| 2537 | * The key ins is changed to record the hole: |
| 2538 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2539 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2540 | * ins->offset == number of blocks |
| 2541 | * Any available blocks before search_start are skipped. |
| 2542 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2543 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2544 | struct btrfs_root *orig_root, |
| 2545 | u64 num_bytes, u64 empty_size, |
| 2546 | u64 search_start, u64 search_end, |
| 2547 | u64 hint_byte, struct btrfs_key *ins, |
| 2548 | u64 exclude_start, u64 exclude_nr, |
| 2549 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2550 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2551 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2552 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2553 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2554 | u64 *last_ptr = NULL; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2555 | u64 last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2556 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2557 | int chunk_alloc_done = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2558 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2559 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2560 | struct list_head *head = NULL, *cur = NULL; |
| 2561 | int loop = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2562 | int extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2563 | struct btrfs_space_info *space_info; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2564 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2565 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2566 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2567 | ins->objectid = 0; |
| 2568 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2569 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2570 | if (orig_root->ref_cows || empty_size) |
| 2571 | allowed_chunk_alloc = 1; |
| 2572 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2573 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 2574 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 2575 | if (!btrfs_test_opt(root, SSD)) |
| 2576 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2577 | } |
| 2578 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2579 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2580 | last_ptr = &root->fs_info->last_data_alloc; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2581 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2582 | if (last_ptr) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2583 | if (*last_ptr) { |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2584 | hint_byte = *last_ptr; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2585 | last_wanted = *last_ptr; |
| 2586 | } else |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2587 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2588 | } else { |
| 2589 | empty_cluster = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2590 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2591 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2592 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2593 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2594 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2595 | last_wanted = 0; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2596 | empty_size += empty_cluster; |
| 2597 | } |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2598 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2599 | total_needed += empty_size; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2600 | block_group = btrfs_lookup_block_group(root->fs_info, search_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2601 | if (!block_group) |
| 2602 | block_group = btrfs_lookup_first_block_group(root->fs_info, |
| 2603 | search_start); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2604 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2605 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2606 | down_read(&space_info->groups_sem); |
| 2607 | while (1) { |
| 2608 | struct btrfs_free_space *free_space; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2609 | /* |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2610 | * the only way this happens if our hint points to a block |
| 2611 | * group thats not of the proper type, while looping this |
| 2612 | * should never happen |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2613 | */ |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2614 | if (empty_size) |
| 2615 | extra_loop = 1; |
| 2616 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2617 | if (!block_group) |
| 2618 | goto new_group_no_lock; |
| 2619 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 2620 | if (unlikely(!block_group->cached)) { |
| 2621 | mutex_lock(&block_group->cache_mutex); |
| 2622 | ret = cache_block_group(root, block_group); |
| 2623 | mutex_unlock(&block_group->cache_mutex); |
| 2624 | if (ret) |
| 2625 | break; |
| 2626 | } |
| 2627 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2628 | mutex_lock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2629 | if (unlikely(!block_group_bits(block_group, data))) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2630 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2631 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 2632 | if (unlikely(block_group->ro)) |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2633 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2634 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2635 | free_space = btrfs_find_free_space(block_group, search_start, |
| 2636 | total_needed); |
| 2637 | if (free_space) { |
| 2638 | u64 start = block_group->key.objectid; |
| 2639 | u64 end = block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2640 | block_group->key.offset; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2641 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2642 | search_start = stripe_align(root, free_space->offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2643 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2644 | /* move on to the next group */ |
| 2645 | if (search_start + num_bytes >= search_end) |
| 2646 | goto new_group; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2647 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2648 | /* move on to the next group */ |
| 2649 | if (search_start + num_bytes > end) |
| 2650 | goto new_group; |
| 2651 | |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2652 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 2653 | total_needed += empty_cluster; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2654 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2655 | last_wanted = 0; |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 2656 | /* |
| 2657 | * if search_start is still in this block group |
| 2658 | * then we just re-search this block group |
| 2659 | */ |
| 2660 | if (search_start >= start && |
| 2661 | search_start < end) { |
| 2662 | mutex_unlock(&block_group->alloc_mutex); |
| 2663 | continue; |
| 2664 | } |
| 2665 | |
| 2666 | /* else we go to the next block group */ |
| 2667 | goto new_group; |
| 2668 | } |
| 2669 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2670 | if (exclude_nr > 0 && |
| 2671 | (search_start + num_bytes > exclude_start && |
| 2672 | search_start < exclude_start + exclude_nr)) { |
| 2673 | search_start = exclude_start + exclude_nr; |
| 2674 | /* |
| 2675 | * if search_start is still in this block group |
| 2676 | * then we just re-search this block group |
| 2677 | */ |
| 2678 | if (search_start >= start && |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2679 | search_start < end) { |
| 2680 | mutex_unlock(&block_group->alloc_mutex); |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2681 | last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2682 | continue; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2683 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2684 | |
| 2685 | /* else we go to the next block group */ |
| 2686 | goto new_group; |
| 2687 | } |
| 2688 | |
| 2689 | ins->objectid = search_start; |
| 2690 | ins->offset = num_bytes; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2691 | |
| 2692 | btrfs_remove_free_space_lock(block_group, search_start, |
| 2693 | num_bytes); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2694 | /* we are all good, lets return */ |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2695 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2696 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2697 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2698 | new_group: |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2699 | mutex_unlock(&block_group->alloc_mutex); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2700 | put_block_group(block_group); |
| 2701 | block_group = NULL; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2702 | new_group_no_lock: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2703 | /* don't try to compare new allocations against the |
| 2704 | * last allocation any more |
| 2705 | */ |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2706 | last_wanted = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2707 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2708 | /* |
| 2709 | * Here's how this works. |
| 2710 | * loop == 0: we were searching a block group via a hint |
| 2711 | * and didn't find anything, so we start at |
| 2712 | * the head of the block groups and keep searching |
| 2713 | * loop == 1: we're searching through all of the block groups |
| 2714 | * if we hit the head again we have searched |
| 2715 | * all of the block groups for this space and we |
| 2716 | * need to try and allocate, if we cant error out. |
| 2717 | * loop == 2: we allocated more space and are looping through |
| 2718 | * all of the block groups again. |
| 2719 | */ |
| 2720 | if (loop == 0) { |
| 2721 | head = &space_info->block_groups; |
| 2722 | cur = head->next; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2723 | loop++; |
| 2724 | } else if (loop == 1 && cur == head) { |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2725 | int keep_going; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2726 | |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2727 | /* at this point we give up on the empty_size |
| 2728 | * allocations and just try to allocate the min |
| 2729 | * space. |
| 2730 | * |
| 2731 | * The extra_loop field was set if an empty_size |
| 2732 | * allocation was attempted above, and if this |
| 2733 | * is try we need to try the loop again without |
| 2734 | * the additional empty_size. |
| 2735 | */ |
Chris Mason | 5b7c3fc | 2008-11-10 07:26:33 -0500 | [diff] [blame] | 2736 | total_needed -= empty_size; |
| 2737 | empty_size = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2738 | keep_going = extra_loop; |
| 2739 | loop++; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2740 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2741 | if (allowed_chunk_alloc && !chunk_alloc_done) { |
| 2742 | up_read(&space_info->groups_sem); |
| 2743 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 2744 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2745 | down_read(&space_info->groups_sem); |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 2746 | if (ret < 0) |
| 2747 | goto loop_check; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2748 | head = &space_info->block_groups; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2749 | /* |
| 2750 | * we've allocated a new chunk, keep |
| 2751 | * trying |
| 2752 | */ |
| 2753 | keep_going = 1; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2754 | chunk_alloc_done = 1; |
| 2755 | } else if (!allowed_chunk_alloc) { |
| 2756 | space_info->force_alloc = 1; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2757 | } |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 2758 | loop_check: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2759 | if (keep_going) { |
| 2760 | cur = head->next; |
| 2761 | extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2762 | } else { |
| 2763 | break; |
| 2764 | } |
| 2765 | } else if (cur == head) { |
| 2766 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2767 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2768 | |
| 2769 | block_group = list_entry(cur, struct btrfs_block_group_cache, |
| 2770 | list); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2771 | atomic_inc(&block_group->count); |
| 2772 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2773 | search_start = block_group->key.objectid; |
| 2774 | cur = cur->next; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 2775 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2776 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2777 | /* we found what we needed */ |
| 2778 | if (ins->objectid) { |
| 2779 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2780 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2781 | |
| 2782 | if (last_ptr) |
| 2783 | *last_ptr = ins->objectid + ins->offset; |
| 2784 | ret = 0; |
| 2785 | } else if (!ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2786 | printk(KERN_ERR "btrfs searching for %llu bytes, " |
| 2787 | "num_bytes %llu, loop %d, allowed_alloc %d\n", |
| 2788 | (unsigned long long)total_needed, |
| 2789 | (unsigned long long)num_bytes, |
Josef Bacik | 4ce4cb5 | 2008-11-17 21:12:00 -0500 | [diff] [blame] | 2790 | loop, allowed_chunk_alloc); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2791 | ret = -ENOSPC; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2792 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2793 | if (block_group) |
| 2794 | put_block_group(block_group); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2795 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2796 | up_read(&space_info->groups_sem); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2797 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2798 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2799 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2800 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 2801 | { |
| 2802 | struct btrfs_block_group_cache *cache; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2803 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2804 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 2805 | (unsigned long long)(info->total_bytes - info->bytes_used - |
| 2806 | info->bytes_pinned - info->bytes_reserved), |
| 2807 | (info->full) ? "" : "not "); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2808 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," |
| 2809 | " may_use=%llu, used=%llu\n", info->total_bytes, |
| 2810 | info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use, |
| 2811 | info->bytes_used); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2812 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2813 | down_read(&info->groups_sem); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 2814 | list_for_each_entry(cache, &info->block_groups, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2815 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2816 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 2817 | "%llu pinned %llu reserved\n", |
| 2818 | (unsigned long long)cache->key.objectid, |
| 2819 | (unsigned long long)cache->key.offset, |
| 2820 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 2821 | (unsigned long long)cache->pinned, |
| 2822 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2823 | btrfs_dump_free_space(cache, bytes); |
| 2824 | spin_unlock(&cache->lock); |
| 2825 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2826 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2827 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2828 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2829 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2830 | struct btrfs_root *root, |
| 2831 | u64 num_bytes, u64 min_alloc_size, |
| 2832 | u64 empty_size, u64 hint_byte, |
| 2833 | u64 search_end, struct btrfs_key *ins, |
| 2834 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2835 | { |
| 2836 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 2837 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2838 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2839 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2840 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2841 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2842 | /* |
| 2843 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 2844 | * is not called recursively on allocations |
| 2845 | */ |
| 2846 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2847 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2848 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2849 | 2 * 1024 * 1024, |
| 2850 | BTRFS_BLOCK_GROUP_METADATA | |
| 2851 | (info->metadata_alloc_profile & |
| 2852 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2853 | } |
| 2854 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2855 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2856 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2857 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2858 | WARN_ON(num_bytes < root->sectorsize); |
| 2859 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 2860 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2861 | trans->alloc_exclude_start, |
| 2862 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2863 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2864 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 2865 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2866 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2867 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2868 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2869 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2870 | goto again; |
| 2871 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2872 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2873 | struct btrfs_space_info *sinfo; |
| 2874 | |
| 2875 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2876 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 2877 | "wanted %llu\n", (unsigned long long)data, |
| 2878 | (unsigned long long)num_bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2879 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2880 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2881 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2882 | |
| 2883 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2884 | } |
| 2885 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2886 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 2887 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2888 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2889 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2890 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2891 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2892 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2893 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 2894 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2895 | return -ENOSPC; |
| 2896 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2897 | |
| 2898 | ret = btrfs_discard_extent(root, start, len); |
| 2899 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2900 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2901 | put_block_group(cache); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2902 | update_reserved_extents(root, start, len, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2903 | |
| 2904 | return ret; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2905 | } |
| 2906 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2907 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2908 | struct btrfs_root *root, |
| 2909 | u64 num_bytes, u64 min_alloc_size, |
| 2910 | u64 empty_size, u64 hint_byte, |
| 2911 | u64 search_end, struct btrfs_key *ins, |
| 2912 | u64 data) |
| 2913 | { |
| 2914 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2915 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 2916 | empty_size, hint_byte, search_end, ins, |
| 2917 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2918 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2919 | return ret; |
| 2920 | } |
| 2921 | |
| 2922 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2923 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2924 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2925 | u64 owner, struct btrfs_key *ins, |
| 2926 | int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2927 | { |
| 2928 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2929 | u64 super_used; |
| 2930 | u64 root_used; |
| 2931 | u64 num_bytes = ins->offset; |
| 2932 | u32 sizes[2]; |
| 2933 | struct btrfs_fs_info *info = root->fs_info; |
| 2934 | struct btrfs_root *extent_root = info->extent_root; |
| 2935 | struct btrfs_extent_item *extent_item; |
| 2936 | struct btrfs_extent_ref *ref; |
| 2937 | struct btrfs_path *path; |
| 2938 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2939 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2940 | if (parent == 0) |
| 2941 | parent = ins->objectid; |
| 2942 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2943 | /* block accounting for super block */ |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2944 | spin_lock(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2945 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2946 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2947 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2948 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2949 | root_used = btrfs_root_used(&root->root_item); |
| 2950 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 2951 | spin_unlock(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2952 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2953 | memcpy(&keys[0], ins, sizeof(*ins)); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2954 | keys[1].objectid = ins->objectid; |
| 2955 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2956 | keys[1].offset = parent; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2957 | sizes[0] = sizeof(*extent_item); |
| 2958 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2959 | |
| 2960 | path = btrfs_alloc_path(); |
| 2961 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2962 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 2963 | path->leave_spinning = 1; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2964 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 2965 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2966 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2967 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2968 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2969 | struct btrfs_extent_item); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2970 | btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2971 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 2972 | struct btrfs_extent_ref); |
| 2973 | |
| 2974 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 2975 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 2976 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2977 | btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2978 | |
| 2979 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2980 | |
| 2981 | trans->alloc_exclude_start = 0; |
| 2982 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2983 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2984 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2985 | if (ret) |
| 2986 | goto out; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2987 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2988 | ret = update_block_group(trans, root, ins->objectid, |
| 2989 | ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 2990 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2991 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 2992 | "%llu\n", (unsigned long long)ins->objectid, |
| 2993 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 2994 | BUG(); |
| 2995 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2996 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2997 | return ret; |
| 2998 | } |
| 2999 | |
| 3000 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3001 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3002 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3003 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3004 | { |
| 3005 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 3006 | |
| 3007 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 3008 | return 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3009 | |
| 3010 | ret = btrfs_add_delayed_ref(trans, ins->objectid, |
| 3011 | ins->offset, parent, root_objectid, |
| 3012 | ref_generation, owner, |
| 3013 | BTRFS_ADD_DELAYED_EXTENT, 0); |
| 3014 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3015 | return ret; |
| 3016 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3017 | |
| 3018 | /* |
| 3019 | * this is used by the tree logging recovery code. It records that |
| 3020 | * an extent has been allocated and makes sure to clear the free |
| 3021 | * space cache bits as well |
| 3022 | */ |
| 3023 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3024 | struct btrfs_root *root, u64 parent, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3025 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3026 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3027 | { |
| 3028 | int ret; |
| 3029 | struct btrfs_block_group_cache *block_group; |
| 3030 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3031 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3032 | mutex_lock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3033 | cache_block_group(root, block_group); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3034 | mutex_unlock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3035 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3036 | ret = btrfs_remove_free_space(block_group, ins->objectid, |
| 3037 | ins->offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3038 | BUG_ON(ret); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3039 | put_block_group(block_group); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3040 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3041 | ref_generation, owner, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3042 | return ret; |
| 3043 | } |
| 3044 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3045 | /* |
| 3046 | * finds a free extent and does all the dirty work required for allocation |
| 3047 | * returns the key for the extent through ins, and a tree buffer for |
| 3048 | * the first block of the extent through buf. |
| 3049 | * |
| 3050 | * returns 0 if everything worked, non-zero otherwise. |
| 3051 | */ |
| 3052 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 3053 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3054 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3055 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3056 | u64 owner_objectid, u64 empty_size, u64 hint_byte, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3057 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 3058 | { |
| 3059 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3060 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 3061 | min_alloc_size, empty_size, hint_byte, |
| 3062 | search_end, ins, data); |
| 3063 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3064 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3065 | ret = btrfs_add_delayed_ref(trans, ins->objectid, |
| 3066 | ins->offset, parent, root_objectid, |
| 3067 | ref_generation, owner_objectid, |
| 3068 | BTRFS_ADD_DELAYED_EXTENT, 0); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3069 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3070 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3071 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3072 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3073 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3074 | |
| 3075 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 3076 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3077 | u64 bytenr, u32 blocksize, |
| 3078 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3079 | { |
| 3080 | struct extent_buffer *buf; |
| 3081 | |
| 3082 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 3083 | if (!buf) |
| 3084 | return ERR_PTR(-ENOMEM); |
| 3085 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3086 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3087 | btrfs_tree_lock(buf); |
| 3088 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3089 | |
| 3090 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3091 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3092 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3093 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 3094 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3095 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3096 | } else { |
| 3097 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 3098 | buf->start + buf->len - 1, GFP_NOFS); |
| 3099 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3100 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3101 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3102 | return buf; |
| 3103 | } |
| 3104 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3105 | /* |
| 3106 | * helper function to allocate a block for a given tree |
| 3107 | * returns the tree buffer or NULL. |
| 3108 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3109 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3110 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3111 | u32 blocksize, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3112 | u64 root_objectid, |
| 3113 | u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3114 | int level, |
| 3115 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3116 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3117 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3118 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3119 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3120 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3121 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3122 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3123 | root_objectid, ref_generation, level, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3124 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3125 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3126 | BUG_ON(ret > 0); |
| 3127 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3128 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 3129 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3130 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 3131 | blocksize, level); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3132 | return buf; |
| 3133 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3134 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3135 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3136 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3137 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3138 | u64 leaf_owner; |
| 3139 | u64 leaf_generation; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3140 | struct refsort *sorted; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3141 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3142 | struct btrfs_file_extent_item *fi; |
| 3143 | int i; |
| 3144 | int nritems; |
| 3145 | int ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3146 | int refi = 0; |
| 3147 | int slot; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3148 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3149 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 3150 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3151 | leaf_owner = btrfs_header_owner(leaf); |
| 3152 | leaf_generation = btrfs_header_generation(leaf); |
| 3153 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3154 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); |
| 3155 | /* we do this loop twice. The first time we build a list |
| 3156 | * of the extents we have a reference on, then we sort the list |
| 3157 | * by bytenr. The second time around we actually do the |
| 3158 | * extent freeing. |
| 3159 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3160 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3161 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 3162 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3163 | |
| 3164 | btrfs_item_key_to_cpu(leaf, &key, i); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3165 | |
| 3166 | /* only extents have references, skip everything else */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3167 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3168 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3169 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3170 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3171 | |
| 3172 | /* inline extents live in the btree, they don't have refs */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3173 | if (btrfs_file_extent_type(leaf, fi) == |
| 3174 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 3175 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3176 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3177 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3178 | |
| 3179 | /* holes don't have refs */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3180 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 3181 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3182 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3183 | sorted[refi].bytenr = disk_bytenr; |
| 3184 | sorted[refi].slot = i; |
| 3185 | refi++; |
| 3186 | } |
| 3187 | |
| 3188 | if (refi == 0) |
| 3189 | goto out; |
| 3190 | |
| 3191 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 3192 | |
| 3193 | for (i = 0; i < refi; i++) { |
| 3194 | u64 disk_bytenr; |
| 3195 | |
| 3196 | disk_bytenr = sorted[i].bytenr; |
| 3197 | slot = sorted[i].slot; |
| 3198 | |
| 3199 | cond_resched(); |
| 3200 | |
| 3201 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 3202 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 3203 | continue; |
| 3204 | |
| 3205 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 3206 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3207 | ret = btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3208 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3209 | leaf->start, leaf_owner, leaf_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3210 | key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3211 | BUG_ON(ret); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3212 | |
| 3213 | atomic_inc(&root->fs_info->throttle_gen); |
| 3214 | wake_up(&root->fs_info->transaction_throttle); |
| 3215 | cond_resched(); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3216 | } |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3217 | out: |
| 3218 | kfree(sorted); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3219 | return 0; |
| 3220 | } |
| 3221 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3222 | static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3223 | struct btrfs_root *root, |
| 3224 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3225 | { |
| 3226 | int i; |
| 3227 | int ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3228 | struct btrfs_extent_info *info; |
| 3229 | struct refsort *sorted; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3230 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3231 | if (ref->nritems == 0) |
| 3232 | return 0; |
| 3233 | |
| 3234 | sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3235 | for (i = 0; i < ref->nritems; i++) { |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3236 | sorted[i].bytenr = ref->extents[i].bytenr; |
| 3237 | sorted[i].slot = i; |
| 3238 | } |
| 3239 | sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL); |
| 3240 | |
| 3241 | /* |
| 3242 | * the items in the ref were sorted when the ref was inserted |
| 3243 | * into the ref cache, so this is already in order |
| 3244 | */ |
| 3245 | for (i = 0; i < ref->nritems; i++) { |
| 3246 | info = ref->extents + sorted[i].slot; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3247 | ret = btrfs_free_extent(trans, root, info->bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3248 | info->num_bytes, ref->bytenr, |
| 3249 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3250 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3251 | |
| 3252 | atomic_inc(&root->fs_info->throttle_gen); |
| 3253 | wake_up(&root->fs_info->transaction_throttle); |
| 3254 | cond_resched(); |
| 3255 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3256 | BUG_ON(ret); |
| 3257 | info++; |
| 3258 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3259 | |
Chris Mason | 806638b | 2009-02-05 09:08:14 -0500 | [diff] [blame] | 3260 | kfree(sorted); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3261 | return 0; |
| 3262 | } |
| 3263 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3264 | static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans, |
| 3265 | struct btrfs_root *root, u64 start, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3266 | u64 len, u32 *refs) |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3267 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3268 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3269 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3270 | ret = btrfs_lookup_extent_ref(trans, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3271 | BUG_ON(ret); |
| 3272 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3273 | #if 0 /* some debugging code in case we see problems here */ |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3274 | /* if the refs count is one, it won't get increased again. But |
| 3275 | * if the ref count is > 1, someone may be decreasing it at |
| 3276 | * the same time we are. |
| 3277 | */ |
| 3278 | if (*refs != 1) { |
| 3279 | struct extent_buffer *eb = NULL; |
| 3280 | eb = btrfs_find_create_tree_block(root, start, len); |
| 3281 | if (eb) |
| 3282 | btrfs_tree_lock(eb); |
| 3283 | |
| 3284 | mutex_lock(&root->fs_info->alloc_mutex); |
| 3285 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 3286 | BUG_ON(ret); |
| 3287 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 3288 | |
| 3289 | if (eb) { |
| 3290 | btrfs_tree_unlock(eb); |
| 3291 | free_extent_buffer(eb); |
| 3292 | } |
| 3293 | if (*refs == 1) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3294 | printk(KERN_ERR "btrfs block %llu went down to one " |
| 3295 | "during drop_snap\n", (unsigned long long)start); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | } |
| 3299 | #endif |
| 3300 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3301 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3302 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | /* |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3306 | * this is used while deleting old snapshots, and it drops the refs |
| 3307 | * on a whole subtree starting from a level 1 node. |
| 3308 | * |
| 3309 | * The idea is to sort all the leaf pointers, and then drop the |
| 3310 | * ref on all the leaves in order. Most of the time the leaves |
| 3311 | * will have ref cache entries, so no leaf IOs will be required to |
| 3312 | * find the extents they have references on. |
| 3313 | * |
| 3314 | * For each leaf, any references it has are also dropped in order |
| 3315 | * |
| 3316 | * This ends up dropping the references in something close to optimal |
| 3317 | * order for reading and modifying the extent allocation tree. |
| 3318 | */ |
| 3319 | static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans, |
| 3320 | struct btrfs_root *root, |
| 3321 | struct btrfs_path *path) |
| 3322 | { |
| 3323 | u64 bytenr; |
| 3324 | u64 root_owner; |
| 3325 | u64 root_gen; |
| 3326 | struct extent_buffer *eb = path->nodes[1]; |
| 3327 | struct extent_buffer *leaf; |
| 3328 | struct btrfs_leaf_ref *ref; |
| 3329 | struct refsort *sorted = NULL; |
| 3330 | int nritems = btrfs_header_nritems(eb); |
| 3331 | int ret; |
| 3332 | int i; |
| 3333 | int refi = 0; |
| 3334 | int slot = path->slots[1]; |
| 3335 | u32 blocksize = btrfs_level_size(root, 0); |
| 3336 | u32 refs; |
| 3337 | |
| 3338 | if (nritems == 0) |
| 3339 | goto out; |
| 3340 | |
| 3341 | root_owner = btrfs_header_owner(eb); |
| 3342 | root_gen = btrfs_header_generation(eb); |
| 3343 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); |
| 3344 | |
| 3345 | /* |
| 3346 | * step one, sort all the leaf pointers so we don't scribble |
| 3347 | * randomly into the extent allocation tree |
| 3348 | */ |
| 3349 | for (i = slot; i < nritems; i++) { |
| 3350 | sorted[refi].bytenr = btrfs_node_blockptr(eb, i); |
| 3351 | sorted[refi].slot = i; |
| 3352 | refi++; |
| 3353 | } |
| 3354 | |
| 3355 | /* |
| 3356 | * nritems won't be zero, but if we're picking up drop_snapshot |
| 3357 | * after a crash, slot might be > 0, so double check things |
| 3358 | * just in case. |
| 3359 | */ |
| 3360 | if (refi == 0) |
| 3361 | goto out; |
| 3362 | |
| 3363 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 3364 | |
| 3365 | /* |
| 3366 | * the first loop frees everything the leaves point to |
| 3367 | */ |
| 3368 | for (i = 0; i < refi; i++) { |
| 3369 | u64 ptr_gen; |
| 3370 | |
| 3371 | bytenr = sorted[i].bytenr; |
| 3372 | |
| 3373 | /* |
| 3374 | * check the reference count on this leaf. If it is > 1 |
| 3375 | * we just decrement it below and don't update any |
| 3376 | * of the refs the leaf points to. |
| 3377 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3378 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
| 3379 | blocksize, &refs); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3380 | BUG_ON(ret); |
| 3381 | if (refs != 1) |
| 3382 | continue; |
| 3383 | |
| 3384 | ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot); |
| 3385 | |
| 3386 | /* |
| 3387 | * the leaf only had one reference, which means the |
| 3388 | * only thing pointing to this leaf is the snapshot |
| 3389 | * we're deleting. It isn't possible for the reference |
| 3390 | * count to increase again later |
| 3391 | * |
| 3392 | * The reference cache is checked for the leaf, |
| 3393 | * and if found we'll be able to drop any refs held by |
| 3394 | * the leaf without needing to read it in. |
| 3395 | */ |
| 3396 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
| 3397 | if (ref && ref->generation != ptr_gen) { |
| 3398 | btrfs_free_leaf_ref(root, ref); |
| 3399 | ref = NULL; |
| 3400 | } |
| 3401 | if (ref) { |
| 3402 | ret = cache_drop_leaf_ref(trans, root, ref); |
| 3403 | BUG_ON(ret); |
| 3404 | btrfs_remove_leaf_ref(root, ref); |
| 3405 | btrfs_free_leaf_ref(root, ref); |
| 3406 | } else { |
| 3407 | /* |
| 3408 | * the leaf wasn't in the reference cache, so |
| 3409 | * we have to read it. |
| 3410 | */ |
| 3411 | leaf = read_tree_block(root, bytenr, blocksize, |
| 3412 | ptr_gen); |
| 3413 | ret = btrfs_drop_leaf_ref(trans, root, leaf); |
| 3414 | BUG_ON(ret); |
| 3415 | free_extent_buffer(leaf); |
| 3416 | } |
| 3417 | atomic_inc(&root->fs_info->throttle_gen); |
| 3418 | wake_up(&root->fs_info->transaction_throttle); |
| 3419 | cond_resched(); |
| 3420 | } |
| 3421 | |
| 3422 | /* |
| 3423 | * run through the loop again to free the refs on the leaves. |
| 3424 | * This is faster than doing it in the loop above because |
| 3425 | * the leaves are likely to be clustered together. We end up |
| 3426 | * working in nice chunks on the extent allocation tree. |
| 3427 | */ |
| 3428 | for (i = 0; i < refi; i++) { |
| 3429 | bytenr = sorted[i].bytenr; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3430 | ret = btrfs_free_extent(trans, root, bytenr, |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3431 | blocksize, eb->start, |
| 3432 | root_owner, root_gen, 0, 1); |
| 3433 | BUG_ON(ret); |
| 3434 | |
| 3435 | atomic_inc(&root->fs_info->throttle_gen); |
| 3436 | wake_up(&root->fs_info->transaction_throttle); |
| 3437 | cond_resched(); |
| 3438 | } |
| 3439 | out: |
| 3440 | kfree(sorted); |
| 3441 | |
| 3442 | /* |
| 3443 | * update the path to show we've processed the entire level 1 |
| 3444 | * node. This will get saved into the root's drop_snapshot_progress |
| 3445 | * field so these drops are not repeated again if this transaction |
| 3446 | * commits. |
| 3447 | */ |
| 3448 | path->slots[1] = nritems; |
| 3449 | return 0; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3453 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 3454 | * counts as it goes. |
| 3455 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3456 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3457 | struct btrfs_root *root, |
| 3458 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3459 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3460 | u64 root_owner; |
| 3461 | u64 root_gen; |
| 3462 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3463 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3464 | struct extent_buffer *next; |
| 3465 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3466 | struct extent_buffer *parent; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3467 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3468 | int ret; |
| 3469 | u32 refs; |
| 3470 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3471 | WARN_ON(*level < 0); |
| 3472 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3473 | ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3474 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3475 | BUG_ON(ret); |
| 3476 | if (refs > 1) |
| 3477 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3478 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3479 | /* |
| 3480 | * walk down to the last node level and free all the leaves |
| 3481 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3482 | while (*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3483 | WARN_ON(*level < 0); |
| 3484 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3485 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3486 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3487 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3488 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3489 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3490 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3491 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3492 | break; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3493 | |
| 3494 | /* the new code goes down to level 1 and does all the |
| 3495 | * leaves pointed to that node in bulk. So, this check |
| 3496 | * for level 0 will always be false. |
| 3497 | * |
| 3498 | * But, the disk format allows the drop_snapshot_progress |
| 3499 | * field in the root to leave things in a state where |
| 3500 | * a leaf will need cleaning up here. If someone crashes |
| 3501 | * with the old code and then boots with the new code, |
| 3502 | * we might find a leaf here. |
| 3503 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3504 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3505 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3506 | BUG_ON(ret); |
| 3507 | break; |
| 3508 | } |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3509 | |
| 3510 | /* |
| 3511 | * once we get to level one, process the whole node |
| 3512 | * at once, including everything below it. |
| 3513 | */ |
| 3514 | if (*level == 1) { |
| 3515 | ret = drop_level_one_refs(trans, root, path); |
| 3516 | BUG_ON(ret); |
| 3517 | break; |
| 3518 | } |
| 3519 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3520 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3521 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3522 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3523 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3524 | ret = drop_snap_lookup_refcount(trans, root, bytenr, |
| 3525 | blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3526 | BUG_ON(ret); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3527 | |
| 3528 | /* |
| 3529 | * if there is more than one reference, we don't need |
| 3530 | * to read that node to drop any references it has. We |
| 3531 | * just drop the ref we hold on that node and move on to the |
| 3532 | * next slot in this level. |
| 3533 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3534 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3535 | parent = path->nodes[*level]; |
| 3536 | root_owner = btrfs_header_owner(parent); |
| 3537 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3538 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3539 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3540 | ret = btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3541 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3542 | root_owner, root_gen, |
| 3543 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3544 | BUG_ON(ret); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3545 | |
| 3546 | atomic_inc(&root->fs_info->throttle_gen); |
| 3547 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3548 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3549 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3550 | continue; |
| 3551 | } |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3552 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3553 | /* |
| 3554 | * we need to keep freeing things in the next level down. |
| 3555 | * read the block and loop around to process it |
| 3556 | */ |
| 3557 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3558 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3559 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3560 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3561 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3562 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3563 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3564 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3565 | } |
| 3566 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3567 | WARN_ON(*level < 0); |
| 3568 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3569 | |
| 3570 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3571 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3572 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3573 | } else { |
| 3574 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3575 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3576 | } |
| 3577 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3578 | blocksize = btrfs_level_size(root, *level); |
| 3579 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3580 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3581 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3582 | /* |
| 3583 | * cleanup and free the reference on the last node |
| 3584 | * we processed |
| 3585 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 3586 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3587 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3588 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3589 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3590 | path->nodes[*level] = NULL; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3591 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3592 | *level += 1; |
| 3593 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3594 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3595 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3596 | return 0; |
| 3597 | } |
| 3598 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3599 | /* |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3600 | * helper function for drop_subtree, this function is similar to |
| 3601 | * walk_down_tree. The main difference is that it checks reference |
| 3602 | * counts while tree blocks are locked. |
| 3603 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3604 | static noinline int walk_down_subtree(struct btrfs_trans_handle *trans, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3605 | struct btrfs_root *root, |
| 3606 | struct btrfs_path *path, int *level) |
| 3607 | { |
| 3608 | struct extent_buffer *next; |
| 3609 | struct extent_buffer *cur; |
| 3610 | struct extent_buffer *parent; |
| 3611 | u64 bytenr; |
| 3612 | u64 ptr_gen; |
| 3613 | u32 blocksize; |
| 3614 | u32 refs; |
| 3615 | int ret; |
| 3616 | |
| 3617 | cur = path->nodes[*level]; |
| 3618 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, |
| 3619 | &refs); |
| 3620 | BUG_ON(ret); |
| 3621 | if (refs > 1) |
| 3622 | goto out; |
| 3623 | |
| 3624 | while (*level >= 0) { |
| 3625 | cur = path->nodes[*level]; |
| 3626 | if (*level == 0) { |
| 3627 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
| 3628 | BUG_ON(ret); |
| 3629 | clean_tree_block(trans, root, cur); |
| 3630 | break; |
| 3631 | } |
| 3632 | if (path->slots[*level] >= btrfs_header_nritems(cur)) { |
| 3633 | clean_tree_block(trans, root, cur); |
| 3634 | break; |
| 3635 | } |
| 3636 | |
| 3637 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 3638 | blocksize = btrfs_level_size(root, *level - 1); |
| 3639 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
| 3640 | |
| 3641 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
| 3642 | btrfs_tree_lock(next); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3643 | btrfs_set_lock_blocking(next); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3644 | |
| 3645 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, |
| 3646 | &refs); |
| 3647 | BUG_ON(ret); |
| 3648 | if (refs > 1) { |
| 3649 | parent = path->nodes[*level]; |
| 3650 | ret = btrfs_free_extent(trans, root, bytenr, |
| 3651 | blocksize, parent->start, |
| 3652 | btrfs_header_owner(parent), |
| 3653 | btrfs_header_generation(parent), |
| 3654 | *level - 1, 1); |
| 3655 | BUG_ON(ret); |
| 3656 | path->slots[*level]++; |
| 3657 | btrfs_tree_unlock(next); |
| 3658 | free_extent_buffer(next); |
| 3659 | continue; |
| 3660 | } |
| 3661 | |
| 3662 | *level = btrfs_header_level(next); |
| 3663 | path->nodes[*level] = next; |
| 3664 | path->slots[*level] = 0; |
| 3665 | path->locks[*level] = 1; |
| 3666 | cond_resched(); |
| 3667 | } |
| 3668 | out: |
| 3669 | parent = path->nodes[*level + 1]; |
| 3670 | bytenr = path->nodes[*level]->start; |
| 3671 | blocksize = path->nodes[*level]->len; |
| 3672 | |
| 3673 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
| 3674 | parent->start, btrfs_header_owner(parent), |
| 3675 | btrfs_header_generation(parent), *level, 1); |
| 3676 | BUG_ON(ret); |
| 3677 | |
| 3678 | if (path->locks[*level]) { |
| 3679 | btrfs_tree_unlock(path->nodes[*level]); |
| 3680 | path->locks[*level] = 0; |
| 3681 | } |
| 3682 | free_extent_buffer(path->nodes[*level]); |
| 3683 | path->nodes[*level] = NULL; |
| 3684 | *level += 1; |
| 3685 | cond_resched(); |
| 3686 | return 0; |
| 3687 | } |
| 3688 | |
| 3689 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3690 | * helper for dropping snapshots. This walks back up the tree in the path |
| 3691 | * to find the first node higher up where we haven't yet gone through |
| 3692 | * all the slots |
| 3693 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3694 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3695 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3696 | struct btrfs_path *path, |
| 3697 | int *level, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3698 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3699 | u64 root_owner; |
| 3700 | u64 root_gen; |
| 3701 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3702 | int i; |
| 3703 | int slot; |
| 3704 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3705 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3706 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3707 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3708 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 3709 | struct extent_buffer *node; |
| 3710 | struct btrfs_disk_key disk_key; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3711 | |
| 3712 | /* |
| 3713 | * there is more work to do in this level. |
| 3714 | * Update the drop_progress marker to reflect |
| 3715 | * the work we've done so far, and then bump |
| 3716 | * the slot number |
| 3717 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3718 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3719 | path->slots[i]++; |
| 3720 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3721 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3722 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3723 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3724 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3725 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3726 | return 0; |
| 3727 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3728 | struct extent_buffer *parent; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3729 | |
| 3730 | /* |
| 3731 | * this whole node is done, free our reference |
| 3732 | * on it and go up one level |
| 3733 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3734 | if (path->nodes[*level] == root->node) |
| 3735 | parent = path->nodes[*level]; |
| 3736 | else |
| 3737 | parent = path->nodes[*level + 1]; |
| 3738 | |
| 3739 | root_owner = btrfs_header_owner(parent); |
| 3740 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3741 | |
| 3742 | clean_tree_block(trans, root, path->nodes[*level]); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3743 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3744 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3745 | path->nodes[*level]->len, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3746 | parent->start, root_owner, |
| 3747 | root_gen, *level, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3748 | BUG_ON(ret); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3749 | if (path->locks[*level]) { |
| 3750 | btrfs_tree_unlock(path->nodes[*level]); |
| 3751 | path->locks[*level] = 0; |
| 3752 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3753 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3754 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3755 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3756 | } |
| 3757 | } |
| 3758 | return 1; |
| 3759 | } |
| 3760 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3761 | /* |
| 3762 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 3763 | * the tree freeing any blocks that have a ref count of zero after being |
| 3764 | * decremented. |
| 3765 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3766 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3767 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3768 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 3769 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3770 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3771 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3772 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3773 | int i; |
| 3774 | int orig_level; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3775 | int update_count; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3776 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3777 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3778 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3779 | path = btrfs_alloc_path(); |
| 3780 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3781 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3782 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3783 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3784 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 3785 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3786 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3787 | path->slots[level] = 0; |
| 3788 | } else { |
| 3789 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3790 | struct btrfs_disk_key found_key; |
| 3791 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3792 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3793 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3794 | level = root_item->drop_level; |
| 3795 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3796 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3797 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3798 | ret = wret; |
| 3799 | goto out; |
| 3800 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3801 | node = path->nodes[level]; |
| 3802 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 3803 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 3804 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3805 | /* |
| 3806 | * unlock our path, this is safe because only this |
| 3807 | * function is allowed to delete this snapshot |
| 3808 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3809 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 3810 | if (path->nodes[i] && path->locks[i]) { |
| 3811 | path->locks[i] = 0; |
| 3812 | btrfs_tree_unlock(path->nodes[i]); |
| 3813 | } |
| 3814 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3815 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3816 | while (1) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3817 | unsigned long update; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3818 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3819 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3820 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3821 | if (wret < 0) |
| 3822 | ret = wret; |
| 3823 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3824 | wret = walk_up_tree(trans, root, path, &level, |
| 3825 | BTRFS_MAX_LEVEL); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3826 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3827 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3828 | if (wret < 0) |
| 3829 | ret = wret; |
Chris Mason | b7ec40d | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 3830 | if (trans->transaction->in_commit || |
| 3831 | trans->transaction->delayed_refs.flushing) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3832 | ret = -EAGAIN; |
| 3833 | break; |
| 3834 | } |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3835 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3836 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 3837 | for (update_count = 0; update_count < 16; update_count++) { |
| 3838 | update = trans->delayed_ref_updates; |
| 3839 | trans->delayed_ref_updates = 0; |
| 3840 | if (update) |
| 3841 | btrfs_run_delayed_refs(trans, root, update); |
| 3842 | else |
| 3843 | break; |
| 3844 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3845 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3846 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3847 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3848 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 3849 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3850 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3851 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3852 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3853 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3854 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3855 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3856 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3857 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 3858 | struct btrfs_root *root, |
| 3859 | struct extent_buffer *node, |
| 3860 | struct extent_buffer *parent) |
| 3861 | { |
| 3862 | struct btrfs_path *path; |
| 3863 | int level; |
| 3864 | int parent_level; |
| 3865 | int ret = 0; |
| 3866 | int wret; |
| 3867 | |
| 3868 | path = btrfs_alloc_path(); |
| 3869 | BUG_ON(!path); |
| 3870 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 3871 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3872 | parent_level = btrfs_header_level(parent); |
| 3873 | extent_buffer_get(parent); |
| 3874 | path->nodes[parent_level] = parent; |
| 3875 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 3876 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 3877 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3878 | level = btrfs_header_level(node); |
| 3879 | extent_buffer_get(node); |
| 3880 | path->nodes[level] = node; |
| 3881 | path->slots[level] = 0; |
| 3882 | |
| 3883 | while (1) { |
| 3884 | wret = walk_down_subtree(trans, root, path, &level); |
| 3885 | if (wret < 0) |
| 3886 | ret = wret; |
| 3887 | if (wret != 0) |
| 3888 | break; |
| 3889 | |
| 3890 | wret = walk_up_tree(trans, root, path, &level, parent_level); |
| 3891 | if (wret < 0) |
| 3892 | ret = wret; |
| 3893 | if (wret != 0) |
| 3894 | break; |
| 3895 | } |
| 3896 | |
| 3897 | btrfs_free_path(path); |
| 3898 | return ret; |
| 3899 | } |
| 3900 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3901 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 3902 | unsigned long nr) |
| 3903 | { |
| 3904 | return min(last, start + nr - 1); |
| 3905 | } |
| 3906 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3907 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3908 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3909 | { |
| 3910 | u64 page_start; |
| 3911 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3912 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3913 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3914 | unsigned long i; |
| 3915 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3916 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3917 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3918 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3919 | unsigned int total_read = 0; |
| 3920 | unsigned int total_dirty = 0; |
| 3921 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3922 | |
| 3923 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3924 | |
| 3925 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3926 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3927 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 3928 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3929 | /* make sure the dirty trick played by the caller work */ |
| 3930 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 3931 | first_index, last_index); |
| 3932 | if (ret) |
| 3933 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3934 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3935 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3936 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3937 | for (i = first_index ; i <= last_index; i++) { |
| 3938 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3939 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3940 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3941 | } |
| 3942 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3943 | again: |
| 3944 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3945 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3946 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3947 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3948 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3949 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3950 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3951 | if (!PageUptodate(page)) { |
| 3952 | btrfs_readpage(NULL, page); |
| 3953 | lock_page(page); |
| 3954 | if (!PageUptodate(page)) { |
| 3955 | unlock_page(page); |
| 3956 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3957 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3958 | goto out_unlock; |
| 3959 | } |
| 3960 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3961 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3962 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3963 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 3964 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3965 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3966 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3967 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3968 | if (ordered) { |
| 3969 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3970 | unlock_page(page); |
| 3971 | page_cache_release(page); |
| 3972 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3973 | btrfs_put_ordered_extent(ordered); |
| 3974 | goto again; |
| 3975 | } |
| 3976 | set_page_extent_mapped(page); |
| 3977 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3978 | if (i == first_index) |
| 3979 | set_extent_bits(io_tree, page_start, page_end, |
| 3980 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 3981 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3982 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3983 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3984 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3985 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3986 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3987 | unlock_page(page); |
| 3988 | page_cache_release(page); |
| 3989 | } |
| 3990 | |
| 3991 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3992 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3993 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3994 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3995 | return ret; |
| 3996 | } |
| 3997 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3998 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3999 | struct btrfs_key *extent_key, |
| 4000 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4001 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4002 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4003 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 4004 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4005 | u64 start = extent_key->objectid - offset; |
| 4006 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4007 | |
| 4008 | em = alloc_extent_map(GFP_NOFS); |
| 4009 | BUG_ON(!em || IS_ERR(em)); |
| 4010 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4011 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4012 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4013 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4014 | em->block_start = extent_key->objectid; |
| 4015 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4016 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4017 | |
| 4018 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4019 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4020 | while (1) { |
| 4021 | int ret; |
| 4022 | spin_lock(&em_tree->lock); |
| 4023 | ret = add_extent_mapping(em_tree, em); |
| 4024 | spin_unlock(&em_tree->lock); |
| 4025 | if (ret != -EEXIST) { |
| 4026 | free_extent_map(em); |
| 4027 | break; |
| 4028 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4029 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4030 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4031 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4032 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4033 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | struct btrfs_ref_path { |
| 4037 | u64 extent_start; |
| 4038 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 4039 | u64 root_objectid; |
| 4040 | u64 root_generation; |
| 4041 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4042 | u32 num_refs; |
| 4043 | int lowest_level; |
| 4044 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4045 | int shared_level; |
| 4046 | |
| 4047 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 4048 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4049 | }; |
| 4050 | |
| 4051 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4052 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4053 | u64 disk_bytenr; |
| 4054 | u64 disk_num_bytes; |
| 4055 | u64 offset; |
| 4056 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4057 | u8 compression; |
| 4058 | u8 encryption; |
| 4059 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4060 | }; |
| 4061 | |
| 4062 | static int is_cowonly_root(u64 root_objectid) |
| 4063 | { |
| 4064 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 4065 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 4066 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 4067 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 4068 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 4069 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4070 | return 1; |
| 4071 | return 0; |
| 4072 | } |
| 4073 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4074 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4075 | struct btrfs_root *extent_root, |
| 4076 | struct btrfs_ref_path *ref_path, |
| 4077 | int first_time) |
| 4078 | { |
| 4079 | struct extent_buffer *leaf; |
| 4080 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4081 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4082 | struct btrfs_key key; |
| 4083 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4084 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4085 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4086 | int level; |
| 4087 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4088 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4089 | path = btrfs_alloc_path(); |
| 4090 | if (!path) |
| 4091 | return -ENOMEM; |
| 4092 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4093 | if (first_time) { |
| 4094 | ref_path->lowest_level = -1; |
| 4095 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4096 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4097 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4098 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4099 | walk_down: |
| 4100 | level = ref_path->current_level - 1; |
| 4101 | while (level >= -1) { |
| 4102 | u64 parent; |
| 4103 | if (level < ref_path->lowest_level) |
| 4104 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4105 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4106 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4107 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4108 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4109 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4110 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4111 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4112 | parent = ref_path->nodes[level + 1]; |
| 4113 | ref_path->nodes[level + 1] = 0; |
| 4114 | ref_path->current_level = level; |
| 4115 | BUG_ON(parent == 0); |
| 4116 | |
| 4117 | key.objectid = bytenr; |
| 4118 | key.offset = parent + 1; |
| 4119 | key.type = BTRFS_EXTENT_REF_KEY; |
| 4120 | |
| 4121 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4122 | if (ret < 0) |
| 4123 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4124 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4125 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4126 | leaf = path->nodes[0]; |
| 4127 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4128 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4129 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4130 | if (ret < 0) |
| 4131 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4132 | if (ret > 0) |
| 4133 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 4134 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4135 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4136 | |
| 4137 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4138 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4139 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 4140 | if (level < ref_path->shared_level) |
| 4141 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4142 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4143 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4144 | next: |
| 4145 | level--; |
| 4146 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4147 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4148 | } |
| 4149 | /* reached lowest level */ |
| 4150 | ret = 1; |
| 4151 | goto out; |
| 4152 | walk_up: |
| 4153 | level = ref_path->current_level; |
| 4154 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 4155 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4156 | |
| 4157 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4158 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4159 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4160 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4161 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4162 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4163 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4164 | key.objectid = bytenr; |
| 4165 | key.offset = 0; |
| 4166 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4167 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4168 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 4169 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4170 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4171 | |
| 4172 | leaf = path->nodes[0]; |
| 4173 | nritems = btrfs_header_nritems(leaf); |
| 4174 | if (path->slots[0] >= nritems) { |
| 4175 | ret = btrfs_next_leaf(extent_root, path); |
| 4176 | if (ret < 0) |
| 4177 | goto out; |
| 4178 | if (ret > 0) { |
| 4179 | /* the extent was freed by someone */ |
| 4180 | if (ref_path->lowest_level == level) |
| 4181 | goto out; |
| 4182 | btrfs_release_path(extent_root, path); |
| 4183 | goto walk_down; |
| 4184 | } |
| 4185 | leaf = path->nodes[0]; |
| 4186 | } |
| 4187 | |
| 4188 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4189 | if (found_key.objectid != bytenr || |
| 4190 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 4191 | /* the extent was freed by someone */ |
| 4192 | if (ref_path->lowest_level == level) { |
| 4193 | ret = 1; |
| 4194 | goto out; |
| 4195 | } |
| 4196 | btrfs_release_path(extent_root, path); |
| 4197 | goto walk_down; |
| 4198 | } |
| 4199 | found: |
| 4200 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 4201 | struct btrfs_extent_ref); |
| 4202 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 4203 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4204 | if (first_time) { |
| 4205 | level = (int)ref_objectid; |
| 4206 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 4207 | ref_path->lowest_level = level; |
| 4208 | ref_path->current_level = level; |
| 4209 | ref_path->nodes[level] = bytenr; |
| 4210 | } else { |
| 4211 | WARN_ON(ref_objectid != level); |
| 4212 | } |
| 4213 | } else { |
| 4214 | WARN_ON(level != -1); |
| 4215 | } |
| 4216 | first_time = 0; |
| 4217 | |
| 4218 | if (ref_path->lowest_level == level) { |
| 4219 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4220 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 4221 | } |
| 4222 | |
| 4223 | /* |
| 4224 | * the block is tree root or the block isn't in reference |
| 4225 | * counted tree. |
| 4226 | */ |
| 4227 | if (found_key.objectid == found_key.offset || |
| 4228 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 4229 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4230 | ref_path->root_generation = |
| 4231 | btrfs_ref_generation(leaf, ref); |
| 4232 | if (level < 0) { |
| 4233 | /* special reference from the tree log */ |
| 4234 | ref_path->nodes[0] = found_key.offset; |
| 4235 | ref_path->current_level = 0; |
| 4236 | } |
| 4237 | ret = 0; |
| 4238 | goto out; |
| 4239 | } |
| 4240 | |
| 4241 | level++; |
| 4242 | BUG_ON(ref_path->nodes[level] != 0); |
| 4243 | ref_path->nodes[level] = found_key.offset; |
| 4244 | ref_path->current_level = level; |
| 4245 | |
| 4246 | /* |
| 4247 | * the reference was created in the running transaction, |
| 4248 | * no need to continue walking up. |
| 4249 | */ |
| 4250 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 4251 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4252 | ref_path->root_generation = |
| 4253 | btrfs_ref_generation(leaf, ref); |
| 4254 | ret = 0; |
| 4255 | goto out; |
| 4256 | } |
| 4257 | |
| 4258 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4259 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4260 | } |
| 4261 | /* reached max tree level, but no tree root found. */ |
| 4262 | BUG(); |
| 4263 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4264 | btrfs_free_path(path); |
| 4265 | return ret; |
| 4266 | } |
| 4267 | |
| 4268 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 4269 | struct btrfs_root *extent_root, |
| 4270 | struct btrfs_ref_path *ref_path, |
| 4271 | u64 extent_start) |
| 4272 | { |
| 4273 | memset(ref_path, 0, sizeof(*ref_path)); |
| 4274 | ref_path->extent_start = extent_start; |
| 4275 | |
| 4276 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 4277 | } |
| 4278 | |
| 4279 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 4280 | struct btrfs_root *extent_root, |
| 4281 | struct btrfs_ref_path *ref_path) |
| 4282 | { |
| 4283 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 4284 | } |
| 4285 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4286 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4287 | struct btrfs_key *extent_key, |
| 4288 | u64 offset, int no_fragment, |
| 4289 | struct disk_extent **extents, |
| 4290 | int *nr_extents) |
| 4291 | { |
| 4292 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4293 | struct btrfs_path *path; |
| 4294 | struct btrfs_file_extent_item *fi; |
| 4295 | struct extent_buffer *leaf; |
| 4296 | struct disk_extent *exts = *extents; |
| 4297 | struct btrfs_key found_key; |
| 4298 | u64 cur_pos; |
| 4299 | u64 last_byte; |
| 4300 | u32 nritems; |
| 4301 | int nr = 0; |
| 4302 | int max = *nr_extents; |
| 4303 | int ret; |
| 4304 | |
| 4305 | WARN_ON(!no_fragment && *extents); |
| 4306 | if (!exts) { |
| 4307 | max = 1; |
| 4308 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4309 | if (!exts) |
| 4310 | return -ENOMEM; |
| 4311 | } |
| 4312 | |
| 4313 | path = btrfs_alloc_path(); |
| 4314 | BUG_ON(!path); |
| 4315 | |
| 4316 | cur_pos = extent_key->objectid - offset; |
| 4317 | last_byte = extent_key->objectid + extent_key->offset; |
| 4318 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 4319 | cur_pos, 0); |
| 4320 | if (ret < 0) |
| 4321 | goto out; |
| 4322 | if (ret > 0) { |
| 4323 | ret = -ENOENT; |
| 4324 | goto out; |
| 4325 | } |
| 4326 | |
| 4327 | while (1) { |
| 4328 | leaf = path->nodes[0]; |
| 4329 | nritems = btrfs_header_nritems(leaf); |
| 4330 | if (path->slots[0] >= nritems) { |
| 4331 | ret = btrfs_next_leaf(root, path); |
| 4332 | if (ret < 0) |
| 4333 | goto out; |
| 4334 | if (ret > 0) |
| 4335 | break; |
| 4336 | leaf = path->nodes[0]; |
| 4337 | } |
| 4338 | |
| 4339 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4340 | if (found_key.offset != cur_pos || |
| 4341 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 4342 | found_key.objectid != reloc_inode->i_ino) |
| 4343 | break; |
| 4344 | |
| 4345 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4346 | struct btrfs_file_extent_item); |
| 4347 | if (btrfs_file_extent_type(leaf, fi) != |
| 4348 | BTRFS_FILE_EXTENT_REG || |
| 4349 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4350 | break; |
| 4351 | |
| 4352 | if (nr == max) { |
| 4353 | struct disk_extent *old = exts; |
| 4354 | max *= 2; |
| 4355 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4356 | memcpy(exts, old, sizeof(*exts) * nr); |
| 4357 | if (old != *extents) |
| 4358 | kfree(old); |
| 4359 | } |
| 4360 | |
| 4361 | exts[nr].disk_bytenr = |
| 4362 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4363 | exts[nr].disk_num_bytes = |
| 4364 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4365 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 4366 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4367 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 4368 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 4369 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 4370 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 4371 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4372 | BUG_ON(exts[nr].offset > 0); |
| 4373 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 4374 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4375 | |
| 4376 | cur_pos += exts[nr].num_bytes; |
| 4377 | nr++; |
| 4378 | |
| 4379 | if (cur_pos + offset >= last_byte) |
| 4380 | break; |
| 4381 | |
| 4382 | if (no_fragment) { |
| 4383 | ret = 1; |
| 4384 | goto out; |
| 4385 | } |
| 4386 | path->slots[0]++; |
| 4387 | } |
| 4388 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 4389 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4390 | if (cur_pos + offset < last_byte) { |
| 4391 | ret = -ENOENT; |
| 4392 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4393 | } |
| 4394 | ret = 0; |
| 4395 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4396 | btrfs_free_path(path); |
| 4397 | if (ret) { |
| 4398 | if (exts != *extents) |
| 4399 | kfree(exts); |
| 4400 | } else { |
| 4401 | *extents = exts; |
| 4402 | *nr_extents = nr; |
| 4403 | } |
| 4404 | return ret; |
| 4405 | } |
| 4406 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4407 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4408 | struct btrfs_root *root, |
| 4409 | struct btrfs_path *path, |
| 4410 | struct btrfs_key *extent_key, |
| 4411 | struct btrfs_key *leaf_key, |
| 4412 | struct btrfs_ref_path *ref_path, |
| 4413 | struct disk_extent *new_extents, |
| 4414 | int nr_extents) |
| 4415 | { |
| 4416 | struct extent_buffer *leaf; |
| 4417 | struct btrfs_file_extent_item *fi; |
| 4418 | struct inode *inode = NULL; |
| 4419 | struct btrfs_key key; |
| 4420 | u64 lock_start = 0; |
| 4421 | u64 lock_end = 0; |
| 4422 | u64 num_bytes; |
| 4423 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4424 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4425 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4426 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4427 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4428 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4429 | int ret; |
| 4430 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4431 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4432 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4433 | if (key.objectid < ref_path->owner_objectid || |
| 4434 | (key.objectid == ref_path->owner_objectid && |
| 4435 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 4436 | key.objectid = ref_path->owner_objectid; |
| 4437 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4438 | key.offset = 0; |
| 4439 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | while (1) { |
| 4443 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 4444 | if (ret < 0) |
| 4445 | goto out; |
| 4446 | |
| 4447 | leaf = path->nodes[0]; |
| 4448 | nritems = btrfs_header_nritems(leaf); |
| 4449 | next: |
| 4450 | if (extent_locked && ret > 0) { |
| 4451 | /* |
| 4452 | * the file extent item was modified by someone |
| 4453 | * before the extent got locked. |
| 4454 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4455 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4456 | lock_end, GFP_NOFS); |
| 4457 | extent_locked = 0; |
| 4458 | } |
| 4459 | |
| 4460 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4461 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4462 | break; |
| 4463 | |
| 4464 | BUG_ON(extent_locked); |
| 4465 | ret = btrfs_next_leaf(root, path); |
| 4466 | if (ret < 0) |
| 4467 | goto out; |
| 4468 | if (ret > 0) |
| 4469 | break; |
| 4470 | leaf = path->nodes[0]; |
| 4471 | nritems = btrfs_header_nritems(leaf); |
| 4472 | } |
| 4473 | |
| 4474 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4475 | |
| 4476 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 4477 | if ((key.objectid > ref_path->owner_objectid) || |
| 4478 | (key.objectid == ref_path->owner_objectid && |
| 4479 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4480 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4481 | break; |
| 4482 | } |
| 4483 | |
| 4484 | if (inode && key.objectid != inode->i_ino) { |
| 4485 | BUG_ON(extent_locked); |
| 4486 | btrfs_release_path(root, path); |
| 4487 | mutex_unlock(&inode->i_mutex); |
| 4488 | iput(inode); |
| 4489 | inode = NULL; |
| 4490 | continue; |
| 4491 | } |
| 4492 | |
| 4493 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4494 | path->slots[0]++; |
| 4495 | ret = 1; |
| 4496 | goto next; |
| 4497 | } |
| 4498 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4499 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4500 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4501 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 4502 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4503 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 4504 | extent_key->objectid)) { |
| 4505 | path->slots[0]++; |
| 4506 | ret = 1; |
| 4507 | goto next; |
| 4508 | } |
| 4509 | |
| 4510 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4511 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 4512 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4513 | if (search_end == (u64)-1) { |
| 4514 | search_end = key.offset - ext_offset + |
| 4515 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 4516 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4517 | |
| 4518 | if (!extent_locked) { |
| 4519 | lock_start = key.offset; |
| 4520 | lock_end = lock_start + num_bytes - 1; |
| 4521 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4522 | if (lock_start > key.offset || |
| 4523 | lock_end + 1 < key.offset + num_bytes) { |
| 4524 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4525 | lock_start, lock_end, GFP_NOFS); |
| 4526 | extent_locked = 0; |
| 4527 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | if (!inode) { |
| 4531 | btrfs_release_path(root, path); |
| 4532 | |
| 4533 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 4534 | key.objectid, root); |
| 4535 | if (inode->i_state & I_NEW) { |
| 4536 | BTRFS_I(inode)->root = root; |
| 4537 | BTRFS_I(inode)->location.objectid = |
| 4538 | key.objectid; |
| 4539 | BTRFS_I(inode)->location.type = |
| 4540 | BTRFS_INODE_ITEM_KEY; |
| 4541 | BTRFS_I(inode)->location.offset = 0; |
| 4542 | btrfs_read_locked_inode(inode); |
| 4543 | unlock_new_inode(inode); |
| 4544 | } |
| 4545 | /* |
| 4546 | * some code call btrfs_commit_transaction while |
| 4547 | * holding the i_mutex, so we can't use mutex_lock |
| 4548 | * here. |
| 4549 | */ |
| 4550 | if (is_bad_inode(inode) || |
| 4551 | !mutex_trylock(&inode->i_mutex)) { |
| 4552 | iput(inode); |
| 4553 | inode = NULL; |
| 4554 | key.offset = (u64)-1; |
| 4555 | goto skip; |
| 4556 | } |
| 4557 | } |
| 4558 | |
| 4559 | if (!extent_locked) { |
| 4560 | struct btrfs_ordered_extent *ordered; |
| 4561 | |
| 4562 | btrfs_release_path(root, path); |
| 4563 | |
| 4564 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4565 | lock_end, GFP_NOFS); |
| 4566 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 4567 | lock_end); |
| 4568 | if (ordered && |
| 4569 | ordered->file_offset <= lock_end && |
| 4570 | ordered->file_offset + ordered->len > lock_start) { |
| 4571 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4572 | lock_start, lock_end, GFP_NOFS); |
| 4573 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 4574 | btrfs_put_ordered_extent(ordered); |
| 4575 | key.offset += num_bytes; |
| 4576 | goto skip; |
| 4577 | } |
| 4578 | if (ordered) |
| 4579 | btrfs_put_ordered_extent(ordered); |
| 4580 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4581 | extent_locked = 1; |
| 4582 | continue; |
| 4583 | } |
| 4584 | |
| 4585 | if (nr_extents == 1) { |
| 4586 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4587 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4588 | new_extents[0].disk_bytenr); |
| 4589 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4590 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4591 | btrfs_mark_buffer_dirty(leaf); |
| 4592 | |
| 4593 | btrfs_drop_extent_cache(inode, key.offset, |
| 4594 | key.offset + num_bytes - 1, 0); |
| 4595 | |
| 4596 | ret = btrfs_inc_extent_ref(trans, root, |
| 4597 | new_extents[0].disk_bytenr, |
| 4598 | new_extents[0].disk_num_bytes, |
| 4599 | leaf->start, |
| 4600 | root->root_key.objectid, |
| 4601 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4602 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4603 | BUG_ON(ret); |
| 4604 | |
| 4605 | ret = btrfs_free_extent(trans, root, |
| 4606 | extent_key->objectid, |
| 4607 | extent_key->offset, |
| 4608 | leaf->start, |
| 4609 | btrfs_header_owner(leaf), |
| 4610 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4611 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4612 | BUG_ON(ret); |
| 4613 | |
| 4614 | btrfs_release_path(root, path); |
| 4615 | key.offset += num_bytes; |
| 4616 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4617 | BUG_ON(1); |
| 4618 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4619 | u64 alloc_hint; |
| 4620 | u64 extent_len; |
| 4621 | int i; |
| 4622 | /* |
| 4623 | * drop old extent pointer at first, then insert the |
| 4624 | * new pointers one bye one |
| 4625 | */ |
| 4626 | btrfs_release_path(root, path); |
| 4627 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 4628 | key.offset + num_bytes, |
| 4629 | key.offset, &alloc_hint); |
| 4630 | BUG_ON(ret); |
| 4631 | |
| 4632 | for (i = 0; i < nr_extents; i++) { |
| 4633 | if (ext_offset >= new_extents[i].num_bytes) { |
| 4634 | ext_offset -= new_extents[i].num_bytes; |
| 4635 | continue; |
| 4636 | } |
| 4637 | extent_len = min(new_extents[i].num_bytes - |
| 4638 | ext_offset, num_bytes); |
| 4639 | |
| 4640 | ret = btrfs_insert_empty_item(trans, root, |
| 4641 | path, &key, |
| 4642 | sizeof(*fi)); |
| 4643 | BUG_ON(ret); |
| 4644 | |
| 4645 | leaf = path->nodes[0]; |
| 4646 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4647 | struct btrfs_file_extent_item); |
| 4648 | btrfs_set_file_extent_generation(leaf, fi, |
| 4649 | trans->transid); |
| 4650 | btrfs_set_file_extent_type(leaf, fi, |
| 4651 | BTRFS_FILE_EXTENT_REG); |
| 4652 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4653 | new_extents[i].disk_bytenr); |
| 4654 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4655 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4656 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 4657 | new_extents[i].ram_bytes); |
| 4658 | |
| 4659 | btrfs_set_file_extent_compression(leaf, fi, |
| 4660 | new_extents[i].compression); |
| 4661 | btrfs_set_file_extent_encryption(leaf, fi, |
| 4662 | new_extents[i].encryption); |
| 4663 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 4664 | new_extents[i].other_encoding); |
| 4665 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4666 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4667 | extent_len); |
| 4668 | ext_offset += new_extents[i].offset; |
| 4669 | btrfs_set_file_extent_offset(leaf, fi, |
| 4670 | ext_offset); |
| 4671 | btrfs_mark_buffer_dirty(leaf); |
| 4672 | |
| 4673 | btrfs_drop_extent_cache(inode, key.offset, |
| 4674 | key.offset + extent_len - 1, 0); |
| 4675 | |
| 4676 | ret = btrfs_inc_extent_ref(trans, root, |
| 4677 | new_extents[i].disk_bytenr, |
| 4678 | new_extents[i].disk_num_bytes, |
| 4679 | leaf->start, |
| 4680 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4681 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4682 | BUG_ON(ret); |
| 4683 | btrfs_release_path(root, path); |
| 4684 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4685 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4686 | |
| 4687 | ext_offset = 0; |
| 4688 | num_bytes -= extent_len; |
| 4689 | key.offset += extent_len; |
| 4690 | |
| 4691 | if (num_bytes == 0) |
| 4692 | break; |
| 4693 | } |
| 4694 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4695 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4699 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4700 | lock_end, GFP_NOFS); |
| 4701 | extent_locked = 0; |
| 4702 | } |
| 4703 | skip: |
| 4704 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4705 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4706 | break; |
| 4707 | |
| 4708 | cond_resched(); |
| 4709 | } |
| 4710 | ret = 0; |
| 4711 | out: |
| 4712 | btrfs_release_path(root, path); |
| 4713 | if (inode) { |
| 4714 | mutex_unlock(&inode->i_mutex); |
| 4715 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4716 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4717 | lock_end, GFP_NOFS); |
| 4718 | } |
| 4719 | iput(inode); |
| 4720 | } |
| 4721 | return ret; |
| 4722 | } |
| 4723 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4724 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 4725 | struct btrfs_root *root, |
| 4726 | struct extent_buffer *buf, u64 orig_start) |
| 4727 | { |
| 4728 | int level; |
| 4729 | int ret; |
| 4730 | |
| 4731 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 4732 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 4733 | |
| 4734 | level = btrfs_header_level(buf); |
| 4735 | if (level == 0) { |
| 4736 | struct btrfs_leaf_ref *ref; |
| 4737 | struct btrfs_leaf_ref *orig_ref; |
| 4738 | |
| 4739 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 4740 | if (!orig_ref) |
| 4741 | return -ENOENT; |
| 4742 | |
| 4743 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 4744 | if (!ref) { |
| 4745 | btrfs_free_leaf_ref(root, orig_ref); |
| 4746 | return -ENOMEM; |
| 4747 | } |
| 4748 | |
| 4749 | ref->nritems = orig_ref->nritems; |
| 4750 | memcpy(ref->extents, orig_ref->extents, |
| 4751 | sizeof(ref->extents[0]) * ref->nritems); |
| 4752 | |
| 4753 | btrfs_free_leaf_ref(root, orig_ref); |
| 4754 | |
| 4755 | ref->root_gen = trans->transid; |
| 4756 | ref->bytenr = buf->start; |
| 4757 | ref->owner = btrfs_header_owner(buf); |
| 4758 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4759 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4760 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 4761 | WARN_ON(ret); |
| 4762 | btrfs_free_leaf_ref(root, ref); |
| 4763 | } |
| 4764 | return 0; |
| 4765 | } |
| 4766 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4767 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4768 | struct extent_buffer *leaf, |
| 4769 | struct btrfs_block_group_cache *group, |
| 4770 | struct btrfs_root *target_root) |
| 4771 | { |
| 4772 | struct btrfs_key key; |
| 4773 | struct inode *inode = NULL; |
| 4774 | struct btrfs_file_extent_item *fi; |
| 4775 | u64 num_bytes; |
| 4776 | u64 skip_objectid = 0; |
| 4777 | u32 nritems; |
| 4778 | u32 i; |
| 4779 | |
| 4780 | nritems = btrfs_header_nritems(leaf); |
| 4781 | for (i = 0; i < nritems; i++) { |
| 4782 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4783 | if (key.objectid == skip_objectid || |
| 4784 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 4785 | continue; |
| 4786 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4787 | if (btrfs_file_extent_type(leaf, fi) == |
| 4788 | BTRFS_FILE_EXTENT_INLINE) |
| 4789 | continue; |
| 4790 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4791 | continue; |
| 4792 | if (!inode || inode->i_ino != key.objectid) { |
| 4793 | iput(inode); |
| 4794 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 4795 | key.objectid, target_root, 1); |
| 4796 | } |
| 4797 | if (!inode) { |
| 4798 | skip_objectid = key.objectid; |
| 4799 | continue; |
| 4800 | } |
| 4801 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4802 | |
| 4803 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4804 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4805 | btrfs_drop_extent_cache(inode, key.offset, |
| 4806 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4807 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4808 | key.offset + num_bytes - 1, GFP_NOFS); |
| 4809 | cond_resched(); |
| 4810 | } |
| 4811 | iput(inode); |
| 4812 | return 0; |
| 4813 | } |
| 4814 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4815 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4816 | struct btrfs_root *root, |
| 4817 | struct extent_buffer *leaf, |
| 4818 | struct btrfs_block_group_cache *group, |
| 4819 | struct inode *reloc_inode) |
| 4820 | { |
| 4821 | struct btrfs_key key; |
| 4822 | struct btrfs_key extent_key; |
| 4823 | struct btrfs_file_extent_item *fi; |
| 4824 | struct btrfs_leaf_ref *ref; |
| 4825 | struct disk_extent *new_extent; |
| 4826 | u64 bytenr; |
| 4827 | u64 num_bytes; |
| 4828 | u32 nritems; |
| 4829 | u32 i; |
| 4830 | int ext_index; |
| 4831 | int nr_extent; |
| 4832 | int ret; |
| 4833 | |
| 4834 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 4835 | BUG_ON(!new_extent); |
| 4836 | |
| 4837 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 4838 | BUG_ON(!ref); |
| 4839 | |
| 4840 | ext_index = -1; |
| 4841 | nritems = btrfs_header_nritems(leaf); |
| 4842 | for (i = 0; i < nritems; i++) { |
| 4843 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4844 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 4845 | continue; |
| 4846 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4847 | if (btrfs_file_extent_type(leaf, fi) == |
| 4848 | BTRFS_FILE_EXTENT_INLINE) |
| 4849 | continue; |
| 4850 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4851 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4852 | if (bytenr == 0) |
| 4853 | continue; |
| 4854 | |
| 4855 | ext_index++; |
| 4856 | if (bytenr >= group->key.objectid + group->key.offset || |
| 4857 | bytenr + num_bytes <= group->key.objectid) |
| 4858 | continue; |
| 4859 | |
| 4860 | extent_key.objectid = bytenr; |
| 4861 | extent_key.offset = num_bytes; |
| 4862 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4863 | nr_extent = 1; |
| 4864 | ret = get_new_locations(reloc_inode, &extent_key, |
| 4865 | group->key.objectid, 1, |
| 4866 | &new_extent, &nr_extent); |
| 4867 | if (ret > 0) |
| 4868 | continue; |
| 4869 | BUG_ON(ret < 0); |
| 4870 | |
| 4871 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 4872 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 4873 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 4874 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 4875 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4876 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4877 | new_extent->disk_bytenr); |
| 4878 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4879 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4880 | btrfs_mark_buffer_dirty(leaf); |
| 4881 | |
| 4882 | ret = btrfs_inc_extent_ref(trans, root, |
| 4883 | new_extent->disk_bytenr, |
| 4884 | new_extent->disk_num_bytes, |
| 4885 | leaf->start, |
| 4886 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4887 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4888 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4889 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4890 | ret = btrfs_free_extent(trans, root, |
| 4891 | bytenr, num_bytes, leaf->start, |
| 4892 | btrfs_header_owner(leaf), |
| 4893 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4894 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4895 | BUG_ON(ret); |
| 4896 | cond_resched(); |
| 4897 | } |
| 4898 | kfree(new_extent); |
| 4899 | BUG_ON(ext_index + 1 != ref->nritems); |
| 4900 | btrfs_free_leaf_ref(root, ref); |
| 4901 | return 0; |
| 4902 | } |
| 4903 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4904 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 4905 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4906 | { |
| 4907 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4908 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4909 | |
| 4910 | if (root->reloc_root) { |
| 4911 | reloc_root = root->reloc_root; |
| 4912 | root->reloc_root = NULL; |
| 4913 | list_add(&reloc_root->dead_list, |
| 4914 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4915 | |
| 4916 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 4917 | reloc_root->node->start); |
| 4918 | btrfs_set_root_level(&root->root_item, |
| 4919 | btrfs_header_level(reloc_root->node)); |
| 4920 | memset(&reloc_root->root_item.drop_progress, 0, |
| 4921 | sizeof(struct btrfs_disk_key)); |
| 4922 | reloc_root->root_item.drop_level = 0; |
| 4923 | |
| 4924 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 4925 | &reloc_root->root_key, |
| 4926 | &reloc_root->root_item); |
| 4927 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4928 | } |
| 4929 | return 0; |
| 4930 | } |
| 4931 | |
| 4932 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 4933 | { |
| 4934 | struct btrfs_trans_handle *trans; |
| 4935 | struct btrfs_root *reloc_root; |
| 4936 | struct btrfs_root *prev_root = NULL; |
| 4937 | struct list_head dead_roots; |
| 4938 | int ret; |
| 4939 | unsigned long nr; |
| 4940 | |
| 4941 | INIT_LIST_HEAD(&dead_roots); |
| 4942 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 4943 | |
| 4944 | while (!list_empty(&dead_roots)) { |
| 4945 | reloc_root = list_entry(dead_roots.prev, |
| 4946 | struct btrfs_root, dead_list); |
| 4947 | list_del_init(&reloc_root->dead_list); |
| 4948 | |
| 4949 | BUG_ON(reloc_root->commit_root != NULL); |
| 4950 | while (1) { |
| 4951 | trans = btrfs_join_transaction(root, 1); |
| 4952 | BUG_ON(!trans); |
| 4953 | |
| 4954 | mutex_lock(&root->fs_info->drop_mutex); |
| 4955 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 4956 | if (ret != -EAGAIN) |
| 4957 | break; |
| 4958 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4959 | |
| 4960 | nr = trans->blocks_used; |
| 4961 | ret = btrfs_end_transaction(trans, root); |
| 4962 | BUG_ON(ret); |
| 4963 | btrfs_btree_balance_dirty(root, nr); |
| 4964 | } |
| 4965 | |
| 4966 | free_extent_buffer(reloc_root->node); |
| 4967 | |
| 4968 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 4969 | &reloc_root->root_key); |
| 4970 | BUG_ON(ret); |
| 4971 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4972 | |
| 4973 | nr = trans->blocks_used; |
| 4974 | ret = btrfs_end_transaction(trans, root); |
| 4975 | BUG_ON(ret); |
| 4976 | btrfs_btree_balance_dirty(root, nr); |
| 4977 | |
| 4978 | kfree(prev_root); |
| 4979 | prev_root = reloc_root; |
| 4980 | } |
| 4981 | if (prev_root) { |
| 4982 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 4983 | kfree(prev_root); |
| 4984 | } |
| 4985 | return 0; |
| 4986 | } |
| 4987 | |
| 4988 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 4989 | { |
| 4990 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 4991 | return 0; |
| 4992 | } |
| 4993 | |
| 4994 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 4995 | { |
| 4996 | struct btrfs_root *reloc_root; |
| 4997 | struct btrfs_trans_handle *trans; |
| 4998 | struct btrfs_key location; |
| 4999 | int found; |
| 5000 | int ret; |
| 5001 | |
| 5002 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5003 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 5004 | BUG_ON(ret); |
| 5005 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 5006 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 5007 | |
| 5008 | if (found) { |
| 5009 | trans = btrfs_start_transaction(root, 1); |
| 5010 | BUG_ON(!trans); |
| 5011 | ret = btrfs_commit_transaction(trans, root); |
| 5012 | BUG_ON(ret); |
| 5013 | } |
| 5014 | |
| 5015 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5016 | location.offset = (u64)-1; |
| 5017 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 5018 | |
| 5019 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 5020 | BUG_ON(!reloc_root); |
| 5021 | btrfs_orphan_cleanup(reloc_root); |
| 5022 | return 0; |
| 5023 | } |
| 5024 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5025 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5026 | struct btrfs_root *root) |
| 5027 | { |
| 5028 | struct btrfs_root *reloc_root; |
| 5029 | struct extent_buffer *eb; |
| 5030 | struct btrfs_root_item *root_item; |
| 5031 | struct btrfs_key root_key; |
| 5032 | int ret; |
| 5033 | |
| 5034 | BUG_ON(!root->ref_cows); |
| 5035 | if (root->reloc_root) |
| 5036 | return 0; |
| 5037 | |
| 5038 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 5039 | BUG_ON(!root_item); |
| 5040 | |
| 5041 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 5042 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 5043 | BUG_ON(ret); |
| 5044 | |
| 5045 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 5046 | root_key.offset = root->root_key.objectid; |
| 5047 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5048 | |
| 5049 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 5050 | btrfs_set_root_refs(root_item, 0); |
| 5051 | btrfs_set_root_bytenr(root_item, eb->start); |
| 5052 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5053 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5054 | |
| 5055 | btrfs_tree_unlock(eb); |
| 5056 | free_extent_buffer(eb); |
| 5057 | |
| 5058 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 5059 | &root_key, root_item); |
| 5060 | BUG_ON(ret); |
| 5061 | kfree(root_item); |
| 5062 | |
| 5063 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 5064 | &root_key); |
| 5065 | BUG_ON(!reloc_root); |
| 5066 | reloc_root->last_trans = trans->transid; |
| 5067 | reloc_root->commit_root = NULL; |
| 5068 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 5069 | |
| 5070 | root->reloc_root = reloc_root; |
| 5071 | return 0; |
| 5072 | } |
| 5073 | |
| 5074 | /* |
| 5075 | * Core function of space balance. |
| 5076 | * |
| 5077 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5078 | * counted roots. There is one reloc tree for each subvol, and all |
| 5079 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 5080 | * of the latest committed roots of subvols (root->commit_root). |
| 5081 | * |
| 5082 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 5083 | * COW the block through subvol's reloc tree, then update block pointer |
| 5084 | * in the subvol to point to the new block. Since all reloc trees share |
| 5085 | * same root key objectid, doing special handing for tree blocks owned |
| 5086 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 5087 | * we can use the resulting new block directly when the same block is |
| 5088 | * required to COW again through other reloc trees. By this way, relocated |
| 5089 | * tree blocks are shared between reloc trees, so they are also shared |
| 5090 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5091 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5092 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5093 | struct btrfs_root *root, |
| 5094 | struct btrfs_path *path, |
| 5095 | struct btrfs_key *first_key, |
| 5096 | struct btrfs_ref_path *ref_path, |
| 5097 | struct btrfs_block_group_cache *group, |
| 5098 | struct inode *reloc_inode) |
| 5099 | { |
| 5100 | struct btrfs_root *reloc_root; |
| 5101 | struct extent_buffer *eb = NULL; |
| 5102 | struct btrfs_key *keys; |
| 5103 | u64 *nodes; |
| 5104 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5105 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5106 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5107 | int ret; |
| 5108 | |
| 5109 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 5110 | lowest_level = ref_path->owner_objectid; |
| 5111 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5112 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5113 | path->lowest_level = lowest_level; |
| 5114 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 5115 | BUG_ON(ret < 0); |
| 5116 | path->lowest_level = 0; |
| 5117 | btrfs_release_path(root, path); |
| 5118 | return 0; |
| 5119 | } |
| 5120 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5121 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5122 | ret = init_reloc_tree(trans, root); |
| 5123 | BUG_ON(ret); |
| 5124 | reloc_root = root->reloc_root; |
| 5125 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5126 | shared_level = ref_path->shared_level; |
| 5127 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5128 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5129 | keys = ref_path->node_keys; |
| 5130 | nodes = ref_path->new_nodes; |
| 5131 | memset(&keys[shared_level + 1], 0, |
| 5132 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 5133 | memset(&nodes[shared_level + 1], 0, |
| 5134 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5135 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5136 | if (nodes[lowest_level] == 0) { |
| 5137 | path->lowest_level = lowest_level; |
| 5138 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5139 | 0, 1); |
| 5140 | BUG_ON(ret); |
| 5141 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 5142 | eb = path->nodes[level]; |
| 5143 | if (!eb || eb == reloc_root->node) |
| 5144 | break; |
| 5145 | nodes[level] = eb->start; |
| 5146 | if (level == 0) |
| 5147 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 5148 | else |
| 5149 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 5150 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5151 | if (nodes[0] && |
| 5152 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5153 | eb = path->nodes[0]; |
| 5154 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 5155 | group, reloc_inode); |
| 5156 | BUG_ON(ret); |
| 5157 | } |
| 5158 | btrfs_release_path(reloc_root, path); |
| 5159 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5160 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5161 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5162 | BUG_ON(ret); |
| 5163 | } |
| 5164 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5165 | /* |
| 5166 | * replace tree blocks in the fs tree with tree blocks in |
| 5167 | * the reloc tree. |
| 5168 | */ |
| 5169 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 5170 | BUG_ON(ret < 0); |
| 5171 | |
| 5172 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5173 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5174 | 0, 0); |
| 5175 | BUG_ON(ret); |
| 5176 | extent_buffer_get(path->nodes[0]); |
| 5177 | eb = path->nodes[0]; |
| 5178 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5179 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 5180 | BUG_ON(ret); |
| 5181 | free_extent_buffer(eb); |
| 5182 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5183 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5184 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5185 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5186 | return 0; |
| 5187 | } |
| 5188 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5189 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5190 | struct btrfs_root *root, |
| 5191 | struct btrfs_path *path, |
| 5192 | struct btrfs_key *first_key, |
| 5193 | struct btrfs_ref_path *ref_path) |
| 5194 | { |
| 5195 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5196 | |
| 5197 | ret = relocate_one_path(trans, root, path, first_key, |
| 5198 | ref_path, NULL, NULL); |
| 5199 | BUG_ON(ret); |
| 5200 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5201 | return 0; |
| 5202 | } |
| 5203 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5204 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5205 | struct btrfs_root *extent_root, |
| 5206 | struct btrfs_path *path, |
| 5207 | struct btrfs_key *extent_key) |
| 5208 | { |
| 5209 | int ret; |
| 5210 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5211 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 5212 | if (ret) |
| 5213 | goto out; |
| 5214 | ret = btrfs_del_item(trans, extent_root, path); |
| 5215 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5216 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5217 | return ret; |
| 5218 | } |
| 5219 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5220 | 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] | 5221 | struct btrfs_ref_path *ref_path) |
| 5222 | { |
| 5223 | struct btrfs_key root_key; |
| 5224 | |
| 5225 | root_key.objectid = ref_path->root_objectid; |
| 5226 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5227 | if (is_cowonly_root(ref_path->root_objectid)) |
| 5228 | root_key.offset = 0; |
| 5229 | else |
| 5230 | root_key.offset = (u64)-1; |
| 5231 | |
| 5232 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5233 | } |
| 5234 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5235 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5236 | struct btrfs_path *path, |
| 5237 | struct btrfs_key *extent_key, |
| 5238 | struct btrfs_block_group_cache *group, |
| 5239 | struct inode *reloc_inode, int pass) |
| 5240 | { |
| 5241 | struct btrfs_trans_handle *trans; |
| 5242 | struct btrfs_root *found_root; |
| 5243 | struct btrfs_ref_path *ref_path = NULL; |
| 5244 | struct disk_extent *new_extents = NULL; |
| 5245 | int nr_extents = 0; |
| 5246 | int loops; |
| 5247 | int ret; |
| 5248 | int level; |
| 5249 | struct btrfs_key first_key; |
| 5250 | u64 prev_block = 0; |
| 5251 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5252 | |
| 5253 | trans = btrfs_start_transaction(extent_root, 1); |
| 5254 | BUG_ON(!trans); |
| 5255 | |
| 5256 | if (extent_key->objectid == 0) { |
| 5257 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 5258 | goto out; |
| 5259 | } |
| 5260 | |
| 5261 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 5262 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5263 | ret = -ENOMEM; |
| 5264 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | for (loops = 0; ; loops++) { |
| 5268 | if (loops == 0) { |
| 5269 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 5270 | extent_key->objectid); |
| 5271 | } else { |
| 5272 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 5273 | } |
| 5274 | if (ret < 0) |
| 5275 | goto out; |
| 5276 | if (ret > 0) |
| 5277 | break; |
| 5278 | |
| 5279 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5280 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 5281 | continue; |
| 5282 | |
| 5283 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 5284 | BUG_ON(!found_root); |
| 5285 | /* |
| 5286 | * for reference counted tree, only process reference paths |
| 5287 | * rooted at the latest committed root. |
| 5288 | */ |
| 5289 | if (found_root->ref_cows && |
| 5290 | ref_path->root_generation != found_root->root_key.offset) |
| 5291 | continue; |
| 5292 | |
| 5293 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5294 | if (pass == 0) { |
| 5295 | /* |
| 5296 | * copy data extents to new locations |
| 5297 | */ |
| 5298 | u64 group_start = group->key.objectid; |
| 5299 | ret = relocate_data_extent(reloc_inode, |
| 5300 | extent_key, |
| 5301 | group_start); |
| 5302 | if (ret < 0) |
| 5303 | goto out; |
| 5304 | break; |
| 5305 | } |
| 5306 | level = 0; |
| 5307 | } else { |
| 5308 | level = ref_path->owner_objectid; |
| 5309 | } |
| 5310 | |
| 5311 | if (prev_block != ref_path->nodes[level]) { |
| 5312 | struct extent_buffer *eb; |
| 5313 | u64 block_start = ref_path->nodes[level]; |
| 5314 | u64 block_size = btrfs_level_size(found_root, level); |
| 5315 | |
| 5316 | eb = read_tree_block(found_root, block_start, |
| 5317 | block_size, 0); |
| 5318 | btrfs_tree_lock(eb); |
| 5319 | BUG_ON(level != btrfs_header_level(eb)); |
| 5320 | |
| 5321 | if (level == 0) |
| 5322 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 5323 | else |
| 5324 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 5325 | |
| 5326 | btrfs_tree_unlock(eb); |
| 5327 | free_extent_buffer(eb); |
| 5328 | prev_block = block_start; |
| 5329 | } |
| 5330 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 5331 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5332 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 5333 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5334 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5335 | /* |
| 5336 | * try to update data extent references while |
| 5337 | * keeping metadata shared between snapshots. |
| 5338 | */ |
| 5339 | if (pass == 1) { |
| 5340 | ret = relocate_one_path(trans, found_root, |
| 5341 | path, &first_key, ref_path, |
| 5342 | group, reloc_inode); |
| 5343 | if (ret < 0) |
| 5344 | goto out; |
| 5345 | continue; |
| 5346 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5347 | /* |
| 5348 | * use fallback method to process the remaining |
| 5349 | * references. |
| 5350 | */ |
| 5351 | if (!new_extents) { |
| 5352 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5353 | new_extents = kmalloc(sizeof(*new_extents), |
| 5354 | GFP_NOFS); |
| 5355 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5356 | ret = get_new_locations(reloc_inode, |
| 5357 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5358 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5359 | &new_extents, |
| 5360 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5361 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5362 | goto out; |
| 5363 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5364 | ret = replace_one_extent(trans, found_root, |
| 5365 | path, extent_key, |
| 5366 | &first_key, ref_path, |
| 5367 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5368 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5369 | ret = relocate_tree_block(trans, found_root, path, |
| 5370 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5371 | } |
| 5372 | if (ret < 0) |
| 5373 | goto out; |
| 5374 | } |
| 5375 | ret = 0; |
| 5376 | out: |
| 5377 | btrfs_end_transaction(trans, extent_root); |
| 5378 | kfree(new_extents); |
| 5379 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5380 | return ret; |
| 5381 | } |
| 5382 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5383 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 5384 | { |
| 5385 | u64 num_devices; |
| 5386 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 5387 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 5388 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5389 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5390 | if (num_devices == 1) { |
| 5391 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5392 | stripped = flags & ~stripped; |
| 5393 | |
| 5394 | /* turn raid0 into single device chunks */ |
| 5395 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 5396 | return stripped; |
| 5397 | |
| 5398 | /* turn mirroring into duplication */ |
| 5399 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5400 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5401 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 5402 | return flags; |
| 5403 | } else { |
| 5404 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5405 | if (flags & stripped) |
| 5406 | return flags; |
| 5407 | |
| 5408 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5409 | stripped = flags & ~stripped; |
| 5410 | |
| 5411 | /* switch duplicated blocks with raid1 */ |
| 5412 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 5413 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 5414 | |
| 5415 | /* turn single device chunks into raid0 */ |
| 5416 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 5417 | } |
| 5418 | return flags; |
| 5419 | } |
| 5420 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 5421 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5422 | struct btrfs_block_group_cache *shrink_block_group, |
| 5423 | int force) |
| 5424 | { |
| 5425 | struct btrfs_trans_handle *trans; |
| 5426 | u64 new_alloc_flags; |
| 5427 | u64 calc; |
| 5428 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5429 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5430 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5431 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5432 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5433 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5434 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5435 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5436 | new_alloc_flags = update_block_group_flags(root, |
| 5437 | shrink_block_group->flags); |
| 5438 | if (new_alloc_flags != shrink_block_group->flags) { |
| 5439 | calc = |
| 5440 | btrfs_block_group_used(&shrink_block_group->item); |
| 5441 | } else { |
| 5442 | calc = shrink_block_group->key.offset; |
| 5443 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5444 | spin_unlock(&shrink_block_group->lock); |
| 5445 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5446 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5447 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5448 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5449 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5450 | } else |
| 5451 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5452 | return 0; |
| 5453 | } |
| 5454 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5455 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 5456 | struct btrfs_root *root, |
| 5457 | u64 objectid, u64 size) |
| 5458 | { |
| 5459 | struct btrfs_path *path; |
| 5460 | struct btrfs_inode_item *item; |
| 5461 | struct extent_buffer *leaf; |
| 5462 | int ret; |
| 5463 | |
| 5464 | path = btrfs_alloc_path(); |
| 5465 | if (!path) |
| 5466 | return -ENOMEM; |
| 5467 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame^] | 5468 | path->leave_spinning = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5469 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 5470 | if (ret) |
| 5471 | goto out; |
| 5472 | |
| 5473 | leaf = path->nodes[0]; |
| 5474 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 5475 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 5476 | btrfs_set_inode_generation(leaf, item, 1); |
| 5477 | btrfs_set_inode_size(leaf, item, size); |
| 5478 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 5479 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5480 | btrfs_mark_buffer_dirty(leaf); |
| 5481 | btrfs_release_path(root, path); |
| 5482 | out: |
| 5483 | btrfs_free_path(path); |
| 5484 | return ret; |
| 5485 | } |
| 5486 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5487 | 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] | 5488 | struct btrfs_block_group_cache *group) |
| 5489 | { |
| 5490 | struct inode *inode = NULL; |
| 5491 | struct btrfs_trans_handle *trans; |
| 5492 | struct btrfs_root *root; |
| 5493 | struct btrfs_key root_key; |
| 5494 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 5495 | int err = 0; |
| 5496 | |
| 5497 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5498 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5499 | root_key.offset = (u64)-1; |
| 5500 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5501 | if (IS_ERR(root)) |
| 5502 | return ERR_CAST(root); |
| 5503 | |
| 5504 | trans = btrfs_start_transaction(root, 1); |
| 5505 | BUG_ON(!trans); |
| 5506 | |
| 5507 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 5508 | if (err) |
| 5509 | goto out; |
| 5510 | |
| 5511 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 5512 | BUG_ON(err); |
| 5513 | |
| 5514 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5515 | group->key.offset, 0, group->key.offset, |
| 5516 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5517 | BUG_ON(err); |
| 5518 | |
| 5519 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 5520 | if (inode->i_state & I_NEW) { |
| 5521 | BTRFS_I(inode)->root = root; |
| 5522 | BTRFS_I(inode)->location.objectid = objectid; |
| 5523 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5524 | BTRFS_I(inode)->location.offset = 0; |
| 5525 | btrfs_read_locked_inode(inode); |
| 5526 | unlock_new_inode(inode); |
| 5527 | BUG_ON(is_bad_inode(inode)); |
| 5528 | } else { |
| 5529 | BUG_ON(1); |
| 5530 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 5531 | BTRFS_I(inode)->index_cnt = group->key.objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5532 | |
| 5533 | err = btrfs_orphan_add(trans, inode); |
| 5534 | out: |
| 5535 | btrfs_end_transaction(trans, root); |
| 5536 | if (err) { |
| 5537 | if (inode) |
| 5538 | iput(inode); |
| 5539 | inode = ERR_PTR(err); |
| 5540 | } |
| 5541 | return inode; |
| 5542 | } |
| 5543 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 5544 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) |
| 5545 | { |
| 5546 | |
| 5547 | struct btrfs_ordered_sum *sums; |
| 5548 | struct btrfs_sector_sum *sector_sum; |
| 5549 | struct btrfs_ordered_extent *ordered; |
| 5550 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5551 | struct list_head list; |
| 5552 | size_t offset; |
| 5553 | int ret; |
| 5554 | u64 disk_bytenr; |
| 5555 | |
| 5556 | INIT_LIST_HEAD(&list); |
| 5557 | |
| 5558 | ordered = btrfs_lookup_ordered_extent(inode, file_pos); |
| 5559 | BUG_ON(ordered->file_offset != file_pos || ordered->len != len); |
| 5560 | |
| 5561 | disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 5562 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 5563 | disk_bytenr + len - 1, &list); |
| 5564 | |
| 5565 | while (!list_empty(&list)) { |
| 5566 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 5567 | list_del_init(&sums->list); |
| 5568 | |
| 5569 | sector_sum = sums->sums; |
| 5570 | sums->bytenr = ordered->start; |
| 5571 | |
| 5572 | offset = 0; |
| 5573 | while (offset < sums->len) { |
| 5574 | sector_sum->bytenr += ordered->start - disk_bytenr; |
| 5575 | sector_sum++; |
| 5576 | offset += root->sectorsize; |
| 5577 | } |
| 5578 | |
| 5579 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 5580 | } |
| 5581 | btrfs_put_ordered_extent(ordered); |
| 5582 | return 0; |
| 5583 | } |
| 5584 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5585 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5586 | { |
| 5587 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5588 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5589 | struct btrfs_fs_info *info = root->fs_info; |
| 5590 | struct extent_buffer *leaf; |
| 5591 | struct inode *reloc_inode; |
| 5592 | struct btrfs_block_group_cache *block_group; |
| 5593 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5594 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5595 | u64 cur_byte; |
| 5596 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5597 | u32 nritems; |
| 5598 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5599 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5600 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5601 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5602 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5603 | |
| 5604 | block_group = btrfs_lookup_block_group(info, group_start); |
| 5605 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5606 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5607 | printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5608 | (unsigned long long)block_group->key.objectid, |
| 5609 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5610 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5611 | path = btrfs_alloc_path(); |
| 5612 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5613 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5614 | reloc_inode = create_reloc_inode(info, block_group); |
| 5615 | BUG_ON(IS_ERR(reloc_inode)); |
| 5616 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5617 | __alloc_chunk_for_shrink(root, block_group, 1); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5618 | set_block_group_readonly(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5619 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5620 | btrfs_start_delalloc_inodes(info->tree_root); |
| 5621 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5622 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5623 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5624 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5625 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5626 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5627 | key.offset = 0; |
| 5628 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5629 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5630 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5631 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5632 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5633 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5634 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 5635 | btrfs_clean_old_snapshots(info->tree_root); |
| 5636 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 5637 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5638 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5639 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5640 | btrfs_commit_transaction(trans, info->tree_root); |
| 5641 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5642 | while (1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5643 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5644 | if (ret < 0) |
| 5645 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5646 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5647 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5648 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5649 | if (path->slots[0] >= nritems) { |
| 5650 | ret = btrfs_next_leaf(root, path); |
| 5651 | if (ret < 0) |
| 5652 | goto out; |
| 5653 | if (ret == 1) { |
| 5654 | ret = 0; |
| 5655 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5656 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5657 | leaf = path->nodes[0]; |
| 5658 | nritems = btrfs_header_nritems(leaf); |
| 5659 | } |
| 5660 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5661 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5662 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5663 | if (key.objectid >= block_group->key.objectid + |
| 5664 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5665 | break; |
| 5666 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5667 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5668 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5669 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5670 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5671 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5672 | } |
| 5673 | progress = 1; |
| 5674 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5675 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 5676 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5677 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5678 | goto next; |
| 5679 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5680 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5681 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5682 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5683 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5684 | |
| 5685 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 5686 | ret = relocate_one_extent(root, path, &key, block_group, |
| 5687 | reloc_inode, pass); |
| 5688 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5689 | if (ret > 0) |
| 5690 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5691 | |
| 5692 | key.objectid = cur_byte; |
| 5693 | key.type = 0; |
| 5694 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5695 | } |
| 5696 | |
| 5697 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5698 | |
| 5699 | if (pass == 0) { |
| 5700 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 5701 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5702 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5703 | |
| 5704 | if (total_found > 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5705 | printk(KERN_INFO "btrfs found %llu extents in pass %d\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5706 | (unsigned long long)total_found, pass); |
| 5707 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5708 | if (total_found == skipped && pass > 2) { |
| 5709 | iput(reloc_inode); |
| 5710 | reloc_inode = create_reloc_inode(info, block_group); |
| 5711 | pass = 0; |
| 5712 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5713 | goto again; |
| 5714 | } |
| 5715 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5716 | /* delete reloc_inode */ |
| 5717 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5718 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5719 | /* unpin extents in this range */ |
| 5720 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5721 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5722 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5723 | spin_lock(&block_group->lock); |
| 5724 | WARN_ON(block_group->pinned > 0); |
| 5725 | WARN_ON(block_group->reserved > 0); |
| 5726 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 5727 | spin_unlock(&block_group->lock); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5728 | put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5729 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5730 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5731 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5732 | return ret; |
| 5733 | } |
| 5734 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 5735 | static int find_first_block_group(struct btrfs_root *root, |
| 5736 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5737 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5738 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5739 | struct btrfs_key found_key; |
| 5740 | struct extent_buffer *leaf; |
| 5741 | int slot; |
| 5742 | |
| 5743 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 5744 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5745 | goto out; |
| 5746 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5747 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5748 | slot = path->slots[0]; |
| 5749 | leaf = path->nodes[0]; |
| 5750 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5751 | ret = btrfs_next_leaf(root, path); |
| 5752 | if (ret == 0) |
| 5753 | continue; |
| 5754 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5755 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5756 | break; |
| 5757 | } |
| 5758 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5759 | |
| 5760 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5761 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 5762 | ret = 0; |
| 5763 | goto out; |
| 5764 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5765 | path->slots[0]++; |
| 5766 | } |
| 5767 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5768 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5769 | return ret; |
| 5770 | } |
| 5771 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5772 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 5773 | { |
| 5774 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 5775 | struct btrfs_space_info *space_info; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5776 | struct rb_node *n; |
| 5777 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5778 | spin_lock(&info->block_group_cache_lock); |
| 5779 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 5780 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 5781 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5782 | rb_erase(&block_group->cache_node, |
| 5783 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5784 | spin_unlock(&info->block_group_cache_lock); |
| 5785 | |
| 5786 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5787 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5788 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5789 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5790 | |
| 5791 | WARN_ON(atomic_read(&block_group->count) != 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5792 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5793 | |
| 5794 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5795 | } |
| 5796 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 5797 | |
| 5798 | /* now that all the block groups are freed, go through and |
| 5799 | * free all the space_info structs. This is only called during |
| 5800 | * the final stages of unmount, and so we know nobody is |
| 5801 | * using them. We call synchronize_rcu() once before we start, |
| 5802 | * just to be on the safe side. |
| 5803 | */ |
| 5804 | synchronize_rcu(); |
| 5805 | |
| 5806 | while(!list_empty(&info->space_info)) { |
| 5807 | space_info = list_entry(info->space_info.next, |
| 5808 | struct btrfs_space_info, |
| 5809 | list); |
| 5810 | |
| 5811 | list_del(&space_info->list); |
| 5812 | kfree(space_info); |
| 5813 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5814 | return 0; |
| 5815 | } |
| 5816 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5817 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 5818 | { |
| 5819 | struct btrfs_path *path; |
| 5820 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5821 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5822 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5823 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5824 | struct btrfs_key key; |
| 5825 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5826 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5827 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5828 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5829 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5830 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5831 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5832 | path = btrfs_alloc_path(); |
| 5833 | if (!path) |
| 5834 | return -ENOMEM; |
| 5835 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5836 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5837 | ret = find_first_block_group(root, path, &key); |
| 5838 | if (ret > 0) { |
| 5839 | ret = 0; |
| 5840 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5841 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5842 | if (ret != 0) |
| 5843 | goto error; |
| 5844 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5845 | leaf = path->nodes[0]; |
| 5846 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5847 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5848 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5849 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5850 | break; |
| 5851 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5852 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5853 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5854 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5855 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5856 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5857 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5858 | read_extent_buffer(leaf, &cache->item, |
| 5859 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 5860 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5861 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5862 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5863 | key.objectid = found_key.objectid + found_key.offset; |
| 5864 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5865 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5866 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5867 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 5868 | btrfs_block_group_used(&cache->item), |
| 5869 | &space_info); |
| 5870 | BUG_ON(ret); |
| 5871 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5872 | down_write(&space_info->groups_sem); |
| 5873 | list_add_tail(&cache->list, &space_info->block_groups); |
| 5874 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5875 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5876 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5877 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 5878 | |
| 5879 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5880 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 5881 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5882 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5883 | ret = 0; |
| 5884 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5885 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5886 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5887 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5888 | |
| 5889 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 5890 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5891 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5892 | u64 size) |
| 5893 | { |
| 5894 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5895 | struct btrfs_root *extent_root; |
| 5896 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5897 | |
| 5898 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5899 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5900 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
| 5901 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5902 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5903 | if (!cache) |
| 5904 | return -ENOMEM; |
| 5905 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5906 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5907 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5908 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 5909 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5910 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5911 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5912 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5913 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5914 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5915 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5916 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 5917 | cache->flags = type; |
| 5918 | btrfs_set_block_group_flags(&cache->item, type); |
| 5919 | |
| 5920 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 5921 | &cache->space_info); |
| 5922 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5923 | down_write(&cache->space_info->groups_sem); |
| 5924 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 5925 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5926 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5927 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5928 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5929 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5930 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 5931 | sizeof(cache->item)); |
| 5932 | BUG_ON(ret); |
| 5933 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 5934 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5935 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5936 | return 0; |
| 5937 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5938 | |
| 5939 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 5940 | struct btrfs_root *root, u64 group_start) |
| 5941 | { |
| 5942 | struct btrfs_path *path; |
| 5943 | struct btrfs_block_group_cache *block_group; |
| 5944 | struct btrfs_key key; |
| 5945 | int ret; |
| 5946 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5947 | root = root->fs_info->extent_root; |
| 5948 | |
| 5949 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 5950 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5951 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5952 | |
| 5953 | memcpy(&key, &block_group->key, sizeof(key)); |
| 5954 | |
| 5955 | path = btrfs_alloc_path(); |
| 5956 | BUG_ON(!path); |
| 5957 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5958 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5959 | rb_erase(&block_group->cache_node, |
| 5960 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5961 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 5962 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5963 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5964 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5965 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5966 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5967 | spin_lock(&block_group->space_info->lock); |
| 5968 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 5969 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 5970 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5971 | block_group->space_info->full = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5972 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5973 | put_block_group(block_group); |
| 5974 | put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5975 | |
| 5976 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 5977 | if (ret > 0) |
| 5978 | ret = -EIO; |
| 5979 | if (ret < 0) |
| 5980 | goto out; |
| 5981 | |
| 5982 | ret = btrfs_del_item(trans, root, path); |
| 5983 | out: |
| 5984 | btrfs_free_path(path); |
| 5985 | return ret; |
| 5986 | } |