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