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