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