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 |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 9 | search_end, struct btrfs_key *ins, int data); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 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 | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 15 | static struct btrfs_block_group_cache *lookup_block_group(struct |
| 16 | btrfs_fs_info *info, |
| 17 | u64 blocknr) |
| 18 | { |
| 19 | struct btrfs_block_group_cache *block_group; |
| 20 | int ret; |
| 21 | |
| 22 | ret = radix_tree_gang_lookup(&info->block_group_radix, |
| 23 | (void **)&block_group, |
| 24 | blocknr, 1); |
| 25 | if (ret) { |
| 26 | if (block_group->key.objectid <= blocknr && blocknr < |
| 27 | block_group->key.objectid + block_group->key.offset) |
| 28 | return block_group; |
| 29 | } |
| 30 | ret = radix_tree_gang_lookup(&info->block_group_data_radix, |
| 31 | (void **)&block_group, |
| 32 | blocknr, 1); |
| 33 | if (ret) { |
| 34 | if (block_group->key.objectid <= blocknr && blocknr < |
| 35 | block_group->key.objectid + block_group->key.offset) |
| 36 | return block_group; |
| 37 | } |
| 38 | printk("lookup_block_group fails for blocknr %Lu\n", blocknr); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 42 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 43 | struct btrfs_block_group_cache |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 44 | *hint, u64 search_start, |
| 45 | int data) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 46 | { |
| 47 | struct btrfs_block_group_cache *cache[8]; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 48 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 49 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 50 | struct radix_tree_root *radix; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 51 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 52 | u64 last = 0; |
| 53 | u64 hint_last; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 54 | int i; |
| 55 | int ret; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 56 | int full_search = 0; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 57 | |
| 58 | if (data) |
| 59 | radix = &info->block_group_data_radix; |
| 60 | else |
| 61 | radix = &info->block_group_radix; |
| 62 | |
| 63 | if (search_start) { |
| 64 | struct btrfs_block_group_cache *shint; |
| 65 | shint = lookup_block_group(info, search_start); |
| 66 | if (shint->data == data) { |
| 67 | used = btrfs_block_group_used(&shint->item); |
| 68 | if (used + shint->pinned < |
| 69 | (shint->key.offset * 8) / 10) { |
| 70 | return shint; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | if (hint && hint->data == data) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 75 | used = btrfs_block_group_used(&hint->item); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 76 | if (used + hint->pinned < (hint->key.offset * 8) / 10) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 77 | return hint; |
| 78 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 79 | if (used >= (hint->key.offset * 8) / 10) { |
| 80 | radix_tree_tag_clear(radix, |
| 81 | hint->key.objectid + |
| 82 | hint->key.offset - 1, |
| 83 | BTRFS_BLOCK_GROUP_AVAIL); |
| 84 | } |
| 85 | last = hint->key.offset * 2; |
| 86 | if (hint->key.objectid >= last) |
| 87 | last = max(search_start, hint->key.objectid - last); |
| 88 | else |
| 89 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 90 | hint_last = last; |
| 91 | } else { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 92 | hint_last = search_start; |
| 93 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 94 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 95 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 96 | ret = radix_tree_gang_lookup_tag(radix, (void **)cache, |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 97 | last, ARRAY_SIZE(cache), |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 98 | BTRFS_BLOCK_GROUP_AVAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 99 | if (!ret) |
| 100 | break; |
| 101 | for (i = 0; i < ret; i++) { |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 102 | last = cache[i]->key.objectid + |
| 103 | cache[i]->key.offset; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 104 | used = btrfs_block_group_used(&cache[i]->item); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 105 | if (used + cache[i]->pinned < |
| 106 | (cache[i]->key.offset * 8) / 10) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 107 | found_group = cache[i]; |
| 108 | goto found; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 109 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 110 | if (used >= (cache[i]->key.offset * 8) / 10) { |
| 111 | radix_tree_tag_clear(radix, |
| 112 | cache[i]->key.objectid + |
| 113 | cache[i]->key.offset - 1, |
| 114 | BTRFS_BLOCK_GROUP_AVAIL); |
| 115 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 116 | } |
| 117 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 118 | last = hint_last; |
| 119 | again: |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 120 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 121 | ret = radix_tree_gang_lookup(radix, (void **)cache, |
| 122 | last, ARRAY_SIZE(cache)); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 123 | if (!ret) |
| 124 | break; |
| 125 | for (i = 0; i < ret; i++) { |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 126 | last = cache[i]->key.objectid + |
| 127 | cache[i]->key.offset; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 128 | used = btrfs_block_group_used(&cache[i]->item); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 129 | if (used + cache[i]->pinned < cache[i]->key.offset) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 130 | found_group = cache[i]; |
| 131 | goto found; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 132 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 133 | if (used >= cache[i]->key.offset) { |
| 134 | radix_tree_tag_clear(radix, |
| 135 | cache[i]->key.objectid + |
| 136 | cache[i]->key.offset - 1, |
| 137 | BTRFS_BLOCK_GROUP_AVAIL); |
| 138 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 141 | if (!full_search) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 142 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 143 | full_search = 1; |
| 144 | goto again; |
| 145 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 146 | if (!found_group) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 147 | ret = radix_tree_gang_lookup(radix, |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 148 | (void **)&found_group, 0, 1); |
| 149 | BUG_ON(ret != 1); |
| 150 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 151 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 152 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 155 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 156 | struct btrfs_root *root, |
| 157 | u64 blocknr, u64 num_blocks) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 158 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 159 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 160 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 161 | struct btrfs_key key; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 162 | struct btrfs_leaf *l; |
| 163 | struct btrfs_extent_item *item; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 164 | struct btrfs_key ins; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 165 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 166 | |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 167 | find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 168 | &ins, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 169 | path = btrfs_alloc_path(); |
| 170 | BUG_ON(!path); |
| 171 | btrfs_init_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 172 | key.objectid = blocknr; |
| 173 | key.flags = 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 174 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 175 | key.offset = num_blocks; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 176 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 177 | 0, 1); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 178 | if (ret != 0) { |
| 179 | printk("can't find block %Lu %Lu\n", blocknr, num_blocks); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 180 | BUG(); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 181 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 182 | BUG_ON(ret != 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 183 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 184 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 185 | refs = btrfs_extent_refs(item); |
| 186 | btrfs_set_extent_refs(item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 187 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 188 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 189 | btrfs_release_path(root->fs_info->extent_root, path); |
| 190 | btrfs_free_path(path); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 191 | finish_current_insert(trans, root->fs_info->extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 192 | del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 196 | static int lookup_extent_ref(struct btrfs_trans_handle *trans, |
| 197 | struct btrfs_root *root, u64 blocknr, |
| 198 | u64 num_blocks, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 199 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 200 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 201 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 202 | struct btrfs_key key; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 203 | struct btrfs_leaf *l; |
| 204 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 205 | |
| 206 | path = btrfs_alloc_path(); |
| 207 | btrfs_init_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 208 | key.objectid = blocknr; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 209 | key.offset = num_blocks; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 210 | key.flags = 0; |
| 211 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 212 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 213 | 0, 0); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 214 | if (ret != 0) |
| 215 | BUG(); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 216 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 217 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 218 | *refs = btrfs_extent_refs(item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 219 | btrfs_release_path(root->fs_info->extent_root, path); |
| 220 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 224 | int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, |
| 225 | struct btrfs_root *root) |
| 226 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 227 | return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 230 | 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] | 231 | struct buffer_head *buf) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 232 | { |
| 233 | u64 blocknr; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 234 | struct btrfs_node *buf_node; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 235 | struct btrfs_leaf *buf_leaf; |
| 236 | struct btrfs_disk_key *key; |
| 237 | struct btrfs_file_extent_item *fi; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 238 | int i; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 239 | int leaf; |
| 240 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 241 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 242 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 243 | return 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 244 | buf_node = btrfs_buffer_node(buf); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 245 | leaf = btrfs_is_leaf(buf_node); |
| 246 | buf_leaf = btrfs_buffer_leaf(buf); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 247 | for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 248 | if (leaf) { |
| 249 | key = &buf_leaf->items[i].key; |
| 250 | if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) |
| 251 | continue; |
| 252 | fi = btrfs_item_ptr(buf_leaf, i, |
| 253 | struct btrfs_file_extent_item); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 254 | if (btrfs_file_extent_type(fi) == |
| 255 | BTRFS_FILE_EXTENT_INLINE) |
| 256 | continue; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 257 | ret = btrfs_inc_extent_ref(trans, root, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 258 | btrfs_file_extent_disk_blocknr(fi), |
| 259 | btrfs_file_extent_disk_num_blocks(fi)); |
| 260 | BUG_ON(ret); |
| 261 | } else { |
| 262 | blocknr = btrfs_node_blockptr(buf_node, i); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 263 | ret = btrfs_inc_extent_ref(trans, root, blocknr, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 264 | BUG_ON(ret); |
| 265 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 270 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 271 | struct btrfs_root *root, |
| 272 | struct btrfs_path *path, |
| 273 | struct btrfs_block_group_cache *cache) |
| 274 | { |
| 275 | int ret; |
| 276 | int pending_ret; |
| 277 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 278 | struct btrfs_block_group_item *bi; |
| 279 | struct btrfs_key ins; |
| 280 | |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 281 | find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 282 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
| 283 | BUG_ON(ret); |
| 284 | bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
| 285 | struct btrfs_block_group_item); |
| 286 | memcpy(bi, &cache->item, sizeof(*bi)); |
| 287 | mark_buffer_dirty(path->nodes[0]); |
| 288 | btrfs_release_path(extent_root, path); |
| 289 | |
| 290 | finish_current_insert(trans, extent_root); |
| 291 | pending_ret = del_pending_extents(trans, extent_root); |
| 292 | if (ret) |
| 293 | return ret; |
| 294 | if (pending_ret) |
| 295 | return pending_ret; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 296 | if (cache->data) |
| 297 | cache->last_alloc = cache->first_free; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 298 | return 0; |
| 299 | |
| 300 | } |
| 301 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 302 | static int write_dirty_block_radix(struct btrfs_trans_handle *trans, |
| 303 | struct btrfs_root *root, |
| 304 | struct radix_tree_root *radix) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 305 | { |
| 306 | struct btrfs_block_group_cache *cache[8]; |
| 307 | int ret; |
| 308 | int err = 0; |
| 309 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 310 | int i; |
| 311 | struct btrfs_path *path; |
| 312 | |
| 313 | path = btrfs_alloc_path(); |
| 314 | if (!path) |
| 315 | return -ENOMEM; |
| 316 | |
| 317 | while(1) { |
| 318 | ret = radix_tree_gang_lookup_tag(radix, (void **)cache, |
| 319 | 0, ARRAY_SIZE(cache), |
| 320 | BTRFS_BLOCK_GROUP_DIRTY); |
| 321 | if (!ret) |
| 322 | break; |
| 323 | for (i = 0; i < ret; i++) { |
| 324 | radix_tree_tag_clear(radix, cache[i]->key.objectid + |
| 325 | cache[i]->key.offset - 1, |
| 326 | BTRFS_BLOCK_GROUP_DIRTY); |
| 327 | err = write_one_cache_group(trans, root, |
| 328 | path, cache[i]); |
| 329 | if (err) |
| 330 | werr = err; |
| 331 | } |
| 332 | } |
| 333 | btrfs_free_path(path); |
| 334 | return werr; |
| 335 | } |
| 336 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 337 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 338 | struct btrfs_root *root) |
| 339 | { |
| 340 | int ret; |
| 341 | int ret2; |
| 342 | ret = write_dirty_block_radix(trans, root, |
| 343 | &root->fs_info->block_group_radix); |
| 344 | ret2 = write_dirty_block_radix(trans, root, |
| 345 | &root->fs_info->block_group_data_radix); |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | if (ret2) |
| 349 | return ret2; |
| 350 | return 0; |
| 351 | } |
| 352 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 353 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 354 | struct btrfs_root *root, |
| 355 | u64 blocknr, u64 num, int alloc) |
| 356 | { |
| 357 | struct btrfs_block_group_cache *cache; |
| 358 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 359 | struct radix_tree_root *radix; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 360 | u64 total = num; |
| 361 | u64 old_val; |
| 362 | u64 block_in_group; |
| 363 | int ret; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 364 | if (num != 1) |
| 365 | radix = &info->block_group_data_radix; |
| 366 | else |
| 367 | radix = &info->block_group_radix; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 368 | while(total) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 369 | ret = radix_tree_gang_lookup(radix, (void **)&cache, |
| 370 | blocknr, 1); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 371 | if (!ret) { |
| 372 | printk(KERN_CRIT "blocknr %Lu lookup failed\n", |
| 373 | blocknr); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 374 | return -1; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 375 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 376 | block_in_group = blocknr - cache->key.objectid; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 377 | if (block_in_group > cache->key.offset || cache->key.objectid > |
| 378 | blocknr) { |
| 379 | if (radix == &info->block_group_data_radix) |
| 380 | radix = &info->block_group_radix; |
| 381 | else |
| 382 | radix = &info->block_group_data_radix; |
| 383 | ret = radix_tree_gang_lookup(radix, (void **)&cache, |
| 384 | blocknr, 1); |
| 385 | if (!ret) { |
| 386 | printk(KERN_CRIT "blocknr %Lu lookup failed\n", |
| 387 | blocknr); |
| 388 | return -1; |
| 389 | } |
| 390 | block_in_group = blocknr - cache->key.objectid; |
| 391 | if (block_in_group > cache->key.offset || |
| 392 | cache->key.objectid > blocknr) { |
| 393 | BUG(); |
| 394 | } |
| 395 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 396 | WARN_ON(block_in_group > cache->key.offset); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 397 | radix_tree_tag_set(radix, cache->key.objectid + |
| 398 | cache->key.offset - 1, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 399 | BTRFS_BLOCK_GROUP_DIRTY); |
| 400 | |
| 401 | old_val = btrfs_block_group_used(&cache->item); |
| 402 | num = min(total, cache->key.offset - block_in_group); |
| 403 | total -= num; |
| 404 | blocknr += num; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 405 | if (alloc) { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 406 | old_val += num; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 407 | if (blocknr > cache->last_alloc) |
| 408 | cache->last_alloc = blocknr; |
| 409 | } else { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 410 | old_val -= num; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 411 | if (blocknr < cache->first_free) |
| 412 | cache->first_free = blocknr; |
| 413 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 414 | btrfs_set_block_group_used(&cache->item, old_val); |
| 415 | } |
| 416 | return 0; |
| 417 | } |
| 418 | |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 419 | static int try_remove_page(struct address_space *mapping, unsigned long index) |
| 420 | { |
| 421 | int ret; |
| 422 | ret = invalidate_mapping_pages(mapping, index, index); |
| 423 | return ret; |
| 424 | } |
| 425 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 426 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct |
| 427 | btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 428 | { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 429 | unsigned long gang[8]; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 430 | struct inode *btree_inode = root->fs_info->btree_inode; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 431 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 88fd146 | 2007-03-16 08:56:18 -0400 | [diff] [blame] | 432 | u64 first = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 433 | int ret; |
| 434 | int i; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 435 | struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 436 | |
| 437 | while(1) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 438 | ret = find_first_radix_bit(pinned_radix, gang, |
| 439 | ARRAY_SIZE(gang)); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 440 | if (!ret) |
| 441 | break; |
Chris Mason | 88fd146 | 2007-03-16 08:56:18 -0400 | [diff] [blame] | 442 | if (!first) |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 443 | first = gang[0]; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 444 | for (i = 0; i < ret; i++) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 445 | clear_radix_bit(pinned_radix, gang[i]); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 446 | block_group = lookup_block_group(root->fs_info, |
| 447 | gang[i]); |
| 448 | if (block_group) { |
| 449 | WARN_ON(block_group->pinned == 0); |
| 450 | block_group->pinned--; |
| 451 | if (gang[i] < block_group->last_alloc) |
| 452 | block_group->last_alloc = gang[i]; |
| 453 | } |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 454 | try_remove_page(btree_inode->i_mapping, |
| 455 | gang[i] << (PAGE_CACHE_SHIFT - |
| 456 | btree_inode->i_blkbits)); |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 457 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 458 | } |
| 459 | return 0; |
| 460 | } |
| 461 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 462 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
| 463 | btrfs_root *extent_root) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 464 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 465 | struct btrfs_key ins; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 466 | struct btrfs_extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 467 | int i; |
| 468 | int ret; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 469 | u64 super_blocks_used; |
| 470 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 471 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 472 | btrfs_set_extent_refs(&extent_item, 1); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 473 | ins.offset = 1; |
| 474 | ins.flags = 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 475 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5d0c3e6 | 2007-04-23 17:01:05 -0400 | [diff] [blame] | 476 | btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 477 | |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 478 | for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) { |
| 479 | ins.objectid = extent_root->fs_info->extent_tree_insert[i]; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 480 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
| 481 | btrfs_set_super_blocks_used(info->disk_super, |
| 482 | super_blocks_used + 1); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 483 | ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item, |
| 484 | sizeof(extent_item)); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 485 | BUG_ON(ret); |
| 486 | } |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 487 | extent_root->fs_info->extent_tree_insert_nr = 0; |
| 488 | extent_root->fs_info->extent_tree_prealloc_nr = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 492 | 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] | 493 | { |
| 494 | int err; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 495 | struct btrfs_header *header; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 496 | struct buffer_head *bh; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 497 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 498 | if (!pending) { |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 499 | bh = btrfs_find_tree_block(root, blocknr); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 500 | if (bh) { |
| 501 | if (buffer_uptodate(bh)) { |
| 502 | u64 transid = |
| 503 | root->fs_info->running_transaction->transid; |
| 504 | header = btrfs_buffer_header(bh); |
| 505 | if (btrfs_header_generation(header) == |
| 506 | transid) { |
| 507 | btrfs_block_release(root, bh); |
| 508 | return 0; |
| 509 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 510 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 511 | btrfs_block_release(root, bh); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 512 | } |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 513 | err = set_radix_bit(&root->fs_info->pinned_radix, blocknr); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 514 | if (!err) { |
| 515 | struct btrfs_block_group_cache *cache; |
| 516 | cache = lookup_block_group(root->fs_info, blocknr); |
| 517 | if (cache) |
| 518 | cache->pinned++; |
| 519 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 520 | } else { |
| 521 | err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr); |
| 522 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 523 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 527 | /* |
| 528 | * remove an extent from the root, returns 0 on success |
| 529 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 530 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 531 | *root, u64 blocknr, u64 num_blocks, int pin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 532 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 533 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 534 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 535 | struct btrfs_fs_info *info = root->fs_info; |
| 536 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 537 | int ret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 538 | struct btrfs_extent_item *ei; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 539 | struct btrfs_key ins; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 540 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 541 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 542 | key.objectid = blocknr; |
| 543 | key.flags = 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 544 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 545 | key.offset = num_blocks; |
| 546 | |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 547 | find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 548 | path = btrfs_alloc_path(); |
| 549 | BUG_ON(!path); |
| 550 | btrfs_init_path(path); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 551 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 552 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 553 | if (ret) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 554 | printk("failed to find %Lu\n", key.objectid); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 555 | btrfs_print_tree(extent_root, extent_root->node); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 556 | printk("failed to find %Lu\n", key.objectid); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 557 | BUG(); |
| 558 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 559 | 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] | 560 | struct btrfs_extent_item); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 561 | BUG_ON(ei->refs == 0); |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 562 | refs = btrfs_extent_refs(ei) - 1; |
| 563 | btrfs_set_extent_refs(ei, refs); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 564 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 565 | if (refs == 0) { |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 566 | u64 super_blocks_used; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 567 | |
| 568 | if (pin) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 569 | ret = pin_down_block(root, blocknr, 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 570 | BUG_ON(ret); |
| 571 | } |
| 572 | |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 573 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
| 574 | btrfs_set_super_blocks_used(info->disk_super, |
| 575 | super_blocks_used - num_blocks); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 576 | ret = btrfs_del_item(trans, extent_root, path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 577 | if (ret) |
| 578 | BUG(); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 579 | ret = update_block_group(trans, root, blocknr, num_blocks, 0); |
| 580 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 581 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 582 | btrfs_release_path(extent_root, path); |
| 583 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 584 | finish_current_insert(trans, extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 589 | * find all the blocks marked as pending in the radix tree and remove |
| 590 | * them from the extent map |
| 591 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 592 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 593 | btrfs_root *extent_root) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 594 | { |
| 595 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 596 | int wret; |
| 597 | int err = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 598 | unsigned long gang[4]; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 599 | int i; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 600 | struct radix_tree_root *pending_radix; |
| 601 | struct radix_tree_root *pinned_radix; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 602 | struct btrfs_block_group_cache *cache; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 603 | |
| 604 | pending_radix = &extent_root->fs_info->pending_del_radix; |
| 605 | pinned_radix = &extent_root->fs_info->pinned_radix; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 606 | |
| 607 | while(1) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 608 | ret = find_first_radix_bit(pending_radix, gang, |
| 609 | ARRAY_SIZE(gang)); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 610 | if (!ret) |
| 611 | break; |
| 612 | for (i = 0; i < ret; i++) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 613 | wret = set_radix_bit(pinned_radix, gang[i]); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 614 | if (wret == 0) { |
| 615 | cache = lookup_block_group(extent_root->fs_info, |
| 616 | gang[i]); |
| 617 | if (cache) |
| 618 | cache->pinned++; |
| 619 | } |
| 620 | if (wret < 0) { |
| 621 | printk(KERN_CRIT "set_radix_bit, err %d\n", |
| 622 | wret); |
| 623 | BUG_ON(wret < 0); |
| 624 | } |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 625 | wret = clear_radix_bit(pending_radix, gang[i]); |
| 626 | BUG_ON(wret); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 627 | wret = __free_extent(trans, extent_root, |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 628 | gang[i], 1, 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 629 | if (wret) |
| 630 | err = wret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 631 | } |
| 632 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 633 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /* |
| 637 | * remove an extent from the root, returns 0 on success |
| 638 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 639 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
| 640 | *root, u64 blocknr, u64 num_blocks, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 641 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 642 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 643 | int pending_ret; |
| 644 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 645 | |
| 646 | if (root == extent_root) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 647 | pin_down_block(root, blocknr, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 648 | return 0; |
| 649 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 650 | ret = __free_extent(trans, root, blocknr, num_blocks, pin); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 651 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 652 | return ret ? ret : pending_ret; |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * walks the btree of allocated extents and find a hole of a given size. |
| 657 | * The key ins is changed to record the hole: |
| 658 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 659 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 660 | * ins->offset == number of blocks |
| 661 | * Any available blocks before search_start are skipped. |
| 662 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 663 | static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
| 664 | *orig_root, u64 num_blocks, u64 search_start, u64 |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 665 | search_end, struct btrfs_key *ins, int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 666 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 667 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 668 | struct btrfs_key key; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 669 | int ret; |
| 670 | u64 hole_size = 0; |
| 671 | int slot = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 672 | u64 last_block = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 673 | u64 test_block; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 674 | u64 orig_search_start = search_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 675 | int start_found; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 676 | struct btrfs_leaf *l; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 677 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 678 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 679 | int total_needed = num_blocks; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 680 | int total_found = 0; |
| 681 | int fill_prealloc = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 682 | int level; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 683 | struct btrfs_block_group_cache *block_group; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 684 | int full_scan = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 685 | |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 686 | path = btrfs_alloc_path(); |
| 687 | ins->flags = 0; |
| 688 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
| 689 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 690 | level = btrfs_header_level(btrfs_buffer_header(root->node)); |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 691 | if (num_blocks == 0) { |
| 692 | fill_prealloc = 1; |
| 693 | num_blocks = 1; |
Chris Mason | 308535a | 2007-04-28 15:17:08 -0400 | [diff] [blame] | 694 | total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 695 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 696 | if (search_start) { |
| 697 | block_group = lookup_block_group(info, search_start); |
| 698 | block_group = btrfs_find_block_group(root, block_group, |
| 699 | search_start, data); |
| 700 | } else { |
| 701 | block_group = btrfs_find_block_group(root, |
| 702 | trans->block_group, 0, |
| 703 | data); |
| 704 | } |
| 705 | |
| 706 | check_failed: |
| 707 | if (block_group->data != data) |
| 708 | WARN_ON(1); |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 709 | if (block_group->last_alloc > search_start) |
| 710 | search_start = block_group->last_alloc; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 711 | btrfs_init_path(path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 712 | ins->objectid = search_start; |
| 713 | ins->offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 714 | start_found = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 715 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 716 | if (ret < 0) |
| 717 | goto error; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 718 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 719 | if (path->slots[0] > 0) |
| 720 | path->slots[0]--; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 721 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 722 | while (1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 723 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 724 | slot = path->slots[0]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 725 | if (slot >= btrfs_header_nritems(&l->header)) { |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 726 | if (fill_prealloc) { |
| 727 | info->extent_tree_prealloc_nr = 0; |
| 728 | total_found = 0; |
| 729 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 730 | ret = btrfs_next_leaf(root, path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 731 | if (ret == 0) |
| 732 | continue; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 733 | if (ret < 0) |
| 734 | goto error; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 735 | if (!start_found) { |
| 736 | ins->objectid = search_start; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 737 | ins->offset = (u64)-1 - search_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 738 | start_found = 1; |
| 739 | goto check_pending; |
| 740 | } |
| 741 | ins->objectid = last_block > search_start ? |
| 742 | last_block : search_start; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 743 | ins->offset = (u64)-1 - ins->objectid; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 744 | goto check_pending; |
| 745 | } |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 746 | btrfs_disk_key_to_cpu(&key, &l->items[slot].key); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 747 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) |
| 748 | goto next; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 749 | if (key.objectid >= search_start) { |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 750 | if (start_found) { |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 751 | if (last_block < search_start) |
| 752 | last_block = search_start; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 753 | hole_size = key.objectid - last_block; |
Chris Mason | 28b8bb9 | 2007-04-27 11:42:05 -0400 | [diff] [blame] | 754 | if (hole_size >= num_blocks) { |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 755 | ins->objectid = last_block; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 756 | ins->offset = hole_size; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 757 | goto check_pending; |
| 758 | } |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 759 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 760 | } |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 761 | start_found = 1; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 762 | last_block = key.objectid + key.offset; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 763 | if (last_block >= block_group->key.objectid + |
| 764 | block_group->key.offset) { |
| 765 | btrfs_release_path(root, path); |
| 766 | search_start = block_group->key.objectid + |
| 767 | block_group->key.offset * 2; |
| 768 | goto new_group; |
| 769 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 770 | next: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 771 | path->slots[0]++; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 772 | } |
| 773 | // FIXME -ENOSPC |
| 774 | check_pending: |
| 775 | /* we have to make sure we didn't find an extent that has already |
| 776 | * been allocated by the map tree or the original allocation |
| 777 | */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 778 | btrfs_release_path(root, path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 779 | BUG_ON(ins->objectid < search_start); |
Chris Mason | 06a2f9f | 2007-04-28 08:48:10 -0400 | [diff] [blame] | 780 | if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 781 | if (full_scan) |
Chris Mason | 06a2f9f | 2007-04-28 08:48:10 -0400 | [diff] [blame] | 782 | return -ENOSPC; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 783 | search_start = orig_search_start; |
| 784 | full_scan = 1; |
| 785 | goto new_group; |
Chris Mason | 06a2f9f | 2007-04-28 08:48:10 -0400 | [diff] [blame] | 786 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 787 | for (test_block = ins->objectid; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 788 | test_block < ins->objectid + num_blocks; test_block++) { |
| 789 | if (test_radix_bit(&info->pinned_radix, test_block)) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 790 | search_start = test_block + 1; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 791 | goto new_group; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 792 | } |
| 793 | } |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 794 | if (!fill_prealloc && info->extent_tree_insert_nr) { |
| 795 | u64 last = |
| 796 | info->extent_tree_insert[info->extent_tree_insert_nr - 1]; |
| 797 | if (ins->objectid + num_blocks > |
| 798 | info->extent_tree_insert[0] && |
| 799 | ins->objectid <= last) { |
| 800 | search_start = last + 1; |
| 801 | WARN_ON(1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 802 | goto new_group; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | if (!fill_prealloc && info->extent_tree_prealloc_nr) { |
| 806 | u64 first = |
| 807 | info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1]; |
| 808 | if (ins->objectid + num_blocks > first && |
| 809 | ins->objectid <= info->extent_tree_prealloc[0]) { |
| 810 | search_start = info->extent_tree_prealloc[0] + 1; |
| 811 | WARN_ON(1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 812 | goto new_group; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | if (fill_prealloc) { |
| 816 | int nr; |
| 817 | test_block = ins->objectid; |
| 818 | while(test_block < ins->objectid + ins->offset && |
| 819 | total_found < total_needed) { |
| 820 | nr = total_needed - total_found - 1; |
| 821 | BUG_ON(nr < 0); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 822 | info->extent_tree_prealloc[nr] = test_block; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 823 | total_found++; |
| 824 | test_block++; |
| 825 | } |
| 826 | if (total_found < total_needed) { |
| 827 | search_start = test_block; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 828 | goto new_group; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 829 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 830 | info->extent_tree_prealloc_nr = total_found; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 831 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 832 | block_group = lookup_block_group(info, ins->objectid); |
| 833 | if (block_group) { |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 834 | block_group->last_alloc = ins->objectid; |
| 835 | if (!data) |
| 836 | trans->block_group = block_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 837 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 838 | ins->offset = num_blocks; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 839 | btrfs_free_path(path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 840 | return 0; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 841 | |
| 842 | new_group: |
| 843 | if (search_start >= btrfs_super_total_blocks(info->disk_super)) { |
| 844 | search_start = orig_search_start; |
| 845 | full_scan = 1; |
| 846 | } |
| 847 | block_group = lookup_block_group(info, search_start); |
| 848 | if (!full_scan) |
| 849 | block_group = btrfs_find_block_group(root, block_group, |
| 850 | search_start, data); |
| 851 | goto check_failed; |
| 852 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 853 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 854 | btrfs_release_path(root, path); |
| 855 | btrfs_free_path(path); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 856 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 857 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 858 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 859 | * finds a free extent and does all the dirty work required for allocation |
| 860 | * returns the key for the extent through ins, and a tree buffer for |
| 861 | * the first block of the extent through buf. |
| 862 | * |
| 863 | * returns 0 if everything worked, non-zero otherwise. |
| 864 | */ |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 865 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 866 | struct btrfs_root *root, u64 owner, |
Chris Mason | c62a192 | 2007-04-24 12:07:39 -0400 | [diff] [blame] | 867 | u64 num_blocks, u64 search_start, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 868 | u64 search_end, struct btrfs_key *ins, int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 869 | { |
| 870 | int ret; |
| 871 | int pending_ret; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 872 | u64 super_blocks_used; |
| 873 | struct btrfs_fs_info *info = root->fs_info; |
| 874 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 875 | struct btrfs_extent_item extent_item; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 876 | struct btrfs_key prealloc_key; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 877 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 878 | btrfs_set_extent_refs(&extent_item, 1); |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 879 | btrfs_set_extent_owner(&extent_item, owner); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 880 | |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 881 | if (root == extent_root) { |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 882 | int nr; |
| 883 | BUG_ON(info->extent_tree_prealloc_nr == 0); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 884 | BUG_ON(num_blocks != 1); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 885 | ins->offset = 1; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 886 | info->extent_tree_prealloc_nr--; |
| 887 | nr = info->extent_tree_prealloc_nr; |
| 888 | ins->objectid = info->extent_tree_prealloc[nr]; |
| 889 | info->extent_tree_insert[info->extent_tree_insert_nr++] = |
| 890 | ins->objectid; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 891 | ret = update_block_group(trans, root, |
| 892 | ins->objectid, ins->offset, 1); |
| 893 | BUG_ON(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 894 | return 0; |
| 895 | } |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 896 | /* do the real allocation */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 897 | ret = find_free_extent(trans, root, num_blocks, search_start, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 898 | search_end, ins, data); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 899 | if (ret) |
| 900 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 901 | |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 902 | /* then do prealloc for the extent tree */ |
| 903 | ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 904 | search_end, &prealloc_key, 0); |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 905 | if (ret) |
| 906 | return ret; |
| 907 | |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 908 | super_blocks_used = btrfs_super_blocks_used(info->disk_super); |
| 909 | btrfs_set_super_blocks_used(info->disk_super, super_blocks_used + |
| 910 | num_blocks); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 911 | ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, |
| 912 | sizeof(extent_item)); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 913 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 914 | finish_current_insert(trans, extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 915 | pending_ret = del_pending_extents(trans, extent_root); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 916 | if (ret) |
| 917 | return ret; |
| 918 | if (pending_ret) |
| 919 | return pending_ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 920 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 921 | return 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /* |
| 925 | * helper function to allocate a block for a given tree |
| 926 | * returns the tree buffer or NULL. |
| 927 | */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 928 | struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 929 | struct btrfs_root *root, u64 hint) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 930 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 931 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 932 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 933 | struct buffer_head *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 934 | |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 935 | ret = btrfs_alloc_extent(trans, root, root->root_key.objectid, |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 936 | 1, hint, (unsigned long)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 937 | if (ret) { |
| 938 | BUG(); |
| 939 | return NULL; |
| 940 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 941 | BUG_ON(ret); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 942 | buf = btrfs_find_create_tree_block(root, ins.objectid); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 943 | set_buffer_uptodate(buf); |
Chris Mason | 090d187 | 2007-05-01 08:53:32 -0400 | [diff] [blame] | 944 | set_buffer_checked(buf); |
Chris Mason | 7c4452b | 2007-04-28 09:29:35 -0400 | [diff] [blame] | 945 | set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 946 | return buf; |
| 947 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 948 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 949 | static int drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 950 | struct btrfs_root *root, struct buffer_head *cur) |
| 951 | { |
| 952 | struct btrfs_disk_key *key; |
| 953 | struct btrfs_leaf *leaf; |
| 954 | struct btrfs_file_extent_item *fi; |
| 955 | int i; |
| 956 | int nritems; |
| 957 | int ret; |
| 958 | |
| 959 | BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur))); |
| 960 | leaf = btrfs_buffer_leaf(cur); |
| 961 | nritems = btrfs_header_nritems(&leaf->header); |
| 962 | for (i = 0; i < nritems; i++) { |
| 963 | key = &leaf->items[i].key; |
| 964 | if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY) |
| 965 | continue; |
| 966 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 967 | if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE) |
| 968 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 969 | /* |
| 970 | * FIXME make sure to insert a trans record that |
| 971 | * repeats the snapshot del on crash |
| 972 | */ |
| 973 | ret = btrfs_free_extent(trans, root, |
| 974 | btrfs_file_extent_disk_blocknr(fi), |
| 975 | btrfs_file_extent_disk_num_blocks(fi), |
| 976 | 0); |
| 977 | BUG_ON(ret); |
| 978 | } |
| 979 | return 0; |
| 980 | } |
| 981 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 982 | /* |
| 983 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 984 | * counts as it goes. |
| 985 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 986 | static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
| 987 | *root, struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 988 | { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 989 | struct buffer_head *next; |
| 990 | struct buffer_head *cur; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 991 | u64 blocknr; |
| 992 | int ret; |
| 993 | u32 refs; |
| 994 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 995 | WARN_ON(*level < 0); |
| 996 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 997 | ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]), |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 998 | 1, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 999 | BUG_ON(ret); |
| 1000 | if (refs > 1) |
| 1001 | goto out; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1002 | /* |
| 1003 | * walk down to the last node level and free all the leaves |
| 1004 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1005 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1006 | WARN_ON(*level < 0); |
| 1007 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1008 | cur = path->nodes[*level]; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1009 | if (btrfs_header_level(btrfs_buffer_header(cur)) != *level) |
| 1010 | WARN_ON(1); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1011 | if (path->slots[*level] >= |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1012 | btrfs_header_nritems(btrfs_buffer_header(cur))) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1013 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1014 | if (*level == 0) { |
| 1015 | ret = drop_leaf_ref(trans, root, cur); |
| 1016 | BUG_ON(ret); |
| 1017 | break; |
| 1018 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1019 | blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur), |
| 1020 | path->slots[*level]); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1021 | ret = lookup_extent_ref(trans, root, blocknr, 1, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1022 | BUG_ON(ret); |
| 1023 | if (refs != 1) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1024 | path->slots[*level]++; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1025 | ret = btrfs_free_extent(trans, root, blocknr, 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1026 | BUG_ON(ret); |
| 1027 | continue; |
| 1028 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1029 | next = read_tree_block(root, blocknr); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1030 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1031 | if (path->nodes[*level-1]) |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1032 | btrfs_block_release(root, path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1033 | path->nodes[*level-1] = next; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1034 | *level = btrfs_header_level(btrfs_buffer_header(next)); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1035 | path->slots[*level] = 0; |
| 1036 | } |
| 1037 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1038 | WARN_ON(*level < 0); |
| 1039 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1040 | ret = btrfs_free_extent(trans, root, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1041 | bh_blocknr(path->nodes[*level]), 1, 1); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1042 | btrfs_block_release(root, path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1043 | path->nodes[*level] = NULL; |
| 1044 | *level += 1; |
| 1045 | BUG_ON(ret); |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1049 | /* |
| 1050 | * helper for dropping snapshots. This walks back up the tree in the path |
| 1051 | * to find the first node higher up where we haven't yet gone through |
| 1052 | * all the slots |
| 1053 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1054 | static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1055 | *root, struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1056 | { |
| 1057 | int i; |
| 1058 | int slot; |
| 1059 | int ret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1060 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1061 | slot = path->slots[i]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1062 | if (slot < btrfs_header_nritems( |
| 1063 | btrfs_buffer_header(path->nodes[i])) - 1) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1064 | path->slots[i]++; |
| 1065 | *level = i; |
| 1066 | return 0; |
| 1067 | } else { |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1068 | ret = btrfs_free_extent(trans, root, |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1069 | bh_blocknr(path->nodes[*level]), |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1070 | 1, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1071 | BUG_ON(ret); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1072 | btrfs_block_release(root, path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1073 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1074 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | return 1; |
| 1078 | } |
| 1079 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1080 | /* |
| 1081 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 1082 | * the tree freeing any blocks that have a ref count of zero after being |
| 1083 | * decremented. |
| 1084 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1085 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1086 | *root, struct buffer_head *snap) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1087 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1088 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1089 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1090 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1091 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1092 | int i; |
| 1093 | int orig_level; |
| 1094 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1095 | path = btrfs_alloc_path(); |
| 1096 | BUG_ON(!path); |
| 1097 | btrfs_init_path(path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1098 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1099 | level = btrfs_header_level(btrfs_buffer_header(snap)); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1100 | orig_level = level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1101 | path->nodes[level] = snap; |
| 1102 | path->slots[level] = 0; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1103 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1104 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1105 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1106 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1107 | if (wret < 0) |
| 1108 | ret = wret; |
| 1109 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1110 | wret = walk_up_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1111 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1112 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1113 | if (wret < 0) |
| 1114 | ret = wret; |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 1115 | btrfs_btree_balance_dirty(root); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1116 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1117 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1118 | if (path->nodes[i]) { |
| 1119 | btrfs_block_release(root, path->nodes[i]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1120 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1121 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1122 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1123 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1124 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1125 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1126 | static int free_block_group_radix(struct radix_tree_root *radix) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1127 | { |
| 1128 | int ret; |
| 1129 | struct btrfs_block_group_cache *cache[8]; |
| 1130 | int i; |
| 1131 | |
| 1132 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1133 | ret = radix_tree_gang_lookup(radix, (void **)cache, 0, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1134 | ARRAY_SIZE(cache)); |
| 1135 | if (!ret) |
| 1136 | break; |
| 1137 | for (i = 0; i < ret; i++) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1138 | radix_tree_delete(radix, cache[i]->key.objectid + |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1139 | cache[i]->key.offset - 1); |
| 1140 | kfree(cache[i]); |
| 1141 | } |
| 1142 | } |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1146 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 1147 | { |
| 1148 | int ret; |
| 1149 | int ret2; |
| 1150 | |
| 1151 | ret = free_block_group_radix(&info->block_group_radix); |
| 1152 | ret2 = free_block_group_radix(&info->block_group_data_radix); |
| 1153 | if (ret) |
| 1154 | return ret; |
| 1155 | if (ret2) |
| 1156 | return ret2; |
| 1157 | return 0; |
| 1158 | } |
| 1159 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1160 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 1161 | { |
| 1162 | struct btrfs_path *path; |
| 1163 | int ret; |
| 1164 | int err = 0; |
| 1165 | struct btrfs_block_group_item *bi; |
| 1166 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1167 | struct btrfs_fs_info *info = root->fs_info; |
| 1168 | struct radix_tree_root *radix; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1169 | struct btrfs_key key; |
| 1170 | struct btrfs_key found_key; |
| 1171 | struct btrfs_leaf *leaf; |
| 1172 | u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1173 | u64 used; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1174 | u64 nr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1175 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1176 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1177 | key.objectid = 0; |
| 1178 | key.offset = group_size_blocks; |
| 1179 | key.flags = 0; |
| 1180 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
| 1181 | |
| 1182 | path = btrfs_alloc_path(); |
| 1183 | if (!path) |
| 1184 | return -ENOMEM; |
| 1185 | |
| 1186 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1187 | ret = btrfs_search_slot(NULL, info->extent_root, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1188 | &key, path, 0, 0); |
| 1189 | if (ret != 0) { |
| 1190 | err = ret; |
| 1191 | break; |
| 1192 | } |
| 1193 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1194 | btrfs_disk_key_to_cpu(&found_key, |
| 1195 | &leaf->items[path->slots[0]].key); |
| 1196 | cache = kmalloc(sizeof(*cache), GFP_NOFS); |
| 1197 | if (!cache) { |
| 1198 | err = -1; |
| 1199 | break; |
| 1200 | } |
| 1201 | bi = btrfs_item_ptr(leaf, path->slots[0], |
| 1202 | struct btrfs_block_group_item); |
| 1203 | memcpy(&cache->item, bi, sizeof(*bi)); |
| 1204 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1205 | cache->last_alloc = cache->key.objectid; |
| 1206 | cache->first_free = cache->key.objectid; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1207 | cache->pinned = 0; |
| 1208 | cache->data = (nr & 1); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1209 | key.objectid = found_key.objectid + found_key.offset; |
| 1210 | btrfs_release_path(root, path); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1211 | if (nr & 1) |
| 1212 | radix = &info->block_group_data_radix; |
| 1213 | else |
| 1214 | radix = &info->block_group_radix; |
| 1215 | ret = radix_tree_insert(radix, found_key.objectid + |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1216 | found_key.offset - 1, |
| 1217 | (void *)cache); |
| 1218 | BUG_ON(ret); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1219 | used = btrfs_block_group_used(bi); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1220 | if (used < (key.offset * 8) / 10) { |
| 1221 | radix_tree_tag_set(radix, found_key.objectid + |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 1222 | found_key.offset - 1, |
| 1223 | BTRFS_BLOCK_GROUP_AVAIL); |
| 1224 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1225 | if (key.objectid >= |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1226 | btrfs_super_total_blocks(info->disk_super)) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1227 | break; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame^] | 1228 | nr++; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | btrfs_free_path(path); |
| 1232 | return 0; |
| 1233 | } |