Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include "kerncompat.h" |
| 4 | #include "radix-tree.h" |
| 5 | #include "ctree.h" |
| 6 | #include "disk-io.h" |
| 7 | #include "print-tree.h" |
| 8 | |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 9 | static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks, |
| 10 | u64 search_start, u64 search_end, struct key *ins); |
| 11 | static int finish_current_insert(struct ctree_root *extent_root); |
| 12 | static int run_pending(struct ctree_root *extent_root); |
| 13 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 14 | /* |
| 15 | * pending extents are blocks that we're trying to allocate in the extent |
| 16 | * map while trying to grow the map because of other allocations. To avoid |
| 17 | * recursing, they are tagged in the radix tree and cleaned up after |
| 18 | * other allocations are done. The pending tag is also used in the same |
| 19 | * manner for deletes. |
| 20 | */ |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 21 | #define CTREE_EXTENT_PENDING_DEL 0 |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 22 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 23 | static int inc_block_ref(struct ctree_root *root, u64 blocknr) |
| 24 | { |
| 25 | struct ctree_path path; |
| 26 | int ret; |
| 27 | struct key key; |
| 28 | struct leaf *l; |
| 29 | struct extent_item *item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 30 | struct key ins; |
| 31 | |
| 32 | find_free_extent(root->extent_root, 0, 0, (u64)-1, &ins); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 33 | init_path(&path); |
| 34 | key.objectid = blocknr; |
| 35 | key.flags = 0; |
| 36 | key.offset = 1; |
| 37 | ret = search_slot(root->extent_root, &key, &path, 0, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 38 | if (ret != 0) |
| 39 | BUG(); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 40 | BUG_ON(ret != 0); |
| 41 | l = &path.nodes[0]->leaf; |
| 42 | item = (struct extent_item *)(l->data + |
| 43 | l->items[path.slots[0]].offset); |
| 44 | item->refs++; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 45 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 46 | BUG_ON(list_empty(&path.nodes[0]->dirty)); |
| 47 | release_path(root->extent_root, &path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 48 | finish_current_insert(root->extent_root); |
| 49 | run_pending(root->extent_root); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 53 | static int lookup_block_ref(struct ctree_root *root, u64 blocknr, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 54 | { |
| 55 | struct ctree_path path; |
| 56 | int ret; |
| 57 | struct key key; |
| 58 | struct leaf *l; |
| 59 | struct extent_item *item; |
| 60 | init_path(&path); |
| 61 | key.objectid = blocknr; |
| 62 | key.flags = 0; |
| 63 | key.offset = 1; |
| 64 | ret = search_slot(root->extent_root, &key, &path, 0, 0); |
| 65 | if (ret != 0) |
| 66 | BUG(); |
| 67 | l = &path.nodes[0]->leaf; |
| 68 | item = (struct extent_item *)(l->data + |
| 69 | l->items[path.slots[0]].offset); |
| 70 | *refs = item->refs; |
| 71 | release_path(root->extent_root, &path); |
| 72 | return 0; |
| 73 | } |
| 74 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 75 | int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf) |
| 76 | { |
| 77 | u64 blocknr; |
| 78 | int i; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 79 | |
| 80 | if (root == root->extent_root) |
| 81 | return 0; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 82 | if (btrfs_is_leaf(&buf->node)) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 83 | return 0; |
| 84 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 85 | for (i = 0; i < btrfs_header_nritems(&buf->node.header); i++) { |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 86 | blocknr = buf->node.blockptrs[i]; |
| 87 | inc_block_ref(root, blocknr); |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 92 | int btrfs_finish_extent_commit(struct ctree_root *root) |
| 93 | { |
| 94 | struct ctree_root *extent_root = root->extent_root; |
| 95 | unsigned long gang[8]; |
| 96 | int ret; |
| 97 | int i; |
| 98 | |
| 99 | while(1) { |
| 100 | ret = radix_tree_gang_lookup(&extent_root->pinned_radix, |
| 101 | (void **)gang, 0, |
| 102 | ARRAY_SIZE(gang)); |
| 103 | if (!ret) |
| 104 | break; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 105 | for (i = 0; i < ret; i++) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 106 | radix_tree_delete(&extent_root->pinned_radix, gang[i]); |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 107 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 108 | } |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 109 | extent_root->last_insert.objectid = 0; |
| 110 | extent_root->last_insert.offset = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 114 | static int finish_current_insert(struct ctree_root *extent_root) |
| 115 | { |
| 116 | struct key ins; |
| 117 | struct extent_item extent_item; |
| 118 | int i; |
| 119 | int ret; |
| 120 | |
| 121 | extent_item.refs = 1; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 122 | extent_item.owner = |
| 123 | btrfs_header_parentid(&extent_root->node->node.header); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 124 | ins.offset = 1; |
| 125 | ins.flags = 0; |
| 126 | |
| 127 | for (i = 0; i < extent_root->current_insert.flags; i++) { |
| 128 | ins.objectid = extent_root->current_insert.objectid + i; |
| 129 | ret = insert_item(extent_root, &ins, &extent_item, |
| 130 | sizeof(extent_item)); |
| 131 | BUG_ON(ret); |
| 132 | } |
| 133 | extent_root->current_insert.offset = 0; |
| 134 | return 0; |
| 135 | } |
| 136 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 137 | /* |
| 138 | * remove an extent from the root, returns 0 on success |
| 139 | */ |
| 140 | int __free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks) |
| 141 | { |
| 142 | struct ctree_path path; |
| 143 | struct key key; |
| 144 | struct ctree_root *extent_root = root->extent_root; |
| 145 | int ret; |
| 146 | struct item *item; |
| 147 | struct extent_item *ei; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 148 | struct key ins; |
| 149 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 150 | key.objectid = blocknr; |
| 151 | key.flags = 0; |
| 152 | key.offset = num_blocks; |
| 153 | |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 154 | find_free_extent(root, 0, 0, (u64)-1, &ins); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 155 | init_path(&path); |
| 156 | ret = search_slot(extent_root, &key, &path, -1, 1); |
| 157 | if (ret) { |
| 158 | printf("failed to find %Lu\n", key.objectid); |
| 159 | print_tree(extent_root, extent_root->node); |
| 160 | printf("failed to find %Lu\n", key.objectid); |
| 161 | BUG(); |
| 162 | } |
| 163 | item = path.nodes[0]->leaf.items + path.slots[0]; |
| 164 | ei = (struct extent_item *)(path.nodes[0]->leaf.data + item->offset); |
| 165 | BUG_ON(ei->refs == 0); |
| 166 | ei->refs--; |
| 167 | if (ei->refs == 0) { |
| 168 | if (root == extent_root) { |
| 169 | int err; |
| 170 | radix_tree_preload(GFP_KERNEL); |
| 171 | err = radix_tree_insert(&extent_root->pinned_radix, |
| 172 | blocknr, (void *)blocknr); |
| 173 | BUG_ON(err); |
| 174 | radix_tree_preload_end(); |
| 175 | } |
| 176 | ret = del_item(extent_root, &path); |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 177 | if (root != extent_root && |
| 178 | extent_root->last_insert.objectid < blocknr) |
| 179 | extent_root->last_insert.objectid = blocknr; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 180 | if (ret) |
| 181 | BUG(); |
| 182 | } |
| 183 | release_path(extent_root, &path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 184 | finish_current_insert(extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 189 | * find all the blocks marked as pending in the radix tree and remove |
| 190 | * them from the extent map |
| 191 | */ |
| 192 | static int del_pending_extents(struct ctree_root *extent_root) |
| 193 | { |
| 194 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 195 | struct tree_buffer *gang[4]; |
| 196 | int i; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 197 | |
| 198 | while(1) { |
| 199 | ret = radix_tree_gang_lookup_tag(&extent_root->cache_radix, |
| 200 | (void **)gang, 0, |
| 201 | ARRAY_SIZE(gang), |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 202 | CTREE_EXTENT_PENDING_DEL); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 203 | if (!ret) |
| 204 | break; |
| 205 | for (i = 0; i < ret; i++) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 206 | ret = __free_extent(extent_root, gang[i]->blocknr, 1); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 207 | radix_tree_tag_clear(&extent_root->cache_radix, |
| 208 | gang[i]->blocknr, |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 209 | CTREE_EXTENT_PENDING_DEL); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 210 | tree_block_release(extent_root, gang[i]); |
| 211 | } |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 216 | static int run_pending(struct ctree_root *extent_root) |
| 217 | { |
| 218 | while(radix_tree_tagged(&extent_root->cache_radix, |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 219 | CTREE_EXTENT_PENDING_DEL)) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 220 | del_pending_extents(extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 225 | /* |
| 226 | * remove an extent from the root, returns 0 on success |
| 227 | */ |
| 228 | int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks) |
| 229 | { |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 230 | struct key key; |
| 231 | struct ctree_root *extent_root = root->extent_root; |
| 232 | struct tree_buffer *t; |
| 233 | int pending_ret; |
| 234 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 235 | |
| 236 | if (root == extent_root) { |
| 237 | t = find_tree_block(root, blocknr); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 238 | radix_tree_tag_set(&root->cache_radix, blocknr, |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 239 | CTREE_EXTENT_PENDING_DEL); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 240 | return 0; |
| 241 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 242 | key.objectid = blocknr; |
| 243 | key.flags = 0; |
| 244 | key.offset = num_blocks; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 245 | ret = __free_extent(root, blocknr, num_blocks); |
| 246 | pending_ret = run_pending(root->extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 247 | return ret ? ret : pending_ret; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * walks the btree of allocated extents and find a hole of a given size. |
| 252 | * The key ins is changed to record the hole: |
| 253 | * ins->objectid == block start |
| 254 | * ins->flags = 0 |
| 255 | * ins->offset == number of blocks |
| 256 | * Any available blocks before search_start are skipped. |
| 257 | */ |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 258 | static int find_free_extent(struct ctree_root *orig_root, u64 num_blocks, |
| 259 | u64 search_start, u64 search_end, struct key *ins) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 260 | { |
| 261 | struct ctree_path path; |
| 262 | struct key *key; |
| 263 | int ret; |
| 264 | u64 hole_size = 0; |
| 265 | int slot = 0; |
| 266 | u64 last_block; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 267 | u64 test_block; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 268 | int start_found; |
| 269 | struct leaf *l; |
| 270 | struct ctree_root * root = orig_root->extent_root; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 271 | int total_needed = num_blocks; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 272 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 273 | total_needed += (btrfs_header_level(&root->node->node.header) + 1) * 3; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 274 | if (root->last_insert.objectid > search_start) |
| 275 | search_start = root->last_insert.objectid; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 276 | check_failed: |
| 277 | init_path(&path); |
| 278 | ins->objectid = search_start; |
| 279 | ins->offset = 0; |
| 280 | ins->flags = 0; |
| 281 | start_found = 0; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 282 | ret = search_slot(root, ins, &path, 0, 0); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 283 | if (ret < 0) |
| 284 | goto error; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 285 | |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 286 | if (path.slots[0] > 0) |
| 287 | path.slots[0]--; |
| 288 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 289 | while (1) { |
| 290 | l = &path.nodes[0]->leaf; |
| 291 | slot = path.slots[0]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 292 | if (slot >= btrfs_header_nritems(&l->header)) { |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 293 | ret = next_leaf(root, &path); |
| 294 | if (ret == 0) |
| 295 | continue; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 296 | if (ret < 0) |
| 297 | goto error; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 298 | if (!start_found) { |
| 299 | ins->objectid = search_start; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 300 | ins->offset = (u64)-1; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 301 | start_found = 1; |
| 302 | goto check_pending; |
| 303 | } |
| 304 | ins->objectid = last_block > search_start ? |
| 305 | last_block : search_start; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 306 | ins->offset = (u64)-1; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 307 | goto check_pending; |
| 308 | } |
| 309 | key = &l->items[slot].key; |
| 310 | if (key->objectid >= search_start) { |
| 311 | if (start_found) { |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 312 | if (last_block < search_start) |
| 313 | last_block = search_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 314 | hole_size = key->objectid - last_block; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 315 | if (hole_size > total_needed) { |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 316 | ins->objectid = last_block; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 317 | ins->offset = hole_size; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 318 | goto check_pending; |
| 319 | } |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 320 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 321 | } |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 322 | start_found = 1; |
| 323 | last_block = key->objectid + key->offset; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 324 | path.slots[0]++; |
| 325 | } |
| 326 | // FIXME -ENOSPC |
| 327 | check_pending: |
| 328 | /* we have to make sure we didn't find an extent that has already |
| 329 | * been allocated by the map tree or the original allocation |
| 330 | */ |
| 331 | release_path(root, &path); |
| 332 | BUG_ON(ins->objectid < search_start); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 333 | for (test_block = ins->objectid; |
| 334 | test_block < ins->objectid + total_needed; test_block++) { |
| 335 | if (radix_tree_lookup(&root->pinned_radix, test_block)) { |
| 336 | search_start = test_block + 1; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 337 | goto check_failed; |
| 338 | } |
| 339 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 340 | BUG_ON(root->current_insert.offset); |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 341 | root->current_insert.offset = total_needed - num_blocks; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 342 | root->current_insert.objectid = ins->objectid + num_blocks; |
| 343 | root->current_insert.flags = 0; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 344 | root->last_insert.objectid = ins->objectid; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 345 | ins->offset = num_blocks; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 346 | return 0; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 347 | error: |
| 348 | release_path(root, &path); |
| 349 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 353 | * finds a free extent and does all the dirty work required for allocation |
| 354 | * returns the key for the extent through ins, and a tree buffer for |
| 355 | * the first block of the extent through buf. |
| 356 | * |
| 357 | * returns 0 if everything worked, non-zero otherwise. |
| 358 | */ |
| 359 | int alloc_extent(struct ctree_root *root, u64 num_blocks, u64 search_start, |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 360 | u64 search_end, u64 owner, struct key *ins) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 361 | { |
| 362 | int ret; |
| 363 | int pending_ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 364 | struct ctree_root *extent_root = root->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 365 | struct extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 366 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 367 | extent_item.refs = 1; |
| 368 | extent_item.owner = owner; |
| 369 | |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 370 | if (root == extent_root) { |
| 371 | BUG_ON(extent_root->current_insert.offset == 0); |
| 372 | BUG_ON(num_blocks != 1); |
| 373 | BUG_ON(extent_root->current_insert.flags == |
| 374 | extent_root->current_insert.offset); |
| 375 | ins->offset = 1; |
| 376 | ins->objectid = extent_root->current_insert.objectid + |
| 377 | extent_root->current_insert.flags++; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 378 | return 0; |
| 379 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 380 | ret = find_free_extent(root, num_blocks, search_start, |
| 381 | search_end, ins); |
| 382 | if (ret) |
| 383 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 384 | |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 385 | ret = insert_item(extent_root, ins, &extent_item, |
| 386 | sizeof(extent_item)); |
| 387 | |
| 388 | finish_current_insert(extent_root); |
| 389 | pending_ret = run_pending(extent_root); |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | if (pending_ret) |
| 393 | return pending_ret; |
| 394 | return 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* |
| 398 | * helper function to allocate a block for a given tree |
| 399 | * returns the tree buffer or NULL. |
| 400 | */ |
| 401 | struct tree_buffer *alloc_free_block(struct ctree_root *root) |
| 402 | { |
| 403 | struct key ins; |
| 404 | int ret; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 405 | struct tree_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 406 | |
| 407 | ret = alloc_extent(root, 1, 0, (unsigned long)-1, |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 408 | btrfs_header_parentid(&root->node->node.header), |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 409 | &ins); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 410 | if (ret) { |
| 411 | BUG(); |
| 412 | return NULL; |
| 413 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 414 | buf = find_tree_block(root, ins.objectid); |
| 415 | dirty_tree_block(root, buf); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 416 | return buf; |
| 417 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 418 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 419 | int walk_down_tree(struct ctree_root *root, struct ctree_path *path, int *level) |
| 420 | { |
| 421 | struct tree_buffer *next; |
| 422 | struct tree_buffer *cur; |
| 423 | u64 blocknr; |
| 424 | int ret; |
| 425 | u32 refs; |
| 426 | |
| 427 | ret = lookup_block_ref(root, path->nodes[*level]->blocknr, &refs); |
| 428 | BUG_ON(ret); |
| 429 | if (refs > 1) |
| 430 | goto out; |
| 431 | while(*level > 0) { |
| 432 | cur = path->nodes[*level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 433 | if (path->slots[*level] >= |
| 434 | btrfs_header_nritems(&cur->node.header)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 435 | break; |
| 436 | blocknr = cur->node.blockptrs[path->slots[*level]]; |
| 437 | ret = lookup_block_ref(root, blocknr, &refs); |
| 438 | if (refs != 1 || *level == 1) { |
| 439 | path->slots[*level]++; |
| 440 | ret = free_extent(root, blocknr, 1); |
| 441 | BUG_ON(ret); |
| 442 | continue; |
| 443 | } |
| 444 | BUG_ON(ret); |
| 445 | next = read_tree_block(root, blocknr); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 446 | if (path->nodes[*level-1]) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 447 | tree_block_release(root, path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 448 | path->nodes[*level-1] = next; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 449 | *level = btrfs_header_level(&next->node.header); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 450 | path->slots[*level] = 0; |
| 451 | } |
| 452 | out: |
| 453 | ret = free_extent(root, path->nodes[*level]->blocknr, 1); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 454 | tree_block_release(root, path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 455 | path->nodes[*level] = NULL; |
| 456 | *level += 1; |
| 457 | BUG_ON(ret); |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | int walk_up_tree(struct ctree_root *root, struct ctree_path *path, int *level) |
| 462 | { |
| 463 | int i; |
| 464 | int slot; |
| 465 | int ret; |
| 466 | for(i = *level; i < MAX_LEVEL - 1 && path->nodes[i]; i++) { |
| 467 | slot = path->slots[i]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 468 | if (slot < |
| 469 | btrfs_header_nritems(&path->nodes[i]->node.header)- 1) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 470 | path->slots[i]++; |
| 471 | *level = i; |
| 472 | return 0; |
| 473 | } else { |
| 474 | ret = free_extent(root, |
| 475 | path->nodes[*level]->blocknr, 1); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 476 | tree_block_release(root, path->nodes[*level]); |
| 477 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 478 | *level = i + 1; |
| 479 | BUG_ON(ret); |
| 480 | } |
| 481 | } |
| 482 | return 1; |
| 483 | } |
| 484 | |
| 485 | int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap) |
| 486 | { |
| 487 | int ret; |
| 488 | int level; |
| 489 | struct ctree_path path; |
| 490 | int i; |
| 491 | int orig_level; |
| 492 | |
| 493 | init_path(&path); |
| 494 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame^] | 495 | level = btrfs_header_level(&snap->node.header); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 496 | orig_level = level; |
| 497 | path.nodes[level] = snap; |
| 498 | path.slots[level] = 0; |
| 499 | while(1) { |
| 500 | ret = walk_down_tree(root, &path, &level); |
| 501 | if (ret > 0) |
| 502 | break; |
| 503 | ret = walk_up_tree(root, &path, &level); |
| 504 | if (ret > 0) |
| 505 | break; |
| 506 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 507 | for (i = 0; i <= orig_level; i++) { |
| 508 | if (path.nodes[i]) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 509 | tree_block_release(root, path.nodes[i]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 510 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | return 0; |
| 514 | } |