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