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