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 | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 20 | #include <linux/crc32c.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 21 | #include <linux/pagemap.h> |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 22 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 23 | #include "ctree.h" |
| 24 | #include "disk-io.h" |
| 25 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 26 | #include "transaction.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 27 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 28 | #define BLOCK_GROUP_DATA EXTENT_WRITEBACK |
| 29 | #define BLOCK_GROUP_METADATA EXTENT_UPTODATE |
| 30 | #define BLOCK_GROUP_DIRTY EXTENT_DIRTY |
| 31 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 32 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
| 33 | btrfs_root *extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 34 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 35 | btrfs_root *extent_root); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 36 | static int find_previous_extent(struct btrfs_root *root, |
| 37 | struct btrfs_path *path) |
| 38 | { |
| 39 | struct btrfs_key found_key; |
| 40 | struct extent_buffer *leaf; |
| 41 | int ret; |
| 42 | |
| 43 | while(1) { |
| 44 | if (path->slots[0] == 0) { |
| 45 | ret = btrfs_prev_leaf(root, path); |
| 46 | if (ret != 0) |
| 47 | return ret; |
| 48 | } else { |
| 49 | path->slots[0]--; |
| 50 | } |
| 51 | leaf = path->nodes[0]; |
| 52 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 53 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY) |
| 54 | return 0; |
| 55 | } |
| 56 | return 1; |
| 57 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 58 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 59 | static int cache_block_group(struct btrfs_root *root, |
| 60 | struct btrfs_block_group_cache *block_group) |
| 61 | { |
| 62 | struct btrfs_path *path; |
| 63 | int ret; |
| 64 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 65 | struct extent_buffer *leaf; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 66 | struct extent_map_tree *free_space_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 67 | int slot; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 68 | u64 last = 0; |
| 69 | u64 hole_size; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 70 | u64 first_free; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 71 | int found = 0; |
| 72 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 73 | if (!block_group) |
| 74 | return 0; |
| 75 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 76 | root = root->fs_info->extent_root; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 77 | free_space_cache = &root->fs_info->free_space_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 78 | |
| 79 | if (block_group->cached) |
| 80 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 81 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 82 | path = btrfs_alloc_path(); |
| 83 | if (!path) |
| 84 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 85 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 86 | path->reada = 2; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 87 | first_free = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 88 | key.objectid = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 89 | key.offset = 0; |
| 90 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 91 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 92 | if (ret < 0) |
| 93 | return ret; |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 94 | ret = find_previous_extent(root, path); |
| 95 | if (ret < 0) |
| 96 | return ret; |
| 97 | if (ret == 0) { |
| 98 | leaf = path->nodes[0]; |
| 99 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 100 | if (key.objectid + key.offset > first_free) |
| 101 | first_free = key.objectid + key.offset; |
| 102 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 103 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 104 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 105 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 106 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 107 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 108 | if (ret < 0) |
| 109 | goto err; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 110 | if (ret == 0) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 111 | continue; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 112 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 113 | break; |
| 114 | } |
| 115 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 116 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 117 | if (key.objectid < block_group->key.objectid) { |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 118 | goto next; |
| 119 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 120 | if (key.objectid >= block_group->key.objectid + |
| 121 | block_group->key.offset) { |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | |
| 125 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
| 126 | if (!found) { |
| 127 | last = first_free; |
| 128 | found = 1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 129 | } |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 130 | if (key.objectid > last) { |
| 131 | hole_size = key.objectid - last; |
| 132 | set_extent_dirty(free_space_cache, last, |
| 133 | last + hole_size - 1, |
| 134 | GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 135 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 136 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 137 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 138 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 139 | path->slots[0]++; |
| 140 | } |
| 141 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 142 | if (!found) |
| 143 | last = first_free; |
| 144 | if (block_group->key.objectid + |
| 145 | block_group->key.offset > last) { |
| 146 | hole_size = block_group->key.objectid + |
| 147 | block_group->key.offset - last; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 148 | set_extent_dirty(free_space_cache, last, |
| 149 | last + hole_size - 1, GFP_NOFS); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 150 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 151 | block_group->cached = 1; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 152 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 153 | btrfs_free_path(path); |
| 154 | return 0; |
| 155 | } |
| 156 | |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 157 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
| 158 | btrfs_fs_info *info, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 159 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 160 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 161 | struct extent_map_tree *block_group_cache; |
| 162 | struct btrfs_block_group_cache *block_group = NULL; |
| 163 | u64 ptr; |
| 164 | u64 start; |
| 165 | u64 end; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 166 | int ret; |
| 167 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 168 | block_group_cache = &info->block_group_cache; |
| 169 | ret = find_first_extent_bit(block_group_cache, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 170 | bytenr, &start, &end, |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 171 | BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 172 | if (ret) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 173 | return NULL; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 174 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 175 | ret = get_state_private(block_group_cache, start, &ptr); |
| 176 | if (ret) |
| 177 | return NULL; |
| 178 | |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 179 | block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Yan | 5cf6642 | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 180 | if (block_group->key.objectid <= bytenr && bytenr < |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 181 | block_group->key.objectid + block_group->key.offset) |
| 182 | return block_group; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 183 | return NULL; |
| 184 | } |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 185 | static u64 noinline find_search_start(struct btrfs_root *root, |
| 186 | struct btrfs_block_group_cache **cache_ret, |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 187 | u64 search_start, int num, int data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 188 | { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 189 | int ret; |
| 190 | struct btrfs_block_group_cache *cache = *cache_ret; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 191 | u64 last; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 192 | u64 start = 0; |
| 193 | u64 end = 0; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 194 | u64 cache_miss = 0; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 195 | int wrapped = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 196 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 197 | if (!cache) { |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 198 | goto out; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 199 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 200 | again: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 201 | ret = cache_block_group(root, cache); |
| 202 | if (ret) |
| 203 | goto out; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 204 | |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 205 | last = max(search_start, cache->key.objectid); |
| 206 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 207 | while(1) { |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 208 | ret = find_first_extent_bit(&root->fs_info->free_space_cache, |
| 209 | last, &start, &end, EXTENT_DIRTY); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 210 | if (ret) { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 211 | if (!cache_miss) |
| 212 | cache_miss = last; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 213 | goto new_group; |
| 214 | } |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 215 | |
| 216 | start = max(last, start); |
| 217 | last = end + 1; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 218 | if (last - start < num) { |
| 219 | if (last == cache->key.objectid + cache->key.offset) |
| 220 | cache_miss = start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 221 | continue; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 222 | } |
| 223 | if (data != BTRFS_BLOCK_GROUP_MIXED && |
Yan | 5cf6642 | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 224 | start + num > cache->key.objectid + cache->key.offset) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 225 | goto new_group; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 226 | return start; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 227 | } |
| 228 | out: |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 229 | cache = btrfs_lookup_block_group(root->fs_info, search_start); |
| 230 | if (!cache) { |
| 231 | printk("Unable to find block group for %Lu\n", |
| 232 | search_start); |
| 233 | WARN_ON(1); |
| 234 | return search_start; |
| 235 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 236 | return search_start; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 237 | |
| 238 | new_group: |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 239 | last = cache->key.objectid + cache->key.offset; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 240 | wrapped: |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 241 | cache = btrfs_lookup_block_group(root->fs_info, last); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 242 | if (!cache) { |
Chris Mason | 0e4de58 | 2007-11-26 10:55:49 -0500 | [diff] [blame] | 243 | no_cache: |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 244 | if (!wrapped) { |
| 245 | wrapped = 1; |
| 246 | last = search_start; |
| 247 | data = BTRFS_BLOCK_GROUP_MIXED; |
| 248 | goto wrapped; |
| 249 | } |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 250 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 251 | } |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 252 | if (cache_miss && !cache->cached) { |
| 253 | cache_block_group(root, cache); |
| 254 | last = cache_miss; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 255 | cache = btrfs_lookup_block_group(root->fs_info, last); |
| 256 | } |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 257 | cache = btrfs_find_block_group(root, cache, last, data, 0); |
Chris Mason | 0e4de58 | 2007-11-26 10:55:49 -0500 | [diff] [blame] | 258 | if (!cache) |
| 259 | goto no_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 260 | *cache_ret = cache; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 261 | cache_miss = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 262 | goto again; |
| 263 | } |
| 264 | |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 265 | static u64 div_factor(u64 num, int factor) |
| 266 | { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 267 | if (factor == 10) |
| 268 | return num; |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 269 | num *= factor; |
| 270 | do_div(num, 10); |
| 271 | return num; |
| 272 | } |
| 273 | |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 274 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 275 | struct btrfs_block_group_cache |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 276 | *hint, u64 search_start, |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 277 | int data, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 278 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 279 | struct btrfs_block_group_cache *cache; |
| 280 | struct extent_map_tree *block_group_cache; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 281 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 282 | struct btrfs_fs_info *info = root->fs_info; |
| 283 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 284 | u64 last = 0; |
| 285 | u64 hint_last; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 286 | u64 start; |
| 287 | u64 end; |
| 288 | u64 free_check; |
| 289 | u64 ptr; |
| 290 | int bit; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 291 | int ret; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 292 | int full_search = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 293 | int factor = 8; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 294 | int data_swap = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 295 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 296 | block_group_cache = &info->block_group_cache; |
| 297 | |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 298 | if (!owner) |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 299 | factor = 8; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 300 | |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 301 | if (data == BTRFS_BLOCK_GROUP_MIXED) { |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 302 | bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 303 | factor = 10; |
| 304 | } else if (data) |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 305 | bit = BLOCK_GROUP_DATA; |
| 306 | else |
| 307 | bit = BLOCK_GROUP_METADATA; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 308 | |
| 309 | if (search_start) { |
| 310 | struct btrfs_block_group_cache *shint; |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 311 | shint = btrfs_lookup_block_group(info, search_start); |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 312 | if (shint && (shint->data == data || |
| 313 | shint->data == BTRFS_BLOCK_GROUP_MIXED)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 314 | used = btrfs_block_group_used(&shint->item); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 315 | if (used + shint->pinned < |
| 316 | div_factor(shint->key.offset, factor)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 317 | return shint; |
| 318 | } |
| 319 | } |
| 320 | } |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 321 | if (hint && (hint->data == data || |
| 322 | hint->data == BTRFS_BLOCK_GROUP_MIXED)) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 323 | used = btrfs_block_group_used(&hint->item); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 324 | if (used + hint->pinned < |
| 325 | div_factor(hint->key.offset, factor)) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 326 | return hint; |
| 327 | } |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 328 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 329 | hint_last = last; |
| 330 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 331 | if (hint) |
| 332 | hint_last = max(hint->key.objectid, search_start); |
| 333 | else |
| 334 | hint_last = search_start; |
| 335 | |
| 336 | last = hint_last; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 337 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 338 | again: |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 339 | while(1) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 340 | ret = find_first_extent_bit(block_group_cache, last, |
| 341 | &start, &end, bit); |
| 342 | if (ret) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 343 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 344 | |
| 345 | ret = get_state_private(block_group_cache, start, &ptr); |
| 346 | if (ret) |
| 347 | break; |
| 348 | |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 349 | cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 350 | last = cache->key.objectid + cache->key.offset; |
| 351 | used = btrfs_block_group_used(&cache->item); |
| 352 | |
| 353 | if (full_search) |
| 354 | free_check = cache->key.offset; |
| 355 | else |
| 356 | free_check = div_factor(cache->key.offset, factor); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 357 | if (used + cache->pinned < free_check) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 358 | found_group = cache; |
| 359 | goto found; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 360 | } |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 361 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 362 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 363 | if (!full_search) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 364 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 365 | full_search = 1; |
| 366 | goto again; |
| 367 | } |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 368 | if (!data_swap) { |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 369 | data_swap = 1; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 370 | bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 371 | last = search_start; |
| 372 | goto again; |
| 373 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 374 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 375 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 376 | } |
| 377 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 378 | static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 379 | u64 owner, u64 owner_offset) |
| 380 | { |
| 381 | u32 high_crc = ~(u32)0; |
| 382 | u32 low_crc = ~(u32)0; |
| 383 | __le64 lenum; |
| 384 | |
| 385 | lenum = cpu_to_le64(root_objectid); |
| 386 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 387 | lenum = cpu_to_le64(ref_generation); |
| 388 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 389 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 390 | #if 0 |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 391 | lenum = cpu_to_le64(owner); |
| 392 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 393 | lenum = cpu_to_le64(owner_offset); |
| 394 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 395 | #endif |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 396 | return ((u64)high_crc << 32) | (u64)low_crc; |
| 397 | } |
| 398 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 399 | static int match_extent_ref(struct extent_buffer *leaf, |
| 400 | struct btrfs_extent_ref *disk_ref, |
| 401 | struct btrfs_extent_ref *cpu_ref) |
| 402 | { |
| 403 | int ret; |
| 404 | int len; |
| 405 | |
| 406 | if (cpu_ref->objectid) |
| 407 | len = sizeof(*cpu_ref); |
| 408 | else |
| 409 | len = 2 * sizeof(u64); |
| 410 | ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref, |
| 411 | len); |
| 412 | return ret == 0; |
| 413 | } |
| 414 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 415 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 416 | struct btrfs_root *root, |
| 417 | struct btrfs_path *path, u64 bytenr, |
| 418 | u64 root_objectid, |
| 419 | u64 ref_generation, u64 owner, |
| 420 | u64 owner_offset, int del) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 421 | { |
| 422 | u64 hash; |
| 423 | struct btrfs_key key; |
| 424 | struct btrfs_key found_key; |
| 425 | struct btrfs_extent_ref ref; |
| 426 | struct extent_buffer *leaf; |
| 427 | struct btrfs_extent_ref *disk_ref; |
| 428 | int ret; |
| 429 | int ret2; |
| 430 | |
| 431 | btrfs_set_stack_ref_root(&ref, root_objectid); |
| 432 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
| 433 | btrfs_set_stack_ref_objectid(&ref, owner); |
| 434 | btrfs_set_stack_ref_offset(&ref, owner_offset); |
| 435 | |
| 436 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
| 437 | owner_offset); |
| 438 | key.offset = hash; |
| 439 | key.objectid = bytenr; |
| 440 | key.type = BTRFS_EXTENT_REF_KEY; |
| 441 | |
| 442 | while (1) { |
| 443 | ret = btrfs_search_slot(trans, root, &key, path, |
| 444 | del ? -1 : 0, del); |
| 445 | if (ret < 0) |
| 446 | goto out; |
| 447 | leaf = path->nodes[0]; |
| 448 | if (ret != 0) { |
| 449 | u32 nritems = btrfs_header_nritems(leaf); |
| 450 | if (path->slots[0] >= nritems) { |
| 451 | ret2 = btrfs_next_leaf(root, path); |
| 452 | if (ret2) |
| 453 | goto out; |
| 454 | leaf = path->nodes[0]; |
| 455 | } |
| 456 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 457 | if (found_key.objectid != bytenr || |
| 458 | found_key.type != BTRFS_EXTENT_REF_KEY) |
| 459 | goto out; |
| 460 | key.offset = found_key.offset; |
| 461 | if (del) { |
| 462 | btrfs_release_path(root, path); |
| 463 | continue; |
| 464 | } |
| 465 | } |
| 466 | disk_ref = btrfs_item_ptr(path->nodes[0], |
| 467 | path->slots[0], |
| 468 | struct btrfs_extent_ref); |
| 469 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) { |
| 470 | ret = 0; |
| 471 | goto out; |
| 472 | } |
| 473 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 474 | key.offset = found_key.offset + 1; |
| 475 | btrfs_release_path(root, path); |
| 476 | } |
| 477 | out: |
| 478 | return ret; |
| 479 | } |
| 480 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 481 | /* |
| 482 | * Back reference rules. Back refs have three main goals: |
| 483 | * |
| 484 | * 1) differentiate between all holders of references to an extent so that |
| 485 | * when a reference is dropped we can make sure it was a valid reference |
| 486 | * before freeing the extent. |
| 487 | * |
| 488 | * 2) Provide enough information to quickly find the holders of an extent |
| 489 | * if we notice a given block is corrupted or bad. |
| 490 | * |
| 491 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 492 | * maintenance. This is actually the same as #2, but with a slightly |
| 493 | * different use case. |
| 494 | * |
| 495 | * File extents can be referenced by: |
| 496 | * |
| 497 | * - multiple snapshots, subvolumes, or different generations in one subvol |
| 498 | * - different files inside a single subvolume (in theory, not implemented yet) |
| 499 | * - different offsets inside a file (bookend extents in file.c) |
| 500 | * |
| 501 | * The extent ref structure has fields for: |
| 502 | * |
| 503 | * - Objectid of the subvolume root |
| 504 | * - Generation number of the tree holding the reference |
| 505 | * - objectid of the file holding the reference |
| 506 | * - offset in the file corresponding to the key holding the reference |
| 507 | * |
| 508 | * When a file extent is allocated the fields are filled in: |
| 509 | * (root_key.objectid, trans->transid, inode objectid, offset in file) |
| 510 | * |
| 511 | * When a leaf is cow'd new references are added for every file extent found |
| 512 | * in the leaf. It looks the same as the create case, but trans->transid |
| 513 | * will be different when the block is cow'd. |
| 514 | * |
| 515 | * (root_key.objectid, trans->transid, inode objectid, offset in file) |
| 516 | * |
| 517 | * When a file extent is removed either during snapshot deletion or file |
| 518 | * truncation, the corresponding back reference is found |
| 519 | * by searching for: |
| 520 | * |
| 521 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 522 | * inode objectid, offset in file) |
| 523 | * |
| 524 | * Btree extents can be referenced by: |
| 525 | * |
| 526 | * - Different subvolumes |
| 527 | * - Different generations of the same subvolume |
| 528 | * |
| 529 | * Storing sufficient information for a full reverse mapping of a btree |
| 530 | * block would require storing the lowest key of the block in the backref, |
| 531 | * and it would require updating that lowest key either before write out or |
| 532 | * every time it changed. Instead, the objectid of the lowest key is stored |
| 533 | * along with the level of the tree block. This provides a hint |
| 534 | * about where in the btree the block can be found. Searches through the |
| 535 | * btree only need to look for a pointer to that block, so they stop one |
| 536 | * level higher than the level recorded in the backref. |
| 537 | * |
| 538 | * Some btrees do not do reference counting on their extents. These |
| 539 | * include the extent tree and the tree of tree roots. Backrefs for these |
| 540 | * trees always have a generation of zero. |
| 541 | * |
| 542 | * When a tree block is created, back references are inserted: |
| 543 | * |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 544 | * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 545 | * |
| 546 | * When a tree block is cow'd in a reference counted root, |
| 547 | * new back references are added for all the blocks it points to. |
| 548 | * These are of the form (trans->transid will have increased since creation): |
| 549 | * |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 550 | * (root->root_key.objectid, trans->transid, level, lowest_key_objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 551 | * |
| 552 | * Because the lowest_key_objectid and the level are just hints |
| 553 | * they are not used when backrefs are deleted. When a backref is deleted: |
| 554 | * |
| 555 | * if backref was for a tree root: |
| 556 | * root_objectid = root->root_key.objectid |
| 557 | * else |
| 558 | * root_objectid = btrfs_header_owner(parent) |
| 559 | * |
| 560 | * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0) |
| 561 | * |
| 562 | * Back Reference Key hashing: |
| 563 | * |
| 564 | * Back references have four fields, each 64 bits long. Unfortunately, |
| 565 | * This is hashed into a single 64 bit number and placed into the key offset. |
| 566 | * The key objectid corresponds to the first byte in the extent, and the |
| 567 | * key type is set to BTRFS_EXTENT_REF_KEY |
| 568 | */ |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 569 | int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, |
| 570 | struct btrfs_root *root, |
| 571 | struct btrfs_path *path, u64 bytenr, |
| 572 | u64 root_objectid, u64 ref_generation, |
| 573 | u64 owner, u64 owner_offset) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 574 | { |
| 575 | u64 hash; |
| 576 | struct btrfs_key key; |
| 577 | struct btrfs_extent_ref ref; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 578 | struct btrfs_extent_ref *disk_ref; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 579 | int ret; |
| 580 | |
| 581 | btrfs_set_stack_ref_root(&ref, root_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 582 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 583 | btrfs_set_stack_ref_objectid(&ref, owner); |
| 584 | btrfs_set_stack_ref_offset(&ref, owner_offset); |
| 585 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 586 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
| 587 | owner_offset); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 588 | key.offset = hash; |
| 589 | key.objectid = bytenr; |
| 590 | key.type = BTRFS_EXTENT_REF_KEY; |
| 591 | |
| 592 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref)); |
| 593 | while (ret == -EEXIST) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 594 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 595 | struct btrfs_extent_ref); |
| 596 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) |
| 597 | goto out; |
| 598 | key.offset++; |
| 599 | btrfs_release_path(root, path); |
| 600 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 601 | sizeof(ref)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 602 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 603 | if (ret) |
| 604 | goto out; |
| 605 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 606 | struct btrfs_extent_ref); |
| 607 | write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref, |
| 608 | sizeof(ref)); |
| 609 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 610 | out: |
| 611 | btrfs_release_path(root, path); |
| 612 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 613 | } |
| 614 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 615 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 616 | struct btrfs_root *root, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 617 | u64 bytenr, u64 num_bytes, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 618 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 619 | u64 owner, u64 owner_offset) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 620 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 621 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 622 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 623 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 624 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 625 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 626 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 627 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 628 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 629 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 630 | if (!path) |
| 631 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 632 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 633 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 634 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 635 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 636 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 637 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 638 | if (ret < 0) |
| 639 | return ret; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 640 | if (ret != 0) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 641 | BUG(); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 642 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 643 | BUG_ON(ret != 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 644 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 645 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 646 | refs = btrfs_extent_refs(l, item); |
| 647 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 648 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 649 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 650 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 651 | |
| 652 | ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root, |
| 653 | path, bytenr, root_objectid, |
| 654 | ref_generation, owner, owner_offset); |
| 655 | BUG_ON(ret); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 656 | finish_current_insert(trans, root->fs_info->extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 657 | del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 658 | |
| 659 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 660 | return 0; |
| 661 | } |
| 662 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 663 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 664 | struct btrfs_root *root) |
| 665 | { |
| 666 | finish_current_insert(trans, root->fs_info->extent_root); |
| 667 | del_pending_extents(trans, root->fs_info->extent_root); |
| 668 | return 0; |
| 669 | } |
| 670 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 671 | static int lookup_extent_ref(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 672 | struct btrfs_root *root, u64 bytenr, |
| 673 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 674 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 675 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 676 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 677 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 678 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 679 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 680 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 681 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 682 | path = btrfs_alloc_path(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 683 | key.objectid = bytenr; |
| 684 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 685 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 686 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 687 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 688 | if (ret < 0) |
| 689 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 690 | if (ret != 0) { |
| 691 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 692 | printk("failed to find block number %Lu\n", bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 693 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 694 | } |
| 695 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 696 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 697 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 698 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 699 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 703 | u32 btrfs_count_snapshots_in_path(struct btrfs_root *root, |
| 704 | struct btrfs_path *count_path, |
| 705 | u64 first_extent) |
| 706 | { |
| 707 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 708 | struct btrfs_path *path; |
| 709 | u64 bytenr; |
| 710 | u64 found_objectid; |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 711 | u64 root_objectid = root->root_key.objectid; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 712 | u32 total_count = 0; |
| 713 | u32 cur_count; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 714 | u32 nritems; |
| 715 | int ret; |
| 716 | struct btrfs_key key; |
| 717 | struct btrfs_key found_key; |
| 718 | struct extent_buffer *l; |
| 719 | struct btrfs_extent_item *item; |
| 720 | struct btrfs_extent_ref *ref_item; |
| 721 | int level = -1; |
| 722 | |
| 723 | path = btrfs_alloc_path(); |
| 724 | again: |
| 725 | if (level == -1) |
| 726 | bytenr = first_extent; |
| 727 | else |
| 728 | bytenr = count_path->nodes[level]->start; |
| 729 | |
| 730 | cur_count = 0; |
| 731 | key.objectid = bytenr; |
| 732 | key.offset = 0; |
| 733 | |
| 734 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 735 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 736 | if (ret < 0) |
| 737 | goto out; |
| 738 | BUG_ON(ret == 0); |
| 739 | |
| 740 | l = path->nodes[0]; |
| 741 | btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); |
| 742 | |
| 743 | if (found_key.objectid != bytenr || |
| 744 | found_key.type != BTRFS_EXTENT_ITEM_KEY) { |
| 745 | goto out; |
| 746 | } |
| 747 | |
| 748 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 749 | while (1) { |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 750 | l = path->nodes[0]; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 751 | nritems = btrfs_header_nritems(l); |
| 752 | if (path->slots[0] >= nritems) { |
| 753 | ret = btrfs_next_leaf(extent_root, path); |
| 754 | if (ret == 0) |
| 755 | continue; |
| 756 | break; |
| 757 | } |
| 758 | btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); |
| 759 | if (found_key.objectid != bytenr) |
| 760 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 761 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 762 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 763 | path->slots[0]++; |
| 764 | continue; |
| 765 | } |
| 766 | |
| 767 | cur_count++; |
| 768 | ref_item = btrfs_item_ptr(l, path->slots[0], |
| 769 | struct btrfs_extent_ref); |
| 770 | found_objectid = btrfs_ref_root(l, ref_item); |
| 771 | |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 772 | if (found_objectid != root_objectid) { |
| 773 | total_count = 2; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 774 | goto out; |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 775 | } |
| 776 | total_count = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 777 | path->slots[0]++; |
| 778 | } |
| 779 | if (cur_count == 0) { |
| 780 | total_count = 0; |
| 781 | goto out; |
| 782 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 783 | if (level >= 0 && root->node == count_path->nodes[level]) |
| 784 | goto out; |
| 785 | level++; |
| 786 | btrfs_release_path(root, path); |
| 787 | goto again; |
| 788 | |
| 789 | out: |
| 790 | btrfs_free_path(path); |
| 791 | return total_count; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 792 | } |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 793 | int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 794 | struct btrfs_root *root, u64 owner_objectid) |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 795 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 796 | u64 generation; |
| 797 | u64 key_objectid; |
| 798 | u64 level; |
| 799 | u32 nritems; |
| 800 | struct btrfs_disk_key disk_key; |
| 801 | |
| 802 | level = btrfs_header_level(root->node); |
| 803 | generation = trans->transid; |
| 804 | nritems = btrfs_header_nritems(root->node); |
| 805 | if (nritems > 0) { |
| 806 | if (level == 0) |
| 807 | btrfs_item_key(root->node, &disk_key, 0); |
| 808 | else |
| 809 | btrfs_node_key(root->node, &disk_key, 0); |
| 810 | key_objectid = btrfs_disk_key_objectid(&disk_key); |
| 811 | } else { |
| 812 | key_objectid = 0; |
| 813 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 814 | return btrfs_inc_extent_ref(trans, root, root->node->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 815 | root->node->len, owner_objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 816 | generation, level, key_objectid); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 817 | } |
| 818 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 819 | 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] | 820 | struct extent_buffer *buf) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 821 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 822 | u64 bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 823 | u32 nritems; |
| 824 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 825 | struct btrfs_file_extent_item *fi; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 826 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 827 | int level; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 828 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 829 | int faili; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 830 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 831 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 832 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 833 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 834 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 835 | nritems = btrfs_header_nritems(buf); |
| 836 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 837 | if (level == 0) { |
| 838 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 839 | btrfs_item_key_to_cpu(buf, &key, i); |
| 840 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 841 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 842 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 843 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 844 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 845 | BTRFS_FILE_EXTENT_INLINE) |
| 846 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 847 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 848 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 849 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 850 | ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 851 | btrfs_file_extent_disk_num_bytes(buf, fi), |
| 852 | root->root_key.objectid, trans->transid, |
| 853 | key.objectid, key.offset); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 854 | if (ret) { |
| 855 | faili = i; |
| 856 | goto fail; |
| 857 | } |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 858 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 859 | bytenr = btrfs_node_blockptr(buf, i); |
Chris Mason | 6caab48 | 2007-12-13 09:48:07 -0500 | [diff] [blame] | 860 | btrfs_node_key_to_cpu(buf, &key, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 861 | ret = btrfs_inc_extent_ref(trans, root, bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 862 | btrfs_level_size(root, level - 1), |
| 863 | root->root_key.objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 864 | trans->transid, |
| 865 | level - 1, key.objectid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 866 | if (ret) { |
| 867 | faili = i; |
| 868 | goto fail; |
| 869 | } |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 870 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 871 | } |
| 872 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 873 | fail: |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 874 | WARN_ON(1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 875 | #if 0 |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 876 | for (i =0; i < faili; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 877 | if (level == 0) { |
| 878 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 879 | btrfs_item_key_to_cpu(buf, &key, i); |
| 880 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 881 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 882 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 883 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 884 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 885 | BTRFS_FILE_EXTENT_INLINE) |
| 886 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 887 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 888 | if (disk_bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 889 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 890 | err = btrfs_free_extent(trans, root, disk_bytenr, |
| 891 | btrfs_file_extent_disk_num_bytes(buf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 892 | fi), 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 893 | BUG_ON(err); |
| 894 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 895 | bytenr = btrfs_node_blockptr(buf, i); |
| 896 | err = btrfs_free_extent(trans, root, bytenr, |
| 897 | btrfs_level_size(root, level - 1), 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 898 | BUG_ON(err); |
| 899 | } |
| 900 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 901 | #endif |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 902 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 903 | } |
| 904 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 905 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 906 | struct btrfs_root *root, |
| 907 | struct btrfs_path *path, |
| 908 | struct btrfs_block_group_cache *cache) |
| 909 | { |
| 910 | int ret; |
| 911 | int pending_ret; |
| 912 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 913 | unsigned long bi; |
| 914 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 915 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 916 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 917 | if (ret < 0) |
| 918 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 919 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 920 | |
| 921 | leaf = path->nodes[0]; |
| 922 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 923 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 924 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 925 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 926 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 927 | finish_current_insert(trans, extent_root); |
| 928 | pending_ret = del_pending_extents(trans, extent_root); |
| 929 | if (ret) |
| 930 | return ret; |
| 931 | if (pending_ret) |
| 932 | return pending_ret; |
| 933 | return 0; |
| 934 | |
| 935 | } |
| 936 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 937 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 938 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 939 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 940 | struct extent_map_tree *block_group_cache; |
| 941 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 942 | int ret; |
| 943 | int err = 0; |
| 944 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 945 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 946 | u64 last = 0; |
| 947 | u64 start; |
| 948 | u64 end; |
| 949 | u64 ptr; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 950 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 951 | block_group_cache = &root->fs_info->block_group_cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 952 | path = btrfs_alloc_path(); |
| 953 | if (!path) |
| 954 | return -ENOMEM; |
| 955 | |
| 956 | while(1) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 957 | ret = find_first_extent_bit(block_group_cache, last, |
| 958 | &start, &end, BLOCK_GROUP_DIRTY); |
| 959 | if (ret) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 960 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 961 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 962 | last = end + 1; |
| 963 | ret = get_state_private(block_group_cache, start, &ptr); |
| 964 | if (ret) |
| 965 | break; |
| 966 | |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 967 | cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 968 | err = write_one_cache_group(trans, root, |
| 969 | path, cache); |
| 970 | /* |
| 971 | * if we fail to write the cache group, we want |
| 972 | * to keep it marked dirty in hopes that a later |
| 973 | * write will work |
| 974 | */ |
| 975 | if (err) { |
| 976 | werr = err; |
| 977 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 978 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 979 | clear_extent_bits(block_group_cache, start, end, |
| 980 | BLOCK_GROUP_DIRTY, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 981 | } |
| 982 | btrfs_free_path(path); |
| 983 | return werr; |
| 984 | } |
| 985 | |
| 986 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 987 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 988 | u64 bytenr, u64 num_bytes, int alloc, |
| 989 | int mark_free, int data) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 990 | { |
| 991 | struct btrfs_block_group_cache *cache; |
| 992 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 993 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 994 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 995 | u64 byte_in_group; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 996 | u64 start; |
| 997 | u64 end; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 998 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 999 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1000 | cache = btrfs_lookup_block_group(info, bytenr); |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1001 | if (!cache) { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1002 | return -1; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1003 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1004 | byte_in_group = bytenr - cache->key.objectid; |
| 1005 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1006 | start = cache->key.objectid; |
| 1007 | end = start + cache->key.offset - 1; |
| 1008 | set_extent_bits(&info->block_group_cache, start, end, |
| 1009 | BLOCK_GROUP_DIRTY, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1010 | |
| 1011 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1012 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1013 | if (alloc) { |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1014 | if (cache->data != data && |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 1015 | old_val < (cache->key.offset >> 1)) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1016 | int bit_to_clear; |
| 1017 | int bit_to_set; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1018 | cache->data = data; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1019 | if (data) { |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1020 | bit_to_clear = BLOCK_GROUP_METADATA; |
| 1021 | bit_to_set = BLOCK_GROUP_DATA; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1022 | cache->item.flags &= |
| 1023 | ~BTRFS_BLOCK_GROUP_MIXED; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1024 | cache->item.flags |= |
| 1025 | BTRFS_BLOCK_GROUP_DATA; |
| 1026 | } else { |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1027 | bit_to_clear = BLOCK_GROUP_DATA; |
| 1028 | bit_to_set = BLOCK_GROUP_METADATA; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1029 | cache->item.flags &= |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1030 | ~BTRFS_BLOCK_GROUP_MIXED; |
| 1031 | cache->item.flags &= |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1032 | ~BTRFS_BLOCK_GROUP_DATA; |
| 1033 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1034 | clear_extent_bits(&info->block_group_cache, |
| 1035 | start, end, bit_to_clear, |
| 1036 | GFP_NOFS); |
| 1037 | set_extent_bits(&info->block_group_cache, |
| 1038 | start, end, bit_to_set, |
| 1039 | GFP_NOFS); |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1040 | } else if (cache->data != data && |
| 1041 | cache->data != BTRFS_BLOCK_GROUP_MIXED) { |
| 1042 | cache->data = BTRFS_BLOCK_GROUP_MIXED; |
| 1043 | set_extent_bits(&info->block_group_cache, |
| 1044 | start, end, |
| 1045 | BLOCK_GROUP_DATA | |
| 1046 | BLOCK_GROUP_METADATA, |
| 1047 | GFP_NOFS); |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1048 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1049 | old_val += num_bytes; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1050 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1051 | old_val -= num_bytes; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1052 | if (mark_free) { |
| 1053 | set_extent_dirty(&info->free_space_cache, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1054 | bytenr, bytenr + num_bytes - 1, |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1055 | GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1056 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1057 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1058 | btrfs_set_block_group_used(&cache->item, old_val); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1059 | total -= num_bytes; |
| 1060 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1061 | } |
| 1062 | return 0; |
| 1063 | } |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1064 | static int update_pinned_extents(struct btrfs_root *root, |
| 1065 | u64 bytenr, u64 num, int pin) |
| 1066 | { |
| 1067 | u64 len; |
| 1068 | struct btrfs_block_group_cache *cache; |
| 1069 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1070 | |
| 1071 | if (pin) { |
| 1072 | set_extent_dirty(&fs_info->pinned_extents, |
| 1073 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1074 | } else { |
| 1075 | clear_extent_dirty(&fs_info->pinned_extents, |
| 1076 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1077 | } |
| 1078 | while (num > 0) { |
| 1079 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 1080 | WARN_ON(!cache); |
| 1081 | len = min(num, cache->key.offset - |
| 1082 | (bytenr - cache->key.objectid)); |
| 1083 | if (pin) { |
| 1084 | cache->pinned += len; |
| 1085 | fs_info->total_pinned += len; |
| 1086 | } else { |
| 1087 | cache->pinned -= len; |
| 1088 | fs_info->total_pinned -= len; |
| 1089 | } |
| 1090 | bytenr += len; |
| 1091 | num -= len; |
| 1092 | } |
| 1093 | return 0; |
| 1094 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1095 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1096 | 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] | 1097 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1098 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1099 | u64 start; |
| 1100 | u64 end; |
| 1101 | struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1102 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1103 | |
| 1104 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1105 | ret = find_first_extent_bit(pinned_extents, last, |
| 1106 | &start, &end, EXTENT_DIRTY); |
| 1107 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1108 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1109 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 1110 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1111 | } |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 1116 | struct btrfs_root *root, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1117 | struct extent_map_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1118 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1119 | u64 start; |
| 1120 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1121 | int ret; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1122 | struct extent_map_tree *free_space_cache; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1123 | free_space_cache = &root->fs_info->free_space_cache; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1124 | |
| 1125 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1126 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 1127 | EXTENT_DIRTY); |
| 1128 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1129 | break; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1130 | update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1131 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
| 1132 | set_extent_dirty(free_space_cache, start, end, GFP_NOFS); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1133 | } |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1137 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
| 1138 | struct btrfs_root *extent_root) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1139 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1140 | u64 start; |
| 1141 | u64 end; |
| 1142 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1143 | struct extent_buffer *eb; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1144 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1145 | struct btrfs_key ins; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1146 | struct btrfs_disk_key first; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1147 | struct btrfs_extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1148 | int ret; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1149 | int level; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1150 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1151 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1152 | btrfs_set_stack_extent_refs(&extent_item, 1); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1153 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1154 | path = btrfs_alloc_path(); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1155 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1156 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1157 | ret = find_first_extent_bit(&info->extent_ins, 0, &start, |
| 1158 | &end, EXTENT_LOCKED); |
| 1159 | if (ret) |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1160 | break; |
| 1161 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1162 | ins.objectid = start; |
| 1163 | ins.offset = end + 1 - start; |
| 1164 | err = btrfs_insert_item(trans, extent_root, &ins, |
| 1165 | &extent_item, sizeof(extent_item)); |
| 1166 | clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, |
| 1167 | GFP_NOFS); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1168 | eb = read_tree_block(extent_root, ins.objectid, ins.offset); |
| 1169 | level = btrfs_header_level(eb); |
| 1170 | if (level == 0) { |
| 1171 | btrfs_item_key(eb, &first, 0); |
| 1172 | } else { |
| 1173 | btrfs_node_key(eb, &first, 0); |
| 1174 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1175 | err = btrfs_insert_extent_backref(trans, extent_root, path, |
| 1176 | start, extent_root->root_key.objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 1177 | 0, level, |
| 1178 | btrfs_disk_key_objectid(&first)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1179 | BUG_ON(err); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1180 | free_extent_buffer(eb); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1181 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1182 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1183 | return 0; |
| 1184 | } |
| 1185 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1186 | static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, |
| 1187 | int pending) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1188 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1189 | int err = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1190 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1191 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1192 | if (!pending) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1193 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1194 | if (buf) { |
| 1195 | if (btrfs_buffer_uptodate(buf)) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1196 | u64 transid = |
| 1197 | root->fs_info->running_transaction->transid; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame^] | 1198 | u64 header_transid = |
| 1199 | btrfs_header_generation(buf); |
| 1200 | if (header_transid == transid) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1201 | free_extent_buffer(buf); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1202 | return 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1203 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1204 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1205 | free_extent_buffer(buf); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1206 | } |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1207 | update_pinned_extents(root, bytenr, num_bytes, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1208 | } else { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1209 | set_extent_bits(&root->fs_info->pending_del, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1210 | bytenr, bytenr + num_bytes - 1, |
| 1211 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1212 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1213 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1217 | /* |
| 1218 | * remove an extent from the root, returns 0 on success |
| 1219 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1220 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1221 | *root, u64 bytenr, u64 num_bytes, |
| 1222 | u64 root_objectid, u64 ref_generation, |
| 1223 | u64 owner_objectid, u64 owner_offset, int pin, |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1224 | int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1225 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1226 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1227 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1228 | struct btrfs_fs_info *info = root->fs_info; |
| 1229 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1230 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1231 | int ret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1232 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1233 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1234 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1235 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1236 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1237 | key.offset = num_bytes; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1238 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1239 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1240 | if (!path) |
| 1241 | return -ENOMEM; |
| 1242 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1243 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1244 | bytenr, root_objectid, |
| 1245 | ref_generation, |
| 1246 | owner_objectid, owner_offset, 1); |
| 1247 | if (ret == 0) { |
| 1248 | ret = btrfs_del_item(trans, extent_root, path); |
| 1249 | } else { |
| 1250 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 1251 | WARN_ON(1); |
| 1252 | printk("Unable to find ref byte nr %Lu root %Lu " |
| 1253 | " gen %Lu owner %Lu offset %Lu\n", bytenr, |
| 1254 | root_objectid, ref_generation, owner_objectid, |
| 1255 | owner_offset); |
| 1256 | } |
| 1257 | btrfs_release_path(extent_root, path); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1258 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1259 | if (ret < 0) |
| 1260 | return ret; |
| 1261 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1262 | |
| 1263 | leaf = path->nodes[0]; |
| 1264 | ei = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1265 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1266 | refs = btrfs_extent_refs(leaf, ei); |
| 1267 | BUG_ON(refs == 0); |
| 1268 | refs -= 1; |
| 1269 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1270 | btrfs_mark_buffer_dirty(leaf); |
| 1271 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1272 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1273 | u64 super_used; |
| 1274 | u64 root_used; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1275 | |
| 1276 | if (pin) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1277 | ret = pin_down_bytes(root, bytenr, num_bytes, 0); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1278 | if (ret > 0) |
| 1279 | mark_free = 1; |
| 1280 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1281 | } |
| 1282 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1283 | /* block accounting for super block */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1284 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1285 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1286 | super_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1287 | |
| 1288 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1289 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1290 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1291 | root_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1292 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1293 | ret = btrfs_del_item(trans, extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1294 | if (ret) { |
| 1295 | return ret; |
| 1296 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1297 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1298 | mark_free, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1299 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1300 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1301 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1302 | finish_current_insert(trans, extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1307 | * find all the blocks marked as pending in the radix tree and remove |
| 1308 | * them from the extent map |
| 1309 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1310 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 1311 | btrfs_root *extent_root) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1312 | { |
| 1313 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1314 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1315 | u64 start; |
| 1316 | u64 end; |
| 1317 | struct extent_map_tree *pending_del; |
| 1318 | struct extent_map_tree *pinned_extents; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1319 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1320 | pending_del = &extent_root->fs_info->pending_del; |
| 1321 | pinned_extents = &extent_root->fs_info->pinned_extents; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1322 | |
| 1323 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1324 | ret = find_first_extent_bit(pending_del, 0, &start, &end, |
| 1325 | EXTENT_LOCKED); |
| 1326 | if (ret) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1327 | break; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1328 | update_pinned_extents(extent_root, start, end + 1 - start, 1); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1329 | clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, |
| 1330 | GFP_NOFS); |
| 1331 | ret = __free_extent(trans, extent_root, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1332 | start, end + 1 - start, |
| 1333 | extent_root->root_key.objectid, |
| 1334 | 0, 0, 0, 0, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1335 | if (ret) |
| 1336 | err = ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1337 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1338 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /* |
| 1342 | * remove an extent from the root, returns 0 on success |
| 1343 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1344 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1345 | *root, u64 bytenr, u64 num_bytes, |
| 1346 | u64 root_objectid, u64 ref_generation, |
| 1347 | u64 owner_objectid, u64 owner_offset, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1348 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1349 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1350 | int pending_ret; |
| 1351 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1352 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1353 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1354 | if (!root->ref_cows) |
| 1355 | ref_generation = 0; |
| 1356 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1357 | if (root == extent_root) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1358 | pin_down_bytes(root, bytenr, num_bytes, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1359 | return 0; |
| 1360 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1361 | ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid, |
| 1362 | ref_generation, owner_objectid, owner_offset, |
| 1363 | pin, pin == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1364 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1365 | return ret ? ret : pending_ret; |
| 1366 | } |
| 1367 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1368 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 1369 | { |
| 1370 | u64 mask = ((u64)root->stripesize - 1); |
| 1371 | u64 ret = (val + mask) & ~mask; |
| 1372 | return ret; |
| 1373 | } |
| 1374 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1375 | /* |
| 1376 | * walks the btree of allocated extents and find a hole of a given size. |
| 1377 | * The key ins is changed to record the hole: |
| 1378 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1379 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1380 | * ins->offset == number of blocks |
| 1381 | * Any available blocks before search_start are skipped. |
| 1382 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1383 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
| 1384 | struct btrfs_root *orig_root, |
| 1385 | u64 num_bytes, u64 empty_size, |
| 1386 | u64 search_start, u64 search_end, |
| 1387 | u64 hint_byte, struct btrfs_key *ins, |
| 1388 | u64 exclude_start, u64 exclude_nr, |
| 1389 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1390 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1391 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1392 | struct btrfs_key key; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1393 | u64 hole_size = 0; |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1394 | u64 aligned; |
| 1395 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1396 | int slot = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1397 | u64 last_byte = 0; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1398 | u64 orig_search_start = search_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1399 | int start_found; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1400 | struct extent_buffer *l; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1401 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1402 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1403 | u64 total_needed = num_bytes; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1404 | int level; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 1405 | struct btrfs_block_group_cache *block_group; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1406 | int full_scan = 0; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1407 | int wrapped = 0; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1408 | u64 cached_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1409 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1410 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 1411 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
| 1412 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1413 | level = btrfs_header_level(root->node); |
| 1414 | |
Chris Mason | 015a739c | 2007-11-26 16:15:16 -0800 | [diff] [blame] | 1415 | if (num_bytes >= 32 * 1024 * 1024 && hint_byte) { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 1416 | data = BTRFS_BLOCK_GROUP_MIXED; |
| 1417 | } |
| 1418 | |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1419 | if (search_end == (u64)-1) |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1420 | search_end = btrfs_super_total_bytes(&info->super_copy); |
| 1421 | if (hint_byte) { |
| 1422 | block_group = btrfs_lookup_block_group(info, hint_byte); |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1423 | if (!block_group) |
| 1424 | hint_byte = search_start; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1425 | block_group = btrfs_find_block_group(root, block_group, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1426 | hint_byte, data, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1427 | } else { |
| 1428 | block_group = btrfs_find_block_group(root, |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1429 | trans->block_group, |
| 1430 | search_start, data, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1431 | } |
| 1432 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1433 | total_needed += empty_size; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1434 | path = btrfs_alloc_path(); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1435 | check_failed: |
Chris Mason | 70b043f | 2007-12-13 09:02:46 -0500 | [diff] [blame] | 1436 | if (!block_group) { |
| 1437 | block_group = btrfs_lookup_block_group(info, search_start); |
| 1438 | if (!block_group) |
| 1439 | block_group = btrfs_lookup_block_group(info, |
| 1440 | orig_search_start); |
| 1441 | } |
Yan | 5e5745d | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 1442 | search_start = find_search_start(root, &block_group, search_start, |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 1443 | total_needed, data); |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1444 | search_start = stripe_align(root, search_start); |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1445 | cached_start = search_start; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1446 | btrfs_init_path(path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1447 | ins->objectid = search_start; |
| 1448 | ins->offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1449 | start_found = 0; |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1450 | path->reada = 2; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1451 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1452 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1453 | if (ret < 0) |
| 1454 | goto error; |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 1455 | ret = find_previous_extent(root, path); |
| 1456 | if (ret < 0) |
| 1457 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1458 | l = path->nodes[0]; |
| 1459 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1460 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1461 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1462 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1463 | if (slot >= btrfs_header_nritems(l)) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1464 | ret = btrfs_next_leaf(root, path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1465 | if (ret == 0) |
| 1466 | continue; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1467 | if (ret < 0) |
| 1468 | goto error; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1469 | |
| 1470 | search_start = max(search_start, |
| 1471 | block_group->key.objectid); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1472 | if (!start_found) { |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1473 | aligned = stripe_align(root, search_start); |
| 1474 | ins->objectid = aligned; |
| 1475 | if (aligned >= search_end) { |
| 1476 | ret = -ENOSPC; |
| 1477 | goto error; |
| 1478 | } |
| 1479 | ins->offset = search_end - aligned; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1480 | start_found = 1; |
| 1481 | goto check_pending; |
| 1482 | } |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1483 | ins->objectid = stripe_align(root, |
| 1484 | last_byte > search_start ? |
| 1485 | last_byte : search_start); |
| 1486 | if (search_end <= ins->objectid) { |
| 1487 | ret = -ENOSPC; |
| 1488 | goto error; |
| 1489 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1490 | ins->offset = search_end - ins->objectid; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1491 | BUG_ON(ins->objectid >= search_end); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1492 | goto check_pending; |
| 1493 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1494 | btrfs_item_key_to_cpu(l, &key, slot); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1495 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1496 | if (key.objectid >= search_start && key.objectid > last_byte && |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1497 | start_found) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1498 | if (last_byte < search_start) |
| 1499 | last_byte = search_start; |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1500 | aligned = stripe_align(root, last_byte); |
| 1501 | hole_size = key.objectid - aligned; |
| 1502 | if (key.objectid > aligned && hole_size >= num_bytes) { |
| 1503 | ins->objectid = aligned; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1504 | ins->offset = hole_size; |
| 1505 | goto check_pending; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 1506 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1507 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1508 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1509 | if (!start_found && btrfs_key_type(&key) == |
| 1510 | BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1511 | last_byte = key.objectid; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1512 | start_found = 1; |
| 1513 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1514 | goto next; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1515 | } |
| 1516 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1517 | |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 1518 | start_found = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1519 | last_byte = key.objectid + key.offset; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1520 | |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 1521 | if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED && |
| 1522 | last_byte >= block_group->key.objectid + |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1523 | block_group->key.offset) { |
| 1524 | btrfs_release_path(root, path); |
| 1525 | search_start = block_group->key.objectid + |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1526 | block_group->key.offset; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1527 | goto new_group; |
| 1528 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1529 | next: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1530 | path->slots[0]++; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 1531 | cond_resched(); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1532 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1533 | check_pending: |
| 1534 | /* we have to make sure we didn't find an extent that has already |
| 1535 | * been allocated by the map tree or the original allocation |
| 1536 | */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1537 | btrfs_release_path(root, path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1538 | BUG_ON(ins->objectid < search_start); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1539 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1540 | if (ins->objectid + num_bytes >= search_end) |
Chris Mason | cf67582 | 2007-09-17 11:00:51 -0400 | [diff] [blame] | 1541 | goto enospc; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 1542 | if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED && |
Yan | 5cf6642 | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 1543 | ins->objectid + num_bytes > block_group-> |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1544 | key.objectid + block_group->key.offset) { |
| 1545 | search_start = block_group->key.objectid + |
| 1546 | block_group->key.offset; |
| 1547 | goto new_group; |
| 1548 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1549 | if (test_range_bit(&info->extent_ins, ins->objectid, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1550 | ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) { |
| 1551 | search_start = ins->objectid + num_bytes; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1552 | goto new_group; |
| 1553 | } |
| 1554 | if (test_range_bit(&info->pinned_extents, ins->objectid, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1555 | ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) { |
| 1556 | search_start = ins->objectid + num_bytes; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1557 | goto new_group; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1558 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1559 | if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 1560 | ins->objectid < exclude_start + exclude_nr)) { |
| 1561 | search_start = exclude_start + exclude_nr; |
| 1562 | goto new_group; |
| 1563 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1564 | if (!data) { |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 1565 | block_group = btrfs_lookup_block_group(info, ins->objectid); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1566 | if (block_group) |
| 1567 | trans->block_group = block_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1568 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1569 | ins->offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1570 | btrfs_free_path(path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1571 | return 0; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1572 | |
| 1573 | new_group: |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1574 | if (search_start + num_bytes >= search_end) { |
Chris Mason | cf67582 | 2007-09-17 11:00:51 -0400 | [diff] [blame] | 1575 | enospc: |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1576 | search_start = orig_search_start; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1577 | if (full_scan) { |
| 1578 | ret = -ENOSPC; |
| 1579 | goto error; |
| 1580 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1581 | if (wrapped) { |
| 1582 | if (!full_scan) |
| 1583 | total_needed -= empty_size; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1584 | full_scan = 1; |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1585 | data = BTRFS_BLOCK_GROUP_MIXED; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1586 | } else |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1587 | wrapped = 1; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1588 | } |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 1589 | block_group = btrfs_lookup_block_group(info, search_start); |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1590 | cond_resched(); |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1591 | block_group = btrfs_find_block_group(root, block_group, |
| 1592 | search_start, data, 0); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1593 | goto check_failed; |
| 1594 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1595 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1596 | btrfs_release_path(root, path); |
| 1597 | btrfs_free_path(path); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1598 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1599 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1600 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1601 | * finds a free extent and does all the dirty work required for allocation |
| 1602 | * returns the key for the extent through ins, and a tree buffer for |
| 1603 | * the first block of the extent through buf. |
| 1604 | * |
| 1605 | * returns 0 if everything worked, non-zero otherwise. |
| 1606 | */ |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1607 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1608 | struct btrfs_root *root, |
| 1609 | u64 num_bytes, u64 root_objectid, u64 ref_generation, |
| 1610 | u64 owner, u64 owner_offset, |
| 1611 | u64 empty_size, u64 hint_byte, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 1612 | u64 search_end, struct btrfs_key *ins, int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1613 | { |
| 1614 | int ret; |
| 1615 | int pending_ret; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1616 | u64 super_used, root_used; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1617 | u64 search_start = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 1618 | u64 new_hint; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1619 | struct btrfs_fs_info *info = root->fs_info; |
| 1620 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1621 | struct btrfs_extent_item extent_item; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1622 | struct btrfs_path *path; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1623 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1624 | btrfs_set_stack_extent_refs(&extent_item, 1); |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 1625 | |
| 1626 | new_hint = max(hint_byte, root->fs_info->alloc_start); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 1627 | if (new_hint < btrfs_super_total_bytes(&info->super_copy)) |
| 1628 | hint_byte = new_hint; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 1629 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1630 | WARN_ON(num_bytes < root->sectorsize); |
| 1631 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 1632 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1633 | trans->alloc_exclude_start, |
| 1634 | trans->alloc_exclude_nr, data); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1635 | BUG_ON(ret); |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 1636 | if (ret) |
| 1637 | return ret; |
| 1638 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1639 | /* block accounting for super block */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1640 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1641 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1642 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1643 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1644 | root_used = btrfs_root_used(&root->root_item); |
| 1645 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1646 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1647 | clear_extent_dirty(&root->fs_info->free_space_cache, |
| 1648 | ins->objectid, ins->objectid + ins->offset - 1, |
| 1649 | GFP_NOFS); |
| 1650 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1651 | if (root == extent_root) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1652 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 1653 | ins->objectid + ins->offset - 1, |
| 1654 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1655 | WARN_ON(data == 1); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1656 | goto update_block; |
| 1657 | } |
| 1658 | |
| 1659 | WARN_ON(trans->alloc_exclude_nr); |
| 1660 | trans->alloc_exclude_start = ins->objectid; |
| 1661 | trans->alloc_exclude_nr = ins->offset; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1662 | ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, |
| 1663 | sizeof(extent_item)); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1664 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1665 | trans->alloc_exclude_start = 0; |
| 1666 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1667 | BUG_ON(ret); |
| 1668 | |
| 1669 | path = btrfs_alloc_path(); |
| 1670 | BUG_ON(!path); |
| 1671 | ret = btrfs_insert_extent_backref(trans, extent_root, path, |
| 1672 | ins->objectid, root_objectid, |
| 1673 | ref_generation, owner, owner_offset); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1674 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1675 | BUG_ON(ret); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1676 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1677 | finish_current_insert(trans, extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1678 | pending_ret = del_pending_extents(trans, extent_root); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1679 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1680 | if (ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1681 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1682 | } |
| 1683 | if (pending_ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1684 | return pending_ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1685 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1686 | |
| 1687 | update_block: |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1688 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0, |
| 1689 | data); |
Chris Mason | fabb568 | 2007-06-07 22:13:21 -0400 | [diff] [blame] | 1690 | BUG_ON(ret); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1691 | return 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | /* |
| 1695 | * helper function to allocate a block for a given tree |
| 1696 | * returns the tree buffer or NULL. |
| 1697 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1698 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1699 | struct btrfs_root *root, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1700 | u32 blocksize, |
| 1701 | u64 root_objectid, u64 hint, |
| 1702 | u64 empty_size) |
| 1703 | { |
| 1704 | u64 ref_generation; |
| 1705 | |
| 1706 | if (root->ref_cows) |
| 1707 | ref_generation = trans->transid; |
| 1708 | else |
| 1709 | ref_generation = 0; |
| 1710 | |
| 1711 | |
| 1712 | return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid, |
| 1713 | ref_generation, 0, 0, hint, empty_size); |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * helper function to allocate a block for a given tree |
| 1718 | * returns the tree buffer or NULL. |
| 1719 | */ |
| 1720 | struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
| 1721 | struct btrfs_root *root, |
| 1722 | u32 blocksize, |
| 1723 | u64 root_objectid, |
| 1724 | u64 ref_generation, |
| 1725 | u64 first_objectid, |
| 1726 | int level, |
| 1727 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1728 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1729 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1730 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1731 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1732 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1733 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1734 | ret = btrfs_alloc_extent(trans, root, blocksize, |
| 1735 | root_objectid, ref_generation, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 1736 | level, first_objectid, empty_size, hint, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1737 | (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1738 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1739 | BUG_ON(ret > 0); |
| 1740 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1741 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1742 | buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1743 | if (!buf) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1744 | btrfs_free_extent(trans, root, ins.objectid, blocksize, |
| 1745 | root->root_key.objectid, ref_generation, |
| 1746 | 0, 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1747 | return ERR_PTR(-ENOMEM); |
| 1748 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1749 | btrfs_set_buffer_uptodate(buf); |
| 1750 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 1751 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1752 | set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree, |
| 1753 | buf->start, buf->start + buf->len - 1, |
| 1754 | EXTENT_CSUM, GFP_NOFS); |
| 1755 | buf->flags |= EXTENT_CSUM; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1756 | btrfs_set_buffer_defrag(buf); |
Chris Mason | d3c2fdc | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1757 | trans->blocks_used++; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1758 | return buf; |
| 1759 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1760 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1761 | static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 1762 | struct btrfs_root *root, |
| 1763 | struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1764 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1765 | u64 leaf_owner; |
| 1766 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1767 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1768 | struct btrfs_file_extent_item *fi; |
| 1769 | int i; |
| 1770 | int nritems; |
| 1771 | int ret; |
| 1772 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1773 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 1774 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1775 | leaf_owner = btrfs_header_owner(leaf); |
| 1776 | leaf_generation = btrfs_header_generation(leaf); |
| 1777 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1778 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1779 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1780 | |
| 1781 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 1782 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1783 | continue; |
| 1784 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1785 | if (btrfs_file_extent_type(leaf, fi) == |
| 1786 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 1787 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1788 | /* |
| 1789 | * FIXME make sure to insert a trans record that |
| 1790 | * repeats the snapshot del on crash |
| 1791 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1792 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 1793 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 1794 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1795 | ret = btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1796 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
| 1797 | leaf_owner, leaf_generation, |
| 1798 | key.objectid, key.offset, 0); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1799 | BUG_ON(ret); |
| 1800 | } |
| 1801 | return 0; |
| 1802 | } |
| 1803 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1804 | static void noinline reada_walk_down(struct btrfs_root *root, |
| 1805 | struct extent_buffer *node) |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1806 | { |
| 1807 | int i; |
| 1808 | u32 nritems; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1809 | u64 bytenr; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1810 | int ret; |
| 1811 | u32 refs; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1812 | int level; |
| 1813 | u32 blocksize; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1814 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1815 | nritems = btrfs_header_nritems(node); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1816 | level = btrfs_header_level(node); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1817 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1818 | bytenr = btrfs_node_blockptr(node, i); |
| 1819 | blocksize = btrfs_level_size(root, level - 1); |
| 1820 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1821 | BUG_ON(ret); |
| 1822 | if (refs != 1) |
| 1823 | continue; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1824 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1825 | ret = readahead_tree_block(root, bytenr, blocksize); |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1826 | cond_resched(); |
| 1827 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1828 | if (ret) |
| 1829 | break; |
| 1830 | } |
| 1831 | } |
| 1832 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1833 | /* |
| 1834 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 1835 | * counts as it goes. |
| 1836 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1837 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
| 1838 | struct btrfs_root *root, |
| 1839 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1840 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1841 | u64 root_owner; |
| 1842 | u64 root_gen; |
| 1843 | u64 bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1844 | struct extent_buffer *next; |
| 1845 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1846 | struct extent_buffer *parent; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1847 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1848 | int ret; |
| 1849 | u32 refs; |
| 1850 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1851 | WARN_ON(*level < 0); |
| 1852 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1853 | ret = lookup_extent_ref(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1854 | path->nodes[*level]->start, |
| 1855 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1856 | BUG_ON(ret); |
| 1857 | if (refs > 1) |
| 1858 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1859 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1860 | /* |
| 1861 | * walk down to the last node level and free all the leaves |
| 1862 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1863 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1864 | WARN_ON(*level < 0); |
| 1865 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1866 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1867 | |
| 1868 | if (*level > 0 && path->slots[*level] == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1869 | reada_walk_down(root, cur); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1870 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1871 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1872 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1873 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1874 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1875 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1876 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1877 | if (*level == 0) { |
| 1878 | ret = drop_leaf_ref(trans, root, cur); |
| 1879 | BUG_ON(ret); |
| 1880 | break; |
| 1881 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1882 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 1883 | blocksize = btrfs_level_size(root, *level - 1); |
| 1884 | ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1885 | BUG_ON(ret); |
| 1886 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1887 | parent = path->nodes[*level]; |
| 1888 | root_owner = btrfs_header_owner(parent); |
| 1889 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1890 | path->slots[*level]++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1891 | ret = btrfs_free_extent(trans, root, bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1892 | blocksize, root_owner, |
| 1893 | root_gen, 0, 0, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1894 | BUG_ON(ret); |
| 1895 | continue; |
| 1896 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1897 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1898 | if (!next || !btrfs_buffer_uptodate(next)) { |
| 1899 | free_extent_buffer(next); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1900 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1901 | next = read_tree_block(root, bytenr, blocksize); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1902 | mutex_lock(&root->fs_info->fs_mutex); |
| 1903 | |
| 1904 | /* we dropped the lock, check one more time */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1905 | ret = lookup_extent_ref(trans, root, bytenr, |
| 1906 | blocksize, &refs); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1907 | BUG_ON(ret); |
| 1908 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1909 | parent = path->nodes[*level]; |
| 1910 | root_owner = btrfs_header_owner(parent); |
| 1911 | root_gen = btrfs_header_generation(parent); |
| 1912 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1913 | path->slots[*level]++; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1914 | free_extent_buffer(next); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1915 | ret = btrfs_free_extent(trans, root, bytenr, |
| 1916 | blocksize, |
| 1917 | root_owner, |
| 1918 | root_gen, 0, 0, 1); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1919 | BUG_ON(ret); |
| 1920 | continue; |
| 1921 | } |
| 1922 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1923 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1924 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1925 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1926 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1927 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1928 | path->slots[*level] = 0; |
| 1929 | } |
| 1930 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1931 | WARN_ON(*level < 0); |
| 1932 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1933 | |
| 1934 | if (path->nodes[*level] == root->node) { |
| 1935 | root_owner = root->root_key.objectid; |
| 1936 | parent = path->nodes[*level]; |
| 1937 | } else { |
| 1938 | parent = path->nodes[*level + 1]; |
| 1939 | root_owner = btrfs_header_owner(parent); |
| 1940 | } |
| 1941 | |
| 1942 | root_gen = btrfs_header_generation(parent); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1943 | ret = btrfs_free_extent(trans, root, path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1944 | path->nodes[*level]->len, |
| 1945 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1946 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1947 | path->nodes[*level] = NULL; |
| 1948 | *level += 1; |
| 1949 | BUG_ON(ret); |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1953 | /* |
| 1954 | * helper for dropping snapshots. This walks back up the tree in the path |
| 1955 | * to find the first node higher up where we haven't yet gone through |
| 1956 | * all the slots |
| 1957 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1958 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
| 1959 | struct btrfs_root *root, |
| 1960 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1961 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1962 | u64 root_owner; |
| 1963 | u64 root_gen; |
| 1964 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1965 | int i; |
| 1966 | int slot; |
| 1967 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1968 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1969 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1970 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1971 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 1972 | struct extent_buffer *node; |
| 1973 | struct btrfs_disk_key disk_key; |
| 1974 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1975 | path->slots[i]++; |
| 1976 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1977 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1978 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1979 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1980 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1981 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1982 | return 0; |
| 1983 | } else { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1984 | if (path->nodes[*level] == root->node) { |
| 1985 | root_owner = root->root_key.objectid; |
| 1986 | root_gen = |
| 1987 | btrfs_header_generation(path->nodes[*level]); |
| 1988 | } else { |
| 1989 | struct extent_buffer *node; |
| 1990 | node = path->nodes[*level + 1]; |
| 1991 | root_owner = btrfs_header_owner(node); |
| 1992 | root_gen = btrfs_header_generation(node); |
| 1993 | } |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1994 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1995 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1996 | path->nodes[*level]->len, |
| 1997 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1998 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1999 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2000 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2001 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | return 1; |
| 2005 | } |
| 2006 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2007 | /* |
| 2008 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2009 | * the tree freeing any blocks that have a ref count of zero after being |
| 2010 | * decremented. |
| 2011 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2012 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2013 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2014 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2015 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2016 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2017 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2018 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2019 | int i; |
| 2020 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2021 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2022 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2023 | path = btrfs_alloc_path(); |
| 2024 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2025 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2026 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2027 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2028 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 2029 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2030 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2031 | path->slots[level] = 0; |
| 2032 | } else { |
| 2033 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2034 | struct btrfs_disk_key found_key; |
| 2035 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2036 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2037 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2038 | level = root_item->drop_level; |
| 2039 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2040 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2041 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2042 | ret = wret; |
| 2043 | goto out; |
| 2044 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2045 | node = path->nodes[level]; |
| 2046 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 2047 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 2048 | sizeof(found_key))); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2049 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2050 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2051 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2052 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2053 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2054 | if (wret < 0) |
| 2055 | ret = wret; |
| 2056 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2057 | wret = walk_up_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2058 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2059 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2060 | if (wret < 0) |
| 2061 | ret = wret; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2062 | ret = -EAGAIN; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2063 | break; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2064 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2065 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2066 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2067 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 2068 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2069 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2070 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2071 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2072 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2073 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2074 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2075 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2076 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 2077 | { |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2078 | u64 start; |
| 2079 | u64 end; |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2080 | u64 ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2081 | int ret; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2082 | while(1) { |
| 2083 | ret = find_first_extent_bit(&info->block_group_cache, 0, |
| 2084 | &start, &end, (unsigned int)-1); |
| 2085 | if (ret) |
| 2086 | break; |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2087 | ret = get_state_private(&info->block_group_cache, start, &ptr); |
| 2088 | if (!ret) |
| 2089 | kfree((void *)(unsigned long)ptr); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2090 | clear_extent_bits(&info->block_group_cache, start, |
| 2091 | end, (unsigned int)-1, GFP_NOFS); |
| 2092 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2093 | while(1) { |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2094 | ret = find_first_extent_bit(&info->free_space_cache, 0, |
| 2095 | &start, &end, EXTENT_DIRTY); |
| 2096 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2097 | break; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2098 | clear_extent_dirty(&info->free_space_cache, start, |
| 2099 | end, GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2100 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2101 | return 0; |
| 2102 | } |
| 2103 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2104 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
| 2105 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2106 | { |
| 2107 | u64 page_start; |
| 2108 | u64 page_end; |
| 2109 | u64 delalloc_start; |
| 2110 | u64 existing_delalloc; |
| 2111 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2112 | unsigned long i; |
| 2113 | struct page *page; |
| 2114 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2115 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2116 | struct file_ra_state *ra; |
| 2117 | |
| 2118 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2119 | |
| 2120 | mutex_lock(&inode->i_mutex); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2121 | i = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2122 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 2123 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2124 | file_ra_state_init(ra, inode->i_mapping); |
| 2125 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index); |
| 2126 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2127 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2128 | for (; i <= last_index; i++) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2129 | page = grab_cache_page(inode->i_mapping, i); |
| 2130 | if (!page) |
| 2131 | goto out_unlock; |
| 2132 | if (!PageUptodate(page)) { |
| 2133 | btrfs_readpage(NULL, page); |
| 2134 | lock_page(page); |
| 2135 | if (!PageUptodate(page)) { |
| 2136 | unlock_page(page); |
| 2137 | page_cache_release(page); |
| 2138 | goto out_unlock; |
| 2139 | } |
| 2140 | } |
| 2141 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2142 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 2143 | |
| 2144 | lock_extent(em_tree, page_start, page_end, GFP_NOFS); |
| 2145 | |
| 2146 | delalloc_start = page_start; |
| 2147 | existing_delalloc = |
| 2148 | count_range_bits(&BTRFS_I(inode)->extent_tree, |
| 2149 | &delalloc_start, page_end, |
| 2150 | PAGE_CACHE_SIZE, EXTENT_DELALLOC); |
| 2151 | |
| 2152 | set_extent_delalloc(em_tree, page_start, |
| 2153 | page_end, GFP_NOFS); |
| 2154 | |
| 2155 | spin_lock(&root->fs_info->delalloc_lock); |
| 2156 | root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE - |
| 2157 | existing_delalloc; |
| 2158 | spin_unlock(&root->fs_info->delalloc_lock); |
| 2159 | |
| 2160 | unlock_extent(em_tree, page_start, page_end, GFP_NOFS); |
| 2161 | set_page_dirty(page); |
| 2162 | unlock_page(page); |
| 2163 | page_cache_release(page); |
| 2164 | } |
| 2165 | |
| 2166 | out_unlock: |
| 2167 | mutex_unlock(&inode->i_mutex); |
| 2168 | return 0; |
| 2169 | } |
| 2170 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2171 | /* |
| 2172 | * note, this releases the path |
| 2173 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2174 | static int noinline relocate_one_reference(struct btrfs_root *extent_root, |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2175 | struct btrfs_path *path, |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2176 | struct btrfs_key *extent_key) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2177 | { |
| 2178 | struct inode *inode; |
| 2179 | struct btrfs_root *found_root; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2180 | struct btrfs_key *root_location; |
| 2181 | struct btrfs_extent_ref *ref; |
| 2182 | u64 ref_root; |
| 2183 | u64 ref_gen; |
| 2184 | u64 ref_objectid; |
| 2185 | u64 ref_offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2186 | int ret; |
| 2187 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2188 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2189 | struct btrfs_extent_ref); |
| 2190 | ref_root = btrfs_ref_root(path->nodes[0], ref); |
| 2191 | ref_gen = btrfs_ref_generation(path->nodes[0], ref); |
| 2192 | ref_objectid = btrfs_ref_objectid(path->nodes[0], ref); |
| 2193 | ref_offset = btrfs_ref_offset(path->nodes[0], ref); |
| 2194 | btrfs_release_path(extent_root, path); |
| 2195 | |
| 2196 | root_location = kmalloc(sizeof(*root_location), GFP_NOFS); |
| 2197 | root_location->objectid = ref_root; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2198 | if (ref_gen == 0) |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2199 | root_location->offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2200 | else |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2201 | root_location->offset = (u64)-1; |
| 2202 | root_location->type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2203 | |
| 2204 | found_root = btrfs_read_fs_root_no_name(extent_root->fs_info, |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2205 | root_location); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2206 | BUG_ON(!found_root); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2207 | kfree(root_location); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2208 | |
| 2209 | if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 2210 | mutex_unlock(&extent_root->fs_info->fs_mutex); |
| 2211 | inode = btrfs_iget_locked(extent_root->fs_info->sb, |
| 2212 | ref_objectid, found_root); |
| 2213 | if (inode->i_state & I_NEW) { |
| 2214 | /* the inode and parent dir are two different roots */ |
| 2215 | BTRFS_I(inode)->root = found_root; |
| 2216 | BTRFS_I(inode)->location.objectid = ref_objectid; |
| 2217 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 2218 | BTRFS_I(inode)->location.offset = 0; |
| 2219 | btrfs_read_locked_inode(inode); |
| 2220 | unlock_new_inode(inode); |
| 2221 | |
| 2222 | } |
| 2223 | /* this can happen if the reference is not against |
| 2224 | * the latest version of the tree root |
| 2225 | */ |
| 2226 | if (is_bad_inode(inode)) { |
| 2227 | mutex_lock(&extent_root->fs_info->fs_mutex); |
| 2228 | goto out; |
| 2229 | } |
| 2230 | relocate_inode_pages(inode, ref_offset, extent_key->offset); |
| 2231 | /* FIXME, data=ordered will help get rid of this */ |
| 2232 | filemap_fdatawrite(inode->i_mapping); |
| 2233 | iput(inode); |
| 2234 | mutex_lock(&extent_root->fs_info->fs_mutex); |
| 2235 | } else { |
| 2236 | struct btrfs_trans_handle *trans; |
| 2237 | struct btrfs_key found_key; |
| 2238 | struct extent_buffer *eb; |
| 2239 | int level; |
| 2240 | int i; |
| 2241 | |
| 2242 | trans = btrfs_start_transaction(found_root, 1); |
| 2243 | eb = read_tree_block(found_root, extent_key->objectid, |
| 2244 | extent_key->offset); |
| 2245 | level = btrfs_header_level(eb); |
| 2246 | |
| 2247 | if (level == 0) |
| 2248 | btrfs_item_key_to_cpu(eb, &found_key, 0); |
| 2249 | else |
| 2250 | btrfs_node_key_to_cpu(eb, &found_key, 0); |
| 2251 | |
| 2252 | free_extent_buffer(eb); |
| 2253 | |
| 2254 | path->lowest_level = level; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 2255 | path->reada = 2; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2256 | ret = btrfs_search_slot(trans, found_root, &found_key, path, |
| 2257 | 0, 1); |
| 2258 | path->lowest_level = 0; |
| 2259 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2260 | if (!path->nodes[i]) |
| 2261 | break; |
| 2262 | free_extent_buffer(path->nodes[i]); |
| 2263 | path->nodes[i] = NULL; |
| 2264 | } |
| 2265 | btrfs_release_path(found_root, path); |
| 2266 | btrfs_end_transaction(trans, found_root); |
| 2267 | } |
| 2268 | |
| 2269 | out: |
| 2270 | return 0; |
| 2271 | } |
| 2272 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2273 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
| 2274 | struct btrfs_path *path, |
| 2275 | struct btrfs_key *extent_key) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2276 | { |
| 2277 | struct btrfs_key key; |
| 2278 | struct btrfs_key found_key; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2279 | struct extent_buffer *leaf; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2280 | u32 nritems; |
| 2281 | u32 item_size; |
| 2282 | int ret = 0; |
| 2283 | |
| 2284 | key.objectid = extent_key->objectid; |
| 2285 | key.type = BTRFS_EXTENT_REF_KEY; |
| 2286 | key.offset = 0; |
| 2287 | |
| 2288 | while(1) { |
| 2289 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2290 | |
| 2291 | BUG_ON(ret == 0); |
| 2292 | |
| 2293 | if (ret < 0) |
| 2294 | goto out; |
| 2295 | |
| 2296 | ret = 0; |
| 2297 | leaf = path->nodes[0]; |
| 2298 | nritems = btrfs_header_nritems(leaf); |
| 2299 | if (path->slots[0] == nritems) |
| 2300 | goto out; |
| 2301 | |
| 2302 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2303 | if (found_key.objectid != extent_key->objectid) |
| 2304 | break; |
| 2305 | |
| 2306 | if (found_key.type != BTRFS_EXTENT_REF_KEY) |
| 2307 | break; |
| 2308 | |
| 2309 | key.offset = found_key.offset + 1; |
| 2310 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2311 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2312 | ret = relocate_one_reference(extent_root, path, extent_key); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2313 | if (ret) |
| 2314 | goto out; |
| 2315 | } |
| 2316 | ret = 0; |
| 2317 | out: |
| 2318 | btrfs_release_path(extent_root, path); |
| 2319 | return ret; |
| 2320 | } |
| 2321 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2322 | int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size) |
| 2323 | { |
| 2324 | struct btrfs_trans_handle *trans; |
| 2325 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
| 2326 | struct btrfs_path *path; |
| 2327 | u64 cur_byte; |
| 2328 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2329 | struct btrfs_fs_info *info = root->fs_info; |
| 2330 | struct extent_map_tree *block_group_cache; |
| 2331 | struct btrfs_key key; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2332 | struct btrfs_key found_key; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2333 | struct extent_buffer *leaf; |
| 2334 | u32 nritems; |
| 2335 | int ret; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 2336 | int progress = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2337 | |
| 2338 | btrfs_set_super_total_bytes(&info->super_copy, new_size); |
| 2339 | block_group_cache = &info->block_group_cache; |
| 2340 | path = btrfs_alloc_path(); |
| 2341 | root = root->fs_info->extent_root; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 2342 | path->reada = 2; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2343 | |
| 2344 | again: |
| 2345 | total_found = 0; |
| 2346 | key.objectid = new_size; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2347 | key.offset = 0; |
| 2348 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2349 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2350 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2351 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2352 | if (ret < 0) |
| 2353 | goto out; |
| 2354 | |
| 2355 | ret = find_previous_extent(root, path); |
| 2356 | if (ret < 0) |
| 2357 | goto out; |
| 2358 | if (ret == 0) { |
| 2359 | leaf = path->nodes[0]; |
| 2360 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2361 | if (found_key.objectid + found_key.offset > new_size) { |
| 2362 | cur_byte = found_key.objectid; |
| 2363 | key.objectid = cur_byte; |
| 2364 | } |
| 2365 | } |
| 2366 | btrfs_release_path(root, path); |
| 2367 | |
| 2368 | while(1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2369 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2370 | if (ret < 0) |
| 2371 | goto out; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2372 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2373 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2374 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2375 | next: |
| 2376 | if (path->slots[0] >= nritems) { |
| 2377 | ret = btrfs_next_leaf(root, path); |
| 2378 | if (ret < 0) |
| 2379 | goto out; |
| 2380 | if (ret == 1) { |
| 2381 | ret = 0; |
| 2382 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2383 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2384 | leaf = path->nodes[0]; |
| 2385 | nritems = btrfs_header_nritems(leaf); |
| 2386 | } |
| 2387 | |
| 2388 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 2389 | |
| 2390 | if (progress && need_resched()) { |
| 2391 | memcpy(&key, &found_key, sizeof(key)); |
| 2392 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2393 | cond_resched(); |
| 2394 | mutex_lock(&root->fs_info->fs_mutex); |
| 2395 | btrfs_release_path(root, path); |
| 2396 | btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2397 | progress = 0; |
| 2398 | goto next; |
| 2399 | } |
| 2400 | progress = 1; |
| 2401 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2402 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY || |
| 2403 | found_key.objectid + found_key.offset <= cur_byte) { |
| 2404 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2405 | goto next; |
| 2406 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2407 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2408 | total_found++; |
| 2409 | cur_byte = found_key.objectid + found_key.offset; |
| 2410 | key.objectid = cur_byte; |
| 2411 | btrfs_release_path(root, path); |
| 2412 | ret = relocate_one_extent(root, path, &found_key); |
| 2413 | } |
| 2414 | |
| 2415 | btrfs_release_path(root, path); |
| 2416 | |
| 2417 | if (total_found > 0) { |
| 2418 | trans = btrfs_start_transaction(tree_root, 1); |
| 2419 | btrfs_commit_transaction(trans, tree_root); |
| 2420 | |
| 2421 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2422 | btrfs_clean_old_snapshots(tree_root); |
| 2423 | mutex_lock(&root->fs_info->fs_mutex); |
| 2424 | |
| 2425 | trans = btrfs_start_transaction(tree_root, 1); |
| 2426 | btrfs_commit_transaction(trans, tree_root); |
| 2427 | goto again; |
| 2428 | } |
| 2429 | |
| 2430 | trans = btrfs_start_transaction(root, 1); |
| 2431 | key.objectid = new_size; |
| 2432 | key.offset = 0; |
| 2433 | key.type = 0; |
| 2434 | while(1) { |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2435 | u64 ptr; |
| 2436 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2437 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2438 | if (ret < 0) |
| 2439 | goto out; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2440 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2441 | leaf = path->nodes[0]; |
| 2442 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2443 | bg_next: |
| 2444 | if (path->slots[0] >= nritems) { |
| 2445 | ret = btrfs_next_leaf(root, path); |
| 2446 | if (ret < 0) |
| 2447 | break; |
| 2448 | if (ret == 1) { |
| 2449 | ret = 0; |
| 2450 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2451 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2452 | leaf = path->nodes[0]; |
Chris Mason | 1372f8e | 2008-01-04 09:34:54 -0500 | [diff] [blame] | 2453 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2454 | |
| 2455 | /* |
| 2456 | * btrfs_next_leaf doesn't cow buffers, we have to |
| 2457 | * do the search again |
| 2458 | */ |
| 2459 | memcpy(&key, &found_key, sizeof(key)); |
| 2460 | btrfs_release_path(root, path); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 2461 | goto resched_check; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2465 | if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 2466 | printk("shrinker found key %Lu %u %Lu\n", |
| 2467 | found_key.objectid, found_key.type, |
| 2468 | found_key.offset); |
| 2469 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2470 | goto bg_next; |
| 2471 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2472 | ret = get_state_private(&info->block_group_cache, |
| 2473 | found_key.objectid, &ptr); |
| 2474 | if (!ret) |
| 2475 | kfree((void *)(unsigned long)ptr); |
| 2476 | |
| 2477 | clear_extent_bits(&info->block_group_cache, found_key.objectid, |
| 2478 | found_key.objectid + found_key.offset - 1, |
| 2479 | (unsigned int)-1, GFP_NOFS); |
| 2480 | |
| 2481 | key.objectid = found_key.objectid + 1; |
| 2482 | btrfs_del_item(trans, root, path); |
| 2483 | btrfs_release_path(root, path); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 2484 | resched_check: |
| 2485 | if (need_resched()) { |
| 2486 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2487 | cond_resched(); |
| 2488 | mutex_lock(&root->fs_info->fs_mutex); |
| 2489 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2490 | } |
| 2491 | clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1, |
| 2492 | GFP_NOFS); |
| 2493 | btrfs_commit_transaction(trans, root); |
| 2494 | out: |
| 2495 | btrfs_free_path(path); |
| 2496 | return ret; |
| 2497 | } |
| 2498 | |
| 2499 | int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans, |
| 2500 | struct btrfs_root *root, u64 new_size) |
| 2501 | { |
| 2502 | struct btrfs_path *path; |
| 2503 | u64 nr = 0; |
| 2504 | u64 cur_byte; |
| 2505 | u64 old_size; |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 2506 | unsigned long rem; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2507 | struct btrfs_block_group_cache *cache; |
| 2508 | struct btrfs_block_group_item *item; |
| 2509 | struct btrfs_fs_info *info = root->fs_info; |
| 2510 | struct extent_map_tree *block_group_cache; |
| 2511 | struct btrfs_key key; |
| 2512 | struct extent_buffer *leaf; |
| 2513 | int ret; |
| 2514 | int bit; |
| 2515 | |
| 2516 | old_size = btrfs_super_total_bytes(&info->super_copy); |
| 2517 | block_group_cache = &info->block_group_cache; |
| 2518 | |
| 2519 | root = info->extent_root; |
| 2520 | |
| 2521 | cache = btrfs_lookup_block_group(root->fs_info, old_size - 1); |
| 2522 | |
| 2523 | cur_byte = cache->key.objectid + cache->key.offset; |
| 2524 | if (cur_byte >= new_size) |
| 2525 | goto set_size; |
| 2526 | |
| 2527 | key.offset = BTRFS_BLOCK_GROUP_SIZE; |
| 2528 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
| 2529 | |
| 2530 | path = btrfs_alloc_path(); |
| 2531 | if (!path) |
| 2532 | return -ENOMEM; |
| 2533 | |
| 2534 | while(cur_byte < new_size) { |
| 2535 | key.objectid = cur_byte; |
| 2536 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 2537 | sizeof(struct btrfs_block_group_item)); |
| 2538 | BUG_ON(ret); |
| 2539 | leaf = path->nodes[0]; |
| 2540 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2541 | struct btrfs_block_group_item); |
| 2542 | |
| 2543 | btrfs_set_disk_block_group_used(leaf, item, 0); |
Chris Mason | f9ef660 | 2008-01-03 09:22:38 -0500 | [diff] [blame] | 2544 | div_long_long_rem(nr, 3, &rem); |
| 2545 | if (rem) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2546 | btrfs_set_disk_block_group_flags(leaf, item, |
| 2547 | BTRFS_BLOCK_GROUP_DATA); |
| 2548 | } else { |
| 2549 | btrfs_set_disk_block_group_flags(leaf, item, 0); |
| 2550 | } |
| 2551 | nr++; |
| 2552 | |
| 2553 | cache = kmalloc(sizeof(*cache), GFP_NOFS); |
| 2554 | BUG_ON(!cache); |
| 2555 | |
| 2556 | read_extent_buffer(leaf, &cache->item, (unsigned long)item, |
| 2557 | sizeof(cache->item)); |
| 2558 | |
| 2559 | memcpy(&cache->key, &key, sizeof(key)); |
| 2560 | cache->cached = 0; |
| 2561 | cache->pinned = 0; |
| 2562 | cur_byte = key.objectid + key.offset; |
| 2563 | btrfs_release_path(root, path); |
| 2564 | |
| 2565 | if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) { |
| 2566 | bit = BLOCK_GROUP_DATA; |
| 2567 | cache->data = BTRFS_BLOCK_GROUP_DATA; |
| 2568 | } else { |
| 2569 | bit = BLOCK_GROUP_METADATA; |
| 2570 | cache->data = 0; |
| 2571 | } |
| 2572 | |
| 2573 | /* use EXTENT_LOCKED to prevent merging */ |
| 2574 | set_extent_bits(block_group_cache, key.objectid, |
| 2575 | key.objectid + key.offset - 1, |
| 2576 | bit | EXTENT_LOCKED, GFP_NOFS); |
| 2577 | set_state_private(block_group_cache, key.objectid, |
| 2578 | (unsigned long)cache); |
| 2579 | } |
| 2580 | btrfs_free_path(path); |
| 2581 | set_size: |
| 2582 | btrfs_set_super_total_bytes(&info->super_copy, new_size); |
| 2583 | return 0; |
| 2584 | } |
| 2585 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2586 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 2587 | { |
| 2588 | struct btrfs_path *path; |
| 2589 | int ret; |
| 2590 | int err = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2591 | int bit; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2592 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2593 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2594 | struct extent_map_tree *block_group_cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2595 | struct btrfs_key key; |
| 2596 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2597 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2598 | |
| 2599 | block_group_cache = &info->block_group_cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2600 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2601 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2602 | key.objectid = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2603 | key.offset = BTRFS_BLOCK_GROUP_SIZE; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2604 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
| 2605 | |
| 2606 | path = btrfs_alloc_path(); |
| 2607 | if (!path) |
| 2608 | return -ENOMEM; |
| 2609 | |
| 2610 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2611 | ret = btrfs_search_slot(NULL, info->extent_root, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2612 | &key, path, 0, 0); |
| 2613 | if (ret != 0) { |
| 2614 | err = ret; |
| 2615 | break; |
| 2616 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2617 | leaf = path->nodes[0]; |
| 2618 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2619 | cache = kmalloc(sizeof(*cache), GFP_NOFS); |
| 2620 | if (!cache) { |
| 2621 | err = -1; |
| 2622 | break; |
| 2623 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 2624 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2625 | read_extent_buffer(leaf, &cache->item, |
| 2626 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2627 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2628 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2629 | cache->cached = 0; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2630 | cache->pinned = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2631 | key.objectid = found_key.objectid + found_key.offset; |
| 2632 | btrfs_release_path(root, path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2633 | |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 2634 | if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) { |
| 2635 | bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; |
| 2636 | cache->data = BTRFS_BLOCK_GROUP_MIXED; |
| 2637 | } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2638 | bit = BLOCK_GROUP_DATA; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 2639 | cache->data = BTRFS_BLOCK_GROUP_DATA; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2640 | } else { |
| 2641 | bit = BLOCK_GROUP_METADATA; |
| 2642 | cache->data = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 2643 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2644 | |
| 2645 | /* use EXTENT_LOCKED to prevent merging */ |
| 2646 | set_extent_bits(block_group_cache, found_key.objectid, |
| 2647 | found_key.objectid + found_key.offset - 1, |
| 2648 | bit | EXTENT_LOCKED, GFP_NOFS); |
| 2649 | set_state_private(block_group_cache, found_key.objectid, |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 2650 | (unsigned long)cache); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2651 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2652 | if (key.objectid >= |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2653 | btrfs_super_total_bytes(&info->super_copy)) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2654 | break; |
| 2655 | } |
| 2656 | |
| 2657 | btrfs_free_path(path); |
| 2658 | return 0; |
| 2659 | } |