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 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1095 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1096 | u64 root_gen; |
| 1097 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1098 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1099 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1100 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1101 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1102 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1103 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1104 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1105 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1106 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 1107 | shared = 0; |
| 1108 | root_gen = root->root_key.offset; |
| 1109 | } else { |
| 1110 | shared = 1; |
| 1111 | root_gen = trans->transid - 1; |
| 1112 | } |
| 1113 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1114 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1115 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1116 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1117 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1118 | struct btrfs_leaf_ref *ref; |
| 1119 | struct btrfs_extent_info *info; |
| 1120 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1121 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1122 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1123 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1124 | goto out; |
| 1125 | } |
| 1126 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1127 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1128 | ref->bytenr = buf->start; |
| 1129 | ref->owner = btrfs_header_owner(buf); |
| 1130 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1131 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1132 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1133 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1134 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1135 | u64 disk_bytenr; |
| 1136 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1137 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1138 | continue; |
| 1139 | fi = btrfs_item_ptr(buf, i, |
| 1140 | struct btrfs_file_extent_item); |
| 1141 | if (btrfs_file_extent_type(buf, fi) == |
| 1142 | BTRFS_FILE_EXTENT_INLINE) |
| 1143 | continue; |
| 1144 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1145 | if (disk_bytenr == 0) |
| 1146 | continue; |
| 1147 | |
| 1148 | info->bytenr = disk_bytenr; |
| 1149 | info->num_bytes = |
| 1150 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 1151 | info->objectid = key.objectid; |
| 1152 | info->offset = key.offset; |
| 1153 | info++; |
| 1154 | } |
| 1155 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1156 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1157 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1158 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1159 | } |
| 1160 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1165 | struct extent_buffer *orig_buf, struct extent_buffer *buf, |
| 1166 | u32 *nr_extents) |
| 1167 | { |
| 1168 | u64 bytenr; |
| 1169 | u64 ref_root; |
| 1170 | u64 orig_root; |
| 1171 | u64 ref_generation; |
| 1172 | u64 orig_generation; |
| 1173 | u32 nritems; |
| 1174 | u32 nr_file_extents = 0; |
| 1175 | struct btrfs_key key; |
| 1176 | struct btrfs_file_extent_item *fi; |
| 1177 | int i; |
| 1178 | int level; |
| 1179 | int ret = 0; |
| 1180 | int faili = 0; |
| 1181 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
| 1182 | u64, u64, u64, u64, u64, u64, u64, u64, u64); |
| 1183 | |
| 1184 | ref_root = btrfs_header_owner(buf); |
| 1185 | ref_generation = btrfs_header_generation(buf); |
| 1186 | orig_root = btrfs_header_owner(orig_buf); |
| 1187 | orig_generation = btrfs_header_generation(orig_buf); |
| 1188 | |
| 1189 | nritems = btrfs_header_nritems(buf); |
| 1190 | level = btrfs_header_level(buf); |
| 1191 | |
| 1192 | if (root->ref_cows) { |
| 1193 | process_func = __btrfs_inc_extent_ref; |
| 1194 | } else { |
| 1195 | if (level == 0 && |
| 1196 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1197 | goto out; |
| 1198 | if (level != 0 && |
| 1199 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1200 | goto out; |
| 1201 | process_func = __btrfs_update_extent_ref; |
| 1202 | } |
| 1203 | |
| 1204 | for (i = 0; i < nritems; i++) { |
| 1205 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1206 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1207 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1208 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1209 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1210 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1211 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1212 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1213 | BTRFS_FILE_EXTENT_INLINE) |
| 1214 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1215 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1216 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1217 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1218 | |
| 1219 | nr_file_extents++; |
| 1220 | |
| 1221 | maybe_lock_mutex(root); |
| 1222 | ret = process_func(trans, root, bytenr, |
| 1223 | orig_buf->start, buf->start, |
| 1224 | orig_root, ref_root, |
| 1225 | orig_generation, ref_generation, |
| 1226 | key.objectid, key.offset); |
| 1227 | maybe_unlock_mutex(root); |
| 1228 | |
| 1229 | if (ret) { |
| 1230 | faili = i; |
| 1231 | WARN_ON(1); |
| 1232 | goto fail; |
| 1233 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1234 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1235 | bytenr = btrfs_node_blockptr(buf, i); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1236 | maybe_lock_mutex(root); |
| 1237 | ret = process_func(trans, root, bytenr, |
| 1238 | orig_buf->start, buf->start, |
| 1239 | orig_root, ref_root, |
| 1240 | orig_generation, ref_generation, |
| 1241 | level - 1, 0); |
| 1242 | maybe_unlock_mutex(root); |
| 1243 | if (ret) { |
| 1244 | faili = i; |
| 1245 | WARN_ON(1); |
| 1246 | goto fail; |
| 1247 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1248 | } |
| 1249 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1250 | out: |
| 1251 | if (nr_extents) { |
| 1252 | if (level == 0) |
| 1253 | *nr_extents = nr_file_extents; |
| 1254 | else |
| 1255 | *nr_extents = nritems; |
| 1256 | } |
| 1257 | return 0; |
| 1258 | fail: |
| 1259 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1260 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1263 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
| 1264 | struct btrfs_root *root, struct extent_buffer *orig_buf, |
| 1265 | struct extent_buffer *buf, int start_slot, int nr) |
| 1266 | |
| 1267 | { |
| 1268 | u64 bytenr; |
| 1269 | u64 ref_root; |
| 1270 | u64 orig_root; |
| 1271 | u64 ref_generation; |
| 1272 | u64 orig_generation; |
| 1273 | struct btrfs_key key; |
| 1274 | struct btrfs_file_extent_item *fi; |
| 1275 | int i; |
| 1276 | int ret; |
| 1277 | int slot; |
| 1278 | int level; |
| 1279 | |
| 1280 | BUG_ON(start_slot < 0); |
| 1281 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); |
| 1282 | |
| 1283 | ref_root = btrfs_header_owner(buf); |
| 1284 | ref_generation = btrfs_header_generation(buf); |
| 1285 | orig_root = btrfs_header_owner(orig_buf); |
| 1286 | orig_generation = btrfs_header_generation(orig_buf); |
| 1287 | level = btrfs_header_level(buf); |
| 1288 | |
| 1289 | if (!root->ref_cows) { |
| 1290 | if (level == 0 && |
| 1291 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1292 | return 0; |
| 1293 | if (level != 0 && |
| 1294 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { |
| 1299 | cond_resched(); |
| 1300 | if (level == 0) { |
| 1301 | btrfs_item_key_to_cpu(buf, &key, slot); |
| 1302 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1303 | continue; |
| 1304 | fi = btrfs_item_ptr(buf, slot, |
| 1305 | struct btrfs_file_extent_item); |
| 1306 | if (btrfs_file_extent_type(buf, fi) == |
| 1307 | BTRFS_FILE_EXTENT_INLINE) |
| 1308 | continue; |
| 1309 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1310 | if (bytenr == 0) |
| 1311 | continue; |
| 1312 | maybe_lock_mutex(root); |
| 1313 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1314 | orig_buf->start, buf->start, |
| 1315 | orig_root, ref_root, |
| 1316 | orig_generation, ref_generation, |
| 1317 | key.objectid, key.offset); |
| 1318 | maybe_unlock_mutex(root); |
| 1319 | if (ret) |
| 1320 | goto fail; |
| 1321 | } else { |
| 1322 | bytenr = btrfs_node_blockptr(buf, slot); |
| 1323 | maybe_lock_mutex(root); |
| 1324 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1325 | orig_buf->start, buf->start, |
| 1326 | orig_root, ref_root, |
| 1327 | orig_generation, ref_generation, |
| 1328 | level - 1, 0); |
| 1329 | maybe_unlock_mutex(root); |
| 1330 | if (ret) |
| 1331 | goto fail; |
| 1332 | } |
| 1333 | } |
| 1334 | return 0; |
| 1335 | fail: |
| 1336 | WARN_ON(1); |
| 1337 | return -1; |
| 1338 | } |
| 1339 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1340 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1341 | struct btrfs_root *root, |
| 1342 | struct btrfs_path *path, |
| 1343 | struct btrfs_block_group_cache *cache) |
| 1344 | { |
| 1345 | int ret; |
| 1346 | int pending_ret; |
| 1347 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1348 | unsigned long bi; |
| 1349 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1350 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1351 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1352 | if (ret < 0) |
| 1353 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1354 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1355 | |
| 1356 | leaf = path->nodes[0]; |
| 1357 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1358 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1359 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1360 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1361 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1362 | finish_current_insert(trans, extent_root); |
| 1363 | pending_ret = del_pending_extents(trans, extent_root); |
| 1364 | if (ret) |
| 1365 | return ret; |
| 1366 | if (pending_ret) |
| 1367 | return pending_ret; |
| 1368 | return 0; |
| 1369 | |
| 1370 | } |
| 1371 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1372 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1373 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1374 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1375 | struct btrfs_block_group_cache *cache, *entry; |
| 1376 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1377 | int err = 0; |
| 1378 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1379 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1380 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1381 | |
| 1382 | path = btrfs_alloc_path(); |
| 1383 | if (!path) |
| 1384 | return -ENOMEM; |
| 1385 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1386 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1387 | while(1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1388 | cache = NULL; |
| 1389 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1390 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1391 | n; n = rb_next(n)) { |
| 1392 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1393 | cache_node); |
| 1394 | if (entry->dirty) { |
| 1395 | cache = entry; |
| 1396 | break; |
| 1397 | } |
| 1398 | } |
| 1399 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1400 | |
| 1401 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1402 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1403 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1404 | cache->dirty = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1405 | last += cache->key.offset; |
| 1406 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1407 | err = write_one_cache_group(trans, root, |
| 1408 | path, cache); |
| 1409 | /* |
| 1410 | * if we fail to write the cache group, we want |
| 1411 | * to keep it marked dirty in hopes that a later |
| 1412 | * write will work |
| 1413 | */ |
| 1414 | if (err) { |
| 1415 | werr = err; |
| 1416 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1417 | } |
| 1418 | } |
| 1419 | btrfs_free_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1420 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1421 | return werr; |
| 1422 | } |
| 1423 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1424 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1425 | u64 total_bytes, u64 bytes_used, |
| 1426 | struct btrfs_space_info **space_info) |
| 1427 | { |
| 1428 | struct btrfs_space_info *found; |
| 1429 | |
| 1430 | found = __find_space_info(info, flags); |
| 1431 | if (found) { |
| 1432 | found->total_bytes += total_bytes; |
| 1433 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1434 | found->full = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1435 | *space_info = found; |
| 1436 | return 0; |
| 1437 | } |
| 1438 | found = kmalloc(sizeof(*found), GFP_NOFS); |
| 1439 | if (!found) |
| 1440 | return -ENOMEM; |
| 1441 | |
| 1442 | list_add(&found->list, &info->space_info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1443 | INIT_LIST_HEAD(&found->block_groups); |
| 1444 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1445 | found->flags = flags; |
| 1446 | found->total_bytes = total_bytes; |
| 1447 | found->bytes_used = bytes_used; |
| 1448 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1449 | found->bytes_reserved = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1450 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1451 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1452 | *space_info = found; |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1456 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1457 | { |
| 1458 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1459 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1460 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1461 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1462 | if (extra_flags) { |
| 1463 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1464 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1465 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1466 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1467 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1468 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1469 | } |
| 1470 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1471 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1472 | static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1473 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1474 | u64 num_devices = root->fs_info->fs_devices->num_devices; |
| 1475 | |
| 1476 | if (num_devices == 1) |
| 1477 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1478 | if (num_devices < 4) |
| 1479 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1480 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1481 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1482 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1483 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1484 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1485 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1486 | |
| 1487 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1488 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1489 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1490 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1491 | |
| 1492 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1493 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1494 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1495 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1496 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1497 | return flags; |
| 1498 | } |
| 1499 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1500 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1501 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1502 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1503 | { |
| 1504 | struct btrfs_space_info *space_info; |
| 1505 | u64 thresh; |
| 1506 | u64 start; |
| 1507 | u64 num_bytes; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1508 | int ret = 0; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1509 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1510 | flags = reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1511 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1512 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1513 | if (!space_info) { |
| 1514 | ret = update_space_info(extent_root->fs_info, flags, |
| 1515 | 0, 0, &space_info); |
| 1516 | BUG_ON(ret); |
| 1517 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1518 | BUG_ON(!space_info); |
| 1519 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1520 | if (space_info->force_alloc) { |
| 1521 | force = 1; |
| 1522 | space_info->force_alloc = 0; |
| 1523 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1524 | if (space_info->full) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1525 | goto out; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1526 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1527 | thresh = div_factor(space_info->total_bytes, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1528 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1529 | (space_info->bytes_used + space_info->bytes_pinned + |
| 1530 | space_info->bytes_reserved + alloc_bytes) < thresh) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1531 | goto out; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1532 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1533 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1534 | ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags); |
| 1535 | if (ret == -ENOSPC) { |
| 1536 | printk("space info full %Lu\n", flags); |
| 1537 | space_info->full = 1; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1538 | goto out_unlock; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1539 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1540 | BUG_ON(ret); |
| 1541 | |
| 1542 | ret = btrfs_make_block_group(trans, extent_root, 0, flags, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1543 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1544 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1545 | |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1546 | out_unlock: |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1547 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1548 | out: |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1549 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1550 | } |
| 1551 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1552 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1553 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1554 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1555 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1556 | { |
| 1557 | struct btrfs_block_group_cache *cache; |
| 1558 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1559 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1560 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1561 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1562 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1563 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1564 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1565 | cache = btrfs_lookup_block_group(info, bytenr); |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1566 | if (!cache) { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1567 | return -1; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1568 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1569 | byte_in_group = bytenr - cache->key.objectid; |
| 1570 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1571 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1572 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1573 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1574 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1575 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1576 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1577 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1578 | cache->space_info->bytes_used += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1579 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1580 | spin_unlock(&cache->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1581 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1582 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1583 | cache->space_info->bytes_used -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1584 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1585 | spin_unlock(&cache->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1586 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1587 | int ret; |
| 1588 | ret = btrfs_add_free_space(cache, bytenr, |
| 1589 | num_bytes); |
| 1590 | if (ret) |
| 1591 | return -1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1592 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1593 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1594 | total -= num_bytes; |
| 1595 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1596 | } |
| 1597 | return 0; |
| 1598 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1599 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1600 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 1601 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1602 | struct btrfs_block_group_cache *cache; |
| 1603 | |
| 1604 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 1605 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1606 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1607 | |
| 1608 | return cache->key.objectid; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1609 | } |
| 1610 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1611 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1612 | u64 bytenr, u64 num, int pin) |
| 1613 | { |
| 1614 | u64 len; |
| 1615 | struct btrfs_block_group_cache *cache; |
| 1616 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1617 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1618 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1619 | if (pin) { |
| 1620 | set_extent_dirty(&fs_info->pinned_extents, |
| 1621 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1622 | } else { |
| 1623 | clear_extent_dirty(&fs_info->pinned_extents, |
| 1624 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1625 | } |
| 1626 | while (num > 0) { |
| 1627 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1628 | BUG_ON(!cache); |
| 1629 | len = min(num, cache->key.offset - |
| 1630 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1631 | if (pin) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1632 | spin_lock(&cache->lock); |
| 1633 | cache->pinned += len; |
| 1634 | cache->space_info->bytes_pinned += len; |
| 1635 | spin_unlock(&cache->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1636 | fs_info->total_pinned += len; |
| 1637 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1638 | spin_lock(&cache->lock); |
| 1639 | cache->pinned -= len; |
| 1640 | cache->space_info->bytes_pinned -= len; |
| 1641 | spin_unlock(&cache->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1642 | fs_info->total_pinned -= len; |
| 1643 | } |
| 1644 | bytenr += len; |
| 1645 | num -= len; |
| 1646 | } |
| 1647 | return 0; |
| 1648 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1649 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1650 | static int update_reserved_extents(struct btrfs_root *root, |
| 1651 | u64 bytenr, u64 num, int reserve) |
| 1652 | { |
| 1653 | u64 len; |
| 1654 | struct btrfs_block_group_cache *cache; |
| 1655 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1656 | |
| 1657 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
| 1658 | while (num > 0) { |
| 1659 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 1660 | BUG_ON(!cache); |
| 1661 | len = min(num, cache->key.offset - |
| 1662 | (bytenr - cache->key.objectid)); |
| 1663 | if (reserve) { |
| 1664 | spin_lock(&cache->lock); |
| 1665 | cache->reserved += len; |
| 1666 | cache->space_info->bytes_reserved += len; |
| 1667 | spin_unlock(&cache->lock); |
| 1668 | } else { |
| 1669 | spin_lock(&cache->lock); |
| 1670 | cache->reserved -= len; |
| 1671 | cache->space_info->bytes_reserved -= len; |
| 1672 | spin_unlock(&cache->lock); |
| 1673 | } |
| 1674 | bytenr += len; |
| 1675 | num -= len; |
| 1676 | } |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1680 | 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] | 1681 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1682 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1683 | u64 start; |
| 1684 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1685 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1686 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1687 | |
| 1688 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1689 | ret = find_first_extent_bit(pinned_extents, last, |
| 1690 | &start, &end, EXTENT_DIRTY); |
| 1691 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1692 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1693 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 1694 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1695 | } |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 1700 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1701 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1702 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1703 | u64 start; |
| 1704 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1705 | int ret; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1706 | struct btrfs_block_group_cache *cache; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1707 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1708 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1709 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1710 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 1711 | EXTENT_DIRTY); |
| 1712 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1713 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1714 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1715 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1716 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 1717 | if (cache->cached) |
| 1718 | btrfs_add_free_space(cache, start, end - start + 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1719 | if (need_resched()) { |
| 1720 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 1721 | cond_resched(); |
| 1722 | mutex_lock(&root->fs_info->alloc_mutex); |
| 1723 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1724 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1725 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1726 | return 0; |
| 1727 | } |
| 1728 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1729 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
| 1730 | struct btrfs_root *extent_root) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1731 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1732 | u64 start; |
| 1733 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1734 | u64 priv; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1735 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 1736 | struct btrfs_path *path; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1737 | struct btrfs_extent_ref *ref; |
| 1738 | struct pending_extent_op *extent_op; |
| 1739 | struct btrfs_key key; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1740 | struct btrfs_extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1741 | int ret; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1742 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1743 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1744 | WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1745 | btrfs_set_stack_extent_refs(&extent_item, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1746 | path = btrfs_alloc_path(); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1747 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1748 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1749 | ret = find_first_extent_bit(&info->extent_ins, 0, &start, |
| 1750 | &end, EXTENT_LOCKED); |
| 1751 | if (ret) |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1752 | break; |
| 1753 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1754 | ret = get_state_private(&info->extent_ins, start, &priv); |
| 1755 | BUG_ON(ret); |
| 1756 | extent_op = (struct pending_extent_op *)(unsigned long)priv; |
| 1757 | |
| 1758 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
| 1759 | key.objectid = start; |
| 1760 | key.offset = end + 1 - start; |
| 1761 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1762 | err = btrfs_insert_item(trans, extent_root, &key, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1763 | &extent_item, sizeof(extent_item)); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1764 | BUG_ON(err); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1765 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1766 | clear_extent_bits(&info->extent_ins, start, end, |
| 1767 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1768 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1769 | err = insert_extent_backref(trans, extent_root, path, |
| 1770 | start, extent_op->parent, |
| 1771 | extent_root->root_key.objectid, |
| 1772 | extent_op->generation, |
| 1773 | extent_op->level, 0); |
| 1774 | BUG_ON(err); |
| 1775 | } else if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 1776 | err = lookup_extent_backref(trans, extent_root, path, |
| 1777 | start, extent_op->orig_parent, |
| 1778 | extent_root->root_key.objectid, |
| 1779 | extent_op->orig_generation, 0); |
| 1780 | BUG_ON(err); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1781 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1782 | clear_extent_bits(&info->extent_ins, start, end, |
| 1783 | EXTENT_LOCKED, GFP_NOFS); |
| 1784 | |
| 1785 | key.objectid = start; |
| 1786 | key.offset = extent_op->parent; |
| 1787 | key.type = BTRFS_EXTENT_REF_KEY; |
| 1788 | err = btrfs_set_item_key_safe(trans, extent_root, path, |
| 1789 | &key); |
| 1790 | BUG_ON(err); |
| 1791 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1792 | struct btrfs_extent_ref); |
| 1793 | btrfs_set_ref_generation(path->nodes[0], ref, |
| 1794 | extent_op->generation); |
| 1795 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 1796 | btrfs_release_path(extent_root, path); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1797 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1798 | BUG_ON(1); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1799 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1800 | kfree(extent_op); |
| 1801 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1802 | if (need_resched()) { |
| 1803 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 1804 | cond_resched(); |
| 1805 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
| 1806 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1807 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1808 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1809 | return 0; |
| 1810 | } |
| 1811 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1812 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 1813 | struct btrfs_root *root, |
| 1814 | u64 bytenr, u64 num_bytes, int is_data) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1815 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1816 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1817 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1818 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1819 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1820 | if (is_data) |
| 1821 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1822 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1823 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 1824 | if (!buf) |
| 1825 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1826 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1827 | /* we can reuse a block if it hasn't been written |
| 1828 | * and it is from this transaction. We can't |
| 1829 | * reuse anything from the tree log root because |
| 1830 | * it has tiny sub-transactions. |
| 1831 | */ |
| 1832 | if (btrfs_buffer_uptodate(buf, 0) && |
| 1833 | btrfs_try_tree_lock(buf)) { |
| 1834 | u64 header_owner = btrfs_header_owner(buf); |
| 1835 | u64 header_transid = btrfs_header_generation(buf); |
| 1836 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 1837 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1838 | header_transid == trans->transid && |
| 1839 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 1840 | clean_tree_block(NULL, root, buf); |
| 1841 | btrfs_tree_unlock(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1842 | free_extent_buffer(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1843 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1844 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1845 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1846 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1847 | free_extent_buffer(buf); |
| 1848 | pinit: |
| 1849 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 1850 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1851 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1852 | return 0; |
| 1853 | } |
| 1854 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1855 | /* |
| 1856 | * remove an extent from the root, returns 0 on success |
| 1857 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1858 | static int __free_extent(struct btrfs_trans_handle *trans, |
| 1859 | struct btrfs_root *root, |
| 1860 | u64 bytenr, u64 num_bytes, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1861 | u64 root_objectid, u64 ref_generation, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1862 | u64 owner_objectid, u64 owner_offset, |
| 1863 | int pin, int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1864 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1865 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1866 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1867 | struct btrfs_fs_info *info = root->fs_info; |
| 1868 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1869 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1870 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1871 | int extent_slot = 0; |
| 1872 | int found_extent = 0; |
| 1873 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1874 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1875 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1876 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 1877 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1878 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1879 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1880 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1881 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1882 | if (!path) |
| 1883 | return -ENOMEM; |
| 1884 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1885 | path->reada = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1886 | ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent, |
| 1887 | root_objectid, ref_generation, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1888 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1889 | struct btrfs_key found_key; |
| 1890 | extent_slot = path->slots[0]; |
| 1891 | while(extent_slot > 0) { |
| 1892 | extent_slot--; |
| 1893 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1894 | extent_slot); |
| 1895 | if (found_key.objectid != bytenr) |
| 1896 | break; |
| 1897 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1898 | found_key.offset == num_bytes) { |
| 1899 | found_extent = 1; |
| 1900 | break; |
| 1901 | } |
| 1902 | if (path->slots[0] - extent_slot > 5) |
| 1903 | break; |
| 1904 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1905 | if (!found_extent) { |
| 1906 | ret = remove_extent_backref(trans, extent_root, path); |
| 1907 | BUG_ON(ret); |
| 1908 | btrfs_release_path(extent_root, path); |
| 1909 | ret = btrfs_search_slot(trans, extent_root, |
| 1910 | &key, path, -1, 1); |
| 1911 | BUG_ON(ret); |
| 1912 | extent_slot = path->slots[0]; |
| 1913 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1914 | } else { |
| 1915 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 1916 | WARN_ON(1); |
| 1917 | printk("Unable to find ref byte nr %Lu root %Lu " |
| 1918 | " gen %Lu owner %Lu offset %Lu\n", bytenr, |
| 1919 | root_objectid, ref_generation, owner_objectid, |
| 1920 | owner_offset); |
| 1921 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1922 | |
| 1923 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1924 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1925 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1926 | refs = btrfs_extent_refs(leaf, ei); |
| 1927 | BUG_ON(refs == 0); |
| 1928 | refs -= 1; |
| 1929 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1930 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1931 | btrfs_mark_buffer_dirty(leaf); |
| 1932 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1933 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1934 | struct btrfs_extent_ref *ref; |
| 1935 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1936 | struct btrfs_extent_ref); |
| 1937 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1938 | /* if the back ref and the extent are next to each other |
| 1939 | * they get deleted below in one shot |
| 1940 | */ |
| 1941 | path->slots[0] = extent_slot; |
| 1942 | num_to_del = 2; |
| 1943 | } else if (found_extent) { |
| 1944 | /* otherwise delete the extent back ref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1945 | ret = remove_extent_backref(trans, extent_root, path); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1946 | BUG_ON(ret); |
| 1947 | /* if refs are 0, we need to setup the path for deletion */ |
| 1948 | if (refs == 0) { |
| 1949 | btrfs_release_path(extent_root, path); |
| 1950 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 1951 | -1, 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1952 | BUG_ON(ret); |
| 1953 | } |
| 1954 | } |
| 1955 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1956 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1957 | u64 super_used; |
| 1958 | u64 root_used; |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 1959 | #ifdef BIO_RW_DISCARD |
| 1960 | u64 map_length = num_bytes; |
| 1961 | struct btrfs_multi_bio *multi = NULL; |
| 1962 | #endif |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1963 | |
| 1964 | if (pin) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1965 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, |
| 1966 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1967 | if (ret > 0) |
| 1968 | mark_free = 1; |
| 1969 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1970 | } |
| 1971 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1972 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1973 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1974 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1975 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1976 | super_used - num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1977 | spin_unlock_irq(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1978 | |
| 1979 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1980 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1981 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1982 | root_used - num_bytes); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1983 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 1984 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1985 | BUG_ON(ret); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1986 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1987 | mark_free); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1988 | BUG_ON(ret); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 1989 | |
| 1990 | #ifdef BIO_RW_DISCARD |
| 1991 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1992 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1993 | bytenr, &map_length, &multi, 0); |
| 1994 | if (!ret) { |
| 1995 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1996 | int i; |
| 1997 | |
| 1998 | if (map_length > num_bytes) |
| 1999 | map_length = num_bytes; |
| 2000 | |
| 2001 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 2002 | blkdev_issue_discard(stripe->dev->bdev, |
| 2003 | stripe->physical >> 9, |
| 2004 | map_length >> 9); |
| 2005 | } |
| 2006 | kfree(multi); |
| 2007 | } |
| 2008 | #endif |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2009 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2010 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2011 | finish_current_insert(trans, extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2012 | return ret; |
| 2013 | } |
| 2014 | |
| 2015 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2016 | * find all the blocks marked as pending in the radix tree and remove |
| 2017 | * them from the extent map |
| 2018 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2019 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 2020 | btrfs_root *extent_root) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2021 | { |
| 2022 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2023 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2024 | int mark_free = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2025 | u64 start; |
| 2026 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2027 | u64 priv; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2028 | struct extent_io_tree *pending_del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2029 | struct extent_io_tree *extent_ins; |
| 2030 | struct pending_extent_op *extent_op; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2031 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 2032 | WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex)); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2033 | extent_ins = &extent_root->fs_info->extent_ins; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2034 | pending_del = &extent_root->fs_info->pending_del; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2035 | |
| 2036 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2037 | ret = find_first_extent_bit(pending_del, 0, &start, &end, |
| 2038 | EXTENT_LOCKED); |
| 2039 | if (ret) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2040 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2041 | |
| 2042 | ret = get_state_private(pending_del, start, &priv); |
| 2043 | BUG_ON(ret); |
| 2044 | extent_op = (struct pending_extent_op *)(unsigned long)priv; |
| 2045 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2046 | clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, |
| 2047 | GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2048 | |
| 2049 | ret = pin_down_bytes(trans, extent_root, start, |
| 2050 | end + 1 - start, 0); |
| 2051 | mark_free = ret > 0; |
| 2052 | if (!test_range_bit(extent_ins, start, end, |
| 2053 | EXTENT_LOCKED, 0)) { |
| 2054 | free_extent: |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2055 | ret = __free_extent(trans, extent_root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2056 | start, end + 1 - start, |
| 2057 | extent_op->orig_parent, |
| 2058 | extent_root->root_key.objectid, |
| 2059 | extent_op->orig_generation, |
| 2060 | extent_op->level, 0, 0, mark_free); |
| 2061 | kfree(extent_op); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2062 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2063 | kfree(extent_op); |
| 2064 | ret = get_state_private(extent_ins, start, &priv); |
| 2065 | BUG_ON(ret); |
| 2066 | extent_op = (struct pending_extent_op *) |
| 2067 | (unsigned long)priv; |
| 2068 | |
| 2069 | clear_extent_bits(extent_ins, start, end, |
| 2070 | EXTENT_LOCKED, GFP_NOFS); |
| 2071 | |
| 2072 | if (extent_op->type == PENDING_BACKREF_UPDATE) |
| 2073 | goto free_extent; |
| 2074 | |
| 2075 | ret = update_block_group(trans, extent_root, start, |
| 2076 | end + 1 - start, 0, mark_free); |
| 2077 | BUG_ON(ret); |
| 2078 | kfree(extent_op); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2079 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2080 | if (ret) |
| 2081 | err = ret; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2082 | |
| 2083 | if (need_resched()) { |
| 2084 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 2085 | cond_resched(); |
| 2086 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
| 2087 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2088 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2089 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | /* |
| 2093 | * remove an extent from the root, returns 0 on success |
| 2094 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2095 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2096 | struct btrfs_root *root, |
| 2097 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2098 | u64 root_objectid, u64 ref_generation, |
| 2099 | u64 owner_objectid, u64 owner_offset, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2100 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2101 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2102 | int pending_ret; |
| 2103 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2104 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2105 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2106 | if (root == extent_root) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2107 | struct pending_extent_op *extent_op; |
| 2108 | |
| 2109 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2110 | BUG_ON(!extent_op); |
| 2111 | |
| 2112 | extent_op->type = PENDING_EXTENT_DELETE; |
| 2113 | extent_op->bytenr = bytenr; |
| 2114 | extent_op->num_bytes = num_bytes; |
| 2115 | extent_op->parent = parent; |
| 2116 | extent_op->orig_parent = parent; |
| 2117 | extent_op->generation = ref_generation; |
| 2118 | extent_op->orig_generation = ref_generation; |
| 2119 | extent_op->level = (int)owner_objectid; |
| 2120 | |
| 2121 | set_extent_bits(&root->fs_info->pending_del, |
| 2122 | bytenr, bytenr + num_bytes - 1, |
| 2123 | EXTENT_LOCKED, GFP_NOFS); |
| 2124 | set_state_private(&root->fs_info->pending_del, |
| 2125 | bytenr, (unsigned long)extent_op); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2126 | return 0; |
| 2127 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2128 | /* if metadata always pin */ |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2129 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2130 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2131 | struct btrfs_block_group_cache *cache; |
| 2132 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2133 | /* btrfs_free_reserved_extent */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2134 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2135 | BUG_ON(!cache); |
| 2136 | btrfs_add_free_space(cache, bytenr, num_bytes); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2137 | update_reserved_extents(root, bytenr, num_bytes, 0); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2138 | return 0; |
| 2139 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2140 | pin = 1; |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2141 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2142 | |
| 2143 | /* if data pin when any transaction has committed this */ |
| 2144 | if (ref_generation != trans->transid) |
| 2145 | pin = 1; |
| 2146 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2147 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, |
| 2148 | root_objectid, ref_generation, owner_objectid, |
| 2149 | owner_offset, pin, pin == 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 2150 | |
| 2151 | finish_current_insert(trans, root->fs_info->extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2152 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2153 | return ret ? ret : pending_ret; |
| 2154 | } |
| 2155 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2156 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2157 | struct btrfs_root *root, |
| 2158 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2159 | u64 root_objectid, u64 ref_generation, |
| 2160 | u64 owner_objectid, u64 owner_offset, int pin) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2161 | { |
| 2162 | int ret; |
| 2163 | |
| 2164 | maybe_lock_mutex(root); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2165 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2166 | root_objectid, ref_generation, |
| 2167 | owner_objectid, owner_offset, pin); |
| 2168 | maybe_unlock_mutex(root); |
| 2169 | return ret; |
| 2170 | } |
| 2171 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2172 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 2173 | { |
| 2174 | u64 mask = ((u64)root->stripesize - 1); |
| 2175 | u64 ret = (val + mask) & ~mask; |
| 2176 | return ret; |
| 2177 | } |
| 2178 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2179 | /* |
| 2180 | * walks the btree of allocated extents and find a hole of a given size. |
| 2181 | * The key ins is changed to record the hole: |
| 2182 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2183 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2184 | * ins->offset == number of blocks |
| 2185 | * Any available blocks before search_start are skipped. |
| 2186 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2187 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
| 2188 | struct btrfs_root *orig_root, |
| 2189 | u64 num_bytes, u64 empty_size, |
| 2190 | u64 search_start, u64 search_end, |
| 2191 | u64 hint_byte, struct btrfs_key *ins, |
| 2192 | u64 exclude_start, u64 exclude_nr, |
| 2193 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2194 | { |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2195 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2196 | u64 orig_search_start; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2197 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 2198 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2199 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2200 | u64 *last_ptr = NULL; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 2201 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2202 | int chunk_alloc_done = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2203 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2204 | int allowed_chunk_alloc = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2205 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2206 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2207 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
| 2208 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2209 | if (orig_root->ref_cows || empty_size) |
| 2210 | allowed_chunk_alloc = 1; |
| 2211 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2212 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 2213 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2214 | empty_cluster = 256 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2215 | } |
| 2216 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2217 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2218 | last_ptr = &root->fs_info->last_data_alloc; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2219 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2220 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 2221 | last_ptr = &root->fs_info->last_log_alloc; |
| 2222 | if (!last_ptr == 0 && root->fs_info->last_alloc) { |
| 2223 | *last_ptr = root->fs_info->last_alloc + empty_cluster; |
| 2224 | } |
| 2225 | } |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2226 | |
| 2227 | if (last_ptr) { |
| 2228 | if (*last_ptr) |
| 2229 | hint_byte = *last_ptr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2230 | else |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2231 | empty_size += empty_cluster; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2232 | } |
| 2233 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2234 | search_start = max(search_start, first_logical_byte(root, 0)); |
| 2235 | orig_search_start = search_start; |
| 2236 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2237 | search_start = max(search_start, hint_byte); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2238 | total_needed += empty_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2239 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2240 | new_group: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2241 | block_group = btrfs_lookup_first_block_group(info, search_start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2242 | |
| 2243 | /* |
| 2244 | * Ok this looks a little tricky, buts its really simple. First if we |
| 2245 | * didn't find a block group obviously we want to start over. |
| 2246 | * Secondly, if the block group we found does not match the type we |
| 2247 | * need, and we have a last_ptr and its not 0, chances are the last |
| 2248 | * allocation we made was at the end of the block group, so lets go |
| 2249 | * ahead and skip the looking through the rest of the block groups and |
| 2250 | * start at the beginning. This helps with metadata allocations, |
| 2251 | * since you are likely to have a bunch of data block groups to search |
| 2252 | * through first before you realize that you need to start over, so go |
| 2253 | * ahead and start over and save the time. |
| 2254 | */ |
| 2255 | if (!block_group || (!block_group_bits(block_group, data) && |
| 2256 | last_ptr && *last_ptr)) { |
| 2257 | if (search_start != orig_search_start) { |
| 2258 | if (last_ptr && *last_ptr) |
| 2259 | *last_ptr = 0; |
| 2260 | search_start = orig_search_start; |
| 2261 | goto new_group; |
| 2262 | } else if (!chunk_alloc_done && allowed_chunk_alloc) { |
| 2263 | ret = do_chunk_alloc(trans, root, |
| 2264 | num_bytes + 2 * 1024 * 1024, |
| 2265 | data, 1); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2266 | if (ret < 0) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2267 | goto error; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2268 | BUG_ON(ret); |
| 2269 | chunk_alloc_done = 1; |
| 2270 | search_start = orig_search_start; |
| 2271 | goto new_group; |
| 2272 | } else { |
| 2273 | ret = -ENOSPC; |
| 2274 | goto error; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2275 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | /* |
| 2279 | * this is going to seach through all of the existing block groups it |
| 2280 | * can find, so if we don't find something we need to see if we can |
| 2281 | * allocate what we need. |
| 2282 | */ |
| 2283 | ret = find_free_space(root, &block_group, &search_start, |
| 2284 | total_needed, data); |
| 2285 | if (ret == -ENOSPC) { |
| 2286 | /* |
| 2287 | * instead of allocating, start at the original search start |
| 2288 | * and see if there is something to be found, if not then we |
| 2289 | * allocate |
| 2290 | */ |
| 2291 | if (search_start != orig_search_start) { |
| 2292 | if (last_ptr && *last_ptr) { |
| 2293 | *last_ptr = 0; |
| 2294 | total_needed += empty_cluster; |
| 2295 | } |
| 2296 | search_start = orig_search_start; |
| 2297 | goto new_group; |
| 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | * we've already allocated, we're pretty screwed |
| 2302 | */ |
| 2303 | if (chunk_alloc_done) { |
| 2304 | goto error; |
| 2305 | } else if (!allowed_chunk_alloc && block_group && |
| 2306 | block_group_bits(block_group, data)) { |
| 2307 | block_group->space_info->force_alloc = 1; |
| 2308 | goto error; |
| 2309 | } else if (!allowed_chunk_alloc) { |
| 2310 | goto error; |
| 2311 | } |
| 2312 | |
| 2313 | ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024, |
| 2314 | data, 1); |
| 2315 | if (ret < 0) |
| 2316 | goto error; |
| 2317 | |
| 2318 | BUG_ON(ret); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2319 | chunk_alloc_done = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2320 | if (block_group) |
| 2321 | search_start = block_group->key.objectid + |
| 2322 | block_group->key.offset; |
| 2323 | else |
| 2324 | search_start = orig_search_start; |
| 2325 | goto new_group; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2326 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2327 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2328 | if (ret) |
| 2329 | goto error; |
| 2330 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2331 | search_start = stripe_align(root, search_start); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2332 | ins->objectid = search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2333 | ins->offset = num_bytes; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2334 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2335 | if (ins->objectid + num_bytes >= search_end) { |
| 2336 | search_start = orig_search_start; |
| 2337 | if (chunk_alloc_done) { |
| 2338 | ret = -ENOSPC; |
| 2339 | goto error; |
| 2340 | } |
| 2341 | goto new_group; |
| 2342 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2343 | |
| 2344 | if (ins->objectid + num_bytes > |
| 2345 | block_group->key.objectid + block_group->key.offset) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2346 | if (search_start == orig_search_start && chunk_alloc_done) { |
| 2347 | ret = -ENOSPC; |
| 2348 | goto error; |
| 2349 | } |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 2350 | search_start = block_group->key.objectid + |
| 2351 | block_group->key.offset; |
| 2352 | goto new_group; |
| 2353 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2354 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2355 | if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2356 | ins->objectid < exclude_start + exclude_nr)) { |
| 2357 | search_start = exclude_start + exclude_nr; |
| 2358 | goto new_group; |
| 2359 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2360 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2361 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
| 2362 | trans->block_group = block_group; |
| 2363 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2364 | ins->offset = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2365 | if (last_ptr) { |
| 2366 | *last_ptr = ins->objectid + ins->offset; |
| 2367 | if (*last_ptr == |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2368 | btrfs_super_total_bytes(&root->fs_info->super_copy)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2369 | *last_ptr = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2370 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2371 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2372 | ret = 0; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2373 | error: |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2374 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2375 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2376 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2377 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 2378 | { |
| 2379 | struct btrfs_block_group_cache *cache; |
| 2380 | struct list_head *l; |
| 2381 | |
| 2382 | printk(KERN_INFO "space_info has %Lu free, is %sfull\n", |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2383 | info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 2384 | info->bytes_reserved, (info->full) ? "" : "not "); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2385 | |
| 2386 | spin_lock(&info->lock); |
| 2387 | list_for_each(l, &info->block_groups) { |
| 2388 | cache = list_entry(l, struct btrfs_block_group_cache, list); |
| 2389 | spin_lock(&cache->lock); |
| 2390 | printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used " |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2391 | "%Lu pinned %Lu reserved\n", |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2392 | cache->key.objectid, cache->key.offset, |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2393 | btrfs_block_group_used(&cache->item), |
| 2394 | cache->pinned, cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2395 | btrfs_dump_free_space(cache, bytes); |
| 2396 | spin_unlock(&cache->lock); |
| 2397 | } |
| 2398 | spin_unlock(&info->lock); |
| 2399 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2400 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2401 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2402 | struct btrfs_root *root, |
| 2403 | u64 num_bytes, u64 min_alloc_size, |
| 2404 | u64 empty_size, u64 hint_byte, |
| 2405 | u64 search_end, struct btrfs_key *ins, |
| 2406 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2407 | { |
| 2408 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 2409 | u64 search_start = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2410 | u64 alloc_profile; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2411 | struct btrfs_fs_info *info = root->fs_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2412 | struct btrfs_block_group_cache *cache; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2413 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2414 | if (data) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2415 | alloc_profile = info->avail_data_alloc_bits & |
| 2416 | info->data_alloc_profile; |
| 2417 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2418 | } else if (root == root->fs_info->chunk_root) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2419 | alloc_profile = info->avail_system_alloc_bits & |
| 2420 | info->system_alloc_profile; |
| 2421 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2422 | } else { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2423 | alloc_profile = info->avail_metadata_alloc_bits & |
| 2424 | info->metadata_alloc_profile; |
| 2425 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2426 | } |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2427 | again: |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2428 | data = reduce_alloc_profile(root, data); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2429 | /* |
| 2430 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 2431 | * is not called recursively on allocations |
| 2432 | */ |
| 2433 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2434 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2435 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2436 | 2 * 1024 * 1024, |
| 2437 | BTRFS_BLOCK_GROUP_METADATA | |
| 2438 | (info->metadata_alloc_profile & |
| 2439 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2440 | } |
| 2441 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2442 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2443 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2444 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2445 | WARN_ON(num_bytes < root->sectorsize); |
| 2446 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 2447 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2448 | trans->alloc_exclude_start, |
| 2449 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2450 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2451 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 2452 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2453 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2454 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2455 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2456 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2457 | goto again; |
| 2458 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2459 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2460 | struct btrfs_space_info *sinfo; |
| 2461 | |
| 2462 | sinfo = __find_space_info(root->fs_info, data); |
| 2463 | printk("allocation failed flags %Lu, wanted %Lu\n", |
| 2464 | data, num_bytes); |
| 2465 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2466 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2467 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2468 | cache = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
| 2469 | if (!cache) { |
| 2470 | printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid); |
| 2471 | return -ENOSPC; |
| 2472 | } |
| 2473 | |
| 2474 | ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset); |
| 2475 | |
| 2476 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2477 | } |
| 2478 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2479 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 2480 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2481 | struct btrfs_block_group_cache *cache; |
| 2482 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2483 | maybe_lock_mutex(root); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2484 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2485 | if (!cache) { |
| 2486 | printk(KERN_ERR "Unable to find block group for %Lu\n", start); |
| 2487 | maybe_unlock_mutex(root); |
| 2488 | return -ENOSPC; |
| 2489 | } |
| 2490 | btrfs_add_free_space(cache, start, len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 2491 | update_reserved_extents(root, start, len, 0); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2492 | maybe_unlock_mutex(root); |
| 2493 | return 0; |
| 2494 | } |
| 2495 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2496 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2497 | struct btrfs_root *root, |
| 2498 | u64 num_bytes, u64 min_alloc_size, |
| 2499 | u64 empty_size, u64 hint_byte, |
| 2500 | u64 search_end, struct btrfs_key *ins, |
| 2501 | u64 data) |
| 2502 | { |
| 2503 | int ret; |
| 2504 | maybe_lock_mutex(root); |
| 2505 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 2506 | empty_size, hint_byte, search_end, ins, |
| 2507 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2508 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2509 | maybe_unlock_mutex(root); |
| 2510 | return ret; |
| 2511 | } |
| 2512 | |
| 2513 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2514 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2515 | u64 root_objectid, u64 ref_generation, |
| 2516 | u64 owner, u64 owner_offset, |
| 2517 | struct btrfs_key *ins) |
| 2518 | { |
| 2519 | int ret; |
| 2520 | int pending_ret; |
| 2521 | u64 super_used; |
| 2522 | u64 root_used; |
| 2523 | u64 num_bytes = ins->offset; |
| 2524 | u32 sizes[2]; |
| 2525 | struct btrfs_fs_info *info = root->fs_info; |
| 2526 | struct btrfs_root *extent_root = info->extent_root; |
| 2527 | struct btrfs_extent_item *extent_item; |
| 2528 | struct btrfs_extent_ref *ref; |
| 2529 | struct btrfs_path *path; |
| 2530 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2531 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2532 | if (parent == 0) |
| 2533 | parent = ins->objectid; |
| 2534 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2535 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2536 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2537 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2538 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2539 | spin_unlock_irq(&info->delalloc_lock); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2540 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2541 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2542 | root_used = btrfs_root_used(&root->root_item); |
| 2543 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2544 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2545 | if (root == extent_root) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2546 | struct pending_extent_op *extent_op; |
| 2547 | |
| 2548 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2549 | BUG_ON(!extent_op); |
| 2550 | |
| 2551 | extent_op->type = PENDING_EXTENT_INSERT; |
| 2552 | extent_op->bytenr = ins->objectid; |
| 2553 | extent_op->num_bytes = ins->offset; |
| 2554 | extent_op->parent = parent; |
| 2555 | extent_op->orig_parent = 0; |
| 2556 | extent_op->generation = ref_generation; |
| 2557 | extent_op->orig_generation = 0; |
| 2558 | extent_op->level = (int)owner; |
| 2559 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2560 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 2561 | ins->objectid + ins->offset - 1, |
| 2562 | EXTENT_LOCKED, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2563 | set_state_private(&root->fs_info->extent_ins, |
| 2564 | ins->objectid, (unsigned long)extent_op); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2565 | goto update_block; |
| 2566 | } |
| 2567 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2568 | memcpy(&keys[0], ins, sizeof(*ins)); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2569 | keys[1].objectid = ins->objectid; |
| 2570 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2571 | keys[1].offset = parent; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2572 | sizes[0] = sizeof(*extent_item); |
| 2573 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2574 | |
| 2575 | path = btrfs_alloc_path(); |
| 2576 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2577 | |
| 2578 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 2579 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2580 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2581 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2582 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2583 | struct btrfs_extent_item); |
| 2584 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); |
| 2585 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 2586 | struct btrfs_extent_ref); |
| 2587 | |
| 2588 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 2589 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 2590 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
| 2591 | btrfs_set_ref_offset(path->nodes[0], ref, owner_offset); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2592 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2593 | |
| 2594 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2595 | |
| 2596 | trans->alloc_exclude_start = 0; |
| 2597 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2598 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2599 | finish_current_insert(trans, extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2600 | pending_ret = del_pending_extents(trans, extent_root); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2601 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2602 | if (ret) |
| 2603 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2604 | if (pending_ret) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2605 | ret = pending_ret; |
| 2606 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2607 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2608 | |
| 2609 | update_block: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2610 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 2611 | if (ret) { |
| 2612 | printk("update block group failed for %Lu %Lu\n", |
| 2613 | ins->objectid, ins->offset); |
| 2614 | BUG(); |
| 2615 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2616 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2617 | return ret; |
| 2618 | } |
| 2619 | |
| 2620 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2621 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2622 | u64 root_objectid, u64 ref_generation, |
| 2623 | u64 owner, u64 owner_offset, |
| 2624 | struct btrfs_key *ins) |
| 2625 | { |
| 2626 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 2627 | |
| 2628 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 2629 | return 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2630 | maybe_lock_mutex(root); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2631 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
| 2632 | root_objectid, ref_generation, |
| 2633 | owner, owner_offset, ins); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2634 | update_reserved_extents(root, ins->objectid, ins->offset, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2635 | maybe_unlock_mutex(root); |
| 2636 | return ret; |
| 2637 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2638 | |
| 2639 | /* |
| 2640 | * this is used by the tree logging recovery code. It records that |
| 2641 | * an extent has been allocated and makes sure to clear the free |
| 2642 | * space cache bits as well |
| 2643 | */ |
| 2644 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2645 | struct btrfs_root *root, u64 parent, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2646 | u64 root_objectid, u64 ref_generation, |
| 2647 | u64 owner, u64 owner_offset, |
| 2648 | struct btrfs_key *ins) |
| 2649 | { |
| 2650 | int ret; |
| 2651 | struct btrfs_block_group_cache *block_group; |
| 2652 | |
| 2653 | maybe_lock_mutex(root); |
| 2654 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
| 2655 | cache_block_group(root, block_group); |
| 2656 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2657 | ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset); |
| 2658 | BUG_ON(ret); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2659 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
| 2660 | root_objectid, ref_generation, |
| 2661 | owner, owner_offset, ins); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2662 | maybe_unlock_mutex(root); |
| 2663 | return ret; |
| 2664 | } |
| 2665 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2666 | /* |
| 2667 | * finds a free extent and does all the dirty work required for allocation |
| 2668 | * returns the key for the extent through ins, and a tree buffer for |
| 2669 | * the first block of the extent through buf. |
| 2670 | * |
| 2671 | * returns 0 if everything worked, non-zero otherwise. |
| 2672 | */ |
| 2673 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 2674 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2675 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2676 | u64 root_objectid, u64 ref_generation, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2677 | u64 owner_objectid, u64 owner_offset, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2678 | u64 empty_size, u64 hint_byte, |
| 2679 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 2680 | { |
| 2681 | int ret; |
| 2682 | |
| 2683 | maybe_lock_mutex(root); |
| 2684 | |
| 2685 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 2686 | min_alloc_size, empty_size, hint_byte, |
| 2687 | search_end, ins, data); |
| 2688 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2689 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2690 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
| 2691 | root_objectid, ref_generation, |
| 2692 | owner_objectid, owner_offset, ins); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2693 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2694 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2695 | } else { |
| 2696 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2697 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2698 | maybe_unlock_mutex(root); |
| 2699 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2700 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2701 | |
| 2702 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 2703 | struct btrfs_root *root, |
| 2704 | u64 bytenr, u32 blocksize) |
| 2705 | { |
| 2706 | struct extent_buffer *buf; |
| 2707 | |
| 2708 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 2709 | if (!buf) |
| 2710 | return ERR_PTR(-ENOMEM); |
| 2711 | btrfs_set_header_generation(buf, trans->transid); |
| 2712 | btrfs_tree_lock(buf); |
| 2713 | clean_tree_block(trans, root, buf); |
| 2714 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2715 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 2716 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2717 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2718 | } else { |
| 2719 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 2720 | buf->start + buf->len - 1, GFP_NOFS); |
| 2721 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2722 | trans->blocks_used++; |
| 2723 | return buf; |
| 2724 | } |
| 2725 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2726 | /* |
| 2727 | * helper function to allocate a block for a given tree |
| 2728 | * returns the tree buffer or NULL. |
| 2729 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2730 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2731 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2732 | u32 blocksize, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2733 | u64 root_objectid, |
| 2734 | u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2735 | int level, |
| 2736 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2737 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2738 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2739 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2740 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2741 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2742 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2743 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
| 2744 | root_objectid, ref_generation, level, 0, |
| 2745 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2746 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2747 | BUG_ON(ret > 0); |
| 2748 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2749 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 2750 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2751 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2752 | return buf; |
| 2753 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2754 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2755 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 2756 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2757 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2758 | u64 leaf_owner; |
| 2759 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2760 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2761 | struct btrfs_file_extent_item *fi; |
| 2762 | int i; |
| 2763 | int nritems; |
| 2764 | int ret; |
| 2765 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2766 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 2767 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2768 | leaf_owner = btrfs_header_owner(leaf); |
| 2769 | leaf_generation = btrfs_header_generation(leaf); |
| 2770 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2771 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2772 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 2773 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2774 | |
| 2775 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 2776 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2777 | continue; |
| 2778 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2779 | if (btrfs_file_extent_type(leaf, fi) == |
| 2780 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 2781 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2782 | /* |
| 2783 | * FIXME make sure to insert a trans record that |
| 2784 | * repeats the snapshot del on crash |
| 2785 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2786 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 2787 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 2788 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2789 | |
| 2790 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2791 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2792 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2793 | leaf->start, leaf_owner, leaf_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2794 | key.objectid, key.offset, 0); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2795 | mutex_unlock(&root->fs_info->alloc_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2796 | BUG_ON(ret); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2797 | |
| 2798 | atomic_inc(&root->fs_info->throttle_gen); |
| 2799 | wake_up(&root->fs_info->transaction_throttle); |
| 2800 | cond_resched(); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2801 | } |
| 2802 | return 0; |
| 2803 | } |
| 2804 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2805 | static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 2806 | struct btrfs_root *root, |
| 2807 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2808 | { |
| 2809 | int i; |
| 2810 | int ret; |
| 2811 | struct btrfs_extent_info *info = ref->extents; |
| 2812 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2813 | for (i = 0; i < ref->nritems; i++) { |
| 2814 | mutex_lock(&root->fs_info->alloc_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2815 | ret = __btrfs_free_extent(trans, root, info->bytenr, |
| 2816 | info->num_bytes, ref->bytenr, |
| 2817 | ref->owner, ref->generation, |
| 2818 | info->objectid, info->offset, 0); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2819 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2820 | |
| 2821 | atomic_inc(&root->fs_info->throttle_gen); |
| 2822 | wake_up(&root->fs_info->transaction_throttle); |
| 2823 | cond_resched(); |
| 2824 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2825 | BUG_ON(ret); |
| 2826 | info++; |
| 2827 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2828 | |
| 2829 | return 0; |
| 2830 | } |
| 2831 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2832 | int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len, |
| 2833 | u32 *refs) |
| 2834 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2835 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2836 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2837 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2838 | BUG_ON(ret); |
| 2839 | |
| 2840 | #if 0 // some debugging code in case we see problems here |
| 2841 | /* if the refs count is one, it won't get increased again. But |
| 2842 | * if the ref count is > 1, someone may be decreasing it at |
| 2843 | * the same time we are. |
| 2844 | */ |
| 2845 | if (*refs != 1) { |
| 2846 | struct extent_buffer *eb = NULL; |
| 2847 | eb = btrfs_find_create_tree_block(root, start, len); |
| 2848 | if (eb) |
| 2849 | btrfs_tree_lock(eb); |
| 2850 | |
| 2851 | mutex_lock(&root->fs_info->alloc_mutex); |
| 2852 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 2853 | BUG_ON(ret); |
| 2854 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 2855 | |
| 2856 | if (eb) { |
| 2857 | btrfs_tree_unlock(eb); |
| 2858 | free_extent_buffer(eb); |
| 2859 | } |
| 2860 | if (*refs == 1) { |
| 2861 | printk("block %llu went down to one during drop_snap\n", |
| 2862 | (unsigned long long)start); |
| 2863 | } |
| 2864 | |
| 2865 | } |
| 2866 | #endif |
| 2867 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2868 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2869 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2873 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 2874 | * counts as it goes. |
| 2875 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2876 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
| 2877 | struct btrfs_root *root, |
| 2878 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2879 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2880 | u64 root_owner; |
| 2881 | u64 root_gen; |
| 2882 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2883 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2884 | struct extent_buffer *next; |
| 2885 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2886 | struct extent_buffer *parent; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2887 | struct btrfs_leaf_ref *ref; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2888 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2889 | int ret; |
| 2890 | u32 refs; |
| 2891 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2892 | WARN_ON(*level < 0); |
| 2893 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2894 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2895 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2896 | BUG_ON(ret); |
| 2897 | if (refs > 1) |
| 2898 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2899 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2900 | /* |
| 2901 | * walk down to the last node level and free all the leaves |
| 2902 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2903 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2904 | WARN_ON(*level < 0); |
| 2905 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2906 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2907 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2908 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2909 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2910 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2911 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2912 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2913 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2914 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2915 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2916 | BUG_ON(ret); |
| 2917 | break; |
| 2918 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2919 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2920 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2921 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2922 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2923 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2924 | BUG_ON(ret); |
| 2925 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2926 | parent = path->nodes[*level]; |
| 2927 | root_owner = btrfs_header_owner(parent); |
| 2928 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2929 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2930 | |
| 2931 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2932 | ret = __btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2933 | blocksize, parent->start, |
| 2934 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2935 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2936 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 2937 | |
| 2938 | atomic_inc(&root->fs_info->throttle_gen); |
| 2939 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2940 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 2941 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2942 | continue; |
| 2943 | } |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2944 | /* |
| 2945 | * at this point, we have a single ref, and since the |
| 2946 | * only place referencing this extent is a dead root |
| 2947 | * the reference count should never go higher. |
| 2948 | * So, we don't need to check it again |
| 2949 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2950 | if (*level == 1) { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2951 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 2952 | if (ref && ref->generation != ptr_gen) { |
| 2953 | btrfs_free_leaf_ref(root, ref); |
| 2954 | ref = NULL; |
| 2955 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2956 | if (ref) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2957 | ret = cache_drop_leaf_ref(trans, root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2958 | BUG_ON(ret); |
| 2959 | btrfs_remove_leaf_ref(root, ref); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2960 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2961 | *level = 0; |
| 2962 | break; |
| 2963 | } |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 2964 | if (printk_ratelimit()) |
| 2965 | printk("leaf ref miss for bytenr %llu\n", |
| 2966 | (unsigned long long)bytenr); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2967 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2968 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2969 | if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2970 | free_extent_buffer(next); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2971 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2972 | next = read_tree_block(root, bytenr, blocksize, |
| 2973 | ptr_gen); |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2974 | cond_resched(); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2975 | #if 0 |
| 2976 | /* |
| 2977 | * this is a debugging check and can go away |
| 2978 | * the ref should never go all the way down to 1 |
| 2979 | * at this point |
| 2980 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2981 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, |
| 2982 | &refs); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2983 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2984 | WARN_ON(refs != 1); |
| 2985 | #endif |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2986 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2987 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2988 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2989 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2990 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2991 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2992 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2993 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2994 | } |
| 2995 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2996 | WARN_ON(*level < 0); |
| 2997 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2998 | |
| 2999 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3000 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3001 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3002 | } else { |
| 3003 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3004 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3005 | } |
| 3006 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3007 | blocksize = btrfs_level_size(root, *level); |
| 3008 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3009 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3010 | |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3011 | mutex_lock(&root->fs_info->alloc_mutex); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3012 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3013 | parent->start, root_owner, root_gen, |
| 3014 | 0, 0, 1); |
| 3015 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3016 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3017 | path->nodes[*level] = NULL; |
| 3018 | *level += 1; |
| 3019 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3020 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3021 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3022 | return 0; |
| 3023 | } |
| 3024 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3025 | /* |
| 3026 | * helper for dropping snapshots. This walks back up the tree in the path |
| 3027 | * to find the first node higher up where we haven't yet gone through |
| 3028 | * all the slots |
| 3029 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3030 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
| 3031 | struct btrfs_root *root, |
| 3032 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3033 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3034 | u64 root_owner; |
| 3035 | u64 root_gen; |
| 3036 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3037 | int i; |
| 3038 | int slot; |
| 3039 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3040 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 3041 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3042 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3043 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 3044 | struct extent_buffer *node; |
| 3045 | struct btrfs_disk_key disk_key; |
| 3046 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3047 | path->slots[i]++; |
| 3048 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3049 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3050 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3051 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3052 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3053 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3054 | return 0; |
| 3055 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3056 | struct extent_buffer *parent; |
| 3057 | if (path->nodes[*level] == root->node) |
| 3058 | parent = path->nodes[*level]; |
| 3059 | else |
| 3060 | parent = path->nodes[*level + 1]; |
| 3061 | |
| 3062 | root_owner = btrfs_header_owner(parent); |
| 3063 | root_gen = btrfs_header_generation(parent); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3064 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3065 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3066 | path->nodes[*level]->len, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3067 | parent->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3068 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3069 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3070 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3071 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3072 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3073 | } |
| 3074 | } |
| 3075 | return 1; |
| 3076 | } |
| 3077 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3078 | /* |
| 3079 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 3080 | * the tree freeing any blocks that have a ref count of zero after being |
| 3081 | * decremented. |
| 3082 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3083 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3084 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3085 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 3086 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3087 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3088 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3089 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3090 | int i; |
| 3091 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3092 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3093 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3094 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3095 | path = btrfs_alloc_path(); |
| 3096 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3097 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3098 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3099 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3100 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 3101 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3102 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3103 | path->slots[level] = 0; |
| 3104 | } else { |
| 3105 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3106 | struct btrfs_disk_key found_key; |
| 3107 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3108 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3109 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3110 | level = root_item->drop_level; |
| 3111 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3112 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3113 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3114 | ret = wret; |
| 3115 | goto out; |
| 3116 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3117 | node = path->nodes[level]; |
| 3118 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 3119 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 3120 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3121 | /* |
| 3122 | * unlock our path, this is safe because only this |
| 3123 | * function is allowed to delete this snapshot |
| 3124 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3125 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 3126 | if (path->nodes[i] && path->locks[i]) { |
| 3127 | path->locks[i] = 0; |
| 3128 | btrfs_tree_unlock(path->nodes[i]); |
| 3129 | } |
| 3130 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3131 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3132 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3133 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3134 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3135 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3136 | if (wret < 0) |
| 3137 | ret = wret; |
| 3138 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3139 | wret = walk_up_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3140 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3141 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3142 | if (wret < 0) |
| 3143 | ret = wret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3144 | if (trans->transaction->in_commit) { |
| 3145 | ret = -EAGAIN; |
| 3146 | break; |
| 3147 | } |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3148 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3149 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3150 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3151 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3152 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3153 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 3154 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3155 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3156 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3157 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3158 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3159 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3160 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3161 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3162 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 3163 | unsigned long nr) |
| 3164 | { |
| 3165 | return min(last, start + nr - 1); |
| 3166 | } |
| 3167 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3168 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
| 3169 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3170 | { |
| 3171 | u64 page_start; |
| 3172 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3173 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3174 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3175 | unsigned long i; |
| 3176 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3177 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3178 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3179 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3180 | unsigned int total_read = 0; |
| 3181 | unsigned int total_dirty = 0; |
| 3182 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3183 | |
| 3184 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3185 | |
| 3186 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3187 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3188 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 3189 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3190 | /* make sure the dirty trick played by the caller work */ |
| 3191 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 3192 | first_index, last_index); |
| 3193 | if (ret) |
| 3194 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3195 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3196 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3197 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3198 | for (i = first_index ; i <= last_index; i++) { |
| 3199 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3200 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3201 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3202 | } |
| 3203 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3204 | again: |
| 3205 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3206 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3207 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3208 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3209 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3210 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3211 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3212 | if (!PageUptodate(page)) { |
| 3213 | btrfs_readpage(NULL, page); |
| 3214 | lock_page(page); |
| 3215 | if (!PageUptodate(page)) { |
| 3216 | unlock_page(page); |
| 3217 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3218 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3219 | goto out_unlock; |
| 3220 | } |
| 3221 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3222 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3223 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3224 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 3225 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3226 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3227 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3228 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3229 | if (ordered) { |
| 3230 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3231 | unlock_page(page); |
| 3232 | page_cache_release(page); |
| 3233 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3234 | btrfs_put_ordered_extent(ordered); |
| 3235 | goto again; |
| 3236 | } |
| 3237 | set_page_extent_mapped(page); |
| 3238 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3239 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3240 | if (i == first_index) |
| 3241 | set_extent_bits(io_tree, page_start, page_end, |
| 3242 | EXTENT_BOUNDARY, GFP_NOFS); |
| 3243 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3244 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3245 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3246 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3247 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3248 | unlock_page(page); |
| 3249 | page_cache_release(page); |
| 3250 | } |
| 3251 | |
| 3252 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3253 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3254 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3255 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3256 | return ret; |
| 3257 | } |
| 3258 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3259 | static int noinline relocate_data_extent(struct inode *reloc_inode, |
| 3260 | struct btrfs_key *extent_key, |
| 3261 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3262 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3263 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 3264 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 3265 | struct extent_map *em; |
| 3266 | |
| 3267 | em = alloc_extent_map(GFP_NOFS); |
| 3268 | BUG_ON(!em || IS_ERR(em)); |
| 3269 | |
| 3270 | em->start = extent_key->objectid - offset; |
| 3271 | em->len = extent_key->offset; |
| 3272 | em->block_start = extent_key->objectid; |
| 3273 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 3274 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 3275 | |
| 3276 | /* setup extent map to cheat btrfs_readpage */ |
| 3277 | mutex_lock(&BTRFS_I(reloc_inode)->extent_mutex); |
| 3278 | while (1) { |
| 3279 | int ret; |
| 3280 | spin_lock(&em_tree->lock); |
| 3281 | ret = add_extent_mapping(em_tree, em); |
| 3282 | spin_unlock(&em_tree->lock); |
| 3283 | if (ret != -EEXIST) { |
| 3284 | free_extent_map(em); |
| 3285 | break; |
| 3286 | } |
| 3287 | btrfs_drop_extent_cache(reloc_inode, em->start, |
| 3288 | em->start + em->len - 1, 0); |
| 3289 | } |
| 3290 | mutex_unlock(&BTRFS_I(reloc_inode)->extent_mutex); |
| 3291 | |
| 3292 | return relocate_inode_pages(reloc_inode, extent_key->objectid - offset, |
| 3293 | extent_key->offset); |
| 3294 | } |
| 3295 | |
| 3296 | struct btrfs_ref_path { |
| 3297 | u64 extent_start; |
| 3298 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 3299 | u64 root_objectid; |
| 3300 | u64 root_generation; |
| 3301 | u64 owner_objectid; |
| 3302 | u64 owner_offset; |
| 3303 | u32 num_refs; |
| 3304 | int lowest_level; |
| 3305 | int current_level; |
| 3306 | }; |
| 3307 | |
| 3308 | struct disk_extent { |
| 3309 | u64 disk_bytenr; |
| 3310 | u64 disk_num_bytes; |
| 3311 | u64 offset; |
| 3312 | u64 num_bytes; |
| 3313 | }; |
| 3314 | |
| 3315 | static int is_cowonly_root(u64 root_objectid) |
| 3316 | { |
| 3317 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 3318 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 3319 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 3320 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
| 3321 | root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 3322 | return 1; |
| 3323 | return 0; |
| 3324 | } |
| 3325 | |
| 3326 | static int noinline __next_ref_path(struct btrfs_trans_handle *trans, |
| 3327 | struct btrfs_root *extent_root, |
| 3328 | struct btrfs_ref_path *ref_path, |
| 3329 | int first_time) |
| 3330 | { |
| 3331 | struct extent_buffer *leaf; |
| 3332 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3333 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3334 | struct btrfs_key key; |
| 3335 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3336 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3337 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3338 | int level; |
| 3339 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3340 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3341 | path = btrfs_alloc_path(); |
| 3342 | if (!path) |
| 3343 | return -ENOMEM; |
| 3344 | |
| 3345 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
| 3346 | |
| 3347 | if (first_time) { |
| 3348 | ref_path->lowest_level = -1; |
| 3349 | ref_path->current_level = -1; |
| 3350 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3351 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3352 | walk_down: |
| 3353 | level = ref_path->current_level - 1; |
| 3354 | while (level >= -1) { |
| 3355 | u64 parent; |
| 3356 | if (level < ref_path->lowest_level) |
| 3357 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3358 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3359 | if (level >= 0) { |
| 3360 | bytenr = ref_path->nodes[level]; |
| 3361 | } else { |
| 3362 | bytenr = ref_path->extent_start; |
| 3363 | } |
| 3364 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3365 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3366 | parent = ref_path->nodes[level + 1]; |
| 3367 | ref_path->nodes[level + 1] = 0; |
| 3368 | ref_path->current_level = level; |
| 3369 | BUG_ON(parent == 0); |
| 3370 | |
| 3371 | key.objectid = bytenr; |
| 3372 | key.offset = parent + 1; |
| 3373 | key.type = BTRFS_EXTENT_REF_KEY; |
| 3374 | |
| 3375 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3376 | if (ret < 0) |
| 3377 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3378 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3379 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3380 | leaf = path->nodes[0]; |
| 3381 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3382 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3383 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3384 | if (ret < 0) |
| 3385 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3386 | if (ret > 0) |
| 3387 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3388 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3389 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3390 | |
| 3391 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3392 | if (found_key.objectid == bytenr && |
| 3393 | found_key.type == BTRFS_EXTENT_REF_KEY) |
| 3394 | goto found; |
| 3395 | next: |
| 3396 | level--; |
| 3397 | btrfs_release_path(extent_root, path); |
| 3398 | if (need_resched()) { |
| 3399 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 3400 | cond_resched(); |
| 3401 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3402 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3403 | } |
| 3404 | /* reached lowest level */ |
| 3405 | ret = 1; |
| 3406 | goto out; |
| 3407 | walk_up: |
| 3408 | level = ref_path->current_level; |
| 3409 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 3410 | u64 ref_objectid; |
| 3411 | if (level >= 0) { |
| 3412 | bytenr = ref_path->nodes[level]; |
| 3413 | } else { |
| 3414 | bytenr = ref_path->extent_start; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3415 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3416 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3417 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3418 | key.objectid = bytenr; |
| 3419 | key.offset = 0; |
| 3420 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3421 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3422 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 3423 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3424 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3425 | |
| 3426 | leaf = path->nodes[0]; |
| 3427 | nritems = btrfs_header_nritems(leaf); |
| 3428 | if (path->slots[0] >= nritems) { |
| 3429 | ret = btrfs_next_leaf(extent_root, path); |
| 3430 | if (ret < 0) |
| 3431 | goto out; |
| 3432 | if (ret > 0) { |
| 3433 | /* the extent was freed by someone */ |
| 3434 | if (ref_path->lowest_level == level) |
| 3435 | goto out; |
| 3436 | btrfs_release_path(extent_root, path); |
| 3437 | goto walk_down; |
| 3438 | } |
| 3439 | leaf = path->nodes[0]; |
| 3440 | } |
| 3441 | |
| 3442 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3443 | if (found_key.objectid != bytenr || |
| 3444 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 3445 | /* the extent was freed by someone */ |
| 3446 | if (ref_path->lowest_level == level) { |
| 3447 | ret = 1; |
| 3448 | goto out; |
| 3449 | } |
| 3450 | btrfs_release_path(extent_root, path); |
| 3451 | goto walk_down; |
| 3452 | } |
| 3453 | found: |
| 3454 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 3455 | struct btrfs_extent_ref); |
| 3456 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 3457 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 3458 | if (first_time) { |
| 3459 | level = (int)ref_objectid; |
| 3460 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 3461 | ref_path->lowest_level = level; |
| 3462 | ref_path->current_level = level; |
| 3463 | ref_path->nodes[level] = bytenr; |
| 3464 | } else { |
| 3465 | WARN_ON(ref_objectid != level); |
| 3466 | } |
| 3467 | } else { |
| 3468 | WARN_ON(level != -1); |
| 3469 | } |
| 3470 | first_time = 0; |
| 3471 | |
| 3472 | if (ref_path->lowest_level == level) { |
| 3473 | ref_path->owner_objectid = ref_objectid; |
| 3474 | ref_path->owner_offset = btrfs_ref_offset(leaf, ref); |
| 3475 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 3476 | } |
| 3477 | |
| 3478 | /* |
| 3479 | * the block is tree root or the block isn't in reference |
| 3480 | * counted tree. |
| 3481 | */ |
| 3482 | if (found_key.objectid == found_key.offset || |
| 3483 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 3484 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 3485 | ref_path->root_generation = |
| 3486 | btrfs_ref_generation(leaf, ref); |
| 3487 | if (level < 0) { |
| 3488 | /* special reference from the tree log */ |
| 3489 | ref_path->nodes[0] = found_key.offset; |
| 3490 | ref_path->current_level = 0; |
| 3491 | } |
| 3492 | ret = 0; |
| 3493 | goto out; |
| 3494 | } |
| 3495 | |
| 3496 | level++; |
| 3497 | BUG_ON(ref_path->nodes[level] != 0); |
| 3498 | ref_path->nodes[level] = found_key.offset; |
| 3499 | ref_path->current_level = level; |
| 3500 | |
| 3501 | /* |
| 3502 | * the reference was created in the running transaction, |
| 3503 | * no need to continue walking up. |
| 3504 | */ |
| 3505 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 3506 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 3507 | ref_path->root_generation = |
| 3508 | btrfs_ref_generation(leaf, ref); |
| 3509 | ret = 0; |
| 3510 | goto out; |
| 3511 | } |
| 3512 | |
| 3513 | btrfs_release_path(extent_root, path); |
| 3514 | if (need_resched()) { |
| 3515 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 3516 | cond_resched(); |
| 3517 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
| 3518 | } |
| 3519 | } |
| 3520 | /* reached max tree level, but no tree root found. */ |
| 3521 | BUG(); |
| 3522 | out: |
| 3523 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 3524 | btrfs_free_path(path); |
| 3525 | return ret; |
| 3526 | } |
| 3527 | |
| 3528 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 3529 | struct btrfs_root *extent_root, |
| 3530 | struct btrfs_ref_path *ref_path, |
| 3531 | u64 extent_start) |
| 3532 | { |
| 3533 | memset(ref_path, 0, sizeof(*ref_path)); |
| 3534 | ref_path->extent_start = extent_start; |
| 3535 | |
| 3536 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 3537 | } |
| 3538 | |
| 3539 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 3540 | struct btrfs_root *extent_root, |
| 3541 | struct btrfs_ref_path *ref_path) |
| 3542 | { |
| 3543 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 3544 | } |
| 3545 | |
| 3546 | static int noinline get_new_locations(struct inode *reloc_inode, |
| 3547 | struct btrfs_key *extent_key, |
| 3548 | u64 offset, int no_fragment, |
| 3549 | struct disk_extent **extents, |
| 3550 | int *nr_extents) |
| 3551 | { |
| 3552 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 3553 | struct btrfs_path *path; |
| 3554 | struct btrfs_file_extent_item *fi; |
| 3555 | struct extent_buffer *leaf; |
| 3556 | struct disk_extent *exts = *extents; |
| 3557 | struct btrfs_key found_key; |
| 3558 | u64 cur_pos; |
| 3559 | u64 last_byte; |
| 3560 | u32 nritems; |
| 3561 | int nr = 0; |
| 3562 | int max = *nr_extents; |
| 3563 | int ret; |
| 3564 | |
| 3565 | WARN_ON(!no_fragment && *extents); |
| 3566 | if (!exts) { |
| 3567 | max = 1; |
| 3568 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 3569 | if (!exts) |
| 3570 | return -ENOMEM; |
| 3571 | } |
| 3572 | |
| 3573 | path = btrfs_alloc_path(); |
| 3574 | BUG_ON(!path); |
| 3575 | |
| 3576 | cur_pos = extent_key->objectid - offset; |
| 3577 | last_byte = extent_key->objectid + extent_key->offset; |
| 3578 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 3579 | cur_pos, 0); |
| 3580 | if (ret < 0) |
| 3581 | goto out; |
| 3582 | if (ret > 0) { |
| 3583 | ret = -ENOENT; |
| 3584 | goto out; |
| 3585 | } |
| 3586 | |
| 3587 | while (1) { |
| 3588 | leaf = path->nodes[0]; |
| 3589 | nritems = btrfs_header_nritems(leaf); |
| 3590 | if (path->slots[0] >= nritems) { |
| 3591 | ret = btrfs_next_leaf(root, path); |
| 3592 | if (ret < 0) |
| 3593 | goto out; |
| 3594 | if (ret > 0) |
| 3595 | break; |
| 3596 | leaf = path->nodes[0]; |
| 3597 | } |
| 3598 | |
| 3599 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3600 | if (found_key.offset != cur_pos || |
| 3601 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 3602 | found_key.objectid != reloc_inode->i_ino) |
| 3603 | break; |
| 3604 | |
| 3605 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3606 | struct btrfs_file_extent_item); |
| 3607 | if (btrfs_file_extent_type(leaf, fi) != |
| 3608 | BTRFS_FILE_EXTENT_REG || |
| 3609 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 3610 | break; |
| 3611 | |
| 3612 | if (nr == max) { |
| 3613 | struct disk_extent *old = exts; |
| 3614 | max *= 2; |
| 3615 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 3616 | memcpy(exts, old, sizeof(*exts) * nr); |
| 3617 | if (old != *extents) |
| 3618 | kfree(old); |
| 3619 | } |
| 3620 | |
| 3621 | exts[nr].disk_bytenr = |
| 3622 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 3623 | exts[nr].disk_num_bytes = |
| 3624 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 3625 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 3626 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 3627 | WARN_ON(exts[nr].offset > 0); |
| 3628 | WARN_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
| 3629 | |
| 3630 | cur_pos += exts[nr].num_bytes; |
| 3631 | nr++; |
| 3632 | |
| 3633 | if (cur_pos + offset >= last_byte) |
| 3634 | break; |
| 3635 | |
| 3636 | if (no_fragment) { |
| 3637 | ret = 1; |
| 3638 | goto out; |
| 3639 | } |
| 3640 | path->slots[0]++; |
| 3641 | } |
| 3642 | |
| 3643 | WARN_ON(cur_pos + offset > last_byte); |
| 3644 | if (cur_pos + offset < last_byte) { |
| 3645 | ret = -ENOENT; |
| 3646 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3647 | } |
| 3648 | ret = 0; |
| 3649 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 3650 | btrfs_free_path(path); |
| 3651 | if (ret) { |
| 3652 | if (exts != *extents) |
| 3653 | kfree(exts); |
| 3654 | } else { |
| 3655 | *extents = exts; |
| 3656 | *nr_extents = nr; |
| 3657 | } |
| 3658 | return ret; |
| 3659 | } |
| 3660 | |
| 3661 | static int noinline replace_one_extent(struct btrfs_trans_handle *trans, |
| 3662 | struct btrfs_root *root, |
| 3663 | struct btrfs_path *path, |
| 3664 | struct btrfs_key *extent_key, |
| 3665 | struct btrfs_key *leaf_key, |
| 3666 | struct btrfs_ref_path *ref_path, |
| 3667 | struct disk_extent *new_extents, |
| 3668 | int nr_extents) |
| 3669 | { |
| 3670 | struct extent_buffer *leaf; |
| 3671 | struct btrfs_file_extent_item *fi; |
| 3672 | struct inode *inode = NULL; |
| 3673 | struct btrfs_key key; |
| 3674 | u64 lock_start = 0; |
| 3675 | u64 lock_end = 0; |
| 3676 | u64 num_bytes; |
| 3677 | u64 ext_offset; |
| 3678 | u64 first_pos; |
| 3679 | u32 nritems; |
| 3680 | int extent_locked = 0; |
| 3681 | int ret; |
| 3682 | |
| 3683 | first_pos = ref_path->owner_offset; |
| 3684 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 3685 | key.objectid = ref_path->owner_objectid; |
| 3686 | key.offset = ref_path->owner_offset; |
| 3687 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 3688 | } else { |
| 3689 | memcpy(&key, leaf_key, sizeof(key)); |
| 3690 | } |
| 3691 | |
| 3692 | while (1) { |
| 3693 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 3694 | if (ret < 0) |
| 3695 | goto out; |
| 3696 | |
| 3697 | leaf = path->nodes[0]; |
| 3698 | nritems = btrfs_header_nritems(leaf); |
| 3699 | next: |
| 3700 | if (extent_locked && ret > 0) { |
| 3701 | /* |
| 3702 | * the file extent item was modified by someone |
| 3703 | * before the extent got locked. |
| 3704 | */ |
| 3705 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
| 3706 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 3707 | lock_end, GFP_NOFS); |
| 3708 | extent_locked = 0; |
| 3709 | } |
| 3710 | |
| 3711 | if (path->slots[0] >= nritems) { |
| 3712 | if (ref_path->owner_objectid == |
| 3713 | BTRFS_MULTIPLE_OBJECTIDS) |
| 3714 | break; |
| 3715 | |
| 3716 | BUG_ON(extent_locked); |
| 3717 | ret = btrfs_next_leaf(root, path); |
| 3718 | if (ret < 0) |
| 3719 | goto out; |
| 3720 | if (ret > 0) |
| 3721 | break; |
| 3722 | leaf = path->nodes[0]; |
| 3723 | nritems = btrfs_header_nritems(leaf); |
| 3724 | } |
| 3725 | |
| 3726 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 3727 | |
| 3728 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 3729 | if ((key.objectid > ref_path->owner_objectid) || |
| 3730 | (key.objectid == ref_path->owner_objectid && |
| 3731 | key.type > BTRFS_EXTENT_DATA_KEY) || |
| 3732 | (key.offset >= first_pos + extent_key->offset)) |
| 3733 | break; |
| 3734 | } |
| 3735 | |
| 3736 | if (inode && key.objectid != inode->i_ino) { |
| 3737 | BUG_ON(extent_locked); |
| 3738 | btrfs_release_path(root, path); |
| 3739 | mutex_unlock(&inode->i_mutex); |
| 3740 | iput(inode); |
| 3741 | inode = NULL; |
| 3742 | continue; |
| 3743 | } |
| 3744 | |
| 3745 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 3746 | path->slots[0]++; |
| 3747 | ret = 1; |
| 3748 | goto next; |
| 3749 | } |
| 3750 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3751 | struct btrfs_file_extent_item); |
| 3752 | if ((btrfs_file_extent_type(leaf, fi) != |
| 3753 | BTRFS_FILE_EXTENT_REG) || |
| 3754 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 3755 | extent_key->objectid)) { |
| 3756 | path->slots[0]++; |
| 3757 | ret = 1; |
| 3758 | goto next; |
| 3759 | } |
| 3760 | |
| 3761 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 3762 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 3763 | |
| 3764 | if (first_pos > key.offset - ext_offset) |
| 3765 | first_pos = key.offset - ext_offset; |
| 3766 | |
| 3767 | if (!extent_locked) { |
| 3768 | lock_start = key.offset; |
| 3769 | lock_end = lock_start + num_bytes - 1; |
| 3770 | } else { |
| 3771 | BUG_ON(lock_start != key.offset); |
| 3772 | BUG_ON(lock_end - lock_start + 1 < num_bytes); |
| 3773 | } |
| 3774 | |
| 3775 | if (!inode) { |
| 3776 | btrfs_release_path(root, path); |
| 3777 | |
| 3778 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 3779 | key.objectid, root); |
| 3780 | if (inode->i_state & I_NEW) { |
| 3781 | BTRFS_I(inode)->root = root; |
| 3782 | BTRFS_I(inode)->location.objectid = |
| 3783 | key.objectid; |
| 3784 | BTRFS_I(inode)->location.type = |
| 3785 | BTRFS_INODE_ITEM_KEY; |
| 3786 | BTRFS_I(inode)->location.offset = 0; |
| 3787 | btrfs_read_locked_inode(inode); |
| 3788 | unlock_new_inode(inode); |
| 3789 | } |
| 3790 | /* |
| 3791 | * some code call btrfs_commit_transaction while |
| 3792 | * holding the i_mutex, so we can't use mutex_lock |
| 3793 | * here. |
| 3794 | */ |
| 3795 | if (is_bad_inode(inode) || |
| 3796 | !mutex_trylock(&inode->i_mutex)) { |
| 3797 | iput(inode); |
| 3798 | inode = NULL; |
| 3799 | key.offset = (u64)-1; |
| 3800 | goto skip; |
| 3801 | } |
| 3802 | } |
| 3803 | |
| 3804 | if (!extent_locked) { |
| 3805 | struct btrfs_ordered_extent *ordered; |
| 3806 | |
| 3807 | btrfs_release_path(root, path); |
| 3808 | |
| 3809 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 3810 | lock_end, GFP_NOFS); |
| 3811 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 3812 | lock_end); |
| 3813 | if (ordered && |
| 3814 | ordered->file_offset <= lock_end && |
| 3815 | ordered->file_offset + ordered->len > lock_start) { |
| 3816 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 3817 | lock_start, lock_end, GFP_NOFS); |
| 3818 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3819 | btrfs_put_ordered_extent(ordered); |
| 3820 | key.offset += num_bytes; |
| 3821 | goto skip; |
| 3822 | } |
| 3823 | if (ordered) |
| 3824 | btrfs_put_ordered_extent(ordered); |
| 3825 | |
| 3826 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
| 3827 | extent_locked = 1; |
| 3828 | continue; |
| 3829 | } |
| 3830 | |
| 3831 | if (nr_extents == 1) { |
| 3832 | /* update extent pointer in place */ |
| 3833 | btrfs_set_file_extent_generation(leaf, fi, |
| 3834 | trans->transid); |
| 3835 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 3836 | new_extents[0].disk_bytenr); |
| 3837 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 3838 | new_extents[0].disk_num_bytes); |
| 3839 | ext_offset += new_extents[0].offset; |
| 3840 | btrfs_set_file_extent_offset(leaf, fi, ext_offset); |
| 3841 | btrfs_mark_buffer_dirty(leaf); |
| 3842 | |
| 3843 | btrfs_drop_extent_cache(inode, key.offset, |
| 3844 | key.offset + num_bytes - 1, 0); |
| 3845 | |
| 3846 | ret = btrfs_inc_extent_ref(trans, root, |
| 3847 | new_extents[0].disk_bytenr, |
| 3848 | new_extents[0].disk_num_bytes, |
| 3849 | leaf->start, |
| 3850 | root->root_key.objectid, |
| 3851 | trans->transid, |
| 3852 | key.objectid, key.offset); |
| 3853 | BUG_ON(ret); |
| 3854 | |
| 3855 | ret = btrfs_free_extent(trans, root, |
| 3856 | extent_key->objectid, |
| 3857 | extent_key->offset, |
| 3858 | leaf->start, |
| 3859 | btrfs_header_owner(leaf), |
| 3860 | btrfs_header_generation(leaf), |
| 3861 | key.objectid, key.offset, 0); |
| 3862 | BUG_ON(ret); |
| 3863 | |
| 3864 | btrfs_release_path(root, path); |
| 3865 | key.offset += num_bytes; |
| 3866 | } else { |
| 3867 | u64 alloc_hint; |
| 3868 | u64 extent_len; |
| 3869 | int i; |
| 3870 | /* |
| 3871 | * drop old extent pointer at first, then insert the |
| 3872 | * new pointers one bye one |
| 3873 | */ |
| 3874 | btrfs_release_path(root, path); |
| 3875 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 3876 | key.offset + num_bytes, |
| 3877 | key.offset, &alloc_hint); |
| 3878 | BUG_ON(ret); |
| 3879 | |
| 3880 | for (i = 0; i < nr_extents; i++) { |
| 3881 | if (ext_offset >= new_extents[i].num_bytes) { |
| 3882 | ext_offset -= new_extents[i].num_bytes; |
| 3883 | continue; |
| 3884 | } |
| 3885 | extent_len = min(new_extents[i].num_bytes - |
| 3886 | ext_offset, num_bytes); |
| 3887 | |
| 3888 | ret = btrfs_insert_empty_item(trans, root, |
| 3889 | path, &key, |
| 3890 | sizeof(*fi)); |
| 3891 | BUG_ON(ret); |
| 3892 | |
| 3893 | leaf = path->nodes[0]; |
| 3894 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3895 | struct btrfs_file_extent_item); |
| 3896 | btrfs_set_file_extent_generation(leaf, fi, |
| 3897 | trans->transid); |
| 3898 | btrfs_set_file_extent_type(leaf, fi, |
| 3899 | BTRFS_FILE_EXTENT_REG); |
| 3900 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 3901 | new_extents[i].disk_bytenr); |
| 3902 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 3903 | new_extents[i].disk_num_bytes); |
| 3904 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3905 | extent_len); |
| 3906 | ext_offset += new_extents[i].offset; |
| 3907 | btrfs_set_file_extent_offset(leaf, fi, |
| 3908 | ext_offset); |
| 3909 | btrfs_mark_buffer_dirty(leaf); |
| 3910 | |
| 3911 | btrfs_drop_extent_cache(inode, key.offset, |
| 3912 | key.offset + extent_len - 1, 0); |
| 3913 | |
| 3914 | ret = btrfs_inc_extent_ref(trans, root, |
| 3915 | new_extents[i].disk_bytenr, |
| 3916 | new_extents[i].disk_num_bytes, |
| 3917 | leaf->start, |
| 3918 | root->root_key.objectid, |
| 3919 | trans->transid, |
| 3920 | key.objectid, key.offset); |
| 3921 | BUG_ON(ret); |
| 3922 | btrfs_release_path(root, path); |
| 3923 | |
| 3924 | inode->i_blocks += extent_len >> 9; |
| 3925 | |
| 3926 | ext_offset = 0; |
| 3927 | num_bytes -= extent_len; |
| 3928 | key.offset += extent_len; |
| 3929 | |
| 3930 | if (num_bytes == 0) |
| 3931 | break; |
| 3932 | } |
| 3933 | BUG_ON(i >= nr_extents); |
| 3934 | } |
| 3935 | |
| 3936 | if (extent_locked) { |
| 3937 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
| 3938 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 3939 | lock_end, GFP_NOFS); |
| 3940 | extent_locked = 0; |
| 3941 | } |
| 3942 | skip: |
| 3943 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
| 3944 | key.offset >= first_pos + extent_key->offset) |
| 3945 | break; |
| 3946 | |
| 3947 | cond_resched(); |
| 3948 | } |
| 3949 | ret = 0; |
| 3950 | out: |
| 3951 | btrfs_release_path(root, path); |
| 3952 | if (inode) { |
| 3953 | mutex_unlock(&inode->i_mutex); |
| 3954 | if (extent_locked) { |
| 3955 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
| 3956 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 3957 | lock_end, GFP_NOFS); |
| 3958 | } |
| 3959 | iput(inode); |
| 3960 | } |
| 3961 | return ret; |
| 3962 | } |
| 3963 | |
| 3964 | int btrfs_add_reloc_mapping(struct btrfs_root *root, u64 orig_bytenr, |
| 3965 | u64 num_bytes, u64 new_bytenr) |
| 3966 | { |
| 3967 | set_extent_bits(&root->fs_info->reloc_mapping_tree, |
| 3968 | orig_bytenr, orig_bytenr + num_bytes - 1, |
| 3969 | EXTENT_LOCKED, GFP_NOFS); |
| 3970 | set_state_private(&root->fs_info->reloc_mapping_tree, |
| 3971 | orig_bytenr, new_bytenr); |
| 3972 | return 0; |
| 3973 | } |
| 3974 | |
| 3975 | int btrfs_get_reloc_mapping(struct btrfs_root *root, u64 orig_bytenr, |
| 3976 | u64 num_bytes, u64 *new_bytenr) |
| 3977 | { |
| 3978 | u64 bytenr; |
| 3979 | u64 cur_bytenr = orig_bytenr; |
| 3980 | u64 prev_bytenr = orig_bytenr; |
| 3981 | int ret; |
| 3982 | |
| 3983 | while (1) { |
| 3984 | ret = get_state_private(&root->fs_info->reloc_mapping_tree, |
| 3985 | cur_bytenr, &bytenr); |
| 3986 | if (ret) |
| 3987 | break; |
| 3988 | prev_bytenr = cur_bytenr; |
| 3989 | cur_bytenr = bytenr; |
| 3990 | } |
| 3991 | |
| 3992 | if (orig_bytenr == cur_bytenr) |
| 3993 | return -ENOENT; |
| 3994 | |
| 3995 | if (prev_bytenr != orig_bytenr) { |
| 3996 | set_state_private(&root->fs_info->reloc_mapping_tree, |
| 3997 | orig_bytenr, cur_bytenr); |
| 3998 | } |
| 3999 | *new_bytenr = cur_bytenr; |
| 4000 | return 0; |
| 4001 | } |
| 4002 | |
| 4003 | void btrfs_free_reloc_mappings(struct btrfs_root *root) |
| 4004 | { |
| 4005 | clear_extent_bits(&root->fs_info->reloc_mapping_tree, |
| 4006 | 0, (u64)-1, -1, GFP_NOFS); |
| 4007 | } |
| 4008 | |
| 4009 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 4010 | struct btrfs_root *root, |
| 4011 | struct extent_buffer *buf, u64 orig_start) |
| 4012 | { |
| 4013 | int level; |
| 4014 | int ret; |
| 4015 | |
| 4016 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 4017 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 4018 | |
| 4019 | level = btrfs_header_level(buf); |
| 4020 | if (level == 0) { |
| 4021 | struct btrfs_leaf_ref *ref; |
| 4022 | struct btrfs_leaf_ref *orig_ref; |
| 4023 | |
| 4024 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 4025 | if (!orig_ref) |
| 4026 | return -ENOENT; |
| 4027 | |
| 4028 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 4029 | if (!ref) { |
| 4030 | btrfs_free_leaf_ref(root, orig_ref); |
| 4031 | return -ENOMEM; |
| 4032 | } |
| 4033 | |
| 4034 | ref->nritems = orig_ref->nritems; |
| 4035 | memcpy(ref->extents, orig_ref->extents, |
| 4036 | sizeof(ref->extents[0]) * ref->nritems); |
| 4037 | |
| 4038 | btrfs_free_leaf_ref(root, orig_ref); |
| 4039 | |
| 4040 | ref->root_gen = trans->transid; |
| 4041 | ref->bytenr = buf->start; |
| 4042 | ref->owner = btrfs_header_owner(buf); |
| 4043 | ref->generation = btrfs_header_generation(buf); |
| 4044 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 4045 | WARN_ON(ret); |
| 4046 | btrfs_free_leaf_ref(root, ref); |
| 4047 | } |
| 4048 | return 0; |
| 4049 | } |
| 4050 | |
| 4051 | static int noinline invalidate_extent_cache(struct btrfs_root *root, |
| 4052 | struct extent_buffer *leaf, |
| 4053 | struct btrfs_block_group_cache *group, |
| 4054 | struct btrfs_root *target_root) |
| 4055 | { |
| 4056 | struct btrfs_key key; |
| 4057 | struct inode *inode = NULL; |
| 4058 | struct btrfs_file_extent_item *fi; |
| 4059 | u64 num_bytes; |
| 4060 | u64 skip_objectid = 0; |
| 4061 | u32 nritems; |
| 4062 | u32 i; |
| 4063 | |
| 4064 | nritems = btrfs_header_nritems(leaf); |
| 4065 | for (i = 0; i < nritems; i++) { |
| 4066 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4067 | if (key.objectid == skip_objectid || |
| 4068 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 4069 | continue; |
| 4070 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4071 | if (btrfs_file_extent_type(leaf, fi) == |
| 4072 | BTRFS_FILE_EXTENT_INLINE) |
| 4073 | continue; |
| 4074 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4075 | continue; |
| 4076 | if (!inode || inode->i_ino != key.objectid) { |
| 4077 | iput(inode); |
| 4078 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 4079 | key.objectid, target_root, 1); |
| 4080 | } |
| 4081 | if (!inode) { |
| 4082 | skip_objectid = key.objectid; |
| 4083 | continue; |
| 4084 | } |
| 4085 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4086 | |
| 4087 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4088 | key.offset + num_bytes - 1, GFP_NOFS); |
| 4089 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
| 4090 | btrfs_drop_extent_cache(inode, key.offset, |
| 4091 | key.offset + num_bytes - 1, 1); |
| 4092 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
| 4093 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4094 | key.offset + num_bytes - 1, GFP_NOFS); |
| 4095 | cond_resched(); |
| 4096 | } |
| 4097 | iput(inode); |
| 4098 | return 0; |
| 4099 | } |
| 4100 | |
| 4101 | static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
| 4102 | struct btrfs_root *root, |
| 4103 | struct extent_buffer *leaf, |
| 4104 | struct btrfs_block_group_cache *group, |
| 4105 | struct inode *reloc_inode) |
| 4106 | { |
| 4107 | struct btrfs_key key; |
| 4108 | struct btrfs_key extent_key; |
| 4109 | struct btrfs_file_extent_item *fi; |
| 4110 | struct btrfs_leaf_ref *ref; |
| 4111 | struct disk_extent *new_extent; |
| 4112 | u64 bytenr; |
| 4113 | u64 num_bytes; |
| 4114 | u32 nritems; |
| 4115 | u32 i; |
| 4116 | int ext_index; |
| 4117 | int nr_extent; |
| 4118 | int ret; |
| 4119 | |
| 4120 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 4121 | BUG_ON(!new_extent); |
| 4122 | |
| 4123 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 4124 | BUG_ON(!ref); |
| 4125 | |
| 4126 | ext_index = -1; |
| 4127 | nritems = btrfs_header_nritems(leaf); |
| 4128 | for (i = 0; i < nritems; i++) { |
| 4129 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4130 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 4131 | continue; |
| 4132 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4133 | if (btrfs_file_extent_type(leaf, fi) == |
| 4134 | BTRFS_FILE_EXTENT_INLINE) |
| 4135 | continue; |
| 4136 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4137 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4138 | if (bytenr == 0) |
| 4139 | continue; |
| 4140 | |
| 4141 | ext_index++; |
| 4142 | if (bytenr >= group->key.objectid + group->key.offset || |
| 4143 | bytenr + num_bytes <= group->key.objectid) |
| 4144 | continue; |
| 4145 | |
| 4146 | extent_key.objectid = bytenr; |
| 4147 | extent_key.offset = num_bytes; |
| 4148 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4149 | nr_extent = 1; |
| 4150 | ret = get_new_locations(reloc_inode, &extent_key, |
| 4151 | group->key.objectid, 1, |
| 4152 | &new_extent, &nr_extent); |
| 4153 | if (ret > 0) |
| 4154 | continue; |
| 4155 | BUG_ON(ret < 0); |
| 4156 | |
| 4157 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 4158 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 4159 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 4160 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 4161 | |
| 4162 | btrfs_set_file_extent_generation(leaf, fi, trans->transid); |
| 4163 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4164 | new_extent->disk_bytenr); |
| 4165 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4166 | new_extent->disk_num_bytes); |
| 4167 | new_extent->offset += btrfs_file_extent_offset(leaf, fi); |
| 4168 | btrfs_set_file_extent_offset(leaf, fi, new_extent->offset); |
| 4169 | btrfs_mark_buffer_dirty(leaf); |
| 4170 | |
| 4171 | ret = btrfs_inc_extent_ref(trans, root, |
| 4172 | new_extent->disk_bytenr, |
| 4173 | new_extent->disk_num_bytes, |
| 4174 | leaf->start, |
| 4175 | root->root_key.objectid, |
| 4176 | trans->transid, |
| 4177 | key.objectid, key.offset); |
| 4178 | BUG_ON(ret); |
| 4179 | ret = btrfs_free_extent(trans, root, |
| 4180 | bytenr, num_bytes, leaf->start, |
| 4181 | btrfs_header_owner(leaf), |
| 4182 | btrfs_header_generation(leaf), |
| 4183 | key.objectid, key.offset, 0); |
| 4184 | BUG_ON(ret); |
| 4185 | cond_resched(); |
| 4186 | } |
| 4187 | kfree(new_extent); |
| 4188 | BUG_ON(ext_index + 1 != ref->nritems); |
| 4189 | btrfs_free_leaf_ref(root, ref); |
| 4190 | return 0; |
| 4191 | } |
| 4192 | |
| 4193 | int btrfs_free_reloc_root(struct btrfs_root *root) |
| 4194 | { |
| 4195 | struct btrfs_root *reloc_root; |
| 4196 | |
| 4197 | if (root->reloc_root) { |
| 4198 | reloc_root = root->reloc_root; |
| 4199 | root->reloc_root = NULL; |
| 4200 | list_add(&reloc_root->dead_list, |
| 4201 | &root->fs_info->dead_reloc_roots); |
| 4202 | } |
| 4203 | return 0; |
| 4204 | } |
| 4205 | |
| 4206 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 4207 | { |
| 4208 | struct btrfs_trans_handle *trans; |
| 4209 | struct btrfs_root *reloc_root; |
| 4210 | struct btrfs_root *prev_root = NULL; |
| 4211 | struct list_head dead_roots; |
| 4212 | int ret; |
| 4213 | unsigned long nr; |
| 4214 | |
| 4215 | INIT_LIST_HEAD(&dead_roots); |
| 4216 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 4217 | |
| 4218 | while (!list_empty(&dead_roots)) { |
| 4219 | reloc_root = list_entry(dead_roots.prev, |
| 4220 | struct btrfs_root, dead_list); |
| 4221 | list_del_init(&reloc_root->dead_list); |
| 4222 | |
| 4223 | BUG_ON(reloc_root->commit_root != NULL); |
| 4224 | while (1) { |
| 4225 | trans = btrfs_join_transaction(root, 1); |
| 4226 | BUG_ON(!trans); |
| 4227 | |
| 4228 | mutex_lock(&root->fs_info->drop_mutex); |
| 4229 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 4230 | if (ret != -EAGAIN) |
| 4231 | break; |
| 4232 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4233 | |
| 4234 | nr = trans->blocks_used; |
| 4235 | ret = btrfs_end_transaction(trans, root); |
| 4236 | BUG_ON(ret); |
| 4237 | btrfs_btree_balance_dirty(root, nr); |
| 4238 | } |
| 4239 | |
| 4240 | free_extent_buffer(reloc_root->node); |
| 4241 | |
| 4242 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 4243 | &reloc_root->root_key); |
| 4244 | BUG_ON(ret); |
| 4245 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4246 | |
| 4247 | nr = trans->blocks_used; |
| 4248 | ret = btrfs_end_transaction(trans, root); |
| 4249 | BUG_ON(ret); |
| 4250 | btrfs_btree_balance_dirty(root, nr); |
| 4251 | |
| 4252 | kfree(prev_root); |
| 4253 | prev_root = reloc_root; |
| 4254 | } |
| 4255 | if (prev_root) { |
| 4256 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 4257 | kfree(prev_root); |
| 4258 | } |
| 4259 | return 0; |
| 4260 | } |
| 4261 | |
| 4262 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 4263 | { |
| 4264 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 4265 | return 0; |
| 4266 | } |
| 4267 | |
| 4268 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 4269 | { |
| 4270 | struct btrfs_root *reloc_root; |
| 4271 | struct btrfs_trans_handle *trans; |
| 4272 | struct btrfs_key location; |
| 4273 | int found; |
| 4274 | int ret; |
| 4275 | |
| 4276 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 4277 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 4278 | BUG_ON(ret); |
| 4279 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 4280 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 4281 | |
| 4282 | if (found) { |
| 4283 | trans = btrfs_start_transaction(root, 1); |
| 4284 | BUG_ON(!trans); |
| 4285 | ret = btrfs_commit_transaction(trans, root); |
| 4286 | BUG_ON(ret); |
| 4287 | } |
| 4288 | |
| 4289 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 4290 | location.offset = (u64)-1; |
| 4291 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 4292 | |
| 4293 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 4294 | BUG_ON(!reloc_root); |
| 4295 | btrfs_orphan_cleanup(reloc_root); |
| 4296 | return 0; |
| 4297 | } |
| 4298 | |
| 4299 | static int noinline init_reloc_tree(struct btrfs_trans_handle *trans, |
| 4300 | struct btrfs_root *root) |
| 4301 | { |
| 4302 | struct btrfs_root *reloc_root; |
| 4303 | struct extent_buffer *eb; |
| 4304 | struct btrfs_root_item *root_item; |
| 4305 | struct btrfs_key root_key; |
| 4306 | int ret; |
| 4307 | |
| 4308 | BUG_ON(!root->ref_cows); |
| 4309 | if (root->reloc_root) |
| 4310 | return 0; |
| 4311 | |
| 4312 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 4313 | BUG_ON(!root_item); |
| 4314 | |
| 4315 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 4316 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 4317 | BUG_ON(ret); |
| 4318 | |
| 4319 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 4320 | root_key.offset = root->root_key.objectid; |
| 4321 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 4322 | |
| 4323 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 4324 | btrfs_set_root_refs(root_item, 0); |
| 4325 | btrfs_set_root_bytenr(root_item, eb->start); |
| 4326 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
| 4327 | memset(&root_item->drop_progress, 0, sizeof(root_item->drop_progress)); |
| 4328 | root_item->drop_level = 0; |
| 4329 | |
| 4330 | btrfs_tree_unlock(eb); |
| 4331 | free_extent_buffer(eb); |
| 4332 | |
| 4333 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 4334 | &root_key, root_item); |
| 4335 | BUG_ON(ret); |
| 4336 | kfree(root_item); |
| 4337 | |
| 4338 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 4339 | &root_key); |
| 4340 | BUG_ON(!reloc_root); |
| 4341 | reloc_root->last_trans = trans->transid; |
| 4342 | reloc_root->commit_root = NULL; |
| 4343 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 4344 | |
| 4345 | root->reloc_root = reloc_root; |
| 4346 | return 0; |
| 4347 | } |
| 4348 | |
| 4349 | /* |
| 4350 | * Core function of space balance. |
| 4351 | * |
| 4352 | * The idea is using reloc trees to relocate tree blocks in reference |
| 4353 | * counted roots. There is one reloc tree for each subvol, all reloc |
| 4354 | * trees share same key objectid. Reloc trees are snapshots of the |
| 4355 | * latest committed roots (subvol root->commit_root). To relocate a tree |
| 4356 | * block referenced by a subvol, the code COW the block through the reloc |
| 4357 | * tree, then update pointer in the subvol to point to the new block. |
| 4358 | * Since all reloc trees share same key objectid, we can easily do special |
| 4359 | * handing to share tree blocks between reloc trees. Once a tree block has |
| 4360 | * been COWed in one reloc tree, we can use the result when the same block |
| 4361 | * is COWed again through other reloc trees. |
| 4362 | */ |
| 4363 | static int noinline relocate_one_path(struct btrfs_trans_handle *trans, |
| 4364 | struct btrfs_root *root, |
| 4365 | struct btrfs_path *path, |
| 4366 | struct btrfs_key *first_key, |
| 4367 | struct btrfs_ref_path *ref_path, |
| 4368 | struct btrfs_block_group_cache *group, |
| 4369 | struct inode *reloc_inode) |
| 4370 | { |
| 4371 | struct btrfs_root *reloc_root; |
| 4372 | struct extent_buffer *eb = NULL; |
| 4373 | struct btrfs_key *keys; |
| 4374 | u64 *nodes; |
| 4375 | int level; |
| 4376 | int lowest_merge; |
| 4377 | int lowest_level = 0; |
| 4378 | int update_refs; |
| 4379 | int ret; |
| 4380 | |
| 4381 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 4382 | lowest_level = ref_path->owner_objectid; |
| 4383 | |
| 4384 | if (is_cowonly_root(ref_path->root_objectid)) { |
| 4385 | path->lowest_level = lowest_level; |
| 4386 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 4387 | BUG_ON(ret < 0); |
| 4388 | path->lowest_level = 0; |
| 4389 | btrfs_release_path(root, path); |
| 4390 | return 0; |
| 4391 | } |
| 4392 | |
| 4393 | keys = kzalloc(sizeof(*keys) * BTRFS_MAX_LEVEL, GFP_NOFS); |
| 4394 | BUG_ON(!keys); |
| 4395 | nodes = kzalloc(sizeof(*nodes) * BTRFS_MAX_LEVEL, GFP_NOFS); |
| 4396 | BUG_ON(!nodes); |
| 4397 | |
| 4398 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 4399 | ret = init_reloc_tree(trans, root); |
| 4400 | BUG_ON(ret); |
| 4401 | reloc_root = root->reloc_root; |
| 4402 | |
| 4403 | path->lowest_level = lowest_level; |
| 4404 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, 0, 0); |
| 4405 | BUG_ON(ret); |
| 4406 | /* |
| 4407 | * get relocation mapping for tree blocks in the path |
| 4408 | */ |
| 4409 | lowest_merge = BTRFS_MAX_LEVEL; |
| 4410 | for (level = BTRFS_MAX_LEVEL - 1; level >= lowest_level; level--) { |
| 4411 | u64 new_bytenr; |
| 4412 | eb = path->nodes[level]; |
| 4413 | if (!eb || eb == reloc_root->node) |
| 4414 | continue; |
| 4415 | ret = btrfs_get_reloc_mapping(reloc_root, eb->start, eb->len, |
| 4416 | &new_bytenr); |
| 4417 | if (ret) |
| 4418 | continue; |
| 4419 | if (level == 0) |
| 4420 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 4421 | else |
| 4422 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 4423 | nodes[level] = new_bytenr; |
| 4424 | lowest_merge = level; |
| 4425 | } |
| 4426 | |
| 4427 | update_refs = 0; |
| 4428 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 4429 | eb = path->nodes[0]; |
| 4430 | if (btrfs_header_generation(eb) < trans->transid) |
| 4431 | update_refs = 1; |
| 4432 | } |
| 4433 | |
| 4434 | btrfs_release_path(reloc_root, path); |
| 4435 | /* |
| 4436 | * merge tree blocks that already relocated in other reloc trees |
| 4437 | */ |
| 4438 | if (lowest_merge != BTRFS_MAX_LEVEL) { |
| 4439 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
| 4440 | lowest_merge); |
| 4441 | BUG_ON(ret < 0); |
| 4442 | } |
| 4443 | /* |
| 4444 | * cow any tree blocks that still haven't been relocated |
| 4445 | */ |
| 4446 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, 0, 1); |
| 4447 | BUG_ON(ret); |
| 4448 | /* |
| 4449 | * if we are relocating data block group, update extent pointers |
| 4450 | * in the newly created tree leaf. |
| 4451 | */ |
| 4452 | eb = path->nodes[0]; |
| 4453 | if (update_refs && nodes[0] != eb->start) { |
| 4454 | ret = replace_extents_in_leaf(trans, reloc_root, eb, group, |
| 4455 | reloc_inode); |
| 4456 | BUG_ON(ret); |
| 4457 | } |
| 4458 | |
| 4459 | memset(keys, 0, sizeof(*keys) * BTRFS_MAX_LEVEL); |
| 4460 | memset(nodes, 0, sizeof(*nodes) * BTRFS_MAX_LEVEL); |
| 4461 | for (level = BTRFS_MAX_LEVEL - 1; level >= lowest_level; level--) { |
| 4462 | eb = path->nodes[level]; |
| 4463 | if (!eb || eb == reloc_root->node) |
| 4464 | continue; |
| 4465 | BUG_ON(btrfs_header_owner(eb) != BTRFS_TREE_RELOC_OBJECTID); |
| 4466 | nodes[level] = eb->start; |
| 4467 | if (level == 0) |
| 4468 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 4469 | else |
| 4470 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 4471 | } |
| 4472 | |
| 4473 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 4474 | eb = path->nodes[0]; |
| 4475 | extent_buffer_get(eb); |
| 4476 | } |
| 4477 | btrfs_release_path(reloc_root, path); |
| 4478 | /* |
| 4479 | * replace tree blocks in the fs tree with tree blocks in |
| 4480 | * the reloc tree. |
| 4481 | */ |
| 4482 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 4483 | BUG_ON(ret < 0); |
| 4484 | |
| 4485 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 4486 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 4487 | BUG_ON(ret); |
| 4488 | free_extent_buffer(eb); |
| 4489 | } |
| 4490 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 4491 | |
| 4492 | path->lowest_level = 0; |
| 4493 | kfree(nodes); |
| 4494 | kfree(keys); |
| 4495 | return 0; |
| 4496 | } |
| 4497 | |
| 4498 | static int noinline relocate_tree_block(struct btrfs_trans_handle *trans, |
| 4499 | struct btrfs_root *root, |
| 4500 | struct btrfs_path *path, |
| 4501 | struct btrfs_key *first_key, |
| 4502 | struct btrfs_ref_path *ref_path) |
| 4503 | { |
| 4504 | int ret; |
| 4505 | int needs_lock = 0; |
| 4506 | |
| 4507 | if (root == root->fs_info->extent_root || |
| 4508 | root == root->fs_info->chunk_root || |
| 4509 | root == root->fs_info->dev_root) { |
| 4510 | needs_lock = 1; |
| 4511 | mutex_lock(&root->fs_info->alloc_mutex); |
| 4512 | } |
| 4513 | |
| 4514 | ret = relocate_one_path(trans, root, path, first_key, |
| 4515 | ref_path, NULL, NULL); |
| 4516 | BUG_ON(ret); |
| 4517 | |
| 4518 | if (root == root->fs_info->extent_root) |
| 4519 | btrfs_extent_post_op(trans, root); |
| 4520 | if (needs_lock) |
| 4521 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 4522 | |
| 4523 | return 0; |
| 4524 | } |
| 4525 | |
| 4526 | static int noinline del_extent_zero(struct btrfs_trans_handle *trans, |
| 4527 | struct btrfs_root *extent_root, |
| 4528 | struct btrfs_path *path, |
| 4529 | struct btrfs_key *extent_key) |
| 4530 | { |
| 4531 | int ret; |
| 4532 | |
| 4533 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
| 4534 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 4535 | if (ret) |
| 4536 | goto out; |
| 4537 | ret = btrfs_del_item(trans, extent_root, path); |
| 4538 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4539 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4540 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 4541 | return ret; |
| 4542 | } |
| 4543 | |
| 4544 | static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info, |
| 4545 | struct btrfs_ref_path *ref_path) |
| 4546 | { |
| 4547 | struct btrfs_key root_key; |
| 4548 | |
| 4549 | root_key.objectid = ref_path->root_objectid; |
| 4550 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 4551 | if (is_cowonly_root(ref_path->root_objectid)) |
| 4552 | root_key.offset = 0; |
| 4553 | else |
| 4554 | root_key.offset = (u64)-1; |
| 4555 | |
| 4556 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 4557 | } |
| 4558 | |
| 4559 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
| 4560 | struct btrfs_path *path, |
| 4561 | struct btrfs_key *extent_key, |
| 4562 | struct btrfs_block_group_cache *group, |
| 4563 | struct inode *reloc_inode, int pass) |
| 4564 | { |
| 4565 | struct btrfs_trans_handle *trans; |
| 4566 | struct btrfs_root *found_root; |
| 4567 | struct btrfs_ref_path *ref_path = NULL; |
| 4568 | struct disk_extent *new_extents = NULL; |
| 4569 | int nr_extents = 0; |
| 4570 | int loops; |
| 4571 | int ret; |
| 4572 | int level; |
| 4573 | struct btrfs_key first_key; |
| 4574 | u64 prev_block = 0; |
| 4575 | |
| 4576 | mutex_unlock(&extent_root->fs_info->alloc_mutex); |
| 4577 | |
| 4578 | trans = btrfs_start_transaction(extent_root, 1); |
| 4579 | BUG_ON(!trans); |
| 4580 | |
| 4581 | if (extent_key->objectid == 0) { |
| 4582 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 4583 | goto out; |
| 4584 | } |
| 4585 | |
| 4586 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 4587 | if (!ref_path) { |
| 4588 | ret = -ENOMEM; |
| 4589 | goto out; |
| 4590 | } |
| 4591 | |
| 4592 | for (loops = 0; ; loops++) { |
| 4593 | if (loops == 0) { |
| 4594 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 4595 | extent_key->objectid); |
| 4596 | } else { |
| 4597 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 4598 | } |
| 4599 | if (ret < 0) |
| 4600 | goto out; |
| 4601 | if (ret > 0) |
| 4602 | break; |
| 4603 | |
| 4604 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 4605 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 4606 | continue; |
| 4607 | |
| 4608 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 4609 | BUG_ON(!found_root); |
| 4610 | /* |
| 4611 | * for reference counted tree, only process reference paths |
| 4612 | * rooted at the latest committed root. |
| 4613 | */ |
| 4614 | if (found_root->ref_cows && |
| 4615 | ref_path->root_generation != found_root->root_key.offset) |
| 4616 | continue; |
| 4617 | |
| 4618 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 4619 | if (pass == 0) { |
| 4620 | /* |
| 4621 | * copy data extents to new locations |
| 4622 | */ |
| 4623 | u64 group_start = group->key.objectid; |
| 4624 | ret = relocate_data_extent(reloc_inode, |
| 4625 | extent_key, |
| 4626 | group_start); |
| 4627 | if (ret < 0) |
| 4628 | goto out; |
| 4629 | break; |
| 4630 | } |
| 4631 | level = 0; |
| 4632 | } else { |
| 4633 | level = ref_path->owner_objectid; |
| 4634 | } |
| 4635 | |
| 4636 | if (prev_block != ref_path->nodes[level]) { |
| 4637 | struct extent_buffer *eb; |
| 4638 | u64 block_start = ref_path->nodes[level]; |
| 4639 | u64 block_size = btrfs_level_size(found_root, level); |
| 4640 | |
| 4641 | eb = read_tree_block(found_root, block_start, |
| 4642 | block_size, 0); |
| 4643 | btrfs_tree_lock(eb); |
| 4644 | BUG_ON(level != btrfs_header_level(eb)); |
| 4645 | |
| 4646 | if (level == 0) |
| 4647 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 4648 | else |
| 4649 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 4650 | |
| 4651 | btrfs_tree_unlock(eb); |
| 4652 | free_extent_buffer(eb); |
| 4653 | prev_block = block_start; |
| 4654 | } |
| 4655 | |
| 4656 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID && |
| 4657 | pass >= 2) { |
| 4658 | /* |
| 4659 | * use fallback method to process the remaining |
| 4660 | * references. |
| 4661 | */ |
| 4662 | if (!new_extents) { |
| 4663 | u64 group_start = group->key.objectid; |
| 4664 | ret = get_new_locations(reloc_inode, |
| 4665 | extent_key, |
| 4666 | group_start, 0, |
| 4667 | &new_extents, |
| 4668 | &nr_extents); |
| 4669 | if (ret < 0) |
| 4670 | goto out; |
| 4671 | } |
| 4672 | btrfs_record_root_in_trans(found_root); |
| 4673 | ret = replace_one_extent(trans, found_root, |
| 4674 | path, extent_key, |
| 4675 | &first_key, ref_path, |
| 4676 | new_extents, nr_extents); |
| 4677 | if (ret < 0) |
| 4678 | goto out; |
| 4679 | continue; |
| 4680 | } |
| 4681 | |
| 4682 | btrfs_record_root_in_trans(found_root); |
| 4683 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4684 | ret = relocate_tree_block(trans, found_root, path, |
| 4685 | &first_key, ref_path); |
| 4686 | } else { |
| 4687 | /* |
| 4688 | * try to update data extent references while |
| 4689 | * keeping metadata shared between snapshots. |
| 4690 | */ |
| 4691 | ret = relocate_one_path(trans, found_root, path, |
| 4692 | &first_key, ref_path, |
| 4693 | group, reloc_inode); |
| 4694 | } |
| 4695 | if (ret < 0) |
| 4696 | goto out; |
| 4697 | } |
| 4698 | ret = 0; |
| 4699 | out: |
| 4700 | btrfs_end_transaction(trans, extent_root); |
| 4701 | kfree(new_extents); |
| 4702 | kfree(ref_path); |
| 4703 | mutex_lock(&extent_root->fs_info->alloc_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4704 | return ret; |
| 4705 | } |
| 4706 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4707 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 4708 | { |
| 4709 | u64 num_devices; |
| 4710 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 4711 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 4712 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4713 | num_devices = root->fs_info->fs_devices->num_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4714 | if (num_devices == 1) { |
| 4715 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 4716 | stripped = flags & ~stripped; |
| 4717 | |
| 4718 | /* turn raid0 into single device chunks */ |
| 4719 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4720 | return stripped; |
| 4721 | |
| 4722 | /* turn mirroring into duplication */ |
| 4723 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4724 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4725 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 4726 | return flags; |
| 4727 | } else { |
| 4728 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4729 | if (flags & stripped) |
| 4730 | return flags; |
| 4731 | |
| 4732 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 4733 | stripped = flags & ~stripped; |
| 4734 | |
| 4735 | /* switch duplicated blocks with raid1 */ |
| 4736 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 4737 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 4738 | |
| 4739 | /* turn single device chunks into raid0 */ |
| 4740 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 4741 | } |
| 4742 | return flags; |
| 4743 | } |
| 4744 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4745 | int __alloc_chunk_for_shrink(struct btrfs_root *root, |
| 4746 | struct btrfs_block_group_cache *shrink_block_group, |
| 4747 | int force) |
| 4748 | { |
| 4749 | struct btrfs_trans_handle *trans; |
| 4750 | u64 new_alloc_flags; |
| 4751 | u64 calc; |
| 4752 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4753 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4754 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4755 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4756 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4757 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4758 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4759 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4760 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4761 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4762 | new_alloc_flags = update_block_group_flags(root, |
| 4763 | shrink_block_group->flags); |
| 4764 | if (new_alloc_flags != shrink_block_group->flags) { |
| 4765 | calc = |
| 4766 | btrfs_block_group_used(&shrink_block_group->item); |
| 4767 | } else { |
| 4768 | calc = shrink_block_group->key.offset; |
| 4769 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4770 | spin_unlock(&shrink_block_group->lock); |
| 4771 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4772 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 4773 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4774 | |
| 4775 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4776 | btrfs_end_transaction(trans, root); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4777 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4778 | } else |
| 4779 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4780 | return 0; |
| 4781 | } |
| 4782 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4783 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 4784 | struct btrfs_root *root, |
| 4785 | u64 objectid, u64 size) |
| 4786 | { |
| 4787 | struct btrfs_path *path; |
| 4788 | struct btrfs_inode_item *item; |
| 4789 | struct extent_buffer *leaf; |
| 4790 | int ret; |
| 4791 | |
| 4792 | path = btrfs_alloc_path(); |
| 4793 | if (!path) |
| 4794 | return -ENOMEM; |
| 4795 | |
| 4796 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 4797 | if (ret) |
| 4798 | goto out; |
| 4799 | |
| 4800 | leaf = path->nodes[0]; |
| 4801 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 4802 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 4803 | btrfs_set_inode_generation(leaf, item, 1); |
| 4804 | btrfs_set_inode_size(leaf, item, size); |
| 4805 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
| 4806 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM); |
| 4807 | btrfs_mark_buffer_dirty(leaf); |
| 4808 | btrfs_release_path(root, path); |
| 4809 | out: |
| 4810 | btrfs_free_path(path); |
| 4811 | return ret; |
| 4812 | } |
| 4813 | |
| 4814 | static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info, |
| 4815 | struct btrfs_block_group_cache *group) |
| 4816 | { |
| 4817 | struct inode *inode = NULL; |
| 4818 | struct btrfs_trans_handle *trans; |
| 4819 | struct btrfs_root *root; |
| 4820 | struct btrfs_key root_key; |
| 4821 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 4822 | int err = 0; |
| 4823 | |
| 4824 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 4825 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 4826 | root_key.offset = (u64)-1; |
| 4827 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 4828 | if (IS_ERR(root)) |
| 4829 | return ERR_CAST(root); |
| 4830 | |
| 4831 | trans = btrfs_start_transaction(root, 1); |
| 4832 | BUG_ON(!trans); |
| 4833 | |
| 4834 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 4835 | if (err) |
| 4836 | goto out; |
| 4837 | |
| 4838 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 4839 | BUG_ON(err); |
| 4840 | |
| 4841 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
| 4842 | group->key.offset, 0); |
| 4843 | BUG_ON(err); |
| 4844 | |
| 4845 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 4846 | if (inode->i_state & I_NEW) { |
| 4847 | BTRFS_I(inode)->root = root; |
| 4848 | BTRFS_I(inode)->location.objectid = objectid; |
| 4849 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 4850 | BTRFS_I(inode)->location.offset = 0; |
| 4851 | btrfs_read_locked_inode(inode); |
| 4852 | unlock_new_inode(inode); |
| 4853 | BUG_ON(is_bad_inode(inode)); |
| 4854 | } else { |
| 4855 | BUG_ON(1); |
| 4856 | } |
| 4857 | |
| 4858 | err = btrfs_orphan_add(trans, inode); |
| 4859 | out: |
| 4860 | btrfs_end_transaction(trans, root); |
| 4861 | if (err) { |
| 4862 | if (inode) |
| 4863 | iput(inode); |
| 4864 | inode = ERR_PTR(err); |
| 4865 | } |
| 4866 | return inode; |
| 4867 | } |
| 4868 | |
| 4869 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4870 | { |
| 4871 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4872 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4873 | struct btrfs_fs_info *info = root->fs_info; |
| 4874 | struct extent_buffer *leaf; |
| 4875 | struct inode *reloc_inode; |
| 4876 | struct btrfs_block_group_cache *block_group; |
| 4877 | struct btrfs_key key; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4878 | u64 cur_byte; |
| 4879 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4880 | u32 nritems; |
| 4881 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4882 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4883 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4884 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4885 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4886 | |
| 4887 | block_group = btrfs_lookup_block_group(info, group_start); |
| 4888 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4889 | |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 4890 | printk("btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4891 | (unsigned long long)block_group->key.objectid, |
| 4892 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 4893 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4894 | path = btrfs_alloc_path(); |
| 4895 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 4896 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4897 | reloc_inode = create_reloc_inode(info, block_group); |
| 4898 | BUG_ON(IS_ERR(reloc_inode)); |
| 4899 | |
| 4900 | mutex_lock(&root->fs_info->alloc_mutex); |
| 4901 | |
| 4902 | __alloc_chunk_for_shrink(root, block_group, 1); |
| 4903 | block_group->ro = 1; |
| 4904 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 4905 | |
| 4906 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 4907 | |
| 4908 | btrfs_start_delalloc_inodes(info->tree_root); |
| 4909 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4910 | again: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4911 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4912 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4913 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4914 | key.offset = 0; |
| 4915 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4916 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4917 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4918 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 4919 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4920 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4921 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 4922 | btrfs_clean_old_snapshots(info->tree_root); |
| 4923 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 4924 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4925 | |
| 4926 | mutex_lock(&root->fs_info->alloc_mutex); |
| 4927 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4928 | while(1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4929 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4930 | if (ret < 0) |
| 4931 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4932 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4933 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4934 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4935 | if (path->slots[0] >= nritems) { |
| 4936 | ret = btrfs_next_leaf(root, path); |
| 4937 | if (ret < 0) |
| 4938 | goto out; |
| 4939 | if (ret == 1) { |
| 4940 | ret = 0; |
| 4941 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4942 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4943 | leaf = path->nodes[0]; |
| 4944 | nritems = btrfs_header_nritems(leaf); |
| 4945 | } |
| 4946 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4947 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4948 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4949 | if (key.objectid >= block_group->key.objectid + |
| 4950 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4951 | break; |
| 4952 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4953 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4954 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4955 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 4956 | cond_resched(); |
| 4957 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4958 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4959 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4960 | } |
| 4961 | progress = 1; |
| 4962 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4963 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 4964 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4965 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4966 | goto next; |
| 4967 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4968 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4969 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4970 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4971 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4972 | |
| 4973 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 4974 | ret = relocate_one_extent(root, path, &key, block_group, |
| 4975 | reloc_inode, pass); |
| 4976 | BUG_ON(ret < 0); |
| 4977 | |
| 4978 | key.objectid = cur_byte; |
| 4979 | key.type = 0; |
| 4980 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4981 | } |
| 4982 | |
| 4983 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4984 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 4985 | |
| 4986 | if (pass == 0) { |
| 4987 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 4988 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
| 4989 | WARN_ON(reloc_inode->i_mapping->nrpages); |
| 4990 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4991 | |
| 4992 | if (total_found > 0) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4993 | printk("btrfs found %llu extents in pass %d\n", |
| 4994 | (unsigned long long)total_found, pass); |
| 4995 | pass++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4996 | goto again; |
| 4997 | } |
| 4998 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 4999 | /* delete reloc_inode */ |
| 5000 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5001 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 5002 | /* unpin extents in this range */ |
| 5003 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5004 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5005 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5006 | mutex_lock(&root->fs_info->alloc_mutex); |
| 5007 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 5008 | spin_lock(&block_group->lock); |
| 5009 | WARN_ON(block_group->pinned > 0); |
| 5010 | WARN_ON(block_group->reserved > 0); |
| 5011 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 5012 | spin_unlock(&block_group->lock); |
| 5013 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5014 | out: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5015 | mutex_unlock(&root->fs_info->alloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 5016 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5017 | return ret; |
| 5018 | } |
| 5019 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5020 | int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, |
| 5021 | struct btrfs_key *key) |
| 5022 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5023 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5024 | struct btrfs_key found_key; |
| 5025 | struct extent_buffer *leaf; |
| 5026 | int slot; |
| 5027 | |
| 5028 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 5029 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5030 | goto out; |
| 5031 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5032 | while(1) { |
| 5033 | slot = path->slots[0]; |
| 5034 | leaf = path->nodes[0]; |
| 5035 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5036 | ret = btrfs_next_leaf(root, path); |
| 5037 | if (ret == 0) |
| 5038 | continue; |
| 5039 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5040 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5041 | break; |
| 5042 | } |
| 5043 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5044 | |
| 5045 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5046 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 5047 | ret = 0; |
| 5048 | goto out; |
| 5049 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5050 | path->slots[0]++; |
| 5051 | } |
| 5052 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5053 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5054 | return ret; |
| 5055 | } |
| 5056 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 5057 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 5058 | { |
| 5059 | struct btrfs_block_group_cache *block_group; |
| 5060 | struct rb_node *n; |
| 5061 | |
| 5062 | mutex_lock(&info->alloc_mutex); |
| 5063 | spin_lock(&info->block_group_cache_lock); |
| 5064 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 5065 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 5066 | cache_node); |
| 5067 | |
| 5068 | spin_unlock(&info->block_group_cache_lock); |
| 5069 | btrfs_remove_free_space_cache(block_group); |
| 5070 | spin_lock(&info->block_group_cache_lock); |
| 5071 | |
| 5072 | rb_erase(&block_group->cache_node, |
| 5073 | &info->block_group_cache_tree); |
| 5074 | spin_lock(&block_group->space_info->lock); |
| 5075 | list_del(&block_group->list); |
| 5076 | spin_unlock(&block_group->space_info->lock); |
| 5077 | kfree(block_group); |
| 5078 | } |
| 5079 | spin_unlock(&info->block_group_cache_lock); |
| 5080 | mutex_unlock(&info->alloc_mutex); |
| 5081 | return 0; |
| 5082 | } |
| 5083 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5084 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 5085 | { |
| 5086 | struct btrfs_path *path; |
| 5087 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5088 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5089 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5090 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5091 | struct btrfs_key key; |
| 5092 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5093 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5094 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5095 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5096 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5097 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5098 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5099 | path = btrfs_alloc_path(); |
| 5100 | if (!path) |
| 5101 | return -ENOMEM; |
| 5102 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5103 | mutex_lock(&root->fs_info->alloc_mutex); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5104 | while(1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5105 | ret = find_first_block_group(root, path, &key); |
| 5106 | if (ret > 0) { |
| 5107 | ret = 0; |
| 5108 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5109 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5110 | if (ret != 0) |
| 5111 | goto error; |
| 5112 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5113 | leaf = path->nodes[0]; |
| 5114 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5115 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5116 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5117 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5118 | break; |
| 5119 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5120 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5121 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5122 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5123 | read_extent_buffer(leaf, &cache->item, |
| 5124 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 5125 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5126 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5127 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5128 | key.objectid = found_key.objectid + found_key.offset; |
| 5129 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5130 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5131 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5132 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 5133 | btrfs_block_group_used(&cache->item), |
| 5134 | &space_info); |
| 5135 | BUG_ON(ret); |
| 5136 | cache->space_info = space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5137 | spin_lock(&space_info->lock); |
| 5138 | list_add(&cache->list, &space_info->block_groups); |
| 5139 | spin_unlock(&space_info->lock); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5140 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5141 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5142 | BUG_ON(ret); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5143 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5144 | ret = 0; |
| 5145 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5146 | btrfs_free_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5147 | mutex_unlock(&root->fs_info->alloc_mutex); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5148 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5149 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5150 | |
| 5151 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 5152 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5153 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5154 | u64 size) |
| 5155 | { |
| 5156 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5157 | struct btrfs_root *extent_root; |
| 5158 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5159 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5160 | WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5161 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5162 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5163 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
| 5164 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5165 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5166 | if (!cache) |
| 5167 | return -ENOMEM; |
| 5168 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5169 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5170 | cache->key.offset = size; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5171 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5172 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5173 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5174 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5175 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5176 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 5177 | cache->flags = type; |
| 5178 | btrfs_set_block_group_flags(&cache->item, type); |
| 5179 | |
| 5180 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 5181 | &cache->space_info); |
| 5182 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5183 | spin_lock(&cache->space_info->lock); |
| 5184 | list_add(&cache->list, &cache->space_info->block_groups); |
| 5185 | spin_unlock(&cache->space_info->lock); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5186 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5187 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5188 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5189 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5190 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 5191 | sizeof(cache->item)); |
| 5192 | BUG_ON(ret); |
| 5193 | |
| 5194 | finish_current_insert(trans, extent_root); |
| 5195 | ret = del_pending_extents(trans, extent_root); |
| 5196 | BUG_ON(ret); |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 5197 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5198 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5199 | return 0; |
| 5200 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame^] | 5201 | |
| 5202 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 5203 | struct btrfs_root *root, u64 group_start) |
| 5204 | { |
| 5205 | struct btrfs_path *path; |
| 5206 | struct btrfs_block_group_cache *block_group; |
| 5207 | struct btrfs_key key; |
| 5208 | int ret; |
| 5209 | |
| 5210 | BUG_ON(!mutex_is_locked(&root->fs_info->alloc_mutex)); |
| 5211 | root = root->fs_info->extent_root; |
| 5212 | |
| 5213 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 5214 | BUG_ON(!block_group); |
| 5215 | |
| 5216 | memcpy(&key, &block_group->key, sizeof(key)); |
| 5217 | |
| 5218 | path = btrfs_alloc_path(); |
| 5219 | BUG_ON(!path); |
| 5220 | |
| 5221 | btrfs_remove_free_space_cache(block_group); |
| 5222 | rb_erase(&block_group->cache_node, |
| 5223 | &root->fs_info->block_group_cache_tree); |
| 5224 | spin_lock(&block_group->space_info->lock); |
| 5225 | list_del(&block_group->list); |
| 5226 | spin_unlock(&block_group->space_info->lock); |
| 5227 | |
| 5228 | /* |
| 5229 | memset(shrink_block_group, 0, sizeof(*shrink_block_group)); |
| 5230 | kfree(shrink_block_group); |
| 5231 | */ |
| 5232 | |
| 5233 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 5234 | if (ret > 0) |
| 5235 | ret = -EIO; |
| 5236 | if (ret < 0) |
| 5237 | goto out; |
| 5238 | |
| 5239 | ret = btrfs_del_item(trans, root, path); |
| 5240 | out: |
| 5241 | btrfs_free_path(path); |
| 5242 | return ret; |
| 5243 | } |