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