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