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