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 | */ |
| 18 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 20 | #include "ctree.h" |
| 21 | #include "disk-io.h" |
| 22 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 23 | #include "transaction.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 24 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 25 | static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 26 | *orig_root, u64 num_blocks, u64 search_start, |
| 27 | u64 search_end, u64 hint_block, |
| 28 | struct btrfs_key *ins, int data); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 29 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
| 30 | btrfs_root *extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 31 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 32 | btrfs_root *extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 33 | |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 34 | static void reada_extent_leaves(struct btrfs_root *root, |
| 35 | struct btrfs_path *path, u64 limit) |
| 36 | { |
| 37 | struct btrfs_node *node; |
| 38 | int i; |
| 39 | int nritems; |
| 40 | u64 item_objectid; |
| 41 | u64 blocknr; |
| 42 | int slot; |
| 43 | int ret; |
| 44 | |
| 45 | if (!path->nodes[1]) |
| 46 | return; |
| 47 | node = btrfs_buffer_node(path->nodes[1]); |
| 48 | slot = path->slots[1] + 1; |
| 49 | nritems = btrfs_header_nritems(&node->header); |
| 50 | for (i = slot; i < nritems && i < slot + 8; i++) { |
| 51 | item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key); |
| 52 | if (item_objectid > limit) |
| 53 | break; |
| 54 | blocknr = btrfs_node_blockptr(node, i); |
| 55 | ret = readahead_tree_block(root, blocknr); |
| 56 | if (ret) |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 61 | static int cache_block_group(struct btrfs_root *root, |
| 62 | struct btrfs_block_group_cache *block_group) |
| 63 | { |
| 64 | struct btrfs_path *path; |
| 65 | int ret; |
| 66 | struct btrfs_key key; |
| 67 | struct btrfs_leaf *leaf; |
| 68 | struct radix_tree_root *extent_radix; |
| 69 | int slot; |
| 70 | u64 i; |
| 71 | u64 last = 0; |
| 72 | u64 hole_size; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 73 | u64 limit; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 74 | int found = 0; |
| 75 | |
| 76 | root = root->fs_info->extent_root; |
| 77 | extent_radix = &root->fs_info->extent_map_radix; |
| 78 | |
| 79 | if (block_group->cached) |
| 80 | return 0; |
| 81 | if (block_group->data) |
| 82 | return 0; |
| 83 | path = btrfs_alloc_path(); |
| 84 | if (!path) |
| 85 | return -ENOMEM; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 86 | key.objectid = block_group->key.objectid; |
| 87 | key.flags = 0; |
| 88 | key.offset = 0; |
| 89 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 90 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 91 | if (ret < 0) |
| 92 | return ret; |
| 93 | if (ret && path->slots[0] > 0) |
| 94 | path->slots[0]--; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 95 | limit = block_group->key.objectid + block_group->key.offset; |
| 96 | reada_extent_leaves(root, path, limit); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 97 | while(1) { |
| 98 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 99 | slot = path->slots[0]; |
| 100 | if (slot >= btrfs_header_nritems(&leaf->header)) { |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 101 | reada_extent_leaves(root, path, limit); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 102 | ret = btrfs_next_leaf(root, path); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 103 | if (ret == 0) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 104 | continue; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 105 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 106 | if (found) { |
| 107 | hole_size = block_group->key.objectid + |
| 108 | block_group->key.offset - last; |
| 109 | } else { |
| 110 | last = block_group->key.objectid; |
| 111 | hole_size = block_group->key.offset; |
| 112 | } |
| 113 | for (i = 0; i < hole_size; i++) { |
| 114 | set_radix_bit(extent_radix, |
| 115 | last + i); |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); |
| 121 | if (key.objectid >= block_group->key.objectid + |
| 122 | block_group->key.offset) { |
| 123 | if (found) { |
| 124 | hole_size = block_group->key.objectid + |
| 125 | block_group->key.offset - last; |
| 126 | } else { |
| 127 | last = block_group->key.objectid; |
| 128 | hole_size = block_group->key.offset; |
| 129 | } |
| 130 | for (i = 0; i < hole_size; i++) { |
| 131 | set_radix_bit(extent_radix, last + i); |
| 132 | } |
| 133 | break; |
| 134 | } |
| 135 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
| 136 | if (!found) { |
| 137 | last = key.objectid + key.offset; |
| 138 | found = 1; |
| 139 | } else { |
| 140 | hole_size = key.objectid - last; |
| 141 | for (i = 0; i < hole_size; i++) { |
| 142 | set_radix_bit(extent_radix, last + i); |
| 143 | } |
| 144 | last = key.objectid + key.offset; |
| 145 | } |
| 146 | } |
| 147 | path->slots[0]++; |
| 148 | } |
| 149 | |
| 150 | block_group->cached = 1; |
| 151 | btrfs_free_path(path); |
| 152 | return 0; |
| 153 | } |
| 154 | |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 155 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
| 156 | btrfs_fs_info *info, |
| 157 | u64 blocknr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 158 | { |
| 159 | struct btrfs_block_group_cache *block_group; |
| 160 | int ret; |
| 161 | |
| 162 | ret = radix_tree_gang_lookup(&info->block_group_radix, |
| 163 | (void **)&block_group, |
| 164 | blocknr, 1); |
| 165 | if (ret) { |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 166 | if (block_group->key.objectid <= blocknr && blocknr <= |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 167 | block_group->key.objectid + block_group->key.offset) |
| 168 | return block_group; |
| 169 | } |
| 170 | ret = radix_tree_gang_lookup(&info->block_group_data_radix, |
| 171 | (void **)&block_group, |
| 172 | blocknr, 1); |
| 173 | if (ret) { |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 174 | if (block_group->key.objectid <= blocknr && blocknr <= |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 175 | block_group->key.objectid + block_group->key.offset) |
| 176 | return block_group; |
| 177 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
| 180 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 181 | static u64 leaf_range(struct btrfs_root *root) |
| 182 | { |
| 183 | u64 size = BTRFS_LEAF_DATA_SIZE(root); |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 184 | do_div(size, sizeof(struct btrfs_extent_item) + |
| 185 | sizeof(struct btrfs_item)); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 186 | return size; |
| 187 | } |
| 188 | |
| 189 | static u64 find_search_start(struct btrfs_root *root, |
| 190 | struct btrfs_block_group_cache **cache_ret, |
| 191 | u64 search_start, int num) |
| 192 | { |
| 193 | unsigned long gang[8]; |
| 194 | int ret; |
| 195 | struct btrfs_block_group_cache *cache = *cache_ret; |
| 196 | u64 last = max(search_start, cache->key.objectid); |
| 197 | |
| 198 | if (cache->data) |
| 199 | goto out; |
| 200 | if (num > 1) { |
| 201 | last = max(last, cache->last_prealloc); |
| 202 | } |
| 203 | again: |
| 204 | cache_block_group(root, cache); |
| 205 | while(1) { |
| 206 | ret = find_first_radix_bit(&root->fs_info->extent_map_radix, |
| 207 | gang, last, ARRAY_SIZE(gang)); |
| 208 | if (!ret) |
| 209 | goto out; |
| 210 | last = gang[ret-1] + 1; |
| 211 | if (num > 1) { |
| 212 | if (ret != ARRAY_SIZE(gang)) { |
| 213 | goto new_group; |
| 214 | } |
| 215 | if (gang[ret-1] - gang[0] > leaf_range(root)) { |
| 216 | continue; |
| 217 | } |
| 218 | } |
| 219 | if (gang[0] >= cache->key.objectid + cache->key.offset) { |
| 220 | goto new_group; |
| 221 | } |
| 222 | return gang[0]; |
| 223 | } |
| 224 | out: |
| 225 | return max(cache->last_alloc, search_start); |
| 226 | |
| 227 | new_group: |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 228 | cache = btrfs_lookup_block_group(root->fs_info, |
| 229 | last + cache->key.offset - 1); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 230 | if (!cache) { |
| 231 | return max((*cache_ret)->last_alloc, search_start); |
| 232 | } |
| 233 | cache = btrfs_find_block_group(root, cache, |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 234 | last + cache->key.offset - 1, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 235 | *cache_ret = cache; |
| 236 | goto again; |
| 237 | } |
| 238 | |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 239 | static u64 div_factor(u64 num, int factor) |
| 240 | { |
| 241 | num *= factor; |
| 242 | do_div(num, 10); |
| 243 | return num; |
| 244 | } |
| 245 | |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 246 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 247 | struct btrfs_block_group_cache |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 248 | *hint, u64 search_start, |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 249 | int data, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 250 | { |
| 251 | struct btrfs_block_group_cache *cache[8]; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 252 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 253 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 254 | struct radix_tree_root *radix; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 255 | struct radix_tree_root *swap_radix; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 256 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 257 | u64 last = 0; |
| 258 | u64 hint_last; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 259 | int i; |
| 260 | int ret; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 261 | int full_search = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 262 | int factor = 8; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 263 | int data_swap = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 264 | |
| 265 | if (!owner) |
| 266 | factor = 5; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 267 | |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 268 | if (data) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 269 | radix = &info->block_group_data_radix; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 270 | swap_radix = &info->block_group_radix; |
| 271 | } else { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 272 | radix = &info->block_group_radix; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 273 | swap_radix = &info->block_group_data_radix; |
| 274 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 275 | |
| 276 | if (search_start) { |
| 277 | struct btrfs_block_group_cache *shint; |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 278 | shint = btrfs_lookup_block_group(info, search_start); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 279 | if (shint->data == data) { |
| 280 | used = btrfs_block_group_used(&shint->item); |
| 281 | if (used + shint->pinned < |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 282 | div_factor(shint->key.offset, factor)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 283 | return shint; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | if (hint && hint->data == data) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 288 | used = btrfs_block_group_used(&hint->item); |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 289 | if (used + hint->pinned < |
| 290 | div_factor(hint->key.offset, factor)) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 291 | return hint; |
| 292 | } |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 293 | if (used >= div_factor(hint->key.offset, 8)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 294 | radix_tree_tag_clear(radix, |
| 295 | hint->key.objectid + |
| 296 | hint->key.offset - 1, |
| 297 | BTRFS_BLOCK_GROUP_AVAIL); |
| 298 | } |
Chris Mason | 8d7be55 | 2007-05-10 11:24:42 -0400 | [diff] [blame] | 299 | last = hint->key.offset * 3; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 300 | if (hint->key.objectid >= last) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 301 | last = max(search_start + hint->key.offset - 1, |
| 302 | hint->key.objectid - last); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 303 | else |
| 304 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 305 | hint_last = last; |
| 306 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 307 | if (hint) |
| 308 | hint_last = max(hint->key.objectid, search_start); |
| 309 | else |
| 310 | hint_last = search_start; |
| 311 | |
| 312 | last = hint_last; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 313 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 314 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 315 | ret = radix_tree_gang_lookup_tag(radix, (void **)cache, |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 316 | last, ARRAY_SIZE(cache), |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 317 | BTRFS_BLOCK_GROUP_AVAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 318 | if (!ret) |
| 319 | break; |
| 320 | for (i = 0; i < ret; i++) { |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 321 | last = cache[i]->key.objectid + |
| 322 | cache[i]->key.offset; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 323 | used = btrfs_block_group_used(&cache[i]->item); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 324 | if (used + cache[i]->pinned < |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 325 | div_factor(cache[i]->key.offset, factor)) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 326 | found_group = cache[i]; |
| 327 | goto found; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 328 | } |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 329 | if (used >= div_factor(cache[i]->key.offset, 8)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 330 | radix_tree_tag_clear(radix, |
| 331 | cache[i]->key.objectid + |
| 332 | cache[i]->key.offset - 1, |
| 333 | BTRFS_BLOCK_GROUP_AVAIL); |
| 334 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 335 | } |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 336 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 337 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 338 | last = hint_last; |
| 339 | again: |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 340 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 341 | ret = radix_tree_gang_lookup(radix, (void **)cache, |
| 342 | last, ARRAY_SIZE(cache)); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 343 | if (!ret) |
| 344 | break; |
| 345 | for (i = 0; i < ret; i++) { |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 346 | last = cache[i]->key.objectid + |
| 347 | cache[i]->key.offset; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 348 | used = btrfs_block_group_used(&cache[i]->item); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 349 | if (used + cache[i]->pinned < cache[i]->key.offset) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 350 | found_group = cache[i]; |
| 351 | goto found; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 352 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 353 | if (used >= cache[i]->key.offset) { |
| 354 | radix_tree_tag_clear(radix, |
| 355 | cache[i]->key.objectid + |
| 356 | cache[i]->key.offset - 1, |
| 357 | BTRFS_BLOCK_GROUP_AVAIL); |
| 358 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 359 | } |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 360 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 361 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 362 | if (!full_search) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 363 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 364 | full_search = 1; |
| 365 | goto again; |
| 366 | } |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 367 | if (!data_swap) { |
| 368 | struct radix_tree_root *tmp = radix; |
| 369 | data_swap = 1; |
| 370 | radix = swap_radix; |
| 371 | swap_radix = tmp; |
| 372 | last = search_start; |
| 373 | goto again; |
| 374 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 375 | if (!found_group) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 376 | ret = radix_tree_gang_lookup(radix, |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 377 | (void **)&found_group, 0, 1); |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 378 | if (ret == 0) { |
| 379 | ret = radix_tree_gang_lookup(swap_radix, |
| 380 | (void **)&found_group, |
| 381 | 0, 1); |
| 382 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 383 | BUG_ON(ret != 1); |
| 384 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 385 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 386 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 389 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 390 | struct btrfs_root *root, |
| 391 | u64 blocknr, u64 num_blocks) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 392 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 393 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 394 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 395 | struct btrfs_key key; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 396 | struct btrfs_leaf *l; |
| 397 | struct btrfs_extent_item *item; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 398 | struct btrfs_key ins; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 399 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 400 | |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 401 | find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 402 | &ins, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 403 | path = btrfs_alloc_path(); |
| 404 | BUG_ON(!path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 405 | key.objectid = blocknr; |
| 406 | key.flags = 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 407 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 408 | key.offset = num_blocks; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 409 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 410 | 0, 1); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 411 | if (ret != 0) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 412 | BUG(); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 413 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 414 | BUG_ON(ret != 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 415 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 416 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 417 | refs = btrfs_extent_refs(item); |
| 418 | btrfs_set_extent_refs(item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 419 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 420 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 421 | btrfs_release_path(root->fs_info->extent_root, path); |
| 422 | btrfs_free_path(path); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 423 | finish_current_insert(trans, root->fs_info->extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 424 | del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 428 | static int lookup_extent_ref(struct btrfs_trans_handle *trans, |
| 429 | struct btrfs_root *root, u64 blocknr, |
| 430 | u64 num_blocks, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 431 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 432 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 433 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 434 | struct btrfs_key key; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 435 | struct btrfs_leaf *l; |
| 436 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 437 | |
| 438 | path = btrfs_alloc_path(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 439 | key.objectid = blocknr; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 440 | key.offset = num_blocks; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 441 | key.flags = 0; |
| 442 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 443 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 444 | 0, 0); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 445 | if (ret != 0) |
| 446 | BUG(); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 447 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 448 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 449 | *refs = btrfs_extent_refs(item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 450 | btrfs_release_path(root->fs_info->extent_root, path); |
| 451 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 452 | return 0; |
| 453 | } |
| 454 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 455 | int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, |
| 456 | struct btrfs_root *root) |
| 457 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 458 | return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 459 | } |
| 460 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 461 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 462 | struct buffer_head *buf) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 463 | { |
| 464 | u64 blocknr; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 465 | struct btrfs_node *buf_node; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 466 | struct btrfs_leaf *buf_leaf; |
| 467 | struct btrfs_disk_key *key; |
| 468 | struct btrfs_file_extent_item *fi; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 469 | int i; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 470 | int leaf; |
| 471 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 472 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 473 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 474 | return 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 475 | buf_node = btrfs_buffer_node(buf); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 476 | leaf = btrfs_is_leaf(buf_node); |
| 477 | buf_leaf = btrfs_buffer_leaf(buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 478 | for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 479 | if (leaf) { |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 480 | u64 disk_blocknr; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 481 | key = &buf_leaf->items[i].key; |
| 482 | if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) |
| 483 | continue; |
| 484 | fi = btrfs_item_ptr(buf_leaf, i, |
| 485 | struct btrfs_file_extent_item); |
Chris Mason | 236454df | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 486 | if (btrfs_file_extent_type(fi) == |
| 487 | BTRFS_FILE_EXTENT_INLINE) |
| 488 | continue; |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 489 | disk_blocknr = btrfs_file_extent_disk_blocknr(fi); |
| 490 | if (disk_blocknr == 0) |
| 491 | continue; |
| 492 | ret = btrfs_inc_extent_ref(trans, root, disk_blocknr, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 493 | btrfs_file_extent_disk_num_blocks(fi)); |
| 494 | BUG_ON(ret); |
| 495 | } else { |
| 496 | blocknr = btrfs_node_blockptr(buf_node, i); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 497 | ret = btrfs_inc_extent_ref(trans, root, blocknr, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 498 | BUG_ON(ret); |
| 499 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 500 | } |
| 501 | return 0; |
| 502 | } |
| 503 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 504 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 505 | struct btrfs_root *root, |
| 506 | struct btrfs_path *path, |
| 507 | struct btrfs_block_group_cache *cache) |
| 508 | { |
| 509 | int ret; |
| 510 | int pending_ret; |
| 511 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 512 | struct btrfs_block_group_item *bi; |
| 513 | struct btrfs_key ins; |
| 514 | |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 515 | find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 516 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
| 517 | BUG_ON(ret); |
| 518 | bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
| 519 | struct btrfs_block_group_item); |
| 520 | memcpy(bi, &cache->item, sizeof(*bi)); |
| 521 | mark_buffer_dirty(path->nodes[0]); |
| 522 | btrfs_release_path(extent_root, path); |
| 523 | |
| 524 | finish_current_insert(trans, extent_root); |
| 525 | pending_ret = del_pending_extents(trans, extent_root); |
| 526 | if (ret) |
| 527 | return ret; |
| 528 | if (pending_ret) |
| 529 | return pending_ret; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 530 | if (cache->data) |
| 531 | cache->last_alloc = cache->first_free; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 532 | return 0; |
| 533 | |
| 534 | } |
| 535 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 536 | static int write_dirty_block_radix(struct btrfs_trans_handle *trans, |
| 537 | struct btrfs_root *root, |
| 538 | struct radix_tree_root *radix) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 539 | { |
| 540 | struct btrfs_block_group_cache *cache[8]; |
| 541 | int ret; |
| 542 | int err = 0; |
| 543 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 544 | int i; |
| 545 | struct btrfs_path *path; |
| 546 | |
| 547 | path = btrfs_alloc_path(); |
| 548 | if (!path) |
| 549 | return -ENOMEM; |
| 550 | |
| 551 | while(1) { |
| 552 | ret = radix_tree_gang_lookup_tag(radix, (void **)cache, |
| 553 | 0, ARRAY_SIZE(cache), |
| 554 | BTRFS_BLOCK_GROUP_DIRTY); |
| 555 | if (!ret) |
| 556 | break; |
| 557 | for (i = 0; i < ret; i++) { |
| 558 | radix_tree_tag_clear(radix, cache[i]->key.objectid + |
| 559 | cache[i]->key.offset - 1, |
| 560 | BTRFS_BLOCK_GROUP_DIRTY); |
| 561 | err = write_one_cache_group(trans, root, |
| 562 | path, cache[i]); |
| 563 | if (err) |
| 564 | werr = err; |
| 565 | } |
| 566 | } |
| 567 | btrfs_free_path(path); |
| 568 | return werr; |
| 569 | } |
| 570 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 571 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 572 | struct btrfs_root *root) |
| 573 | { |
| 574 | int ret; |
| 575 | int ret2; |
| 576 | ret = write_dirty_block_radix(trans, root, |
| 577 | &root->fs_info->block_group_radix); |
| 578 | ret2 = write_dirty_block_radix(trans, root, |
| 579 | &root->fs_info->block_group_data_radix); |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | if (ret2) |
| 583 | return ret2; |
| 584 | return 0; |
| 585 | } |
| 586 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 587 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 588 | struct btrfs_root *root, |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 589 | u64 blocknr, u64 num, int alloc, int mark_free, |
| 590 | int data) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 591 | { |
| 592 | struct btrfs_block_group_cache *cache; |
| 593 | struct btrfs_fs_info *info = root->fs_info; |
| 594 | u64 total = num; |
| 595 | u64 old_val; |
| 596 | u64 block_in_group; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 597 | u64 i; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 598 | int ret; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 599 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 600 | while(total) { |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 601 | cache = btrfs_lookup_block_group(info, blocknr); |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 602 | if (!cache) { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 603 | return -1; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 604 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 605 | block_in_group = blocknr - cache->key.objectid; |
| 606 | WARN_ON(block_in_group > cache->key.offset); |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 607 | radix_tree_tag_set(cache->radix, cache->key.objectid + |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 608 | cache->key.offset - 1, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 609 | BTRFS_BLOCK_GROUP_DIRTY); |
| 610 | |
| 611 | old_val = btrfs_block_group_used(&cache->item); |
| 612 | num = min(total, cache->key.offset - block_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 613 | if (alloc) { |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 614 | if (blocknr > cache->last_alloc) |
| 615 | cache->last_alloc = blocknr; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 616 | if (!cache->data) { |
| 617 | for (i = 0; i < num; i++) { |
| 618 | clear_radix_bit(&info->extent_map_radix, |
| 619 | blocknr + i); |
| 620 | } |
| 621 | } |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 622 | if (cache->data != data && |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 623 | old_val < (cache->key.offset >> 1)) { |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 624 | cache->data = data; |
| 625 | radix_tree_delete(cache->radix, |
| 626 | cache->key.objectid + |
| 627 | cache->key.offset - 1); |
| 628 | |
| 629 | if (data) { |
| 630 | cache->radix = |
| 631 | &info->block_group_data_radix; |
| 632 | cache->item.flags |= |
| 633 | BTRFS_BLOCK_GROUP_DATA; |
| 634 | } else { |
| 635 | cache->radix = &info->block_group_radix; |
| 636 | cache->item.flags &= |
| 637 | ~BTRFS_BLOCK_GROUP_DATA; |
| 638 | } |
| 639 | ret = radix_tree_insert(cache->radix, |
| 640 | cache->key.objectid + |
| 641 | cache->key.offset - 1, |
| 642 | (void *)cache); |
| 643 | } |
| 644 | old_val += num; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 645 | } else { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 646 | old_val -= num; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 647 | if (blocknr < cache->first_free) |
| 648 | cache->first_free = blocknr; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 649 | if (!cache->data && mark_free) { |
| 650 | for (i = 0; i < num; i++) { |
| 651 | set_radix_bit(&info->extent_map_radix, |
| 652 | blocknr + i); |
| 653 | } |
| 654 | } |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 655 | if (old_val < (cache->key.offset >> 1) && |
| 656 | old_val + num >= (cache->key.offset >> 1)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 657 | radix_tree_tag_set(cache->radix, |
| 658 | cache->key.objectid + |
| 659 | cache->key.offset - 1, |
| 660 | BTRFS_BLOCK_GROUP_AVAIL); |
| 661 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 662 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 663 | btrfs_set_block_group_used(&cache->item, old_val); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 664 | total -= num; |
| 665 | blocknr += num; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 666 | } |
| 667 | return 0; |
| 668 | } |
| 669 | |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 670 | static int try_remove_page(struct address_space *mapping, unsigned long index) |
| 671 | { |
| 672 | int ret; |
| 673 | ret = invalidate_mapping_pages(mapping, index, index); |
| 674 | return ret; |
| 675 | } |
| 676 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 677 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct |
| 678 | btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 679 | { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 680 | unsigned long gang[8]; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 681 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 682 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 88fd146 | 2007-03-16 08:56:18 -0400 | [diff] [blame] | 683 | u64 first = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 684 | int ret; |
| 685 | int i; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 686 | struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 687 | struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 688 | |
| 689 | while(1) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 690 | ret = find_first_radix_bit(pinned_radix, gang, 0, |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 691 | ARRAY_SIZE(gang)); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 692 | if (!ret) |
| 693 | break; |
Chris Mason | 88fd146 | 2007-03-16 08:56:18 -0400 | [diff] [blame] | 694 | if (!first) |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 695 | first = gang[0]; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 696 | for (i = 0; i < ret; i++) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 697 | clear_radix_bit(pinned_radix, gang[i]); |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 698 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 699 | gang[i]); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 700 | if (block_group) { |
| 701 | WARN_ON(block_group->pinned == 0); |
| 702 | block_group->pinned--; |
| 703 | if (gang[i] < block_group->last_alloc) |
| 704 | block_group->last_alloc = gang[i]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 705 | if (gang[i] < block_group->last_prealloc) |
| 706 | block_group->last_prealloc = gang[i]; |
| 707 | if (!block_group->data) |
| 708 | set_radix_bit(extent_radix, gang[i]); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 709 | } |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 710 | try_remove_page(btree_inode->i_mapping, |
| 711 | gang[i] << (PAGE_CACHE_SHIFT - |
| 712 | btree_inode->i_blkbits)); |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 713 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 714 | } |
| 715 | return 0; |
| 716 | } |
| 717 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 718 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
| 719 | btrfs_root *extent_root) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 720 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 721 | struct btrfs_key ins; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 722 | struct btrfs_extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 723 | int i; |
| 724 | int ret; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 725 | u64 super_blocks_used; |
| 726 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 727 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 728 | btrfs_set_extent_refs(&extent_item, 1); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 729 | ins.offset = 1; |
| 730 | ins.flags = 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 731 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5d0c3e6 | 2007-04-23 17:01:05 -0400 | [diff] [blame] | 732 | btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 733 | |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 734 | for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) { |
| 735 | ins.objectid = extent_root->fs_info->extent_tree_insert[i]; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 736 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
| 737 | btrfs_set_super_blocks_used(info->disk_super, |
| 738 | super_blocks_used + 1); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 739 | ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item, |
| 740 | sizeof(extent_item)); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 741 | BUG_ON(ret); |
| 742 | } |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 743 | extent_root->fs_info->extent_tree_insert_nr = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 744 | return 0; |
| 745 | } |
| 746 | |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 747 | static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 748 | { |
| 749 | int err; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 750 | struct btrfs_header *header; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 751 | struct buffer_head *bh; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 752 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 753 | if (!pending) { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 754 | bh = btrfs_find_tree_block(root, blocknr); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 755 | if (bh) { |
| 756 | if (buffer_uptodate(bh)) { |
| 757 | u64 transid = |
| 758 | root->fs_info->running_transaction->transid; |
| 759 | header = btrfs_buffer_header(bh); |
| 760 | if (btrfs_header_generation(header) == |
| 761 | transid) { |
| 762 | btrfs_block_release(root, bh); |
| 763 | return 0; |
| 764 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 765 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 766 | btrfs_block_release(root, bh); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 767 | } |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 768 | err = set_radix_bit(&root->fs_info->pinned_radix, blocknr); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 769 | if (!err) { |
| 770 | struct btrfs_block_group_cache *cache; |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 771 | cache = btrfs_lookup_block_group(root->fs_info, |
| 772 | blocknr); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 773 | if (cache) |
| 774 | cache->pinned++; |
| 775 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 776 | } else { |
| 777 | err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr); |
| 778 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 779 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 783 | /* |
| 784 | * remove an extent from the root, returns 0 on success |
| 785 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 786 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 787 | *root, u64 blocknr, u64 num_blocks, int pin, |
| 788 | int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 789 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 790 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 791 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 792 | struct btrfs_fs_info *info = root->fs_info; |
| 793 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 794 | int ret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 795 | struct btrfs_extent_item *ei; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 796 | struct btrfs_key ins; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 797 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 798 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 799 | key.objectid = blocknr; |
| 800 | key.flags = 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 801 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 802 | key.offset = num_blocks; |
| 803 | |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 804 | find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 805 | path = btrfs_alloc_path(); |
| 806 | BUG_ON(!path); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 807 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 808 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 809 | if (ret) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 810 | BUG(); |
| 811 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 812 | ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 813 | struct btrfs_extent_item); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 814 | BUG_ON(ei->refs == 0); |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 815 | refs = btrfs_extent_refs(ei) - 1; |
| 816 | btrfs_set_extent_refs(ei, refs); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 817 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 818 | if (refs == 0) { |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 819 | u64 super_blocks_used; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 820 | |
| 821 | if (pin) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 822 | ret = pin_down_block(root, blocknr, 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 823 | BUG_ON(ret); |
| 824 | } |
| 825 | |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 826 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
| 827 | btrfs_set_super_blocks_used(info->disk_super, |
| 828 | super_blocks_used - num_blocks); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 829 | ret = btrfs_del_item(trans, extent_root, path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 830 | if (ret) |
| 831 | BUG(); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 832 | ret = update_block_group(trans, root, blocknr, num_blocks, 0, |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 833 | mark_free, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 834 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 835 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 836 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 837 | finish_current_insert(trans, extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 838 | return ret; |
| 839 | } |
| 840 | |
| 841 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 842 | * find all the blocks marked as pending in the radix tree and remove |
| 843 | * them from the extent map |
| 844 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 845 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 846 | btrfs_root *extent_root) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 847 | { |
| 848 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 849 | int wret; |
| 850 | int err = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 851 | unsigned long gang[4]; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 852 | int i; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 853 | struct radix_tree_root *pending_radix; |
| 854 | struct radix_tree_root *pinned_radix; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 855 | struct btrfs_block_group_cache *cache; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 856 | |
| 857 | pending_radix = &extent_root->fs_info->pending_del_radix; |
| 858 | pinned_radix = &extent_root->fs_info->pinned_radix; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 859 | |
| 860 | while(1) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 861 | ret = find_first_radix_bit(pending_radix, gang, 0, |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 862 | ARRAY_SIZE(gang)); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 863 | if (!ret) |
| 864 | break; |
| 865 | for (i = 0; i < ret; i++) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 866 | wret = set_radix_bit(pinned_radix, gang[i]); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 867 | if (wret == 0) { |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 868 | cache = |
| 869 | btrfs_lookup_block_group(extent_root->fs_info, |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 870 | gang[i]); |
| 871 | if (cache) |
| 872 | cache->pinned++; |
| 873 | } |
| 874 | if (wret < 0) { |
| 875 | printk(KERN_CRIT "set_radix_bit, err %d\n", |
| 876 | wret); |
| 877 | BUG_ON(wret < 0); |
| 878 | } |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 879 | wret = clear_radix_bit(pending_radix, gang[i]); |
| 880 | BUG_ON(wret); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 881 | wret = __free_extent(trans, extent_root, |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 882 | gang[i], 1, 0, 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 883 | if (wret) |
| 884 | err = wret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 885 | } |
| 886 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 887 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /* |
| 891 | * remove an extent from the root, returns 0 on success |
| 892 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 893 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
| 894 | *root, u64 blocknr, u64 num_blocks, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 895 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 896 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 897 | int pending_ret; |
| 898 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 899 | |
| 900 | if (root == extent_root) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 901 | pin_down_block(root, blocknr, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 902 | return 0; |
| 903 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 904 | ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 905 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 906 | return ret ? ret : pending_ret; |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * walks the btree of allocated extents and find a hole of a given size. |
| 911 | * The key ins is changed to record the hole: |
| 912 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 913 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 914 | * ins->offset == number of blocks |
| 915 | * Any available blocks before search_start are skipped. |
| 916 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 917 | static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
| 918 | *orig_root, u64 num_blocks, u64 search_start, u64 |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 919 | search_end, u64 hint_block, |
| 920 | struct btrfs_key *ins, int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 921 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 922 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 923 | struct btrfs_key key; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 924 | int ret; |
| 925 | u64 hole_size = 0; |
| 926 | int slot = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 927 | u64 last_block = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 928 | u64 test_block; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 929 | u64 orig_search_start = search_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 930 | int start_found; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 931 | struct btrfs_leaf *l; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 932 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 933 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 934 | int total_needed = num_blocks; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 935 | int total_found = 0; |
| 936 | int fill_prealloc = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 937 | int level; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 938 | struct btrfs_block_group_cache *block_group; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 939 | int full_scan = 0; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 940 | int wrapped = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 941 | u64 limit; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 942 | |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 943 | ins->flags = 0; |
| 944 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
| 945 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 946 | level = btrfs_header_level(btrfs_buffer_header(root->node)); |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 947 | if (num_blocks == 0) { |
| 948 | fill_prealloc = 1; |
| 949 | num_blocks = 1; |
Chris Mason | 308535a | 2007-04-28 15:17:08 -0400 | [diff] [blame] | 950 | total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 951 | } |
Chris Mason | 85e55b1 | 2007-06-19 15:50:51 -0400 | [diff] [blame] | 952 | if (fill_prealloc) { |
| 953 | u64 first; |
| 954 | int nr = info->extent_tree_prealloc_nr; |
| 955 | first = info->extent_tree_prealloc[nr - 1]; |
| 956 | if (info->extent_tree_prealloc_nr >= total_needed && |
| 957 | first >= search_start) { |
| 958 | ins->objectid = info->extent_tree_prealloc[0]; |
| 959 | ins->offset = 1; |
| 960 | return 0; |
| 961 | } |
| 962 | info->extent_tree_prealloc_nr = 0; |
| 963 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 964 | if (search_end == (u64)-1) |
| 965 | search_end = btrfs_super_total_blocks(info->disk_super); |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 966 | if (hint_block) { |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 967 | block_group = btrfs_lookup_block_group(info, hint_block); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 968 | block_group = btrfs_find_block_group(root, block_group, |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 969 | hint_block, data, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 970 | } else { |
| 971 | block_group = btrfs_find_block_group(root, |
| 972 | trans->block_group, 0, |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 973 | data, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 974 | } |
| 975 | |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame^] | 976 | path = btrfs_alloc_path(); |
| 977 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 978 | check_failed: |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 979 | if (!block_group->data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 980 | search_start = find_search_start(root, &block_group, |
| 981 | search_start, total_needed); |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 982 | else if (!full_scan) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 983 | search_start = max(block_group->last_alloc, search_start); |
| 984 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 985 | btrfs_init_path(path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 986 | ins->objectid = search_start; |
| 987 | ins->offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 988 | start_found = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 989 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 990 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 991 | if (ret < 0) |
| 992 | goto error; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 993 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 994 | if (path->slots[0] > 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 995 | path->slots[0]--; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 999 | btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key); |
| 1000 | /* |
| 1001 | * a rare case, go back one key if we hit a block group item |
| 1002 | * instead of an extent item |
| 1003 | */ |
| 1004 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY && |
| 1005 | key.objectid + key.offset >= search_start) { |
| 1006 | ins->objectid = key.objectid; |
| 1007 | ins->offset = key.offset - 1; |
| 1008 | btrfs_release_path(root, path); |
| 1009 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); |
| 1010 | if (ret < 0) |
| 1011 | goto error; |
| 1012 | |
| 1013 | if (path->slots[0] > 0) { |
| 1014 | path->slots[0]--; |
| 1015 | } |
| 1016 | } |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 1017 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1018 | while (1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1019 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 1020 | slot = path->slots[0]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1021 | if (slot >= btrfs_header_nritems(&l->header)) { |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1022 | if (fill_prealloc) { |
| 1023 | info->extent_tree_prealloc_nr = 0; |
| 1024 | total_found = 0; |
| 1025 | } |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 1026 | if (start_found) |
| 1027 | limit = last_block + |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 1028 | (block_group->key.offset >> 1); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 1029 | else |
| 1030 | limit = search_start + |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 1031 | (block_group->key.offset >> 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1032 | ret = btrfs_next_leaf(root, path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1033 | if (ret == 0) |
| 1034 | continue; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1035 | if (ret < 0) |
| 1036 | goto error; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1037 | if (!start_found) { |
| 1038 | ins->objectid = search_start; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1039 | ins->offset = search_end - search_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1040 | start_found = 1; |
| 1041 | goto check_pending; |
| 1042 | } |
| 1043 | ins->objectid = last_block > search_start ? |
| 1044 | last_block : search_start; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1045 | ins->offset = search_end - ins->objectid; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1046 | goto check_pending; |
| 1047 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1048 | |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1049 | btrfs_disk_key_to_cpu(&key, &l->items[slot].key); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1050 | if (key.objectid >= search_start && key.objectid > last_block && |
| 1051 | start_found) { |
| 1052 | if (last_block < search_start) |
| 1053 | last_block = search_start; |
| 1054 | hole_size = key.objectid - last_block; |
| 1055 | if (hole_size >= num_blocks) { |
| 1056 | ins->objectid = last_block; |
| 1057 | ins->offset = hole_size; |
| 1058 | goto check_pending; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 1059 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1060 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1061 | |
| 1062 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) |
| 1063 | goto next; |
| 1064 | |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 1065 | start_found = 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1066 | last_block = key.objectid + key.offset; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1067 | if (!full_scan && last_block >= block_group->key.objectid + |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1068 | block_group->key.offset) { |
| 1069 | btrfs_release_path(root, path); |
| 1070 | search_start = block_group->key.objectid + |
| 1071 | block_group->key.offset * 2; |
| 1072 | goto new_group; |
| 1073 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1074 | next: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1075 | path->slots[0]++; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 1076 | cond_resched(); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1077 | } |
| 1078 | // FIXME -ENOSPC |
| 1079 | check_pending: |
| 1080 | /* we have to make sure we didn't find an extent that has already |
| 1081 | * been allocated by the map tree or the original allocation |
| 1082 | */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1083 | btrfs_release_path(root, path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1084 | BUG_ON(ins->objectid < search_start); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1085 | |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1086 | if (ins->objectid + num_blocks >= search_end) { |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1087 | if (full_scan) { |
| 1088 | ret = -ENOSPC; |
| 1089 | goto error; |
| 1090 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1091 | search_start = orig_search_start; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1092 | if (wrapped) |
| 1093 | full_scan = 1; |
| 1094 | else |
| 1095 | wrapped = 1; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1096 | goto new_group; |
Chris Mason | 06a2f9f | 2007-04-28 08:48:10 -0400 | [diff] [blame] | 1097 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1098 | for (test_block = ins->objectid; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1099 | test_block < ins->objectid + num_blocks; test_block++) { |
| 1100 | if (test_radix_bit(&info->pinned_radix, test_block)) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1101 | search_start = test_block + 1; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1102 | goto new_group; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1103 | } |
| 1104 | } |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1105 | if (!fill_prealloc && info->extent_tree_insert_nr) { |
| 1106 | u64 last = |
| 1107 | info->extent_tree_insert[info->extent_tree_insert_nr - 1]; |
| 1108 | if (ins->objectid + num_blocks > |
| 1109 | info->extent_tree_insert[0] && |
| 1110 | ins->objectid <= last) { |
| 1111 | search_start = last + 1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1112 | WARN_ON(!full_scan); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1113 | goto new_group; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | if (!fill_prealloc && info->extent_tree_prealloc_nr) { |
| 1117 | u64 first = |
| 1118 | info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1]; |
| 1119 | if (ins->objectid + num_blocks > first && |
| 1120 | ins->objectid <= info->extent_tree_prealloc[0]) { |
| 1121 | search_start = info->extent_tree_prealloc[0] + 1; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1122 | goto new_group; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | if (fill_prealloc) { |
| 1126 | int nr; |
| 1127 | test_block = ins->objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1128 | if (test_block - info->extent_tree_prealloc[total_needed - 1] >= |
| 1129 | leaf_range(root)) { |
| 1130 | total_found = 0; |
| 1131 | info->extent_tree_prealloc_nr = total_found; |
| 1132 | } |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1133 | while(test_block < ins->objectid + ins->offset && |
| 1134 | total_found < total_needed) { |
| 1135 | nr = total_needed - total_found - 1; |
| 1136 | BUG_ON(nr < 0); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1137 | info->extent_tree_prealloc[nr] = test_block; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1138 | total_found++; |
| 1139 | test_block++; |
| 1140 | } |
| 1141 | if (total_found < total_needed) { |
| 1142 | search_start = test_block; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1143 | goto new_group; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1144 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1145 | info->extent_tree_prealloc_nr = total_found; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1146 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1147 | if (!data) { |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 1148 | block_group = btrfs_lookup_block_group(info, ins->objectid); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1149 | if (block_group) { |
| 1150 | if (fill_prealloc) |
| 1151 | block_group->last_prealloc = |
| 1152 | info->extent_tree_prealloc[total_needed-1]; |
| 1153 | else |
| 1154 | trans->block_group = block_group; |
| 1155 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1156 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1157 | ins->offset = num_blocks; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1158 | btrfs_free_path(path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1159 | return 0; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1160 | |
| 1161 | new_group: |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1162 | if (search_start + num_blocks >= search_end) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1163 | search_start = orig_search_start; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1164 | if (full_scan) { |
| 1165 | ret = -ENOSPC; |
| 1166 | goto error; |
| 1167 | } |
| 1168 | if (wrapped) |
| 1169 | full_scan = 1; |
| 1170 | else |
| 1171 | wrapped = 1; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1172 | } |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 1173 | block_group = btrfs_lookup_block_group(info, search_start); |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1174 | cond_resched(); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1175 | if (!full_scan) |
| 1176 | block_group = btrfs_find_block_group(root, block_group, |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 1177 | search_start, data, 0); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1178 | goto check_failed; |
| 1179 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1180 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1181 | btrfs_release_path(root, path); |
| 1182 | btrfs_free_path(path); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1183 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1184 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1185 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1186 | * finds a free extent and does all the dirty work required for allocation |
| 1187 | * returns the key for the extent through ins, and a tree buffer for |
| 1188 | * the first block of the extent through buf. |
| 1189 | * |
| 1190 | * returns 0 if everything worked, non-zero otherwise. |
| 1191 | */ |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1192 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 1193 | struct btrfs_root *root, u64 owner, |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1194 | u64 num_blocks, u64 hint_block, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 1195 | u64 search_end, struct btrfs_key *ins, int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1196 | { |
| 1197 | int ret; |
| 1198 | int pending_ret; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1199 | u64 super_blocks_used; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1200 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1201 | struct btrfs_fs_info *info = root->fs_info; |
| 1202 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1203 | struct btrfs_extent_item extent_item; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1204 | struct btrfs_key prealloc_key; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1205 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1206 | btrfs_set_extent_refs(&extent_item, 1); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1207 | btrfs_set_extent_owner(&extent_item, owner); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1208 | |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1209 | if (root == extent_root) { |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1210 | int nr; |
| 1211 | BUG_ON(info->extent_tree_prealloc_nr == 0); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1212 | BUG_ON(num_blocks != 1); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1213 | ins->offset = 1; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1214 | info->extent_tree_prealloc_nr--; |
| 1215 | nr = info->extent_tree_prealloc_nr; |
| 1216 | ins->objectid = info->extent_tree_prealloc[nr]; |
| 1217 | info->extent_tree_insert[info->extent_tree_insert_nr++] = |
| 1218 | ins->objectid; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1219 | ret = update_block_group(trans, root, |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1220 | ins->objectid, ins->offset, 1, 0, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1221 | BUG_ON(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1224 | |
| 1225 | /* |
| 1226 | * if we're doing a data allocation, preallocate room in the |
| 1227 | * extent tree first. This way the extent tree blocks end up |
| 1228 | * in the correct block group. |
| 1229 | */ |
| 1230 | if (data) { |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 1231 | ret = find_free_extent(trans, root, 0, 0, |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1232 | search_end, 0, &prealloc_key, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1233 | if (ret) { |
| 1234 | return ret; |
| 1235 | } |
| 1236 | if (prealloc_key.objectid + prealloc_key.offset >= search_end) { |
| 1237 | int nr = info->extent_tree_prealloc_nr; |
| 1238 | search_end = info->extent_tree_prealloc[nr - 1] - 1; |
| 1239 | } else { |
| 1240 | search_start = info->extent_tree_prealloc[0] + 1; |
| 1241 | } |
| 1242 | } |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1243 | if (hint_block < search_start) |
| 1244 | hint_block = search_start; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1245 | /* do the real allocation */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1246 | ret = find_free_extent(trans, root, num_blocks, search_start, |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1247 | search_end, hint_block, ins, data); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1248 | if (ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1249 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1250 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1251 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1252 | /* |
| 1253 | * if we're doing a metadata allocation, preallocate space in the |
| 1254 | * extent tree second. This way, we don't create a tiny hole |
| 1255 | * in the allocation map between any unused preallocation blocks |
| 1256 | * and the metadata block we're actually allocating. On disk, |
| 1257 | * it'll go: |
| 1258 | * [block we've allocated], [used prealloc 1], [ unused prealloc ] |
| 1259 | * The unused prealloc will get reused the next time around. |
| 1260 | */ |
| 1261 | if (!data) { |
| 1262 | if (ins->objectid + ins->offset >= search_end) |
| 1263 | search_end = ins->objectid - 1; |
| 1264 | else |
| 1265 | search_start = ins->objectid + ins->offset; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1266 | |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1267 | if (hint_block < search_start) |
| 1268 | hint_block = search_start; |
| 1269 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1270 | ret = find_free_extent(trans, root, 0, search_start, |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1271 | search_end, hint_block, |
| 1272 | &prealloc_key, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1273 | if (ret) { |
| 1274 | return ret; |
| 1275 | } |
| 1276 | } |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1277 | |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1278 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
| 1279 | btrfs_set_super_blocks_used(info->disk_super, super_blocks_used + |
| 1280 | num_blocks); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1281 | ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, |
| 1282 | sizeof(extent_item)); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1283 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1284 | finish_current_insert(trans, extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1285 | pending_ret = del_pending_extents(trans, extent_root); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1286 | if (ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1287 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1288 | } |
| 1289 | if (pending_ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1290 | return pending_ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1291 | } |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1292 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0, |
| 1293 | data); |
Chris Mason | fabb568 | 2007-06-07 22:13:21 -0400 | [diff] [blame] | 1294 | BUG_ON(ret); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1295 | return 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * helper function to allocate a block for a given tree |
| 1300 | * returns the tree buffer or NULL. |
| 1301 | */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1302 | struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1303 | struct btrfs_root *root, u64 hint) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1304 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1305 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1306 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1307 | struct buffer_head *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1308 | |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1309 | ret = btrfs_alloc_extent(trans, root, root->root_key.objectid, |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 1310 | 1, hint, (unsigned long)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1311 | if (ret) { |
| 1312 | BUG(); |
| 1313 | return NULL; |
| 1314 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1315 | BUG_ON(ret); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 1316 | buf = btrfs_find_create_tree_block(root, ins.objectid); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 1317 | set_buffer_uptodate(buf); |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 1318 | set_buffer_checked(buf); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 1319 | set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1320 | return buf; |
| 1321 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1322 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1323 | static int drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 1324 | struct btrfs_root *root, struct buffer_head *cur) |
| 1325 | { |
| 1326 | struct btrfs_disk_key *key; |
| 1327 | struct btrfs_leaf *leaf; |
| 1328 | struct btrfs_file_extent_item *fi; |
| 1329 | int i; |
| 1330 | int nritems; |
| 1331 | int ret; |
| 1332 | |
| 1333 | BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur))); |
| 1334 | leaf = btrfs_buffer_leaf(cur); |
| 1335 | nritems = btrfs_header_nritems(&leaf->header); |
| 1336 | for (i = 0; i < nritems; i++) { |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 1337 | u64 disk_blocknr; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1338 | key = &leaf->items[i].key; |
| 1339 | if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) |
| 1340 | continue; |
| 1341 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 236454df | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 1342 | if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE) |
| 1343 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1344 | /* |
| 1345 | * FIXME make sure to insert a trans record that |
| 1346 | * repeats the snapshot del on crash |
| 1347 | */ |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 1348 | disk_blocknr = btrfs_file_extent_disk_blocknr(fi); |
| 1349 | if (disk_blocknr == 0) |
| 1350 | continue; |
| 1351 | ret = btrfs_free_extent(trans, root, disk_blocknr, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1352 | btrfs_file_extent_disk_num_blocks(fi), |
| 1353 | 0); |
| 1354 | BUG_ON(ret); |
| 1355 | } |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame^] | 1359 | static void reada_walk_down(struct btrfs_root *root, |
| 1360 | struct btrfs_node *node) |
| 1361 | { |
| 1362 | int i; |
| 1363 | u32 nritems; |
| 1364 | u64 blocknr; |
| 1365 | int ret; |
| 1366 | u32 refs; |
| 1367 | |
| 1368 | nritems = btrfs_header_nritems(&node->header); |
| 1369 | for (i = 0; i < nritems; i++) { |
| 1370 | blocknr = btrfs_node_blockptr(node, i); |
| 1371 | ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs); |
| 1372 | BUG_ON(ret); |
| 1373 | if (refs != 1) |
| 1374 | continue; |
| 1375 | ret = readahead_tree_block(root, blocknr); |
| 1376 | if (ret) |
| 1377 | break; |
| 1378 | } |
| 1379 | } |
| 1380 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1381 | /* |
| 1382 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 1383 | * counts as it goes. |
| 1384 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1385 | static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1386 | *root, struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1387 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1388 | struct buffer_head *next; |
| 1389 | struct buffer_head *cur; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1390 | u64 blocknr; |
| 1391 | int ret; |
| 1392 | u32 refs; |
| 1393 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1394 | WARN_ON(*level < 0); |
| 1395 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1396 | ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]), |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1397 | 1, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1398 | BUG_ON(ret); |
| 1399 | if (refs > 1) |
| 1400 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame^] | 1401 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1402 | /* |
| 1403 | * walk down to the last node level and free all the leaves |
| 1404 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1405 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1406 | WARN_ON(*level < 0); |
| 1407 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1408 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame^] | 1409 | |
| 1410 | if (*level > 0 && path->slots[*level] == 0) |
| 1411 | reada_walk_down(root, btrfs_buffer_node(cur)); |
| 1412 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1413 | if (btrfs_header_level(btrfs_buffer_header(cur)) != *level) |
| 1414 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame^] | 1415 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1416 | if (path->slots[*level] >= |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1417 | btrfs_header_nritems(btrfs_buffer_header(cur))) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1418 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1419 | if (*level == 0) { |
| 1420 | ret = drop_leaf_ref(trans, root, cur); |
| 1421 | BUG_ON(ret); |
| 1422 | break; |
| 1423 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1424 | blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur), |
| 1425 | path->slots[*level]); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1426 | ret = lookup_extent_ref(trans, root, blocknr, 1, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1427 | BUG_ON(ret); |
| 1428 | if (refs != 1) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1429 | path->slots[*level]++; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1430 | ret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1431 | BUG_ON(ret); |
| 1432 | continue; |
| 1433 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1434 | next = read_tree_block(root, blocknr); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1435 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1436 | if (path->nodes[*level-1]) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1437 | btrfs_block_release(root, path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1438 | path->nodes[*level-1] = next; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1439 | *level = btrfs_header_level(btrfs_buffer_header(next)); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1440 | path->slots[*level] = 0; |
| 1441 | } |
| 1442 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1443 | WARN_ON(*level < 0); |
| 1444 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1445 | ret = btrfs_free_extent(trans, root, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1446 | bh_blocknr(path->nodes[*level]), 1, 1); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1447 | btrfs_block_release(root, path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1448 | path->nodes[*level] = NULL; |
| 1449 | *level += 1; |
| 1450 | BUG_ON(ret); |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1454 | /* |
| 1455 | * helper for dropping snapshots. This walks back up the tree in the path |
| 1456 | * to find the first node higher up where we haven't yet gone through |
| 1457 | * all the slots |
| 1458 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1459 | static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1460 | *root, struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1461 | { |
| 1462 | int i; |
| 1463 | int slot; |
| 1464 | int ret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1465 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1466 | slot = path->slots[i]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1467 | if (slot < btrfs_header_nritems( |
| 1468 | btrfs_buffer_header(path->nodes[i])) - 1) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1469 | path->slots[i]++; |
| 1470 | *level = i; |
| 1471 | return 0; |
| 1472 | } else { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1473 | ret = btrfs_free_extent(trans, root, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1474 | bh_blocknr(path->nodes[*level]), |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1475 | 1, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1476 | BUG_ON(ret); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1477 | btrfs_block_release(root, path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1478 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1479 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | return 1; |
| 1483 | } |
| 1484 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1485 | /* |
| 1486 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 1487 | * the tree freeing any blocks that have a ref count of zero after being |
| 1488 | * decremented. |
| 1489 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1490 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1491 | *root, struct buffer_head *snap) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1492 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1493 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1494 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1495 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1496 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1497 | int i; |
| 1498 | int orig_level; |
| 1499 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1500 | path = btrfs_alloc_path(); |
| 1501 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1502 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1503 | level = btrfs_header_level(btrfs_buffer_header(snap)); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1504 | orig_level = level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1505 | path->nodes[level] = snap; |
| 1506 | path->slots[level] = 0; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1507 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1508 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1509 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1510 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1511 | if (wret < 0) |
| 1512 | ret = wret; |
| 1513 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1514 | wret = walk_up_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1515 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1516 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1517 | if (wret < 0) |
| 1518 | ret = wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1519 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1520 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1521 | if (path->nodes[i]) { |
| 1522 | btrfs_block_release(root, path->nodes[i]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1523 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1524 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1525 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1526 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1527 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1528 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1529 | static int free_block_group_radix(struct radix_tree_root *radix) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1530 | { |
| 1531 | int ret; |
| 1532 | struct btrfs_block_group_cache *cache[8]; |
| 1533 | int i; |
| 1534 | |
| 1535 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1536 | ret = radix_tree_gang_lookup(radix, (void **)cache, 0, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1537 | ARRAY_SIZE(cache)); |
| 1538 | if (!ret) |
| 1539 | break; |
| 1540 | for (i = 0; i < ret; i++) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1541 | radix_tree_delete(radix, cache[i]->key.objectid + |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1542 | cache[i]->key.offset - 1); |
| 1543 | kfree(cache[i]); |
| 1544 | } |
| 1545 | } |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1549 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 1550 | { |
| 1551 | int ret; |
| 1552 | int ret2; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1553 | unsigned long gang[16]; |
| 1554 | int i; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1555 | |
| 1556 | ret = free_block_group_radix(&info->block_group_radix); |
| 1557 | ret2 = free_block_group_radix(&info->block_group_data_radix); |
| 1558 | if (ret) |
| 1559 | return ret; |
| 1560 | if (ret2) |
| 1561 | return ret2; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1562 | |
| 1563 | while(1) { |
| 1564 | ret = find_first_radix_bit(&info->extent_map_radix, |
| 1565 | gang, 0, ARRAY_SIZE(gang)); |
| 1566 | if (!ret) |
| 1567 | break; |
| 1568 | for (i = 0; i < ret; i++) { |
| 1569 | clear_radix_bit(&info->extent_map_radix, gang[i]); |
| 1570 | } |
| 1571 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1572 | return 0; |
| 1573 | } |
| 1574 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1575 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 1576 | { |
| 1577 | struct btrfs_path *path; |
| 1578 | int ret; |
| 1579 | int err = 0; |
| 1580 | struct btrfs_block_group_item *bi; |
| 1581 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1582 | struct btrfs_fs_info *info = root->fs_info; |
| 1583 | struct radix_tree_root *radix; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1584 | struct btrfs_key key; |
| 1585 | struct btrfs_key found_key; |
| 1586 | struct btrfs_leaf *leaf; |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 1587 | u64 group_size_blocks; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1588 | u64 used; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1589 | |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 1590 | group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >> |
| 1591 | root->fs_info->sb->s_blocksize_bits; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1592 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1593 | key.objectid = 0; |
| 1594 | key.offset = group_size_blocks; |
| 1595 | key.flags = 0; |
| 1596 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
| 1597 | |
| 1598 | path = btrfs_alloc_path(); |
| 1599 | if (!path) |
| 1600 | return -ENOMEM; |
| 1601 | |
| 1602 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1603 | ret = btrfs_search_slot(NULL, info->extent_root, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1604 | &key, path, 0, 0); |
| 1605 | if (ret != 0) { |
| 1606 | err = ret; |
| 1607 | break; |
| 1608 | } |
| 1609 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1610 | btrfs_disk_key_to_cpu(&found_key, |
| 1611 | &leaf->items[path->slots[0]].key); |
| 1612 | cache = kmalloc(sizeof(*cache), GFP_NOFS); |
| 1613 | if (!cache) { |
| 1614 | err = -1; |
| 1615 | break; |
| 1616 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1617 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1618 | bi = btrfs_item_ptr(leaf, path->slots[0], |
| 1619 | struct btrfs_block_group_item); |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1620 | if (bi->flags & BTRFS_BLOCK_GROUP_DATA) { |
| 1621 | radix = &info->block_group_data_radix; |
| 1622 | cache->data = 1; |
| 1623 | } else { |
| 1624 | radix = &info->block_group_radix; |
| 1625 | cache->data = 0; |
| 1626 | } |
| 1627 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1628 | memcpy(&cache->item, bi, sizeof(*bi)); |
| 1629 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1630 | cache->last_alloc = cache->key.objectid; |
| 1631 | cache->first_free = cache->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1632 | cache->last_prealloc = cache->key.objectid; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1633 | cache->pinned = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1634 | cache->cached = 0; |
| 1635 | |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1636 | cache->radix = radix; |
| 1637 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1638 | key.objectid = found_key.objectid + found_key.offset; |
| 1639 | btrfs_release_path(root, path); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1640 | ret = radix_tree_insert(radix, found_key.objectid + |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1641 | found_key.offset - 1, |
| 1642 | (void *)cache); |
| 1643 | BUG_ON(ret); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1644 | used = btrfs_block_group_used(bi); |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 1645 | if (used < div_factor(key.offset, 8)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1646 | radix_tree_tag_set(radix, found_key.objectid + |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1647 | found_key.offset - 1, |
| 1648 | BTRFS_BLOCK_GROUP_AVAIL); |
| 1649 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1650 | if (key.objectid >= |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1651 | btrfs_super_total_blocks(info->disk_super)) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1652 | break; |
| 1653 | } |
| 1654 | |
| 1655 | btrfs_free_path(path); |
| 1656 | return 0; |
| 1657 | } |