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