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 |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 48 | btrfs_root *extent_root, int all); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 49 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 50 | btrfs_root *extent_root, int all); |
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 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 56 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 57 | { |
| 58 | return (cache->flags & bits) == bits; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * this adds the block group to the fs_info rb tree for the block group |
| 63 | * cache |
| 64 | */ |
| 65 | int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
| 66 | struct btrfs_block_group_cache *block_group) |
| 67 | { |
| 68 | struct rb_node **p; |
| 69 | struct rb_node *parent = NULL; |
| 70 | struct btrfs_block_group_cache *cache; |
| 71 | |
| 72 | spin_lock(&info->block_group_cache_lock); |
| 73 | p = &info->block_group_cache_tree.rb_node; |
| 74 | |
| 75 | while (*p) { |
| 76 | parent = *p; |
| 77 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 78 | cache_node); |
| 79 | if (block_group->key.objectid < cache->key.objectid) { |
| 80 | p = &(*p)->rb_left; |
| 81 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 82 | p = &(*p)->rb_right; |
| 83 | } else { |
| 84 | spin_unlock(&info->block_group_cache_lock); |
| 85 | return -EEXIST; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | rb_link_node(&block_group->cache_node, parent, p); |
| 90 | rb_insert_color(&block_group->cache_node, |
| 91 | &info->block_group_cache_tree); |
| 92 | spin_unlock(&info->block_group_cache_lock); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * This will return the block group at or after bytenr if contains is 0, else |
| 99 | * it will return the block group that contains the bytenr |
| 100 | */ |
| 101 | static struct btrfs_block_group_cache * |
| 102 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 103 | int contains) |
| 104 | { |
| 105 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 106 | struct rb_node *n; |
| 107 | u64 end, start; |
| 108 | |
| 109 | spin_lock(&info->block_group_cache_lock); |
| 110 | n = info->block_group_cache_tree.rb_node; |
| 111 | |
| 112 | while (n) { |
| 113 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 114 | cache_node); |
| 115 | end = cache->key.objectid + cache->key.offset - 1; |
| 116 | start = cache->key.objectid; |
| 117 | |
| 118 | if (bytenr < start) { |
| 119 | if (!contains && (!ret || start < ret->key.objectid)) |
| 120 | ret = cache; |
| 121 | n = n->rb_left; |
| 122 | } else if (bytenr > start) { |
| 123 | if (contains && bytenr <= end) { |
| 124 | ret = cache; |
| 125 | break; |
| 126 | } |
| 127 | n = n->rb_right; |
| 128 | } else { |
| 129 | ret = cache; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | spin_unlock(&info->block_group_cache_lock); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * this is only called by cache_block_group, since we could have freed extents |
| 140 | * we need to check the pinned_extents for any extents that can't be used yet |
| 141 | * since their free space will be released as soon as the transaction commits. |
| 142 | */ |
| 143 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 144 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 145 | { |
| 146 | u64 extent_start, extent_end, size; |
| 147 | int ret; |
| 148 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 149 | mutex_lock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 150 | while (start < end) { |
| 151 | ret = find_first_extent_bit(&info->pinned_extents, start, |
| 152 | &extent_start, &extent_end, |
| 153 | EXTENT_DIRTY); |
| 154 | if (ret) |
| 155 | break; |
| 156 | |
| 157 | if (extent_start == start) { |
| 158 | start = extent_end + 1; |
| 159 | } else if (extent_start > start && extent_start < end) { |
| 160 | size = extent_start - start; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 161 | ret = btrfs_add_free_space_lock(block_group, start, |
| 162 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 163 | BUG_ON(ret); |
| 164 | start = extent_end + 1; |
| 165 | } else { |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (start < end) { |
| 171 | size = end - start; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 172 | ret = btrfs_add_free_space_lock(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 173 | BUG_ON(ret); |
| 174 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 175 | mutex_unlock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 180 | static int cache_block_group(struct btrfs_root *root, |
| 181 | struct btrfs_block_group_cache *block_group) |
| 182 | { |
| 183 | struct btrfs_path *path; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 184 | int ret = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 185 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 186 | struct extent_buffer *leaf; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 187 | int slot; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 188 | u64 last = 0; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 189 | u64 first_free; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 190 | int found = 0; |
| 191 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 192 | if (!block_group) |
| 193 | return 0; |
| 194 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 195 | root = root->fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 196 | |
| 197 | if (block_group->cached) |
| 198 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 199 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 200 | path = btrfs_alloc_path(); |
| 201 | if (!path) |
| 202 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 203 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 204 | path->reada = 2; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 205 | /* |
| 206 | * we get into deadlocks with paths held by callers of this function. |
| 207 | * since the alloc_mutex is protecting things right now, just |
| 208 | * skip the locking here |
| 209 | */ |
| 210 | path->skip_locking = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 211 | first_free = max_t(u64, block_group->key.objectid, |
| 212 | BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 213 | key.objectid = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 214 | key.offset = 0; |
| 215 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 216 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 217 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 218 | goto err; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 219 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 220 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 221 | goto err; |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 222 | if (ret == 0) { |
| 223 | leaf = path->nodes[0]; |
| 224 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 225 | if (key.objectid + key.offset > first_free) |
| 226 | first_free = key.objectid + key.offset; |
| 227 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 228 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 229 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 230 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 231 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 232 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 233 | if (ret < 0) |
| 234 | goto err; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 235 | if (ret == 0) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 236 | continue; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 237 | else |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 238 | break; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 239 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 240 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 241 | if (key.objectid < block_group->key.objectid) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 242 | goto next; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 243 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 244 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 245 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 246 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 247 | |
| 248 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
| 249 | if (!found) { |
| 250 | last = first_free; |
| 251 | found = 1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 252 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 253 | |
| 254 | add_new_free_space(block_group, root->fs_info, last, |
| 255 | key.objectid); |
| 256 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 257 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 258 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 259 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 260 | path->slots[0]++; |
| 261 | } |
| 262 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 263 | if (!found) |
| 264 | last = first_free; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 265 | |
| 266 | add_new_free_space(block_group, root->fs_info, last, |
| 267 | block_group->key.objectid + |
| 268 | block_group->key.offset); |
| 269 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 270 | block_group->cached = 1; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 271 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 272 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 273 | btrfs_free_path(path); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 274 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 277 | /* |
| 278 | * return the block group that starts at or after bytenr |
| 279 | */ |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 280 | struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct |
| 281 | btrfs_fs_info *info, |
| 282 | u64 bytenr) |
| 283 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 284 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 285 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 286 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 287 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 288 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 291 | /* |
| 292 | * return the block group that contains teh given bytenr |
| 293 | */ |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 294 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
| 295 | btrfs_fs_info *info, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 296 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 297 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 298 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 299 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 300 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 301 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 302 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 303 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 304 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 305 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 306 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 307 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 308 | struct list_head *head = &info->space_info; |
| 309 | struct list_head *cur; |
| 310 | struct btrfs_space_info *found; |
| 311 | list_for_each(cur, head) { |
| 312 | found = list_entry(cur, struct btrfs_space_info, list); |
| 313 | if (found->flags == flags) |
| 314 | return found; |
| 315 | } |
| 316 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 319 | static u64 div_factor(u64 num, int factor) |
| 320 | { |
| 321 | if (factor == 10) |
| 322 | return num; |
| 323 | num *= factor; |
| 324 | do_div(num, 10); |
| 325 | return num; |
| 326 | } |
| 327 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 328 | static struct btrfs_block_group_cache * |
| 329 | __btrfs_find_block_group(struct btrfs_root *root, |
| 330 | struct btrfs_block_group_cache *hint, |
| 331 | u64 search_start, int data, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 332 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 333 | struct btrfs_block_group_cache *cache; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 334 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 335 | struct btrfs_fs_info *info = root->fs_info; |
| 336 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 337 | u64 last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 338 | u64 free_check; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 339 | int full_search = 0; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 340 | int factor = 10; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 341 | int wrapped = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 342 | |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 343 | if (data & BTRFS_BLOCK_GROUP_METADATA) |
| 344 | factor = 9; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 345 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 346 | if (search_start) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 347 | struct btrfs_block_group_cache *shint; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 348 | shint = btrfs_lookup_first_block_group(info, search_start); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 349 | if (shint && block_group_bits(shint, data) && !shint->ro) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 350 | spin_lock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 351 | used = btrfs_block_group_used(&shint->item); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 352 | if (used + shint->pinned + shint->reserved < |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 353 | div_factor(shint->key.offset, factor)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 354 | spin_unlock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 355 | return shint; |
| 356 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 357 | spin_unlock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 358 | } |
| 359 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 360 | if (hint && !hint->ro && block_group_bits(hint, data)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 361 | spin_lock(&hint->lock); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 362 | used = btrfs_block_group_used(&hint->item); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 363 | if (used + hint->pinned + hint->reserved < |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 364 | div_factor(hint->key.offset, factor)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 365 | spin_unlock(&hint->lock); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 366 | return hint; |
| 367 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 368 | spin_unlock(&hint->lock); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 369 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 370 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 371 | if (hint) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 372 | last = max(hint->key.objectid, search_start); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 373 | else |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 374 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 375 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 376 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 377 | while (1) { |
| 378 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 379 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 380 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 381 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 382 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 383 | last = cache->key.objectid + cache->key.offset; |
| 384 | used = btrfs_block_group_used(&cache->item); |
| 385 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 386 | if (!cache->ro && block_group_bits(cache, data)) { |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 387 | free_check = div_factor(cache->key.offset, factor); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 388 | if (used + cache->pinned + cache->reserved < |
| 389 | free_check) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 390 | found_group = cache; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 391 | spin_unlock(&cache->lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 392 | goto found; |
| 393 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 394 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 395 | spin_unlock(&cache->lock); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 396 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 397 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 398 | if (!wrapped) { |
| 399 | last = search_start; |
| 400 | wrapped = 1; |
| 401 | goto again; |
| 402 | } |
| 403 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 404 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 405 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 406 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 407 | goto again; |
| 408 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 409 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 410 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 413 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 414 | struct btrfs_block_group_cache |
| 415 | *hint, u64 search_start, |
| 416 | int data, int owner) |
| 417 | { |
| 418 | |
| 419 | struct btrfs_block_group_cache *ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 420 | ret = __btrfs_find_block_group(root, hint, search_start, data, owner); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 421 | return ret; |
| 422 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 423 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 424 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 425 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 426 | { |
| 427 | int ret; |
| 428 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 429 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 430 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 431 | path = btrfs_alloc_path(); |
| 432 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 433 | key.objectid = start; |
| 434 | key.offset = len; |
| 435 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 436 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 437 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 438 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 439 | return ret; |
| 440 | } |
| 441 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 442 | /* |
| 443 | * Back reference rules. Back refs have three main goals: |
| 444 | * |
| 445 | * 1) differentiate between all holders of references to an extent so that |
| 446 | * when a reference is dropped we can make sure it was a valid reference |
| 447 | * before freeing the extent. |
| 448 | * |
| 449 | * 2) Provide enough information to quickly find the holders of an extent |
| 450 | * if we notice a given block is corrupted or bad. |
| 451 | * |
| 452 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 453 | * maintenance. This is actually the same as #2, but with a slightly |
| 454 | * different use case. |
| 455 | * |
| 456 | * File extents can be referenced by: |
| 457 | * |
| 458 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 459 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 460 | * - different offsets inside a file (bookend extents in file.c) |
| 461 | * |
| 462 | * The extent ref structure has fields for: |
| 463 | * |
| 464 | * - Objectid of the subvolume root |
| 465 | * - Generation number of the tree holding the reference |
| 466 | * - objectid of the file holding the reference |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 467 | * - number of references holding by parent node (alway 1 for tree blocks) |
| 468 | * |
| 469 | * Btree leaf may hold multiple references to a file extent. In most cases, |
| 470 | * these references are from same file and the corresponding offsets inside |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 471 | * the file are close together. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 472 | * |
| 473 | * When a file extent is allocated the fields are filled in: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 474 | * (root_key.objectid, trans->transid, inode objectid, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 475 | * |
| 476 | * 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] | 477 | * in the leaf. It looks similar to the create case, but trans->transid will |
| 478 | * be different when the block is cow'd. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 479 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 480 | * (root_key.objectid, trans->transid, inode objectid, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 481 | * number of references in the leaf) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 482 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 483 | * When a file extent is removed either during snapshot deletion or |
| 484 | * file truncation, we find the corresponding back reference and check |
| 485 | * the following fields: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 486 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 487 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 488 | * inode objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 489 | * |
| 490 | * Btree extents can be referenced by: |
| 491 | * |
| 492 | * - Different subvolumes |
| 493 | * - Different generations of the same subvolume |
| 494 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 495 | * When a tree block is created, back references are inserted: |
| 496 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 497 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 498 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 499 | * When a tree block is cow'd, new back references are added for all the |
| 500 | * blocks it points to. If the tree block isn't in reference counted root, |
| 501 | * the old back references are removed. These new back references are of |
| 502 | * the form (trans->transid will have increased since creation): |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 503 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 504 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 505 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 506 | * When a backref is in deleting, the following fields are checked: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 507 | * |
| 508 | * if backref was for a tree root: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 509 | * (btrfs_header_owner(itself), btrfs_header_generation(itself), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 510 | * else |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 511 | * (btrfs_header_owner(parent), btrfs_header_generation(parent), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 512 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 513 | * Back Reference Key composing: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 514 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 515 | * The key objectid corresponds to the first byte in the extent, the key |
| 516 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first |
| 517 | * byte of parent extent. If a extent is tree root, the key offset is set |
| 518 | * to the key objectid. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 519 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 520 | |
| 521 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 522 | struct btrfs_root *root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 523 | struct btrfs_path *path, |
| 524 | u64 bytenr, u64 parent, |
| 525 | u64 ref_root, u64 ref_generation, |
| 526 | u64 owner_objectid, int del) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 527 | { |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 528 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 529 | struct btrfs_extent_ref *ref; |
| 530 | struct extent_buffer *leaf; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 531 | u64 ref_objectid; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 532 | int ret; |
| 533 | |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 534 | key.objectid = bytenr; |
| 535 | key.type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 536 | key.offset = parent; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 537 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 538 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); |
| 539 | if (ret < 0) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 540 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 541 | if (ret > 0) { |
| 542 | ret = -ENOENT; |
| 543 | goto out; |
| 544 | } |
| 545 | |
| 546 | leaf = path->nodes[0]; |
| 547 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 548 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 549 | if (btrfs_ref_root(leaf, ref) != ref_root || |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 550 | btrfs_ref_generation(leaf, ref) != ref_generation || |
| 551 | (ref_objectid != owner_objectid && |
| 552 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 553 | ret = -EIO; |
| 554 | WARN_ON(1); |
| 555 | goto out; |
| 556 | } |
| 557 | ret = 0; |
| 558 | out: |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | static int noinline insert_extent_backref(struct btrfs_trans_handle *trans, |
| 563 | struct btrfs_root *root, |
| 564 | struct btrfs_path *path, |
| 565 | u64 bytenr, u64 parent, |
| 566 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 567 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 568 | { |
| 569 | struct btrfs_key key; |
| 570 | struct extent_buffer *leaf; |
| 571 | struct btrfs_extent_ref *ref; |
| 572 | u32 num_refs; |
| 573 | int ret; |
| 574 | |
| 575 | key.objectid = bytenr; |
| 576 | key.type = BTRFS_EXTENT_REF_KEY; |
| 577 | key.offset = parent; |
| 578 | |
| 579 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
| 580 | if (ret == 0) { |
| 581 | leaf = path->nodes[0]; |
| 582 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 583 | struct btrfs_extent_ref); |
| 584 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 585 | btrfs_set_ref_generation(leaf, ref, ref_generation); |
| 586 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 587 | btrfs_set_ref_num_refs(leaf, ref, 1); |
| 588 | } else if (ret == -EEXIST) { |
| 589 | u64 existing_owner; |
| 590 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); |
| 591 | leaf = path->nodes[0]; |
| 592 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 593 | struct btrfs_extent_ref); |
| 594 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 595 | btrfs_ref_generation(leaf, ref) != ref_generation) { |
| 596 | ret = -EIO; |
| 597 | WARN_ON(1); |
| 598 | goto out; |
| 599 | } |
| 600 | |
| 601 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 602 | BUG_ON(num_refs == 0); |
| 603 | btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); |
| 604 | |
| 605 | existing_owner = btrfs_ref_objectid(leaf, ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 606 | if (existing_owner != owner_objectid && |
| 607 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 608 | btrfs_set_ref_objectid(leaf, ref, |
| 609 | BTRFS_MULTIPLE_OBJECTIDS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 610 | } |
| 611 | ret = 0; |
| 612 | } else { |
| 613 | goto out; |
| 614 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 615 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 616 | out: |
| 617 | btrfs_release_path(root, path); |
| 618 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 619 | } |
| 620 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 621 | static int noinline remove_extent_backref(struct btrfs_trans_handle *trans, |
| 622 | struct btrfs_root *root, |
| 623 | struct btrfs_path *path) |
| 624 | { |
| 625 | struct extent_buffer *leaf; |
| 626 | struct btrfs_extent_ref *ref; |
| 627 | u32 num_refs; |
| 628 | int ret = 0; |
| 629 | |
| 630 | leaf = path->nodes[0]; |
| 631 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 632 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 633 | BUG_ON(num_refs == 0); |
| 634 | num_refs -= 1; |
| 635 | if (num_refs == 0) { |
| 636 | ret = btrfs_del_item(trans, root, path); |
| 637 | } else { |
| 638 | btrfs_set_ref_num_refs(leaf, ref, num_refs); |
| 639 | btrfs_mark_buffer_dirty(leaf); |
| 640 | } |
| 641 | btrfs_release_path(root, path); |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 646 | struct btrfs_root *root, u64 bytenr, |
| 647 | u64 orig_parent, u64 parent, |
| 648 | u64 orig_root, u64 ref_root, |
| 649 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 650 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 651 | { |
| 652 | int ret; |
| 653 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 654 | struct btrfs_path *path; |
| 655 | |
| 656 | if (root == root->fs_info->extent_root) { |
| 657 | struct pending_extent_op *extent_op; |
| 658 | u64 num_bytes; |
| 659 | |
| 660 | BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL); |
| 661 | num_bytes = btrfs_level_size(root, (int)owner_objectid); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 662 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 663 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 664 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 665 | u64 priv; |
| 666 | ret = get_state_private(&root->fs_info->extent_ins, |
| 667 | bytenr, &priv); |
| 668 | BUG_ON(ret); |
| 669 | extent_op = (struct pending_extent_op *) |
| 670 | (unsigned long)priv; |
| 671 | BUG_ON(extent_op->parent != orig_parent); |
| 672 | BUG_ON(extent_op->generation != orig_generation); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 673 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 674 | extent_op->parent = parent; |
| 675 | extent_op->generation = ref_generation; |
| 676 | } else { |
| 677 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 678 | BUG_ON(!extent_op); |
| 679 | |
| 680 | extent_op->type = PENDING_BACKREF_UPDATE; |
| 681 | extent_op->bytenr = bytenr; |
| 682 | extent_op->num_bytes = num_bytes; |
| 683 | extent_op->parent = parent; |
| 684 | extent_op->orig_parent = orig_parent; |
| 685 | extent_op->generation = ref_generation; |
| 686 | extent_op->orig_generation = orig_generation; |
| 687 | extent_op->level = (int)owner_objectid; |
| 688 | |
| 689 | set_extent_bits(&root->fs_info->extent_ins, |
| 690 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 691 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 692 | set_state_private(&root->fs_info->extent_ins, |
| 693 | bytenr, (unsigned long)extent_op); |
| 694 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 695 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | path = btrfs_alloc_path(); |
| 700 | if (!path) |
| 701 | return -ENOMEM; |
| 702 | ret = lookup_extent_backref(trans, extent_root, path, |
| 703 | bytenr, orig_parent, orig_root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 704 | orig_generation, owner_objectid, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 705 | if (ret) |
| 706 | goto out; |
| 707 | ret = remove_extent_backref(trans, extent_root, path); |
| 708 | if (ret) |
| 709 | goto out; |
| 710 | ret = insert_extent_backref(trans, extent_root, path, bytenr, |
| 711 | parent, ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 712 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 713 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 714 | finish_current_insert(trans, extent_root, 0); |
| 715 | del_pending_extents(trans, extent_root, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 716 | out: |
| 717 | btrfs_free_path(path); |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 722 | struct btrfs_root *root, u64 bytenr, |
| 723 | u64 orig_parent, u64 parent, |
| 724 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 725 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 726 | { |
| 727 | int ret; |
| 728 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 729 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 730 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 731 | ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, |
| 732 | parent, ref_root, ref_root, |
| 733 | ref_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 734 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 735 | return ret; |
| 736 | } |
| 737 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 738 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 739 | struct btrfs_root *root, u64 bytenr, |
| 740 | u64 orig_parent, u64 parent, |
| 741 | u64 orig_root, u64 ref_root, |
| 742 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 743 | u64 owner_objectid) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 744 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 745 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 746 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 747 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 748 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 749 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 750 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 751 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 752 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 753 | if (!path) |
| 754 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 755 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 756 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 757 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 758 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 759 | key.offset = (u64)-1; |
| 760 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 761 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 762 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 763 | if (ret < 0) |
| 764 | return ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 765 | BUG_ON(ret == 0 || path->slots[0] == 0); |
| 766 | |
| 767 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 768 | l = path->nodes[0]; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 769 | |
| 770 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 771 | BUG_ON(key.objectid != bytenr); |
| 772 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
| 773 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 774 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 775 | refs = btrfs_extent_refs(l, item); |
| 776 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 777 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 778 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 779 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 780 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 781 | path->reada = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 782 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
| 783 | path, bytenr, parent, |
| 784 | ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 785 | owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 786 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 787 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 788 | del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 789 | |
| 790 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 794 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 795 | struct btrfs_root *root, |
| 796 | u64 bytenr, u64 num_bytes, u64 parent, |
| 797 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 798 | u64 owner_objectid) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 799 | { |
| 800 | int ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 801 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 802 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 803 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 804 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, |
| 805 | 0, ref_root, 0, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 806 | owner_objectid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 807 | return ret; |
| 808 | } |
| 809 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 810 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 811 | struct btrfs_root *root) |
| 812 | { |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 813 | finish_current_insert(trans, root->fs_info->extent_root, 1); |
| 814 | del_pending_extents(trans, root->fs_info->extent_root, 1); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 815 | return 0; |
| 816 | } |
| 817 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 818 | int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, |
| 819 | struct btrfs_root *root, u64 bytenr, |
| 820 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 821 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 822 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 823 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 824 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 825 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 826 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 827 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 828 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 829 | path = btrfs_alloc_path(); |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 830 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 831 | key.objectid = bytenr; |
| 832 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 833 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 834 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 835 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 836 | if (ret < 0) |
| 837 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 838 | if (ret != 0) { |
| 839 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 840 | printk("failed to find block number %Lu\n", bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 841 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 842 | } |
| 843 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 844 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 845 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 846 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 847 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 851 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 852 | struct btrfs_root *root, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 853 | { |
| 854 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 855 | struct btrfs_path *path; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 856 | struct extent_buffer *leaf; |
| 857 | struct btrfs_extent_ref *ref_item; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 858 | struct btrfs_key key; |
| 859 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 860 | u64 ref_root; |
| 861 | u64 last_snapshot; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 862 | u32 nritems; |
| 863 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 864 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 865 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 866 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 867 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 868 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 869 | path = btrfs_alloc_path(); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 870 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 871 | if (ret < 0) |
| 872 | goto out; |
| 873 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 874 | |
| 875 | ret = -ENOENT; |
| 876 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 877 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 878 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 879 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 880 | leaf = path->nodes[0]; |
| 881 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 882 | |
| 883 | if (found_key.objectid != bytenr || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 884 | found_key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 885 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 886 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 887 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 888 | while (1) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 889 | leaf = path->nodes[0]; |
| 890 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 891 | if (path->slots[0] >= nritems) { |
| 892 | ret = btrfs_next_leaf(extent_root, path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 893 | if (ret < 0) |
| 894 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 895 | if (ret == 0) |
| 896 | continue; |
| 897 | break; |
| 898 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 899 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 900 | if (found_key.objectid != bytenr) |
| 901 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 902 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 903 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 904 | path->slots[0]++; |
| 905 | continue; |
| 906 | } |
| 907 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 908 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 909 | struct btrfs_extent_ref); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 910 | ref_root = btrfs_ref_root(leaf, ref_item); |
| 911 | if (ref_root != root->root_key.objectid && |
| 912 | ref_root != BTRFS_TREE_LOG_OBJECTID) { |
| 913 | ret = 1; |
| 914 | goto out; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 915 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 916 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { |
| 917 | ret = 1; |
| 918 | goto out; |
| 919 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 920 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 921 | path->slots[0]++; |
| 922 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 923 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 924 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 925 | btrfs_free_path(path); |
| 926 | return ret; |
| 927 | } |
| 928 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 929 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 930 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 931 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 932 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 933 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 934 | u64 root_gen; |
| 935 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 936 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 937 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 938 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 939 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 940 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 941 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 942 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 943 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 944 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 945 | shared = 0; |
| 946 | root_gen = root->root_key.offset; |
| 947 | } else { |
| 948 | shared = 1; |
| 949 | root_gen = trans->transid - 1; |
| 950 | } |
| 951 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 952 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 953 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 954 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 955 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 956 | struct btrfs_leaf_ref *ref; |
| 957 | struct btrfs_extent_info *info; |
| 958 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 959 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 960 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 961 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 962 | goto out; |
| 963 | } |
| 964 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 965 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 966 | ref->bytenr = buf->start; |
| 967 | ref->owner = btrfs_header_owner(buf); |
| 968 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 969 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 970 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 971 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 972 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 973 | u64 disk_bytenr; |
| 974 | btrfs_item_key_to_cpu(buf, &key, i); |
| 975 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 976 | continue; |
| 977 | fi = btrfs_item_ptr(buf, i, |
| 978 | struct btrfs_file_extent_item); |
| 979 | if (btrfs_file_extent_type(buf, fi) == |
| 980 | BTRFS_FILE_EXTENT_INLINE) |
| 981 | continue; |
| 982 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 983 | if (disk_bytenr == 0) |
| 984 | continue; |
| 985 | |
| 986 | info->bytenr = disk_bytenr; |
| 987 | info->num_bytes = |
| 988 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 989 | info->objectid = key.objectid; |
| 990 | info->offset = key.offset; |
| 991 | info++; |
| 992 | } |
| 993 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 994 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 995 | if (ret == -EEXIST && shared) { |
| 996 | struct btrfs_leaf_ref *old; |
| 997 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 998 | BUG_ON(!old); |
| 999 | btrfs_remove_leaf_ref(root, old); |
| 1000 | btrfs_free_leaf_ref(root, old); |
| 1001 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 1002 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1003 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1004 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1005 | } |
| 1006 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1007 | return ret; |
| 1008 | } |
| 1009 | |
| 1010 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1011 | struct extent_buffer *orig_buf, struct extent_buffer *buf, |
| 1012 | u32 *nr_extents) |
| 1013 | { |
| 1014 | u64 bytenr; |
| 1015 | u64 ref_root; |
| 1016 | u64 orig_root; |
| 1017 | u64 ref_generation; |
| 1018 | u64 orig_generation; |
| 1019 | u32 nritems; |
| 1020 | u32 nr_file_extents = 0; |
| 1021 | struct btrfs_key key; |
| 1022 | struct btrfs_file_extent_item *fi; |
| 1023 | int i; |
| 1024 | int level; |
| 1025 | int ret = 0; |
| 1026 | int faili = 0; |
| 1027 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1028 | u64, u64, u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1029 | |
| 1030 | ref_root = btrfs_header_owner(buf); |
| 1031 | ref_generation = btrfs_header_generation(buf); |
| 1032 | orig_root = btrfs_header_owner(orig_buf); |
| 1033 | orig_generation = btrfs_header_generation(orig_buf); |
| 1034 | |
| 1035 | nritems = btrfs_header_nritems(buf); |
| 1036 | level = btrfs_header_level(buf); |
| 1037 | |
| 1038 | if (root->ref_cows) { |
| 1039 | process_func = __btrfs_inc_extent_ref; |
| 1040 | } else { |
| 1041 | if (level == 0 && |
| 1042 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1043 | goto out; |
| 1044 | if (level != 0 && |
| 1045 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1046 | goto out; |
| 1047 | process_func = __btrfs_update_extent_ref; |
| 1048 | } |
| 1049 | |
| 1050 | for (i = 0; i < nritems; i++) { |
| 1051 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1052 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1053 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1054 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1055 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1056 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1057 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1058 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1059 | BTRFS_FILE_EXTENT_INLINE) |
| 1060 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1061 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1062 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1063 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1064 | |
| 1065 | nr_file_extents++; |
| 1066 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1067 | ret = process_func(trans, root, bytenr, |
| 1068 | orig_buf->start, buf->start, |
| 1069 | orig_root, ref_root, |
| 1070 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1071 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1072 | |
| 1073 | if (ret) { |
| 1074 | faili = i; |
| 1075 | WARN_ON(1); |
| 1076 | goto fail; |
| 1077 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1078 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1079 | bytenr = btrfs_node_blockptr(buf, i); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1080 | ret = process_func(trans, root, bytenr, |
| 1081 | orig_buf->start, buf->start, |
| 1082 | orig_root, ref_root, |
| 1083 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1084 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1085 | if (ret) { |
| 1086 | faili = i; |
| 1087 | WARN_ON(1); |
| 1088 | goto fail; |
| 1089 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1090 | } |
| 1091 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1092 | out: |
| 1093 | if (nr_extents) { |
| 1094 | if (level == 0) |
| 1095 | *nr_extents = nr_file_extents; |
| 1096 | else |
| 1097 | *nr_extents = nritems; |
| 1098 | } |
| 1099 | return 0; |
| 1100 | fail: |
| 1101 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1102 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1103 | } |
| 1104 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1105 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
| 1106 | struct btrfs_root *root, struct extent_buffer *orig_buf, |
| 1107 | struct extent_buffer *buf, int start_slot, int nr) |
| 1108 | |
| 1109 | { |
| 1110 | u64 bytenr; |
| 1111 | u64 ref_root; |
| 1112 | u64 orig_root; |
| 1113 | u64 ref_generation; |
| 1114 | u64 orig_generation; |
| 1115 | struct btrfs_key key; |
| 1116 | struct btrfs_file_extent_item *fi; |
| 1117 | int i; |
| 1118 | int ret; |
| 1119 | int slot; |
| 1120 | int level; |
| 1121 | |
| 1122 | BUG_ON(start_slot < 0); |
| 1123 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); |
| 1124 | |
| 1125 | ref_root = btrfs_header_owner(buf); |
| 1126 | ref_generation = btrfs_header_generation(buf); |
| 1127 | orig_root = btrfs_header_owner(orig_buf); |
| 1128 | orig_generation = btrfs_header_generation(orig_buf); |
| 1129 | level = btrfs_header_level(buf); |
| 1130 | |
| 1131 | if (!root->ref_cows) { |
| 1132 | if (level == 0 && |
| 1133 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1134 | return 0; |
| 1135 | if (level != 0 && |
| 1136 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
| 1140 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { |
| 1141 | cond_resched(); |
| 1142 | if (level == 0) { |
| 1143 | btrfs_item_key_to_cpu(buf, &key, slot); |
| 1144 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1145 | continue; |
| 1146 | fi = btrfs_item_ptr(buf, slot, |
| 1147 | struct btrfs_file_extent_item); |
| 1148 | if (btrfs_file_extent_type(buf, fi) == |
| 1149 | BTRFS_FILE_EXTENT_INLINE) |
| 1150 | continue; |
| 1151 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1152 | if (bytenr == 0) |
| 1153 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1154 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1155 | orig_buf->start, buf->start, |
| 1156 | orig_root, ref_root, |
| 1157 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1158 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1159 | if (ret) |
| 1160 | goto fail; |
| 1161 | } else { |
| 1162 | bytenr = btrfs_node_blockptr(buf, slot); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1163 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1164 | orig_buf->start, buf->start, |
| 1165 | orig_root, ref_root, |
| 1166 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1167 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1168 | if (ret) |
| 1169 | goto fail; |
| 1170 | } |
| 1171 | } |
| 1172 | return 0; |
| 1173 | fail: |
| 1174 | WARN_ON(1); |
| 1175 | return -1; |
| 1176 | } |
| 1177 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1178 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1179 | struct btrfs_root *root, |
| 1180 | struct btrfs_path *path, |
| 1181 | struct btrfs_block_group_cache *cache) |
| 1182 | { |
| 1183 | int ret; |
| 1184 | int pending_ret; |
| 1185 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1186 | unsigned long bi; |
| 1187 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1188 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1189 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1190 | if (ret < 0) |
| 1191 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1192 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1193 | |
| 1194 | leaf = path->nodes[0]; |
| 1195 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1196 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1197 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1198 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1199 | fail: |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1200 | finish_current_insert(trans, extent_root, 0); |
| 1201 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1202 | if (ret) |
| 1203 | return ret; |
| 1204 | if (pending_ret) |
| 1205 | return pending_ret; |
| 1206 | return 0; |
| 1207 | |
| 1208 | } |
| 1209 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1210 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1211 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1212 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1213 | struct btrfs_block_group_cache *cache, *entry; |
| 1214 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1215 | int err = 0; |
| 1216 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1217 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1218 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1219 | |
| 1220 | path = btrfs_alloc_path(); |
| 1221 | if (!path) |
| 1222 | return -ENOMEM; |
| 1223 | |
| 1224 | while(1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1225 | cache = NULL; |
| 1226 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1227 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1228 | n; n = rb_next(n)) { |
| 1229 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1230 | cache_node); |
| 1231 | if (entry->dirty) { |
| 1232 | cache = entry; |
| 1233 | break; |
| 1234 | } |
| 1235 | } |
| 1236 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1237 | |
| 1238 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1239 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1240 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1241 | cache->dirty = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1242 | last += cache->key.offset; |
| 1243 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1244 | err = write_one_cache_group(trans, root, |
| 1245 | path, cache); |
| 1246 | /* |
| 1247 | * if we fail to write the cache group, we want |
| 1248 | * to keep it marked dirty in hopes that a later |
| 1249 | * write will work |
| 1250 | */ |
| 1251 | if (err) { |
| 1252 | werr = err; |
| 1253 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | btrfs_free_path(path); |
| 1257 | return werr; |
| 1258 | } |
| 1259 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1260 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1261 | u64 total_bytes, u64 bytes_used, |
| 1262 | struct btrfs_space_info **space_info) |
| 1263 | { |
| 1264 | struct btrfs_space_info *found; |
| 1265 | |
| 1266 | found = __find_space_info(info, flags); |
| 1267 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1268 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1269 | found->total_bytes += total_bytes; |
| 1270 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1271 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1272 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1273 | *space_info = found; |
| 1274 | return 0; |
| 1275 | } |
| 1276 | found = kmalloc(sizeof(*found), GFP_NOFS); |
| 1277 | if (!found) |
| 1278 | return -ENOMEM; |
| 1279 | |
| 1280 | list_add(&found->list, &info->space_info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1281 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1282 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1283 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1284 | found->flags = flags; |
| 1285 | found->total_bytes = total_bytes; |
| 1286 | found->bytes_used = bytes_used; |
| 1287 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1288 | found->bytes_reserved = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1289 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1290 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1291 | *space_info = found; |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1295 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1296 | { |
| 1297 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1298 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1299 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1300 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1301 | if (extra_flags) { |
| 1302 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1303 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1304 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1305 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1306 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1307 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1308 | } |
| 1309 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1310 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1311 | static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1312 | { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1313 | u64 num_devices = root->fs_info->fs_devices->num_devices; |
| 1314 | |
| 1315 | if (num_devices == 1) |
| 1316 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1317 | if (num_devices < 4) |
| 1318 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1319 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1320 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1321 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1322 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1323 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1324 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1325 | |
| 1326 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1327 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1328 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1329 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1330 | |
| 1331 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1332 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1333 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1334 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1335 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1336 | return flags; |
| 1337 | } |
| 1338 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1339 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1340 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1341 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1342 | { |
| 1343 | struct btrfs_space_info *space_info; |
| 1344 | u64 thresh; |
| 1345 | u64 start; |
| 1346 | u64 num_bytes; |
Josef Bacik | cf74982 | 2008-10-01 19:11:18 -0400 | [diff] [blame] | 1347 | int ret = 0, waited = 0; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1348 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1349 | flags = reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1350 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1351 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1352 | if (!space_info) { |
| 1353 | ret = update_space_info(extent_root->fs_info, flags, |
| 1354 | 0, 0, &space_info); |
| 1355 | BUG_ON(ret); |
| 1356 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1357 | BUG_ON(!space_info); |
| 1358 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1359 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1360 | if (space_info->force_alloc) { |
| 1361 | force = 1; |
| 1362 | space_info->force_alloc = 0; |
| 1363 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1364 | if (space_info->full) { |
| 1365 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1366 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1367 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1368 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1369 | thresh = div_factor(space_info->total_bytes, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1370 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1371 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1372 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 1373 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1374 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1375 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1376 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1377 | spin_unlock(&space_info->lock); |
| 1378 | |
| 1379 | ret = mutex_trylock(&extent_root->fs_info->chunk_mutex); |
| 1380 | if (!ret && !force) { |
| 1381 | goto out; |
| 1382 | } else if (!ret) { |
| 1383 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | cf74982 | 2008-10-01 19:11:18 -0400 | [diff] [blame] | 1384 | waited = 1; |
| 1385 | } |
| 1386 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1387 | if (waited) { |
| 1388 | spin_lock(&space_info->lock); |
| 1389 | if (space_info->full) { |
| 1390 | spin_unlock(&space_info->lock); |
| 1391 | goto out_unlock; |
| 1392 | } |
| 1393 | spin_unlock(&space_info->lock); |
| 1394 | } |
Josef Bacik | cf74982 | 2008-10-01 19:11:18 -0400 | [diff] [blame] | 1395 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1396 | ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1397 | if (ret) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1398 | printk("space info full %Lu\n", flags); |
| 1399 | space_info->full = 1; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1400 | goto out_unlock; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1401 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1402 | |
| 1403 | ret = btrfs_make_block_group(trans, extent_root, 0, flags, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1404 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1405 | BUG_ON(ret); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1406 | out_unlock: |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1407 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1408 | out: |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1409 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1410 | } |
| 1411 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1412 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1413 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1414 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1415 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1416 | { |
| 1417 | struct btrfs_block_group_cache *cache; |
| 1418 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1419 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1420 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1421 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1422 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1423 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1424 | cache = btrfs_lookup_block_group(info, bytenr); |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1425 | if (!cache) { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1426 | return -1; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1427 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1428 | byte_in_group = bytenr - cache->key.objectid; |
| 1429 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1430 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1431 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1432 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1433 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1434 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1435 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1436 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1437 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1438 | cache->space_info->bytes_used += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1439 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1440 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1441 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1442 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1443 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1444 | cache->space_info->bytes_used -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1445 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1446 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1447 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1448 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1449 | int ret; |
| 1450 | ret = btrfs_add_free_space(cache, bytenr, |
| 1451 | num_bytes); |
| 1452 | if (ret) |
| 1453 | return -1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1454 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1455 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1456 | total -= num_bytes; |
| 1457 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1458 | } |
| 1459 | return 0; |
| 1460 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1461 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1462 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 1463 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1464 | struct btrfs_block_group_cache *cache; |
| 1465 | |
| 1466 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 1467 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1468 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1469 | |
| 1470 | return cache->key.objectid; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1471 | } |
| 1472 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1473 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1474 | u64 bytenr, u64 num, int pin) |
| 1475 | { |
| 1476 | u64 len; |
| 1477 | struct btrfs_block_group_cache *cache; |
| 1478 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1479 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1480 | WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1481 | if (pin) { |
| 1482 | set_extent_dirty(&fs_info->pinned_extents, |
| 1483 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1484 | } else { |
| 1485 | clear_extent_dirty(&fs_info->pinned_extents, |
| 1486 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1487 | } |
| 1488 | while (num > 0) { |
| 1489 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1490 | BUG_ON(!cache); |
| 1491 | len = min(num, cache->key.offset - |
| 1492 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1493 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1494 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1495 | spin_lock(&cache->lock); |
| 1496 | cache->pinned += len; |
| 1497 | cache->space_info->bytes_pinned += len; |
| 1498 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1499 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1500 | fs_info->total_pinned += len; |
| 1501 | } else { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1502 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1503 | spin_lock(&cache->lock); |
| 1504 | cache->pinned -= len; |
| 1505 | cache->space_info->bytes_pinned -= len; |
| 1506 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1507 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1508 | fs_info->total_pinned -= len; |
| 1509 | } |
| 1510 | bytenr += len; |
| 1511 | num -= len; |
| 1512 | } |
| 1513 | return 0; |
| 1514 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1515 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1516 | static int update_reserved_extents(struct btrfs_root *root, |
| 1517 | u64 bytenr, u64 num, int reserve) |
| 1518 | { |
| 1519 | u64 len; |
| 1520 | struct btrfs_block_group_cache *cache; |
| 1521 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1522 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1523 | while (num > 0) { |
| 1524 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 1525 | BUG_ON(!cache); |
| 1526 | len = min(num, cache->key.offset - |
| 1527 | (bytenr - cache->key.objectid)); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1528 | |
| 1529 | spin_lock(&cache->space_info->lock); |
| 1530 | spin_lock(&cache->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1531 | if (reserve) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1532 | cache->reserved += len; |
| 1533 | cache->space_info->bytes_reserved += len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1534 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1535 | cache->reserved -= len; |
| 1536 | cache->space_info->bytes_reserved -= len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1537 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1538 | spin_unlock(&cache->lock); |
| 1539 | spin_unlock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1540 | bytenr += len; |
| 1541 | num -= len; |
| 1542 | } |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1546 | 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] | 1547 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1548 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1549 | u64 start; |
| 1550 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1551 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1552 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1553 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1554 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1555 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1556 | ret = find_first_extent_bit(pinned_extents, last, |
| 1557 | &start, &end, EXTENT_DIRTY); |
| 1558 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1559 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1560 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 1561 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1562 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1563 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1564 | return 0; |
| 1565 | } |
| 1566 | |
| 1567 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 1568 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1569 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1570 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1571 | u64 start; |
| 1572 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1573 | int ret; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1574 | struct btrfs_block_group_cache *cache; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1575 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1576 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1577 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1578 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 1579 | EXTENT_DIRTY); |
| 1580 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1581 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1582 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1583 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1584 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 1585 | if (cache->cached) |
| 1586 | btrfs_add_free_space(cache, start, end - start + 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1587 | if (need_resched()) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1588 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1589 | cond_resched(); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1590 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1591 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1592 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1593 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1594 | return 0; |
| 1595 | } |
| 1596 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1597 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1598 | struct btrfs_root *extent_root, int all) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1599 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1600 | u64 start; |
| 1601 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1602 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1603 | u64 search = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1604 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 1605 | struct btrfs_path *path; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1606 | struct btrfs_extent_ref *ref; |
| 1607 | struct pending_extent_op *extent_op; |
| 1608 | struct btrfs_key key; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1609 | struct btrfs_extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1610 | int ret; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1611 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1612 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1613 | btrfs_set_stack_extent_refs(&extent_item, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1614 | path = btrfs_alloc_path(); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1615 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1616 | while(1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1617 | mutex_lock(&info->extent_ins_mutex); |
| 1618 | ret = find_first_extent_bit(&info->extent_ins, search, &start, |
| 1619 | &end, EXTENT_WRITEBACK); |
| 1620 | if (ret) { |
| 1621 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1622 | if (search && all) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1623 | search = 0; |
| 1624 | continue; |
| 1625 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1626 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS); |
| 1630 | if (!ret) { |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1631 | search = end + 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1632 | mutex_unlock(&info->extent_ins_mutex); |
| 1633 | cond_resched(); |
| 1634 | continue; |
| 1635 | } |
| 1636 | BUG_ON(ret < 0); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1637 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1638 | ret = get_state_private(&info->extent_ins, start, &priv); |
| 1639 | BUG_ON(ret); |
| 1640 | extent_op = (struct pending_extent_op *)(unsigned long)priv; |
| 1641 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1642 | mutex_unlock(&info->extent_ins_mutex); |
| 1643 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1644 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
| 1645 | key.objectid = start; |
| 1646 | key.offset = end + 1 - start; |
| 1647 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1648 | err = btrfs_insert_item(trans, extent_root, &key, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1649 | &extent_item, sizeof(extent_item)); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1650 | BUG_ON(err); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1651 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1652 | mutex_lock(&info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1653 | clear_extent_bits(&info->extent_ins, start, end, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1654 | EXTENT_WRITEBACK, GFP_NOFS); |
| 1655 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1656 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1657 | err = insert_extent_backref(trans, extent_root, path, |
| 1658 | start, extent_op->parent, |
| 1659 | extent_root->root_key.objectid, |
| 1660 | extent_op->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1661 | extent_op->level); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1662 | BUG_ON(err); |
| 1663 | } else if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 1664 | err = lookup_extent_backref(trans, extent_root, path, |
| 1665 | start, extent_op->orig_parent, |
| 1666 | extent_root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1667 | extent_op->orig_generation, |
| 1668 | extent_op->level, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1669 | BUG_ON(err); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1670 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1671 | mutex_lock(&info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1672 | clear_extent_bits(&info->extent_ins, start, end, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1673 | EXTENT_WRITEBACK, GFP_NOFS); |
| 1674 | mutex_unlock(&info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1675 | |
| 1676 | key.objectid = start; |
| 1677 | key.offset = extent_op->parent; |
| 1678 | key.type = BTRFS_EXTENT_REF_KEY; |
| 1679 | err = btrfs_set_item_key_safe(trans, extent_root, path, |
| 1680 | &key); |
| 1681 | BUG_ON(err); |
| 1682 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1683 | struct btrfs_extent_ref); |
| 1684 | btrfs_set_ref_generation(path->nodes[0], ref, |
| 1685 | extent_op->generation); |
| 1686 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 1687 | btrfs_release_path(extent_root, path); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1688 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1689 | BUG_ON(1); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1690 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1691 | kfree(extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1692 | unlock_extent(&info->extent_ins, start, end, GFP_NOFS); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1693 | if (all) |
| 1694 | search = 0; |
| 1695 | else |
| 1696 | search = end + 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1697 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1698 | cond_resched(); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1699 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1700 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1701 | return 0; |
| 1702 | } |
| 1703 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1704 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 1705 | struct btrfs_root *root, |
| 1706 | u64 bytenr, u64 num_bytes, int is_data) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1707 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1708 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1709 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1710 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1711 | if (is_data) |
| 1712 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1713 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1714 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 1715 | if (!buf) |
| 1716 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 1717 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1718 | /* we can reuse a block if it hasn't been written |
| 1719 | * and it is from this transaction. We can't |
| 1720 | * reuse anything from the tree log root because |
| 1721 | * it has tiny sub-transactions. |
| 1722 | */ |
| 1723 | if (btrfs_buffer_uptodate(buf, 0) && |
| 1724 | btrfs_try_tree_lock(buf)) { |
| 1725 | u64 header_owner = btrfs_header_owner(buf); |
| 1726 | u64 header_transid = btrfs_header_generation(buf); |
| 1727 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1728 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1729 | header_transid == trans->transid && |
| 1730 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 1731 | clean_tree_block(NULL, root, buf); |
| 1732 | btrfs_tree_unlock(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1733 | free_extent_buffer(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1734 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1735 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1736 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1737 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1738 | free_extent_buffer(buf); |
| 1739 | pinit: |
| 1740 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 1741 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1742 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1743 | return 0; |
| 1744 | } |
| 1745 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1746 | /* |
| 1747 | * remove an extent from the root, returns 0 on success |
| 1748 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1749 | static int __free_extent(struct btrfs_trans_handle *trans, |
| 1750 | struct btrfs_root *root, |
| 1751 | u64 bytenr, u64 num_bytes, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1752 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1753 | u64 owner_objectid, int pin, int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1754 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1755 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1756 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1757 | struct btrfs_fs_info *info = root->fs_info; |
| 1758 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1759 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1760 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1761 | int extent_slot = 0; |
| 1762 | int found_extent = 0; |
| 1763 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1764 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1765 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1766 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1767 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1768 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1769 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1770 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1771 | if (!path) |
| 1772 | return -ENOMEM; |
| 1773 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1774 | path->reada = 1; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1775 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1776 | bytenr, parent, root_objectid, |
| 1777 | ref_generation, owner_objectid, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1778 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1779 | struct btrfs_key found_key; |
| 1780 | extent_slot = path->slots[0]; |
| 1781 | while(extent_slot > 0) { |
| 1782 | extent_slot--; |
| 1783 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1784 | extent_slot); |
| 1785 | if (found_key.objectid != bytenr) |
| 1786 | break; |
| 1787 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1788 | found_key.offset == num_bytes) { |
| 1789 | found_extent = 1; |
| 1790 | break; |
| 1791 | } |
| 1792 | if (path->slots[0] - extent_slot > 5) |
| 1793 | break; |
| 1794 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1795 | if (!found_extent) { |
| 1796 | ret = remove_extent_backref(trans, extent_root, path); |
| 1797 | BUG_ON(ret); |
| 1798 | btrfs_release_path(extent_root, path); |
| 1799 | ret = btrfs_search_slot(trans, extent_root, |
| 1800 | &key, path, -1, 1); |
| 1801 | BUG_ON(ret); |
| 1802 | extent_slot = path->slots[0]; |
| 1803 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1804 | } else { |
| 1805 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 1806 | WARN_ON(1); |
| 1807 | printk("Unable to find ref byte nr %Lu root %Lu " |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1808 | "gen %Lu owner %Lu\n", bytenr, |
| 1809 | root_objectid, ref_generation, owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1810 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1811 | |
| 1812 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1813 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1814 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1815 | refs = btrfs_extent_refs(leaf, ei); |
| 1816 | BUG_ON(refs == 0); |
| 1817 | refs -= 1; |
| 1818 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1819 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1820 | btrfs_mark_buffer_dirty(leaf); |
| 1821 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1822 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1823 | struct btrfs_extent_ref *ref; |
| 1824 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1825 | struct btrfs_extent_ref); |
| 1826 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1827 | /* if the back ref and the extent are next to each other |
| 1828 | * they get deleted below in one shot |
| 1829 | */ |
| 1830 | path->slots[0] = extent_slot; |
| 1831 | num_to_del = 2; |
| 1832 | } else if (found_extent) { |
| 1833 | /* otherwise delete the extent back ref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1834 | ret = remove_extent_backref(trans, extent_root, path); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1835 | BUG_ON(ret); |
| 1836 | /* if refs are 0, we need to setup the path for deletion */ |
| 1837 | if (refs == 0) { |
| 1838 | btrfs_release_path(extent_root, path); |
| 1839 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 1840 | -1, 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1841 | BUG_ON(ret); |
| 1842 | } |
| 1843 | } |
| 1844 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1845 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1846 | u64 super_used; |
| 1847 | u64 root_used; |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 1848 | #ifdef BIO_RW_DISCARD |
| 1849 | u64 map_length = num_bytes; |
| 1850 | struct btrfs_multi_bio *multi = NULL; |
| 1851 | #endif |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1852 | |
| 1853 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1854 | mutex_lock(&root->fs_info->pinned_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1855 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, |
| 1856 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1857 | mutex_unlock(&root->fs_info->pinned_mutex); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1858 | if (ret > 0) |
| 1859 | mark_free = 1; |
| 1860 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1861 | } |
| 1862 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1863 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1864 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1865 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1866 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1867 | super_used - num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1868 | spin_unlock_irq(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1869 | |
| 1870 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1871 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1872 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1873 | root_used - num_bytes); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1874 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 1875 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1876 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1877 | btrfs_release_path(extent_root, path); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1878 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1879 | mark_free); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1880 | BUG_ON(ret); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 1881 | |
| 1882 | #ifdef BIO_RW_DISCARD |
| 1883 | /* Tell the block device(s) that the sectors can be discarded */ |
| 1884 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 1885 | bytenr, &map_length, &multi, 0); |
| 1886 | if (!ret) { |
| 1887 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1888 | int i; |
| 1889 | |
| 1890 | if (map_length > num_bytes) |
| 1891 | map_length = num_bytes; |
| 1892 | |
| 1893 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 1894 | blkdev_issue_discard(stripe->dev->bdev, |
| 1895 | stripe->physical >> 9, |
| 1896 | map_length >> 9); |
| 1897 | } |
| 1898 | kfree(multi); |
| 1899 | } |
| 1900 | #endif |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1901 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1902 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1903 | finish_current_insert(trans, extent_root, 0); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1904 | return ret; |
| 1905 | } |
| 1906 | |
| 1907 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1908 | * find all the blocks marked as pending in the radix tree and remove |
| 1909 | * them from the extent map |
| 1910 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1911 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1912 | btrfs_root *extent_root, int all) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1913 | { |
| 1914 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1915 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1916 | u64 start; |
| 1917 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1918 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1919 | u64 search = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1920 | struct extent_io_tree *pending_del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1921 | struct extent_io_tree *extent_ins; |
| 1922 | struct pending_extent_op *extent_op; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1923 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1924 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1925 | extent_ins = &extent_root->fs_info->extent_ins; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1926 | pending_del = &extent_root->fs_info->pending_del; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1927 | |
| 1928 | while(1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1929 | mutex_lock(&info->extent_ins_mutex); |
| 1930 | ret = find_first_extent_bit(pending_del, search, &start, &end, |
| 1931 | EXTENT_WRITEBACK); |
| 1932 | if (ret) { |
| 1933 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1934 | if (all && search) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1935 | search = 0; |
| 1936 | continue; |
| 1937 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1938 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | ret = try_lock_extent(extent_ins, start, end, GFP_NOFS); |
| 1942 | if (!ret) { |
| 1943 | search = end+1; |
| 1944 | mutex_unlock(&info->extent_ins_mutex); |
| 1945 | cond_resched(); |
| 1946 | continue; |
| 1947 | } |
| 1948 | BUG_ON(ret < 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1949 | |
| 1950 | ret = get_state_private(pending_del, start, &priv); |
| 1951 | BUG_ON(ret); |
| 1952 | extent_op = (struct pending_extent_op *)(unsigned long)priv; |
| 1953 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1954 | clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1955 | GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1956 | if (!test_range_bit(extent_ins, start, end, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1957 | EXTENT_WRITEBACK, 0)) { |
| 1958 | mutex_unlock(&info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1959 | free_extent: |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1960 | ret = __free_extent(trans, extent_root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1961 | start, end + 1 - start, |
| 1962 | extent_op->orig_parent, |
| 1963 | extent_root->root_key.objectid, |
| 1964 | extent_op->orig_generation, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1965 | extent_op->level, 1, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1966 | kfree(extent_op); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1967 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1968 | kfree(extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1969 | |
| 1970 | ret = get_state_private(&info->extent_ins, start, |
| 1971 | &priv); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1972 | BUG_ON(ret); |
| 1973 | extent_op = (struct pending_extent_op *) |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1974 | (unsigned long)priv; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1975 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1976 | clear_extent_bits(&info->extent_ins, start, end, |
| 1977 | EXTENT_WRITEBACK, GFP_NOFS); |
| 1978 | |
| 1979 | mutex_unlock(&info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1980 | |
| 1981 | if (extent_op->type == PENDING_BACKREF_UPDATE) |
| 1982 | goto free_extent; |
| 1983 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1984 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 1985 | ret = pin_down_bytes(trans, extent_root, start, |
| 1986 | end + 1 - start, 0); |
| 1987 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 1988 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1989 | ret = update_block_group(trans, extent_root, start, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1990 | end + 1 - start, 0, ret > 0); |
| 1991 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1992 | BUG_ON(ret); |
| 1993 | kfree(extent_op); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1994 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1995 | if (ret) |
| 1996 | err = ret; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1997 | unlock_extent(extent_ins, start, end, GFP_NOFS); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1998 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1999 | if (all) |
| 2000 | search = 0; |
| 2001 | else |
| 2002 | search = end + 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2003 | cond_resched(); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2004 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2005 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /* |
| 2009 | * remove an extent from the root, returns 0 on success |
| 2010 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2011 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2012 | struct btrfs_root *root, |
| 2013 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2014 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2015 | u64 owner_objectid, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2016 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2017 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2018 | int pending_ret; |
| 2019 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2020 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2021 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2022 | if (root == extent_root) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2023 | struct pending_extent_op *extent_op; |
| 2024 | |
| 2025 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2026 | BUG_ON(!extent_op); |
| 2027 | |
| 2028 | extent_op->type = PENDING_EXTENT_DELETE; |
| 2029 | extent_op->bytenr = bytenr; |
| 2030 | extent_op->num_bytes = num_bytes; |
| 2031 | extent_op->parent = parent; |
| 2032 | extent_op->orig_parent = parent; |
| 2033 | extent_op->generation = ref_generation; |
| 2034 | extent_op->orig_generation = ref_generation; |
| 2035 | extent_op->level = (int)owner_objectid; |
| 2036 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2037 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2038 | set_extent_bits(&root->fs_info->pending_del, |
| 2039 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2040 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2041 | set_state_private(&root->fs_info->pending_del, |
| 2042 | bytenr, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2043 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2044 | return 0; |
| 2045 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2046 | /* if metadata always pin */ |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2047 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2048 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2049 | struct btrfs_block_group_cache *cache; |
| 2050 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2051 | /* btrfs_free_reserved_extent */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2052 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2053 | BUG_ON(!cache); |
| 2054 | btrfs_add_free_space(cache, bytenr, num_bytes); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2055 | update_reserved_extents(root, bytenr, num_bytes, 0); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2056 | return 0; |
| 2057 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2058 | pin = 1; |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2059 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2060 | |
| 2061 | /* if data pin when any transaction has committed this */ |
| 2062 | if (ref_generation != trans->transid) |
| 2063 | pin = 1; |
| 2064 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2065 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2066 | root_objectid, ref_generation, |
| 2067 | owner_objectid, pin, pin == 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 2068 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2069 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 2070 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2071 | return ret ? ret : pending_ret; |
| 2072 | } |
| 2073 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2074 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2075 | struct btrfs_root *root, |
| 2076 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2077 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2078 | u64 owner_objectid, int pin) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2079 | { |
| 2080 | int ret; |
| 2081 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2082 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2083 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2084 | owner_objectid, pin); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2085 | return ret; |
| 2086 | } |
| 2087 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2088 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 2089 | { |
| 2090 | u64 mask = ((u64)root->stripesize - 1); |
| 2091 | u64 ret = (val + mask) & ~mask; |
| 2092 | return ret; |
| 2093 | } |
| 2094 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2095 | /* |
| 2096 | * walks the btree of allocated extents and find a hole of a given size. |
| 2097 | * The key ins is changed to record the hole: |
| 2098 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2099 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2100 | * ins->offset == number of blocks |
| 2101 | * Any available blocks before search_start are skipped. |
| 2102 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2103 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
| 2104 | struct btrfs_root *orig_root, |
| 2105 | u64 num_bytes, u64 empty_size, |
| 2106 | u64 search_start, u64 search_end, |
| 2107 | u64 hint_byte, struct btrfs_key *ins, |
| 2108 | u64 exclude_start, u64 exclude_nr, |
| 2109 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2110 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2111 | int ret = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2112 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2113 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2114 | u64 *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2115 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2116 | int chunk_alloc_done = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2117 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2118 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2119 | struct list_head *head = NULL, *cur = NULL; |
| 2120 | int loop = 0; |
| 2121 | struct btrfs_space_info *space_info; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2122 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2123 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2124 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2125 | ins->objectid = 0; |
| 2126 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2127 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2128 | if (orig_root->ref_cows || empty_size) |
| 2129 | allowed_chunk_alloc = 1; |
| 2130 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2131 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 2132 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2133 | empty_cluster = 256 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2134 | } |
| 2135 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2136 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2137 | last_ptr = &root->fs_info->last_data_alloc; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2138 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2139 | if (last_ptr) { |
| 2140 | if (*last_ptr) |
| 2141 | hint_byte = *last_ptr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2142 | else |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2143 | empty_size += empty_cluster; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2144 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2145 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2146 | search_start = max(search_start, hint_byte); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2147 | total_needed += empty_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2148 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2149 | block_group = btrfs_lookup_block_group(root->fs_info, search_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 2150 | if (!block_group) |
| 2151 | block_group = btrfs_lookup_first_block_group(root->fs_info, |
| 2152 | search_start); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2153 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2154 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2155 | down_read(&space_info->groups_sem); |
| 2156 | while (1) { |
| 2157 | struct btrfs_free_space *free_space; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2158 | /* |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2159 | * the only way this happens if our hint points to a block |
| 2160 | * group thats not of the proper type, while looping this |
| 2161 | * should never happen |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2162 | */ |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2163 | WARN_ON(!block_group); |
| 2164 | mutex_lock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2165 | if (unlikely(!block_group_bits(block_group, data))) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2166 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2167 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2168 | ret = cache_block_group(root, block_group); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2169 | if (ret) { |
| 2170 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2171 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2172 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2173 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2174 | if (block_group->ro) |
| 2175 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2176 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2177 | free_space = btrfs_find_free_space(block_group, search_start, |
| 2178 | total_needed); |
| 2179 | if (free_space) { |
| 2180 | u64 start = block_group->key.objectid; |
| 2181 | u64 end = block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2182 | block_group->key.offset; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2183 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2184 | search_start = stripe_align(root, free_space->offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2185 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2186 | /* move on to the next group */ |
| 2187 | if (search_start + num_bytes >= search_end) |
| 2188 | goto new_group; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2189 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2190 | /* move on to the next group */ |
| 2191 | if (search_start + num_bytes > end) |
| 2192 | goto new_group; |
| 2193 | |
| 2194 | if (exclude_nr > 0 && |
| 2195 | (search_start + num_bytes > exclude_start && |
| 2196 | search_start < exclude_start + exclude_nr)) { |
| 2197 | search_start = exclude_start + exclude_nr; |
| 2198 | /* |
| 2199 | * if search_start is still in this block group |
| 2200 | * then we just re-search this block group |
| 2201 | */ |
| 2202 | if (search_start >= start && |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2203 | search_start < end) { |
| 2204 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2205 | continue; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2206 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2207 | |
| 2208 | /* else we go to the next block group */ |
| 2209 | goto new_group; |
| 2210 | } |
| 2211 | |
| 2212 | ins->objectid = search_start; |
| 2213 | ins->offset = num_bytes; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2214 | |
| 2215 | btrfs_remove_free_space_lock(block_group, search_start, |
| 2216 | num_bytes); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2217 | /* we are all good, lets return */ |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2218 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2219 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2220 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2221 | new_group: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2222 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2223 | /* |
| 2224 | * Here's how this works. |
| 2225 | * loop == 0: we were searching a block group via a hint |
| 2226 | * and didn't find anything, so we start at |
| 2227 | * the head of the block groups and keep searching |
| 2228 | * loop == 1: we're searching through all of the block groups |
| 2229 | * if we hit the head again we have searched |
| 2230 | * all of the block groups for this space and we |
| 2231 | * need to try and allocate, if we cant error out. |
| 2232 | * loop == 2: we allocated more space and are looping through |
| 2233 | * all of the block groups again. |
| 2234 | */ |
| 2235 | if (loop == 0) { |
| 2236 | head = &space_info->block_groups; |
| 2237 | cur = head->next; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2238 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2239 | if (last_ptr && *last_ptr) { |
| 2240 | total_needed += empty_cluster; |
| 2241 | *last_ptr = 0; |
| 2242 | } |
| 2243 | loop++; |
| 2244 | } else if (loop == 1 && cur == head) { |
| 2245 | if (allowed_chunk_alloc && !chunk_alloc_done) { |
| 2246 | up_read(&space_info->groups_sem); |
| 2247 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 2248 | 2 * 1024 * 1024, data, 1); |
| 2249 | if (ret < 0) |
| 2250 | break; |
| 2251 | down_read(&space_info->groups_sem); |
| 2252 | loop++; |
| 2253 | head = &space_info->block_groups; |
| 2254 | cur = head->next; |
| 2255 | chunk_alloc_done = 1; |
| 2256 | } else if (!allowed_chunk_alloc) { |
| 2257 | space_info->force_alloc = 1; |
| 2258 | break; |
| 2259 | } else { |
| 2260 | break; |
| 2261 | } |
| 2262 | } else if (cur == head) { |
| 2263 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2264 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2265 | |
| 2266 | block_group = list_entry(cur, struct btrfs_block_group_cache, |
| 2267 | list); |
| 2268 | search_start = block_group->key.objectid; |
| 2269 | cur = cur->next; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 2270 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2271 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2272 | /* we found what we needed */ |
| 2273 | if (ins->objectid) { |
| 2274 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
| 2275 | trans->block_group = block_group; |
| 2276 | |
| 2277 | if (last_ptr) |
| 2278 | *last_ptr = ins->objectid + ins->offset; |
| 2279 | ret = 0; |
| 2280 | } else if (!ret) { |
| 2281 | ret = -ENOSPC; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2282 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2283 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2284 | up_read(&space_info->groups_sem); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2285 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2286 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2287 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2288 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 2289 | { |
| 2290 | struct btrfs_block_group_cache *cache; |
| 2291 | struct list_head *l; |
| 2292 | |
| 2293 | printk(KERN_INFO "space_info has %Lu free, is %sfull\n", |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2294 | info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 2295 | info->bytes_reserved, (info->full) ? "" : "not "); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2296 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2297 | down_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2298 | list_for_each(l, &info->block_groups) { |
| 2299 | cache = list_entry(l, struct btrfs_block_group_cache, list); |
| 2300 | spin_lock(&cache->lock); |
| 2301 | printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used " |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2302 | "%Lu pinned %Lu reserved\n", |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2303 | cache->key.objectid, cache->key.offset, |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2304 | btrfs_block_group_used(&cache->item), |
| 2305 | cache->pinned, cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2306 | btrfs_dump_free_space(cache, bytes); |
| 2307 | spin_unlock(&cache->lock); |
| 2308 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2309 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2310 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2311 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2312 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2313 | struct btrfs_root *root, |
| 2314 | u64 num_bytes, u64 min_alloc_size, |
| 2315 | u64 empty_size, u64 hint_byte, |
| 2316 | u64 search_end, struct btrfs_key *ins, |
| 2317 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2318 | { |
| 2319 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 2320 | u64 search_start = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2321 | u64 alloc_profile; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2322 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2323 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2324 | if (data) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2325 | alloc_profile = info->avail_data_alloc_bits & |
| 2326 | info->data_alloc_profile; |
| 2327 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2328 | } else if (root == root->fs_info->chunk_root) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2329 | alloc_profile = info->avail_system_alloc_bits & |
| 2330 | info->system_alloc_profile; |
| 2331 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2332 | } else { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2333 | alloc_profile = info->avail_metadata_alloc_bits & |
| 2334 | info->metadata_alloc_profile; |
| 2335 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2336 | } |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2337 | again: |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2338 | data = reduce_alloc_profile(root, data); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2339 | /* |
| 2340 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 2341 | * is not called recursively on allocations |
| 2342 | */ |
| 2343 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2344 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2345 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2346 | 2 * 1024 * 1024, |
| 2347 | BTRFS_BLOCK_GROUP_METADATA | |
| 2348 | (info->metadata_alloc_profile & |
| 2349 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2350 | } |
| 2351 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2352 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2353 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2354 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2355 | WARN_ON(num_bytes < root->sectorsize); |
| 2356 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 2357 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2358 | trans->alloc_exclude_start, |
| 2359 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 2360 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2361 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 2362 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2363 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2364 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2365 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2366 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 2367 | goto again; |
| 2368 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2369 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2370 | struct btrfs_space_info *sinfo; |
| 2371 | |
| 2372 | sinfo = __find_space_info(root->fs_info, data); |
| 2373 | printk("allocation failed flags %Lu, wanted %Lu\n", |
| 2374 | data, num_bytes); |
| 2375 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2376 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2377 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2378 | |
| 2379 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2380 | } |
| 2381 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2382 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 2383 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2384 | struct btrfs_block_group_cache *cache; |
| 2385 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2386 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2387 | if (!cache) { |
| 2388 | printk(KERN_ERR "Unable to find block group for %Lu\n", start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2389 | return -ENOSPC; |
| 2390 | } |
| 2391 | btrfs_add_free_space(cache, start, len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2392 | update_reserved_extents(root, start, len, 0); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2393 | return 0; |
| 2394 | } |
| 2395 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2396 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 2397 | struct btrfs_root *root, |
| 2398 | u64 num_bytes, u64 min_alloc_size, |
| 2399 | u64 empty_size, u64 hint_byte, |
| 2400 | u64 search_end, struct btrfs_key *ins, |
| 2401 | u64 data) |
| 2402 | { |
| 2403 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2404 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 2405 | empty_size, hint_byte, search_end, ins, |
| 2406 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2407 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2408 | return ret; |
| 2409 | } |
| 2410 | |
| 2411 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2412 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2413 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2414 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2415 | { |
| 2416 | int ret; |
| 2417 | int pending_ret; |
| 2418 | u64 super_used; |
| 2419 | u64 root_used; |
| 2420 | u64 num_bytes = ins->offset; |
| 2421 | u32 sizes[2]; |
| 2422 | struct btrfs_fs_info *info = root->fs_info; |
| 2423 | struct btrfs_root *extent_root = info->extent_root; |
| 2424 | struct btrfs_extent_item *extent_item; |
| 2425 | struct btrfs_extent_ref *ref; |
| 2426 | struct btrfs_path *path; |
| 2427 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2428 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2429 | if (parent == 0) |
| 2430 | parent = ins->objectid; |
| 2431 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2432 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2433 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2434 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2435 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2436 | spin_unlock_irq(&info->delalloc_lock); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2437 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2438 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2439 | root_used = btrfs_root_used(&root->root_item); |
| 2440 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2441 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2442 | if (root == extent_root) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2443 | struct pending_extent_op *extent_op; |
| 2444 | |
| 2445 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2446 | BUG_ON(!extent_op); |
| 2447 | |
| 2448 | extent_op->type = PENDING_EXTENT_INSERT; |
| 2449 | extent_op->bytenr = ins->objectid; |
| 2450 | extent_op->num_bytes = ins->offset; |
| 2451 | extent_op->parent = parent; |
| 2452 | extent_op->orig_parent = 0; |
| 2453 | extent_op->generation = ref_generation; |
| 2454 | extent_op->orig_generation = 0; |
| 2455 | extent_op->level = (int)owner; |
| 2456 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2457 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2458 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 2459 | ins->objectid + ins->offset - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2460 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2461 | set_state_private(&root->fs_info->extent_ins, |
| 2462 | ins->objectid, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2463 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2464 | goto update_block; |
| 2465 | } |
| 2466 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2467 | memcpy(&keys[0], ins, sizeof(*ins)); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2468 | keys[1].objectid = ins->objectid; |
| 2469 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2470 | keys[1].offset = parent; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2471 | sizes[0] = sizeof(*extent_item); |
| 2472 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2473 | |
| 2474 | path = btrfs_alloc_path(); |
| 2475 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2476 | |
| 2477 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 2478 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2479 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2480 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2481 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2482 | struct btrfs_extent_item); |
| 2483 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); |
| 2484 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 2485 | struct btrfs_extent_ref); |
| 2486 | |
| 2487 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 2488 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 2489 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2490 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 2491 | |
| 2492 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 2493 | |
| 2494 | trans->alloc_exclude_start = 0; |
| 2495 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2496 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2497 | finish_current_insert(trans, extent_root, 0); |
| 2498 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2499 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2500 | if (ret) |
| 2501 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2502 | if (pending_ret) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2503 | ret = pending_ret; |
| 2504 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2505 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2506 | |
| 2507 | update_block: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2508 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 2509 | if (ret) { |
| 2510 | printk("update block group failed for %Lu %Lu\n", |
| 2511 | ins->objectid, ins->offset); |
| 2512 | BUG(); |
| 2513 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2514 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2515 | return ret; |
| 2516 | } |
| 2517 | |
| 2518 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2519 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2520 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2521 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2522 | { |
| 2523 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 2524 | |
| 2525 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 2526 | return 0; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2527 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 2528 | ref_generation, owner, ins); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2529 | update_reserved_extents(root, ins->objectid, ins->offset, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2530 | return ret; |
| 2531 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2532 | |
| 2533 | /* |
| 2534 | * this is used by the tree logging recovery code. It records that |
| 2535 | * an extent has been allocated and makes sure to clear the free |
| 2536 | * space cache bits as well |
| 2537 | */ |
| 2538 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2539 | struct btrfs_root *root, u64 parent, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2540 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2541 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2542 | { |
| 2543 | int ret; |
| 2544 | struct btrfs_block_group_cache *block_group; |
| 2545 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2546 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2547 | mutex_lock(&block_group->alloc_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2548 | cache_block_group(root, block_group); |
| 2549 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2550 | ret = btrfs_remove_free_space_lock(block_group, ins->objectid, |
| 2551 | ins->offset); |
| 2552 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2553 | BUG_ON(ret); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2554 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 2555 | ref_generation, owner, ins); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2556 | return ret; |
| 2557 | } |
| 2558 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2559 | /* |
| 2560 | * finds a free extent and does all the dirty work required for allocation |
| 2561 | * returns the key for the extent through ins, and a tree buffer for |
| 2562 | * the first block of the extent through buf. |
| 2563 | * |
| 2564 | * returns 0 if everything worked, non-zero otherwise. |
| 2565 | */ |
| 2566 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 2567 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2568 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2569 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2570 | u64 owner_objectid, u64 empty_size, u64 hint_byte, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2571 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 2572 | { |
| 2573 | int ret; |
| 2574 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2575 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 2576 | min_alloc_size, empty_size, hint_byte, |
| 2577 | search_end, ins, data); |
| 2578 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2579 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2580 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
| 2581 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2582 | owner_objectid, ins); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2583 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2584 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2585 | } else { |
| 2586 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2587 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2588 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2589 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2590 | |
| 2591 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 2592 | struct btrfs_root *root, |
| 2593 | u64 bytenr, u32 blocksize) |
| 2594 | { |
| 2595 | struct extent_buffer *buf; |
| 2596 | |
| 2597 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 2598 | if (!buf) |
| 2599 | return ERR_PTR(-ENOMEM); |
| 2600 | btrfs_set_header_generation(buf, trans->transid); |
| 2601 | btrfs_tree_lock(buf); |
| 2602 | clean_tree_block(trans, root, buf); |
| 2603 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2604 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 2605 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2606 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 2607 | } else { |
| 2608 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 2609 | buf->start + buf->len - 1, GFP_NOFS); |
| 2610 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2611 | trans->blocks_used++; |
| 2612 | return buf; |
| 2613 | } |
| 2614 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2615 | /* |
| 2616 | * helper function to allocate a block for a given tree |
| 2617 | * returns the tree buffer or NULL. |
| 2618 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2619 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2620 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2621 | u32 blocksize, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2622 | u64 root_objectid, |
| 2623 | u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2624 | int level, |
| 2625 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2626 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2627 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2628 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2629 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2630 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2631 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2632 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2633 | root_objectid, ref_generation, level, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2634 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2635 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2636 | BUG_ON(ret > 0); |
| 2637 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2638 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 2639 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2640 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2641 | return buf; |
| 2642 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2643 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2644 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 2645 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2646 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2647 | u64 leaf_owner; |
| 2648 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2649 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2650 | struct btrfs_file_extent_item *fi; |
| 2651 | int i; |
| 2652 | int nritems; |
| 2653 | int ret; |
| 2654 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2655 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 2656 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2657 | leaf_owner = btrfs_header_owner(leaf); |
| 2658 | leaf_generation = btrfs_header_generation(leaf); |
| 2659 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2660 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2661 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 2662 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2663 | |
| 2664 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 2665 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2666 | continue; |
| 2667 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2668 | if (btrfs_file_extent_type(leaf, fi) == |
| 2669 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 2670 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2671 | /* |
| 2672 | * FIXME make sure to insert a trans record that |
| 2673 | * repeats the snapshot del on crash |
| 2674 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2675 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 2676 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 2677 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2678 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2679 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2680 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2681 | leaf->start, leaf_owner, leaf_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2682 | key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2683 | BUG_ON(ret); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2684 | |
| 2685 | atomic_inc(&root->fs_info->throttle_gen); |
| 2686 | wake_up(&root->fs_info->transaction_throttle); |
| 2687 | cond_resched(); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2688 | } |
| 2689 | return 0; |
| 2690 | } |
| 2691 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2692 | static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 2693 | struct btrfs_root *root, |
| 2694 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2695 | { |
| 2696 | int i; |
| 2697 | int ret; |
| 2698 | struct btrfs_extent_info *info = ref->extents; |
| 2699 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2700 | for (i = 0; i < ref->nritems; i++) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2701 | ret = __btrfs_free_extent(trans, root, info->bytenr, |
| 2702 | info->num_bytes, ref->bytenr, |
| 2703 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2704 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2705 | |
| 2706 | atomic_inc(&root->fs_info->throttle_gen); |
| 2707 | wake_up(&root->fs_info->transaction_throttle); |
| 2708 | cond_resched(); |
| 2709 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2710 | BUG_ON(ret); |
| 2711 | info++; |
| 2712 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2713 | |
| 2714 | return 0; |
| 2715 | } |
| 2716 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2717 | int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len, |
| 2718 | u32 *refs) |
| 2719 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2720 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2721 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2722 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2723 | BUG_ON(ret); |
| 2724 | |
| 2725 | #if 0 // some debugging code in case we see problems here |
| 2726 | /* if the refs count is one, it won't get increased again. But |
| 2727 | * if the ref count is > 1, someone may be decreasing it at |
| 2728 | * the same time we are. |
| 2729 | */ |
| 2730 | if (*refs != 1) { |
| 2731 | struct extent_buffer *eb = NULL; |
| 2732 | eb = btrfs_find_create_tree_block(root, start, len); |
| 2733 | if (eb) |
| 2734 | btrfs_tree_lock(eb); |
| 2735 | |
| 2736 | mutex_lock(&root->fs_info->alloc_mutex); |
| 2737 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 2738 | BUG_ON(ret); |
| 2739 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 2740 | |
| 2741 | if (eb) { |
| 2742 | btrfs_tree_unlock(eb); |
| 2743 | free_extent_buffer(eb); |
| 2744 | } |
| 2745 | if (*refs == 1) { |
| 2746 | printk("block %llu went down to one during drop_snap\n", |
| 2747 | (unsigned long long)start); |
| 2748 | } |
| 2749 | |
| 2750 | } |
| 2751 | #endif |
| 2752 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2753 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2754 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2758 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 2759 | * counts as it goes. |
| 2760 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2761 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
| 2762 | struct btrfs_root *root, |
| 2763 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2764 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2765 | u64 root_owner; |
| 2766 | u64 root_gen; |
| 2767 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2768 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2769 | struct extent_buffer *next; |
| 2770 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2771 | struct extent_buffer *parent; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2772 | struct btrfs_leaf_ref *ref; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2773 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2774 | int ret; |
| 2775 | u32 refs; |
| 2776 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2777 | WARN_ON(*level < 0); |
| 2778 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2779 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2780 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2781 | BUG_ON(ret); |
| 2782 | if (refs > 1) |
| 2783 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2784 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2785 | /* |
| 2786 | * walk down to the last node level and free all the leaves |
| 2787 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2788 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2789 | WARN_ON(*level < 0); |
| 2790 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2791 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2792 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2793 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2794 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2795 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2796 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2797 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2798 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2799 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2800 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2801 | BUG_ON(ret); |
| 2802 | break; |
| 2803 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2804 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2805 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2806 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2807 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2808 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2809 | BUG_ON(ret); |
| 2810 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2811 | parent = path->nodes[*level]; |
| 2812 | root_owner = btrfs_header_owner(parent); |
| 2813 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2814 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2815 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2816 | ret = __btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2817 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2818 | root_owner, root_gen, |
| 2819 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2820 | BUG_ON(ret); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 2821 | |
| 2822 | atomic_inc(&root->fs_info->throttle_gen); |
| 2823 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2824 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 2825 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2826 | continue; |
| 2827 | } |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2828 | /* |
| 2829 | * at this point, we have a single ref, and since the |
| 2830 | * only place referencing this extent is a dead root |
| 2831 | * the reference count should never go higher. |
| 2832 | * So, we don't need to check it again |
| 2833 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2834 | if (*level == 1) { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 2835 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2836 | if (ref && ref->generation != ptr_gen) { |
| 2837 | btrfs_free_leaf_ref(root, ref); |
| 2838 | ref = NULL; |
| 2839 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2840 | if (ref) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2841 | ret = cache_drop_leaf_ref(trans, root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2842 | BUG_ON(ret); |
| 2843 | btrfs_remove_leaf_ref(root, ref); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2844 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2845 | *level = 0; |
| 2846 | break; |
| 2847 | } |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2848 | if (printk_ratelimit()) { |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 2849 | printk("leaf ref miss for bytenr %llu\n", |
| 2850 | (unsigned long long)bytenr); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2851 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2852 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2853 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 2854 | if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2855 | free_extent_buffer(next); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2856 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2857 | next = read_tree_block(root, bytenr, blocksize, |
| 2858 | ptr_gen); |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2859 | cond_resched(); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2860 | #if 0 |
| 2861 | /* |
| 2862 | * this is a debugging check and can go away |
| 2863 | * the ref should never go all the way down to 1 |
| 2864 | * at this point |
| 2865 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 2866 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, |
| 2867 | &refs); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2868 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2869 | WARN_ON(refs != 1); |
| 2870 | #endif |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2871 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2872 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2873 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2874 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2875 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2876 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2877 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 2878 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2879 | } |
| 2880 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2881 | WARN_ON(*level < 0); |
| 2882 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2883 | |
| 2884 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2885 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2886 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2887 | } else { |
| 2888 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2889 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2890 | } |
| 2891 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2892 | blocksize = btrfs_level_size(root, *level); |
| 2893 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2894 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2895 | |
| 2896 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2897 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2898 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2899 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2900 | path->nodes[*level] = NULL; |
| 2901 | *level += 1; |
| 2902 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 2903 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2904 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2905 | return 0; |
| 2906 | } |
| 2907 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2908 | /* |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2909 | * helper function for drop_subtree, this function is similar to |
| 2910 | * walk_down_tree. The main difference is that it checks reference |
| 2911 | * counts while tree blocks are locked. |
| 2912 | */ |
| 2913 | static int noinline walk_down_subtree(struct btrfs_trans_handle *trans, |
| 2914 | struct btrfs_root *root, |
| 2915 | struct btrfs_path *path, int *level) |
| 2916 | { |
| 2917 | struct extent_buffer *next; |
| 2918 | struct extent_buffer *cur; |
| 2919 | struct extent_buffer *parent; |
| 2920 | u64 bytenr; |
| 2921 | u64 ptr_gen; |
| 2922 | u32 blocksize; |
| 2923 | u32 refs; |
| 2924 | int ret; |
| 2925 | |
| 2926 | cur = path->nodes[*level]; |
| 2927 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, |
| 2928 | &refs); |
| 2929 | BUG_ON(ret); |
| 2930 | if (refs > 1) |
| 2931 | goto out; |
| 2932 | |
| 2933 | while (*level >= 0) { |
| 2934 | cur = path->nodes[*level]; |
| 2935 | if (*level == 0) { |
| 2936 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
| 2937 | BUG_ON(ret); |
| 2938 | clean_tree_block(trans, root, cur); |
| 2939 | break; |
| 2940 | } |
| 2941 | if (path->slots[*level] >= btrfs_header_nritems(cur)) { |
| 2942 | clean_tree_block(trans, root, cur); |
| 2943 | break; |
| 2944 | } |
| 2945 | |
| 2946 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 2947 | blocksize = btrfs_level_size(root, *level - 1); |
| 2948 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
| 2949 | |
| 2950 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
| 2951 | btrfs_tree_lock(next); |
| 2952 | |
| 2953 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, |
| 2954 | &refs); |
| 2955 | BUG_ON(ret); |
| 2956 | if (refs > 1) { |
| 2957 | parent = path->nodes[*level]; |
| 2958 | ret = btrfs_free_extent(trans, root, bytenr, |
| 2959 | blocksize, parent->start, |
| 2960 | btrfs_header_owner(parent), |
| 2961 | btrfs_header_generation(parent), |
| 2962 | *level - 1, 1); |
| 2963 | BUG_ON(ret); |
| 2964 | path->slots[*level]++; |
| 2965 | btrfs_tree_unlock(next); |
| 2966 | free_extent_buffer(next); |
| 2967 | continue; |
| 2968 | } |
| 2969 | |
| 2970 | *level = btrfs_header_level(next); |
| 2971 | path->nodes[*level] = next; |
| 2972 | path->slots[*level] = 0; |
| 2973 | path->locks[*level] = 1; |
| 2974 | cond_resched(); |
| 2975 | } |
| 2976 | out: |
| 2977 | parent = path->nodes[*level + 1]; |
| 2978 | bytenr = path->nodes[*level]->start; |
| 2979 | blocksize = path->nodes[*level]->len; |
| 2980 | |
| 2981 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
| 2982 | parent->start, btrfs_header_owner(parent), |
| 2983 | btrfs_header_generation(parent), *level, 1); |
| 2984 | BUG_ON(ret); |
| 2985 | |
| 2986 | if (path->locks[*level]) { |
| 2987 | btrfs_tree_unlock(path->nodes[*level]); |
| 2988 | path->locks[*level] = 0; |
| 2989 | } |
| 2990 | free_extent_buffer(path->nodes[*level]); |
| 2991 | path->nodes[*level] = NULL; |
| 2992 | *level += 1; |
| 2993 | cond_resched(); |
| 2994 | return 0; |
| 2995 | } |
| 2996 | |
| 2997 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2998 | * helper for dropping snapshots. This walks back up the tree in the path |
| 2999 | * to find the first node higher up where we haven't yet gone through |
| 3000 | * all the slots |
| 3001 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3002 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
| 3003 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3004 | struct btrfs_path *path, |
| 3005 | int *level, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3006 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3007 | u64 root_owner; |
| 3008 | u64 root_gen; |
| 3009 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3010 | int i; |
| 3011 | int slot; |
| 3012 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3013 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3014 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3015 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3016 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 3017 | struct extent_buffer *node; |
| 3018 | struct btrfs_disk_key disk_key; |
| 3019 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3020 | path->slots[i]++; |
| 3021 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3022 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3023 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3024 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3025 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3026 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3027 | return 0; |
| 3028 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3029 | struct extent_buffer *parent; |
| 3030 | if (path->nodes[*level] == root->node) |
| 3031 | parent = path->nodes[*level]; |
| 3032 | else |
| 3033 | parent = path->nodes[*level + 1]; |
| 3034 | |
| 3035 | root_owner = btrfs_header_owner(parent); |
| 3036 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3037 | |
| 3038 | clean_tree_block(trans, root, path->nodes[*level]); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3039 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3040 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3041 | path->nodes[*level]->len, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3042 | parent->start, root_owner, |
| 3043 | root_gen, *level, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3044 | BUG_ON(ret); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3045 | if (path->locks[*level]) { |
| 3046 | btrfs_tree_unlock(path->nodes[*level]); |
| 3047 | path->locks[*level] = 0; |
| 3048 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3049 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3050 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3051 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | return 1; |
| 3055 | } |
| 3056 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3057 | /* |
| 3058 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 3059 | * the tree freeing any blocks that have a ref count of zero after being |
| 3060 | * decremented. |
| 3061 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3062 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3063 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3064 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 3065 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3066 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3067 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3068 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3069 | int i; |
| 3070 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3071 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3072 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3073 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3074 | path = btrfs_alloc_path(); |
| 3075 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3076 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3077 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3078 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3079 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 3080 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3081 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3082 | path->slots[level] = 0; |
| 3083 | } else { |
| 3084 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3085 | struct btrfs_disk_key found_key; |
| 3086 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3087 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3088 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3089 | level = root_item->drop_level; |
| 3090 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3091 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3092 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3093 | ret = wret; |
| 3094 | goto out; |
| 3095 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3096 | node = path->nodes[level]; |
| 3097 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 3098 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 3099 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3100 | /* |
| 3101 | * unlock our path, this is safe because only this |
| 3102 | * function is allowed to delete this snapshot |
| 3103 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3104 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 3105 | if (path->nodes[i] && path->locks[i]) { |
| 3106 | path->locks[i] = 0; |
| 3107 | btrfs_tree_unlock(path->nodes[i]); |
| 3108 | } |
| 3109 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3110 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3111 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3112 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3113 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3114 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3115 | if (wret < 0) |
| 3116 | ret = wret; |
| 3117 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3118 | wret = walk_up_tree(trans, root, path, &level, |
| 3119 | BTRFS_MAX_LEVEL); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3120 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3121 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3122 | if (wret < 0) |
| 3123 | ret = wret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3124 | if (trans->transaction->in_commit) { |
| 3125 | ret = -EAGAIN; |
| 3126 | break; |
| 3127 | } |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3128 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3129 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3130 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3131 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3132 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3133 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 3134 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3135 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3136 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3137 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3138 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3139 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3140 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3141 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3142 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 3143 | struct btrfs_root *root, |
| 3144 | struct extent_buffer *node, |
| 3145 | struct extent_buffer *parent) |
| 3146 | { |
| 3147 | struct btrfs_path *path; |
| 3148 | int level; |
| 3149 | int parent_level; |
| 3150 | int ret = 0; |
| 3151 | int wret; |
| 3152 | |
| 3153 | path = btrfs_alloc_path(); |
| 3154 | BUG_ON(!path); |
| 3155 | |
| 3156 | BUG_ON(!btrfs_tree_locked(parent)); |
| 3157 | parent_level = btrfs_header_level(parent); |
| 3158 | extent_buffer_get(parent); |
| 3159 | path->nodes[parent_level] = parent; |
| 3160 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 3161 | |
| 3162 | BUG_ON(!btrfs_tree_locked(node)); |
| 3163 | level = btrfs_header_level(node); |
| 3164 | extent_buffer_get(node); |
| 3165 | path->nodes[level] = node; |
| 3166 | path->slots[level] = 0; |
| 3167 | |
| 3168 | while (1) { |
| 3169 | wret = walk_down_subtree(trans, root, path, &level); |
| 3170 | if (wret < 0) |
| 3171 | ret = wret; |
| 3172 | if (wret != 0) |
| 3173 | break; |
| 3174 | |
| 3175 | wret = walk_up_tree(trans, root, path, &level, parent_level); |
| 3176 | if (wret < 0) |
| 3177 | ret = wret; |
| 3178 | if (wret != 0) |
| 3179 | break; |
| 3180 | } |
| 3181 | |
| 3182 | btrfs_free_path(path); |
| 3183 | return ret; |
| 3184 | } |
| 3185 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3186 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 3187 | unsigned long nr) |
| 3188 | { |
| 3189 | return min(last, start + nr - 1); |
| 3190 | } |
| 3191 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3192 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
| 3193 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3194 | { |
| 3195 | u64 page_start; |
| 3196 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3197 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3198 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3199 | unsigned long i; |
| 3200 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3201 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3202 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3203 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3204 | unsigned int total_read = 0; |
| 3205 | unsigned int total_dirty = 0; |
| 3206 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3207 | |
| 3208 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3209 | |
| 3210 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3211 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3212 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 3213 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3214 | /* make sure the dirty trick played by the caller work */ |
| 3215 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 3216 | first_index, last_index); |
| 3217 | if (ret) |
| 3218 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3219 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3220 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3221 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3222 | for (i = first_index ; i <= last_index; i++) { |
| 3223 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3224 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3225 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3226 | } |
| 3227 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3228 | again: |
| 3229 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3230 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3231 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3232 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3233 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3234 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3235 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3236 | if (!PageUptodate(page)) { |
| 3237 | btrfs_readpage(NULL, page); |
| 3238 | lock_page(page); |
| 3239 | if (!PageUptodate(page)) { |
| 3240 | unlock_page(page); |
| 3241 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3242 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3243 | goto out_unlock; |
| 3244 | } |
| 3245 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3246 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3247 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3248 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 3249 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3250 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3251 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3252 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3253 | if (ordered) { |
| 3254 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3255 | unlock_page(page); |
| 3256 | page_cache_release(page); |
| 3257 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3258 | btrfs_put_ordered_extent(ordered); |
| 3259 | goto again; |
| 3260 | } |
| 3261 | set_page_extent_mapped(page); |
| 3262 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3263 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3264 | if (i == first_index) |
| 3265 | set_extent_bits(io_tree, page_start, page_end, |
| 3266 | EXTENT_BOUNDARY, GFP_NOFS); |
| 3267 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3268 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3269 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3270 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3271 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3272 | unlock_page(page); |
| 3273 | page_cache_release(page); |
| 3274 | } |
| 3275 | |
| 3276 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3277 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3278 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3279 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3280 | return ret; |
| 3281 | } |
| 3282 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3283 | static int noinline relocate_data_extent(struct inode *reloc_inode, |
| 3284 | struct btrfs_key *extent_key, |
| 3285 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3286 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3287 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 3288 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 3289 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3290 | u64 start = extent_key->objectid - offset; |
| 3291 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3292 | |
| 3293 | em = alloc_extent_map(GFP_NOFS); |
| 3294 | BUG_ON(!em || IS_ERR(em)); |
| 3295 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3296 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3297 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3298 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3299 | em->block_start = extent_key->objectid; |
| 3300 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 3301 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 3302 | |
| 3303 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3304 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3305 | while (1) { |
| 3306 | int ret; |
| 3307 | spin_lock(&em_tree->lock); |
| 3308 | ret = add_extent_mapping(em_tree, em); |
| 3309 | spin_unlock(&em_tree->lock); |
| 3310 | if (ret != -EEXIST) { |
| 3311 | free_extent_map(em); |
| 3312 | break; |
| 3313 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3314 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3315 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3316 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3317 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3318 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | struct btrfs_ref_path { |
| 3322 | u64 extent_start; |
| 3323 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 3324 | u64 root_objectid; |
| 3325 | u64 root_generation; |
| 3326 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3327 | u32 num_refs; |
| 3328 | int lowest_level; |
| 3329 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3330 | int shared_level; |
| 3331 | |
| 3332 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 3333 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3334 | }; |
| 3335 | |
| 3336 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3337 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3338 | u64 disk_bytenr; |
| 3339 | u64 disk_num_bytes; |
| 3340 | u64 offset; |
| 3341 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3342 | u8 compression; |
| 3343 | u8 encryption; |
| 3344 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3345 | }; |
| 3346 | |
| 3347 | static int is_cowonly_root(u64 root_objectid) |
| 3348 | { |
| 3349 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 3350 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 3351 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 3352 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
| 3353 | root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 3354 | return 1; |
| 3355 | return 0; |
| 3356 | } |
| 3357 | |
| 3358 | static int noinline __next_ref_path(struct btrfs_trans_handle *trans, |
| 3359 | struct btrfs_root *extent_root, |
| 3360 | struct btrfs_ref_path *ref_path, |
| 3361 | int first_time) |
| 3362 | { |
| 3363 | struct extent_buffer *leaf; |
| 3364 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3365 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3366 | struct btrfs_key key; |
| 3367 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3368 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3369 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3370 | int level; |
| 3371 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3372 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3373 | path = btrfs_alloc_path(); |
| 3374 | if (!path) |
| 3375 | return -ENOMEM; |
| 3376 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3377 | if (first_time) { |
| 3378 | ref_path->lowest_level = -1; |
| 3379 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3380 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3381 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3382 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3383 | walk_down: |
| 3384 | level = ref_path->current_level - 1; |
| 3385 | while (level >= -1) { |
| 3386 | u64 parent; |
| 3387 | if (level < ref_path->lowest_level) |
| 3388 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3389 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3390 | if (level >= 0) { |
| 3391 | bytenr = ref_path->nodes[level]; |
| 3392 | } else { |
| 3393 | bytenr = ref_path->extent_start; |
| 3394 | } |
| 3395 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3396 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3397 | parent = ref_path->nodes[level + 1]; |
| 3398 | ref_path->nodes[level + 1] = 0; |
| 3399 | ref_path->current_level = level; |
| 3400 | BUG_ON(parent == 0); |
| 3401 | |
| 3402 | key.objectid = bytenr; |
| 3403 | key.offset = parent + 1; |
| 3404 | key.type = BTRFS_EXTENT_REF_KEY; |
| 3405 | |
| 3406 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3407 | if (ret < 0) |
| 3408 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3409 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3410 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3411 | leaf = path->nodes[0]; |
| 3412 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3413 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3414 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3415 | if (ret < 0) |
| 3416 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3417 | if (ret > 0) |
| 3418 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3419 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3420 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3421 | |
| 3422 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3423 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3424 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 3425 | if (level < ref_path->shared_level) |
| 3426 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3427 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3428 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3429 | next: |
| 3430 | level--; |
| 3431 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 3432 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3433 | } |
| 3434 | /* reached lowest level */ |
| 3435 | ret = 1; |
| 3436 | goto out; |
| 3437 | walk_up: |
| 3438 | level = ref_path->current_level; |
| 3439 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 3440 | u64 ref_objectid; |
| 3441 | if (level >= 0) { |
| 3442 | bytenr = ref_path->nodes[level]; |
| 3443 | } else { |
| 3444 | bytenr = ref_path->extent_start; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3445 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3446 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3447 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3448 | key.objectid = bytenr; |
| 3449 | key.offset = 0; |
| 3450 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3451 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3452 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 3453 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3454 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3455 | |
| 3456 | leaf = path->nodes[0]; |
| 3457 | nritems = btrfs_header_nritems(leaf); |
| 3458 | if (path->slots[0] >= nritems) { |
| 3459 | ret = btrfs_next_leaf(extent_root, path); |
| 3460 | if (ret < 0) |
| 3461 | goto out; |
| 3462 | if (ret > 0) { |
| 3463 | /* the extent was freed by someone */ |
| 3464 | if (ref_path->lowest_level == level) |
| 3465 | goto out; |
| 3466 | btrfs_release_path(extent_root, path); |
| 3467 | goto walk_down; |
| 3468 | } |
| 3469 | leaf = path->nodes[0]; |
| 3470 | } |
| 3471 | |
| 3472 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3473 | if (found_key.objectid != bytenr || |
| 3474 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 3475 | /* the extent was freed by someone */ |
| 3476 | if (ref_path->lowest_level == level) { |
| 3477 | ret = 1; |
| 3478 | goto out; |
| 3479 | } |
| 3480 | btrfs_release_path(extent_root, path); |
| 3481 | goto walk_down; |
| 3482 | } |
| 3483 | found: |
| 3484 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 3485 | struct btrfs_extent_ref); |
| 3486 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 3487 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 3488 | if (first_time) { |
| 3489 | level = (int)ref_objectid; |
| 3490 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 3491 | ref_path->lowest_level = level; |
| 3492 | ref_path->current_level = level; |
| 3493 | ref_path->nodes[level] = bytenr; |
| 3494 | } else { |
| 3495 | WARN_ON(ref_objectid != level); |
| 3496 | } |
| 3497 | } else { |
| 3498 | WARN_ON(level != -1); |
| 3499 | } |
| 3500 | first_time = 0; |
| 3501 | |
| 3502 | if (ref_path->lowest_level == level) { |
| 3503 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3504 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 3505 | } |
| 3506 | |
| 3507 | /* |
| 3508 | * the block is tree root or the block isn't in reference |
| 3509 | * counted tree. |
| 3510 | */ |
| 3511 | if (found_key.objectid == found_key.offset || |
| 3512 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 3513 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 3514 | ref_path->root_generation = |
| 3515 | btrfs_ref_generation(leaf, ref); |
| 3516 | if (level < 0) { |
| 3517 | /* special reference from the tree log */ |
| 3518 | ref_path->nodes[0] = found_key.offset; |
| 3519 | ref_path->current_level = 0; |
| 3520 | } |
| 3521 | ret = 0; |
| 3522 | goto out; |
| 3523 | } |
| 3524 | |
| 3525 | level++; |
| 3526 | BUG_ON(ref_path->nodes[level] != 0); |
| 3527 | ref_path->nodes[level] = found_key.offset; |
| 3528 | ref_path->current_level = level; |
| 3529 | |
| 3530 | /* |
| 3531 | * the reference was created in the running transaction, |
| 3532 | * no need to continue walking up. |
| 3533 | */ |
| 3534 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 3535 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 3536 | ref_path->root_generation = |
| 3537 | btrfs_ref_generation(leaf, ref); |
| 3538 | ret = 0; |
| 3539 | goto out; |
| 3540 | } |
| 3541 | |
| 3542 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 3543 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3544 | } |
| 3545 | /* reached max tree level, but no tree root found. */ |
| 3546 | BUG(); |
| 3547 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3548 | btrfs_free_path(path); |
| 3549 | return ret; |
| 3550 | } |
| 3551 | |
| 3552 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 3553 | struct btrfs_root *extent_root, |
| 3554 | struct btrfs_ref_path *ref_path, |
| 3555 | u64 extent_start) |
| 3556 | { |
| 3557 | memset(ref_path, 0, sizeof(*ref_path)); |
| 3558 | ref_path->extent_start = extent_start; |
| 3559 | |
| 3560 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 3561 | } |
| 3562 | |
| 3563 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 3564 | struct btrfs_root *extent_root, |
| 3565 | struct btrfs_ref_path *ref_path) |
| 3566 | { |
| 3567 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 3568 | } |
| 3569 | |
| 3570 | static int noinline get_new_locations(struct inode *reloc_inode, |
| 3571 | struct btrfs_key *extent_key, |
| 3572 | u64 offset, int no_fragment, |
| 3573 | struct disk_extent **extents, |
| 3574 | int *nr_extents) |
| 3575 | { |
| 3576 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 3577 | struct btrfs_path *path; |
| 3578 | struct btrfs_file_extent_item *fi; |
| 3579 | struct extent_buffer *leaf; |
| 3580 | struct disk_extent *exts = *extents; |
| 3581 | struct btrfs_key found_key; |
| 3582 | u64 cur_pos; |
| 3583 | u64 last_byte; |
| 3584 | u32 nritems; |
| 3585 | int nr = 0; |
| 3586 | int max = *nr_extents; |
| 3587 | int ret; |
| 3588 | |
| 3589 | WARN_ON(!no_fragment && *extents); |
| 3590 | if (!exts) { |
| 3591 | max = 1; |
| 3592 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 3593 | if (!exts) |
| 3594 | return -ENOMEM; |
| 3595 | } |
| 3596 | |
| 3597 | path = btrfs_alloc_path(); |
| 3598 | BUG_ON(!path); |
| 3599 | |
| 3600 | cur_pos = extent_key->objectid - offset; |
| 3601 | last_byte = extent_key->objectid + extent_key->offset; |
| 3602 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 3603 | cur_pos, 0); |
| 3604 | if (ret < 0) |
| 3605 | goto out; |
| 3606 | if (ret > 0) { |
| 3607 | ret = -ENOENT; |
| 3608 | goto out; |
| 3609 | } |
| 3610 | |
| 3611 | while (1) { |
| 3612 | leaf = path->nodes[0]; |
| 3613 | nritems = btrfs_header_nritems(leaf); |
| 3614 | if (path->slots[0] >= nritems) { |
| 3615 | ret = btrfs_next_leaf(root, path); |
| 3616 | if (ret < 0) |
| 3617 | goto out; |
| 3618 | if (ret > 0) |
| 3619 | break; |
| 3620 | leaf = path->nodes[0]; |
| 3621 | } |
| 3622 | |
| 3623 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 3624 | if (found_key.offset != cur_pos || |
| 3625 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 3626 | found_key.objectid != reloc_inode->i_ino) |
| 3627 | break; |
| 3628 | |
| 3629 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3630 | struct btrfs_file_extent_item); |
| 3631 | if (btrfs_file_extent_type(leaf, fi) != |
| 3632 | BTRFS_FILE_EXTENT_REG || |
| 3633 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 3634 | break; |
| 3635 | |
| 3636 | if (nr == max) { |
| 3637 | struct disk_extent *old = exts; |
| 3638 | max *= 2; |
| 3639 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 3640 | memcpy(exts, old, sizeof(*exts) * nr); |
| 3641 | if (old != *extents) |
| 3642 | kfree(old); |
| 3643 | } |
| 3644 | |
| 3645 | exts[nr].disk_bytenr = |
| 3646 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 3647 | exts[nr].disk_num_bytes = |
| 3648 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 3649 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 3650 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3651 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 3652 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 3653 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 3654 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 3655 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 3656 | BUG_ON(exts[nr].offset > 0); |
| 3657 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 3658 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3659 | |
| 3660 | cur_pos += exts[nr].num_bytes; |
| 3661 | nr++; |
| 3662 | |
| 3663 | if (cur_pos + offset >= last_byte) |
| 3664 | break; |
| 3665 | |
| 3666 | if (no_fragment) { |
| 3667 | ret = 1; |
| 3668 | goto out; |
| 3669 | } |
| 3670 | path->slots[0]++; |
| 3671 | } |
| 3672 | |
| 3673 | WARN_ON(cur_pos + offset > last_byte); |
| 3674 | if (cur_pos + offset < last_byte) { |
| 3675 | ret = -ENOENT; |
| 3676 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3677 | } |
| 3678 | ret = 0; |
| 3679 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3680 | btrfs_free_path(path); |
| 3681 | if (ret) { |
| 3682 | if (exts != *extents) |
| 3683 | kfree(exts); |
| 3684 | } else { |
| 3685 | *extents = exts; |
| 3686 | *nr_extents = nr; |
| 3687 | } |
| 3688 | return ret; |
| 3689 | } |
| 3690 | |
| 3691 | static int noinline replace_one_extent(struct btrfs_trans_handle *trans, |
| 3692 | struct btrfs_root *root, |
| 3693 | struct btrfs_path *path, |
| 3694 | struct btrfs_key *extent_key, |
| 3695 | struct btrfs_key *leaf_key, |
| 3696 | struct btrfs_ref_path *ref_path, |
| 3697 | struct disk_extent *new_extents, |
| 3698 | int nr_extents) |
| 3699 | { |
| 3700 | struct extent_buffer *leaf; |
| 3701 | struct btrfs_file_extent_item *fi; |
| 3702 | struct inode *inode = NULL; |
| 3703 | struct btrfs_key key; |
| 3704 | u64 lock_start = 0; |
| 3705 | u64 lock_end = 0; |
| 3706 | u64 num_bytes; |
| 3707 | u64 ext_offset; |
| 3708 | u64 first_pos; |
| 3709 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3710 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3711 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 3712 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3713 | int ret; |
| 3714 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3715 | memcpy(&key, leaf_key, sizeof(key)); |
| 3716 | first_pos = INT_LIMIT(loff_t) - extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3717 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3718 | if (key.objectid < ref_path->owner_objectid || |
| 3719 | (key.objectid == ref_path->owner_objectid && |
| 3720 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 3721 | key.objectid = ref_path->owner_objectid; |
| 3722 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 3723 | key.offset = 0; |
| 3724 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | while (1) { |
| 3728 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 3729 | if (ret < 0) |
| 3730 | goto out; |
| 3731 | |
| 3732 | leaf = path->nodes[0]; |
| 3733 | nritems = btrfs_header_nritems(leaf); |
| 3734 | next: |
| 3735 | if (extent_locked && ret > 0) { |
| 3736 | /* |
| 3737 | * the file extent item was modified by someone |
| 3738 | * before the extent got locked. |
| 3739 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3740 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 3741 | lock_end, GFP_NOFS); |
| 3742 | extent_locked = 0; |
| 3743 | } |
| 3744 | |
| 3745 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3746 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3747 | break; |
| 3748 | |
| 3749 | BUG_ON(extent_locked); |
| 3750 | ret = btrfs_next_leaf(root, path); |
| 3751 | if (ret < 0) |
| 3752 | goto out; |
| 3753 | if (ret > 0) |
| 3754 | break; |
| 3755 | leaf = path->nodes[0]; |
| 3756 | nritems = btrfs_header_nritems(leaf); |
| 3757 | } |
| 3758 | |
| 3759 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 3760 | |
| 3761 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 3762 | if ((key.objectid > ref_path->owner_objectid) || |
| 3763 | (key.objectid == ref_path->owner_objectid && |
| 3764 | key.type > BTRFS_EXTENT_DATA_KEY) || |
| 3765 | (key.offset >= first_pos + extent_key->offset)) |
| 3766 | break; |
| 3767 | } |
| 3768 | |
| 3769 | if (inode && key.objectid != inode->i_ino) { |
| 3770 | BUG_ON(extent_locked); |
| 3771 | btrfs_release_path(root, path); |
| 3772 | mutex_unlock(&inode->i_mutex); |
| 3773 | iput(inode); |
| 3774 | inode = NULL; |
| 3775 | continue; |
| 3776 | } |
| 3777 | |
| 3778 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 3779 | path->slots[0]++; |
| 3780 | ret = 1; |
| 3781 | goto next; |
| 3782 | } |
| 3783 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3784 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 3785 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 3786 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 3787 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3788 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 3789 | extent_key->objectid)) { |
| 3790 | path->slots[0]++; |
| 3791 | ret = 1; |
| 3792 | goto next; |
| 3793 | } |
| 3794 | |
| 3795 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 3796 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 3797 | |
| 3798 | if (first_pos > key.offset - ext_offset) |
| 3799 | first_pos = key.offset - ext_offset; |
| 3800 | |
| 3801 | if (!extent_locked) { |
| 3802 | lock_start = key.offset; |
| 3803 | lock_end = lock_start + num_bytes - 1; |
| 3804 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3805 | if (lock_start > key.offset || |
| 3806 | lock_end + 1 < key.offset + num_bytes) { |
| 3807 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 3808 | lock_start, lock_end, GFP_NOFS); |
| 3809 | extent_locked = 0; |
| 3810 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | if (!inode) { |
| 3814 | btrfs_release_path(root, path); |
| 3815 | |
| 3816 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 3817 | key.objectid, root); |
| 3818 | if (inode->i_state & I_NEW) { |
| 3819 | BTRFS_I(inode)->root = root; |
| 3820 | BTRFS_I(inode)->location.objectid = |
| 3821 | key.objectid; |
| 3822 | BTRFS_I(inode)->location.type = |
| 3823 | BTRFS_INODE_ITEM_KEY; |
| 3824 | BTRFS_I(inode)->location.offset = 0; |
| 3825 | btrfs_read_locked_inode(inode); |
| 3826 | unlock_new_inode(inode); |
| 3827 | } |
| 3828 | /* |
| 3829 | * some code call btrfs_commit_transaction while |
| 3830 | * holding the i_mutex, so we can't use mutex_lock |
| 3831 | * here. |
| 3832 | */ |
| 3833 | if (is_bad_inode(inode) || |
| 3834 | !mutex_trylock(&inode->i_mutex)) { |
| 3835 | iput(inode); |
| 3836 | inode = NULL; |
| 3837 | key.offset = (u64)-1; |
| 3838 | goto skip; |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | if (!extent_locked) { |
| 3843 | struct btrfs_ordered_extent *ordered; |
| 3844 | |
| 3845 | btrfs_release_path(root, path); |
| 3846 | |
| 3847 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 3848 | lock_end, GFP_NOFS); |
| 3849 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 3850 | lock_end); |
| 3851 | if (ordered && |
| 3852 | ordered->file_offset <= lock_end && |
| 3853 | ordered->file_offset + ordered->len > lock_start) { |
| 3854 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 3855 | lock_start, lock_end, GFP_NOFS); |
| 3856 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3857 | btrfs_put_ordered_extent(ordered); |
| 3858 | key.offset += num_bytes; |
| 3859 | goto skip; |
| 3860 | } |
| 3861 | if (ordered) |
| 3862 | btrfs_put_ordered_extent(ordered); |
| 3863 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3864 | extent_locked = 1; |
| 3865 | continue; |
| 3866 | } |
| 3867 | |
| 3868 | if (nr_extents == 1) { |
| 3869 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3870 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 3871 | new_extents[0].disk_bytenr); |
| 3872 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 3873 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3874 | btrfs_mark_buffer_dirty(leaf); |
| 3875 | |
| 3876 | btrfs_drop_extent_cache(inode, key.offset, |
| 3877 | key.offset + num_bytes - 1, 0); |
| 3878 | |
| 3879 | ret = btrfs_inc_extent_ref(trans, root, |
| 3880 | new_extents[0].disk_bytenr, |
| 3881 | new_extents[0].disk_num_bytes, |
| 3882 | leaf->start, |
| 3883 | root->root_key.objectid, |
| 3884 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3885 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3886 | BUG_ON(ret); |
| 3887 | |
| 3888 | ret = btrfs_free_extent(trans, root, |
| 3889 | extent_key->objectid, |
| 3890 | extent_key->offset, |
| 3891 | leaf->start, |
| 3892 | btrfs_header_owner(leaf), |
| 3893 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3894 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3895 | BUG_ON(ret); |
| 3896 | |
| 3897 | btrfs_release_path(root, path); |
| 3898 | key.offset += num_bytes; |
| 3899 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 3900 | BUG_ON(1); |
| 3901 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3902 | u64 alloc_hint; |
| 3903 | u64 extent_len; |
| 3904 | int i; |
| 3905 | /* |
| 3906 | * drop old extent pointer at first, then insert the |
| 3907 | * new pointers one bye one |
| 3908 | */ |
| 3909 | btrfs_release_path(root, path); |
| 3910 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 3911 | key.offset + num_bytes, |
| 3912 | key.offset, &alloc_hint); |
| 3913 | BUG_ON(ret); |
| 3914 | |
| 3915 | for (i = 0; i < nr_extents; i++) { |
| 3916 | if (ext_offset >= new_extents[i].num_bytes) { |
| 3917 | ext_offset -= new_extents[i].num_bytes; |
| 3918 | continue; |
| 3919 | } |
| 3920 | extent_len = min(new_extents[i].num_bytes - |
| 3921 | ext_offset, num_bytes); |
| 3922 | |
| 3923 | ret = btrfs_insert_empty_item(trans, root, |
| 3924 | path, &key, |
| 3925 | sizeof(*fi)); |
| 3926 | BUG_ON(ret); |
| 3927 | |
| 3928 | leaf = path->nodes[0]; |
| 3929 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 3930 | struct btrfs_file_extent_item); |
| 3931 | btrfs_set_file_extent_generation(leaf, fi, |
| 3932 | trans->transid); |
| 3933 | btrfs_set_file_extent_type(leaf, fi, |
| 3934 | BTRFS_FILE_EXTENT_REG); |
| 3935 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 3936 | new_extents[i].disk_bytenr); |
| 3937 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 3938 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3939 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 3940 | new_extents[i].ram_bytes); |
| 3941 | |
| 3942 | btrfs_set_file_extent_compression(leaf, fi, |
| 3943 | new_extents[i].compression); |
| 3944 | btrfs_set_file_extent_encryption(leaf, fi, |
| 3945 | new_extents[i].encryption); |
| 3946 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 3947 | new_extents[i].other_encoding); |
| 3948 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3949 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 3950 | extent_len); |
| 3951 | ext_offset += new_extents[i].offset; |
| 3952 | btrfs_set_file_extent_offset(leaf, fi, |
| 3953 | ext_offset); |
| 3954 | btrfs_mark_buffer_dirty(leaf); |
| 3955 | |
| 3956 | btrfs_drop_extent_cache(inode, key.offset, |
| 3957 | key.offset + extent_len - 1, 0); |
| 3958 | |
| 3959 | ret = btrfs_inc_extent_ref(trans, root, |
| 3960 | new_extents[i].disk_bytenr, |
| 3961 | new_extents[i].disk_num_bytes, |
| 3962 | leaf->start, |
| 3963 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3964 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3965 | BUG_ON(ret); |
| 3966 | btrfs_release_path(root, path); |
| 3967 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 3968 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3969 | |
| 3970 | ext_offset = 0; |
| 3971 | num_bytes -= extent_len; |
| 3972 | key.offset += extent_len; |
| 3973 | |
| 3974 | if (num_bytes == 0) |
| 3975 | break; |
| 3976 | } |
| 3977 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 3978 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3982 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 3983 | lock_end, GFP_NOFS); |
| 3984 | extent_locked = 0; |
| 3985 | } |
| 3986 | skip: |
| 3987 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
| 3988 | key.offset >= first_pos + extent_key->offset) |
| 3989 | break; |
| 3990 | |
| 3991 | cond_resched(); |
| 3992 | } |
| 3993 | ret = 0; |
| 3994 | out: |
| 3995 | btrfs_release_path(root, path); |
| 3996 | if (inode) { |
| 3997 | mutex_unlock(&inode->i_mutex); |
| 3998 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3999 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4000 | lock_end, GFP_NOFS); |
| 4001 | } |
| 4002 | iput(inode); |
| 4003 | } |
| 4004 | return ret; |
| 4005 | } |
| 4006 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4007 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 4008 | struct btrfs_root *root, |
| 4009 | struct extent_buffer *buf, u64 orig_start) |
| 4010 | { |
| 4011 | int level; |
| 4012 | int ret; |
| 4013 | |
| 4014 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 4015 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 4016 | |
| 4017 | level = btrfs_header_level(buf); |
| 4018 | if (level == 0) { |
| 4019 | struct btrfs_leaf_ref *ref; |
| 4020 | struct btrfs_leaf_ref *orig_ref; |
| 4021 | |
| 4022 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 4023 | if (!orig_ref) |
| 4024 | return -ENOENT; |
| 4025 | |
| 4026 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 4027 | if (!ref) { |
| 4028 | btrfs_free_leaf_ref(root, orig_ref); |
| 4029 | return -ENOMEM; |
| 4030 | } |
| 4031 | |
| 4032 | ref->nritems = orig_ref->nritems; |
| 4033 | memcpy(ref->extents, orig_ref->extents, |
| 4034 | sizeof(ref->extents[0]) * ref->nritems); |
| 4035 | |
| 4036 | btrfs_free_leaf_ref(root, orig_ref); |
| 4037 | |
| 4038 | ref->root_gen = trans->transid; |
| 4039 | ref->bytenr = buf->start; |
| 4040 | ref->owner = btrfs_header_owner(buf); |
| 4041 | ref->generation = btrfs_header_generation(buf); |
| 4042 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 4043 | WARN_ON(ret); |
| 4044 | btrfs_free_leaf_ref(root, ref); |
| 4045 | } |
| 4046 | return 0; |
| 4047 | } |
| 4048 | |
| 4049 | static int noinline invalidate_extent_cache(struct btrfs_root *root, |
| 4050 | struct extent_buffer *leaf, |
| 4051 | struct btrfs_block_group_cache *group, |
| 4052 | struct btrfs_root *target_root) |
| 4053 | { |
| 4054 | struct btrfs_key key; |
| 4055 | struct inode *inode = NULL; |
| 4056 | struct btrfs_file_extent_item *fi; |
| 4057 | u64 num_bytes; |
| 4058 | u64 skip_objectid = 0; |
| 4059 | u32 nritems; |
| 4060 | u32 i; |
| 4061 | |
| 4062 | nritems = btrfs_header_nritems(leaf); |
| 4063 | for (i = 0; i < nritems; i++) { |
| 4064 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4065 | if (key.objectid == skip_objectid || |
| 4066 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 4067 | continue; |
| 4068 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4069 | if (btrfs_file_extent_type(leaf, fi) == |
| 4070 | BTRFS_FILE_EXTENT_INLINE) |
| 4071 | continue; |
| 4072 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4073 | continue; |
| 4074 | if (!inode || inode->i_ino != key.objectid) { |
| 4075 | iput(inode); |
| 4076 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 4077 | key.objectid, target_root, 1); |
| 4078 | } |
| 4079 | if (!inode) { |
| 4080 | skip_objectid = key.objectid; |
| 4081 | continue; |
| 4082 | } |
| 4083 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4084 | |
| 4085 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4086 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4087 | btrfs_drop_extent_cache(inode, key.offset, |
| 4088 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4089 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4090 | key.offset + num_bytes - 1, GFP_NOFS); |
| 4091 | cond_resched(); |
| 4092 | } |
| 4093 | iput(inode); |
| 4094 | return 0; |
| 4095 | } |
| 4096 | |
| 4097 | static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
| 4098 | struct btrfs_root *root, |
| 4099 | struct extent_buffer *leaf, |
| 4100 | struct btrfs_block_group_cache *group, |
| 4101 | struct inode *reloc_inode) |
| 4102 | { |
| 4103 | struct btrfs_key key; |
| 4104 | struct btrfs_key extent_key; |
| 4105 | struct btrfs_file_extent_item *fi; |
| 4106 | struct btrfs_leaf_ref *ref; |
| 4107 | struct disk_extent *new_extent; |
| 4108 | u64 bytenr; |
| 4109 | u64 num_bytes; |
| 4110 | u32 nritems; |
| 4111 | u32 i; |
| 4112 | int ext_index; |
| 4113 | int nr_extent; |
| 4114 | int ret; |
| 4115 | |
| 4116 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 4117 | BUG_ON(!new_extent); |
| 4118 | |
| 4119 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 4120 | BUG_ON(!ref); |
| 4121 | |
| 4122 | ext_index = -1; |
| 4123 | nritems = btrfs_header_nritems(leaf); |
| 4124 | for (i = 0; i < nritems; i++) { |
| 4125 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4126 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 4127 | continue; |
| 4128 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4129 | if (btrfs_file_extent_type(leaf, fi) == |
| 4130 | BTRFS_FILE_EXTENT_INLINE) |
| 4131 | continue; |
| 4132 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4133 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4134 | if (bytenr == 0) |
| 4135 | continue; |
| 4136 | |
| 4137 | ext_index++; |
| 4138 | if (bytenr >= group->key.objectid + group->key.offset || |
| 4139 | bytenr + num_bytes <= group->key.objectid) |
| 4140 | continue; |
| 4141 | |
| 4142 | extent_key.objectid = bytenr; |
| 4143 | extent_key.offset = num_bytes; |
| 4144 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4145 | nr_extent = 1; |
| 4146 | ret = get_new_locations(reloc_inode, &extent_key, |
| 4147 | group->key.objectid, 1, |
| 4148 | &new_extent, &nr_extent); |
| 4149 | if (ret > 0) |
| 4150 | continue; |
| 4151 | BUG_ON(ret < 0); |
| 4152 | |
| 4153 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 4154 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 4155 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 4156 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 4157 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4158 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4159 | new_extent->disk_bytenr); |
| 4160 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4161 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4162 | btrfs_mark_buffer_dirty(leaf); |
| 4163 | |
| 4164 | ret = btrfs_inc_extent_ref(trans, root, |
| 4165 | new_extent->disk_bytenr, |
| 4166 | new_extent->disk_num_bytes, |
| 4167 | leaf->start, |
| 4168 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4169 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4170 | BUG_ON(ret); |
| 4171 | ret = btrfs_free_extent(trans, root, |
| 4172 | bytenr, num_bytes, leaf->start, |
| 4173 | btrfs_header_owner(leaf), |
| 4174 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4175 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4176 | BUG_ON(ret); |
| 4177 | cond_resched(); |
| 4178 | } |
| 4179 | kfree(new_extent); |
| 4180 | BUG_ON(ext_index + 1 != ref->nritems); |
| 4181 | btrfs_free_leaf_ref(root, ref); |
| 4182 | return 0; |
| 4183 | } |
| 4184 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4185 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 4186 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4187 | { |
| 4188 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4189 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4190 | |
| 4191 | if (root->reloc_root) { |
| 4192 | reloc_root = root->reloc_root; |
| 4193 | root->reloc_root = NULL; |
| 4194 | list_add(&reloc_root->dead_list, |
| 4195 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4196 | |
| 4197 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 4198 | reloc_root->node->start); |
| 4199 | btrfs_set_root_level(&root->root_item, |
| 4200 | btrfs_header_level(reloc_root->node)); |
| 4201 | memset(&reloc_root->root_item.drop_progress, 0, |
| 4202 | sizeof(struct btrfs_disk_key)); |
| 4203 | reloc_root->root_item.drop_level = 0; |
| 4204 | |
| 4205 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 4206 | &reloc_root->root_key, |
| 4207 | &reloc_root->root_item); |
| 4208 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4209 | } |
| 4210 | return 0; |
| 4211 | } |
| 4212 | |
| 4213 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 4214 | { |
| 4215 | struct btrfs_trans_handle *trans; |
| 4216 | struct btrfs_root *reloc_root; |
| 4217 | struct btrfs_root *prev_root = NULL; |
| 4218 | struct list_head dead_roots; |
| 4219 | int ret; |
| 4220 | unsigned long nr; |
| 4221 | |
| 4222 | INIT_LIST_HEAD(&dead_roots); |
| 4223 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 4224 | |
| 4225 | while (!list_empty(&dead_roots)) { |
| 4226 | reloc_root = list_entry(dead_roots.prev, |
| 4227 | struct btrfs_root, dead_list); |
| 4228 | list_del_init(&reloc_root->dead_list); |
| 4229 | |
| 4230 | BUG_ON(reloc_root->commit_root != NULL); |
| 4231 | while (1) { |
| 4232 | trans = btrfs_join_transaction(root, 1); |
| 4233 | BUG_ON(!trans); |
| 4234 | |
| 4235 | mutex_lock(&root->fs_info->drop_mutex); |
| 4236 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 4237 | if (ret != -EAGAIN) |
| 4238 | break; |
| 4239 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4240 | |
| 4241 | nr = trans->blocks_used; |
| 4242 | ret = btrfs_end_transaction(trans, root); |
| 4243 | BUG_ON(ret); |
| 4244 | btrfs_btree_balance_dirty(root, nr); |
| 4245 | } |
| 4246 | |
| 4247 | free_extent_buffer(reloc_root->node); |
| 4248 | |
| 4249 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 4250 | &reloc_root->root_key); |
| 4251 | BUG_ON(ret); |
| 4252 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4253 | |
| 4254 | nr = trans->blocks_used; |
| 4255 | ret = btrfs_end_transaction(trans, root); |
| 4256 | BUG_ON(ret); |
| 4257 | btrfs_btree_balance_dirty(root, nr); |
| 4258 | |
| 4259 | kfree(prev_root); |
| 4260 | prev_root = reloc_root; |
| 4261 | } |
| 4262 | if (prev_root) { |
| 4263 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 4264 | kfree(prev_root); |
| 4265 | } |
| 4266 | return 0; |
| 4267 | } |
| 4268 | |
| 4269 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 4270 | { |
| 4271 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 4272 | return 0; |
| 4273 | } |
| 4274 | |
| 4275 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 4276 | { |
| 4277 | struct btrfs_root *reloc_root; |
| 4278 | struct btrfs_trans_handle *trans; |
| 4279 | struct btrfs_key location; |
| 4280 | int found; |
| 4281 | int ret; |
| 4282 | |
| 4283 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 4284 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 4285 | BUG_ON(ret); |
| 4286 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 4287 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 4288 | |
| 4289 | if (found) { |
| 4290 | trans = btrfs_start_transaction(root, 1); |
| 4291 | BUG_ON(!trans); |
| 4292 | ret = btrfs_commit_transaction(trans, root); |
| 4293 | BUG_ON(ret); |
| 4294 | } |
| 4295 | |
| 4296 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 4297 | location.offset = (u64)-1; |
| 4298 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 4299 | |
| 4300 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 4301 | BUG_ON(!reloc_root); |
| 4302 | btrfs_orphan_cleanup(reloc_root); |
| 4303 | return 0; |
| 4304 | } |
| 4305 | |
| 4306 | static int noinline init_reloc_tree(struct btrfs_trans_handle *trans, |
| 4307 | struct btrfs_root *root) |
| 4308 | { |
| 4309 | struct btrfs_root *reloc_root; |
| 4310 | struct extent_buffer *eb; |
| 4311 | struct btrfs_root_item *root_item; |
| 4312 | struct btrfs_key root_key; |
| 4313 | int ret; |
| 4314 | |
| 4315 | BUG_ON(!root->ref_cows); |
| 4316 | if (root->reloc_root) |
| 4317 | return 0; |
| 4318 | |
| 4319 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 4320 | BUG_ON(!root_item); |
| 4321 | |
| 4322 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 4323 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 4324 | BUG_ON(ret); |
| 4325 | |
| 4326 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 4327 | root_key.offset = root->root_key.objectid; |
| 4328 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 4329 | |
| 4330 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 4331 | btrfs_set_root_refs(root_item, 0); |
| 4332 | btrfs_set_root_bytenr(root_item, eb->start); |
| 4333 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4334 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4335 | |
| 4336 | btrfs_tree_unlock(eb); |
| 4337 | free_extent_buffer(eb); |
| 4338 | |
| 4339 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 4340 | &root_key, root_item); |
| 4341 | BUG_ON(ret); |
| 4342 | kfree(root_item); |
| 4343 | |
| 4344 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 4345 | &root_key); |
| 4346 | BUG_ON(!reloc_root); |
| 4347 | reloc_root->last_trans = trans->transid; |
| 4348 | reloc_root->commit_root = NULL; |
| 4349 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 4350 | |
| 4351 | root->reloc_root = reloc_root; |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
| 4355 | /* |
| 4356 | * Core function of space balance. |
| 4357 | * |
| 4358 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4359 | * counted roots. There is one reloc tree for each subvol, and all |
| 4360 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 4361 | * of the latest committed roots of subvols (root->commit_root). |
| 4362 | * |
| 4363 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 4364 | * COW the block through subvol's reloc tree, then update block pointer |
| 4365 | * in the subvol to point to the new block. Since all reloc trees share |
| 4366 | * same root key objectid, doing special handing for tree blocks owned |
| 4367 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 4368 | * we can use the resulting new block directly when the same block is |
| 4369 | * required to COW again through other reloc trees. By this way, relocated |
| 4370 | * tree blocks are shared between reloc trees, so they are also shared |
| 4371 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4372 | */ |
| 4373 | static int noinline relocate_one_path(struct btrfs_trans_handle *trans, |
| 4374 | struct btrfs_root *root, |
| 4375 | struct btrfs_path *path, |
| 4376 | struct btrfs_key *first_key, |
| 4377 | struct btrfs_ref_path *ref_path, |
| 4378 | struct btrfs_block_group_cache *group, |
| 4379 | struct inode *reloc_inode) |
| 4380 | { |
| 4381 | struct btrfs_root *reloc_root; |
| 4382 | struct extent_buffer *eb = NULL; |
| 4383 | struct btrfs_key *keys; |
| 4384 | u64 *nodes; |
| 4385 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4386 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4387 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4388 | int ret; |
| 4389 | |
| 4390 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 4391 | lowest_level = ref_path->owner_objectid; |
| 4392 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4393 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 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 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4402 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 4403 | ret = init_reloc_tree(trans, root); |
| 4404 | BUG_ON(ret); |
| 4405 | reloc_root = root->reloc_root; |
| 4406 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4407 | shared_level = ref_path->shared_level; |
| 4408 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4409 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4410 | keys = ref_path->node_keys; |
| 4411 | nodes = ref_path->new_nodes; |
| 4412 | memset(&keys[shared_level + 1], 0, |
| 4413 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 4414 | memset(&nodes[shared_level + 1], 0, |
| 4415 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4416 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4417 | if (nodes[lowest_level] == 0) { |
| 4418 | path->lowest_level = lowest_level; |
| 4419 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 4420 | 0, 1); |
| 4421 | BUG_ON(ret); |
| 4422 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 4423 | eb = path->nodes[level]; |
| 4424 | if (!eb || eb == reloc_root->node) |
| 4425 | break; |
| 4426 | nodes[level] = eb->start; |
| 4427 | if (level == 0) |
| 4428 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 4429 | else |
| 4430 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 4431 | } |
| 4432 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 4433 | eb = path->nodes[0]; |
| 4434 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 4435 | group, reloc_inode); |
| 4436 | BUG_ON(ret); |
| 4437 | } |
| 4438 | btrfs_release_path(reloc_root, path); |
| 4439 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4440 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4441 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4442 | BUG_ON(ret); |
| 4443 | } |
| 4444 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4445 | /* |
| 4446 | * replace tree blocks in the fs tree with tree blocks in |
| 4447 | * the reloc tree. |
| 4448 | */ |
| 4449 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 4450 | BUG_ON(ret < 0); |
| 4451 | |
| 4452 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4453 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 4454 | 0, 0); |
| 4455 | BUG_ON(ret); |
| 4456 | extent_buffer_get(path->nodes[0]); |
| 4457 | eb = path->nodes[0]; |
| 4458 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4459 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 4460 | BUG_ON(ret); |
| 4461 | free_extent_buffer(eb); |
| 4462 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4463 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4464 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4465 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4466 | return 0; |
| 4467 | } |
| 4468 | |
| 4469 | static int noinline relocate_tree_block(struct btrfs_trans_handle *trans, |
| 4470 | struct btrfs_root *root, |
| 4471 | struct btrfs_path *path, |
| 4472 | struct btrfs_key *first_key, |
| 4473 | struct btrfs_ref_path *ref_path) |
| 4474 | { |
| 4475 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4476 | |
| 4477 | ret = relocate_one_path(trans, root, path, first_key, |
| 4478 | ref_path, NULL, NULL); |
| 4479 | BUG_ON(ret); |
| 4480 | |
| 4481 | if (root == root->fs_info->extent_root) |
| 4482 | btrfs_extent_post_op(trans, root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4483 | |
| 4484 | return 0; |
| 4485 | } |
| 4486 | |
| 4487 | static int noinline del_extent_zero(struct btrfs_trans_handle *trans, |
| 4488 | struct btrfs_root *extent_root, |
| 4489 | struct btrfs_path *path, |
| 4490 | struct btrfs_key *extent_key) |
| 4491 | { |
| 4492 | int ret; |
| 4493 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4494 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 4495 | if (ret) |
| 4496 | goto out; |
| 4497 | ret = btrfs_del_item(trans, extent_root, path); |
| 4498 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4499 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4500 | return ret; |
| 4501 | } |
| 4502 | |
| 4503 | static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info, |
| 4504 | struct btrfs_ref_path *ref_path) |
| 4505 | { |
| 4506 | struct btrfs_key root_key; |
| 4507 | |
| 4508 | root_key.objectid = ref_path->root_objectid; |
| 4509 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 4510 | if (is_cowonly_root(ref_path->root_objectid)) |
| 4511 | root_key.offset = 0; |
| 4512 | else |
| 4513 | root_key.offset = (u64)-1; |
| 4514 | |
| 4515 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 4516 | } |
| 4517 | |
| 4518 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
| 4519 | struct btrfs_path *path, |
| 4520 | struct btrfs_key *extent_key, |
| 4521 | struct btrfs_block_group_cache *group, |
| 4522 | struct inode *reloc_inode, int pass) |
| 4523 | { |
| 4524 | struct btrfs_trans_handle *trans; |
| 4525 | struct btrfs_root *found_root; |
| 4526 | struct btrfs_ref_path *ref_path = NULL; |
| 4527 | struct disk_extent *new_extents = NULL; |
| 4528 | int nr_extents = 0; |
| 4529 | int loops; |
| 4530 | int ret; |
| 4531 | int level; |
| 4532 | struct btrfs_key first_key; |
| 4533 | u64 prev_block = 0; |
| 4534 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4535 | |
| 4536 | trans = btrfs_start_transaction(extent_root, 1); |
| 4537 | BUG_ON(!trans); |
| 4538 | |
| 4539 | if (extent_key->objectid == 0) { |
| 4540 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 4541 | goto out; |
| 4542 | } |
| 4543 | |
| 4544 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 4545 | if (!ref_path) { |
| 4546 | ret = -ENOMEM; |
| 4547 | goto out; |
| 4548 | } |
| 4549 | |
| 4550 | for (loops = 0; ; loops++) { |
| 4551 | if (loops == 0) { |
| 4552 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 4553 | extent_key->objectid); |
| 4554 | } else { |
| 4555 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 4556 | } |
| 4557 | if (ret < 0) |
| 4558 | goto out; |
| 4559 | if (ret > 0) |
| 4560 | break; |
| 4561 | |
| 4562 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 4563 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 4564 | continue; |
| 4565 | |
| 4566 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 4567 | BUG_ON(!found_root); |
| 4568 | /* |
| 4569 | * for reference counted tree, only process reference paths |
| 4570 | * rooted at the latest committed root. |
| 4571 | */ |
| 4572 | if (found_root->ref_cows && |
| 4573 | ref_path->root_generation != found_root->root_key.offset) |
| 4574 | continue; |
| 4575 | |
| 4576 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 4577 | if (pass == 0) { |
| 4578 | /* |
| 4579 | * copy data extents to new locations |
| 4580 | */ |
| 4581 | u64 group_start = group->key.objectid; |
| 4582 | ret = relocate_data_extent(reloc_inode, |
| 4583 | extent_key, |
| 4584 | group_start); |
| 4585 | if (ret < 0) |
| 4586 | goto out; |
| 4587 | break; |
| 4588 | } |
| 4589 | level = 0; |
| 4590 | } else { |
| 4591 | level = ref_path->owner_objectid; |
| 4592 | } |
| 4593 | |
| 4594 | if (prev_block != ref_path->nodes[level]) { |
| 4595 | struct extent_buffer *eb; |
| 4596 | u64 block_start = ref_path->nodes[level]; |
| 4597 | u64 block_size = btrfs_level_size(found_root, level); |
| 4598 | |
| 4599 | eb = read_tree_block(found_root, block_start, |
| 4600 | block_size, 0); |
| 4601 | btrfs_tree_lock(eb); |
| 4602 | BUG_ON(level != btrfs_header_level(eb)); |
| 4603 | |
| 4604 | if (level == 0) |
| 4605 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 4606 | else |
| 4607 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 4608 | |
| 4609 | btrfs_tree_unlock(eb); |
| 4610 | free_extent_buffer(eb); |
| 4611 | prev_block = block_start; |
| 4612 | } |
| 4613 | |
| 4614 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID && |
| 4615 | pass >= 2) { |
| 4616 | /* |
| 4617 | * use fallback method to process the remaining |
| 4618 | * references. |
| 4619 | */ |
| 4620 | if (!new_extents) { |
| 4621 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 4622 | new_extents = kmalloc(sizeof(*new_extents), |
| 4623 | GFP_NOFS); |
| 4624 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4625 | ret = get_new_locations(reloc_inode, |
| 4626 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 4627 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4628 | &new_extents, |
| 4629 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 4630 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4631 | goto out; |
| 4632 | } |
| 4633 | btrfs_record_root_in_trans(found_root); |
| 4634 | ret = replace_one_extent(trans, found_root, |
| 4635 | path, extent_key, |
| 4636 | &first_key, ref_path, |
| 4637 | new_extents, nr_extents); |
| 4638 | if (ret < 0) |
| 4639 | goto out; |
| 4640 | continue; |
| 4641 | } |
| 4642 | |
| 4643 | btrfs_record_root_in_trans(found_root); |
| 4644 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4645 | ret = relocate_tree_block(trans, found_root, path, |
| 4646 | &first_key, ref_path); |
| 4647 | } else { |
| 4648 | /* |
| 4649 | * try to update data extent references while |
| 4650 | * keeping metadata shared between snapshots. |
| 4651 | */ |
| 4652 | ret = relocate_one_path(trans, found_root, path, |
| 4653 | &first_key, ref_path, |
| 4654 | group, reloc_inode); |
| 4655 | } |
| 4656 | if (ret < 0) |
| 4657 | goto out; |
| 4658 | } |
| 4659 | ret = 0; |
| 4660 | out: |
| 4661 | btrfs_end_transaction(trans, extent_root); |
| 4662 | kfree(new_extents); |
| 4663 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4664 | return ret; |
| 4665 | } |
| 4666 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4667 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 4668 | { |
| 4669 | u64 num_devices; |
| 4670 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 4671 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 4672 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4673 | num_devices = root->fs_info->fs_devices->num_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4674 | if (num_devices == 1) { |
| 4675 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 4676 | stripped = flags & ~stripped; |
| 4677 | |
| 4678 | /* turn raid0 into single device chunks */ |
| 4679 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4680 | return stripped; |
| 4681 | |
| 4682 | /* turn mirroring into duplication */ |
| 4683 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 4684 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4685 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 4686 | return flags; |
| 4687 | } else { |
| 4688 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4689 | if (flags & stripped) |
| 4690 | return flags; |
| 4691 | |
| 4692 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 4693 | stripped = flags & ~stripped; |
| 4694 | |
| 4695 | /* switch duplicated blocks with raid1 */ |
| 4696 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 4697 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 4698 | |
| 4699 | /* turn single device chunks into raid0 */ |
| 4700 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 4701 | } |
| 4702 | return flags; |
| 4703 | } |
| 4704 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4705 | int __alloc_chunk_for_shrink(struct btrfs_root *root, |
| 4706 | struct btrfs_block_group_cache *shrink_block_group, |
| 4707 | int force) |
| 4708 | { |
| 4709 | struct btrfs_trans_handle *trans; |
| 4710 | u64 new_alloc_flags; |
| 4711 | u64 calc; |
| 4712 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4713 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4714 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4715 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4716 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4717 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4718 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4719 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4720 | new_alloc_flags = update_block_group_flags(root, |
| 4721 | shrink_block_group->flags); |
| 4722 | if (new_alloc_flags != shrink_block_group->flags) { |
| 4723 | calc = |
| 4724 | btrfs_block_group_used(&shrink_block_group->item); |
| 4725 | } else { |
| 4726 | calc = shrink_block_group->key.offset; |
| 4727 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4728 | spin_unlock(&shrink_block_group->lock); |
| 4729 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4730 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 4731 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4732 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4733 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4734 | } else |
| 4735 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4736 | return 0; |
| 4737 | } |
| 4738 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4739 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 4740 | struct btrfs_root *root, |
| 4741 | u64 objectid, u64 size) |
| 4742 | { |
| 4743 | struct btrfs_path *path; |
| 4744 | struct btrfs_inode_item *item; |
| 4745 | struct extent_buffer *leaf; |
| 4746 | int ret; |
| 4747 | |
| 4748 | path = btrfs_alloc_path(); |
| 4749 | if (!path) |
| 4750 | return -ENOMEM; |
| 4751 | |
| 4752 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 4753 | if (ret) |
| 4754 | goto out; |
| 4755 | |
| 4756 | leaf = path->nodes[0]; |
| 4757 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 4758 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 4759 | btrfs_set_inode_generation(leaf, item, 1); |
| 4760 | btrfs_set_inode_size(leaf, item, size); |
| 4761 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 4762 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM | |
| 4763 | BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4764 | btrfs_mark_buffer_dirty(leaf); |
| 4765 | btrfs_release_path(root, path); |
| 4766 | out: |
| 4767 | btrfs_free_path(path); |
| 4768 | return ret; |
| 4769 | } |
| 4770 | |
| 4771 | static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info, |
| 4772 | struct btrfs_block_group_cache *group) |
| 4773 | { |
| 4774 | struct inode *inode = NULL; |
| 4775 | struct btrfs_trans_handle *trans; |
| 4776 | struct btrfs_root *root; |
| 4777 | struct btrfs_key root_key; |
| 4778 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 4779 | int err = 0; |
| 4780 | |
| 4781 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 4782 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 4783 | root_key.offset = (u64)-1; |
| 4784 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 4785 | if (IS_ERR(root)) |
| 4786 | return ERR_CAST(root); |
| 4787 | |
| 4788 | trans = btrfs_start_transaction(root, 1); |
| 4789 | BUG_ON(!trans); |
| 4790 | |
| 4791 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 4792 | if (err) |
| 4793 | goto out; |
| 4794 | |
| 4795 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 4796 | BUG_ON(err); |
| 4797 | |
| 4798 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4799 | group->key.offset, 0, group->key.offset, |
| 4800 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4801 | BUG_ON(err); |
| 4802 | |
| 4803 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 4804 | if (inode->i_state & I_NEW) { |
| 4805 | BTRFS_I(inode)->root = root; |
| 4806 | BTRFS_I(inode)->location.objectid = objectid; |
| 4807 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 4808 | BTRFS_I(inode)->location.offset = 0; |
| 4809 | btrfs_read_locked_inode(inode); |
| 4810 | unlock_new_inode(inode); |
| 4811 | BUG_ON(is_bad_inode(inode)); |
| 4812 | } else { |
| 4813 | BUG_ON(1); |
| 4814 | } |
| 4815 | |
| 4816 | err = btrfs_orphan_add(trans, inode); |
| 4817 | out: |
| 4818 | btrfs_end_transaction(trans, root); |
| 4819 | if (err) { |
| 4820 | if (inode) |
| 4821 | iput(inode); |
| 4822 | inode = ERR_PTR(err); |
| 4823 | } |
| 4824 | return inode; |
| 4825 | } |
| 4826 | |
| 4827 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4828 | { |
| 4829 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4830 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4831 | struct btrfs_fs_info *info = root->fs_info; |
| 4832 | struct extent_buffer *leaf; |
| 4833 | struct inode *reloc_inode; |
| 4834 | struct btrfs_block_group_cache *block_group; |
| 4835 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 4836 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4837 | u64 cur_byte; |
| 4838 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4839 | u32 nritems; |
| 4840 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4841 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4842 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4843 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4844 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4845 | |
| 4846 | block_group = btrfs_lookup_block_group(info, group_start); |
| 4847 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4848 | |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 4849 | printk("btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4850 | (unsigned long long)block_group->key.objectid, |
| 4851 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 4852 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4853 | path = btrfs_alloc_path(); |
| 4854 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 4855 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4856 | reloc_inode = create_reloc_inode(info, block_group); |
| 4857 | BUG_ON(IS_ERR(reloc_inode)); |
| 4858 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4859 | __alloc_chunk_for_shrink(root, block_group, 1); |
| 4860 | block_group->ro = 1; |
| 4861 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 4862 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4863 | btrfs_start_delalloc_inodes(info->tree_root); |
| 4864 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4865 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 4866 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4867 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4868 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4869 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4870 | key.offset = 0; |
| 4871 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4872 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4873 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4874 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 4875 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4876 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4877 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 4878 | btrfs_clean_old_snapshots(info->tree_root); |
| 4879 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 4880 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 4881 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4882 | while(1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4883 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4884 | if (ret < 0) |
| 4885 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4886 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4887 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4888 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4889 | if (path->slots[0] >= nritems) { |
| 4890 | ret = btrfs_next_leaf(root, path); |
| 4891 | if (ret < 0) |
| 4892 | goto out; |
| 4893 | if (ret == 1) { |
| 4894 | ret = 0; |
| 4895 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4896 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4897 | leaf = path->nodes[0]; |
| 4898 | nritems = btrfs_header_nritems(leaf); |
| 4899 | } |
| 4900 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4901 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4902 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4903 | if (key.objectid >= block_group->key.objectid + |
| 4904 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 4905 | break; |
| 4906 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4907 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4908 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4909 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4910 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4911 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 4912 | } |
| 4913 | progress = 1; |
| 4914 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4915 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 4916 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4917 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4918 | goto next; |
| 4919 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 4920 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4921 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4922 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4923 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4924 | |
| 4925 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 4926 | ret = relocate_one_extent(root, path, &key, block_group, |
| 4927 | reloc_inode, pass); |
| 4928 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 4929 | if (ret > 0) |
| 4930 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4931 | |
| 4932 | key.objectid = cur_byte; |
| 4933 | key.type = 0; |
| 4934 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4938 | |
| 4939 | if (pass == 0) { |
| 4940 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 4941 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
| 4942 | WARN_ON(reloc_inode->i_mapping->nrpages); |
| 4943 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4944 | |
| 4945 | if (total_found > 0) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4946 | printk("btrfs found %llu extents in pass %d\n", |
| 4947 | (unsigned long long)total_found, pass); |
| 4948 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 4949 | if (total_found == skipped && pass > 2) { |
| 4950 | iput(reloc_inode); |
| 4951 | reloc_inode = create_reloc_inode(info, block_group); |
| 4952 | pass = 0; |
| 4953 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4954 | goto again; |
| 4955 | } |
| 4956 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4957 | /* delete reloc_inode */ |
| 4958 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4959 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4960 | /* unpin extents in this range */ |
| 4961 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 4962 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4963 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4964 | spin_lock(&block_group->lock); |
| 4965 | WARN_ON(block_group->pinned > 0); |
| 4966 | WARN_ON(block_group->reserved > 0); |
| 4967 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 4968 | spin_unlock(&block_group->lock); |
| 4969 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4970 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4971 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4972 | return ret; |
| 4973 | } |
| 4974 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4975 | int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, |
| 4976 | struct btrfs_key *key) |
| 4977 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4978 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4979 | struct btrfs_key found_key; |
| 4980 | struct extent_buffer *leaf; |
| 4981 | int slot; |
| 4982 | |
| 4983 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 4984 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4985 | goto out; |
| 4986 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4987 | while(1) { |
| 4988 | slot = path->slots[0]; |
| 4989 | leaf = path->nodes[0]; |
| 4990 | if (slot >= btrfs_header_nritems(leaf)) { |
| 4991 | ret = btrfs_next_leaf(root, path); |
| 4992 | if (ret == 0) |
| 4993 | continue; |
| 4994 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4995 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4996 | break; |
| 4997 | } |
| 4998 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 4999 | |
| 5000 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5001 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 5002 | ret = 0; |
| 5003 | goto out; |
| 5004 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5005 | path->slots[0]++; |
| 5006 | } |
| 5007 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5008 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5009 | return ret; |
| 5010 | } |
| 5011 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5012 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 5013 | { |
| 5014 | struct btrfs_block_group_cache *block_group; |
| 5015 | struct rb_node *n; |
| 5016 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5017 | spin_lock(&info->block_group_cache_lock); |
| 5018 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 5019 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 5020 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5021 | rb_erase(&block_group->cache_node, |
| 5022 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 5023 | spin_unlock(&info->block_group_cache_lock); |
| 5024 | |
| 5025 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5026 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5027 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5028 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5029 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame^] | 5030 | |
| 5031 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5032 | } |
| 5033 | spin_unlock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5034 | return 0; |
| 5035 | } |
| 5036 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5037 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 5038 | { |
| 5039 | struct btrfs_path *path; |
| 5040 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5041 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5042 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5043 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5044 | struct btrfs_key key; |
| 5045 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5046 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5047 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5048 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5049 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5050 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5051 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5052 | path = btrfs_alloc_path(); |
| 5053 | if (!path) |
| 5054 | return -ENOMEM; |
| 5055 | |
| 5056 | while(1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5057 | ret = find_first_block_group(root, path, &key); |
| 5058 | if (ret > 0) { |
| 5059 | ret = 0; |
| 5060 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5061 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5062 | if (ret != 0) |
| 5063 | goto error; |
| 5064 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5065 | leaf = path->nodes[0]; |
| 5066 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5067 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5068 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5069 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5070 | break; |
| 5071 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5072 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5073 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5074 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5075 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5076 | read_extent_buffer(leaf, &cache->item, |
| 5077 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 5078 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5079 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5080 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5081 | key.objectid = found_key.objectid + found_key.offset; |
| 5082 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5083 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5084 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5085 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 5086 | btrfs_block_group_used(&cache->item), |
| 5087 | &space_info); |
| 5088 | BUG_ON(ret); |
| 5089 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5090 | down_write(&space_info->groups_sem); |
| 5091 | list_add_tail(&cache->list, &space_info->block_groups); |
| 5092 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5093 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5094 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5095 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 5096 | |
| 5097 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5098 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5099 | ret = 0; |
| 5100 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5101 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5102 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5103 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5104 | |
| 5105 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 5106 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5107 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5108 | u64 size) |
| 5109 | { |
| 5110 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5111 | struct btrfs_root *extent_root; |
| 5112 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5113 | |
| 5114 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5115 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5116 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
| 5117 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5118 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5119 | if (!cache) |
| 5120 | return -ENOMEM; |
| 5121 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5122 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5123 | cache->key.offset = size; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5124 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5125 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5126 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5127 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5128 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5129 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5130 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 5131 | cache->flags = type; |
| 5132 | btrfs_set_block_group_flags(&cache->item, type); |
| 5133 | |
| 5134 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 5135 | &cache->space_info); |
| 5136 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5137 | down_write(&cache->space_info->groups_sem); |
| 5138 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 5139 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5140 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5141 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5142 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5143 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5144 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 5145 | sizeof(cache->item)); |
| 5146 | BUG_ON(ret); |
| 5147 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 5148 | finish_current_insert(trans, extent_root, 0); |
| 5149 | ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5150 | BUG_ON(ret); |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 5151 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5152 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5153 | return 0; |
| 5154 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5155 | |
| 5156 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 5157 | struct btrfs_root *root, u64 group_start) |
| 5158 | { |
| 5159 | struct btrfs_path *path; |
| 5160 | struct btrfs_block_group_cache *block_group; |
| 5161 | struct btrfs_key key; |
| 5162 | int ret; |
| 5163 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5164 | root = root->fs_info->extent_root; |
| 5165 | |
| 5166 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 5167 | BUG_ON(!block_group); |
| 5168 | |
| 5169 | memcpy(&key, &block_group->key, sizeof(key)); |
| 5170 | |
| 5171 | path = btrfs_alloc_path(); |
| 5172 | BUG_ON(!path); |
| 5173 | |
| 5174 | btrfs_remove_free_space_cache(block_group); |
| 5175 | rb_erase(&block_group->cache_node, |
| 5176 | &root->fs_info->block_group_cache_tree); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5177 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5178 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5179 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5180 | |
| 5181 | /* |
| 5182 | memset(shrink_block_group, 0, sizeof(*shrink_block_group)); |
| 5183 | kfree(shrink_block_group); |
| 5184 | */ |
| 5185 | |
| 5186 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 5187 | if (ret > 0) |
| 5188 | ret = -EIO; |
| 5189 | if (ret < 0) |
| 5190 | goto out; |
| 5191 | |
| 5192 | ret = btrfs_del_item(trans, root, path); |
| 5193 | out: |
| 5194 | btrfs_free_path(path); |
| 5195 | return ret; |
| 5196 | } |