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