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