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