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