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