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 | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 21 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 22 | #include "ctree.h" |
| 23 | #include "disk-io.h" |
| 24 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 25 | #include "transaction.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 26 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 27 | #define BLOCK_GROUP_DATA EXTENT_WRITEBACK |
| 28 | #define BLOCK_GROUP_METADATA EXTENT_UPTODATE |
| 29 | #define BLOCK_GROUP_DIRTY EXTENT_DIRTY |
| 30 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 31 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
| 32 | btrfs_root *extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 33 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 34 | btrfs_root *extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 36 | static int cache_block_group(struct btrfs_root *root, |
| 37 | struct btrfs_block_group_cache *block_group) |
| 38 | { |
| 39 | struct btrfs_path *path; |
| 40 | int ret; |
| 41 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 42 | struct extent_buffer *leaf; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 43 | struct extent_map_tree *free_space_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 44 | int slot; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 45 | u64 last = 0; |
| 46 | u64 hole_size; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 47 | u64 first_free; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 48 | int found = 0; |
| 49 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 50 | if (!block_group) |
| 51 | return 0; |
| 52 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 53 | root = root->fs_info->extent_root; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 54 | free_space_cache = &root->fs_info->free_space_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 55 | |
| 56 | if (block_group->cached) |
| 57 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 58 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 59 | path = btrfs_alloc_path(); |
| 60 | if (!path) |
| 61 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 62 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 63 | path->reada = 2; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 64 | first_free = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 65 | key.objectid = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 66 | key.offset = 0; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 67 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 68 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 69 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 70 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 71 | if (ret < 0) |
| 72 | return ret; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 73 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 74 | if (ret && path->slots[0] > 0) |
| 75 | path->slots[0]--; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 76 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 77 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 78 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 79 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 80 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 81 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 82 | if (ret < 0) |
| 83 | goto err; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 84 | if (ret == 0) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 85 | continue; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 86 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 90 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 91 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 92 | if (key.objectid < block_group->key.objectid) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 93 | if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY && |
| 94 | key.objectid + key.offset > first_free) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 95 | first_free = key.objectid + key.offset; |
| 96 | goto next; |
| 97 | } |
| 98 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 99 | if (key.objectid >= block_group->key.objectid + |
| 100 | block_group->key.offset) { |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | |
| 104 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
| 105 | if (!found) { |
| 106 | last = first_free; |
| 107 | found = 1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 108 | } |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 109 | if (key.objectid > last) { |
| 110 | hole_size = key.objectid - last; |
| 111 | set_extent_dirty(free_space_cache, last, |
| 112 | last + hole_size - 1, |
| 113 | GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 114 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 115 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 116 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 117 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 118 | path->slots[0]++; |
| 119 | } |
| 120 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 121 | if (!found) |
| 122 | last = first_free; |
| 123 | if (block_group->key.objectid + |
| 124 | block_group->key.offset > last) { |
| 125 | hole_size = block_group->key.objectid + |
| 126 | block_group->key.offset - last; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 127 | set_extent_dirty(free_space_cache, last, |
| 128 | last + hole_size - 1, GFP_NOFS); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 129 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 130 | block_group->cached = 1; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 131 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 132 | btrfs_free_path(path); |
| 133 | return 0; |
| 134 | } |
| 135 | |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 136 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
| 137 | btrfs_fs_info *info, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 138 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 139 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 140 | struct extent_map_tree *block_group_cache; |
| 141 | struct btrfs_block_group_cache *block_group = NULL; |
| 142 | u64 ptr; |
| 143 | u64 start; |
| 144 | u64 end; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 145 | int ret; |
| 146 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 147 | block_group_cache = &info->block_group_cache; |
| 148 | ret = find_first_extent_bit(block_group_cache, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 149 | bytenr, &start, &end, |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 150 | BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 151 | if (ret) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 152 | return NULL; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 153 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 154 | ret = get_state_private(block_group_cache, start, &ptr); |
| 155 | if (ret) |
| 156 | return NULL; |
| 157 | |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 158 | block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Yan | 5cf6642 | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 159 | if (block_group->key.objectid <= bytenr && bytenr < |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 160 | block_group->key.objectid + block_group->key.offset) |
| 161 | return block_group; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 162 | return NULL; |
| 163 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 164 | static u64 find_search_start(struct btrfs_root *root, |
| 165 | struct btrfs_block_group_cache **cache_ret, |
Yan | 5e5745d | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 166 | u64 search_start, int num, |
| 167 | int data, int full_scan) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 168 | { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 169 | int ret; |
| 170 | struct btrfs_block_group_cache *cache = *cache_ret; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 171 | u64 last; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 172 | u64 start = 0; |
| 173 | u64 end = 0; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 174 | u64 cache_miss = 0; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 175 | int wrapped = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 176 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 177 | if (!cache) { |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 178 | goto out; |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 179 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 180 | again: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 181 | ret = cache_block_group(root, cache); |
| 182 | if (ret) |
| 183 | goto out; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 184 | |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 185 | last = max(search_start, cache->key.objectid); |
| 186 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 187 | while(1) { |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 188 | ret = find_first_extent_bit(&root->fs_info->free_space_cache, |
| 189 | last, &start, &end, EXTENT_DIRTY); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 190 | if (ret) { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 191 | if (!cache_miss) |
| 192 | cache_miss = last; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 193 | goto new_group; |
| 194 | } |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 195 | |
| 196 | start = max(last, start); |
| 197 | last = end + 1; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 198 | if (last - start < num) { |
| 199 | if (last == cache->key.objectid + cache->key.offset) |
| 200 | cache_miss = start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 201 | continue; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 202 | } |
| 203 | if (data != BTRFS_BLOCK_GROUP_MIXED && |
Yan | 5cf6642 | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 204 | start + num > cache->key.objectid + cache->key.offset) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 205 | goto new_group; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 206 | return start; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 207 | } |
| 208 | out: |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 209 | cache = btrfs_lookup_block_group(root->fs_info, search_start); |
| 210 | if (!cache) { |
| 211 | printk("Unable to find block group for %Lu\n", |
| 212 | search_start); |
| 213 | WARN_ON(1); |
| 214 | return search_start; |
| 215 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 216 | return search_start; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 217 | |
| 218 | new_group: |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 219 | last = cache->key.objectid + cache->key.offset; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 220 | wrapped: |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 221 | cache = btrfs_lookup_block_group(root->fs_info, last); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 222 | if (!cache) { |
Chris Mason | 0e4de58 | 2007-11-26 10:55:49 -0500 | [diff] [blame] | 223 | no_cache: |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 224 | if (!wrapped) { |
| 225 | wrapped = 1; |
| 226 | last = search_start; |
| 227 | data = BTRFS_BLOCK_GROUP_MIXED; |
| 228 | goto wrapped; |
| 229 | } |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 230 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 231 | } |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 232 | if (cache_miss && !cache->cached) { |
| 233 | cache_block_group(root, cache); |
| 234 | last = cache_miss; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 235 | cache = btrfs_lookup_block_group(root->fs_info, last); |
| 236 | } |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 237 | cache = btrfs_find_block_group(root, cache, last, data, 0); |
Chris Mason | 0e4de58 | 2007-11-26 10:55:49 -0500 | [diff] [blame] | 238 | if (!cache) |
| 239 | goto no_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 240 | *cache_ret = cache; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 241 | cache_miss = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 242 | goto again; |
| 243 | } |
| 244 | |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 245 | static u64 div_factor(u64 num, int factor) |
| 246 | { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 247 | if (factor == 10) |
| 248 | return num; |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 249 | num *= factor; |
| 250 | do_div(num, 10); |
| 251 | return num; |
| 252 | } |
| 253 | |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 254 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 255 | struct btrfs_block_group_cache |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 256 | *hint, u64 search_start, |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 257 | int data, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 258 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 259 | struct btrfs_block_group_cache *cache; |
| 260 | struct extent_map_tree *block_group_cache; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 261 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 262 | struct btrfs_fs_info *info = root->fs_info; |
| 263 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 264 | u64 last = 0; |
| 265 | u64 hint_last; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 266 | u64 start; |
| 267 | u64 end; |
| 268 | u64 free_check; |
| 269 | u64 ptr; |
| 270 | int bit; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 271 | int ret; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 272 | int full_search = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 273 | int factor = 8; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 274 | int data_swap = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 275 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 276 | block_group_cache = &info->block_group_cache; |
| 277 | |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 278 | if (!owner) |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 279 | factor = 8; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 280 | |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 281 | if (data == BTRFS_BLOCK_GROUP_MIXED) { |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 282 | bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 283 | factor = 10; |
| 284 | } else if (data) |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 285 | bit = BLOCK_GROUP_DATA; |
| 286 | else |
| 287 | bit = BLOCK_GROUP_METADATA; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 288 | |
| 289 | if (search_start) { |
| 290 | struct btrfs_block_group_cache *shint; |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 291 | shint = btrfs_lookup_block_group(info, search_start); |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 292 | if (shint && (shint->data == data || |
| 293 | shint->data == BTRFS_BLOCK_GROUP_MIXED)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 294 | used = btrfs_block_group_used(&shint->item); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 295 | if (used + shint->pinned < |
| 296 | div_factor(shint->key.offset, factor)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 297 | return shint; |
| 298 | } |
| 299 | } |
| 300 | } |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 301 | if (hint && (hint->data == data || |
| 302 | hint->data == BTRFS_BLOCK_GROUP_MIXED)) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 303 | used = btrfs_block_group_used(&hint->item); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 304 | if (used + hint->pinned < |
| 305 | div_factor(hint->key.offset, factor)) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 306 | return hint; |
| 307 | } |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 308 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 309 | hint_last = last; |
| 310 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 311 | if (hint) |
| 312 | hint_last = max(hint->key.objectid, search_start); |
| 313 | else |
| 314 | hint_last = search_start; |
| 315 | |
| 316 | last = hint_last; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 317 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 318 | again: |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 319 | while(1) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 320 | ret = find_first_extent_bit(block_group_cache, last, |
| 321 | &start, &end, bit); |
| 322 | if (ret) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 323 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 324 | |
| 325 | ret = get_state_private(block_group_cache, start, &ptr); |
| 326 | if (ret) |
| 327 | break; |
| 328 | |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 329 | cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 330 | last = cache->key.objectid + cache->key.offset; |
| 331 | used = btrfs_block_group_used(&cache->item); |
| 332 | |
| 333 | if (full_search) |
| 334 | free_check = cache->key.offset; |
| 335 | else |
| 336 | free_check = div_factor(cache->key.offset, factor); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 337 | if (used + cache->pinned < free_check) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 338 | found_group = cache; |
| 339 | goto found; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 340 | } |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 341 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 342 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 343 | if (!full_search) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 344 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 345 | full_search = 1; |
| 346 | goto again; |
| 347 | } |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 348 | if (!data_swap) { |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 349 | data_swap = 1; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 350 | bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 351 | last = search_start; |
| 352 | goto again; |
| 353 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 354 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 355 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 358 | static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 359 | u64 owner, u64 owner_offset) |
| 360 | { |
| 361 | u32 high_crc = ~(u32)0; |
| 362 | u32 low_crc = ~(u32)0; |
| 363 | __le64 lenum; |
| 364 | |
| 365 | lenum = cpu_to_le64(root_objectid); |
| 366 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 367 | lenum = cpu_to_le64(ref_generation); |
| 368 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 369 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 370 | #if 0 |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 371 | lenum = cpu_to_le64(owner); |
| 372 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 373 | lenum = cpu_to_le64(owner_offset); |
| 374 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 375 | #endif |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 376 | return ((u64)high_crc << 32) | (u64)low_crc; |
| 377 | } |
| 378 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 379 | static int match_extent_ref(struct extent_buffer *leaf, |
| 380 | struct btrfs_extent_ref *disk_ref, |
| 381 | struct btrfs_extent_ref *cpu_ref) |
| 382 | { |
| 383 | int ret; |
| 384 | int len; |
| 385 | |
| 386 | if (cpu_ref->objectid) |
| 387 | len = sizeof(*cpu_ref); |
| 388 | else |
| 389 | len = 2 * sizeof(u64); |
| 390 | ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref, |
| 391 | len); |
| 392 | return ret == 0; |
| 393 | } |
| 394 | |
| 395 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 396 | struct btrfs_root *root, |
| 397 | struct btrfs_path *path, u64 bytenr, |
| 398 | u64 root_objectid, u64 ref_generation, |
| 399 | u64 owner, u64 owner_offset, int del) |
| 400 | { |
| 401 | u64 hash; |
| 402 | struct btrfs_key key; |
| 403 | struct btrfs_key found_key; |
| 404 | struct btrfs_extent_ref ref; |
| 405 | struct extent_buffer *leaf; |
| 406 | struct btrfs_extent_ref *disk_ref; |
| 407 | int ret; |
| 408 | int ret2; |
| 409 | |
| 410 | btrfs_set_stack_ref_root(&ref, root_objectid); |
| 411 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
| 412 | btrfs_set_stack_ref_objectid(&ref, owner); |
| 413 | btrfs_set_stack_ref_offset(&ref, owner_offset); |
| 414 | |
| 415 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
| 416 | owner_offset); |
| 417 | key.offset = hash; |
| 418 | key.objectid = bytenr; |
| 419 | key.type = BTRFS_EXTENT_REF_KEY; |
| 420 | |
| 421 | while (1) { |
| 422 | ret = btrfs_search_slot(trans, root, &key, path, |
| 423 | del ? -1 : 0, del); |
| 424 | if (ret < 0) |
| 425 | goto out; |
| 426 | leaf = path->nodes[0]; |
| 427 | if (ret != 0) { |
| 428 | u32 nritems = btrfs_header_nritems(leaf); |
| 429 | if (path->slots[0] >= nritems) { |
| 430 | ret2 = btrfs_next_leaf(root, path); |
| 431 | if (ret2) |
| 432 | goto out; |
| 433 | leaf = path->nodes[0]; |
| 434 | } |
| 435 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 436 | if (found_key.objectid != bytenr || |
| 437 | found_key.type != BTRFS_EXTENT_REF_KEY) |
| 438 | goto out; |
| 439 | key.offset = found_key.offset; |
| 440 | if (del) { |
| 441 | btrfs_release_path(root, path); |
| 442 | continue; |
| 443 | } |
| 444 | } |
| 445 | disk_ref = btrfs_item_ptr(path->nodes[0], |
| 446 | path->slots[0], |
| 447 | struct btrfs_extent_ref); |
| 448 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) { |
| 449 | ret = 0; |
| 450 | goto out; |
| 451 | } |
| 452 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 453 | key.offset = found_key.offset + 1; |
| 454 | btrfs_release_path(root, path); |
| 455 | } |
| 456 | out: |
| 457 | return ret; |
| 458 | } |
| 459 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 460 | /* |
| 461 | * Back reference rules. Back refs have three main goals: |
| 462 | * |
| 463 | * 1) differentiate between all holders of references to an extent so that |
| 464 | * when a reference is dropped we can make sure it was a valid reference |
| 465 | * before freeing the extent. |
| 466 | * |
| 467 | * 2) Provide enough information to quickly find the holders of an extent |
| 468 | * if we notice a given block is corrupted or bad. |
| 469 | * |
| 470 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 471 | * maintenance. This is actually the same as #2, but with a slightly |
| 472 | * different use case. |
| 473 | * |
| 474 | * File extents can be referenced by: |
| 475 | * |
| 476 | * - multiple snapshots, subvolumes, or different generations in one subvol |
| 477 | * - different files inside a single subvolume (in theory, not implemented yet) |
| 478 | * - different offsets inside a file (bookend extents in file.c) |
| 479 | * |
| 480 | * The extent ref structure has fields for: |
| 481 | * |
| 482 | * - Objectid of the subvolume root |
| 483 | * - Generation number of the tree holding the reference |
| 484 | * - objectid of the file holding the reference |
| 485 | * - offset in the file corresponding to the key holding the reference |
| 486 | * |
| 487 | * When a file extent is allocated the fields are filled in: |
| 488 | * (root_key.objectid, trans->transid, inode objectid, offset in file) |
| 489 | * |
| 490 | * When a leaf is cow'd new references are added for every file extent found |
| 491 | * in the leaf. It looks the same as the create case, but trans->transid |
| 492 | * will be different when the block is cow'd. |
| 493 | * |
| 494 | * (root_key.objectid, trans->transid, inode objectid, offset in file) |
| 495 | * |
| 496 | * When a file extent is removed either during snapshot deletion or file |
| 497 | * truncation, the corresponding back reference is found |
| 498 | * by searching for: |
| 499 | * |
| 500 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 501 | * inode objectid, offset in file) |
| 502 | * |
| 503 | * Btree extents can be referenced by: |
| 504 | * |
| 505 | * - Different subvolumes |
| 506 | * - Different generations of the same subvolume |
| 507 | * |
| 508 | * Storing sufficient information for a full reverse mapping of a btree |
| 509 | * block would require storing the lowest key of the block in the backref, |
| 510 | * and it would require updating that lowest key either before write out or |
| 511 | * every time it changed. Instead, the objectid of the lowest key is stored |
| 512 | * along with the level of the tree block. This provides a hint |
| 513 | * about where in the btree the block can be found. Searches through the |
| 514 | * btree only need to look for a pointer to that block, so they stop one |
| 515 | * level higher than the level recorded in the backref. |
| 516 | * |
| 517 | * Some btrees do not do reference counting on their extents. These |
| 518 | * include the extent tree and the tree of tree roots. Backrefs for these |
| 519 | * trees always have a generation of zero. |
| 520 | * |
| 521 | * When a tree block is created, back references are inserted: |
| 522 | * |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 523 | * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 524 | * |
| 525 | * When a tree block is cow'd in a reference counted root, |
| 526 | * new back references are added for all the blocks it points to. |
| 527 | * These are of the form (trans->transid will have increased since creation): |
| 528 | * |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 529 | * (root->root_key.objectid, trans->transid, level, lowest_key_objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 530 | * |
| 531 | * Because the lowest_key_objectid and the level are just hints |
| 532 | * they are not used when backrefs are deleted. When a backref is deleted: |
| 533 | * |
| 534 | * if backref was for a tree root: |
| 535 | * root_objectid = root->root_key.objectid |
| 536 | * else |
| 537 | * root_objectid = btrfs_header_owner(parent) |
| 538 | * |
| 539 | * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0) |
| 540 | * |
| 541 | * Back Reference Key hashing: |
| 542 | * |
| 543 | * Back references have four fields, each 64 bits long. Unfortunately, |
| 544 | * This is hashed into a single 64 bit number and placed into the key offset. |
| 545 | * The key objectid corresponds to the first byte in the extent, and the |
| 546 | * key type is set to BTRFS_EXTENT_REF_KEY |
| 547 | */ |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 548 | int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, |
| 549 | struct btrfs_root *root, |
| 550 | struct btrfs_path *path, u64 bytenr, |
| 551 | u64 root_objectid, u64 ref_generation, |
| 552 | u64 owner, u64 owner_offset) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 553 | { |
| 554 | u64 hash; |
| 555 | struct btrfs_key key; |
| 556 | struct btrfs_extent_ref ref; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 557 | struct btrfs_extent_ref *disk_ref; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 558 | int ret; |
| 559 | |
| 560 | btrfs_set_stack_ref_root(&ref, root_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 561 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 562 | btrfs_set_stack_ref_objectid(&ref, owner); |
| 563 | btrfs_set_stack_ref_offset(&ref, owner_offset); |
| 564 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 565 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
| 566 | owner_offset); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 567 | key.offset = hash; |
| 568 | key.objectid = bytenr; |
| 569 | key.type = BTRFS_EXTENT_REF_KEY; |
| 570 | |
| 571 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref)); |
| 572 | while (ret == -EEXIST) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 573 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 574 | struct btrfs_extent_ref); |
| 575 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) |
| 576 | goto out; |
| 577 | key.offset++; |
| 578 | btrfs_release_path(root, path); |
| 579 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 580 | sizeof(ref)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 581 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 582 | if (ret) |
| 583 | goto out; |
| 584 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 585 | struct btrfs_extent_ref); |
| 586 | write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref, |
| 587 | sizeof(ref)); |
| 588 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 589 | out: |
| 590 | btrfs_release_path(root, path); |
| 591 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 592 | } |
| 593 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 594 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 595 | struct btrfs_root *root, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 596 | u64 bytenr, u64 num_bytes, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 597 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 598 | u64 owner, u64 owner_offset) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 599 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 600 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 601 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 602 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 603 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 604 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 605 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 606 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 607 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 608 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 609 | if (!path) |
| 610 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 611 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 612 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 613 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 614 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 615 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 616 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 617 | if (ret < 0) |
| 618 | return ret; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 619 | if (ret != 0) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 620 | BUG(); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 621 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 622 | BUG_ON(ret != 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 623 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 624 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 625 | refs = btrfs_extent_refs(l, item); |
| 626 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 627 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 628 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 629 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 630 | |
| 631 | ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root, |
| 632 | path, bytenr, root_objectid, |
| 633 | ref_generation, owner, owner_offset); |
| 634 | BUG_ON(ret); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 635 | finish_current_insert(trans, root->fs_info->extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 636 | del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 637 | |
| 638 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 639 | return 0; |
| 640 | } |
| 641 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 642 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 643 | struct btrfs_root *root) |
| 644 | { |
| 645 | finish_current_insert(trans, root->fs_info->extent_root); |
| 646 | del_pending_extents(trans, root->fs_info->extent_root); |
| 647 | return 0; |
| 648 | } |
| 649 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 650 | static int lookup_extent_ref(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 651 | struct btrfs_root *root, u64 bytenr, |
| 652 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 653 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 654 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 655 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 656 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 657 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 658 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 659 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 660 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 661 | path = btrfs_alloc_path(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 662 | key.objectid = bytenr; |
| 663 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 664 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 665 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 666 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 667 | if (ret < 0) |
| 668 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 669 | if (ret != 0) { |
| 670 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 671 | printk("failed to find block number %Lu\n", bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 672 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 673 | } |
| 674 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 675 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 676 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 677 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 678 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 679 | return 0; |
| 680 | } |
| 681 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame^] | 682 | u32 btrfs_count_snapshots_in_path(struct btrfs_root *root, |
| 683 | struct btrfs_path *count_path, |
| 684 | u64 first_extent) |
| 685 | { |
| 686 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 687 | struct btrfs_path *path; |
| 688 | u64 bytenr; |
| 689 | u64 found_objectid; |
| 690 | u64 root_objectid = 0; |
| 691 | u32 total_count = 0; |
| 692 | u32 cur_count; |
| 693 | u32 refs; |
| 694 | u32 nritems; |
| 695 | int ret; |
| 696 | struct btrfs_key key; |
| 697 | struct btrfs_key found_key; |
| 698 | struct extent_buffer *l; |
| 699 | struct btrfs_extent_item *item; |
| 700 | struct btrfs_extent_ref *ref_item; |
| 701 | int level = -1; |
| 702 | |
| 703 | path = btrfs_alloc_path(); |
| 704 | again: |
| 705 | if (level == -1) |
| 706 | bytenr = first_extent; |
| 707 | else |
| 708 | bytenr = count_path->nodes[level]->start; |
| 709 | |
| 710 | cur_count = 0; |
| 711 | key.objectid = bytenr; |
| 712 | key.offset = 0; |
| 713 | |
| 714 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 715 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 716 | if (ret < 0) |
| 717 | goto out; |
| 718 | BUG_ON(ret == 0); |
| 719 | |
| 720 | l = path->nodes[0]; |
| 721 | btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); |
| 722 | |
| 723 | if (found_key.objectid != bytenr || |
| 724 | found_key.type != BTRFS_EXTENT_ITEM_KEY) { |
| 725 | goto out; |
| 726 | } |
| 727 | |
| 728 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
| 729 | refs = btrfs_extent_refs(l, item); |
| 730 | while (1) { |
| 731 | nritems = btrfs_header_nritems(l); |
| 732 | if (path->slots[0] >= nritems) { |
| 733 | ret = btrfs_next_leaf(extent_root, path); |
| 734 | if (ret == 0) |
| 735 | continue; |
| 736 | break; |
| 737 | } |
| 738 | btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); |
| 739 | if (found_key.objectid != bytenr) |
| 740 | break; |
| 741 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 742 | path->slots[0]++; |
| 743 | continue; |
| 744 | } |
| 745 | |
| 746 | cur_count++; |
| 747 | ref_item = btrfs_item_ptr(l, path->slots[0], |
| 748 | struct btrfs_extent_ref); |
| 749 | found_objectid = btrfs_ref_root(l, ref_item); |
| 750 | |
| 751 | if (found_objectid != root_objectid) |
| 752 | total_count++; |
| 753 | |
| 754 | if (total_count > 1) |
| 755 | goto out; |
| 756 | |
| 757 | if (root_objectid == 0) |
| 758 | root_objectid = found_objectid; |
| 759 | |
| 760 | path->slots[0]++; |
| 761 | } |
| 762 | if (cur_count == 0) { |
| 763 | total_count = 0; |
| 764 | goto out; |
| 765 | } |
| 766 | if (total_count > 1) |
| 767 | goto out; |
| 768 | if (level >= 0 && root->node == count_path->nodes[level]) |
| 769 | goto out; |
| 770 | level++; |
| 771 | btrfs_release_path(root, path); |
| 772 | goto again; |
| 773 | |
| 774 | out: |
| 775 | btrfs_free_path(path); |
| 776 | return total_count; |
| 777 | |
| 778 | } |
| 779 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 780 | int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 781 | struct btrfs_root *root, u64 owner_objectid) |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 782 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 783 | u64 generation; |
| 784 | u64 key_objectid; |
| 785 | u64 level; |
| 786 | u32 nritems; |
| 787 | struct btrfs_disk_key disk_key; |
| 788 | |
| 789 | level = btrfs_header_level(root->node); |
| 790 | generation = trans->transid; |
| 791 | nritems = btrfs_header_nritems(root->node); |
| 792 | if (nritems > 0) { |
| 793 | if (level == 0) |
| 794 | btrfs_item_key(root->node, &disk_key, 0); |
| 795 | else |
| 796 | btrfs_node_key(root->node, &disk_key, 0); |
| 797 | key_objectid = btrfs_disk_key_objectid(&disk_key); |
| 798 | } else { |
| 799 | key_objectid = 0; |
| 800 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 801 | return btrfs_inc_extent_ref(trans, root, root->node->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 802 | root->node->len, owner_objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 803 | generation, level, key_objectid); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 804 | } |
| 805 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 806 | 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] | 807 | struct extent_buffer *buf) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 808 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 809 | u64 bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 810 | u32 nritems; |
| 811 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 812 | struct btrfs_file_extent_item *fi; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 813 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 814 | int level; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 815 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 816 | int faili; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 817 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 818 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 819 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 820 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 821 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 822 | nritems = btrfs_header_nritems(buf); |
| 823 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 824 | if (level == 0) { |
| 825 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 826 | btrfs_item_key_to_cpu(buf, &key, i); |
| 827 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 828 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 829 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 830 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 831 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 832 | BTRFS_FILE_EXTENT_INLINE) |
| 833 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 834 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 835 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 836 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 837 | ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 838 | btrfs_file_extent_disk_num_bytes(buf, fi), |
| 839 | root->root_key.objectid, trans->transid, |
| 840 | key.objectid, key.offset); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 841 | if (ret) { |
| 842 | faili = i; |
| 843 | goto fail; |
| 844 | } |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 845 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 846 | bytenr = btrfs_node_blockptr(buf, i); |
Chris Mason | 6caab48 | 2007-12-13 09:48:07 -0500 | [diff] [blame] | 847 | btrfs_node_key_to_cpu(buf, &key, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 848 | ret = btrfs_inc_extent_ref(trans, root, bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 849 | btrfs_level_size(root, level - 1), |
| 850 | root->root_key.objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 851 | trans->transid, |
| 852 | level - 1, key.objectid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 853 | if (ret) { |
| 854 | faili = i; |
| 855 | goto fail; |
| 856 | } |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 857 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 858 | } |
| 859 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 860 | fail: |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 861 | WARN_ON(1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 862 | #if 0 |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 863 | for (i =0; i < faili; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 864 | if (level == 0) { |
| 865 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 866 | btrfs_item_key_to_cpu(buf, &key, i); |
| 867 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 868 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 869 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 870 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 871 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 872 | BTRFS_FILE_EXTENT_INLINE) |
| 873 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 874 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 875 | if (disk_bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 876 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 877 | err = btrfs_free_extent(trans, root, disk_bytenr, |
| 878 | btrfs_file_extent_disk_num_bytes(buf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 879 | fi), 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 880 | BUG_ON(err); |
| 881 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 882 | bytenr = btrfs_node_blockptr(buf, i); |
| 883 | err = btrfs_free_extent(trans, root, bytenr, |
| 884 | btrfs_level_size(root, level - 1), 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 885 | BUG_ON(err); |
| 886 | } |
| 887 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 888 | #endif |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 889 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 890 | } |
| 891 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 892 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 893 | struct btrfs_root *root, |
| 894 | struct btrfs_path *path, |
| 895 | struct btrfs_block_group_cache *cache) |
| 896 | { |
| 897 | int ret; |
| 898 | int pending_ret; |
| 899 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 900 | unsigned long bi; |
| 901 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 902 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 903 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 904 | if (ret < 0) |
| 905 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 906 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 907 | |
| 908 | leaf = path->nodes[0]; |
| 909 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 910 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 911 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 912 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 913 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 914 | finish_current_insert(trans, extent_root); |
| 915 | pending_ret = del_pending_extents(trans, extent_root); |
| 916 | if (ret) |
| 917 | return ret; |
| 918 | if (pending_ret) |
| 919 | return pending_ret; |
| 920 | return 0; |
| 921 | |
| 922 | } |
| 923 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 924 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 925 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 926 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 927 | struct extent_map_tree *block_group_cache; |
| 928 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 929 | int ret; |
| 930 | int err = 0; |
| 931 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 932 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 933 | u64 last = 0; |
| 934 | u64 start; |
| 935 | u64 end; |
| 936 | u64 ptr; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 937 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 938 | block_group_cache = &root->fs_info->block_group_cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 939 | path = btrfs_alloc_path(); |
| 940 | if (!path) |
| 941 | return -ENOMEM; |
| 942 | |
| 943 | while(1) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 944 | ret = find_first_extent_bit(block_group_cache, last, |
| 945 | &start, &end, BLOCK_GROUP_DIRTY); |
| 946 | if (ret) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 947 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 948 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 949 | last = end + 1; |
| 950 | ret = get_state_private(block_group_cache, start, &ptr); |
| 951 | if (ret) |
| 952 | break; |
| 953 | |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 954 | cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 955 | err = write_one_cache_group(trans, root, |
| 956 | path, cache); |
| 957 | /* |
| 958 | * if we fail to write the cache group, we want |
| 959 | * to keep it marked dirty in hopes that a later |
| 960 | * write will work |
| 961 | */ |
| 962 | if (err) { |
| 963 | werr = err; |
| 964 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 965 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 966 | clear_extent_bits(block_group_cache, start, end, |
| 967 | BLOCK_GROUP_DIRTY, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 968 | } |
| 969 | btrfs_free_path(path); |
| 970 | return werr; |
| 971 | } |
| 972 | |
| 973 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 974 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 975 | u64 bytenr, u64 num_bytes, int alloc, |
| 976 | int mark_free, int data) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 977 | { |
| 978 | struct btrfs_block_group_cache *cache; |
| 979 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 980 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 981 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 982 | u64 byte_in_group; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 983 | u64 start; |
| 984 | u64 end; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 985 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 986 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 987 | cache = btrfs_lookup_block_group(info, bytenr); |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 988 | if (!cache) { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 989 | return -1; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 990 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 991 | byte_in_group = bytenr - cache->key.objectid; |
| 992 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 993 | start = cache->key.objectid; |
| 994 | end = start + cache->key.offset - 1; |
| 995 | set_extent_bits(&info->block_group_cache, start, end, |
| 996 | BLOCK_GROUP_DIRTY, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 997 | |
| 998 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 999 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1000 | if (alloc) { |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1001 | if (cache->data != data && |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 1002 | old_val < (cache->key.offset >> 1)) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1003 | int bit_to_clear; |
| 1004 | int bit_to_set; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1005 | cache->data = data; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1006 | if (data) { |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1007 | bit_to_clear = BLOCK_GROUP_METADATA; |
| 1008 | bit_to_set = BLOCK_GROUP_DATA; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1009 | cache->item.flags &= |
| 1010 | ~BTRFS_BLOCK_GROUP_MIXED; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1011 | cache->item.flags |= |
| 1012 | BTRFS_BLOCK_GROUP_DATA; |
| 1013 | } else { |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 1014 | bit_to_clear = BLOCK_GROUP_DATA; |
| 1015 | bit_to_set = BLOCK_GROUP_METADATA; |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1016 | cache->item.flags &= |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1017 | ~BTRFS_BLOCK_GROUP_MIXED; |
| 1018 | cache->item.flags &= |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1019 | ~BTRFS_BLOCK_GROUP_DATA; |
| 1020 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1021 | clear_extent_bits(&info->block_group_cache, |
| 1022 | start, end, bit_to_clear, |
| 1023 | GFP_NOFS); |
| 1024 | set_extent_bits(&info->block_group_cache, |
| 1025 | start, end, bit_to_set, |
| 1026 | GFP_NOFS); |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1027 | } else if (cache->data != data && |
| 1028 | cache->data != BTRFS_BLOCK_GROUP_MIXED) { |
| 1029 | cache->data = BTRFS_BLOCK_GROUP_MIXED; |
| 1030 | set_extent_bits(&info->block_group_cache, |
| 1031 | start, end, |
| 1032 | BLOCK_GROUP_DATA | |
| 1033 | BLOCK_GROUP_METADATA, |
| 1034 | GFP_NOFS); |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1035 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1036 | old_val += num_bytes; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1037 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1038 | old_val -= num_bytes; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1039 | if (mark_free) { |
| 1040 | set_extent_dirty(&info->free_space_cache, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1041 | bytenr, bytenr + num_bytes - 1, |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1042 | GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1043 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1044 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1045 | btrfs_set_block_group_used(&cache->item, old_val); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1046 | total -= num_bytes; |
| 1047 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1048 | } |
| 1049 | return 0; |
| 1050 | } |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1051 | static int update_pinned_extents(struct btrfs_root *root, |
| 1052 | u64 bytenr, u64 num, int pin) |
| 1053 | { |
| 1054 | u64 len; |
| 1055 | struct btrfs_block_group_cache *cache; |
| 1056 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1057 | |
| 1058 | if (pin) { |
| 1059 | set_extent_dirty(&fs_info->pinned_extents, |
| 1060 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1061 | } else { |
| 1062 | clear_extent_dirty(&fs_info->pinned_extents, |
| 1063 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1064 | } |
| 1065 | while (num > 0) { |
| 1066 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 1067 | WARN_ON(!cache); |
| 1068 | len = min(num, cache->key.offset - |
| 1069 | (bytenr - cache->key.objectid)); |
| 1070 | if (pin) { |
| 1071 | cache->pinned += len; |
| 1072 | fs_info->total_pinned += len; |
| 1073 | } else { |
| 1074 | cache->pinned -= len; |
| 1075 | fs_info->total_pinned -= len; |
| 1076 | } |
| 1077 | bytenr += len; |
| 1078 | num -= len; |
| 1079 | } |
| 1080 | return 0; |
| 1081 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1082 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1083 | 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] | 1084 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1085 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1086 | u64 start; |
| 1087 | u64 end; |
| 1088 | struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1089 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1090 | |
| 1091 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1092 | ret = find_first_extent_bit(pinned_extents, last, |
| 1093 | &start, &end, EXTENT_DIRTY); |
| 1094 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1095 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1096 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 1097 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1098 | } |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 1103 | struct btrfs_root *root, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1104 | struct extent_map_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1105 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1106 | u64 start; |
| 1107 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1108 | int ret; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1109 | struct extent_map_tree *free_space_cache; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1110 | free_space_cache = &root->fs_info->free_space_cache; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1111 | |
| 1112 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1113 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 1114 | EXTENT_DIRTY); |
| 1115 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1116 | break; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1117 | update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1118 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
| 1119 | set_extent_dirty(free_space_cache, start, end, GFP_NOFS); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1120 | } |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1124 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
| 1125 | btrfs_root *extent_root) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1126 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1127 | u64 start; |
| 1128 | u64 end; |
| 1129 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1130 | struct extent_buffer *eb; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1131 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1132 | struct btrfs_key ins; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1133 | struct btrfs_disk_key first; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1134 | struct btrfs_extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1135 | int ret; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1136 | int level; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1137 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1138 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1139 | btrfs_set_stack_extent_refs(&extent_item, 1); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1140 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1141 | path = btrfs_alloc_path(); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1142 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1143 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1144 | ret = find_first_extent_bit(&info->extent_ins, 0, &start, |
| 1145 | &end, EXTENT_LOCKED); |
| 1146 | if (ret) |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1147 | break; |
| 1148 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1149 | ins.objectid = start; |
| 1150 | ins.offset = end + 1 - start; |
| 1151 | err = btrfs_insert_item(trans, extent_root, &ins, |
| 1152 | &extent_item, sizeof(extent_item)); |
| 1153 | clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, |
| 1154 | GFP_NOFS); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1155 | eb = read_tree_block(extent_root, ins.objectid, ins.offset); |
| 1156 | level = btrfs_header_level(eb); |
| 1157 | if (level == 0) { |
| 1158 | btrfs_item_key(eb, &first, 0); |
| 1159 | } else { |
| 1160 | btrfs_node_key(eb, &first, 0); |
| 1161 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1162 | err = btrfs_insert_extent_backref(trans, extent_root, path, |
| 1163 | start, extent_root->root_key.objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 1164 | 0, level, |
| 1165 | btrfs_disk_key_objectid(&first)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1166 | BUG_ON(err); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1167 | free_extent_buffer(eb); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1168 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1169 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1170 | return 0; |
| 1171 | } |
| 1172 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1173 | static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, |
| 1174 | int pending) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1175 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1176 | int err = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1177 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1178 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1179 | if (!pending) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1180 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1181 | if (buf) { |
| 1182 | if (btrfs_buffer_uptodate(buf)) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1183 | u64 transid = |
| 1184 | root->fs_info->running_transaction->transid; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1185 | if (btrfs_header_generation(buf) == transid) { |
| 1186 | free_extent_buffer(buf); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1187 | return 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1188 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1189 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1190 | free_extent_buffer(buf); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1191 | } |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1192 | update_pinned_extents(root, bytenr, num_bytes, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1193 | } else { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1194 | set_extent_bits(&root->fs_info->pending_del, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1195 | bytenr, bytenr + num_bytes - 1, |
| 1196 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1197 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1198 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1199 | return 0; |
| 1200 | } |
| 1201 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1202 | /* |
| 1203 | * remove an extent from the root, returns 0 on success |
| 1204 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1205 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1206 | *root, u64 bytenr, u64 num_bytes, |
| 1207 | u64 root_objectid, u64 ref_generation, |
| 1208 | u64 owner_objectid, u64 owner_offset, int pin, |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1209 | int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1210 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1211 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1212 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1213 | struct btrfs_fs_info *info = root->fs_info; |
| 1214 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1215 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1216 | int ret; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1217 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1218 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1219 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1220 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1221 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1222 | key.offset = num_bytes; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1223 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1224 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1225 | if (!path) |
| 1226 | return -ENOMEM; |
| 1227 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1228 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1229 | bytenr, root_objectid, |
| 1230 | ref_generation, |
| 1231 | owner_objectid, owner_offset, 1); |
| 1232 | if (ret == 0) { |
| 1233 | ret = btrfs_del_item(trans, extent_root, path); |
| 1234 | } else { |
| 1235 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 1236 | WARN_ON(1); |
| 1237 | printk("Unable to find ref byte nr %Lu root %Lu " |
| 1238 | " gen %Lu owner %Lu offset %Lu\n", bytenr, |
| 1239 | root_objectid, ref_generation, owner_objectid, |
| 1240 | owner_offset); |
| 1241 | } |
| 1242 | btrfs_release_path(extent_root, path); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1243 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1244 | if (ret < 0) |
| 1245 | return ret; |
| 1246 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1247 | |
| 1248 | leaf = path->nodes[0]; |
| 1249 | ei = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1250 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1251 | refs = btrfs_extent_refs(leaf, ei); |
| 1252 | BUG_ON(refs == 0); |
| 1253 | refs -= 1; |
| 1254 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1255 | btrfs_mark_buffer_dirty(leaf); |
| 1256 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1257 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1258 | u64 super_used; |
| 1259 | u64 root_used; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1260 | |
| 1261 | if (pin) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1262 | ret = pin_down_bytes(root, bytenr, num_bytes, 0); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1263 | if (ret > 0) |
| 1264 | mark_free = 1; |
| 1265 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1266 | } |
| 1267 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1268 | /* block accounting for super block */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1269 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1270 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1271 | super_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1272 | |
| 1273 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1274 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1275 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1276 | root_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1277 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1278 | ret = btrfs_del_item(trans, extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1279 | if (ret) { |
| 1280 | return ret; |
| 1281 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1282 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1283 | mark_free, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1284 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1285 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1286 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1287 | finish_current_insert(trans, extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1288 | return ret; |
| 1289 | } |
| 1290 | |
| 1291 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1292 | * find all the blocks marked as pending in the radix tree and remove |
| 1293 | * them from the extent map |
| 1294 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1295 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 1296 | btrfs_root *extent_root) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1297 | { |
| 1298 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1299 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1300 | u64 start; |
| 1301 | u64 end; |
| 1302 | struct extent_map_tree *pending_del; |
| 1303 | struct extent_map_tree *pinned_extents; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1304 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1305 | pending_del = &extent_root->fs_info->pending_del; |
| 1306 | pinned_extents = &extent_root->fs_info->pinned_extents; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1307 | |
| 1308 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1309 | ret = find_first_extent_bit(pending_del, 0, &start, &end, |
| 1310 | EXTENT_LOCKED); |
| 1311 | if (ret) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1312 | break; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1313 | update_pinned_extents(extent_root, start, end + 1 - start, 1); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1314 | clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, |
| 1315 | GFP_NOFS); |
| 1316 | ret = __free_extent(trans, extent_root, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1317 | start, end + 1 - start, |
| 1318 | extent_root->root_key.objectid, |
| 1319 | 0, 0, 0, 0, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1320 | if (ret) |
| 1321 | err = ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1322 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1323 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* |
| 1327 | * remove an extent from the root, returns 0 on success |
| 1328 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1329 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1330 | *root, u64 bytenr, u64 num_bytes, |
| 1331 | u64 root_objectid, u64 ref_generation, |
| 1332 | u64 owner_objectid, u64 owner_offset, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1333 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1334 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1335 | int pending_ret; |
| 1336 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1337 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1338 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1339 | if (!root->ref_cows) |
| 1340 | ref_generation = 0; |
| 1341 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1342 | if (root == extent_root) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1343 | pin_down_bytes(root, bytenr, num_bytes, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1344 | return 0; |
| 1345 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1346 | ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid, |
| 1347 | ref_generation, owner_objectid, owner_offset, |
| 1348 | pin, pin == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1349 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1350 | return ret ? ret : pending_ret; |
| 1351 | } |
| 1352 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1353 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 1354 | { |
| 1355 | u64 mask = ((u64)root->stripesize - 1); |
| 1356 | u64 ret = (val + mask) & ~mask; |
| 1357 | return ret; |
| 1358 | } |
| 1359 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1360 | /* |
| 1361 | * walks the btree of allocated extents and find a hole of a given size. |
| 1362 | * The key ins is changed to record the hole: |
| 1363 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1364 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1365 | * ins->offset == number of blocks |
| 1366 | * Any available blocks before search_start are skipped. |
| 1367 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1368 | static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1369 | *orig_root, u64 num_bytes, u64 empty_size, |
| 1370 | u64 search_start, u64 search_end, u64 hint_byte, |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 1371 | struct btrfs_key *ins, u64 exclude_start, |
| 1372 | u64 exclude_nr, int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1373 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1374 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1375 | struct btrfs_key key; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1376 | u64 hole_size = 0; |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1377 | u64 aligned; |
| 1378 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1379 | int slot = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1380 | u64 last_byte = 0; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1381 | u64 orig_search_start = search_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1382 | int start_found; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1383 | struct extent_buffer *l; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1384 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1385 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1386 | u64 total_needed = num_bytes; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1387 | int level; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 1388 | struct btrfs_block_group_cache *block_group; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1389 | int full_scan = 0; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1390 | int wrapped = 0; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1391 | u64 cached_start; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1392 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1393 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 1394 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
| 1395 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1396 | level = btrfs_header_level(root->node); |
| 1397 | |
Chris Mason | 015a739c | 2007-11-26 16:15:16 -0800 | [diff] [blame] | 1398 | if (num_bytes >= 32 * 1024 * 1024 && hint_byte) { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 1399 | data = BTRFS_BLOCK_GROUP_MIXED; |
| 1400 | } |
| 1401 | |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1402 | if (search_end == (u64)-1) |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1403 | search_end = btrfs_super_total_bytes(&info->super_copy); |
| 1404 | if (hint_byte) { |
| 1405 | block_group = btrfs_lookup_block_group(info, hint_byte); |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1406 | if (!block_group) |
| 1407 | hint_byte = search_start; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1408 | block_group = btrfs_find_block_group(root, block_group, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1409 | hint_byte, data, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1410 | } else { |
| 1411 | block_group = btrfs_find_block_group(root, |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1412 | trans->block_group, |
| 1413 | search_start, data, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1414 | } |
| 1415 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1416 | total_needed += empty_size; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1417 | path = btrfs_alloc_path(); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1418 | check_failed: |
Chris Mason | 70b043f | 2007-12-13 09:02:46 -0500 | [diff] [blame] | 1419 | if (!block_group) { |
| 1420 | block_group = btrfs_lookup_block_group(info, search_start); |
| 1421 | if (!block_group) |
| 1422 | block_group = btrfs_lookup_block_group(info, |
| 1423 | orig_search_start); |
| 1424 | } |
Yan | 5e5745d | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 1425 | search_start = find_search_start(root, &block_group, search_start, |
| 1426 | total_needed, data, full_scan); |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1427 | search_start = stripe_align(root, search_start); |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 1428 | cached_start = search_start; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1429 | btrfs_init_path(path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1430 | ins->objectid = search_start; |
| 1431 | ins->offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1432 | start_found = 0; |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1433 | path->reada = 2; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1434 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1435 | ret = btrfs_search_slot(trans, root, ins, path, 0, 0); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1436 | if (ret < 0) |
| 1437 | goto error; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 1438 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1439 | if (path->slots[0] > 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1440 | path->slots[0]--; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1441 | } |
| 1442 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1443 | l = path->nodes[0]; |
| 1444 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
| 1445 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1446 | /* |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1447 | * walk backwards to find the first extent item key |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1448 | */ |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1449 | while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) { |
| 1450 | if (path->slots[0] == 0) { |
| 1451 | ret = btrfs_prev_leaf(root, path); |
| 1452 | if (ret != 0) { |
| 1453 | ret = btrfs_search_slot(trans, root, ins, |
| 1454 | path, 0, 0); |
| 1455 | if (ret < 0) |
| 1456 | goto error; |
| 1457 | if (path->slots[0] > 0) |
| 1458 | path->slots[0]--; |
| 1459 | break; |
| 1460 | } |
| 1461 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1462 | path->slots[0]--; |
| 1463 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1464 | l = path->nodes[0]; |
| 1465 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1466 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1467 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1468 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1469 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1470 | if (slot >= btrfs_header_nritems(l)) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1471 | ret = btrfs_next_leaf(root, path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1472 | if (ret == 0) |
| 1473 | continue; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1474 | if (ret < 0) |
| 1475 | goto error; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1476 | |
| 1477 | search_start = max(search_start, |
| 1478 | block_group->key.objectid); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1479 | if (!start_found) { |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1480 | aligned = stripe_align(root, search_start); |
| 1481 | ins->objectid = aligned; |
| 1482 | if (aligned >= search_end) { |
| 1483 | ret = -ENOSPC; |
| 1484 | goto error; |
| 1485 | } |
| 1486 | ins->offset = search_end - aligned; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1487 | start_found = 1; |
| 1488 | goto check_pending; |
| 1489 | } |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1490 | ins->objectid = stripe_align(root, |
| 1491 | last_byte > search_start ? |
| 1492 | last_byte : search_start); |
| 1493 | if (search_end <= ins->objectid) { |
| 1494 | ret = -ENOSPC; |
| 1495 | goto error; |
| 1496 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1497 | ins->offset = search_end - ins->objectid; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1498 | BUG_ON(ins->objectid >= search_end); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1499 | goto check_pending; |
| 1500 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1501 | btrfs_item_key_to_cpu(l, &key, slot); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1502 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1503 | if (key.objectid >= search_start && key.objectid > last_byte && |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1504 | start_found) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1505 | if (last_byte < search_start) |
| 1506 | last_byte = search_start; |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1507 | aligned = stripe_align(root, last_byte); |
| 1508 | hole_size = key.objectid - aligned; |
| 1509 | if (key.objectid > aligned && hole_size >= num_bytes) { |
| 1510 | ins->objectid = aligned; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1511 | ins->offset = hole_size; |
| 1512 | goto check_pending; |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 1513 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1514 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1515 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1516 | if (!start_found && btrfs_key_type(&key) == |
| 1517 | BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1518 | last_byte = key.objectid; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1519 | start_found = 1; |
| 1520 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1521 | goto next; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1522 | } |
| 1523 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1524 | |
Chris Mason | 0579da4 | 2007-03-07 16:15:30 -0500 | [diff] [blame] | 1525 | start_found = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1526 | last_byte = key.objectid + key.offset; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1527 | |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 1528 | if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED && |
| 1529 | last_byte >= block_group->key.objectid + |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1530 | block_group->key.offset) { |
| 1531 | btrfs_release_path(root, path); |
| 1532 | search_start = block_group->key.objectid + |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1533 | block_group->key.offset; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1534 | goto new_group; |
| 1535 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1536 | next: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1537 | path->slots[0]++; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 1538 | cond_resched(); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1539 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1540 | check_pending: |
| 1541 | /* we have to make sure we didn't find an extent that has already |
| 1542 | * been allocated by the map tree or the original allocation |
| 1543 | */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1544 | btrfs_release_path(root, path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1545 | BUG_ON(ins->objectid < search_start); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1546 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1547 | if (ins->objectid + num_bytes >= search_end) |
Chris Mason | cf67582 | 2007-09-17 11:00:51 -0400 | [diff] [blame] | 1548 | goto enospc; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 1549 | if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED && |
Yan | 5cf6642 | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 1550 | ins->objectid + num_bytes > block_group-> |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1551 | key.objectid + block_group->key.offset) { |
| 1552 | search_start = block_group->key.objectid + |
| 1553 | block_group->key.offset; |
| 1554 | goto new_group; |
| 1555 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1556 | if (test_range_bit(&info->extent_ins, ins->objectid, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1557 | ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) { |
| 1558 | search_start = ins->objectid + num_bytes; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1559 | goto new_group; |
| 1560 | } |
| 1561 | if (test_range_bit(&info->pinned_extents, ins->objectid, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1562 | ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) { |
| 1563 | search_start = ins->objectid + num_bytes; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1564 | goto new_group; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1565 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1566 | if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 1567 | ins->objectid < exclude_start + exclude_nr)) { |
| 1568 | search_start = exclude_start + exclude_nr; |
| 1569 | goto new_group; |
| 1570 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1571 | if (!data) { |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 1572 | block_group = btrfs_lookup_block_group(info, ins->objectid); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1573 | if (block_group) |
| 1574 | trans->block_group = block_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1575 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1576 | ins->offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1577 | btrfs_free_path(path); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1578 | return 0; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1579 | |
| 1580 | new_group: |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1581 | if (search_start + num_bytes >= search_end) { |
Chris Mason | cf67582 | 2007-09-17 11:00:51 -0400 | [diff] [blame] | 1582 | enospc: |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1583 | search_start = orig_search_start; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1584 | if (full_scan) { |
| 1585 | ret = -ENOSPC; |
| 1586 | goto error; |
| 1587 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1588 | if (wrapped) { |
| 1589 | if (!full_scan) |
| 1590 | total_needed -= empty_size; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1591 | full_scan = 1; |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1592 | data = BTRFS_BLOCK_GROUP_MIXED; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1593 | } else |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1594 | wrapped = 1; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1595 | } |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 1596 | block_group = btrfs_lookup_block_group(info, search_start); |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1597 | cond_resched(); |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1598 | block_group = btrfs_find_block_group(root, block_group, |
| 1599 | search_start, data, 0); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1600 | goto check_failed; |
| 1601 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1602 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1603 | btrfs_release_path(root, path); |
| 1604 | btrfs_free_path(path); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1605 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1606 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1607 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1608 | * finds a free extent and does all the dirty work required for allocation |
| 1609 | * returns the key for the extent through ins, and a tree buffer for |
| 1610 | * the first block of the extent through buf. |
| 1611 | * |
| 1612 | * returns 0 if everything worked, non-zero otherwise. |
| 1613 | */ |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1614 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1615 | struct btrfs_root *root, |
| 1616 | u64 num_bytes, u64 root_objectid, u64 ref_generation, |
| 1617 | u64 owner, u64 owner_offset, |
| 1618 | u64 empty_size, u64 hint_byte, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 1619 | u64 search_end, struct btrfs_key *ins, int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1620 | { |
| 1621 | int ret; |
| 1622 | int pending_ret; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1623 | u64 super_used, root_used; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1624 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1625 | struct btrfs_fs_info *info = root->fs_info; |
| 1626 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1627 | struct btrfs_extent_item extent_item; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1628 | struct btrfs_path *path; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1629 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1630 | btrfs_set_stack_extent_refs(&extent_item, 1); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1631 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1632 | WARN_ON(num_bytes < root->sectorsize); |
| 1633 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 1634 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1635 | trans->alloc_exclude_start, |
| 1636 | trans->alloc_exclude_nr, data); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1637 | BUG_ON(ret); |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 1638 | if (ret) |
| 1639 | return ret; |
| 1640 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1641 | /* block accounting for super block */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1642 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1643 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1644 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1645 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1646 | root_used = btrfs_root_used(&root->root_item); |
| 1647 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1648 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1649 | clear_extent_dirty(&root->fs_info->free_space_cache, |
| 1650 | ins->objectid, ins->objectid + ins->offset - 1, |
| 1651 | GFP_NOFS); |
| 1652 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1653 | if (root == extent_root) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1654 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 1655 | ins->objectid + ins->offset - 1, |
| 1656 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1657 | WARN_ON(data == 1); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1658 | goto update_block; |
| 1659 | } |
| 1660 | |
| 1661 | WARN_ON(trans->alloc_exclude_nr); |
| 1662 | trans->alloc_exclude_start = ins->objectid; |
| 1663 | trans->alloc_exclude_nr = ins->offset; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1664 | ret = btrfs_insert_item(trans, extent_root, ins, &extent_item, |
| 1665 | sizeof(extent_item)); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1666 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1667 | trans->alloc_exclude_start = 0; |
| 1668 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1669 | BUG_ON(ret); |
| 1670 | |
| 1671 | path = btrfs_alloc_path(); |
| 1672 | BUG_ON(!path); |
| 1673 | ret = btrfs_insert_extent_backref(trans, extent_root, path, |
| 1674 | ins->objectid, root_objectid, |
| 1675 | ref_generation, owner, owner_offset); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1676 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1677 | BUG_ON(ret); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1678 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1679 | finish_current_insert(trans, extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1680 | pending_ret = del_pending_extents(trans, extent_root); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1681 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1682 | if (ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1683 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1684 | } |
| 1685 | if (pending_ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1686 | return pending_ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1687 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1688 | |
| 1689 | update_block: |
Chris Mason | 1e2677e | 2007-05-29 16:52:18 -0400 | [diff] [blame] | 1690 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0, |
| 1691 | data); |
Chris Mason | fabb568 | 2007-06-07 22:13:21 -0400 | [diff] [blame] | 1692 | BUG_ON(ret); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1693 | return 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * helper function to allocate a block for a given tree |
| 1698 | * returns the tree buffer or NULL. |
| 1699 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1700 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1701 | struct btrfs_root *root, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1702 | u32 blocksize, |
| 1703 | u64 root_objectid, u64 hint, |
| 1704 | u64 empty_size) |
| 1705 | { |
| 1706 | u64 ref_generation; |
| 1707 | |
| 1708 | if (root->ref_cows) |
| 1709 | ref_generation = trans->transid; |
| 1710 | else |
| 1711 | ref_generation = 0; |
| 1712 | |
| 1713 | |
| 1714 | return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid, |
| 1715 | ref_generation, 0, 0, hint, empty_size); |
| 1716 | } |
| 1717 | |
| 1718 | /* |
| 1719 | * helper function to allocate a block for a given tree |
| 1720 | * returns the tree buffer or NULL. |
| 1721 | */ |
| 1722 | struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
| 1723 | struct btrfs_root *root, |
| 1724 | u32 blocksize, |
| 1725 | u64 root_objectid, |
| 1726 | u64 ref_generation, |
| 1727 | u64 first_objectid, |
| 1728 | int level, |
| 1729 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1730 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1731 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1732 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1733 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1734 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1735 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1736 | ret = btrfs_alloc_extent(trans, root, blocksize, |
| 1737 | root_objectid, ref_generation, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 1738 | level, first_objectid, empty_size, hint, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1739 | (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1740 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1741 | BUG_ON(ret > 0); |
| 1742 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1743 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1744 | buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1745 | if (!buf) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1746 | btrfs_free_extent(trans, root, ins.objectid, blocksize, |
| 1747 | root->root_key.objectid, ref_generation, |
| 1748 | 0, 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1749 | return ERR_PTR(-ENOMEM); |
| 1750 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1751 | btrfs_set_buffer_uptodate(buf); |
| 1752 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 1753 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | 19c00dd | 2007-10-15 16:19:22 -0400 | [diff] [blame] | 1754 | set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree, |
| 1755 | buf->start, buf->start + buf->len - 1, |
| 1756 | EXTENT_CSUM, GFP_NOFS); |
| 1757 | buf->flags |= EXTENT_CSUM; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1758 | btrfs_set_buffer_defrag(buf); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1759 | trans->blocks_used++; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1760 | return buf; |
| 1761 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1762 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1763 | static int drop_leaf_ref(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1764 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1765 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1766 | u64 leaf_owner; |
| 1767 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1768 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1769 | struct btrfs_file_extent_item *fi; |
| 1770 | int i; |
| 1771 | int nritems; |
| 1772 | int ret; |
| 1773 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1774 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 1775 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1776 | leaf_owner = btrfs_header_owner(leaf); |
| 1777 | leaf_generation = btrfs_header_generation(leaf); |
| 1778 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1779 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1780 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1781 | |
| 1782 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 1783 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1784 | continue; |
| 1785 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1786 | if (btrfs_file_extent_type(leaf, fi) == |
| 1787 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 1788 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1789 | /* |
| 1790 | * FIXME make sure to insert a trans record that |
| 1791 | * repeats the snapshot del on crash |
| 1792 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1793 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 1794 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 1795 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1796 | ret = btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1797 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
| 1798 | leaf_owner, leaf_generation, |
| 1799 | key.objectid, key.offset, 0); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1800 | BUG_ON(ret); |
| 1801 | } |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1805 | static void reada_walk_down(struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1806 | struct extent_buffer *node) |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1807 | { |
| 1808 | int i; |
| 1809 | u32 nritems; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1810 | u64 bytenr; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1811 | int ret; |
| 1812 | u32 refs; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1813 | int level; |
| 1814 | u32 blocksize; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1815 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1816 | nritems = btrfs_header_nritems(node); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1817 | level = btrfs_header_level(node); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1818 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1819 | bytenr = btrfs_node_blockptr(node, i); |
| 1820 | blocksize = btrfs_level_size(root, level - 1); |
| 1821 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1822 | BUG_ON(ret); |
| 1823 | if (refs != 1) |
| 1824 | continue; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1825 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1826 | ret = readahead_tree_block(root, bytenr, blocksize); |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1827 | cond_resched(); |
| 1828 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1829 | if (ret) |
| 1830 | break; |
| 1831 | } |
| 1832 | } |
| 1833 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1834 | /* |
| 1835 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 1836 | * counts as it goes. |
| 1837 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1838 | static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1839 | *root, 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 | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1958 | static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root |
| 1959 | *root, struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1960 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1961 | u64 root_owner; |
| 1962 | u64 root_gen; |
| 1963 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1964 | int i; |
| 1965 | int slot; |
| 1966 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1967 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1968 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1969 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1970 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 1971 | struct extent_buffer *node; |
| 1972 | struct btrfs_disk_key disk_key; |
| 1973 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1974 | path->slots[i]++; |
| 1975 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1976 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1977 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1978 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1979 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 1980 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 1981 | return 0; |
| 1982 | } else { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1983 | if (path->nodes[*level] == root->node) { |
| 1984 | root_owner = root->root_key.objectid; |
| 1985 | root_gen = |
| 1986 | btrfs_header_generation(path->nodes[*level]); |
| 1987 | } else { |
| 1988 | struct extent_buffer *node; |
| 1989 | node = path->nodes[*level + 1]; |
| 1990 | root_owner = btrfs_header_owner(node); |
| 1991 | root_gen = btrfs_header_generation(node); |
| 1992 | } |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1993 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1994 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1995 | path->nodes[*level]->len, |
| 1996 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1997 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1998 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 1999 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2000 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2001 | } |
| 2002 | } |
| 2003 | return 1; |
| 2004 | } |
| 2005 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2006 | /* |
| 2007 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2008 | * the tree freeing any blocks that have a ref count of zero after being |
| 2009 | * decremented. |
| 2010 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2011 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2012 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2013 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2014 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2015 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2016 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2017 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2018 | int i; |
| 2019 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2020 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2021 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2022 | path = btrfs_alloc_path(); |
| 2023 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2024 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2025 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2026 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2027 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 2028 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2029 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2030 | path->slots[level] = 0; |
| 2031 | } else { |
| 2032 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2033 | struct btrfs_disk_key found_key; |
| 2034 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2035 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2036 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2037 | level = root_item->drop_level; |
| 2038 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2039 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2040 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2041 | ret = wret; |
| 2042 | goto out; |
| 2043 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2044 | node = path->nodes[level]; |
| 2045 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 2046 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 2047 | sizeof(found_key))); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2048 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2049 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2050 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2051 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2052 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2053 | if (wret < 0) |
| 2054 | ret = wret; |
| 2055 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2056 | wret = walk_up_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2057 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2058 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2059 | if (wret < 0) |
| 2060 | ret = wret; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2061 | ret = -EAGAIN; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2062 | break; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2063 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2064 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2065 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2066 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 2067 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2068 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2069 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2070 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2071 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2072 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2073 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2074 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2075 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 2076 | { |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2077 | u64 start; |
| 2078 | u64 end; |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2079 | u64 ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2080 | int ret; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2081 | while(1) { |
| 2082 | ret = find_first_extent_bit(&info->block_group_cache, 0, |
| 2083 | &start, &end, (unsigned int)-1); |
| 2084 | if (ret) |
| 2085 | break; |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2086 | ret = get_state_private(&info->block_group_cache, start, &ptr); |
| 2087 | if (!ret) |
| 2088 | kfree((void *)(unsigned long)ptr); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2089 | clear_extent_bits(&info->block_group_cache, start, |
| 2090 | end, (unsigned int)-1, GFP_NOFS); |
| 2091 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2092 | while(1) { |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2093 | ret = find_first_extent_bit(&info->free_space_cache, 0, |
| 2094 | &start, &end, EXTENT_DIRTY); |
| 2095 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2096 | break; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2097 | clear_extent_dirty(&info->free_space_cache, start, |
| 2098 | end, GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2099 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2100 | return 0; |
| 2101 | } |
| 2102 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2103 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 2104 | { |
| 2105 | struct btrfs_path *path; |
| 2106 | int ret; |
| 2107 | int err = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2108 | int bit; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2109 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2110 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2111 | struct extent_map_tree *block_group_cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2112 | struct btrfs_key key; |
| 2113 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2114 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2115 | |
| 2116 | block_group_cache = &info->block_group_cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2117 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2118 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2119 | key.objectid = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2120 | key.offset = BTRFS_BLOCK_GROUP_SIZE; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2121 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
| 2122 | |
| 2123 | path = btrfs_alloc_path(); |
| 2124 | if (!path) |
| 2125 | return -ENOMEM; |
| 2126 | |
| 2127 | while(1) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2128 | ret = btrfs_search_slot(NULL, info->extent_root, |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2129 | &key, path, 0, 0); |
| 2130 | if (ret != 0) { |
| 2131 | err = ret; |
| 2132 | break; |
| 2133 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2134 | leaf = path->nodes[0]; |
| 2135 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2136 | cache = kmalloc(sizeof(*cache), GFP_NOFS); |
| 2137 | if (!cache) { |
| 2138 | err = -1; |
| 2139 | break; |
| 2140 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 2141 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2142 | read_extent_buffer(leaf, &cache->item, |
| 2143 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2144 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2145 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2146 | cache->cached = 0; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2147 | cache->pinned = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2148 | key.objectid = found_key.objectid + found_key.offset; |
| 2149 | btrfs_release_path(root, path); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2150 | |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 2151 | if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) { |
| 2152 | bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA; |
| 2153 | cache->data = BTRFS_BLOCK_GROUP_MIXED; |
| 2154 | } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2155 | bit = BLOCK_GROUP_DATA; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 2156 | cache->data = BTRFS_BLOCK_GROUP_DATA; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2157 | } else { |
| 2158 | bit = BLOCK_GROUP_METADATA; |
| 2159 | cache->data = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 2160 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2161 | |
| 2162 | /* use EXTENT_LOCKED to prevent merging */ |
| 2163 | set_extent_bits(block_group_cache, found_key.objectid, |
| 2164 | found_key.objectid + found_key.offset - 1, |
| 2165 | bit | EXTENT_LOCKED, GFP_NOFS); |
| 2166 | set_state_private(block_group_cache, found_key.objectid, |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 2167 | (unsigned long)cache); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2168 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2169 | if (key.objectid >= |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2170 | btrfs_super_total_bytes(&info->super_copy)) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2171 | break; |
| 2172 | } |
| 2173 | |
| 2174 | btrfs_free_path(path); |
| 2175 | return 0; |
| 2176 | } |