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