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