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