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