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