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