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 | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 22 | #include "hash.h" |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 23 | #include "crc32c.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 24 | #include "ctree.h" |
| 25 | #include "disk-io.h" |
| 26 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 27 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 28 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 29 | #include "locking.h" |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 30 | #include "ref-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 31 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 32 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
| 33 | btrfs_root *extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 34 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 35 | btrfs_root *extent_root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 36 | static struct btrfs_block_group_cache * |
| 37 | __btrfs_find_block_group(struct btrfs_root *root, |
| 38 | struct btrfs_block_group_cache *hint, |
| 39 | u64 search_start, int data, int owner); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 40 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 41 | void maybe_lock_mutex(struct btrfs_root *root) |
| 42 | { |
| 43 | if (root != root->fs_info->extent_root && |
| 44 | root != root->fs_info->chunk_root && |
| 45 | root != root->fs_info->dev_root) { |
| 46 | mutex_lock(&root->fs_info->alloc_mutex); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void maybe_unlock_mutex(struct btrfs_root *root) |
| 51 | { |
| 52 | if (root != root->fs_info->extent_root && |
| 53 | root != root->fs_info->chunk_root && |
| 54 | root != root->fs_info->dev_root) { |
| 55 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 56 | } |
| 57 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 58 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 59 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 60 | { |
| 61 | return (cache->flags & bits) == bits; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * this adds the block group to the fs_info rb tree for the block group |
| 66 | * cache |
| 67 | */ |
| 68 | int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
| 69 | struct btrfs_block_group_cache *block_group) |
| 70 | { |
| 71 | struct rb_node **p; |
| 72 | struct rb_node *parent = NULL; |
| 73 | struct btrfs_block_group_cache *cache; |
| 74 | |
| 75 | spin_lock(&info->block_group_cache_lock); |
| 76 | p = &info->block_group_cache_tree.rb_node; |
| 77 | |
| 78 | while (*p) { |
| 79 | parent = *p; |
| 80 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 81 | cache_node); |
| 82 | if (block_group->key.objectid < cache->key.objectid) { |
| 83 | p = &(*p)->rb_left; |
| 84 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 85 | p = &(*p)->rb_right; |
| 86 | } else { |
| 87 | spin_unlock(&info->block_group_cache_lock); |
| 88 | return -EEXIST; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | rb_link_node(&block_group->cache_node, parent, p); |
| 93 | rb_insert_color(&block_group->cache_node, |
| 94 | &info->block_group_cache_tree); |
| 95 | spin_unlock(&info->block_group_cache_lock); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * This will return the block group at or after bytenr if contains is 0, else |
| 102 | * it will return the block group that contains the bytenr |
| 103 | */ |
| 104 | static struct btrfs_block_group_cache * |
| 105 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 106 | int contains) |
| 107 | { |
| 108 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 109 | struct rb_node *n; |
| 110 | u64 end, start; |
| 111 | |
| 112 | spin_lock(&info->block_group_cache_lock); |
| 113 | n = info->block_group_cache_tree.rb_node; |
| 114 | |
| 115 | while (n) { |
| 116 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 117 | cache_node); |
| 118 | end = cache->key.objectid + cache->key.offset - 1; |
| 119 | start = cache->key.objectid; |
| 120 | |
| 121 | if (bytenr < start) { |
| 122 | if (!contains && (!ret || start < ret->key.objectid)) |
| 123 | ret = cache; |
| 124 | n = n->rb_left; |
| 125 | } else if (bytenr > start) { |
| 126 | if (contains && bytenr <= end) { |
| 127 | ret = cache; |
| 128 | break; |
| 129 | } |
| 130 | n = n->rb_right; |
| 131 | } else { |
| 132 | ret = cache; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | spin_unlock(&info->block_group_cache_lock); |
| 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * this is only called by cache_block_group, since we could have freed extents |
| 143 | * we need to check the pinned_extents for any extents that can't be used yet |
| 144 | * since their free space will be released as soon as the transaction commits. |
| 145 | */ |
| 146 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 147 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 148 | { |
| 149 | u64 extent_start, extent_end, size; |
| 150 | int ret; |
| 151 | |
| 152 | while (start < end) { |
| 153 | ret = find_first_extent_bit(&info->pinned_extents, start, |
| 154 | &extent_start, &extent_end, |
| 155 | EXTENT_DIRTY); |
| 156 | if (ret) |
| 157 | break; |
| 158 | |
| 159 | if (extent_start == start) { |
| 160 | start = extent_end + 1; |
| 161 | } else if (extent_start > start && extent_start < end) { |
| 162 | size = extent_start - start; |
| 163 | ret = btrfs_add_free_space(block_group, start, size); |
| 164 | BUG_ON(ret); |
| 165 | start = extent_end + 1; |
| 166 | } else { |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if (start < end) { |
| 172 | size = end - start; |
| 173 | ret = btrfs_add_free_space(block_group, start, size); |
| 174 | BUG_ON(ret); |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 180 | static int cache_block_group(struct btrfs_root *root, |
| 181 | struct btrfs_block_group_cache *block_group) |
| 182 | { |
| 183 | struct btrfs_path *path; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 184 | int ret = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 185 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 186 | struct extent_buffer *leaf; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 187 | int slot; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 188 | u64 last = 0; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 189 | u64 first_free; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 190 | int found = 0; |
| 191 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 192 | if (!block_group) |
| 193 | return 0; |
| 194 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 195 | root = root->fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 196 | |
| 197 | if (block_group->cached) |
| 198 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 199 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 200 | path = btrfs_alloc_path(); |
| 201 | if (!path) |
| 202 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 203 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 204 | path->reada = 2; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 205 | /* |
| 206 | * we get into deadlocks with paths held by callers of this function. |
| 207 | * since the alloc_mutex is protecting things right now, just |
| 208 | * skip the locking here |
| 209 | */ |
| 210 | path->skip_locking = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 211 | first_free = max_t(u64, block_group->key.objectid, |
| 212 | BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 213 | key.objectid = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 214 | key.offset = 0; |
| 215 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 216 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 217 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 218 | goto err; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 219 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 220 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 221 | goto err; |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 222 | if (ret == 0) { |
| 223 | leaf = path->nodes[0]; |
| 224 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 225 | if (key.objectid + key.offset > first_free) |
| 226 | first_free = key.objectid + key.offset; |
| 227 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 228 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 229 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 230 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 231 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 232 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 233 | if (ret < 0) |
| 234 | goto err; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 235 | if (ret == 0) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 236 | continue; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 237 | else |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 238 | break; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 239 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 240 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 241 | if (key.objectid < block_group->key.objectid) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 242 | goto next; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 243 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 244 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 245 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 246 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 247 | |
| 248 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
| 249 | if (!found) { |
| 250 | last = first_free; |
| 251 | found = 1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 252 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 253 | |
| 254 | add_new_free_space(block_group, root->fs_info, last, |
| 255 | key.objectid); |
| 256 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 257 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 258 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 259 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 260 | path->slots[0]++; |
| 261 | } |
| 262 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 263 | if (!found) |
| 264 | last = first_free; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 265 | |
| 266 | add_new_free_space(block_group, root->fs_info, last, |
| 267 | block_group->key.objectid + |
| 268 | block_group->key.offset); |
| 269 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 270 | block_group->cached = 1; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 271 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 272 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 273 | btrfs_free_path(path); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 274 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 277 | /* |
| 278 | * return the block group that starts at or after bytenr |
| 279 | */ |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 280 | struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct |
| 281 | btrfs_fs_info *info, |
| 282 | u64 bytenr) |
| 283 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 284 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 285 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 286 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 287 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 288 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 291 | /* |
| 292 | * return the block group that contains teh given bytenr |
| 293 | */ |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 294 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
| 295 | btrfs_fs_info *info, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 296 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 297 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 298 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 299 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 300 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 301 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 302 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 303 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 304 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 305 | static int noinline find_free_space(struct btrfs_root *root, |
| 306 | struct btrfs_block_group_cache **cache_ret, |
| 307 | u64 *start_ret, u64 num, int data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 308 | { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 309 | int ret; |
| 310 | struct btrfs_block_group_cache *cache = *cache_ret; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 311 | struct btrfs_free_space *info = NULL; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 312 | u64 last; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 313 | u64 total_fs_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 314 | u64 search_start = *start_ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 315 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 316 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 317 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 318 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 319 | if (!cache) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 320 | goto out; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 321 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 322 | last = max(search_start, cache->key.objectid); |
| 323 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 324 | again: |
| 325 | ret = cache_block_group(root, cache); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 326 | if (ret) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 327 | goto out; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 328 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 329 | if (cache->ro || !block_group_bits(cache, data)) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 330 | goto new_group; |
| 331 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 332 | info = btrfs_find_free_space(cache, last, num); |
| 333 | if (info) { |
| 334 | *start_ret = info->offset; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 335 | return 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 336 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 337 | |
| 338 | new_group: |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 339 | last = cache->key.objectid + cache->key.offset; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 340 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 341 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 342 | if (!cache || cache->key.objectid >= total_fs_bytes) |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 343 | goto out; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 344 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 345 | *cache_ret = cache; |
| 346 | goto again; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 347 | |
| 348 | out: |
| 349 | return -ENOSPC; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 352 | static u64 div_factor(u64 num, int factor) |
| 353 | { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 354 | if (factor == 10) |
| 355 | return num; |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 356 | num *= factor; |
| 357 | do_div(num, 10); |
| 358 | return num; |
| 359 | } |
| 360 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 361 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 362 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 363 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 364 | struct list_head *head = &info->space_info; |
| 365 | struct list_head *cur; |
| 366 | struct btrfs_space_info *found; |
| 367 | list_for_each(cur, head) { |
| 368 | found = list_entry(cur, struct btrfs_space_info, list); |
| 369 | if (found->flags == flags) |
| 370 | return found; |
| 371 | } |
| 372 | return NULL; |
| 373 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 376 | static struct btrfs_block_group_cache * |
| 377 | __btrfs_find_block_group(struct btrfs_root *root, |
| 378 | struct btrfs_block_group_cache *hint, |
| 379 | u64 search_start, int data, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 380 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 381 | struct btrfs_block_group_cache *cache; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 382 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 383 | struct btrfs_fs_info *info = root->fs_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 384 | struct btrfs_space_info *sinfo; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 385 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 386 | u64 last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 387 | u64 free_check; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 388 | int full_search = 0; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 389 | int factor = 10; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 390 | int wrapped = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 391 | |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 392 | if (data & BTRFS_BLOCK_GROUP_METADATA) |
| 393 | factor = 9; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 394 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 395 | if (search_start) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 396 | struct btrfs_block_group_cache *shint; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 397 | shint = btrfs_lookup_first_block_group(info, search_start); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 398 | if (shint && block_group_bits(shint, data) && !shint->ro) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 399 | spin_lock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 400 | used = btrfs_block_group_used(&shint->item); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 401 | if (used + shint->pinned < |
| 402 | div_factor(shint->key.offset, factor)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 403 | spin_unlock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 404 | return shint; |
| 405 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 406 | spin_unlock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 407 | } |
| 408 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 409 | if (hint && !hint->ro && block_group_bits(hint, data)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 410 | spin_lock(&hint->lock); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 411 | used = btrfs_block_group_used(&hint->item); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 412 | if (used + hint->pinned < |
| 413 | div_factor(hint->key.offset, factor)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 414 | spin_unlock(&hint->lock); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 415 | return hint; |
| 416 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 417 | spin_unlock(&hint->lock); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 418 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 419 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 420 | if (hint) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 421 | last = max(hint->key.objectid, search_start); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 422 | else |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 423 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 424 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 425 | sinfo = __find_space_info(root->fs_info, data); |
| 426 | if (!sinfo) |
| 427 | goto found; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 428 | again: |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 429 | while(1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 430 | struct list_head *l; |
| 431 | |
| 432 | cache = NULL; |
| 433 | |
| 434 | spin_lock(&sinfo->lock); |
| 435 | list_for_each(l, &sinfo->block_groups) { |
| 436 | struct btrfs_block_group_cache *entry; |
| 437 | entry = list_entry(l, struct btrfs_block_group_cache, |
| 438 | list); |
| 439 | if ((entry->key.objectid >= last) && |
| 440 | (!cache || (entry->key.objectid < |
| 441 | cache->key.objectid))) |
| 442 | cache = entry; |
| 443 | } |
| 444 | spin_unlock(&sinfo->lock); |
| 445 | |
| 446 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 447 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 448 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 449 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 450 | last = cache->key.objectid + cache->key.offset; |
| 451 | used = btrfs_block_group_used(&cache->item); |
| 452 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 453 | if (!cache->ro && block_group_bits(cache, data)) { |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 454 | free_check = div_factor(cache->key.offset, factor); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 455 | if (used + cache->pinned < free_check) { |
| 456 | found_group = cache; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 457 | spin_unlock(&cache->lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 458 | goto found; |
| 459 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 460 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 461 | spin_unlock(&cache->lock); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 462 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 463 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 464 | if (!wrapped) { |
| 465 | last = search_start; |
| 466 | wrapped = 1; |
| 467 | goto again; |
| 468 | } |
| 469 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 470 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 471 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 472 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 473 | goto again; |
| 474 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 475 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 476 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 477 | } |
| 478 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 479 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 480 | struct btrfs_block_group_cache |
| 481 | *hint, u64 search_start, |
| 482 | int data, int owner) |
| 483 | { |
| 484 | |
| 485 | struct btrfs_block_group_cache *ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 486 | ret = __btrfs_find_block_group(root, hint, search_start, data, owner); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 487 | return ret; |
| 488 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 489 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 490 | static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 491 | u64 owner, u64 owner_offset) |
| 492 | { |
| 493 | u32 high_crc = ~(u32)0; |
| 494 | u32 low_crc = ~(u32)0; |
| 495 | __le64 lenum; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 496 | lenum = cpu_to_le64(root_objectid); |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 497 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 498 | lenum = cpu_to_le64(ref_generation); |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 499 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 21a4989 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 500 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) { |
| 501 | lenum = cpu_to_le64(owner); |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 502 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 21a4989 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 503 | lenum = cpu_to_le64(owner_offset); |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 504 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 21a4989 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 505 | } |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 506 | return ((u64)high_crc << 32) | (u64)low_crc; |
| 507 | } |
| 508 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 509 | static int match_extent_ref(struct extent_buffer *leaf, |
| 510 | struct btrfs_extent_ref *disk_ref, |
| 511 | struct btrfs_extent_ref *cpu_ref) |
| 512 | { |
| 513 | int ret; |
| 514 | int len; |
| 515 | |
| 516 | if (cpu_ref->objectid) |
| 517 | len = sizeof(*cpu_ref); |
| 518 | else |
| 519 | len = 2 * sizeof(u64); |
| 520 | ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref, |
| 521 | len); |
| 522 | return ret == 0; |
| 523 | } |
| 524 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 525 | /* simple helper to search for an existing extent at a given offset */ |
| 526 | int btrfs_lookup_extent(struct btrfs_root *root, struct btrfs_path *path, |
| 527 | u64 start, u64 len) |
| 528 | { |
| 529 | int ret; |
| 530 | struct btrfs_key key; |
| 531 | |
| 532 | maybe_lock_mutex(root); |
| 533 | key.objectid = start; |
| 534 | key.offset = len; |
| 535 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 536 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 537 | 0, 0); |
| 538 | maybe_unlock_mutex(root); |
| 539 | return ret; |
| 540 | } |
| 541 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 542 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 543 | struct btrfs_root *root, |
| 544 | struct btrfs_path *path, u64 bytenr, |
| 545 | u64 root_objectid, |
| 546 | u64 ref_generation, u64 owner, |
| 547 | u64 owner_offset, int del) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 548 | { |
| 549 | u64 hash; |
| 550 | struct btrfs_key key; |
| 551 | struct btrfs_key found_key; |
| 552 | struct btrfs_extent_ref ref; |
| 553 | struct extent_buffer *leaf; |
| 554 | struct btrfs_extent_ref *disk_ref; |
| 555 | int ret; |
| 556 | int ret2; |
| 557 | |
| 558 | btrfs_set_stack_ref_root(&ref, root_objectid); |
| 559 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
| 560 | btrfs_set_stack_ref_objectid(&ref, owner); |
| 561 | btrfs_set_stack_ref_offset(&ref, owner_offset); |
| 562 | |
| 563 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
| 564 | owner_offset); |
| 565 | key.offset = hash; |
| 566 | key.objectid = bytenr; |
| 567 | key.type = BTRFS_EXTENT_REF_KEY; |
| 568 | |
| 569 | while (1) { |
| 570 | ret = btrfs_search_slot(trans, root, &key, path, |
| 571 | del ? -1 : 0, del); |
| 572 | if (ret < 0) |
| 573 | goto out; |
| 574 | leaf = path->nodes[0]; |
| 575 | if (ret != 0) { |
| 576 | u32 nritems = btrfs_header_nritems(leaf); |
| 577 | if (path->slots[0] >= nritems) { |
| 578 | ret2 = btrfs_next_leaf(root, path); |
| 579 | if (ret2) |
| 580 | goto out; |
| 581 | leaf = path->nodes[0]; |
| 582 | } |
| 583 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 584 | if (found_key.objectid != bytenr || |
| 585 | found_key.type != BTRFS_EXTENT_REF_KEY) |
| 586 | goto out; |
| 587 | key.offset = found_key.offset; |
| 588 | if (del) { |
| 589 | btrfs_release_path(root, path); |
| 590 | continue; |
| 591 | } |
| 592 | } |
| 593 | disk_ref = btrfs_item_ptr(path->nodes[0], |
| 594 | path->slots[0], |
| 595 | struct btrfs_extent_ref); |
| 596 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) { |
| 597 | ret = 0; |
| 598 | goto out; |
| 599 | } |
| 600 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 601 | key.offset = found_key.offset + 1; |
| 602 | btrfs_release_path(root, path); |
| 603 | } |
| 604 | out: |
| 605 | return ret; |
| 606 | } |
| 607 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 608 | /* |
| 609 | * Back reference rules. Back refs have three main goals: |
| 610 | * |
| 611 | * 1) differentiate between all holders of references to an extent so that |
| 612 | * when a reference is dropped we can make sure it was a valid reference |
| 613 | * before freeing the extent. |
| 614 | * |
| 615 | * 2) Provide enough information to quickly find the holders of an extent |
| 616 | * if we notice a given block is corrupted or bad. |
| 617 | * |
| 618 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 619 | * maintenance. This is actually the same as #2, but with a slightly |
| 620 | * different use case. |
| 621 | * |
| 622 | * File extents can be referenced by: |
| 623 | * |
| 624 | * - multiple snapshots, subvolumes, or different generations in one subvol |
| 625 | * - different files inside a single subvolume (in theory, not implemented yet) |
| 626 | * - different offsets inside a file (bookend extents in file.c) |
| 627 | * |
| 628 | * The extent ref structure has fields for: |
| 629 | * |
| 630 | * - Objectid of the subvolume root |
| 631 | * - Generation number of the tree holding the reference |
| 632 | * - objectid of the file holding the reference |
| 633 | * - offset in the file corresponding to the key holding the reference |
| 634 | * |
| 635 | * When a file extent is allocated the fields are filled in: |
| 636 | * (root_key.objectid, trans->transid, inode objectid, offset in file) |
| 637 | * |
| 638 | * When a leaf is cow'd new references are added for every file extent found |
| 639 | * in the leaf. It looks the same as the create case, but trans->transid |
| 640 | * will be different when the block is cow'd. |
| 641 | * |
| 642 | * (root_key.objectid, trans->transid, inode objectid, offset in file) |
| 643 | * |
| 644 | * When a file extent is removed either during snapshot deletion or file |
| 645 | * truncation, the corresponding back reference is found |
| 646 | * by searching for: |
| 647 | * |
| 648 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 649 | * inode objectid, offset in file) |
| 650 | * |
| 651 | * Btree extents can be referenced by: |
| 652 | * |
| 653 | * - Different subvolumes |
| 654 | * - Different generations of the same subvolume |
| 655 | * |
| 656 | * Storing sufficient information for a full reverse mapping of a btree |
| 657 | * block would require storing the lowest key of the block in the backref, |
| 658 | * and it would require updating that lowest key either before write out or |
| 659 | * every time it changed. Instead, the objectid of the lowest key is stored |
| 660 | * along with the level of the tree block. This provides a hint |
| 661 | * about where in the btree the block can be found. Searches through the |
| 662 | * btree only need to look for a pointer to that block, so they stop one |
| 663 | * level higher than the level recorded in the backref. |
| 664 | * |
| 665 | * Some btrees do not do reference counting on their extents. These |
| 666 | * include the extent tree and the tree of tree roots. Backrefs for these |
| 667 | * trees always have a generation of zero. |
| 668 | * |
| 669 | * When a tree block is created, back references are inserted: |
| 670 | * |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 671 | * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 672 | * |
| 673 | * When a tree block is cow'd in a reference counted root, |
| 674 | * new back references are added for all the blocks it points to. |
| 675 | * These are of the form (trans->transid will have increased since creation): |
| 676 | * |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 677 | * (root->root_key.objectid, trans->transid, level, lowest_key_objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 678 | * |
| 679 | * Because the lowest_key_objectid and the level are just hints |
| 680 | * they are not used when backrefs are deleted. When a backref is deleted: |
| 681 | * |
| 682 | * if backref was for a tree root: |
| 683 | * root_objectid = root->root_key.objectid |
| 684 | * else |
| 685 | * root_objectid = btrfs_header_owner(parent) |
| 686 | * |
| 687 | * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0) |
| 688 | * |
| 689 | * Back Reference Key hashing: |
| 690 | * |
| 691 | * Back references have four fields, each 64 bits long. Unfortunately, |
| 692 | * This is hashed into a single 64 bit number and placed into the key offset. |
| 693 | * The key objectid corresponds to the first byte in the extent, and the |
| 694 | * key type is set to BTRFS_EXTENT_REF_KEY |
| 695 | */ |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 696 | int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, |
| 697 | struct btrfs_root *root, |
| 698 | struct btrfs_path *path, u64 bytenr, |
| 699 | u64 root_objectid, u64 ref_generation, |
| 700 | u64 owner, u64 owner_offset) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 701 | { |
| 702 | u64 hash; |
| 703 | struct btrfs_key key; |
| 704 | struct btrfs_extent_ref ref; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 705 | struct btrfs_extent_ref *disk_ref; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 706 | int ret; |
| 707 | |
| 708 | btrfs_set_stack_ref_root(&ref, root_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 709 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 710 | btrfs_set_stack_ref_objectid(&ref, owner); |
| 711 | btrfs_set_stack_ref_offset(&ref, owner_offset); |
| 712 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 713 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
| 714 | owner_offset); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 715 | key.offset = hash; |
| 716 | key.objectid = bytenr; |
| 717 | key.type = BTRFS_EXTENT_REF_KEY; |
| 718 | |
| 719 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref)); |
| 720 | while (ret == -EEXIST) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 721 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 722 | struct btrfs_extent_ref); |
| 723 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) |
| 724 | goto out; |
| 725 | key.offset++; |
| 726 | btrfs_release_path(root, path); |
| 727 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 728 | sizeof(ref)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 729 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 730 | if (ret) |
| 731 | goto out; |
| 732 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 733 | struct btrfs_extent_ref); |
| 734 | write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref, |
| 735 | sizeof(ref)); |
| 736 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 737 | out: |
| 738 | btrfs_release_path(root, path); |
| 739 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 740 | } |
| 741 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 742 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 743 | struct btrfs_root *root, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 744 | u64 bytenr, u64 num_bytes, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 745 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 746 | u64 owner, u64 owner_offset) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 747 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 748 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 749 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 750 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 751 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 752 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 753 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 754 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 755 | WARN_ON(num_bytes < root->sectorsize); |
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 | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 761 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 762 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 763 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 764 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 765 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 766 | if (ret < 0) |
| 767 | return ret; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 768 | if (ret != 0) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 769 | BUG(); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 770 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 771 | BUG_ON(ret != 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 772 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 773 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 774 | refs = btrfs_extent_refs(l, item); |
| 775 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 776 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 777 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 778 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 779 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 780 | path->reada = 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 781 | ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root, |
| 782 | path, bytenr, root_objectid, |
| 783 | ref_generation, owner, owner_offset); |
| 784 | BUG_ON(ret); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 785 | finish_current_insert(trans, root->fs_info->extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 786 | del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 787 | |
| 788 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 789 | return 0; |
| 790 | } |
| 791 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 792 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 793 | struct btrfs_root *root, |
| 794 | u64 bytenr, u64 num_bytes, |
| 795 | u64 root_objectid, u64 ref_generation, |
| 796 | u64 owner, u64 owner_offset) |
| 797 | { |
| 798 | int ret; |
| 799 | |
| 800 | mutex_lock(&root->fs_info->alloc_mutex); |
| 801 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, |
| 802 | root_objectid, ref_generation, |
| 803 | owner, owner_offset); |
| 804 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 805 | return ret; |
| 806 | } |
| 807 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 808 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 809 | struct btrfs_root *root) |
| 810 | { |
| 811 | finish_current_insert(trans, root->fs_info->extent_root); |
| 812 | del_pending_extents(trans, root->fs_info->extent_root); |
| 813 | return 0; |
| 814 | } |
| 815 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 816 | static int lookup_extent_ref(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 817 | struct btrfs_root *root, u64 bytenr, |
| 818 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 819 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 820 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 821 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 822 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 823 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 824 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 825 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 826 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 827 | path = btrfs_alloc_path(); |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 828 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 829 | key.objectid = bytenr; |
| 830 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 831 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 832 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 833 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 834 | if (ret < 0) |
| 835 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 836 | if (ret != 0) { |
| 837 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 838 | printk("failed to find block number %Lu\n", bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 839 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 840 | } |
| 841 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 842 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 843 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 844 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 845 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 849 | |
| 850 | static int get_reference_status(struct btrfs_root *root, u64 bytenr, |
| 851 | u64 parent_gen, u64 ref_objectid, |
| 852 | u64 *min_generation, u32 *ref_count) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 853 | { |
| 854 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 855 | struct btrfs_path *path; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 856 | struct extent_buffer *leaf; |
| 857 | struct btrfs_extent_ref *ref_item; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 858 | struct btrfs_key key; |
| 859 | struct btrfs_key found_key; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 860 | u64 root_objectid = root->root_key.objectid; |
| 861 | u64 ref_generation; |
| 862 | u32 nritems; |
| 863 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 864 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 865 | key.objectid = bytenr; |
| 866 | key.offset = 0; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 867 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 868 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 869 | path = btrfs_alloc_path(); |
| 870 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 871 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 872 | if (ret < 0) |
| 873 | goto out; |
| 874 | BUG_ON(ret == 0); |
| 875 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 876 | leaf = path->nodes[0]; |
| 877 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 878 | |
| 879 | if (found_key.objectid != bytenr || |
| 880 | found_key.type != BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 881 | ret = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 882 | goto out; |
| 883 | } |
| 884 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 885 | *ref_count = 0; |
| 886 | *min_generation = (u64)-1; |
| 887 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 888 | while (1) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 889 | leaf = path->nodes[0]; |
| 890 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 891 | if (path->slots[0] >= nritems) { |
| 892 | ret = btrfs_next_leaf(extent_root, path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 893 | if (ret < 0) |
| 894 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 895 | if (ret == 0) |
| 896 | continue; |
| 897 | break; |
| 898 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 899 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 900 | if (found_key.objectid != bytenr) |
| 901 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 902 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 903 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 904 | path->slots[0]++; |
| 905 | continue; |
| 906 | } |
| 907 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 908 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 909 | struct btrfs_extent_ref); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 910 | ref_generation = btrfs_ref_generation(leaf, ref_item); |
| 911 | /* |
| 912 | * For (parent_gen > 0 && parent_gen > ref_gen): |
| 913 | * |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 914 | * we reach here through the oldest root, therefore |
| 915 | * all other reference from same snapshot should have |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 916 | * a larger generation. |
| 917 | */ |
| 918 | if ((root_objectid != btrfs_ref_root(leaf, ref_item)) || |
| 919 | (parent_gen > 0 && parent_gen > ref_generation) || |
| 920 | (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID && |
| 921 | ref_objectid != btrfs_ref_objectid(leaf, ref_item))) { |
| 922 | if (ref_count) |
| 923 | *ref_count = 2; |
| 924 | break; |
| 925 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 926 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 927 | *ref_count = 1; |
| 928 | if (*min_generation > ref_generation) |
| 929 | *min_generation = ref_generation; |
| 930 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 931 | path->slots[0]++; |
| 932 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 933 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 934 | out: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 935 | mutex_unlock(&root->fs_info->alloc_mutex); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 936 | btrfs_free_path(path); |
| 937 | return ret; |
| 938 | } |
| 939 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 940 | int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans, |
| 941 | struct btrfs_root *root, |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 942 | struct btrfs_key *key, u64 bytenr) |
| 943 | { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 944 | struct btrfs_root *old_root; |
| 945 | struct btrfs_path *path = NULL; |
| 946 | struct extent_buffer *eb; |
| 947 | struct btrfs_file_extent_item *item; |
| 948 | u64 ref_generation; |
| 949 | u64 min_generation; |
| 950 | u64 extent_start; |
| 951 | u32 ref_count; |
| 952 | int level; |
| 953 | int ret; |
| 954 | |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 955 | BUG_ON(trans == NULL); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 956 | BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY); |
| 957 | ret = get_reference_status(root, bytenr, 0, key->objectid, |
| 958 | &min_generation, &ref_count); |
| 959 | if (ret) |
| 960 | return ret; |
| 961 | |
| 962 | if (ref_count != 1) |
| 963 | return 1; |
| 964 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 965 | old_root = root->dirty_root->root; |
| 966 | ref_generation = old_root->root_key.offset; |
| 967 | |
| 968 | /* all references are created in running transaction */ |
| 969 | if (min_generation > ref_generation) { |
| 970 | ret = 0; |
| 971 | goto out; |
| 972 | } |
| 973 | |
| 974 | path = btrfs_alloc_path(); |
| 975 | if (!path) { |
| 976 | ret = -ENOMEM; |
| 977 | goto out; |
| 978 | } |
| 979 | |
| 980 | path->skip_locking = 1; |
| 981 | /* if no item found, the extent is referenced by other snapshot */ |
| 982 | ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0); |
| 983 | if (ret) |
| 984 | goto out; |
| 985 | |
| 986 | eb = path->nodes[0]; |
| 987 | item = btrfs_item_ptr(eb, path->slots[0], |
| 988 | struct btrfs_file_extent_item); |
| 989 | if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG || |
| 990 | btrfs_file_extent_disk_bytenr(eb, item) != bytenr) { |
| 991 | ret = 1; |
| 992 | goto out; |
| 993 | } |
| 994 | |
| 995 | for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) { |
| 996 | if (level >= 0) { |
| 997 | eb = path->nodes[level]; |
| 998 | if (!eb) |
| 999 | continue; |
| 1000 | extent_start = eb->start; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1001 | } else |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1002 | extent_start = bytenr; |
| 1003 | |
| 1004 | ret = get_reference_status(root, extent_start, ref_generation, |
| 1005 | 0, &min_generation, &ref_count); |
| 1006 | if (ret) |
| 1007 | goto out; |
| 1008 | |
| 1009 | if (ref_count != 1) { |
| 1010 | ret = 1; |
| 1011 | goto out; |
| 1012 | } |
| 1013 | if (level >= 0) |
| 1014 | ref_generation = btrfs_header_generation(eb); |
| 1015 | } |
| 1016 | ret = 0; |
| 1017 | out: |
| 1018 | if (path) |
| 1019 | btrfs_free_path(path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1020 | return ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1021 | } |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1022 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1023 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1024 | struct extent_buffer *buf, int cache_ref) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1025 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1026 | u64 bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1027 | u32 nritems; |
| 1028 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1029 | struct btrfs_file_extent_item *fi; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1030 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1031 | int level; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1032 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1033 | int faili; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1034 | int nr_file_extents = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1035 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1036 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1037 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1038 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1039 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1040 | nritems = btrfs_header_nritems(buf); |
| 1041 | for (i = 0; i < nritems; i++) { |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 1042 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1043 | if (level == 0) { |
| 1044 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1045 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1046 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1047 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1048 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1049 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1050 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 1051 | BTRFS_FILE_EXTENT_INLINE) |
| 1052 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1053 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1054 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 1055 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1056 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1057 | if (buf != root->commit_root) |
| 1058 | nr_file_extents++; |
| 1059 | |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1060 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1061 | ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1062 | btrfs_file_extent_disk_num_bytes(buf, fi), |
| 1063 | root->root_key.objectid, trans->transid, |
| 1064 | key.objectid, key.offset); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1065 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1066 | if (ret) { |
| 1067 | faili = i; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1068 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1069 | goto fail; |
| 1070 | } |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1071 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1072 | bytenr = btrfs_node_blockptr(buf, i); |
Chris Mason | 6caab48 | 2007-12-13 09:48:07 -0500 | [diff] [blame] | 1073 | btrfs_node_key_to_cpu(buf, &key, i); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1074 | |
| 1075 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1076 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1077 | btrfs_level_size(root, level - 1), |
| 1078 | root->root_key.objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 1079 | trans->transid, |
| 1080 | level - 1, key.objectid); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1081 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1082 | if (ret) { |
| 1083 | faili = i; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1084 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1085 | goto fail; |
| 1086 | } |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1087 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1088 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1089 | /* cache orignal leaf block's references */ |
| 1090 | if (level == 0 && cache_ref && buf != root->commit_root) { |
| 1091 | struct btrfs_leaf_ref *ref; |
| 1092 | struct btrfs_extent_info *info; |
| 1093 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1094 | ref = btrfs_alloc_leaf_ref(root, nr_file_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1095 | if (!ref) { |
| 1096 | WARN_ON(1); |
| 1097 | goto out; |
| 1098 | } |
| 1099 | |
Chris Mason | 47ac14f | 2008-07-31 09:46:18 -0400 | [diff] [blame] | 1100 | ref->root_gen = root->root_key.offset; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1101 | ref->bytenr = buf->start; |
| 1102 | ref->owner = btrfs_header_owner(buf); |
| 1103 | ref->generation = btrfs_header_generation(buf); |
| 1104 | ref->nritems = nr_file_extents; |
| 1105 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1106 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1107 | for (i = 0; nr_file_extents > 0 && i < nritems; i++) { |
| 1108 | u64 disk_bytenr; |
| 1109 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1110 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1111 | continue; |
| 1112 | fi = btrfs_item_ptr(buf, i, |
| 1113 | struct btrfs_file_extent_item); |
| 1114 | if (btrfs_file_extent_type(buf, fi) == |
| 1115 | BTRFS_FILE_EXTENT_INLINE) |
| 1116 | continue; |
| 1117 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1118 | if (disk_bytenr == 0) |
| 1119 | continue; |
| 1120 | |
| 1121 | info->bytenr = disk_bytenr; |
| 1122 | info->num_bytes = |
| 1123 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 1124 | info->objectid = key.objectid; |
| 1125 | info->offset = key.offset; |
| 1126 | info++; |
| 1127 | } |
| 1128 | |
| 1129 | BUG_ON(!root->ref_tree); |
| 1130 | ret = btrfs_add_leaf_ref(root, ref); |
| 1131 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1132 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1133 | } |
| 1134 | out: |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1135 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1136 | fail: |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1137 | WARN_ON(1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1138 | #if 0 |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1139 | for (i =0; i < faili; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1140 | if (level == 0) { |
| 1141 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1142 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1143 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1144 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1145 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1146 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1147 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1148 | BTRFS_FILE_EXTENT_INLINE) |
| 1149 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1150 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1151 | if (disk_bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1152 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1153 | err = btrfs_free_extent(trans, root, disk_bytenr, |
| 1154 | btrfs_file_extent_disk_num_bytes(buf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1155 | fi), 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1156 | BUG_ON(err); |
| 1157 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1158 | bytenr = btrfs_node_blockptr(buf, i); |
| 1159 | err = btrfs_free_extent(trans, root, bytenr, |
| 1160 | btrfs_level_size(root, level - 1), 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1161 | BUG_ON(err); |
| 1162 | } |
| 1163 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1164 | #endif |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1165 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1166 | } |
| 1167 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1168 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1169 | struct btrfs_root *root, |
| 1170 | struct btrfs_path *path, |
| 1171 | struct btrfs_block_group_cache *cache) |
| 1172 | { |
| 1173 | int ret; |
| 1174 | int pending_ret; |
| 1175 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1176 | unsigned long bi; |
| 1177 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1178 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1179 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1180 | if (ret < 0) |
| 1181 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1182 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1183 | |
| 1184 | leaf = path->nodes[0]; |
| 1185 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1186 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1187 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1188 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1189 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1190 | finish_current_insert(trans, extent_root); |
| 1191 | pending_ret = del_pending_extents(trans, extent_root); |
| 1192 | if (ret) |
| 1193 | return ret; |
| 1194 | if (pending_ret) |
| 1195 | return pending_ret; |
| 1196 | return 0; |
| 1197 | |
| 1198 | } |
| 1199 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1200 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1201 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1202 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1203 | struct btrfs_block_group_cache *cache, *entry; |
| 1204 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1205 | int err = 0; |
| 1206 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1207 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1208 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1209 | |
| 1210 | path = btrfs_alloc_path(); |
| 1211 | if (!path) |
| 1212 | return -ENOMEM; |
| 1213 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1214 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1215 | while(1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1216 | cache = NULL; |
| 1217 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1218 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1219 | n; n = rb_next(n)) { |
| 1220 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1221 | cache_node); |
| 1222 | if (entry->dirty) { |
| 1223 | cache = entry; |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1228 | |
| 1229 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1230 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1231 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1232 | last += cache->key.offset; |
| 1233 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1234 | err = write_one_cache_group(trans, root, |
| 1235 | path, cache); |
| 1236 | /* |
| 1237 | * if we fail to write the cache group, we want |
| 1238 | * to keep it marked dirty in hopes that a later |
| 1239 | * write will work |
| 1240 | */ |
| 1241 | if (err) { |
| 1242 | werr = err; |
| 1243 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1244 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1245 | |
| 1246 | cache->dirty = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1247 | } |
| 1248 | btrfs_free_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1249 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1250 | return werr; |
| 1251 | } |
| 1252 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1253 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1254 | u64 total_bytes, u64 bytes_used, |
| 1255 | struct btrfs_space_info **space_info) |
| 1256 | { |
| 1257 | struct btrfs_space_info *found; |
| 1258 | |
| 1259 | found = __find_space_info(info, flags); |
| 1260 | if (found) { |
| 1261 | found->total_bytes += total_bytes; |
| 1262 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1263 | found->full = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1264 | *space_info = found; |
| 1265 | return 0; |
| 1266 | } |
| 1267 | found = kmalloc(sizeof(*found), GFP_NOFS); |
| 1268 | if (!found) |
| 1269 | return -ENOMEM; |
| 1270 | |
| 1271 | list_add(&found->list, &info->space_info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1272 | INIT_LIST_HEAD(&found->block_groups); |
| 1273 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1274 | found->flags = flags; |
| 1275 | found->total_bytes = total_bytes; |
| 1276 | found->bytes_used = bytes_used; |
| 1277 | found->bytes_pinned = 0; |
| 1278 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1279 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1280 | *space_info = found; |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1284 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1285 | { |
| 1286 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1287 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1288 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1289 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1290 | if (extra_flags) { |
| 1291 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1292 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1293 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1294 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1295 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1296 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1297 | } |
| 1298 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1299 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1300 | static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1301 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1302 | u64 num_devices = root->fs_info->fs_devices->num_devices; |
| 1303 | |
| 1304 | if (num_devices == 1) |
| 1305 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1306 | if (num_devices < 4) |
| 1307 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1308 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1309 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1310 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1311 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1312 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1313 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1314 | |
| 1315 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1316 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1317 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1318 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1319 | |
| 1320 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1321 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1322 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1323 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1324 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1325 | return flags; |
| 1326 | } |
| 1327 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1328 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1329 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1330 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1331 | { |
| 1332 | struct btrfs_space_info *space_info; |
| 1333 | u64 thresh; |
| 1334 | u64 start; |
| 1335 | u64 num_bytes; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1336 | int ret = 0; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1337 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1338 | flags = reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1339 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1340 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1341 | if (!space_info) { |
| 1342 | ret = update_space_info(extent_root->fs_info, flags, |
| 1343 | 0, 0, &space_info); |
| 1344 | BUG_ON(ret); |
| 1345 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1346 | BUG_ON(!space_info); |
| 1347 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1348 | if (space_info->force_alloc) { |
| 1349 | force = 1; |
| 1350 | space_info->force_alloc = 0; |
| 1351 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1352 | if (space_info->full) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1353 | goto out; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1354 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1355 | thresh = div_factor(space_info->total_bytes, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1356 | if (!force && |
| 1357 | (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) < |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1358 | thresh) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1359 | goto out; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1360 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1361 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1362 | ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags); |
| 1363 | if (ret == -ENOSPC) { |
| 1364 | printk("space info full %Lu\n", flags); |
| 1365 | space_info->full = 1; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1366 | goto out_unlock; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1367 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1368 | BUG_ON(ret); |
| 1369 | |
| 1370 | ret = btrfs_make_block_group(trans, extent_root, 0, flags, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1371 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1372 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1373 | |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1374 | out_unlock: |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1375 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1376 | out: |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1377 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1378 | } |
| 1379 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1380 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1381 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1382 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1383 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1384 | { |
| 1385 | struct btrfs_block_group_cache *cache; |
| 1386 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1387 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1388 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1389 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1390 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1391 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1392 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1393 | cache = btrfs_lookup_block_group(info, bytenr); |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1394 | if (!cache) { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1395 | return -1; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1396 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1397 | byte_in_group = bytenr - cache->key.objectid; |
| 1398 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1399 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1400 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1401 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1402 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1403 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1404 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1405 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1406 | cache->space_info->bytes_used += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1407 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1408 | spin_unlock(&cache->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1409 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1410 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1411 | cache->space_info->bytes_used -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1412 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1413 | spin_unlock(&cache->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1414 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1415 | int ret; |
| 1416 | ret = btrfs_add_free_space(cache, bytenr, |
| 1417 | num_bytes); |
| 1418 | if (ret) |
| 1419 | return -1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1420 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1421 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1422 | total -= num_bytes; |
| 1423 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1424 | } |
| 1425 | return 0; |
| 1426 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1427 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1428 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 1429 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1430 | struct btrfs_block_group_cache *cache; |
| 1431 | |
| 1432 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 1433 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1434 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1435 | |
| 1436 | return cache->key.objectid; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1440 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1441 | u64 bytenr, u64 num, int pin) |
| 1442 | { |
| 1443 | u64 len; |
| 1444 | struct btrfs_block_group_cache *cache; |
| 1445 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1446 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1447 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1448 | if (pin) { |
| 1449 | set_extent_dirty(&fs_info->pinned_extents, |
| 1450 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1451 | } else { |
| 1452 | clear_extent_dirty(&fs_info->pinned_extents, |
| 1453 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1454 | } |
| 1455 | while (num > 0) { |
| 1456 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1457 | if (!cache) { |
| 1458 | u64 first = first_logical_byte(root, bytenr); |
| 1459 | WARN_ON(first < bytenr); |
| 1460 | len = min(first - bytenr, num); |
| 1461 | } else { |
| 1462 | len = min(num, cache->key.offset - |
| 1463 | (bytenr - cache->key.objectid)); |
| 1464 | } |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1465 | if (pin) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1466 | if (cache) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1467 | spin_lock(&cache->lock); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1468 | cache->pinned += len; |
| 1469 | cache->space_info->bytes_pinned += len; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1470 | spin_unlock(&cache->lock); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1471 | } |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1472 | fs_info->total_pinned += len; |
| 1473 | } else { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1474 | if (cache) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1475 | spin_lock(&cache->lock); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1476 | cache->pinned -= len; |
| 1477 | cache->space_info->bytes_pinned -= len; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1478 | spin_unlock(&cache->lock); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1479 | } |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1480 | fs_info->total_pinned -= len; |
| 1481 | } |
| 1482 | bytenr += len; |
| 1483 | num -= len; |
| 1484 | } |
| 1485 | return 0; |
| 1486 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1487 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1488 | 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] | 1489 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1490 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1491 | u64 start; |
| 1492 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1493 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1494 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1495 | |
| 1496 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1497 | ret = find_first_extent_bit(pinned_extents, last, |
| 1498 | &start, &end, EXTENT_DIRTY); |
| 1499 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1500 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1501 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 1502 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1503 | } |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 1508 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1509 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1510 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1511 | u64 start; |
| 1512 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1513 | int ret; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1514 | struct btrfs_block_group_cache *cache; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1515 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1516 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1517 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1518 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 1519 | EXTENT_DIRTY); |
| 1520 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1521 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1522 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1523 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1524 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 1525 | if (cache->cached) |
| 1526 | btrfs_add_free_space(cache, start, end - start + 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1527 | if (need_resched()) { |
| 1528 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 1529 | cond_resched(); |
| 1530 | mutex_lock(&root->fs_info->alloc_mutex); |
| 1531 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1532 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1533 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1534 | return 0; |
| 1535 | } |
| 1536 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1537 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
| 1538 | struct btrfs_root *extent_root) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1539 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1540 | u64 start; |
| 1541 | u64 end; |
| 1542 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1543 | struct extent_buffer *eb; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1544 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1545 | struct btrfs_key ins; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1546 | struct btrfs_disk_key first; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1547 | struct btrfs_extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1548 | int ret; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1549 | int level; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1550 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1551 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1552 | WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1553 | btrfs_set_stack_extent_refs(&extent_item, 1); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1554 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1555 | path = btrfs_alloc_path(); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1556 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1557 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1558 | ret = find_first_extent_bit(&info->extent_ins, 0, &start, |
| 1559 | &end, EXTENT_LOCKED); |
| 1560 | if (ret) |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1561 | break; |
| 1562 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1563 | ins.objectid = start; |
| 1564 | ins.offset = end + 1 - start; |
| 1565 | err = btrfs_insert_item(trans, extent_root, &ins, |
| 1566 | &extent_item, sizeof(extent_item)); |
| 1567 | clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, |
| 1568 | GFP_NOFS); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1569 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1570 | eb = btrfs_find_create_tree_block(extent_root, ins.objectid, |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1571 | ins.offset); |
| 1572 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1573 | if (!btrfs_buffer_uptodate(eb, trans->transid)) |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1574 | btrfs_read_buffer(eb, trans->transid); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1575 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1576 | btrfs_tree_lock(eb); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1577 | level = btrfs_header_level(eb); |
| 1578 | if (level == 0) { |
| 1579 | btrfs_item_key(eb, &first, 0); |
| 1580 | } else { |
| 1581 | btrfs_node_key(eb, &first, 0); |
| 1582 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1583 | btrfs_tree_unlock(eb); |
| 1584 | free_extent_buffer(eb); |
| 1585 | /* |
| 1586 | * the first key is just a hint, so the race we've created |
| 1587 | * against reading it is fine |
| 1588 | */ |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1589 | err = btrfs_insert_extent_backref(trans, extent_root, path, |
| 1590 | start, extent_root->root_key.objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 1591 | 0, level, |
| 1592 | btrfs_disk_key_objectid(&first)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1593 | BUG_ON(err); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1594 | if (need_resched()) { |
| 1595 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 1596 | cond_resched(); |
| 1597 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
| 1598 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1599 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1600 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1601 | return 0; |
| 1602 | } |
| 1603 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1604 | static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1605 | int is_data, int pending) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1606 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1607 | int err = 0; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1608 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1609 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1610 | if (!pending) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1611 | struct extent_buffer *buf; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1612 | |
| 1613 | if (is_data) |
| 1614 | goto pinit; |
| 1615 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1616 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1617 | if (buf) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1618 | /* we can reuse a block if it hasn't been written |
| 1619 | * and it is from this transaction. We can't |
| 1620 | * reuse anything from the tree log root because |
| 1621 | * it has tiny sub-transactions. |
| 1622 | */ |
Yan | 974e35a | 2008-07-24 12:18:16 -0400 | [diff] [blame] | 1623 | if (btrfs_buffer_uptodate(buf, 0) && |
| 1624 | btrfs_try_tree_lock(buf)) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1625 | u64 transid = |
| 1626 | root->fs_info->running_transaction->transid; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1627 | u64 header_transid = |
| 1628 | btrfs_header_generation(buf); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1629 | if (btrfs_header_owner(buf) != |
| 1630 | BTRFS_TREE_LOG_OBJECTID && |
| 1631 | header_transid == transid && |
Chris Mason | 6bc3467 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 1632 | !btrfs_header_flag(buf, |
| 1633 | BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 1634 | clean_tree_block(NULL, root, buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1635 | btrfs_tree_unlock(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1636 | free_extent_buffer(buf); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1637 | return 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1638 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1639 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1640 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1641 | free_extent_buffer(buf); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1642 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1643 | pinit: |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1644 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1645 | } else { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1646 | set_extent_bits(&root->fs_info->pending_del, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1647 | bytenr, bytenr + num_bytes - 1, |
| 1648 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1649 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1650 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1651 | return 0; |
| 1652 | } |
| 1653 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1654 | /* |
| 1655 | * remove an extent from the root, returns 0 on success |
| 1656 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1657 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1658 | *root, u64 bytenr, u64 num_bytes, |
| 1659 | u64 root_objectid, u64 ref_generation, |
| 1660 | u64 owner_objectid, u64 owner_offset, int pin, |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1661 | int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1662 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1663 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1664 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1665 | struct btrfs_fs_info *info = root->fs_info; |
| 1666 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1667 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1668 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1669 | int extent_slot = 0; |
| 1670 | int found_extent = 0; |
| 1671 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1672 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1673 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1674 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1675 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1676 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1677 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1678 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1679 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1680 | if (!path) |
| 1681 | return -ENOMEM; |
| 1682 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1683 | path->reada = 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1684 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1685 | bytenr, root_objectid, |
| 1686 | ref_generation, |
| 1687 | owner_objectid, owner_offset, 1); |
| 1688 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1689 | struct btrfs_key found_key; |
| 1690 | extent_slot = path->slots[0]; |
| 1691 | while(extent_slot > 0) { |
| 1692 | extent_slot--; |
| 1693 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1694 | extent_slot); |
| 1695 | if (found_key.objectid != bytenr) |
| 1696 | break; |
| 1697 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1698 | found_key.offset == num_bytes) { |
| 1699 | found_extent = 1; |
| 1700 | break; |
| 1701 | } |
| 1702 | if (path->slots[0] - extent_slot > 5) |
| 1703 | break; |
| 1704 | } |
| 1705 | if (!found_extent) |
| 1706 | ret = btrfs_del_item(trans, extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1707 | } else { |
| 1708 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 1709 | WARN_ON(1); |
| 1710 | printk("Unable to find ref byte nr %Lu root %Lu " |
| 1711 | " gen %Lu owner %Lu offset %Lu\n", bytenr, |
| 1712 | root_objectid, ref_generation, owner_objectid, |
| 1713 | owner_offset); |
| 1714 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1715 | if (!found_extent) { |
| 1716 | btrfs_release_path(extent_root, path); |
| 1717 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
| 1718 | if (ret < 0) |
| 1719 | return ret; |
| 1720 | BUG_ON(ret); |
| 1721 | extent_slot = path->slots[0]; |
| 1722 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1723 | |
| 1724 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1725 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1726 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1727 | refs = btrfs_extent_refs(leaf, ei); |
| 1728 | BUG_ON(refs == 0); |
| 1729 | refs -= 1; |
| 1730 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1731 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1732 | btrfs_mark_buffer_dirty(leaf); |
| 1733 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1734 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
| 1735 | /* if the back ref and the extent are next to each other |
| 1736 | * they get deleted below in one shot |
| 1737 | */ |
| 1738 | path->slots[0] = extent_slot; |
| 1739 | num_to_del = 2; |
| 1740 | } else if (found_extent) { |
| 1741 | /* otherwise delete the extent back ref */ |
| 1742 | ret = btrfs_del_item(trans, extent_root, path); |
| 1743 | BUG_ON(ret); |
| 1744 | /* if refs are 0, we need to setup the path for deletion */ |
| 1745 | if (refs == 0) { |
| 1746 | btrfs_release_path(extent_root, path); |
| 1747 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 1748 | -1, 1); |
| 1749 | if (ret < 0) |
| 1750 | return ret; |
| 1751 | BUG_ON(ret); |
| 1752 | } |
| 1753 | } |
| 1754 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1755 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1756 | u64 super_used; |
| 1757 | u64 root_used; |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 1758 | #ifdef BIO_RW_DISCARD |
| 1759 | u64 map_length = num_bytes; |
| 1760 | struct btrfs_multi_bio *multi = NULL; |
| 1761 | #endif |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1762 | |
| 1763 | if (pin) { |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1764 | ret = pin_down_bytes(root, bytenr, num_bytes, |
| 1765 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID, 0); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1766 | if (ret > 0) |
| 1767 | mark_free = 1; |
| 1768 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1769 | } |
| 1770 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1771 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1772 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1773 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1774 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1775 | super_used - num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1776 | spin_unlock_irq(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1777 | |
| 1778 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1779 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1780 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1781 | root_used - num_bytes); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1782 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 1783 | num_to_del); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1784 | if (ret) { |
| 1785 | return ret; |
| 1786 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1787 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1788 | mark_free); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1789 | BUG_ON(ret); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 1790 | |
| 1791 | #ifdef BIO_RW_DISCARD |
| 1792 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1793 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1794 | bytenr, &map_length, &multi, 0); |
| 1795 | if (!ret) { |
| 1796 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1797 | int i; |
| 1798 | |
| 1799 | if (map_length > num_bytes) |
| 1800 | map_length = num_bytes; |
| 1801 | |
| 1802 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1803 | blkdev_issue_discard(stripe->dev->bdev, |
| 1804 | stripe->physical >> 9, |
| 1805 | map_length >> 9); |
| 1806 | } |
| 1807 | kfree(multi); |
| 1808 | } |
| 1809 | #endif |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1810 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1811 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1812 | finish_current_insert(trans, extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1813 | return ret; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1817 | * find all the blocks marked as pending in the radix tree and remove |
| 1818 | * them from the extent map |
| 1819 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1820 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 1821 | btrfs_root *extent_root) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1822 | { |
| 1823 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1824 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1825 | u64 start; |
| 1826 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1827 | struct extent_io_tree *pending_del; |
| 1828 | struct extent_io_tree *pinned_extents; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1829 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1830 | WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex)); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1831 | pending_del = &extent_root->fs_info->pending_del; |
| 1832 | pinned_extents = &extent_root->fs_info->pinned_extents; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1833 | |
| 1834 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1835 | ret = find_first_extent_bit(pending_del, 0, &start, &end, |
| 1836 | EXTENT_LOCKED); |
| 1837 | if (ret) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1838 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1839 | clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, |
| 1840 | GFP_NOFS); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1841 | if (!test_range_bit(&extent_root->fs_info->extent_ins, |
| 1842 | start, end, EXTENT_LOCKED, 0)) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1843 | btrfs_update_pinned_extents(extent_root, start, |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1844 | end + 1 - start, 1); |
| 1845 | ret = __free_extent(trans, extent_root, |
| 1846 | start, end + 1 - start, |
| 1847 | extent_root->root_key.objectid, |
| 1848 | 0, 0, 0, 0, 0); |
| 1849 | } else { |
| 1850 | clear_extent_bits(&extent_root->fs_info->extent_ins, |
| 1851 | start, end, EXTENT_LOCKED, GFP_NOFS); |
| 1852 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1853 | if (ret) |
| 1854 | err = ret; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1855 | |
| 1856 | if (need_resched()) { |
| 1857 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 1858 | cond_resched(); |
| 1859 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
| 1860 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1861 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1862 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * remove an extent from the root, returns 0 on success |
| 1867 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1868 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 1869 | struct btrfs_root *root, u64 bytenr, |
| 1870 | u64 num_bytes, u64 root_objectid, |
| 1871 | u64 ref_generation, u64 owner_objectid, |
| 1872 | u64 owner_offset, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1873 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1874 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1875 | int pending_ret; |
| 1876 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1877 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1878 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1879 | if (!root->ref_cows) |
| 1880 | ref_generation = 0; |
| 1881 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1882 | if (root == extent_root) { |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1883 | pin_down_bytes(root, bytenr, num_bytes, 0, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1884 | return 0; |
| 1885 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1886 | /* if metadata always pin */ |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1887 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 1888 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1889 | struct btrfs_block_group_cache *cache; |
| 1890 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1891 | /* btrfs_free_reserved_extent */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1892 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 1893 | BUG_ON(!cache); |
| 1894 | btrfs_add_free_space(cache, bytenr, num_bytes); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1895 | return 0; |
| 1896 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1897 | pin = 1; |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 1898 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1899 | |
| 1900 | /* if data pin when any transaction has committed this */ |
| 1901 | if (ref_generation != trans->transid) |
| 1902 | pin = 1; |
| 1903 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1904 | ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid, |
| 1905 | ref_generation, owner_objectid, owner_offset, |
| 1906 | pin, pin == 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 1907 | |
| 1908 | finish_current_insert(trans, root->fs_info->extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1909 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1910 | return ret ? ret : pending_ret; |
| 1911 | } |
| 1912 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1913 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 1914 | struct btrfs_root *root, u64 bytenr, |
| 1915 | u64 num_bytes, u64 root_objectid, |
| 1916 | u64 ref_generation, u64 owner_objectid, |
| 1917 | u64 owner_offset, int pin) |
| 1918 | { |
| 1919 | int ret; |
| 1920 | |
| 1921 | maybe_lock_mutex(root); |
| 1922 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, |
| 1923 | root_objectid, ref_generation, |
| 1924 | owner_objectid, owner_offset, pin); |
| 1925 | maybe_unlock_mutex(root); |
| 1926 | return ret; |
| 1927 | } |
| 1928 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1929 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 1930 | { |
| 1931 | u64 mask = ((u64)root->stripesize - 1); |
| 1932 | u64 ret = (val + mask) & ~mask; |
| 1933 | return ret; |
| 1934 | } |
| 1935 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1936 | /* |
| 1937 | * walks the btree of allocated extents and find a hole of a given size. |
| 1938 | * The key ins is changed to record the hole: |
| 1939 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1940 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1941 | * ins->offset == number of blocks |
| 1942 | * Any available blocks before search_start are skipped. |
| 1943 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1944 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
| 1945 | struct btrfs_root *orig_root, |
| 1946 | u64 num_bytes, u64 empty_size, |
| 1947 | u64 search_start, u64 search_end, |
| 1948 | u64 hint_byte, struct btrfs_key *ins, |
| 1949 | u64 exclude_start, u64 exclude_nr, |
| 1950 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1951 | { |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1952 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1953 | u64 orig_search_start; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1954 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1955 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1956 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1957 | u64 *last_ptr = NULL; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 1958 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1959 | int chunk_alloc_done = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1960 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1961 | int allowed_chunk_alloc = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1962 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1963 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 1964 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
| 1965 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1966 | if (orig_root->ref_cows || empty_size) |
| 1967 | allowed_chunk_alloc = 1; |
| 1968 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1969 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 1970 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1971 | empty_cluster = 256 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1972 | } |
| 1973 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1974 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1975 | last_ptr = &root->fs_info->last_data_alloc; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1976 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1977 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 1978 | last_ptr = &root->fs_info->last_log_alloc; |
| 1979 | if (!last_ptr == 0 && root->fs_info->last_alloc) { |
| 1980 | *last_ptr = root->fs_info->last_alloc + empty_cluster; |
| 1981 | } |
| 1982 | } |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1983 | |
| 1984 | if (last_ptr) { |
| 1985 | if (*last_ptr) |
| 1986 | hint_byte = *last_ptr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1987 | else |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1988 | empty_size += empty_cluster; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1989 | } |
| 1990 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1991 | search_start = max(search_start, first_logical_byte(root, 0)); |
| 1992 | orig_search_start = search_start; |
| 1993 | |
Chris Mason | 7f93bf8 | 2008-03-24 15:01:28 -0400 | [diff] [blame] | 1994 | if (search_end == (u64)-1) |
| 1995 | search_end = btrfs_super_total_bytes(&info->super_copy); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1996 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1997 | search_start = max(search_start, hint_byte); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1998 | total_needed += empty_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1999 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2000 | new_group: |
| 2001 | block_group = btrfs_lookup_block_group(info, search_start); |
| 2002 | |
| 2003 | /* |
| 2004 | * Ok this looks a little tricky, buts its really simple. First if we |
| 2005 | * didn't find a block group obviously we want to start over. |
| 2006 | * Secondly, if the block group we found does not match the type we |
| 2007 | * need, and we have a last_ptr and its not 0, chances are the last |
| 2008 | * allocation we made was at the end of the block group, so lets go |
| 2009 | * ahead and skip the looking through the rest of the block groups and |
| 2010 | * start at the beginning. This helps with metadata allocations, |
| 2011 | * since you are likely to have a bunch of data block groups to search |
| 2012 | * through first before you realize that you need to start over, so go |
| 2013 | * ahead and start over and save the time. |
| 2014 | */ |
| 2015 | if (!block_group || (!block_group_bits(block_group, data) && |
| 2016 | last_ptr && *last_ptr)) { |
| 2017 | if (search_start != orig_search_start) { |
| 2018 | if (last_ptr && *last_ptr) |
| 2019 | *last_ptr = 0; |
| 2020 | search_start = orig_search_start; |
| 2021 | goto new_group; |
| 2022 | } else if (!chunk_alloc_done && allowed_chunk_alloc) { |
| 2023 | ret = do_chunk_alloc(trans, root, |
| 2024 | num_bytes + 2 * 1024 * 1024, |
| 2025 | data, 1); |
| 2026 | if (ret < 0) { |
| 2027 | struct btrfs_space_info *info; |
| 2028 | |
| 2029 | info = __find_space_info(root->fs_info, data); |
| 2030 | goto error; |
| 2031 | } |
| 2032 | BUG_ON(ret); |
| 2033 | chunk_alloc_done = 1; |
| 2034 | search_start = orig_search_start; |
| 2035 | goto new_group; |
| 2036 | } else { |
| 2037 | ret = -ENOSPC; |
| 2038 | goto error; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2039 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | /* |
| 2043 | * this is going to seach through all of the existing block groups it |
| 2044 | * can find, so if we don't find something we need to see if we can |
| 2045 | * allocate what we need. |
| 2046 | */ |
| 2047 | ret = find_free_space(root, &block_group, &search_start, |
| 2048 | total_needed, data); |
| 2049 | if (ret == -ENOSPC) { |
| 2050 | /* |
| 2051 | * instead of allocating, start at the original search start |
| 2052 | * and see if there is something to be found, if not then we |
| 2053 | * allocate |
| 2054 | */ |
| 2055 | if (search_start != orig_search_start) { |
| 2056 | if (last_ptr && *last_ptr) { |
| 2057 | *last_ptr = 0; |
| 2058 | total_needed += empty_cluster; |
| 2059 | } |
| 2060 | search_start = orig_search_start; |
| 2061 | goto new_group; |
| 2062 | } |
| 2063 | |
| 2064 | /* |
| 2065 | * we've already allocated, we're pretty screwed |
| 2066 | */ |
| 2067 | if (chunk_alloc_done) { |
| 2068 | goto error; |
| 2069 | } else if (!allowed_chunk_alloc && block_group && |
| 2070 | block_group_bits(block_group, data)) { |
| 2071 | block_group->space_info->force_alloc = 1; |
| 2072 | goto error; |
| 2073 | } else if (!allowed_chunk_alloc) { |
| 2074 | goto error; |
| 2075 | } |
| 2076 | |
| 2077 | ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024, |
| 2078 | data, 1); |
| 2079 | if (ret < 0) |
| 2080 | goto error; |
| 2081 | |
| 2082 | BUG_ON(ret); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2083 | chunk_alloc_done = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2084 | if (block_group) |
| 2085 | search_start = block_group->key.objectid + |
| 2086 | block_group->key.offset; |
| 2087 | else |
| 2088 | search_start = orig_search_start; |
| 2089 | goto new_group; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2090 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2091 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2092 | if (ret) |
| 2093 | goto error; |
| 2094 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2095 | search_start = stripe_align(root, search_start); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2096 | ins->objectid = search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2097 | ins->offset = num_bytes; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2098 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2099 | if (ins->objectid + num_bytes >= search_end) { |
| 2100 | search_start = orig_search_start; |
| 2101 | if (chunk_alloc_done) { |
| 2102 | ret = -ENOSPC; |
| 2103 | goto error; |
| 2104 | } |
| 2105 | goto new_group; |
| 2106 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2107 | |
| 2108 | if (ins->objectid + num_bytes > |
| 2109 | block_group->key.objectid + block_group->key.offset) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2110 | if (search_start == orig_search_start && chunk_alloc_done) { |
| 2111 | ret = -ENOSPC; |
| 2112 | goto error; |
| 2113 | } |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 2114 | search_start = block_group->key.objectid + |
| 2115 | block_group->key.offset; |
| 2116 | goto new_group; |
| 2117 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2118 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2119 | if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2120 | ins->objectid < exclude_start + exclude_nr)) { |
| 2121 | search_start = exclude_start + exclude_nr; |
| 2122 | goto new_group; |
| 2123 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2124 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2125 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
| 2126 | trans->block_group = block_group; |
| 2127 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2128 | ins->offset = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2129 | if (last_ptr) { |
| 2130 | *last_ptr = ins->objectid + ins->offset; |
| 2131 | if (*last_ptr == |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2132 | btrfs_super_total_bytes(&root->fs_info->super_copy)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2133 | *last_ptr = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2134 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2135 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2136 | ret = 0; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2137 | error: |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2138 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2139 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2140 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2141 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 2142 | { |
| 2143 | struct btrfs_block_group_cache *cache; |
| 2144 | struct list_head *l; |
| 2145 | |
| 2146 | printk(KERN_INFO "space_info has %Lu free, is %sfull\n", |
| 2147 | info->total_bytes - info->bytes_used - info->bytes_pinned, |
| 2148 | (info->full) ? "" : "not "); |
| 2149 | |
| 2150 | spin_lock(&info->lock); |
| 2151 | list_for_each(l, &info->block_groups) { |
| 2152 | cache = list_entry(l, struct btrfs_block_group_cache, list); |
| 2153 | spin_lock(&cache->lock); |
| 2154 | printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used " |
| 2155 | "%Lu pinned\n", |
| 2156 | cache->key.objectid, cache->key.offset, |
| 2157 | btrfs_block_group_used(&cache->item), cache->pinned); |
| 2158 | btrfs_dump_free_space(cache, bytes); |
| 2159 | spin_unlock(&cache->lock); |
| 2160 | } |
| 2161 | spin_unlock(&info->lock); |
| 2162 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2163 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2164 | struct btrfs_root *root, |
| 2165 | u64 num_bytes, u64 min_alloc_size, |
| 2166 | u64 empty_size, u64 hint_byte, |
| 2167 | u64 search_end, struct btrfs_key *ins, |
| 2168 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2169 | { |
| 2170 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 2171 | u64 search_start = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2172 | u64 alloc_profile; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2173 | struct btrfs_fs_info *info = root->fs_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2174 | struct btrfs_block_group_cache *cache; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2175 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2176 | if (data) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2177 | alloc_profile = info->avail_data_alloc_bits & |
| 2178 | info->data_alloc_profile; |
| 2179 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2180 | } else if (root == root->fs_info->chunk_root) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2181 | alloc_profile = info->avail_system_alloc_bits & |
| 2182 | info->system_alloc_profile; |
| 2183 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2184 | } else { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2185 | alloc_profile = info->avail_metadata_alloc_bits & |
| 2186 | info->metadata_alloc_profile; |
| 2187 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2188 | } |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2189 | again: |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2190 | data = reduce_alloc_profile(root, data); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2191 | /* |
| 2192 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 2193 | * is not called recursively on allocations |
| 2194 | */ |
| 2195 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2196 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2197 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2198 | 2 * 1024 * 1024, |
| 2199 | BTRFS_BLOCK_GROUP_METADATA | |
| 2200 | (info->metadata_alloc_profile & |
| 2201 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2202 | } |
| 2203 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2204 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2205 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2206 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2207 | WARN_ON(num_bytes < root->sectorsize); |
| 2208 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 2209 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2210 | trans->alloc_exclude_start, |
| 2211 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2212 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2213 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 2214 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2215 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2216 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2217 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2218 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2219 | goto again; |
| 2220 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2221 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2222 | struct btrfs_space_info *sinfo; |
| 2223 | |
| 2224 | sinfo = __find_space_info(root->fs_info, data); |
| 2225 | printk("allocation failed flags %Lu, wanted %Lu\n", |
| 2226 | data, num_bytes); |
| 2227 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2228 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2229 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2230 | cache = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
| 2231 | if (!cache) { |
| 2232 | printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid); |
| 2233 | return -ENOSPC; |
| 2234 | } |
| 2235 | |
| 2236 | ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset); |
| 2237 | |
| 2238 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2239 | } |
| 2240 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2241 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 2242 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2243 | struct btrfs_block_group_cache *cache; |
| 2244 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2245 | maybe_lock_mutex(root); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2246 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2247 | if (!cache) { |
| 2248 | printk(KERN_ERR "Unable to find block group for %Lu\n", start); |
| 2249 | maybe_unlock_mutex(root); |
| 2250 | return -ENOSPC; |
| 2251 | } |
| 2252 | btrfs_add_free_space(cache, start, len); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2253 | maybe_unlock_mutex(root); |
| 2254 | return 0; |
| 2255 | } |
| 2256 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2257 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2258 | struct btrfs_root *root, |
| 2259 | u64 num_bytes, u64 min_alloc_size, |
| 2260 | u64 empty_size, u64 hint_byte, |
| 2261 | u64 search_end, struct btrfs_key *ins, |
| 2262 | u64 data) |
| 2263 | { |
| 2264 | int ret; |
| 2265 | maybe_lock_mutex(root); |
| 2266 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 2267 | empty_size, hint_byte, search_end, ins, |
| 2268 | data); |
| 2269 | maybe_unlock_mutex(root); |
| 2270 | return ret; |
| 2271 | } |
| 2272 | |
| 2273 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
| 2274 | struct btrfs_root *root, |
| 2275 | u64 root_objectid, u64 ref_generation, |
| 2276 | u64 owner, u64 owner_offset, |
| 2277 | struct btrfs_key *ins) |
| 2278 | { |
| 2279 | int ret; |
| 2280 | int pending_ret; |
| 2281 | u64 super_used; |
| 2282 | u64 root_used; |
| 2283 | u64 num_bytes = ins->offset; |
| 2284 | u32 sizes[2]; |
| 2285 | struct btrfs_fs_info *info = root->fs_info; |
| 2286 | struct btrfs_root *extent_root = info->extent_root; |
| 2287 | struct btrfs_extent_item *extent_item; |
| 2288 | struct btrfs_extent_ref *ref; |
| 2289 | struct btrfs_path *path; |
| 2290 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2291 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2292 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2293 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2294 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2295 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2296 | spin_unlock_irq(&info->delalloc_lock); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2297 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2298 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2299 | root_used = btrfs_root_used(&root->root_item); |
| 2300 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2301 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2302 | if (root == extent_root) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2303 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 2304 | ins->objectid + ins->offset - 1, |
| 2305 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2306 | goto update_block; |
| 2307 | } |
| 2308 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2309 | memcpy(&keys[0], ins, sizeof(*ins)); |
| 2310 | keys[1].offset = hash_extent_ref(root_objectid, ref_generation, |
| 2311 | owner, owner_offset); |
| 2312 | keys[1].objectid = ins->objectid; |
| 2313 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
| 2314 | sizes[0] = sizeof(*extent_item); |
| 2315 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2316 | |
| 2317 | path = btrfs_alloc_path(); |
| 2318 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2319 | |
| 2320 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 2321 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2322 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2323 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2324 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2325 | struct btrfs_extent_item); |
| 2326 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); |
| 2327 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 2328 | struct btrfs_extent_ref); |
| 2329 | |
| 2330 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 2331 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 2332 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
| 2333 | btrfs_set_ref_offset(path->nodes[0], ref, owner_offset); |
| 2334 | |
| 2335 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2336 | |
| 2337 | trans->alloc_exclude_start = 0; |
| 2338 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2339 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2340 | finish_current_insert(trans, extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2341 | pending_ret = del_pending_extents(trans, extent_root); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2342 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2343 | if (ret) |
| 2344 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2345 | if (pending_ret) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2346 | ret = pending_ret; |
| 2347 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2348 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2349 | |
| 2350 | update_block: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2351 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 2352 | if (ret) { |
| 2353 | printk("update block group failed for %Lu %Lu\n", |
| 2354 | ins->objectid, ins->offset); |
| 2355 | BUG(); |
| 2356 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2357 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2358 | return ret; |
| 2359 | } |
| 2360 | |
| 2361 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
| 2362 | struct btrfs_root *root, |
| 2363 | u64 root_objectid, u64 ref_generation, |
| 2364 | u64 owner, u64 owner_offset, |
| 2365 | struct btrfs_key *ins) |
| 2366 | { |
| 2367 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame^] | 2368 | |
| 2369 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 2370 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2371 | maybe_lock_mutex(root); |
| 2372 | ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid, |
| 2373 | ref_generation, owner, |
| 2374 | owner_offset, ins); |
| 2375 | maybe_unlock_mutex(root); |
| 2376 | return ret; |
| 2377 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2378 | |
| 2379 | /* |
| 2380 | * this is used by the tree logging recovery code. It records that |
| 2381 | * an extent has been allocated and makes sure to clear the free |
| 2382 | * space cache bits as well |
| 2383 | */ |
| 2384 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
| 2385 | struct btrfs_root *root, |
| 2386 | u64 root_objectid, u64 ref_generation, |
| 2387 | u64 owner, u64 owner_offset, |
| 2388 | struct btrfs_key *ins) |
| 2389 | { |
| 2390 | int ret; |
| 2391 | struct btrfs_block_group_cache *block_group; |
| 2392 | |
| 2393 | maybe_lock_mutex(root); |
| 2394 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
| 2395 | cache_block_group(root, block_group); |
| 2396 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2397 | ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset); |
| 2398 | BUG_ON(ret); |
| 2399 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2400 | ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid, |
| 2401 | ref_generation, owner, |
| 2402 | owner_offset, ins); |
| 2403 | maybe_unlock_mutex(root); |
| 2404 | return ret; |
| 2405 | } |
| 2406 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2407 | /* |
| 2408 | * finds a free extent and does all the dirty work required for allocation |
| 2409 | * returns the key for the extent through ins, and a tree buffer for |
| 2410 | * the first block of the extent through buf. |
| 2411 | * |
| 2412 | * returns 0 if everything worked, non-zero otherwise. |
| 2413 | */ |
| 2414 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 2415 | struct btrfs_root *root, |
| 2416 | u64 num_bytes, u64 min_alloc_size, |
| 2417 | u64 root_objectid, u64 ref_generation, |
| 2418 | u64 owner, u64 owner_offset, |
| 2419 | u64 empty_size, u64 hint_byte, |
| 2420 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 2421 | { |
| 2422 | int ret; |
| 2423 | |
| 2424 | maybe_lock_mutex(root); |
| 2425 | |
| 2426 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 2427 | min_alloc_size, empty_size, hint_byte, |
| 2428 | search_end, ins, data); |
| 2429 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2430 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 2431 | ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid, |
| 2432 | ref_generation, owner, |
| 2433 | owner_offset, ins); |
| 2434 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2435 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2436 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2437 | maybe_unlock_mutex(root); |
| 2438 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2439 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2440 | |
| 2441 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 2442 | struct btrfs_root *root, |
| 2443 | u64 bytenr, u32 blocksize) |
| 2444 | { |
| 2445 | struct extent_buffer *buf; |
| 2446 | |
| 2447 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 2448 | if (!buf) |
| 2449 | return ERR_PTR(-ENOMEM); |
| 2450 | btrfs_set_header_generation(buf, trans->transid); |
| 2451 | btrfs_tree_lock(buf); |
| 2452 | clean_tree_block(trans, root, buf); |
| 2453 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2454 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 2455 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2456 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2457 | } else { |
| 2458 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 2459 | buf->start + buf->len - 1, GFP_NOFS); |
| 2460 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2461 | trans->blocks_used++; |
| 2462 | return buf; |
| 2463 | } |
| 2464 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2465 | /* |
| 2466 | * helper function to allocate a block for a given tree |
| 2467 | * returns the tree buffer or NULL. |
| 2468 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2469 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2470 | struct btrfs_root *root, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2471 | u32 blocksize, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2472 | u64 root_objectid, |
| 2473 | u64 ref_generation, |
| 2474 | u64 first_objectid, |
| 2475 | int level, |
| 2476 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2477 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2478 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2479 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2480 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2481 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2482 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2483 | ret = btrfs_alloc_extent(trans, root, blocksize, blocksize, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2484 | root_objectid, ref_generation, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 2485 | level, first_objectid, empty_size, hint, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2486 | (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2487 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2488 | BUG_ON(ret > 0); |
| 2489 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2490 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 2491 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2492 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2493 | return buf; |
| 2494 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2495 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2496 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 2497 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2498 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2499 | u64 leaf_owner; |
| 2500 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2501 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2502 | struct btrfs_file_extent_item *fi; |
| 2503 | int i; |
| 2504 | int nritems; |
| 2505 | int ret; |
| 2506 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2507 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 2508 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2509 | leaf_owner = btrfs_header_owner(leaf); |
| 2510 | leaf_generation = btrfs_header_generation(leaf); |
| 2511 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2512 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2513 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 2514 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2515 | |
| 2516 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 2517 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2518 | continue; |
| 2519 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2520 | if (btrfs_file_extent_type(leaf, fi) == |
| 2521 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 2522 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2523 | /* |
| 2524 | * FIXME make sure to insert a trans record that |
| 2525 | * repeats the snapshot del on crash |
| 2526 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2527 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 2528 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 2529 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2530 | |
| 2531 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2532 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2533 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
| 2534 | leaf_owner, leaf_generation, |
| 2535 | key.objectid, key.offset, 0); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2536 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2537 | |
| 2538 | atomic_inc(&root->fs_info->throttle_gen); |
| 2539 | wake_up(&root->fs_info->transaction_throttle); |
| 2540 | cond_resched(); |
| 2541 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2542 | BUG_ON(ret); |
| 2543 | } |
| 2544 | return 0; |
| 2545 | } |
| 2546 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2547 | static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 2548 | struct btrfs_root *root, |
| 2549 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2550 | { |
| 2551 | int i; |
| 2552 | int ret; |
| 2553 | struct btrfs_extent_info *info = ref->extents; |
| 2554 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2555 | for (i = 0; i < ref->nritems; i++) { |
| 2556 | mutex_lock(&root->fs_info->alloc_mutex); |
| 2557 | ret = __btrfs_free_extent(trans, root, |
| 2558 | info->bytenr, info->num_bytes, |
| 2559 | ref->owner, ref->generation, |
| 2560 | info->objectid, info->offset, 0); |
| 2561 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2562 | |
| 2563 | atomic_inc(&root->fs_info->throttle_gen); |
| 2564 | wake_up(&root->fs_info->transaction_throttle); |
| 2565 | cond_resched(); |
| 2566 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2567 | BUG_ON(ret); |
| 2568 | info++; |
| 2569 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2570 | |
| 2571 | return 0; |
| 2572 | } |
| 2573 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2574 | int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len, |
| 2575 | u32 *refs) |
| 2576 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2577 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2578 | |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2579 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2580 | BUG_ON(ret); |
| 2581 | |
| 2582 | #if 0 // some debugging code in case we see problems here |
| 2583 | /* if the refs count is one, it won't get increased again. But |
| 2584 | * if the ref count is > 1, someone may be decreasing it at |
| 2585 | * the same time we are. |
| 2586 | */ |
| 2587 | if (*refs != 1) { |
| 2588 | struct extent_buffer *eb = NULL; |
| 2589 | eb = btrfs_find_create_tree_block(root, start, len); |
| 2590 | if (eb) |
| 2591 | btrfs_tree_lock(eb); |
| 2592 | |
| 2593 | mutex_lock(&root->fs_info->alloc_mutex); |
| 2594 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 2595 | BUG_ON(ret); |
| 2596 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 2597 | |
| 2598 | if (eb) { |
| 2599 | btrfs_tree_unlock(eb); |
| 2600 | free_extent_buffer(eb); |
| 2601 | } |
| 2602 | if (*refs == 1) { |
| 2603 | printk("block %llu went down to one during drop_snap\n", |
| 2604 | (unsigned long long)start); |
| 2605 | } |
| 2606 | |
| 2607 | } |
| 2608 | #endif |
| 2609 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2610 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2611 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2615 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 2616 | * counts as it goes. |
| 2617 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2618 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
| 2619 | struct btrfs_root *root, |
| 2620 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2621 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2622 | u64 root_owner; |
| 2623 | u64 root_gen; |
| 2624 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2625 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2626 | struct extent_buffer *next; |
| 2627 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2628 | struct extent_buffer *parent; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2629 | struct btrfs_leaf_ref *ref; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2630 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2631 | int ret; |
| 2632 | u32 refs; |
| 2633 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2634 | WARN_ON(*level < 0); |
| 2635 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2636 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2637 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2638 | BUG_ON(ret); |
| 2639 | if (refs > 1) |
| 2640 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2641 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2642 | /* |
| 2643 | * walk down to the last node level and free all the leaves |
| 2644 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2645 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2646 | WARN_ON(*level < 0); |
| 2647 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2648 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2649 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2650 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2651 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2652 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2653 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2654 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2655 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2656 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2657 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2658 | BUG_ON(ret); |
| 2659 | break; |
| 2660 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2661 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2662 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2663 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2664 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2665 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2666 | BUG_ON(ret); |
| 2667 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2668 | parent = path->nodes[*level]; |
| 2669 | root_owner = btrfs_header_owner(parent); |
| 2670 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2671 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2672 | |
| 2673 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2674 | ret = __btrfs_free_extent(trans, root, bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2675 | blocksize, root_owner, |
| 2676 | root_gen, 0, 0, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2677 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2678 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 2679 | |
| 2680 | atomic_inc(&root->fs_info->throttle_gen); |
| 2681 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2682 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 2683 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2684 | continue; |
| 2685 | } |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2686 | /* |
| 2687 | * at this point, we have a single ref, and since the |
| 2688 | * only place referencing this extent is a dead root |
| 2689 | * the reference count should never go higher. |
| 2690 | * So, we don't need to check it again |
| 2691 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2692 | if (*level == 1) { |
| 2693 | struct btrfs_key key; |
| 2694 | btrfs_node_key_to_cpu(cur, &key, path->slots[*level]); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2695 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2696 | if (ref) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2697 | ret = cache_drop_leaf_ref(trans, root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2698 | BUG_ON(ret); |
| 2699 | btrfs_remove_leaf_ref(root, ref); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2700 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2701 | *level = 0; |
| 2702 | break; |
| 2703 | } |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 2704 | if (printk_ratelimit()) |
| 2705 | printk("leaf ref miss for bytenr %llu\n", |
| 2706 | (unsigned long long)bytenr); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2707 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2708 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2709 | if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2710 | free_extent_buffer(next); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2711 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2712 | next = read_tree_block(root, bytenr, blocksize, |
| 2713 | ptr_gen); |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2714 | cond_resched(); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2715 | #if 0 |
| 2716 | /* |
| 2717 | * this is a debugging check and can go away |
| 2718 | * the ref should never go all the way down to 1 |
| 2719 | * at this point |
| 2720 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2721 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, |
| 2722 | &refs); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2723 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2724 | WARN_ON(refs != 1); |
| 2725 | #endif |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2726 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2727 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2728 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2729 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2730 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2731 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2732 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2733 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2734 | } |
| 2735 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2736 | WARN_ON(*level < 0); |
| 2737 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2738 | |
| 2739 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2740 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2741 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2742 | } else { |
| 2743 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2744 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2745 | } |
| 2746 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2747 | blocksize = btrfs_level_size(root, *level); |
| 2748 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2749 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2750 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2751 | mutex_lock(&root->fs_info->alloc_mutex); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2752 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, |
| 2753 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2754 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2755 | path->nodes[*level] = NULL; |
| 2756 | *level += 1; |
| 2757 | BUG_ON(ret); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2758 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2759 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2760 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2761 | return 0; |
| 2762 | } |
| 2763 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2764 | /* |
| 2765 | * helper for dropping snapshots. This walks back up the tree in the path |
| 2766 | * to find the first node higher up where we haven't yet gone through |
| 2767 | * all the slots |
| 2768 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2769 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
| 2770 | struct btrfs_root *root, |
| 2771 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2772 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2773 | u64 root_owner; |
| 2774 | u64 root_gen; |
| 2775 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2776 | int i; |
| 2777 | int slot; |
| 2778 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2779 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2780 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2781 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2782 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 2783 | struct extent_buffer *node; |
| 2784 | struct btrfs_disk_key disk_key; |
| 2785 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2786 | path->slots[i]++; |
| 2787 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2788 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2789 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2790 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2791 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2792 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2793 | return 0; |
| 2794 | } else { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2795 | if (path->nodes[*level] == root->node) { |
| 2796 | root_owner = root->root_key.objectid; |
| 2797 | root_gen = |
| 2798 | btrfs_header_generation(path->nodes[*level]); |
| 2799 | } else { |
| 2800 | struct extent_buffer *node; |
| 2801 | node = path->nodes[*level + 1]; |
| 2802 | root_owner = btrfs_header_owner(node); |
| 2803 | root_gen = btrfs_header_generation(node); |
| 2804 | } |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2805 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2806 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2807 | path->nodes[*level]->len, |
| 2808 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2809 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2810 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2811 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2812 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2813 | } |
| 2814 | } |
| 2815 | return 1; |
| 2816 | } |
| 2817 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2818 | /* |
| 2819 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2820 | * the tree freeing any blocks that have a ref count of zero after being |
| 2821 | * decremented. |
| 2822 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2823 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2824 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2825 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2826 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2827 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2828 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2829 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2830 | int i; |
| 2831 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2832 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2833 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2834 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2835 | path = btrfs_alloc_path(); |
| 2836 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2837 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2838 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2839 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2840 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 2841 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2842 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2843 | path->slots[level] = 0; |
| 2844 | } else { |
| 2845 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2846 | struct btrfs_disk_key found_key; |
| 2847 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2848 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2849 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2850 | level = root_item->drop_level; |
| 2851 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2852 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2853 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2854 | ret = wret; |
| 2855 | goto out; |
| 2856 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2857 | node = path->nodes[level]; |
| 2858 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 2859 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 2860 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2861 | /* |
| 2862 | * unlock our path, this is safe because only this |
| 2863 | * function is allowed to delete this snapshot |
| 2864 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2865 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 2866 | if (path->nodes[i] && path->locks[i]) { |
| 2867 | path->locks[i] = 0; |
| 2868 | btrfs_tree_unlock(path->nodes[i]); |
| 2869 | } |
| 2870 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2871 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2872 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2873 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2874 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2875 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2876 | if (wret < 0) |
| 2877 | ret = wret; |
| 2878 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2879 | wret = walk_up_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2880 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2881 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2882 | if (wret < 0) |
| 2883 | ret = wret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2884 | if (trans->transaction->in_commit) { |
| 2885 | ret = -EAGAIN; |
| 2886 | break; |
| 2887 | } |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 2888 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2889 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2890 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2891 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2892 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2893 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 2894 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2895 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2896 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2897 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2898 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2899 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2900 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2901 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2902 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 2903 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2904 | struct btrfs_block_group_cache *block_group; |
| 2905 | struct rb_node *n; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2906 | |
| 2907 | mutex_lock(&info->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2908 | spin_lock(&info->block_group_cache_lock); |
| 2909 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 2910 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 2911 | cache_node); |
| 2912 | |
| 2913 | btrfs_remove_free_space_cache(block_group); |
| 2914 | rb_erase(&block_group->cache_node, |
| 2915 | &info->block_group_cache_tree); |
| 2916 | spin_lock(&block_group->space_info->lock); |
| 2917 | list_del(&block_group->list); |
| 2918 | spin_unlock(&block_group->space_info->lock); |
| 2919 | kfree(block_group); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2920 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2921 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2922 | mutex_unlock(&info->alloc_mutex); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2923 | return 0; |
| 2924 | } |
| 2925 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 2926 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 2927 | unsigned long nr) |
| 2928 | { |
| 2929 | return min(last, start + nr - 1); |
| 2930 | } |
| 2931 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2932 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
| 2933 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2934 | { |
| 2935 | u64 page_start; |
| 2936 | u64 page_end; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2937 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2938 | unsigned long i; |
| 2939 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2940 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2941 | struct file_ra_state *ra; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 2942 | unsigned long total_read = 0; |
| 2943 | unsigned long ra_pages; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 2944 | struct btrfs_ordered_extent *ordered; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2945 | struct btrfs_trans_handle *trans; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2946 | |
| 2947 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2948 | |
| 2949 | mutex_lock(&inode->i_mutex); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2950 | i = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2951 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 2952 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 2953 | ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages; |
| 2954 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2955 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2956 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2957 | for (; i <= last_index; i++) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 2958 | if (total_read % ra_pages == 0) { |
| 2959 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
| 2960 | calc_ra(i, last_index, ra_pages)); |
| 2961 | } |
| 2962 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 2963 | again: |
| 2964 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2965 | goto truncate_racing; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2966 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2967 | if (!page) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2968 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2969 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2970 | if (!PageUptodate(page)) { |
| 2971 | btrfs_readpage(NULL, page); |
| 2972 | lock_page(page); |
| 2973 | if (!PageUptodate(page)) { |
| 2974 | unlock_page(page); |
| 2975 | page_cache_release(page); |
| 2976 | goto out_unlock; |
| 2977 | } |
| 2978 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2979 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 2980 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2981 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2982 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2983 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2984 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 2985 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 2986 | if (ordered) { |
| 2987 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 2988 | unlock_page(page); |
| 2989 | page_cache_release(page); |
| 2990 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 2991 | btrfs_put_ordered_extent(ordered); |
| 2992 | goto again; |
| 2993 | } |
| 2994 | set_page_extent_mapped(page); |
| 2995 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2996 | /* |
| 2997 | * make sure page_mkwrite is called for this page if userland |
| 2998 | * wants to change it from mmap |
| 2999 | */ |
| 3000 | clear_page_dirty_for_io(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3001 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3002 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3003 | set_page_dirty(page); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3004 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3005 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3006 | unlock_page(page); |
| 3007 | page_cache_release(page); |
| 3008 | } |
| 3009 | |
| 3010 | out_unlock: |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3011 | /* we have to start the IO in order to get the ordered extents |
| 3012 | * instantiated. This allows the relocation to code to wait |
| 3013 | * for all the ordered extents to hit the disk. |
| 3014 | * |
| 3015 | * Otherwise, it would constantly loop over the same extents |
| 3016 | * because the old ones don't get deleted until the IO is |
| 3017 | * started |
| 3018 | */ |
| 3019 | btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1, |
| 3020 | WB_SYNC_NONE); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3021 | kfree(ra); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3022 | trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1); |
| 3023 | if (trans) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3024 | btrfs_end_transaction(trans, BTRFS_I(inode)->root); |
| 3025 | mark_inode_dirty(inode); |
| 3026 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3027 | mutex_unlock(&inode->i_mutex); |
| 3028 | return 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3029 | |
| 3030 | truncate_racing: |
| 3031 | vmtruncate(inode, inode->i_size); |
| 3032 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, |
| 3033 | total_read); |
| 3034 | goto out_unlock; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3035 | } |
| 3036 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3037 | /* |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3038 | * The back references tell us which tree holds a ref on a block, |
| 3039 | * but it is possible for the tree root field in the reference to |
| 3040 | * reflect the original root before a snapshot was made. In this |
| 3041 | * case we should search through all the children of a given root |
| 3042 | * to find potential holders of references on a block. |
| 3043 | * |
| 3044 | * Instead, we do something a little less fancy and just search |
| 3045 | * all the roots for a given key/block combination. |
| 3046 | */ |
| 3047 | static int find_root_for_ref(struct btrfs_root *root, |
| 3048 | struct btrfs_path *path, |
| 3049 | struct btrfs_key *key0, |
| 3050 | int level, |
| 3051 | int file_key, |
| 3052 | struct btrfs_root **found_root, |
| 3053 | u64 bytenr) |
| 3054 | { |
| 3055 | struct btrfs_key root_location; |
| 3056 | struct btrfs_root *cur_root = *found_root; |
| 3057 | struct btrfs_file_extent_item *file_extent; |
| 3058 | u64 root_search_start = BTRFS_FS_TREE_OBJECTID; |
| 3059 | u64 found_bytenr; |
| 3060 | int ret; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3061 | |
| 3062 | root_location.offset = (u64)-1; |
| 3063 | root_location.type = BTRFS_ROOT_ITEM_KEY; |
| 3064 | path->lowest_level = level; |
| 3065 | path->reada = 0; |
| 3066 | while(1) { |
| 3067 | ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0); |
| 3068 | found_bytenr = 0; |
| 3069 | if (ret == 0 && file_key) { |
| 3070 | struct extent_buffer *leaf = path->nodes[0]; |
| 3071 | file_extent = btrfs_item_ptr(leaf, path->slots[0], |
| 3072 | struct btrfs_file_extent_item); |
| 3073 | if (btrfs_file_extent_type(leaf, file_extent) == |
| 3074 | BTRFS_FILE_EXTENT_REG) { |
| 3075 | found_bytenr = |
| 3076 | btrfs_file_extent_disk_bytenr(leaf, |
| 3077 | file_extent); |
| 3078 | } |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 3079 | } else if (!file_key) { |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3080 | if (path->nodes[level]) |
| 3081 | found_bytenr = path->nodes[level]->start; |
| 3082 | } |
| 3083 | |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3084 | btrfs_release_path(cur_root, path); |
| 3085 | |
| 3086 | if (found_bytenr == bytenr) { |
| 3087 | *found_root = cur_root; |
| 3088 | ret = 0; |
| 3089 | goto out; |
| 3090 | } |
| 3091 | ret = btrfs_search_root(root->fs_info->tree_root, |
| 3092 | root_search_start, &root_search_start); |
| 3093 | if (ret) |
| 3094 | break; |
| 3095 | |
| 3096 | root_location.objectid = root_search_start; |
| 3097 | cur_root = btrfs_read_fs_root_no_name(root->fs_info, |
| 3098 | &root_location); |
| 3099 | if (!cur_root) { |
| 3100 | ret = 1; |
| 3101 | break; |
| 3102 | } |
| 3103 | } |
| 3104 | out: |
| 3105 | path->lowest_level = 0; |
| 3106 | return ret; |
| 3107 | } |
| 3108 | |
| 3109 | /* |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3110 | * note, this releases the path |
| 3111 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3112 | static int noinline relocate_one_reference(struct btrfs_root *extent_root, |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3113 | struct btrfs_path *path, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3114 | struct btrfs_key *extent_key, |
| 3115 | u64 *last_file_objectid, |
| 3116 | u64 *last_file_offset, |
| 3117 | u64 *last_file_root, |
| 3118 | u64 last_extent) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3119 | { |
| 3120 | struct inode *inode; |
| 3121 | struct btrfs_root *found_root; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3122 | struct btrfs_key root_location; |
| 3123 | struct btrfs_key found_key; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3124 | struct btrfs_extent_ref *ref; |
| 3125 | u64 ref_root; |
| 3126 | u64 ref_gen; |
| 3127 | u64 ref_objectid; |
| 3128 | u64 ref_offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3129 | int ret; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3130 | int level; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3131 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3132 | WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex)); |
| 3133 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3134 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3135 | struct btrfs_extent_ref); |
| 3136 | ref_root = btrfs_ref_root(path->nodes[0], ref); |
| 3137 | ref_gen = btrfs_ref_generation(path->nodes[0], ref); |
| 3138 | ref_objectid = btrfs_ref_objectid(path->nodes[0], ref); |
| 3139 | ref_offset = btrfs_ref_offset(path->nodes[0], ref); |
| 3140 | btrfs_release_path(extent_root, path); |
| 3141 | |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3142 | root_location.objectid = ref_root; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3143 | if (ref_gen == 0) |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3144 | root_location.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3145 | else |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3146 | root_location.offset = (u64)-1; |
| 3147 | root_location.type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3148 | |
| 3149 | found_root = btrfs_read_fs_root_no_name(extent_root->fs_info, |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3150 | &root_location); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3151 | BUG_ON(!found_root); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3152 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3153 | |
| 3154 | if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3155 | found_key.objectid = ref_objectid; |
| 3156 | found_key.type = BTRFS_EXTENT_DATA_KEY; |
| 3157 | found_key.offset = ref_offset; |
| 3158 | level = 0; |
| 3159 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3160 | if (last_extent == extent_key->objectid && |
| 3161 | *last_file_objectid == ref_objectid && |
| 3162 | *last_file_offset == ref_offset && |
| 3163 | *last_file_root == ref_root) |
| 3164 | goto out; |
| 3165 | |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3166 | ret = find_root_for_ref(extent_root, path, &found_key, |
| 3167 | level, 1, &found_root, |
| 3168 | extent_key->objectid); |
| 3169 | |
| 3170 | if (ret) |
| 3171 | goto out; |
| 3172 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3173 | if (last_extent == extent_key->objectid && |
| 3174 | *last_file_objectid == ref_objectid && |
| 3175 | *last_file_offset == ref_offset && |
| 3176 | *last_file_root == ref_root) |
| 3177 | goto out; |
| 3178 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3179 | inode = btrfs_iget_locked(extent_root->fs_info->sb, |
| 3180 | ref_objectid, found_root); |
| 3181 | if (inode->i_state & I_NEW) { |
| 3182 | /* the inode and parent dir are two different roots */ |
| 3183 | BTRFS_I(inode)->root = found_root; |
| 3184 | BTRFS_I(inode)->location.objectid = ref_objectid; |
| 3185 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 3186 | BTRFS_I(inode)->location.offset = 0; |
| 3187 | btrfs_read_locked_inode(inode); |
| 3188 | unlock_new_inode(inode); |
| 3189 | |
| 3190 | } |
| 3191 | /* this can happen if the reference is not against |
| 3192 | * the latest version of the tree root |
| 3193 | */ |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3194 | if (is_bad_inode(inode)) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3195 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3196 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3197 | *last_file_objectid = inode->i_ino; |
| 3198 | *last_file_root = found_root->root_key.objectid; |
| 3199 | *last_file_offset = ref_offset; |
| 3200 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3201 | relocate_inode_pages(inode, ref_offset, extent_key->offset); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3202 | iput(inode); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3203 | } else { |
| 3204 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3205 | struct extent_buffer *eb; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3206 | int needs_lock = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3207 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3208 | eb = read_tree_block(found_root, extent_key->objectid, |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3209 | extent_key->offset, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3210 | btrfs_tree_lock(eb); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3211 | level = btrfs_header_level(eb); |
| 3212 | |
| 3213 | if (level == 0) |
| 3214 | btrfs_item_key_to_cpu(eb, &found_key, 0); |
| 3215 | else |
| 3216 | btrfs_node_key_to_cpu(eb, &found_key, 0); |
| 3217 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3218 | btrfs_tree_unlock(eb); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3219 | free_extent_buffer(eb); |
| 3220 | |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3221 | ret = find_root_for_ref(extent_root, path, &found_key, |
| 3222 | level, 0, &found_root, |
| 3223 | extent_key->objectid); |
| 3224 | |
| 3225 | if (ret) |
| 3226 | goto out; |
| 3227 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3228 | /* |
| 3229 | * right here almost anything could happen to our key, |
| 3230 | * but that's ok. The cow below will either relocate it |
| 3231 | * or someone else will have relocated it. Either way, |
| 3232 | * it is in a different spot than it was before and |
| 3233 | * we're happy. |
| 3234 | */ |
| 3235 | |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3236 | trans = btrfs_start_transaction(found_root, 1); |
| 3237 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3238 | if (found_root == extent_root->fs_info->extent_root || |
| 3239 | found_root == extent_root->fs_info->chunk_root || |
| 3240 | found_root == extent_root->fs_info->dev_root) { |
| 3241 | needs_lock = 1; |
| 3242 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
| 3243 | } |
| 3244 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3245 | path->lowest_level = level; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 3246 | path->reada = 2; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3247 | ret = btrfs_search_slot(trans, found_root, &found_key, path, |
| 3248 | 0, 1); |
| 3249 | path->lowest_level = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3250 | btrfs_release_path(found_root, path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3251 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3252 | if (found_root == found_root->fs_info->extent_root) |
| 3253 | btrfs_extent_post_op(trans, found_root); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3254 | if (needs_lock) |
| 3255 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3256 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3257 | btrfs_end_transaction(trans, found_root); |
| 3258 | |
| 3259 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3260 | out: |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3261 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3262 | return 0; |
| 3263 | } |
| 3264 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3265 | static int noinline del_extent_zero(struct btrfs_root *extent_root, |
| 3266 | struct btrfs_path *path, |
| 3267 | struct btrfs_key *extent_key) |
| 3268 | { |
| 3269 | int ret; |
| 3270 | struct btrfs_trans_handle *trans; |
| 3271 | |
| 3272 | trans = btrfs_start_transaction(extent_root, 1); |
| 3273 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 3274 | if (ret > 0) { |
| 3275 | ret = -EIO; |
| 3276 | goto out; |
| 3277 | } |
| 3278 | if (ret < 0) |
| 3279 | goto out; |
| 3280 | ret = btrfs_del_item(trans, extent_root, path); |
| 3281 | out: |
| 3282 | btrfs_end_transaction(trans, extent_root); |
| 3283 | return ret; |
| 3284 | } |
| 3285 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3286 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
| 3287 | struct btrfs_path *path, |
| 3288 | struct btrfs_key *extent_key) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3289 | { |
| 3290 | struct btrfs_key key; |
| 3291 | struct btrfs_key found_key; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3292 | struct extent_buffer *leaf; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3293 | u64 last_file_objectid = 0; |
| 3294 | u64 last_file_root = 0; |
| 3295 | u64 last_file_offset = (u64)-1; |
| 3296 | u64 last_extent = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3297 | u32 nritems; |
| 3298 | u32 item_size; |
| 3299 | int ret = 0; |
| 3300 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3301 | if (extent_key->objectid == 0) { |
| 3302 | ret = del_extent_zero(extent_root, path, extent_key); |
| 3303 | goto out; |
| 3304 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3305 | key.objectid = extent_key->objectid; |
| 3306 | key.type = BTRFS_EXTENT_REF_KEY; |
| 3307 | key.offset = 0; |
| 3308 | |
| 3309 | while(1) { |
| 3310 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 3311 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3312 | if (ret < 0) |
| 3313 | goto out; |
| 3314 | |
| 3315 | ret = 0; |
| 3316 | leaf = path->nodes[0]; |
| 3317 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3318 | if (path->slots[0] == nritems) { |
| 3319 | ret = btrfs_next_leaf(extent_root, path); |
| 3320 | if (ret > 0) { |
| 3321 | ret = 0; |
| 3322 | goto out; |
| 3323 | } |
| 3324 | if (ret < 0) |
| 3325 | goto out; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3326 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3327 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3328 | |
| 3329 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3330 | if (found_key.objectid != extent_key->objectid) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3331 | break; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3332 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3333 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3334 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3335 | break; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3336 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3337 | |
| 3338 | key.offset = found_key.offset + 1; |
| 3339 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 3340 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3341 | ret = relocate_one_reference(extent_root, path, extent_key, |
| 3342 | &last_file_objectid, |
| 3343 | &last_file_offset, |
| 3344 | &last_file_root, last_extent); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3345 | if (ret) |
| 3346 | goto out; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3347 | last_extent = extent_key->objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3348 | } |
| 3349 | ret = 0; |
| 3350 | out: |
| 3351 | btrfs_release_path(extent_root, path); |
| 3352 | return ret; |
| 3353 | } |
| 3354 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3355 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 3356 | { |
| 3357 | u64 num_devices; |
| 3358 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 3359 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 3360 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3361 | num_devices = root->fs_info->fs_devices->num_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3362 | if (num_devices == 1) { |
| 3363 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 3364 | stripped = flags & ~stripped; |
| 3365 | |
| 3366 | /* turn raid0 into single device chunks */ |
| 3367 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 3368 | return stripped; |
| 3369 | |
| 3370 | /* turn mirroring into duplication */ |
| 3371 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 3372 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3373 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 3374 | return flags; |
| 3375 | } else { |
| 3376 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3377 | if (flags & stripped) |
| 3378 | return flags; |
| 3379 | |
| 3380 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 3381 | stripped = flags & ~stripped; |
| 3382 | |
| 3383 | /* switch duplicated blocks with raid1 */ |
| 3384 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 3385 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 3386 | |
| 3387 | /* turn single device chunks into raid0 */ |
| 3388 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 3389 | } |
| 3390 | return flags; |
| 3391 | } |
| 3392 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3393 | int __alloc_chunk_for_shrink(struct btrfs_root *root, |
| 3394 | struct btrfs_block_group_cache *shrink_block_group, |
| 3395 | int force) |
| 3396 | { |
| 3397 | struct btrfs_trans_handle *trans; |
| 3398 | u64 new_alloc_flags; |
| 3399 | u64 calc; |
| 3400 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3401 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3402 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3403 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3404 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3405 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3406 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3407 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3408 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3409 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3410 | new_alloc_flags = update_block_group_flags(root, |
| 3411 | shrink_block_group->flags); |
| 3412 | if (new_alloc_flags != shrink_block_group->flags) { |
| 3413 | calc = |
| 3414 | btrfs_block_group_used(&shrink_block_group->item); |
| 3415 | } else { |
| 3416 | calc = shrink_block_group->key.offset; |
| 3417 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3418 | spin_unlock(&shrink_block_group->lock); |
| 3419 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3420 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3421 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3422 | |
| 3423 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3424 | btrfs_end_transaction(trans, root); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3425 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3426 | } else |
| 3427 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3428 | return 0; |
| 3429 | } |
| 3430 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3431 | int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3432 | { |
| 3433 | struct btrfs_trans_handle *trans; |
| 3434 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
| 3435 | struct btrfs_path *path; |
| 3436 | u64 cur_byte; |
| 3437 | u64 total_found; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3438 | u64 shrink_last_byte; |
| 3439 | struct btrfs_block_group_cache *shrink_block_group; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3440 | struct btrfs_key key; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3441 | struct btrfs_key found_key; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3442 | struct extent_buffer *leaf; |
| 3443 | u32 nritems; |
| 3444 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3445 | int progress; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3446 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3447 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3448 | shrink_block_group = btrfs_lookup_block_group(root->fs_info, |
| 3449 | shrink_start); |
| 3450 | BUG_ON(!shrink_block_group); |
| 3451 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3452 | shrink_last_byte = shrink_block_group->key.objectid + |
| 3453 | shrink_block_group->key.offset; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3454 | |
| 3455 | shrink_block_group->space_info->total_bytes -= |
| 3456 | shrink_block_group->key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3457 | path = btrfs_alloc_path(); |
| 3458 | root = root->fs_info->extent_root; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 3459 | path->reada = 2; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3460 | |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 3461 | printk("btrfs relocating block group %llu flags %llu\n", |
| 3462 | (unsigned long long)shrink_start, |
| 3463 | (unsigned long long)shrink_block_group->flags); |
| 3464 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3465 | __alloc_chunk_for_shrink(root, shrink_block_group, 1); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 3466 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3467 | again: |
| 3468 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3469 | shrink_block_group->ro = 1; |
| 3470 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3471 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3472 | progress = 0; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3473 | key.objectid = shrink_start; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3474 | key.offset = 0; |
| 3475 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3476 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3477 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3478 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 3479 | |
| 3480 | btrfs_start_delalloc_inodes(root); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 3481 | btrfs_wait_ordered_extents(tree_root, 0); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3482 | |
| 3483 | mutex_lock(&root->fs_info->alloc_mutex); |
| 3484 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3485 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3486 | if (ret < 0) |
| 3487 | goto out; |
| 3488 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3489 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3490 | if (ret < 0) |
| 3491 | goto out; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3492 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3493 | if (ret == 0) { |
| 3494 | leaf = path->nodes[0]; |
| 3495 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3496 | if (found_key.objectid + found_key.offset > shrink_start && |
| 3497 | found_key.objectid < shrink_last_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3498 | cur_byte = found_key.objectid; |
| 3499 | key.objectid = cur_byte; |
| 3500 | } |
| 3501 | } |
| 3502 | btrfs_release_path(root, path); |
| 3503 | |
| 3504 | while(1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3505 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3506 | if (ret < 0) |
| 3507 | goto out; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3508 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3509 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3510 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3511 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3512 | if (path->slots[0] >= nritems) { |
| 3513 | ret = btrfs_next_leaf(root, path); |
| 3514 | if (ret < 0) |
| 3515 | goto out; |
| 3516 | if (ret == 1) { |
| 3517 | ret = 0; |
| 3518 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3519 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3520 | leaf = path->nodes[0]; |
| 3521 | nritems = btrfs_header_nritems(leaf); |
| 3522 | } |
| 3523 | |
| 3524 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 3525 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3526 | if (found_key.objectid >= shrink_last_byte) |
| 3527 | break; |
| 3528 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 3529 | if (progress && need_resched()) { |
| 3530 | memcpy(&key, &found_key, sizeof(key)); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 3531 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 3532 | btrfs_release_path(root, path); |
| 3533 | btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 3534 | progress = 0; |
| 3535 | goto next; |
| 3536 | } |
| 3537 | progress = 1; |
| 3538 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3539 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY || |
| 3540 | found_key.objectid + found_key.offset <= cur_byte) { |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3541 | memcpy(&key, &found_key, sizeof(key)); |
| 3542 | key.offset++; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3543 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3544 | goto next; |
| 3545 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3546 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3547 | total_found++; |
| 3548 | cur_byte = found_key.objectid + found_key.offset; |
| 3549 | key.objectid = cur_byte; |
| 3550 | btrfs_release_path(root, path); |
| 3551 | ret = relocate_one_extent(root, path, &found_key); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3552 | __alloc_chunk_for_shrink(root, shrink_block_group, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | btrfs_release_path(root, path); |
| 3556 | |
| 3557 | if (total_found > 0) { |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 3558 | printk("btrfs relocate found %llu last extent was %llu\n", |
| 3559 | (unsigned long long)total_found, |
| 3560 | (unsigned long long)found_key.objectid); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3561 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3562 | trans = btrfs_start_transaction(tree_root, 1); |
| 3563 | btrfs_commit_transaction(trans, tree_root); |
| 3564 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3565 | btrfs_clean_old_snapshots(tree_root); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3566 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3567 | btrfs_start_delalloc_inodes(root); |
Yan Zheng | 7ea394f | 2008-08-05 13:05:02 -0400 | [diff] [blame] | 3568 | btrfs_wait_ordered_extents(tree_root, 0); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3569 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3570 | trans = btrfs_start_transaction(tree_root, 1); |
| 3571 | btrfs_commit_transaction(trans, tree_root); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3572 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3573 | goto again; |
| 3574 | } |
| 3575 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3576 | /* |
| 3577 | * we've freed all the extents, now remove the block |
| 3578 | * group item from the tree |
| 3579 | */ |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3580 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 3581 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3582 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3583 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3584 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3585 | memcpy(&key, &shrink_block_group->key, sizeof(key)); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3586 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3587 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 3588 | if (ret > 0) |
| 3589 | ret = -EIO; |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3590 | if (ret < 0) { |
| 3591 | btrfs_end_transaction(trans, root); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3592 | goto out; |
Josef Bacik | 8e8a1e3 | 2008-07-24 12:17:14 -0400 | [diff] [blame] | 3593 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3594 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3595 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 3596 | rb_erase(&shrink_block_group->cache_node, |
| 3597 | &root->fs_info->block_group_cache_tree); |
| 3598 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 3599 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3600 | ret = btrfs_remove_free_space(shrink_block_group, key.objectid, |
| 3601 | key.offset); |
| 3602 | if (ret) { |
| 3603 | btrfs_end_transaction(trans, root); |
| 3604 | goto out; |
| 3605 | } |
Chris Mason | d7a029a | 2008-08-04 23:17:26 -0400 | [diff] [blame] | 3606 | /* |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3607 | memset(shrink_block_group, 0, sizeof(*shrink_block_group)); |
| 3608 | kfree(shrink_block_group); |
Chris Mason | d7a029a | 2008-08-04 23:17:26 -0400 | [diff] [blame] | 3609 | */ |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3610 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3611 | btrfs_del_item(trans, root, path); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3612 | btrfs_release_path(root, path); |
| 3613 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3614 | btrfs_commit_transaction(trans, root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3615 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3616 | mutex_lock(&root->fs_info->alloc_mutex); |
| 3617 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3618 | /* the code to unpin extents might set a few bits in the free |
| 3619 | * space cache for this range again |
| 3620 | */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3621 | /* XXX? */ |
| 3622 | ret = btrfs_remove_free_space(shrink_block_group, key.objectid, |
| 3623 | key.offset); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3624 | out: |
| 3625 | btrfs_free_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3626 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3627 | return ret; |
| 3628 | } |
| 3629 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3630 | int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, |
| 3631 | struct btrfs_key *key) |
| 3632 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3633 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3634 | struct btrfs_key found_key; |
| 3635 | struct extent_buffer *leaf; |
| 3636 | int slot; |
| 3637 | |
| 3638 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 3639 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3640 | goto out; |
| 3641 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3642 | while(1) { |
| 3643 | slot = path->slots[0]; |
| 3644 | leaf = path->nodes[0]; |
| 3645 | if (slot >= btrfs_header_nritems(leaf)) { |
| 3646 | ret = btrfs_next_leaf(root, path); |
| 3647 | if (ret == 0) |
| 3648 | continue; |
| 3649 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3650 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3651 | break; |
| 3652 | } |
| 3653 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 3654 | |
| 3655 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3656 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 3657 | ret = 0; |
| 3658 | goto out; |
| 3659 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3660 | path->slots[0]++; |
| 3661 | } |
| 3662 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3663 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3664 | return ret; |
| 3665 | } |
| 3666 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3667 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 3668 | { |
| 3669 | struct btrfs_path *path; |
| 3670 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3671 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 3672 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3673 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3674 | struct btrfs_key key; |
| 3675 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3676 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3677 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 3678 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3679 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3680 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3681 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3682 | path = btrfs_alloc_path(); |
| 3683 | if (!path) |
| 3684 | return -ENOMEM; |
| 3685 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3686 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3687 | while(1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3688 | ret = find_first_block_group(root, path, &key); |
| 3689 | if (ret > 0) { |
| 3690 | ret = 0; |
| 3691 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3692 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3693 | if (ret != 0) |
| 3694 | goto error; |
| 3695 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3696 | leaf = path->nodes[0]; |
| 3697 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3698 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3699 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3700 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3701 | break; |
| 3702 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 3703 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3704 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3705 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3706 | read_extent_buffer(leaf, &cache->item, |
| 3707 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 3708 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3709 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3710 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3711 | key.objectid = found_key.objectid + found_key.offset; |
| 3712 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3713 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3714 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3715 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 3716 | btrfs_block_group_used(&cache->item), |
| 3717 | &space_info); |
| 3718 | BUG_ON(ret); |
| 3719 | cache->space_info = space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3720 | spin_lock(&space_info->lock); |
| 3721 | list_add(&cache->list, &space_info->block_groups); |
| 3722 | spin_unlock(&space_info->lock); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3723 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3724 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 3725 | BUG_ON(ret); |
| 3726 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3727 | if (key.objectid >= |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3728 | btrfs_super_total_bytes(&info->super_copy)) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3729 | break; |
| 3730 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3731 | ret = 0; |
| 3732 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3733 | btrfs_free_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3734 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3735 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3736 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3737 | |
| 3738 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 3739 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3740 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3741 | u64 size) |
| 3742 | { |
| 3743 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3744 | struct btrfs_root *extent_root; |
| 3745 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3746 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3747 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3748 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3749 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3750 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
| 3751 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3752 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3753 | if (!cache) |
| 3754 | return -ENOMEM; |
| 3755 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3756 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3757 | cache->key.offset = size; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3758 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3759 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3760 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3761 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3762 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3763 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 3764 | cache->flags = type; |
| 3765 | btrfs_set_block_group_flags(&cache->item, type); |
| 3766 | |
| 3767 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 3768 | &cache->space_info); |
| 3769 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3770 | spin_lock(&cache->space_info->lock); |
| 3771 | list_add(&cache->list, &cache->space_info->block_groups); |
| 3772 | spin_unlock(&cache->space_info->lock); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3773 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3774 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 3775 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 3776 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3777 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 3778 | sizeof(cache->item)); |
| 3779 | BUG_ON(ret); |
| 3780 | |
| 3781 | finish_current_insert(trans, extent_root); |
| 3782 | ret = del_pending_extents(trans, extent_root); |
| 3783 | BUG_ON(ret); |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 3784 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3785 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3786 | return 0; |
| 3787 | } |