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