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