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