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 | */ |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 20 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 22 | #include "hash.h" |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 23 | #include "crc32c.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 24 | #include "ctree.h" |
| 25 | #include "disk-io.h" |
| 26 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 27 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 28 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 29 | #include "locking.h" |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 30 | #include "ref-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 31 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 32 | #define PENDING_EXTENT_INSERT 0 |
| 33 | #define PENDING_EXTENT_DELETE 1 |
| 34 | #define PENDING_BACKREF_UPDATE 2 |
| 35 | |
| 36 | struct pending_extent_op { |
| 37 | int type; |
| 38 | u64 bytenr; |
| 39 | u64 num_bytes; |
| 40 | u64 parent; |
| 41 | u64 orig_parent; |
| 42 | u64 generation; |
| 43 | u64 orig_generation; |
| 44 | int level; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 45 | struct list_head list; |
| 46 | int del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 47 | }; |
| 48 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 49 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 50 | btrfs_root *extent_root, int all); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 51 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 52 | btrfs_root *extent_root, int all); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 53 | static struct btrfs_block_group_cache * |
| 54 | __btrfs_find_block_group(struct btrfs_root *root, |
| 55 | struct btrfs_block_group_cache *hint, |
| 56 | u64 search_start, int data, int owner); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 57 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 58 | struct btrfs_root *root, |
| 59 | u64 bytenr, u64 num_bytes, int is_data); |
| 60 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 61 | struct btrfs_root *root, |
| 62 | u64 bytenr, u64 num_bytes, int alloc, |
| 63 | int mark_free); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 64 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 65 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 66 | { |
| 67 | return (cache->flags & bits) == bits; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * this adds the block group to the fs_info rb tree for the block group |
| 72 | * cache |
| 73 | */ |
| 74 | int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
| 75 | struct btrfs_block_group_cache *block_group) |
| 76 | { |
| 77 | struct rb_node **p; |
| 78 | struct rb_node *parent = NULL; |
| 79 | struct btrfs_block_group_cache *cache; |
| 80 | |
| 81 | spin_lock(&info->block_group_cache_lock); |
| 82 | p = &info->block_group_cache_tree.rb_node; |
| 83 | |
| 84 | while (*p) { |
| 85 | parent = *p; |
| 86 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 87 | cache_node); |
| 88 | if (block_group->key.objectid < cache->key.objectid) { |
| 89 | p = &(*p)->rb_left; |
| 90 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 91 | p = &(*p)->rb_right; |
| 92 | } else { |
| 93 | spin_unlock(&info->block_group_cache_lock); |
| 94 | return -EEXIST; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | rb_link_node(&block_group->cache_node, parent, p); |
| 99 | rb_insert_color(&block_group->cache_node, |
| 100 | &info->block_group_cache_tree); |
| 101 | spin_unlock(&info->block_group_cache_lock); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * This will return the block group at or after bytenr if contains is 0, else |
| 108 | * it will return the block group that contains the bytenr |
| 109 | */ |
| 110 | static struct btrfs_block_group_cache * |
| 111 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 112 | int contains) |
| 113 | { |
| 114 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 115 | struct rb_node *n; |
| 116 | u64 end, start; |
| 117 | |
| 118 | spin_lock(&info->block_group_cache_lock); |
| 119 | n = info->block_group_cache_tree.rb_node; |
| 120 | |
| 121 | while (n) { |
| 122 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 123 | cache_node); |
| 124 | end = cache->key.objectid + cache->key.offset - 1; |
| 125 | start = cache->key.objectid; |
| 126 | |
| 127 | if (bytenr < start) { |
| 128 | if (!contains && (!ret || start < ret->key.objectid)) |
| 129 | ret = cache; |
| 130 | n = n->rb_left; |
| 131 | } else if (bytenr > start) { |
| 132 | if (contains && bytenr <= end) { |
| 133 | ret = cache; |
| 134 | break; |
| 135 | } |
| 136 | n = n->rb_right; |
| 137 | } else { |
| 138 | ret = cache; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | spin_unlock(&info->block_group_cache_lock); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * this is only called by cache_block_group, since we could have freed extents |
| 149 | * we need to check the pinned_extents for any extents that can't be used yet |
| 150 | * since their free space will be released as soon as the transaction commits. |
| 151 | */ |
| 152 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 153 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 154 | { |
| 155 | u64 extent_start, extent_end, size; |
| 156 | int ret; |
| 157 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 158 | mutex_lock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 159 | while (start < end) { |
| 160 | ret = find_first_extent_bit(&info->pinned_extents, start, |
| 161 | &extent_start, &extent_end, |
| 162 | EXTENT_DIRTY); |
| 163 | if (ret) |
| 164 | break; |
| 165 | |
| 166 | if (extent_start == start) { |
| 167 | start = extent_end + 1; |
| 168 | } else if (extent_start > start && extent_start < end) { |
| 169 | size = extent_start - start; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 170 | ret = btrfs_add_free_space_lock(block_group, start, |
| 171 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 172 | BUG_ON(ret); |
| 173 | start = extent_end + 1; |
| 174 | } else { |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (start < end) { |
| 180 | size = end - start; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 181 | ret = btrfs_add_free_space_lock(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 182 | BUG_ON(ret); |
| 183 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 184 | mutex_unlock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 189 | static int cache_block_group(struct btrfs_root *root, |
| 190 | struct btrfs_block_group_cache *block_group) |
| 191 | { |
| 192 | struct btrfs_path *path; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 193 | int ret = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 194 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 195 | struct extent_buffer *leaf; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 196 | int slot; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 197 | u64 last = 0; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 198 | u64 first_free; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 199 | int found = 0; |
| 200 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 201 | if (!block_group) |
| 202 | return 0; |
| 203 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 204 | root = root->fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 205 | |
| 206 | if (block_group->cached) |
| 207 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 208 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 209 | path = btrfs_alloc_path(); |
| 210 | if (!path) |
| 211 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 212 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 213 | path->reada = 2; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 214 | /* |
| 215 | * we get into deadlocks with paths held by callers of this function. |
| 216 | * since the alloc_mutex is protecting things right now, just |
| 217 | * skip the locking here |
| 218 | */ |
| 219 | path->skip_locking = 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 220 | first_free = max_t(u64, block_group->key.objectid, |
| 221 | BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 222 | key.objectid = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 223 | key.offset = 0; |
| 224 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 225 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 226 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 227 | goto err; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 228 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 229 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 230 | goto err; |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 231 | if (ret == 0) { |
| 232 | leaf = path->nodes[0]; |
| 233 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 234 | if (key.objectid + key.offset > first_free) |
| 235 | first_free = key.objectid + key.offset; |
| 236 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 237 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 238 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 239 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 240 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 241 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 242 | if (ret < 0) |
| 243 | goto err; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 244 | if (ret == 0) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 245 | continue; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 246 | else |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 247 | break; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 248 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 249 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 250 | if (key.objectid < block_group->key.objectid) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 251 | goto next; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 252 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 253 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 254 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 255 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 256 | |
| 257 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
| 258 | if (!found) { |
| 259 | last = first_free; |
| 260 | found = 1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 261 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 262 | |
| 263 | add_new_free_space(block_group, root->fs_info, last, |
| 264 | key.objectid); |
| 265 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 266 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 267 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 268 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 269 | path->slots[0]++; |
| 270 | } |
| 271 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 272 | if (!found) |
| 273 | last = first_free; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 274 | |
| 275 | add_new_free_space(block_group, root->fs_info, last, |
| 276 | block_group->key.objectid + |
| 277 | block_group->key.offset); |
| 278 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 279 | block_group->cached = 1; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 280 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 281 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 282 | btrfs_free_path(path); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 283 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 286 | /* |
| 287 | * return the block group that starts at or after bytenr |
| 288 | */ |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 289 | struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct |
| 290 | btrfs_fs_info *info, |
| 291 | u64 bytenr) |
| 292 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 293 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 294 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 295 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 296 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 297 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 298 | } |
| 299 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 300 | /* |
| 301 | * return the block group that contains teh given bytenr |
| 302 | */ |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 303 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
| 304 | btrfs_fs_info *info, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 305 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 306 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 307 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 308 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 309 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 310 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 311 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 312 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 313 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 314 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 315 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 316 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 317 | struct list_head *head = &info->space_info; |
| 318 | struct list_head *cur; |
| 319 | struct btrfs_space_info *found; |
| 320 | list_for_each(cur, head) { |
| 321 | found = list_entry(cur, struct btrfs_space_info, list); |
| 322 | if (found->flags == flags) |
| 323 | return found; |
| 324 | } |
| 325 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 328 | static u64 div_factor(u64 num, int factor) |
| 329 | { |
| 330 | if (factor == 10) |
| 331 | return num; |
| 332 | num *= factor; |
| 333 | do_div(num, 10); |
| 334 | return num; |
| 335 | } |
| 336 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 337 | static struct btrfs_block_group_cache * |
| 338 | __btrfs_find_block_group(struct btrfs_root *root, |
| 339 | struct btrfs_block_group_cache *hint, |
| 340 | u64 search_start, int data, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 341 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 342 | struct btrfs_block_group_cache *cache; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 343 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 344 | struct btrfs_fs_info *info = root->fs_info; |
| 345 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 346 | u64 last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 347 | u64 free_check; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 348 | int full_search = 0; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 349 | int factor = 10; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 350 | int wrapped = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 351 | |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 352 | if (data & BTRFS_BLOCK_GROUP_METADATA) |
| 353 | factor = 9; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 354 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 355 | if (search_start) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 356 | struct btrfs_block_group_cache *shint; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 357 | shint = btrfs_lookup_first_block_group(info, search_start); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 358 | if (shint && block_group_bits(shint, data)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 359 | spin_lock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 360 | used = btrfs_block_group_used(&shint->item); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 361 | if (used + shint->pinned + shint->reserved < |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 362 | div_factor(shint->key.offset, factor)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 363 | spin_unlock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 364 | return shint; |
| 365 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 366 | spin_unlock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 367 | } |
| 368 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 369 | if (hint && block_group_bits(hint, data)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 370 | spin_lock(&hint->lock); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 371 | used = btrfs_block_group_used(&hint->item); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 372 | if (used + hint->pinned + hint->reserved < |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 373 | div_factor(hint->key.offset, factor)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 374 | spin_unlock(&hint->lock); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 375 | return hint; |
| 376 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 377 | spin_unlock(&hint->lock); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 378 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 379 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 380 | if (hint) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 381 | last = max(hint->key.objectid, search_start); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 382 | else |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 383 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 384 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 385 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 386 | while (1) { |
| 387 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 388 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 389 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 390 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 391 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 392 | last = cache->key.objectid + cache->key.offset; |
| 393 | used = btrfs_block_group_used(&cache->item); |
| 394 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 395 | if (block_group_bits(cache, data)) { |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 396 | free_check = div_factor(cache->key.offset, factor); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 397 | if (used + cache->pinned + cache->reserved < |
| 398 | free_check) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 399 | found_group = cache; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 400 | spin_unlock(&cache->lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 401 | goto found; |
| 402 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 403 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 404 | spin_unlock(&cache->lock); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 405 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 406 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 407 | if (!wrapped) { |
| 408 | last = search_start; |
| 409 | wrapped = 1; |
| 410 | goto again; |
| 411 | } |
| 412 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 413 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 414 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 415 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 416 | goto again; |
| 417 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 418 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 419 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 422 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 423 | struct btrfs_block_group_cache |
| 424 | *hint, u64 search_start, |
| 425 | int data, int owner) |
| 426 | { |
| 427 | |
| 428 | struct btrfs_block_group_cache *ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 429 | ret = __btrfs_find_block_group(root, hint, search_start, data, owner); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 430 | return ret; |
| 431 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 432 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 433 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 434 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 435 | { |
| 436 | int ret; |
| 437 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 438 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 439 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 440 | path = btrfs_alloc_path(); |
| 441 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 442 | key.objectid = start; |
| 443 | key.offset = len; |
| 444 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 445 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 446 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 447 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 448 | return ret; |
| 449 | } |
| 450 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 451 | /* |
| 452 | * Back reference rules. Back refs have three main goals: |
| 453 | * |
| 454 | * 1) differentiate between all holders of references to an extent so that |
| 455 | * when a reference is dropped we can make sure it was a valid reference |
| 456 | * before freeing the extent. |
| 457 | * |
| 458 | * 2) Provide enough information to quickly find the holders of an extent |
| 459 | * if we notice a given block is corrupted or bad. |
| 460 | * |
| 461 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 462 | * maintenance. This is actually the same as #2, but with a slightly |
| 463 | * different use case. |
| 464 | * |
| 465 | * File extents can be referenced by: |
| 466 | * |
| 467 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 468 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 469 | * - different offsets inside a file (bookend extents in file.c) |
| 470 | * |
| 471 | * The extent ref structure has fields for: |
| 472 | * |
| 473 | * - Objectid of the subvolume root |
| 474 | * - Generation number of the tree holding the reference |
| 475 | * - objectid of the file holding the reference |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 476 | * - number of references holding by parent node (alway 1 for tree blocks) |
| 477 | * |
| 478 | * Btree leaf may hold multiple references to a file extent. In most cases, |
| 479 | * these references are from same file and the corresponding offsets inside |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 480 | * the file are close together. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 481 | * |
| 482 | * When a file extent is allocated the fields are filled in: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 483 | * (root_key.objectid, trans->transid, inode objectid, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 484 | * |
| 485 | * When a leaf is cow'd new references are added for every file extent found |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 486 | * in the leaf. It looks similar to the create case, but trans->transid will |
| 487 | * be different when the block is cow'd. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 488 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 489 | * (root_key.objectid, trans->transid, inode objectid, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 490 | * number of references in the leaf) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 491 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 492 | * When a file extent is removed either during snapshot deletion or |
| 493 | * file truncation, we find the corresponding back reference and check |
| 494 | * the following fields: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 495 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 496 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 497 | * inode objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 498 | * |
| 499 | * Btree extents can be referenced by: |
| 500 | * |
| 501 | * - Different subvolumes |
| 502 | * - Different generations of the same subvolume |
| 503 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 504 | * When a tree block is created, back references are inserted: |
| 505 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 506 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 507 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 508 | * When a tree block is cow'd, new back references are added for all the |
| 509 | * blocks it points to. If the tree block isn't in reference counted root, |
| 510 | * the old back references are removed. These new back references are of |
| 511 | * the form (trans->transid will have increased since creation): |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 512 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 513 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 514 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 515 | * When a backref is in deleting, the following fields are checked: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 516 | * |
| 517 | * if backref was for a tree root: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 518 | * (btrfs_header_owner(itself), btrfs_header_generation(itself), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 519 | * else |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 520 | * (btrfs_header_owner(parent), btrfs_header_generation(parent), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 521 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 522 | * Back Reference Key composing: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 523 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 524 | * The key objectid corresponds to the first byte in the extent, the key |
| 525 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first |
| 526 | * byte of parent extent. If a extent is tree root, the key offset is set |
| 527 | * to the key objectid. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 528 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 529 | |
| 530 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 531 | struct btrfs_root *root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 532 | struct btrfs_path *path, |
| 533 | u64 bytenr, u64 parent, |
| 534 | u64 ref_root, u64 ref_generation, |
| 535 | u64 owner_objectid, int del) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 536 | { |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 537 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 538 | struct btrfs_extent_ref *ref; |
| 539 | struct extent_buffer *leaf; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 540 | u64 ref_objectid; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 541 | int ret; |
| 542 | |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 543 | key.objectid = bytenr; |
| 544 | key.type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 545 | key.offset = parent; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 546 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 547 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); |
| 548 | if (ret < 0) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 549 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 550 | if (ret > 0) { |
| 551 | ret = -ENOENT; |
| 552 | goto out; |
| 553 | } |
| 554 | |
| 555 | leaf = path->nodes[0]; |
| 556 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 557 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 558 | if (btrfs_ref_root(leaf, ref) != ref_root || |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 559 | btrfs_ref_generation(leaf, ref) != ref_generation || |
| 560 | (ref_objectid != owner_objectid && |
| 561 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 562 | ret = -EIO; |
| 563 | WARN_ON(1); |
| 564 | goto out; |
| 565 | } |
| 566 | ret = 0; |
| 567 | out: |
| 568 | return ret; |
| 569 | } |
| 570 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 571 | /* |
| 572 | * updates all the backrefs that are pending on update_list for the |
| 573 | * extent_root |
| 574 | */ |
| 575 | static int noinline update_backrefs(struct btrfs_trans_handle *trans, |
| 576 | struct btrfs_root *extent_root, |
| 577 | struct btrfs_path *path, |
| 578 | struct list_head *update_list) |
| 579 | { |
| 580 | struct btrfs_key key; |
| 581 | struct btrfs_extent_ref *ref; |
| 582 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 583 | struct pending_extent_op *op; |
| 584 | struct extent_buffer *leaf; |
| 585 | int ret = 0; |
| 586 | struct list_head *cur = update_list->next; |
| 587 | u64 ref_objectid; |
| 588 | u64 ref_root = extent_root->root_key.objectid; |
| 589 | |
| 590 | op = list_entry(cur, struct pending_extent_op, list); |
| 591 | |
| 592 | search: |
| 593 | key.objectid = op->bytenr; |
| 594 | key.type = BTRFS_EXTENT_REF_KEY; |
| 595 | key.offset = op->orig_parent; |
| 596 | |
| 597 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1); |
| 598 | BUG_ON(ret); |
| 599 | |
| 600 | leaf = path->nodes[0]; |
| 601 | |
| 602 | loop: |
| 603 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 604 | |
| 605 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 606 | |
| 607 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 608 | btrfs_ref_generation(leaf, ref) != op->orig_generation || |
| 609 | (ref_objectid != op->level && |
| 610 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
| 611 | printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, " |
| 612 | "owner %u\n", op->bytenr, op->orig_parent, |
| 613 | ref_root, op->level); |
| 614 | btrfs_print_leaf(extent_root, leaf); |
| 615 | BUG(); |
| 616 | } |
| 617 | |
| 618 | key.objectid = op->bytenr; |
| 619 | key.offset = op->parent; |
| 620 | key.type = BTRFS_EXTENT_REF_KEY; |
| 621 | ret = btrfs_set_item_key_safe(trans, extent_root, path, &key); |
| 622 | BUG_ON(ret); |
| 623 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 624 | btrfs_set_ref_generation(leaf, ref, op->generation); |
| 625 | |
| 626 | cur = cur->next; |
| 627 | |
| 628 | list_del_init(&op->list); |
| 629 | unlock_extent(&info->extent_ins, op->bytenr, |
| 630 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 631 | kfree(op); |
| 632 | |
| 633 | if (cur == update_list) { |
| 634 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 635 | btrfs_release_path(extent_root, path); |
| 636 | goto out; |
| 637 | } |
| 638 | |
| 639 | op = list_entry(cur, struct pending_extent_op, list); |
| 640 | |
| 641 | path->slots[0]++; |
| 642 | while (path->slots[0] < btrfs_header_nritems(leaf)) { |
| 643 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 644 | if (key.objectid == op->bytenr && |
| 645 | key.type == BTRFS_EXTENT_REF_KEY) |
| 646 | goto loop; |
| 647 | path->slots[0]++; |
| 648 | } |
| 649 | |
| 650 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 651 | btrfs_release_path(extent_root, path); |
| 652 | goto search; |
| 653 | |
| 654 | out: |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | static int noinline insert_extents(struct btrfs_trans_handle *trans, |
| 659 | struct btrfs_root *extent_root, |
| 660 | struct btrfs_path *path, |
| 661 | struct list_head *insert_list, int nr) |
| 662 | { |
| 663 | struct btrfs_key *keys; |
| 664 | u32 *data_size; |
| 665 | struct pending_extent_op *op; |
| 666 | struct extent_buffer *leaf; |
| 667 | struct list_head *cur = insert_list->next; |
| 668 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 669 | u64 ref_root = extent_root->root_key.objectid; |
| 670 | int i = 0, last = 0, ret; |
| 671 | int total = nr * 2; |
| 672 | |
| 673 | if (!nr) |
| 674 | return 0; |
| 675 | |
| 676 | keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS); |
| 677 | if (!keys) |
| 678 | return -ENOMEM; |
| 679 | |
| 680 | data_size = kzalloc(total * sizeof(u32), GFP_NOFS); |
| 681 | if (!data_size) { |
| 682 | kfree(keys); |
| 683 | return -ENOMEM; |
| 684 | } |
| 685 | |
| 686 | list_for_each_entry(op, insert_list, list) { |
| 687 | keys[i].objectid = op->bytenr; |
| 688 | keys[i].offset = op->num_bytes; |
| 689 | keys[i].type = BTRFS_EXTENT_ITEM_KEY; |
| 690 | data_size[i] = sizeof(struct btrfs_extent_item); |
| 691 | i++; |
| 692 | |
| 693 | keys[i].objectid = op->bytenr; |
| 694 | keys[i].offset = op->parent; |
| 695 | keys[i].type = BTRFS_EXTENT_REF_KEY; |
| 696 | data_size[i] = sizeof(struct btrfs_extent_ref); |
| 697 | i++; |
| 698 | } |
| 699 | |
| 700 | op = list_entry(cur, struct pending_extent_op, list); |
| 701 | i = 0; |
| 702 | while (i < total) { |
| 703 | int c; |
| 704 | ret = btrfs_insert_some_items(trans, extent_root, path, |
| 705 | keys+i, data_size+i, total-i); |
| 706 | BUG_ON(ret < 0); |
| 707 | |
| 708 | if (last && ret > 1) |
| 709 | BUG(); |
| 710 | |
| 711 | leaf = path->nodes[0]; |
| 712 | for (c = 0; c < ret; c++) { |
| 713 | int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY; |
| 714 | |
| 715 | /* |
| 716 | * if the first item we inserted was a backref, then |
| 717 | * the EXTENT_ITEM will be the odd c's, else it will |
| 718 | * be the even c's |
| 719 | */ |
| 720 | if ((ref_first && (c % 2)) || |
| 721 | (!ref_first && !(c % 2))) { |
| 722 | struct btrfs_extent_item *itm; |
| 723 | |
| 724 | itm = btrfs_item_ptr(leaf, path->slots[0] + c, |
| 725 | struct btrfs_extent_item); |
| 726 | btrfs_set_extent_refs(path->nodes[0], itm, 1); |
| 727 | op->del++; |
| 728 | } else { |
| 729 | struct btrfs_extent_ref *ref; |
| 730 | |
| 731 | ref = btrfs_item_ptr(leaf, path->slots[0] + c, |
| 732 | struct btrfs_extent_ref); |
| 733 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 734 | btrfs_set_ref_generation(leaf, ref, |
| 735 | op->generation); |
| 736 | btrfs_set_ref_objectid(leaf, ref, op->level); |
| 737 | btrfs_set_ref_num_refs(leaf, ref, 1); |
| 738 | op->del++; |
| 739 | } |
| 740 | |
| 741 | /* |
| 742 | * using del to see when its ok to free up the |
| 743 | * pending_extent_op. In the case where we insert the |
| 744 | * last item on the list in order to help do batching |
| 745 | * we need to not free the extent op until we actually |
| 746 | * insert the extent_item |
| 747 | */ |
| 748 | if (op->del == 2) { |
| 749 | unlock_extent(&info->extent_ins, op->bytenr, |
| 750 | op->bytenr + op->num_bytes - 1, |
| 751 | GFP_NOFS); |
| 752 | cur = cur->next; |
| 753 | list_del_init(&op->list); |
| 754 | kfree(op); |
| 755 | if (cur != insert_list) |
| 756 | op = list_entry(cur, |
| 757 | struct pending_extent_op, |
| 758 | list); |
| 759 | } |
| 760 | } |
| 761 | btrfs_mark_buffer_dirty(leaf); |
| 762 | btrfs_release_path(extent_root, path); |
| 763 | |
| 764 | /* |
| 765 | * Ok backref's and items usually go right next to eachother, |
| 766 | * but if we could only insert 1 item that means that we |
| 767 | * inserted on the end of a leaf, and we have no idea what may |
| 768 | * be on the next leaf so we just play it safe. In order to |
| 769 | * try and help this case we insert the last thing on our |
| 770 | * insert list so hopefully it will end up being the last |
| 771 | * thing on the leaf and everything else will be before it, |
| 772 | * which will let us insert a whole bunch of items at the same |
| 773 | * time. |
| 774 | */ |
| 775 | if (ret == 1 && !last && (i + ret < total)) { |
| 776 | /* |
| 777 | * last: where we will pick up the next time around |
| 778 | * i: our current key to insert, will be total - 1 |
| 779 | * cur: the current op we are screwing with |
| 780 | * op: duh |
| 781 | */ |
| 782 | last = i + ret; |
| 783 | i = total - 1; |
| 784 | cur = insert_list->prev; |
| 785 | op = list_entry(cur, struct pending_extent_op, list); |
| 786 | } else if (last) { |
| 787 | /* |
| 788 | * ok we successfully inserted the last item on the |
| 789 | * list, lets reset everything |
| 790 | * |
| 791 | * i: our current key to insert, so where we left off |
| 792 | * last time |
| 793 | * last: done with this |
| 794 | * cur: the op we are messing with |
| 795 | * op: duh |
| 796 | * total: since we inserted the last key, we need to |
| 797 | * decrement total so we dont overflow |
| 798 | */ |
| 799 | i = last; |
| 800 | last = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 801 | total--; |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame^] | 802 | if (i < total) { |
| 803 | cur = insert_list->next; |
| 804 | op = list_entry(cur, struct pending_extent_op, |
| 805 | list); |
| 806 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 807 | } else { |
| 808 | i += ret; |
| 809 | } |
| 810 | |
| 811 | cond_resched(); |
| 812 | } |
| 813 | ret = 0; |
| 814 | kfree(keys); |
| 815 | kfree(data_size); |
| 816 | return ret; |
| 817 | } |
| 818 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 819 | static int noinline insert_extent_backref(struct btrfs_trans_handle *trans, |
| 820 | struct btrfs_root *root, |
| 821 | struct btrfs_path *path, |
| 822 | u64 bytenr, u64 parent, |
| 823 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 824 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 825 | { |
| 826 | struct btrfs_key key; |
| 827 | struct extent_buffer *leaf; |
| 828 | struct btrfs_extent_ref *ref; |
| 829 | u32 num_refs; |
| 830 | int ret; |
| 831 | |
| 832 | key.objectid = bytenr; |
| 833 | key.type = BTRFS_EXTENT_REF_KEY; |
| 834 | key.offset = parent; |
| 835 | |
| 836 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
| 837 | if (ret == 0) { |
| 838 | leaf = path->nodes[0]; |
| 839 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 840 | struct btrfs_extent_ref); |
| 841 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 842 | btrfs_set_ref_generation(leaf, ref, ref_generation); |
| 843 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 844 | btrfs_set_ref_num_refs(leaf, ref, 1); |
| 845 | } else if (ret == -EEXIST) { |
| 846 | u64 existing_owner; |
| 847 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); |
| 848 | leaf = path->nodes[0]; |
| 849 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 850 | struct btrfs_extent_ref); |
| 851 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 852 | btrfs_ref_generation(leaf, ref) != ref_generation) { |
| 853 | ret = -EIO; |
| 854 | WARN_ON(1); |
| 855 | goto out; |
| 856 | } |
| 857 | |
| 858 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 859 | BUG_ON(num_refs == 0); |
| 860 | btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); |
| 861 | |
| 862 | existing_owner = btrfs_ref_objectid(leaf, ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 863 | if (existing_owner != owner_objectid && |
| 864 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 865 | btrfs_set_ref_objectid(leaf, ref, |
| 866 | BTRFS_MULTIPLE_OBJECTIDS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 867 | } |
| 868 | ret = 0; |
| 869 | } else { |
| 870 | goto out; |
| 871 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 872 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 873 | out: |
| 874 | btrfs_release_path(root, path); |
| 875 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 876 | } |
| 877 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 878 | static int noinline remove_extent_backref(struct btrfs_trans_handle *trans, |
| 879 | struct btrfs_root *root, |
| 880 | struct btrfs_path *path) |
| 881 | { |
| 882 | struct extent_buffer *leaf; |
| 883 | struct btrfs_extent_ref *ref; |
| 884 | u32 num_refs; |
| 885 | int ret = 0; |
| 886 | |
| 887 | leaf = path->nodes[0]; |
| 888 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 889 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 890 | BUG_ON(num_refs == 0); |
| 891 | num_refs -= 1; |
| 892 | if (num_refs == 0) { |
| 893 | ret = btrfs_del_item(trans, root, path); |
| 894 | } else { |
| 895 | btrfs_set_ref_num_refs(leaf, ref, num_refs); |
| 896 | btrfs_mark_buffer_dirty(leaf); |
| 897 | } |
| 898 | btrfs_release_path(root, path); |
| 899 | return ret; |
| 900 | } |
| 901 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 902 | static int noinline free_extents(struct btrfs_trans_handle *trans, |
| 903 | struct btrfs_root *extent_root, |
| 904 | struct list_head *del_list) |
| 905 | { |
| 906 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 907 | struct btrfs_path *path; |
| 908 | struct btrfs_key key, found_key; |
| 909 | struct extent_buffer *leaf; |
| 910 | struct list_head *cur; |
| 911 | struct pending_extent_op *op; |
| 912 | struct btrfs_extent_item *ei; |
| 913 | int ret, num_to_del, extent_slot = 0, found_extent = 0; |
| 914 | u32 refs; |
| 915 | u64 bytes_freed = 0; |
| 916 | |
| 917 | path = btrfs_alloc_path(); |
| 918 | if (!path) |
| 919 | return -ENOMEM; |
| 920 | path->reada = 1; |
| 921 | |
| 922 | search: |
| 923 | /* search for the backref for the current ref we want to delete */ |
| 924 | cur = del_list->next; |
| 925 | op = list_entry(cur, struct pending_extent_op, list); |
| 926 | ret = lookup_extent_backref(trans, extent_root, path, op->bytenr, |
| 927 | op->orig_parent, |
| 928 | extent_root->root_key.objectid, |
| 929 | op->orig_generation, op->level, 1); |
| 930 | if (ret) { |
| 931 | printk("Unable to find backref byte nr %Lu root %Lu gen %Lu " |
| 932 | "owner %u\n", op->bytenr, |
| 933 | extent_root->root_key.objectid, op->orig_generation, |
| 934 | op->level); |
| 935 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 936 | WARN_ON(1); |
| 937 | goto out; |
| 938 | } |
| 939 | |
| 940 | extent_slot = path->slots[0]; |
| 941 | num_to_del = 1; |
| 942 | found_extent = 0; |
| 943 | |
| 944 | /* |
| 945 | * if we aren't the first item on the leaf we can move back one and see |
| 946 | * if our ref is right next to our extent item |
| 947 | */ |
| 948 | if (likely(extent_slot)) { |
| 949 | extent_slot--; |
| 950 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 951 | extent_slot); |
| 952 | if (found_key.objectid == op->bytenr && |
| 953 | found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 954 | found_key.offset == op->num_bytes) { |
| 955 | num_to_del++; |
| 956 | found_extent = 1; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * if we didn't find the extent we need to delete the backref and then |
| 962 | * search for the extent item key so we can update its ref count |
| 963 | */ |
| 964 | if (!found_extent) { |
| 965 | key.objectid = op->bytenr; |
| 966 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 967 | key.offset = op->num_bytes; |
| 968 | |
| 969 | ret = remove_extent_backref(trans, extent_root, path); |
| 970 | BUG_ON(ret); |
| 971 | btrfs_release_path(extent_root, path); |
| 972 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
| 973 | BUG_ON(ret); |
| 974 | extent_slot = path->slots[0]; |
| 975 | } |
| 976 | |
| 977 | /* this is where we update the ref count for the extent */ |
| 978 | leaf = path->nodes[0]; |
| 979 | ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item); |
| 980 | refs = btrfs_extent_refs(leaf, ei); |
| 981 | BUG_ON(refs == 0); |
| 982 | refs--; |
| 983 | btrfs_set_extent_refs(leaf, ei, refs); |
| 984 | |
| 985 | btrfs_mark_buffer_dirty(leaf); |
| 986 | |
| 987 | /* |
| 988 | * This extent needs deleting. The reason cur_slot is extent_slot + |
| 989 | * num_to_del is because extent_slot points to the slot where the extent |
| 990 | * is, and if the backref was not right next to the extent we will be |
| 991 | * deleting at least 1 item, and will want to start searching at the |
| 992 | * slot directly next to extent_slot. However if we did find the |
| 993 | * backref next to the extent item them we will be deleting at least 2 |
| 994 | * items and will want to start searching directly after the ref slot |
| 995 | */ |
| 996 | if (!refs) { |
| 997 | struct list_head *pos, *n, *end; |
| 998 | int cur_slot = extent_slot+num_to_del; |
| 999 | u64 super_used; |
| 1000 | u64 root_used; |
| 1001 | |
| 1002 | path->slots[0] = extent_slot; |
| 1003 | bytes_freed = op->num_bytes; |
| 1004 | |
Josef Bacik | e3e469f | 2008-11-17 21:11:49 -0500 | [diff] [blame] | 1005 | mutex_lock(&info->pinned_mutex); |
| 1006 | ret = pin_down_bytes(trans, extent_root, op->bytenr, |
| 1007 | op->num_bytes, op->level >= |
| 1008 | BTRFS_FIRST_FREE_OBJECTID); |
| 1009 | mutex_unlock(&info->pinned_mutex); |
| 1010 | BUG_ON(ret < 0); |
| 1011 | op->del = ret; |
| 1012 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1013 | /* |
| 1014 | * we need to see if we can delete multiple things at once, so |
| 1015 | * start looping through the list of extents we are wanting to |
| 1016 | * delete and see if their extent/backref's are right next to |
| 1017 | * eachother and the extents only have 1 ref |
| 1018 | */ |
| 1019 | for (pos = cur->next; pos != del_list; pos = pos->next) { |
| 1020 | struct pending_extent_op *tmp; |
| 1021 | |
| 1022 | tmp = list_entry(pos, struct pending_extent_op, list); |
| 1023 | |
| 1024 | /* we only want to delete extent+ref at this stage */ |
| 1025 | if (cur_slot >= btrfs_header_nritems(leaf) - 1) |
| 1026 | break; |
| 1027 | |
| 1028 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot); |
| 1029 | if (found_key.objectid != tmp->bytenr || |
| 1030 | found_key.type != BTRFS_EXTENT_ITEM_KEY || |
| 1031 | found_key.offset != tmp->num_bytes) |
| 1032 | break; |
| 1033 | |
| 1034 | /* check to make sure this extent only has one ref */ |
| 1035 | ei = btrfs_item_ptr(leaf, cur_slot, |
| 1036 | struct btrfs_extent_item); |
| 1037 | if (btrfs_extent_refs(leaf, ei) != 1) |
| 1038 | break; |
| 1039 | |
| 1040 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1); |
| 1041 | if (found_key.objectid != tmp->bytenr || |
| 1042 | found_key.type != BTRFS_EXTENT_REF_KEY || |
| 1043 | found_key.offset != tmp->orig_parent) |
| 1044 | break; |
| 1045 | |
| 1046 | /* |
| 1047 | * the ref is right next to the extent, we can set the |
| 1048 | * ref count to 0 since we will delete them both now |
| 1049 | */ |
| 1050 | btrfs_set_extent_refs(leaf, ei, 0); |
| 1051 | |
| 1052 | /* pin down the bytes for this extent */ |
| 1053 | mutex_lock(&info->pinned_mutex); |
| 1054 | ret = pin_down_bytes(trans, extent_root, tmp->bytenr, |
| 1055 | tmp->num_bytes, tmp->level >= |
| 1056 | BTRFS_FIRST_FREE_OBJECTID); |
| 1057 | mutex_unlock(&info->pinned_mutex); |
| 1058 | BUG_ON(ret < 0); |
| 1059 | |
| 1060 | /* |
| 1061 | * use the del field to tell if we need to go ahead and |
| 1062 | * free up the extent when we delete the item or not. |
| 1063 | */ |
| 1064 | tmp->del = ret; |
| 1065 | bytes_freed += tmp->num_bytes; |
| 1066 | |
| 1067 | num_to_del += 2; |
| 1068 | cur_slot += 2; |
| 1069 | } |
| 1070 | end = pos; |
| 1071 | |
| 1072 | /* update the free space counters */ |
| 1073 | spin_lock_irq(&info->delalloc_lock); |
| 1074 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1075 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1076 | super_used - bytes_freed); |
| 1077 | spin_unlock_irq(&info->delalloc_lock); |
| 1078 | |
| 1079 | root_used = btrfs_root_used(&extent_root->root_item); |
| 1080 | btrfs_set_root_used(&extent_root->root_item, |
| 1081 | root_used - bytes_freed); |
| 1082 | |
| 1083 | /* delete the items */ |
| 1084 | ret = btrfs_del_items(trans, extent_root, path, |
| 1085 | path->slots[0], num_to_del); |
| 1086 | BUG_ON(ret); |
| 1087 | |
| 1088 | /* |
| 1089 | * loop through the extents we deleted and do the cleanup work |
| 1090 | * on them |
| 1091 | */ |
| 1092 | for (pos = cur, n = pos->next; pos != end; |
| 1093 | pos = n, n = pos->next) { |
| 1094 | struct pending_extent_op *tmp; |
| 1095 | #ifdef BIO_RW_DISCARD |
| 1096 | u64 map_length; |
| 1097 | struct btrfs_multi_bio *multi = NULL; |
| 1098 | #endif |
| 1099 | tmp = list_entry(pos, struct pending_extent_op, list); |
| 1100 | |
| 1101 | /* |
| 1102 | * remember tmp->del tells us wether or not we pinned |
| 1103 | * down the extent |
| 1104 | */ |
| 1105 | ret = update_block_group(trans, extent_root, |
| 1106 | tmp->bytenr, tmp->num_bytes, 0, |
| 1107 | tmp->del); |
| 1108 | BUG_ON(ret); |
| 1109 | |
| 1110 | #ifdef BIO_RW_DISCARD |
| 1111 | ret = btrfs_map_block(&info->mapping_tree, READ, |
| 1112 | tmp->bytenr, &map_length, &multi, |
| 1113 | 0); |
| 1114 | if (!ret) { |
| 1115 | struct btrfs_bio_stripe *stripe; |
| 1116 | int i; |
| 1117 | |
| 1118 | stripe = multi->stripe; |
| 1119 | |
| 1120 | if (map_length > tmp->num_bytes) |
| 1121 | map_length = tmp->num_bytes; |
| 1122 | |
| 1123 | for (i = 0; i < multi->num_stripes; |
| 1124 | i++, stripe++) |
| 1125 | blkdev_issue_discard(stripe->dev->bdev, |
| 1126 | stripe->physical >> 9, |
| 1127 | map_length >> 9); |
| 1128 | kfree(multi); |
| 1129 | } |
| 1130 | #endif |
| 1131 | list_del_init(&tmp->list); |
| 1132 | unlock_extent(&info->extent_ins, tmp->bytenr, |
| 1133 | tmp->bytenr + tmp->num_bytes - 1, |
| 1134 | GFP_NOFS); |
| 1135 | kfree(tmp); |
| 1136 | } |
| 1137 | } else if (refs && found_extent) { |
| 1138 | /* |
| 1139 | * the ref and extent were right next to eachother, but the |
| 1140 | * extent still has a ref, so just free the backref and keep |
| 1141 | * going |
| 1142 | */ |
| 1143 | ret = remove_extent_backref(trans, extent_root, path); |
| 1144 | BUG_ON(ret); |
| 1145 | |
| 1146 | list_del_init(&op->list); |
| 1147 | unlock_extent(&info->extent_ins, op->bytenr, |
| 1148 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 1149 | kfree(op); |
| 1150 | } else { |
| 1151 | /* |
| 1152 | * the extent has multiple refs and the backref we were looking |
| 1153 | * for was not right next to it, so just unlock and go next, |
| 1154 | * we're good to go |
| 1155 | */ |
| 1156 | list_del_init(&op->list); |
| 1157 | unlock_extent(&info->extent_ins, op->bytenr, |
| 1158 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 1159 | kfree(op); |
| 1160 | } |
| 1161 | |
| 1162 | btrfs_release_path(extent_root, path); |
| 1163 | if (!list_empty(del_list)) |
| 1164 | goto search; |
| 1165 | |
| 1166 | out: |
| 1167 | btrfs_free_path(path); |
| 1168 | return ret; |
| 1169 | } |
| 1170 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1171 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 1172 | struct btrfs_root *root, u64 bytenr, |
| 1173 | u64 orig_parent, u64 parent, |
| 1174 | u64 orig_root, u64 ref_root, |
| 1175 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1176 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1177 | { |
| 1178 | int ret; |
| 1179 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1180 | struct btrfs_path *path; |
| 1181 | |
| 1182 | if (root == root->fs_info->extent_root) { |
| 1183 | struct pending_extent_op *extent_op; |
| 1184 | u64 num_bytes; |
| 1185 | |
| 1186 | BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL); |
| 1187 | num_bytes = btrfs_level_size(root, (int)owner_objectid); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1188 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1189 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1190 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1191 | u64 priv; |
| 1192 | ret = get_state_private(&root->fs_info->extent_ins, |
| 1193 | bytenr, &priv); |
| 1194 | BUG_ON(ret); |
| 1195 | extent_op = (struct pending_extent_op *) |
| 1196 | (unsigned long)priv; |
| 1197 | BUG_ON(extent_op->parent != orig_parent); |
| 1198 | BUG_ON(extent_op->generation != orig_generation); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1199 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1200 | extent_op->parent = parent; |
| 1201 | extent_op->generation = ref_generation; |
| 1202 | } else { |
| 1203 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 1204 | BUG_ON(!extent_op); |
| 1205 | |
| 1206 | extent_op->type = PENDING_BACKREF_UPDATE; |
| 1207 | extent_op->bytenr = bytenr; |
| 1208 | extent_op->num_bytes = num_bytes; |
| 1209 | extent_op->parent = parent; |
| 1210 | extent_op->orig_parent = orig_parent; |
| 1211 | extent_op->generation = ref_generation; |
| 1212 | extent_op->orig_generation = orig_generation; |
| 1213 | extent_op->level = (int)owner_objectid; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1214 | INIT_LIST_HEAD(&extent_op->list); |
| 1215 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1216 | |
| 1217 | set_extent_bits(&root->fs_info->extent_ins, |
| 1218 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1219 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1220 | set_state_private(&root->fs_info->extent_ins, |
| 1221 | bytenr, (unsigned long)extent_op); |
| 1222 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1223 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | path = btrfs_alloc_path(); |
| 1228 | if (!path) |
| 1229 | return -ENOMEM; |
| 1230 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1231 | bytenr, orig_parent, orig_root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1232 | orig_generation, owner_objectid, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1233 | if (ret) |
| 1234 | goto out; |
| 1235 | ret = remove_extent_backref(trans, extent_root, path); |
| 1236 | if (ret) |
| 1237 | goto out; |
| 1238 | ret = insert_extent_backref(trans, extent_root, path, bytenr, |
| 1239 | parent, ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1240 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1241 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1242 | finish_current_insert(trans, extent_root, 0); |
| 1243 | del_pending_extents(trans, extent_root, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1244 | out: |
| 1245 | btrfs_free_path(path); |
| 1246 | return ret; |
| 1247 | } |
| 1248 | |
| 1249 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 1250 | struct btrfs_root *root, u64 bytenr, |
| 1251 | u64 orig_parent, u64 parent, |
| 1252 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1253 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1254 | { |
| 1255 | int ret; |
| 1256 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 1257 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 1258 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1259 | ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, |
| 1260 | parent, ref_root, ref_root, |
| 1261 | ref_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1262 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1263 | return ret; |
| 1264 | } |
| 1265 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1266 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1267 | struct btrfs_root *root, u64 bytenr, |
| 1268 | u64 orig_parent, u64 parent, |
| 1269 | u64 orig_root, u64 ref_root, |
| 1270 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1271 | u64 owner_objectid) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1272 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1273 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1274 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1275 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1276 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1277 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1278 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1279 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1280 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1281 | if (!path) |
| 1282 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1283 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1284 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1285 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1286 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1287 | key.offset = (u64)-1; |
| 1288 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1289 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1290 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1291 | if (ret < 0) |
| 1292 | return ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1293 | BUG_ON(ret == 0 || path->slots[0] == 0); |
| 1294 | |
| 1295 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1296 | l = path->nodes[0]; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1297 | |
| 1298 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1299 | if (key.objectid != bytenr) { |
| 1300 | btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]); |
| 1301 | printk("wanted %Lu found %Lu\n", bytenr, key.objectid); |
| 1302 | BUG(); |
| 1303 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1304 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
| 1305 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1306 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1307 | refs = btrfs_extent_refs(l, item); |
| 1308 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1309 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1310 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1311 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1312 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1313 | path->reada = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1314 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
| 1315 | path, bytenr, parent, |
| 1316 | ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1317 | owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1318 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1319 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 1320 | del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1321 | |
| 1322 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1323 | return 0; |
| 1324 | } |
| 1325 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1326 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1327 | struct btrfs_root *root, |
| 1328 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1329 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1330 | u64 owner_objectid) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1331 | { |
| 1332 | int ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1333 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 1334 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 1335 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1336 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, |
| 1337 | 0, ref_root, 0, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1338 | owner_objectid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1339 | return ret; |
| 1340 | } |
| 1341 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1342 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 1343 | struct btrfs_root *root) |
| 1344 | { |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1345 | finish_current_insert(trans, root->fs_info->extent_root, 1); |
| 1346 | del_pending_extents(trans, root->fs_info->extent_root, 1); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1347 | return 0; |
| 1348 | } |
| 1349 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1350 | int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, |
| 1351 | struct btrfs_root *root, u64 bytenr, |
| 1352 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1353 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1354 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1355 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1356 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1357 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1358 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1359 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1360 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1361 | path = btrfs_alloc_path(); |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1362 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1363 | key.objectid = bytenr; |
| 1364 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1365 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1366 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1367 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1368 | if (ret < 0) |
| 1369 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1370 | if (ret != 0) { |
| 1371 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1372 | printk("failed to find block number %Lu\n", bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1373 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1374 | } |
| 1375 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1376 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1377 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1378 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1379 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1383 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 1384 | struct btrfs_root *root, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1385 | { |
| 1386 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1387 | struct btrfs_path *path; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1388 | struct extent_buffer *leaf; |
| 1389 | struct btrfs_extent_ref *ref_item; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1390 | struct btrfs_key key; |
| 1391 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1392 | u64 ref_root; |
| 1393 | u64 last_snapshot; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1394 | u32 nritems; |
| 1395 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1396 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1397 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1398 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1399 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1400 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1401 | path = btrfs_alloc_path(); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1402 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 1403 | if (ret < 0) |
| 1404 | goto out; |
| 1405 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1406 | |
| 1407 | ret = -ENOENT; |
| 1408 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1409 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1410 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1411 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1412 | leaf = path->nodes[0]; |
| 1413 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1414 | |
| 1415 | if (found_key.objectid != bytenr || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1416 | found_key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1417 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1418 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1419 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1420 | while (1) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1421 | leaf = path->nodes[0]; |
| 1422 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1423 | if (path->slots[0] >= nritems) { |
| 1424 | ret = btrfs_next_leaf(extent_root, path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1425 | if (ret < 0) |
| 1426 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1427 | if (ret == 0) |
| 1428 | continue; |
| 1429 | break; |
| 1430 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1431 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1432 | if (found_key.objectid != bytenr) |
| 1433 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 1434 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1435 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 1436 | path->slots[0]++; |
| 1437 | continue; |
| 1438 | } |
| 1439 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1440 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1441 | struct btrfs_extent_ref); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1442 | ref_root = btrfs_ref_root(leaf, ref_item); |
| 1443 | if (ref_root != root->root_key.objectid && |
| 1444 | ref_root != BTRFS_TREE_LOG_OBJECTID) { |
| 1445 | ret = 1; |
| 1446 | goto out; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1447 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1448 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { |
| 1449 | ret = 1; |
| 1450 | goto out; |
| 1451 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1452 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1453 | path->slots[0]++; |
| 1454 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1455 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1456 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1457 | btrfs_free_path(path); |
| 1458 | return ret; |
| 1459 | } |
| 1460 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1461 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1462 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1463 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1464 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1465 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1466 | u64 root_gen; |
| 1467 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1468 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1469 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1470 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1471 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1472 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1473 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1474 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1475 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1476 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 1477 | shared = 0; |
| 1478 | root_gen = root->root_key.offset; |
| 1479 | } else { |
| 1480 | shared = 1; |
| 1481 | root_gen = trans->transid - 1; |
| 1482 | } |
| 1483 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1484 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1485 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1486 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1487 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1488 | struct btrfs_leaf_ref *ref; |
| 1489 | struct btrfs_extent_info *info; |
| 1490 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1491 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1492 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1493 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1494 | goto out; |
| 1495 | } |
| 1496 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1497 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1498 | ref->bytenr = buf->start; |
| 1499 | ref->owner = btrfs_header_owner(buf); |
| 1500 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1501 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1502 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1503 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1504 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1505 | u64 disk_bytenr; |
| 1506 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1507 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1508 | continue; |
| 1509 | fi = btrfs_item_ptr(buf, i, |
| 1510 | struct btrfs_file_extent_item); |
| 1511 | if (btrfs_file_extent_type(buf, fi) == |
| 1512 | BTRFS_FILE_EXTENT_INLINE) |
| 1513 | continue; |
| 1514 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1515 | if (disk_bytenr == 0) |
| 1516 | continue; |
| 1517 | |
| 1518 | info->bytenr = disk_bytenr; |
| 1519 | info->num_bytes = |
| 1520 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 1521 | info->objectid = key.objectid; |
| 1522 | info->offset = key.offset; |
| 1523 | info++; |
| 1524 | } |
| 1525 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1526 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1527 | if (ret == -EEXIST && shared) { |
| 1528 | struct btrfs_leaf_ref *old; |
| 1529 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 1530 | BUG_ON(!old); |
| 1531 | btrfs_remove_leaf_ref(root, old); |
| 1532 | btrfs_free_leaf_ref(root, old); |
| 1533 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 1534 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1535 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1536 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1537 | } |
| 1538 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1539 | return ret; |
| 1540 | } |
| 1541 | |
| 1542 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1543 | struct extent_buffer *orig_buf, struct extent_buffer *buf, |
| 1544 | u32 *nr_extents) |
| 1545 | { |
| 1546 | u64 bytenr; |
| 1547 | u64 ref_root; |
| 1548 | u64 orig_root; |
| 1549 | u64 ref_generation; |
| 1550 | u64 orig_generation; |
| 1551 | u32 nritems; |
| 1552 | u32 nr_file_extents = 0; |
| 1553 | struct btrfs_key key; |
| 1554 | struct btrfs_file_extent_item *fi; |
| 1555 | int i; |
| 1556 | int level; |
| 1557 | int ret = 0; |
| 1558 | int faili = 0; |
| 1559 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1560 | u64, u64, u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1561 | |
| 1562 | ref_root = btrfs_header_owner(buf); |
| 1563 | ref_generation = btrfs_header_generation(buf); |
| 1564 | orig_root = btrfs_header_owner(orig_buf); |
| 1565 | orig_generation = btrfs_header_generation(orig_buf); |
| 1566 | |
| 1567 | nritems = btrfs_header_nritems(buf); |
| 1568 | level = btrfs_header_level(buf); |
| 1569 | |
| 1570 | if (root->ref_cows) { |
| 1571 | process_func = __btrfs_inc_extent_ref; |
| 1572 | } else { |
| 1573 | if (level == 0 && |
| 1574 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1575 | goto out; |
| 1576 | if (level != 0 && |
| 1577 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1578 | goto out; |
| 1579 | process_func = __btrfs_update_extent_ref; |
| 1580 | } |
| 1581 | |
| 1582 | for (i = 0; i < nritems; i++) { |
| 1583 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1584 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1585 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1586 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1587 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1588 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1589 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1590 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1591 | BTRFS_FILE_EXTENT_INLINE) |
| 1592 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1593 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1594 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1595 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1596 | |
| 1597 | nr_file_extents++; |
| 1598 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1599 | ret = process_func(trans, root, bytenr, |
| 1600 | orig_buf->start, buf->start, |
| 1601 | orig_root, ref_root, |
| 1602 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1603 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1604 | |
| 1605 | if (ret) { |
| 1606 | faili = i; |
| 1607 | WARN_ON(1); |
| 1608 | goto fail; |
| 1609 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1610 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1611 | bytenr = btrfs_node_blockptr(buf, i); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1612 | ret = process_func(trans, root, bytenr, |
| 1613 | orig_buf->start, buf->start, |
| 1614 | orig_root, ref_root, |
| 1615 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1616 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1617 | if (ret) { |
| 1618 | faili = i; |
| 1619 | WARN_ON(1); |
| 1620 | goto fail; |
| 1621 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1622 | } |
| 1623 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1624 | out: |
| 1625 | if (nr_extents) { |
| 1626 | if (level == 0) |
| 1627 | *nr_extents = nr_file_extents; |
| 1628 | else |
| 1629 | *nr_extents = nritems; |
| 1630 | } |
| 1631 | return 0; |
| 1632 | fail: |
| 1633 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1634 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1635 | } |
| 1636 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1637 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
| 1638 | struct btrfs_root *root, struct extent_buffer *orig_buf, |
| 1639 | struct extent_buffer *buf, int start_slot, int nr) |
| 1640 | |
| 1641 | { |
| 1642 | u64 bytenr; |
| 1643 | u64 ref_root; |
| 1644 | u64 orig_root; |
| 1645 | u64 ref_generation; |
| 1646 | u64 orig_generation; |
| 1647 | struct btrfs_key key; |
| 1648 | struct btrfs_file_extent_item *fi; |
| 1649 | int i; |
| 1650 | int ret; |
| 1651 | int slot; |
| 1652 | int level; |
| 1653 | |
| 1654 | BUG_ON(start_slot < 0); |
| 1655 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); |
| 1656 | |
| 1657 | ref_root = btrfs_header_owner(buf); |
| 1658 | ref_generation = btrfs_header_generation(buf); |
| 1659 | orig_root = btrfs_header_owner(orig_buf); |
| 1660 | orig_generation = btrfs_header_generation(orig_buf); |
| 1661 | level = btrfs_header_level(buf); |
| 1662 | |
| 1663 | if (!root->ref_cows) { |
| 1664 | if (level == 0 && |
| 1665 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1666 | return 0; |
| 1667 | if (level != 0 && |
| 1668 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { |
| 1673 | cond_resched(); |
| 1674 | if (level == 0) { |
| 1675 | btrfs_item_key_to_cpu(buf, &key, slot); |
| 1676 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1677 | continue; |
| 1678 | fi = btrfs_item_ptr(buf, slot, |
| 1679 | struct btrfs_file_extent_item); |
| 1680 | if (btrfs_file_extent_type(buf, fi) == |
| 1681 | BTRFS_FILE_EXTENT_INLINE) |
| 1682 | continue; |
| 1683 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1684 | if (bytenr == 0) |
| 1685 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1686 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1687 | orig_buf->start, buf->start, |
| 1688 | orig_root, ref_root, |
| 1689 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1690 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1691 | if (ret) |
| 1692 | goto fail; |
| 1693 | } else { |
| 1694 | bytenr = btrfs_node_blockptr(buf, slot); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1695 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1696 | orig_buf->start, buf->start, |
| 1697 | orig_root, ref_root, |
| 1698 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1699 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1700 | if (ret) |
| 1701 | goto fail; |
| 1702 | } |
| 1703 | } |
| 1704 | return 0; |
| 1705 | fail: |
| 1706 | WARN_ON(1); |
| 1707 | return -1; |
| 1708 | } |
| 1709 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1710 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1711 | struct btrfs_root *root, |
| 1712 | struct btrfs_path *path, |
| 1713 | struct btrfs_block_group_cache *cache) |
| 1714 | { |
| 1715 | int ret; |
| 1716 | int pending_ret; |
| 1717 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1718 | unsigned long bi; |
| 1719 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1720 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1721 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1722 | if (ret < 0) |
| 1723 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1724 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1725 | |
| 1726 | leaf = path->nodes[0]; |
| 1727 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1728 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1729 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1730 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1731 | fail: |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1732 | finish_current_insert(trans, extent_root, 0); |
| 1733 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1734 | if (ret) |
| 1735 | return ret; |
| 1736 | if (pending_ret) |
| 1737 | return pending_ret; |
| 1738 | return 0; |
| 1739 | |
| 1740 | } |
| 1741 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1742 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1743 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1744 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1745 | struct btrfs_block_group_cache *cache, *entry; |
| 1746 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1747 | int err = 0; |
| 1748 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1749 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1750 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1751 | |
| 1752 | path = btrfs_alloc_path(); |
| 1753 | if (!path) |
| 1754 | return -ENOMEM; |
| 1755 | |
| 1756 | while(1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1757 | cache = NULL; |
| 1758 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1759 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1760 | n; n = rb_next(n)) { |
| 1761 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1762 | cache_node); |
| 1763 | if (entry->dirty) { |
| 1764 | cache = entry; |
| 1765 | break; |
| 1766 | } |
| 1767 | } |
| 1768 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1769 | |
| 1770 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1771 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1772 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1773 | cache->dirty = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1774 | last += cache->key.offset; |
| 1775 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1776 | err = write_one_cache_group(trans, root, |
| 1777 | path, cache); |
| 1778 | /* |
| 1779 | * if we fail to write the cache group, we want |
| 1780 | * to keep it marked dirty in hopes that a later |
| 1781 | * write will work |
| 1782 | */ |
| 1783 | if (err) { |
| 1784 | werr = err; |
| 1785 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | btrfs_free_path(path); |
| 1789 | return werr; |
| 1790 | } |
| 1791 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1792 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1793 | u64 total_bytes, u64 bytes_used, |
| 1794 | struct btrfs_space_info **space_info) |
| 1795 | { |
| 1796 | struct btrfs_space_info *found; |
| 1797 | |
| 1798 | found = __find_space_info(info, flags); |
| 1799 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1800 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1801 | found->total_bytes += total_bytes; |
| 1802 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1803 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1804 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1805 | *space_info = found; |
| 1806 | return 0; |
| 1807 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1808 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1809 | if (!found) |
| 1810 | return -ENOMEM; |
| 1811 | |
| 1812 | list_add(&found->list, &info->space_info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1813 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1814 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1815 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1816 | found->flags = flags; |
| 1817 | found->total_bytes = total_bytes; |
| 1818 | found->bytes_used = bytes_used; |
| 1819 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1820 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1821 | found->bytes_readonly = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1822 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1823 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1824 | *space_info = found; |
| 1825 | return 0; |
| 1826 | } |
| 1827 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1828 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1829 | { |
| 1830 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1831 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1832 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1833 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1834 | if (extra_flags) { |
| 1835 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1836 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1837 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1838 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1839 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1840 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1841 | } |
| 1842 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1843 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1844 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 1845 | { |
| 1846 | spin_lock(&cache->space_info->lock); |
| 1847 | spin_lock(&cache->lock); |
| 1848 | if (!cache->ro) { |
| 1849 | cache->space_info->bytes_readonly += cache->key.offset - |
| 1850 | btrfs_block_group_used(&cache->item); |
| 1851 | cache->ro = 1; |
| 1852 | } |
| 1853 | spin_unlock(&cache->lock); |
| 1854 | spin_unlock(&cache->space_info->lock); |
| 1855 | } |
| 1856 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1857 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1858 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1859 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1860 | |
| 1861 | if (num_devices == 1) |
| 1862 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1863 | if (num_devices < 4) |
| 1864 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1865 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1866 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1867 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1868 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1869 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1870 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1871 | |
| 1872 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1873 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1874 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1875 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1876 | |
| 1877 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1878 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1879 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1880 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1881 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1882 | return flags; |
| 1883 | } |
| 1884 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1885 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1886 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1887 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1888 | { |
| 1889 | struct btrfs_space_info *space_info; |
| 1890 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1891 | int ret = 0; |
| 1892 | |
| 1893 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1894 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1895 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1896 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1897 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1898 | if (!space_info) { |
| 1899 | ret = update_space_info(extent_root->fs_info, flags, |
| 1900 | 0, 0, &space_info); |
| 1901 | BUG_ON(ret); |
| 1902 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1903 | BUG_ON(!space_info); |
| 1904 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1905 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1906 | if (space_info->force_alloc) { |
| 1907 | force = 1; |
| 1908 | space_info->force_alloc = 0; |
| 1909 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1910 | if (space_info->full) { |
| 1911 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1912 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1913 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1914 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1915 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
| 1916 | thresh = div_factor(thresh, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1917 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1918 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1919 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 1920 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1921 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1922 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1923 | spin_unlock(&space_info->lock); |
| 1924 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1925 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1926 | if (ret) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1927 | printk("space info full %Lu\n", flags); |
| 1928 | space_info->full = 1; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1929 | } |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1930 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1931 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1932 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1933 | } |
| 1934 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1935 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1936 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1937 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1938 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1939 | { |
| 1940 | struct btrfs_block_group_cache *cache; |
| 1941 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1942 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1943 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1944 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1945 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1946 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1947 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1948 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1949 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1950 | byte_in_group = bytenr - cache->key.objectid; |
| 1951 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1952 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1953 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1954 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1955 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1956 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1957 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1958 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1959 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1960 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1961 | if (cache->ro) { |
| 1962 | cache->space_info->bytes_readonly -= num_bytes; |
| 1963 | WARN_ON(1); |
| 1964 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1965 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1966 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1967 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1968 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1969 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1970 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1971 | if (cache->ro) |
| 1972 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1973 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1974 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1975 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1976 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1977 | int ret; |
| 1978 | ret = btrfs_add_free_space(cache, bytenr, |
| 1979 | num_bytes); |
| 1980 | if (ret) |
| 1981 | return -1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1982 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1983 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1984 | total -= num_bytes; |
| 1985 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1986 | } |
| 1987 | return 0; |
| 1988 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1989 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1990 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 1991 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1992 | struct btrfs_block_group_cache *cache; |
| 1993 | |
| 1994 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 1995 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1996 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1997 | |
| 1998 | return cache->key.objectid; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1999 | } |
| 2000 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2001 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2002 | u64 bytenr, u64 num, int pin) |
| 2003 | { |
| 2004 | u64 len; |
| 2005 | struct btrfs_block_group_cache *cache; |
| 2006 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2007 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2008 | WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2009 | if (pin) { |
| 2010 | set_extent_dirty(&fs_info->pinned_extents, |
| 2011 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2012 | } else { |
| 2013 | clear_extent_dirty(&fs_info->pinned_extents, |
| 2014 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2015 | } |
| 2016 | while (num > 0) { |
| 2017 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2018 | BUG_ON(!cache); |
| 2019 | len = min(num, cache->key.offset - |
| 2020 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2021 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2022 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2023 | spin_lock(&cache->lock); |
| 2024 | cache->pinned += len; |
| 2025 | cache->space_info->bytes_pinned += len; |
| 2026 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2027 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2028 | fs_info->total_pinned += len; |
| 2029 | } else { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2030 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2031 | spin_lock(&cache->lock); |
| 2032 | cache->pinned -= len; |
| 2033 | cache->space_info->bytes_pinned -= len; |
| 2034 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2035 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2036 | fs_info->total_pinned -= len; |
| 2037 | } |
| 2038 | bytenr += len; |
| 2039 | num -= len; |
| 2040 | } |
| 2041 | return 0; |
| 2042 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2043 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2044 | static int update_reserved_extents(struct btrfs_root *root, |
| 2045 | u64 bytenr, u64 num, int reserve) |
| 2046 | { |
| 2047 | u64 len; |
| 2048 | struct btrfs_block_group_cache *cache; |
| 2049 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2050 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2051 | while (num > 0) { |
| 2052 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 2053 | BUG_ON(!cache); |
| 2054 | len = min(num, cache->key.offset - |
| 2055 | (bytenr - cache->key.objectid)); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2056 | |
| 2057 | spin_lock(&cache->space_info->lock); |
| 2058 | spin_lock(&cache->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2059 | if (reserve) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2060 | cache->reserved += len; |
| 2061 | cache->space_info->bytes_reserved += len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2062 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2063 | cache->reserved -= len; |
| 2064 | cache->space_info->bytes_reserved -= len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2065 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2066 | spin_unlock(&cache->lock); |
| 2067 | spin_unlock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2068 | bytenr += len; |
| 2069 | num -= len; |
| 2070 | } |
| 2071 | return 0; |
| 2072 | } |
| 2073 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2074 | int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2075 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2076 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2077 | u64 start; |
| 2078 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2079 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2080 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2081 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2082 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2083 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2084 | ret = find_first_extent_bit(pinned_extents, last, |
| 2085 | &start, &end, EXTENT_DIRTY); |
| 2086 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2087 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2088 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 2089 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2090 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2091 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 2096 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2097 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2098 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2099 | u64 start; |
| 2100 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2101 | int ret; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2102 | struct btrfs_block_group_cache *cache; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2103 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2104 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2105 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2106 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 2107 | EXTENT_DIRTY); |
| 2108 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2109 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2110 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2111 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2112 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2113 | if (cache->cached) |
| 2114 | btrfs_add_free_space(cache, start, end - start + 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2115 | if (need_resched()) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2116 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2117 | cond_resched(); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2118 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2119 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2120 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2121 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2122 | return 0; |
| 2123 | } |
| 2124 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2125 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2126 | struct btrfs_root *extent_root, int all) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2127 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2128 | u64 start; |
| 2129 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2130 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2131 | u64 search = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2132 | u64 skipped = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2133 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 2134 | struct btrfs_path *path; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2135 | struct pending_extent_op *extent_op, *tmp; |
| 2136 | struct list_head insert_list, update_list; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2137 | int ret; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2138 | int num_inserts = 0, max_inserts; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2139 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2140 | path = btrfs_alloc_path(); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2141 | INIT_LIST_HEAD(&insert_list); |
| 2142 | INIT_LIST_HEAD(&update_list); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2143 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2144 | max_inserts = extent_root->leafsize / |
| 2145 | (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) + |
| 2146 | sizeof(struct btrfs_extent_ref) + |
| 2147 | sizeof(struct btrfs_extent_item)); |
| 2148 | again: |
| 2149 | mutex_lock(&info->extent_ins_mutex); |
| 2150 | while (1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2151 | ret = find_first_extent_bit(&info->extent_ins, search, &start, |
| 2152 | &end, EXTENT_WRITEBACK); |
| 2153 | if (ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2154 | if (skipped && all && !num_inserts) { |
| 2155 | skipped = 0; |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame^] | 2156 | search = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2157 | continue; |
| 2158 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2159 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2160 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS); |
| 2164 | if (!ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2165 | skipped = 1; |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2166 | search = end + 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2167 | if (need_resched()) { |
| 2168 | mutex_unlock(&info->extent_ins_mutex); |
| 2169 | cond_resched(); |
| 2170 | mutex_lock(&info->extent_ins_mutex); |
| 2171 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2172 | continue; |
| 2173 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2174 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2175 | ret = get_state_private(&info->extent_ins, start, &priv); |
| 2176 | BUG_ON(ret); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2177 | extent_op = (struct pending_extent_op *)(unsigned long) priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2178 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2179 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2180 | num_inserts++; |
| 2181 | list_add_tail(&extent_op->list, &insert_list); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2182 | search = end + 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2183 | if (num_inserts == max_inserts) { |
| 2184 | mutex_unlock(&info->extent_ins_mutex); |
| 2185 | break; |
| 2186 | } |
| 2187 | } else if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 2188 | list_add_tail(&extent_op->list, &update_list); |
| 2189 | search = end + 1; |
| 2190 | } else { |
| 2191 | BUG(); |
| 2192 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2193 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2194 | |
| 2195 | /* |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame^] | 2196 | * process the update list, clear the writeback bit for it, and if |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2197 | * somebody marked this thing for deletion then just unlock it and be |
| 2198 | * done, the free_extents will handle it |
| 2199 | */ |
| 2200 | mutex_lock(&info->extent_ins_mutex); |
| 2201 | list_for_each_entry_safe(extent_op, tmp, &update_list, list) { |
| 2202 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, |
| 2203 | extent_op->bytenr + extent_op->num_bytes - 1, |
| 2204 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2205 | if (extent_op->del) { |
| 2206 | list_del_init(&extent_op->list); |
| 2207 | unlock_extent(&info->extent_ins, extent_op->bytenr, |
| 2208 | extent_op->bytenr + extent_op->num_bytes |
| 2209 | - 1, GFP_NOFS); |
| 2210 | kfree(extent_op); |
| 2211 | } |
| 2212 | } |
| 2213 | mutex_unlock(&info->extent_ins_mutex); |
| 2214 | |
| 2215 | /* |
| 2216 | * still have things left on the update list, go ahead an update |
| 2217 | * everything |
| 2218 | */ |
| 2219 | if (!list_empty(&update_list)) { |
| 2220 | ret = update_backrefs(trans, extent_root, path, &update_list); |
| 2221 | BUG_ON(ret); |
| 2222 | } |
| 2223 | |
| 2224 | /* |
| 2225 | * if no inserts need to be done, but we skipped some extents and we |
| 2226 | * need to make sure everything is cleaned then reset everything and |
| 2227 | * go back to the beginning |
| 2228 | */ |
| 2229 | if (!num_inserts && all && skipped) { |
| 2230 | search = 0; |
| 2231 | skipped = 0; |
| 2232 | INIT_LIST_HEAD(&update_list); |
| 2233 | INIT_LIST_HEAD(&insert_list); |
| 2234 | goto again; |
| 2235 | } else if (!num_inserts) { |
| 2236 | goto out; |
| 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * process the insert extents list. Again if we are deleting this |
| 2241 | * extent, then just unlock it, pin down the bytes if need be, and be |
| 2242 | * done with it. Saves us from having to actually insert the extent |
| 2243 | * into the tree and then subsequently come along and delete it |
| 2244 | */ |
| 2245 | mutex_lock(&info->extent_ins_mutex); |
| 2246 | list_for_each_entry_safe(extent_op, tmp, &insert_list, list) { |
| 2247 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, |
| 2248 | extent_op->bytenr + extent_op->num_bytes - 1, |
| 2249 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2250 | if (extent_op->del) { |
| 2251 | list_del_init(&extent_op->list); |
| 2252 | unlock_extent(&info->extent_ins, extent_op->bytenr, |
| 2253 | extent_op->bytenr + extent_op->num_bytes |
| 2254 | - 1, GFP_NOFS); |
| 2255 | |
| 2256 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 2257 | ret = pin_down_bytes(trans, extent_root, |
| 2258 | extent_op->bytenr, |
| 2259 | extent_op->num_bytes, 0); |
| 2260 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 2261 | |
| 2262 | ret = update_block_group(trans, extent_root, |
| 2263 | extent_op->bytenr, |
| 2264 | extent_op->num_bytes, |
| 2265 | 0, ret > 0); |
| 2266 | BUG_ON(ret); |
| 2267 | kfree(extent_op); |
| 2268 | num_inserts--; |
| 2269 | } |
| 2270 | } |
| 2271 | mutex_unlock(&info->extent_ins_mutex); |
| 2272 | |
| 2273 | ret = insert_extents(trans, extent_root, path, &insert_list, |
| 2274 | num_inserts); |
| 2275 | BUG_ON(ret); |
| 2276 | |
| 2277 | /* |
| 2278 | * if we broke out of the loop in order to insert stuff because we hit |
| 2279 | * the maximum number of inserts at a time we can handle, then loop |
| 2280 | * back and pick up where we left off |
| 2281 | */ |
| 2282 | if (num_inserts == max_inserts) { |
| 2283 | INIT_LIST_HEAD(&insert_list); |
| 2284 | INIT_LIST_HEAD(&update_list); |
| 2285 | num_inserts = 0; |
| 2286 | goto again; |
| 2287 | } |
| 2288 | |
| 2289 | /* |
| 2290 | * again, if we need to make absolutely sure there are no more pending |
| 2291 | * extent operations left and we know that we skipped some, go back to |
| 2292 | * the beginning and do it all again |
| 2293 | */ |
| 2294 | if (all && skipped) { |
| 2295 | INIT_LIST_HEAD(&insert_list); |
| 2296 | INIT_LIST_HEAD(&update_list); |
| 2297 | search = 0; |
| 2298 | skipped = 0; |
| 2299 | num_inserts = 0; |
| 2300 | goto again; |
| 2301 | } |
| 2302 | out: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2303 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2304 | return 0; |
| 2305 | } |
| 2306 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2307 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 2308 | struct btrfs_root *root, |
| 2309 | u64 bytenr, u64 num_bytes, int is_data) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2310 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2311 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2312 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2313 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2314 | if (is_data) |
| 2315 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2316 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2317 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 2318 | if (!buf) |
| 2319 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2320 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2321 | /* we can reuse a block if it hasn't been written |
| 2322 | * and it is from this transaction. We can't |
| 2323 | * reuse anything from the tree log root because |
| 2324 | * it has tiny sub-transactions. |
| 2325 | */ |
| 2326 | if (btrfs_buffer_uptodate(buf, 0) && |
| 2327 | btrfs_try_tree_lock(buf)) { |
| 2328 | u64 header_owner = btrfs_header_owner(buf); |
| 2329 | u64 header_transid = btrfs_header_generation(buf); |
| 2330 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2331 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2332 | header_transid == trans->transid && |
| 2333 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 2334 | clean_tree_block(NULL, root, buf); |
| 2335 | btrfs_tree_unlock(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2336 | free_extent_buffer(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2337 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2338 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2339 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2340 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2341 | free_extent_buffer(buf); |
| 2342 | pinit: |
| 2343 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 2344 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2345 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2346 | return 0; |
| 2347 | } |
| 2348 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2349 | /* |
| 2350 | * remove an extent from the root, returns 0 on success |
| 2351 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2352 | static int __free_extent(struct btrfs_trans_handle *trans, |
| 2353 | struct btrfs_root *root, |
| 2354 | u64 bytenr, u64 num_bytes, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2355 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2356 | u64 owner_objectid, int pin, int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2357 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2358 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2359 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2360 | struct btrfs_fs_info *info = root->fs_info; |
| 2361 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2362 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2363 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2364 | int extent_slot = 0; |
| 2365 | int found_extent = 0; |
| 2366 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2367 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2368 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2369 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2370 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2371 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2372 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2373 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2374 | if (!path) |
| 2375 | return -ENOMEM; |
| 2376 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2377 | path->reada = 1; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2378 | ret = lookup_extent_backref(trans, extent_root, path, |
| 2379 | bytenr, parent, root_objectid, |
| 2380 | ref_generation, owner_objectid, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2381 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2382 | struct btrfs_key found_key; |
| 2383 | extent_slot = path->slots[0]; |
| 2384 | while(extent_slot > 0) { |
| 2385 | extent_slot--; |
| 2386 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2387 | extent_slot); |
| 2388 | if (found_key.objectid != bytenr) |
| 2389 | break; |
| 2390 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2391 | found_key.offset == num_bytes) { |
| 2392 | found_extent = 1; |
| 2393 | break; |
| 2394 | } |
| 2395 | if (path->slots[0] - extent_slot > 5) |
| 2396 | break; |
| 2397 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2398 | if (!found_extent) { |
| 2399 | ret = remove_extent_backref(trans, extent_root, path); |
| 2400 | BUG_ON(ret); |
| 2401 | btrfs_release_path(extent_root, path); |
| 2402 | ret = btrfs_search_slot(trans, extent_root, |
| 2403 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2404 | if (ret) { |
| 2405 | printk(KERN_ERR "umm, got %d back from search" |
| 2406 | ", was looking for %Lu\n", ret, |
| 2407 | bytenr); |
| 2408 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2409 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2410 | BUG_ON(ret); |
| 2411 | extent_slot = path->slots[0]; |
| 2412 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2413 | } else { |
| 2414 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2415 | WARN_ON(1); |
| 2416 | printk("Unable to find ref byte nr %Lu root %Lu " |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2417 | "gen %Lu owner %Lu\n", bytenr, |
| 2418 | root_objectid, ref_generation, owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2419 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2420 | |
| 2421 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2422 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2423 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2424 | refs = btrfs_extent_refs(leaf, ei); |
| 2425 | BUG_ON(refs == 0); |
| 2426 | refs -= 1; |
| 2427 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2428 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2429 | btrfs_mark_buffer_dirty(leaf); |
| 2430 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2431 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2432 | struct btrfs_extent_ref *ref; |
| 2433 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 2434 | struct btrfs_extent_ref); |
| 2435 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2436 | /* if the back ref and the extent are next to each other |
| 2437 | * they get deleted below in one shot |
| 2438 | */ |
| 2439 | path->slots[0] = extent_slot; |
| 2440 | num_to_del = 2; |
| 2441 | } else if (found_extent) { |
| 2442 | /* otherwise delete the extent back ref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2443 | ret = remove_extent_backref(trans, extent_root, path); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2444 | BUG_ON(ret); |
| 2445 | /* if refs are 0, we need to setup the path for deletion */ |
| 2446 | if (refs == 0) { |
| 2447 | btrfs_release_path(extent_root, path); |
| 2448 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 2449 | -1, 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2450 | BUG_ON(ret); |
| 2451 | } |
| 2452 | } |
| 2453 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2454 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2455 | u64 super_used; |
| 2456 | u64 root_used; |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2457 | #ifdef BIO_RW_DISCARD |
| 2458 | u64 map_length = num_bytes; |
| 2459 | struct btrfs_multi_bio *multi = NULL; |
| 2460 | #endif |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2461 | |
| 2462 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2463 | mutex_lock(&root->fs_info->pinned_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2464 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, |
| 2465 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2466 | mutex_unlock(&root->fs_info->pinned_mutex); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 2467 | if (ret > 0) |
| 2468 | mark_free = 1; |
| 2469 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2470 | } |
| 2471 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2472 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2473 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2474 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2475 | btrfs_set_super_bytes_used(&info->super_copy, |
| 2476 | super_used - num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2477 | spin_unlock_irq(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2478 | |
| 2479 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2480 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2481 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2482 | root_used - num_bytes); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2483 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 2484 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2485 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2486 | btrfs_release_path(extent_root, path); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2487 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2488 | mark_free); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2489 | BUG_ON(ret); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2490 | |
| 2491 | #ifdef BIO_RW_DISCARD |
| 2492 | /* Tell the block device(s) that the sectors can be discarded */ |
| 2493 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 2494 | bytenr, &map_length, &multi, 0); |
| 2495 | if (!ret) { |
| 2496 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 2497 | int i; |
| 2498 | |
| 2499 | if (map_length > num_bytes) |
| 2500 | map_length = num_bytes; |
| 2501 | |
| 2502 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 2503 | blkdev_issue_discard(stripe->dev->bdev, |
| 2504 | stripe->physical >> 9, |
| 2505 | map_length >> 9); |
| 2506 | } |
| 2507 | kfree(multi); |
| 2508 | } |
| 2509 | #endif |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2510 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2511 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2512 | finish_current_insert(trans, extent_root, 0); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2513 | return ret; |
| 2514 | } |
| 2515 | |
| 2516 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2517 | * find all the blocks marked as pending in the radix tree and remove |
| 2518 | * them from the extent map |
| 2519 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2520 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2521 | btrfs_root *extent_root, int all) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2522 | { |
| 2523 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2524 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2525 | u64 start; |
| 2526 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2527 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2528 | u64 search = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2529 | int nr = 0, skipped = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2530 | struct extent_io_tree *pending_del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2531 | struct extent_io_tree *extent_ins; |
| 2532 | struct pending_extent_op *extent_op; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2533 | struct btrfs_fs_info *info = extent_root->fs_info; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2534 | struct list_head delete_list; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2535 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2536 | INIT_LIST_HEAD(&delete_list); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2537 | extent_ins = &extent_root->fs_info->extent_ins; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2538 | pending_del = &extent_root->fs_info->pending_del; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2539 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2540 | again: |
| 2541 | mutex_lock(&info->extent_ins_mutex); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2542 | while(1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2543 | ret = find_first_extent_bit(pending_del, search, &start, &end, |
| 2544 | EXTENT_WRITEBACK); |
| 2545 | if (ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2546 | if (all && skipped && !nr) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2547 | search = 0; |
| 2548 | continue; |
| 2549 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2550 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2551 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | ret = try_lock_extent(extent_ins, start, end, GFP_NOFS); |
| 2555 | if (!ret) { |
| 2556 | search = end+1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2557 | skipped = 1; |
| 2558 | |
| 2559 | if (need_resched()) { |
| 2560 | mutex_unlock(&info->extent_ins_mutex); |
| 2561 | cond_resched(); |
| 2562 | mutex_lock(&info->extent_ins_mutex); |
| 2563 | } |
| 2564 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2565 | continue; |
| 2566 | } |
| 2567 | BUG_ON(ret < 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2568 | |
| 2569 | ret = get_state_private(pending_del, start, &priv); |
| 2570 | BUG_ON(ret); |
| 2571 | extent_op = (struct pending_extent_op *)(unsigned long)priv; |
| 2572 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2573 | clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2574 | GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2575 | if (!test_range_bit(extent_ins, start, end, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2576 | EXTENT_WRITEBACK, 0)) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2577 | list_add_tail(&extent_op->list, &delete_list); |
| 2578 | nr++; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2579 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2580 | kfree(extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2581 | |
| 2582 | ret = get_state_private(&info->extent_ins, start, |
| 2583 | &priv); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2584 | BUG_ON(ret); |
| 2585 | extent_op = (struct pending_extent_op *) |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2586 | (unsigned long)priv; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2587 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2588 | clear_extent_bits(&info->extent_ins, start, end, |
| 2589 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2590 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2591 | if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 2592 | list_add_tail(&extent_op->list, &delete_list); |
| 2593 | search = end + 1; |
| 2594 | nr++; |
| 2595 | continue; |
| 2596 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2597 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2598 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 2599 | ret = pin_down_bytes(trans, extent_root, start, |
| 2600 | end + 1 - start, 0); |
| 2601 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 2602 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2603 | ret = update_block_group(trans, extent_root, start, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2604 | end + 1 - start, 0, ret > 0); |
| 2605 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2606 | unlock_extent(extent_ins, start, end, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2607 | BUG_ON(ret); |
| 2608 | kfree(extent_op); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2609 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2610 | if (ret) |
| 2611 | err = ret; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2612 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2613 | search = end + 1; |
| 2614 | |
| 2615 | if (need_resched()) { |
| 2616 | mutex_unlock(&info->extent_ins_mutex); |
| 2617 | cond_resched(); |
| 2618 | mutex_lock(&info->extent_ins_mutex); |
| 2619 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2620 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2621 | |
| 2622 | if (nr) { |
| 2623 | ret = free_extents(trans, extent_root, &delete_list); |
| 2624 | BUG_ON(ret); |
| 2625 | } |
| 2626 | |
| 2627 | if (all && skipped) { |
| 2628 | INIT_LIST_HEAD(&delete_list); |
| 2629 | search = 0; |
| 2630 | nr = 0; |
| 2631 | goto again; |
| 2632 | } |
| 2633 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2634 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | /* |
| 2638 | * remove an extent from the root, returns 0 on success |
| 2639 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2640 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2641 | struct btrfs_root *root, |
| 2642 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2643 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2644 | u64 owner_objectid, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2645 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2646 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2647 | int pending_ret; |
| 2648 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2649 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2650 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2651 | if (root == extent_root) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2652 | struct pending_extent_op *extent_op = NULL; |
| 2653 | |
| 2654 | mutex_lock(&root->fs_info->extent_ins_mutex); |
| 2655 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
| 2656 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
| 2657 | u64 priv; |
| 2658 | ret = get_state_private(&root->fs_info->extent_ins, |
| 2659 | bytenr, &priv); |
| 2660 | BUG_ON(ret); |
| 2661 | extent_op = (struct pending_extent_op *) |
| 2662 | (unsigned long)priv; |
| 2663 | |
| 2664 | extent_op->del = 1; |
| 2665 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
| 2666 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
| 2667 | return 0; |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | if (extent_op) { |
| 2672 | ref_generation = extent_op->orig_generation; |
| 2673 | parent = extent_op->orig_parent; |
| 2674 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2675 | |
| 2676 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2677 | BUG_ON(!extent_op); |
| 2678 | |
| 2679 | extent_op->type = PENDING_EXTENT_DELETE; |
| 2680 | extent_op->bytenr = bytenr; |
| 2681 | extent_op->num_bytes = num_bytes; |
| 2682 | extent_op->parent = parent; |
| 2683 | extent_op->orig_parent = parent; |
| 2684 | extent_op->generation = ref_generation; |
| 2685 | extent_op->orig_generation = ref_generation; |
| 2686 | extent_op->level = (int)owner_objectid; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2687 | INIT_LIST_HEAD(&extent_op->list); |
| 2688 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2689 | |
| 2690 | set_extent_bits(&root->fs_info->pending_del, |
| 2691 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2692 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2693 | set_state_private(&root->fs_info->pending_del, |
| 2694 | bytenr, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2695 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2696 | return 0; |
| 2697 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2698 | /* if metadata always pin */ |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2699 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2700 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2701 | struct btrfs_block_group_cache *cache; |
| 2702 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2703 | /* btrfs_free_reserved_extent */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2704 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2705 | BUG_ON(!cache); |
| 2706 | btrfs_add_free_space(cache, bytenr, num_bytes); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2707 | update_reserved_extents(root, bytenr, num_bytes, 0); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2708 | return 0; |
| 2709 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2710 | pin = 1; |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2711 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2712 | |
| 2713 | /* if data pin when any transaction has committed this */ |
| 2714 | if (ref_generation != trans->transid) |
| 2715 | pin = 1; |
| 2716 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2717 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2718 | root_objectid, ref_generation, |
| 2719 | owner_objectid, pin, pin == 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 2720 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2721 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 2722 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2723 | return ret ? ret : pending_ret; |
| 2724 | } |
| 2725 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2726 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2727 | struct btrfs_root *root, |
| 2728 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2729 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2730 | u64 owner_objectid, int pin) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2731 | { |
| 2732 | int ret; |
| 2733 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2734 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2735 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2736 | owner_objectid, pin); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2737 | return ret; |
| 2738 | } |
| 2739 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2740 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 2741 | { |
| 2742 | u64 mask = ((u64)root->stripesize - 1); |
| 2743 | u64 ret = (val + mask) & ~mask; |
| 2744 | return ret; |
| 2745 | } |
| 2746 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2747 | /* |
| 2748 | * walks the btree of allocated extents and find a hole of a given size. |
| 2749 | * The key ins is changed to record the hole: |
| 2750 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2751 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2752 | * ins->offset == number of blocks |
| 2753 | * Any available blocks before search_start are skipped. |
| 2754 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2755 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
| 2756 | struct btrfs_root *orig_root, |
| 2757 | u64 num_bytes, u64 empty_size, |
| 2758 | u64 search_start, u64 search_end, |
| 2759 | u64 hint_byte, struct btrfs_key *ins, |
| 2760 | u64 exclude_start, u64 exclude_nr, |
| 2761 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2762 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2763 | int ret = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2764 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2765 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2766 | u64 *last_ptr = NULL; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2767 | u64 last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2768 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2769 | int chunk_alloc_done = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2770 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2771 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2772 | struct list_head *head = NULL, *cur = NULL; |
| 2773 | int loop = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2774 | int extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2775 | struct btrfs_space_info *space_info; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2776 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2777 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2778 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2779 | ins->objectid = 0; |
| 2780 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2781 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2782 | if (orig_root->ref_cows || empty_size) |
| 2783 | allowed_chunk_alloc = 1; |
| 2784 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2785 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 2786 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2787 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2788 | } |
| 2789 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2790 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2791 | last_ptr = &root->fs_info->last_data_alloc; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2792 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2793 | if (last_ptr) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2794 | if (*last_ptr) { |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2795 | hint_byte = *last_ptr; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2796 | last_wanted = *last_ptr; |
| 2797 | } else |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2798 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2799 | } else { |
| 2800 | empty_cluster = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2801 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2802 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2803 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2804 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2805 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2806 | last_wanted = 0; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2807 | empty_size += empty_cluster; |
| 2808 | } |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2809 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2810 | total_needed += empty_size; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2811 | block_group = btrfs_lookup_block_group(root->fs_info, search_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2812 | if (!block_group) |
| 2813 | block_group = btrfs_lookup_first_block_group(root->fs_info, |
| 2814 | search_start); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2815 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2816 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2817 | down_read(&space_info->groups_sem); |
| 2818 | while (1) { |
| 2819 | struct btrfs_free_space *free_space; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2820 | /* |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2821 | * the only way this happens if our hint points to a block |
| 2822 | * group thats not of the proper type, while looping this |
| 2823 | * should never happen |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2824 | */ |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2825 | if (empty_size) |
| 2826 | extra_loop = 1; |
| 2827 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2828 | if (!block_group) |
| 2829 | goto new_group_no_lock; |
| 2830 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2831 | mutex_lock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2832 | if (unlikely(!block_group_bits(block_group, data))) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2833 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2834 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2835 | ret = cache_block_group(root, block_group); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2836 | if (ret) { |
| 2837 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2838 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2839 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2840 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2841 | if (block_group->ro) |
| 2842 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2843 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2844 | free_space = btrfs_find_free_space(block_group, search_start, |
| 2845 | total_needed); |
| 2846 | if (free_space) { |
| 2847 | u64 start = block_group->key.objectid; |
| 2848 | u64 end = block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2849 | block_group->key.offset; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2850 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2851 | search_start = stripe_align(root, free_space->offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2852 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2853 | /* move on to the next group */ |
| 2854 | if (search_start + num_bytes >= search_end) |
| 2855 | goto new_group; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2856 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2857 | /* move on to the next group */ |
| 2858 | if (search_start + num_bytes > end) |
| 2859 | goto new_group; |
| 2860 | |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2861 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 2862 | total_needed += empty_cluster; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2863 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2864 | last_wanted = 0; |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 2865 | /* |
| 2866 | * if search_start is still in this block group |
| 2867 | * then we just re-search this block group |
| 2868 | */ |
| 2869 | if (search_start >= start && |
| 2870 | search_start < end) { |
| 2871 | mutex_unlock(&block_group->alloc_mutex); |
| 2872 | continue; |
| 2873 | } |
| 2874 | |
| 2875 | /* else we go to the next block group */ |
| 2876 | goto new_group; |
| 2877 | } |
| 2878 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2879 | if (exclude_nr > 0 && |
| 2880 | (search_start + num_bytes > exclude_start && |
| 2881 | search_start < exclude_start + exclude_nr)) { |
| 2882 | search_start = exclude_start + exclude_nr; |
| 2883 | /* |
| 2884 | * if search_start is still in this block group |
| 2885 | * then we just re-search this block group |
| 2886 | */ |
| 2887 | if (search_start >= start && |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2888 | search_start < end) { |
| 2889 | mutex_unlock(&block_group->alloc_mutex); |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2890 | last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2891 | continue; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2892 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2893 | |
| 2894 | /* else we go to the next block group */ |
| 2895 | goto new_group; |
| 2896 | } |
| 2897 | |
| 2898 | ins->objectid = search_start; |
| 2899 | ins->offset = num_bytes; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2900 | |
| 2901 | btrfs_remove_free_space_lock(block_group, search_start, |
| 2902 | num_bytes); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2903 | /* we are all good, lets return */ |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2904 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2905 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2906 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2907 | new_group: |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2908 | mutex_unlock(&block_group->alloc_mutex); |
| 2909 | new_group_no_lock: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2910 | /* don't try to compare new allocations against the |
| 2911 | * last allocation any more |
| 2912 | */ |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2913 | last_wanted = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2914 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2915 | /* |
| 2916 | * Here's how this works. |
| 2917 | * loop == 0: we were searching a block group via a hint |
| 2918 | * and didn't find anything, so we start at |
| 2919 | * the head of the block groups and keep searching |
| 2920 | * loop == 1: we're searching through all of the block groups |
| 2921 | * if we hit the head again we have searched |
| 2922 | * all of the block groups for this space and we |
| 2923 | * need to try and allocate, if we cant error out. |
| 2924 | * loop == 2: we allocated more space and are looping through |
| 2925 | * all of the block groups again. |
| 2926 | */ |
| 2927 | if (loop == 0) { |
| 2928 | head = &space_info->block_groups; |
| 2929 | cur = head->next; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2930 | loop++; |
| 2931 | } else if (loop == 1 && cur == head) { |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2932 | int keep_going; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2933 | |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2934 | /* at this point we give up on the empty_size |
| 2935 | * allocations and just try to allocate the min |
| 2936 | * space. |
| 2937 | * |
| 2938 | * The extra_loop field was set if an empty_size |
| 2939 | * allocation was attempted above, and if this |
| 2940 | * is try we need to try the loop again without |
| 2941 | * the additional empty_size. |
| 2942 | */ |
Chris Mason | 5b7c3fc | 2008-11-10 07:26:33 -0500 | [diff] [blame] | 2943 | total_needed -= empty_size; |
| 2944 | empty_size = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2945 | keep_going = extra_loop; |
| 2946 | loop++; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2947 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2948 | if (allowed_chunk_alloc && !chunk_alloc_done) { |
| 2949 | up_read(&space_info->groups_sem); |
| 2950 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 2951 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2952 | down_read(&space_info->groups_sem); |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 2953 | if (ret < 0) |
| 2954 | goto loop_check; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2955 | head = &space_info->block_groups; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2956 | /* |
| 2957 | * we've allocated a new chunk, keep |
| 2958 | * trying |
| 2959 | */ |
| 2960 | keep_going = 1; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2961 | chunk_alloc_done = 1; |
| 2962 | } else if (!allowed_chunk_alloc) { |
| 2963 | space_info->force_alloc = 1; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2964 | } |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 2965 | loop_check: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2966 | if (keep_going) { |
| 2967 | cur = head->next; |
| 2968 | extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2969 | } else { |
| 2970 | break; |
| 2971 | } |
| 2972 | } else if (cur == head) { |
| 2973 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2974 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2975 | |
| 2976 | block_group = list_entry(cur, struct btrfs_block_group_cache, |
| 2977 | list); |
| 2978 | search_start = block_group->key.objectid; |
| 2979 | cur = cur->next; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 2980 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2981 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2982 | /* we found what we needed */ |
| 2983 | if (ins->objectid) { |
| 2984 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
| 2985 | trans->block_group = block_group; |
| 2986 | |
| 2987 | if (last_ptr) |
| 2988 | *last_ptr = ins->objectid + ins->offset; |
| 2989 | ret = 0; |
| 2990 | } else if (!ret) { |
Josef Bacik | 4ce4cb5 | 2008-11-17 21:12:00 -0500 | [diff] [blame] | 2991 | printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu," |
| 2992 | " loop %d, allowed_alloc %d\n", total_needed, num_bytes, |
| 2993 | loop, allowed_chunk_alloc); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2994 | ret = -ENOSPC; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 2995 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2996 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2997 | up_read(&space_info->groups_sem); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2998 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2999 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3000 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3001 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 3002 | { |
| 3003 | struct btrfs_block_group_cache *cache; |
| 3004 | struct list_head *l; |
| 3005 | |
| 3006 | printk(KERN_INFO "space_info has %Lu free, is %sfull\n", |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3007 | info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 3008 | info->bytes_reserved, (info->full) ? "" : "not "); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3009 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3010 | down_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3011 | list_for_each(l, &info->block_groups) { |
| 3012 | cache = list_entry(l, struct btrfs_block_group_cache, list); |
| 3013 | spin_lock(&cache->lock); |
| 3014 | printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used " |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3015 | "%Lu pinned %Lu reserved\n", |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3016 | cache->key.objectid, cache->key.offset, |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3017 | btrfs_block_group_used(&cache->item), |
| 3018 | cache->pinned, cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3019 | btrfs_dump_free_space(cache, bytes); |
| 3020 | spin_unlock(&cache->lock); |
| 3021 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3022 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3023 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3024 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3025 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3026 | struct btrfs_root *root, |
| 3027 | u64 num_bytes, u64 min_alloc_size, |
| 3028 | u64 empty_size, u64 hint_byte, |
| 3029 | u64 search_end, struct btrfs_key *ins, |
| 3030 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3031 | { |
| 3032 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 3033 | u64 search_start = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3034 | u64 alloc_profile; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3035 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3036 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3037 | if (data) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3038 | alloc_profile = info->avail_data_alloc_bits & |
| 3039 | info->data_alloc_profile; |
| 3040 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3041 | } else if (root == root->fs_info->chunk_root) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3042 | alloc_profile = info->avail_system_alloc_bits & |
| 3043 | info->system_alloc_profile; |
| 3044 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3045 | } else { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3046 | alloc_profile = info->avail_metadata_alloc_bits & |
| 3047 | info->metadata_alloc_profile; |
| 3048 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3049 | } |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3050 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3051 | data = btrfs_reduce_alloc_profile(root, data); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3052 | /* |
| 3053 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 3054 | * is not called recursively on allocations |
| 3055 | */ |
| 3056 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3057 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3058 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3059 | 2 * 1024 * 1024, |
| 3060 | BTRFS_BLOCK_GROUP_METADATA | |
| 3061 | (info->metadata_alloc_profile & |
| 3062 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3063 | } |
| 3064 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3065 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3066 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3067 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3068 | WARN_ON(num_bytes < root->sectorsize); |
| 3069 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 3070 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3071 | trans->alloc_exclude_start, |
| 3072 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3073 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3074 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 3075 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3076 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3077 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3078 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3079 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3080 | goto again; |
| 3081 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3082 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3083 | struct btrfs_space_info *sinfo; |
| 3084 | |
| 3085 | sinfo = __find_space_info(root->fs_info, data); |
| 3086 | printk("allocation failed flags %Lu, wanted %Lu\n", |
| 3087 | data, num_bytes); |
| 3088 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3089 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3090 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3091 | |
| 3092 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3093 | } |
| 3094 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3095 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 3096 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3097 | struct btrfs_block_group_cache *cache; |
| 3098 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3099 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 3100 | if (!cache) { |
| 3101 | printk(KERN_ERR "Unable to find block group for %Lu\n", start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3102 | return -ENOSPC; |
| 3103 | } |
| 3104 | btrfs_add_free_space(cache, start, len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3105 | update_reserved_extents(root, start, len, 0); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3106 | return 0; |
| 3107 | } |
| 3108 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3109 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3110 | struct btrfs_root *root, |
| 3111 | u64 num_bytes, u64 min_alloc_size, |
| 3112 | u64 empty_size, u64 hint_byte, |
| 3113 | u64 search_end, struct btrfs_key *ins, |
| 3114 | u64 data) |
| 3115 | { |
| 3116 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3117 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 3118 | empty_size, hint_byte, search_end, ins, |
| 3119 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3120 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3121 | return ret; |
| 3122 | } |
| 3123 | |
| 3124 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3125 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3126 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3127 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3128 | { |
| 3129 | int ret; |
| 3130 | int pending_ret; |
| 3131 | u64 super_used; |
| 3132 | u64 root_used; |
| 3133 | u64 num_bytes = ins->offset; |
| 3134 | u32 sizes[2]; |
| 3135 | struct btrfs_fs_info *info = root->fs_info; |
| 3136 | struct btrfs_root *extent_root = info->extent_root; |
| 3137 | struct btrfs_extent_item *extent_item; |
| 3138 | struct btrfs_extent_ref *ref; |
| 3139 | struct btrfs_path *path; |
| 3140 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 3141 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3142 | if (parent == 0) |
| 3143 | parent = ins->objectid; |
| 3144 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3145 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3146 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3147 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 3148 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3149 | spin_unlock_irq(&info->delalloc_lock); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3150 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3151 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3152 | root_used = btrfs_root_used(&root->root_item); |
| 3153 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3154 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3155 | if (root == extent_root) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3156 | struct pending_extent_op *extent_op; |
| 3157 | |
| 3158 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 3159 | BUG_ON(!extent_op); |
| 3160 | |
| 3161 | extent_op->type = PENDING_EXTENT_INSERT; |
| 3162 | extent_op->bytenr = ins->objectid; |
| 3163 | extent_op->num_bytes = ins->offset; |
| 3164 | extent_op->parent = parent; |
| 3165 | extent_op->orig_parent = 0; |
| 3166 | extent_op->generation = ref_generation; |
| 3167 | extent_op->orig_generation = 0; |
| 3168 | extent_op->level = (int)owner; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3169 | INIT_LIST_HEAD(&extent_op->list); |
| 3170 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3171 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3172 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3173 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 3174 | ins->objectid + ins->offset - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3175 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3176 | set_state_private(&root->fs_info->extent_ins, |
| 3177 | ins->objectid, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3178 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3179 | goto update_block; |
| 3180 | } |
| 3181 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3182 | memcpy(&keys[0], ins, sizeof(*ins)); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3183 | keys[1].objectid = ins->objectid; |
| 3184 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3185 | keys[1].offset = parent; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3186 | sizes[0] = sizeof(*extent_item); |
| 3187 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3188 | |
| 3189 | path = btrfs_alloc_path(); |
| 3190 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3191 | |
| 3192 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 3193 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3194 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3195 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3196 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3197 | struct btrfs_extent_item); |
| 3198 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); |
| 3199 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 3200 | struct btrfs_extent_ref); |
| 3201 | |
| 3202 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 3203 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 3204 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3205 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3206 | |
| 3207 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3208 | |
| 3209 | trans->alloc_exclude_start = 0; |
| 3210 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3211 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 3212 | finish_current_insert(trans, extent_root, 0); |
| 3213 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3214 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3215 | if (ret) |
| 3216 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3217 | if (pending_ret) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3218 | ret = pending_ret; |
| 3219 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3220 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3221 | |
| 3222 | update_block: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3223 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 3224 | if (ret) { |
| 3225 | printk("update block group failed for %Lu %Lu\n", |
| 3226 | ins->objectid, ins->offset); |
| 3227 | BUG(); |
| 3228 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3229 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3230 | return ret; |
| 3231 | } |
| 3232 | |
| 3233 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3234 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3235 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3236 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3237 | { |
| 3238 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 3239 | |
| 3240 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 3241 | return 0; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3242 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 3243 | ref_generation, owner, ins); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3244 | update_reserved_extents(root, ins->objectid, ins->offset, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3245 | return ret; |
| 3246 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3247 | |
| 3248 | /* |
| 3249 | * this is used by the tree logging recovery code. It records that |
| 3250 | * an extent has been allocated and makes sure to clear the free |
| 3251 | * space cache bits as well |
| 3252 | */ |
| 3253 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3254 | struct btrfs_root *root, u64 parent, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3255 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3256 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3257 | { |
| 3258 | int ret; |
| 3259 | struct btrfs_block_group_cache *block_group; |
| 3260 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3261 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3262 | mutex_lock(&block_group->alloc_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3263 | cache_block_group(root, block_group); |
| 3264 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3265 | ret = btrfs_remove_free_space_lock(block_group, ins->objectid, |
| 3266 | ins->offset); |
| 3267 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3268 | BUG_ON(ret); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3269 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 3270 | ref_generation, owner, ins); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3271 | return ret; |
| 3272 | } |
| 3273 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3274 | /* |
| 3275 | * finds a free extent and does all the dirty work required for allocation |
| 3276 | * returns the key for the extent through ins, and a tree buffer for |
| 3277 | * the first block of the extent through buf. |
| 3278 | * |
| 3279 | * returns 0 if everything worked, non-zero otherwise. |
| 3280 | */ |
| 3281 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 3282 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3283 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3284 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3285 | u64 owner_objectid, u64 empty_size, u64 hint_byte, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3286 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 3287 | { |
| 3288 | int ret; |
| 3289 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3290 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 3291 | min_alloc_size, empty_size, hint_byte, |
| 3292 | search_end, ins, data); |
| 3293 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3294 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3295 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
| 3296 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3297 | owner_objectid, ins); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3298 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3299 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3300 | } else { |
| 3301 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3302 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3303 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3304 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3305 | |
| 3306 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 3307 | struct btrfs_root *root, |
| 3308 | u64 bytenr, u32 blocksize) |
| 3309 | { |
| 3310 | struct extent_buffer *buf; |
| 3311 | |
| 3312 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 3313 | if (!buf) |
| 3314 | return ERR_PTR(-ENOMEM); |
| 3315 | btrfs_set_header_generation(buf, trans->transid); |
| 3316 | btrfs_tree_lock(buf); |
| 3317 | clean_tree_block(trans, root, buf); |
| 3318 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3319 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 3320 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3321 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3322 | } else { |
| 3323 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 3324 | buf->start + buf->len - 1, GFP_NOFS); |
| 3325 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3326 | trans->blocks_used++; |
| 3327 | return buf; |
| 3328 | } |
| 3329 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3330 | /* |
| 3331 | * helper function to allocate a block for a given tree |
| 3332 | * returns the tree buffer or NULL. |
| 3333 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3334 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3335 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3336 | u32 blocksize, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3337 | u64 root_objectid, |
| 3338 | u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3339 | int level, |
| 3340 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3341 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3342 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3343 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3344 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3345 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3346 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3347 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3348 | root_objectid, ref_generation, level, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3349 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3350 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3351 | BUG_ON(ret > 0); |
| 3352 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3353 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 3354 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3355 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3356 | return buf; |
| 3357 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3358 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3359 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3360 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3361 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3362 | u64 leaf_owner; |
| 3363 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3364 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3365 | struct btrfs_file_extent_item *fi; |
| 3366 | int i; |
| 3367 | int nritems; |
| 3368 | int ret; |
| 3369 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3370 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 3371 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3372 | leaf_owner = btrfs_header_owner(leaf); |
| 3373 | leaf_generation = btrfs_header_generation(leaf); |
| 3374 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3375 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3376 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 3377 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3378 | |
| 3379 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 3380 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3381 | continue; |
| 3382 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3383 | if (btrfs_file_extent_type(leaf, fi) == |
| 3384 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 3385 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3386 | /* |
| 3387 | * FIXME make sure to insert a trans record that |
| 3388 | * repeats the snapshot del on crash |
| 3389 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3390 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 3391 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 3392 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3393 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3394 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3395 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3396 | leaf->start, leaf_owner, leaf_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3397 | key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3398 | BUG_ON(ret); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3399 | |
| 3400 | atomic_inc(&root->fs_info->throttle_gen); |
| 3401 | wake_up(&root->fs_info->transaction_throttle); |
| 3402 | cond_resched(); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3403 | } |
| 3404 | return 0; |
| 3405 | } |
| 3406 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3407 | static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3408 | struct btrfs_root *root, |
| 3409 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3410 | { |
| 3411 | int i; |
| 3412 | int ret; |
| 3413 | struct btrfs_extent_info *info = ref->extents; |
| 3414 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3415 | for (i = 0; i < ref->nritems; i++) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3416 | ret = __btrfs_free_extent(trans, root, info->bytenr, |
| 3417 | info->num_bytes, ref->bytenr, |
| 3418 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3419 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3420 | |
| 3421 | atomic_inc(&root->fs_info->throttle_gen); |
| 3422 | wake_up(&root->fs_info->transaction_throttle); |
| 3423 | cond_resched(); |
| 3424 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3425 | BUG_ON(ret); |
| 3426 | info++; |
| 3427 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3428 | |
| 3429 | return 0; |
| 3430 | } |
| 3431 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3432 | int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len, |
| 3433 | u32 *refs) |
| 3434 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3435 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3436 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3437 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3438 | BUG_ON(ret); |
| 3439 | |
| 3440 | #if 0 // some debugging code in case we see problems here |
| 3441 | /* if the refs count is one, it won't get increased again. But |
| 3442 | * if the ref count is > 1, someone may be decreasing it at |
| 3443 | * the same time we are. |
| 3444 | */ |
| 3445 | if (*refs != 1) { |
| 3446 | struct extent_buffer *eb = NULL; |
| 3447 | eb = btrfs_find_create_tree_block(root, start, len); |
| 3448 | if (eb) |
| 3449 | btrfs_tree_lock(eb); |
| 3450 | |
| 3451 | mutex_lock(&root->fs_info->alloc_mutex); |
| 3452 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 3453 | BUG_ON(ret); |
| 3454 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 3455 | |
| 3456 | if (eb) { |
| 3457 | btrfs_tree_unlock(eb); |
| 3458 | free_extent_buffer(eb); |
| 3459 | } |
| 3460 | if (*refs == 1) { |
| 3461 | printk("block %llu went down to one during drop_snap\n", |
| 3462 | (unsigned long long)start); |
| 3463 | } |
| 3464 | |
| 3465 | } |
| 3466 | #endif |
| 3467 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3468 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3469 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3473 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 3474 | * counts as it goes. |
| 3475 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3476 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
| 3477 | struct btrfs_root *root, |
| 3478 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3479 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3480 | u64 root_owner; |
| 3481 | u64 root_gen; |
| 3482 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3483 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3484 | struct extent_buffer *next; |
| 3485 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3486 | struct extent_buffer *parent; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3487 | struct btrfs_leaf_ref *ref; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3488 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3489 | int ret; |
| 3490 | u32 refs; |
| 3491 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3492 | WARN_ON(*level < 0); |
| 3493 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3494 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3495 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3496 | BUG_ON(ret); |
| 3497 | if (refs > 1) |
| 3498 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3499 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3500 | /* |
| 3501 | * walk down to the last node level and free all the leaves |
| 3502 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3503 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3504 | WARN_ON(*level < 0); |
| 3505 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3506 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3507 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3508 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3509 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3510 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3511 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3512 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3513 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3514 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3515 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3516 | BUG_ON(ret); |
| 3517 | break; |
| 3518 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3519 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3520 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3521 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3522 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3523 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3524 | BUG_ON(ret); |
| 3525 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3526 | parent = path->nodes[*level]; |
| 3527 | root_owner = btrfs_header_owner(parent); |
| 3528 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3529 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3530 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3531 | ret = __btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3532 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3533 | root_owner, root_gen, |
| 3534 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3535 | BUG_ON(ret); |
Chris Mason | 18e35e0 | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3536 | |
| 3537 | atomic_inc(&root->fs_info->throttle_gen); |
| 3538 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3539 | cond_resched(); |
Chris Mason | 18e35e0 | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3540 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3541 | continue; |
| 3542 | } |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3543 | /* |
| 3544 | * at this point, we have a single ref, and since the |
| 3545 | * only place referencing this extent is a dead root |
| 3546 | * the reference count should never go higher. |
| 3547 | * So, we don't need to check it again |
| 3548 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3549 | if (*level == 1) { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3550 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3551 | if (ref && ref->generation != ptr_gen) { |
| 3552 | btrfs_free_leaf_ref(root, ref); |
| 3553 | ref = NULL; |
| 3554 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3555 | if (ref) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3556 | ret = cache_drop_leaf_ref(trans, root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3557 | BUG_ON(ret); |
| 3558 | btrfs_remove_leaf_ref(root, ref); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 3559 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3560 | *level = 0; |
| 3561 | break; |
| 3562 | } |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3563 | if (printk_ratelimit()) { |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 3564 | printk("leaf ref miss for bytenr %llu\n", |
| 3565 | (unsigned long long)bytenr); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3566 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3567 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3568 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3569 | if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3570 | free_extent_buffer(next); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3571 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3572 | next = read_tree_block(root, bytenr, blocksize, |
| 3573 | ptr_gen); |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3574 | cond_resched(); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3575 | #if 0 |
| 3576 | /* |
| 3577 | * this is a debugging check and can go away |
| 3578 | * the ref should never go all the way down to 1 |
| 3579 | * at this point |
| 3580 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3581 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, |
| 3582 | &refs); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 3583 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3584 | WARN_ON(refs != 1); |
| 3585 | #endif |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 3586 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3587 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3588 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3589 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3590 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3591 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3592 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3593 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3594 | } |
| 3595 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3596 | WARN_ON(*level < 0); |
| 3597 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3598 | |
| 3599 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3600 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3601 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3602 | } else { |
| 3603 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3604 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3605 | } |
| 3606 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3607 | blocksize = btrfs_level_size(root, *level); |
| 3608 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3609 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3610 | |
| 3611 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3612 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3613 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3614 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3615 | path->nodes[*level] = NULL; |
| 3616 | *level += 1; |
| 3617 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3618 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3619 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3620 | return 0; |
| 3621 | } |
| 3622 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3623 | /* |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3624 | * helper function for drop_subtree, this function is similar to |
| 3625 | * walk_down_tree. The main difference is that it checks reference |
| 3626 | * counts while tree blocks are locked. |
| 3627 | */ |
| 3628 | static int noinline walk_down_subtree(struct btrfs_trans_handle *trans, |
| 3629 | struct btrfs_root *root, |
| 3630 | struct btrfs_path *path, int *level) |
| 3631 | { |
| 3632 | struct extent_buffer *next; |
| 3633 | struct extent_buffer *cur; |
| 3634 | struct extent_buffer *parent; |
| 3635 | u64 bytenr; |
| 3636 | u64 ptr_gen; |
| 3637 | u32 blocksize; |
| 3638 | u32 refs; |
| 3639 | int ret; |
| 3640 | |
| 3641 | cur = path->nodes[*level]; |
| 3642 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, |
| 3643 | &refs); |
| 3644 | BUG_ON(ret); |
| 3645 | if (refs > 1) |
| 3646 | goto out; |
| 3647 | |
| 3648 | while (*level >= 0) { |
| 3649 | cur = path->nodes[*level]; |
| 3650 | if (*level == 0) { |
| 3651 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
| 3652 | BUG_ON(ret); |
| 3653 | clean_tree_block(trans, root, cur); |
| 3654 | break; |
| 3655 | } |
| 3656 | if (path->slots[*level] >= btrfs_header_nritems(cur)) { |
| 3657 | clean_tree_block(trans, root, cur); |
| 3658 | break; |
| 3659 | } |
| 3660 | |
| 3661 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 3662 | blocksize = btrfs_level_size(root, *level - 1); |
| 3663 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
| 3664 | |
| 3665 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
| 3666 | btrfs_tree_lock(next); |
| 3667 | |
| 3668 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, |
| 3669 | &refs); |
| 3670 | BUG_ON(ret); |
| 3671 | if (refs > 1) { |
| 3672 | parent = path->nodes[*level]; |
| 3673 | ret = btrfs_free_extent(trans, root, bytenr, |
| 3674 | blocksize, parent->start, |
| 3675 | btrfs_header_owner(parent), |
| 3676 | btrfs_header_generation(parent), |
| 3677 | *level - 1, 1); |
| 3678 | BUG_ON(ret); |
| 3679 | path->slots[*level]++; |
| 3680 | btrfs_tree_unlock(next); |
| 3681 | free_extent_buffer(next); |
| 3682 | continue; |
| 3683 | } |
| 3684 | |
| 3685 | *level = btrfs_header_level(next); |
| 3686 | path->nodes[*level] = next; |
| 3687 | path->slots[*level] = 0; |
| 3688 | path->locks[*level] = 1; |
| 3689 | cond_resched(); |
| 3690 | } |
| 3691 | out: |
| 3692 | parent = path->nodes[*level + 1]; |
| 3693 | bytenr = path->nodes[*level]->start; |
| 3694 | blocksize = path->nodes[*level]->len; |
| 3695 | |
| 3696 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
| 3697 | parent->start, btrfs_header_owner(parent), |
| 3698 | btrfs_header_generation(parent), *level, 1); |
| 3699 | BUG_ON(ret); |
| 3700 | |
| 3701 | if (path->locks[*level]) { |
| 3702 | btrfs_tree_unlock(path->nodes[*level]); |
| 3703 | path->locks[*level] = 0; |
| 3704 | } |
| 3705 | free_extent_buffer(path->nodes[*level]); |
| 3706 | path->nodes[*level] = NULL; |
| 3707 | *level += 1; |
| 3708 | cond_resched(); |
| 3709 | return 0; |
| 3710 | } |
| 3711 | |
| 3712 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3713 | * helper for dropping snapshots. This walks back up the tree in the path |
| 3714 | * to find the first node higher up where we haven't yet gone through |
| 3715 | * all the slots |
| 3716 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3717 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
| 3718 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3719 | struct btrfs_path *path, |
| 3720 | int *level, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3721 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3722 | u64 root_owner; |
| 3723 | u64 root_gen; |
| 3724 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3725 | int i; |
| 3726 | int slot; |
| 3727 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3728 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3729 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3730 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3731 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 3732 | struct extent_buffer *node; |
| 3733 | struct btrfs_disk_key disk_key; |
| 3734 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3735 | path->slots[i]++; |
| 3736 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3737 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3738 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3739 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3740 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3741 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3742 | return 0; |
| 3743 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3744 | struct extent_buffer *parent; |
| 3745 | if (path->nodes[*level] == root->node) |
| 3746 | parent = path->nodes[*level]; |
| 3747 | else |
| 3748 | parent = path->nodes[*level + 1]; |
| 3749 | |
| 3750 | root_owner = btrfs_header_owner(parent); |
| 3751 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3752 | |
| 3753 | clean_tree_block(trans, root, path->nodes[*level]); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3754 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3755 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3756 | path->nodes[*level]->len, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3757 | parent->start, root_owner, |
| 3758 | root_gen, *level, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3759 | BUG_ON(ret); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3760 | if (path->locks[*level]) { |
| 3761 | btrfs_tree_unlock(path->nodes[*level]); |
| 3762 | path->locks[*level] = 0; |
| 3763 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3764 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3765 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3766 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3767 | } |
| 3768 | } |
| 3769 | return 1; |
| 3770 | } |
| 3771 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3772 | /* |
| 3773 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 3774 | * the tree freeing any blocks that have a ref count of zero after being |
| 3775 | * decremented. |
| 3776 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3777 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3778 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3779 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 3780 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3781 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3782 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3783 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3784 | int i; |
| 3785 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3786 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3787 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3788 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3789 | path = btrfs_alloc_path(); |
| 3790 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3791 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3792 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3793 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3794 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 3795 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3796 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3797 | path->slots[level] = 0; |
| 3798 | } else { |
| 3799 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3800 | struct btrfs_disk_key found_key; |
| 3801 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3802 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3803 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3804 | level = root_item->drop_level; |
| 3805 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3806 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3807 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3808 | ret = wret; |
| 3809 | goto out; |
| 3810 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3811 | node = path->nodes[level]; |
| 3812 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 3813 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 3814 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3815 | /* |
| 3816 | * unlock our path, this is safe because only this |
| 3817 | * function is allowed to delete this snapshot |
| 3818 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3819 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 3820 | if (path->nodes[i] && path->locks[i]) { |
| 3821 | path->locks[i] = 0; |
| 3822 | btrfs_tree_unlock(path->nodes[i]); |
| 3823 | } |
| 3824 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3825 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3826 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3827 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3828 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3829 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3830 | if (wret < 0) |
| 3831 | ret = wret; |
| 3832 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3833 | wret = walk_up_tree(trans, root, path, &level, |
| 3834 | BTRFS_MAX_LEVEL); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3835 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3836 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3837 | if (wret < 0) |
| 3838 | ret = wret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3839 | if (trans->transaction->in_commit) { |
| 3840 | ret = -EAGAIN; |
| 3841 | break; |
| 3842 | } |
Chris Mason | 18e35e0 | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3843 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3844 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3845 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3846 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3847 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3848 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 3849 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3850 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3851 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3852 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3853 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3854 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3855 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3856 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3857 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 3858 | struct btrfs_root *root, |
| 3859 | struct extent_buffer *node, |
| 3860 | struct extent_buffer *parent) |
| 3861 | { |
| 3862 | struct btrfs_path *path; |
| 3863 | int level; |
| 3864 | int parent_level; |
| 3865 | int ret = 0; |
| 3866 | int wret; |
| 3867 | |
| 3868 | path = btrfs_alloc_path(); |
| 3869 | BUG_ON(!path); |
| 3870 | |
| 3871 | BUG_ON(!btrfs_tree_locked(parent)); |
| 3872 | parent_level = btrfs_header_level(parent); |
| 3873 | extent_buffer_get(parent); |
| 3874 | path->nodes[parent_level] = parent; |
| 3875 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 3876 | |
| 3877 | BUG_ON(!btrfs_tree_locked(node)); |
| 3878 | level = btrfs_header_level(node); |
| 3879 | extent_buffer_get(node); |
| 3880 | path->nodes[level] = node; |
| 3881 | path->slots[level] = 0; |
| 3882 | |
| 3883 | while (1) { |
| 3884 | wret = walk_down_subtree(trans, root, path, &level); |
| 3885 | if (wret < 0) |
| 3886 | ret = wret; |
| 3887 | if (wret != 0) |
| 3888 | break; |
| 3889 | |
| 3890 | wret = walk_up_tree(trans, root, path, &level, parent_level); |
| 3891 | if (wret < 0) |
| 3892 | ret = wret; |
| 3893 | if (wret != 0) |
| 3894 | break; |
| 3895 | } |
| 3896 | |
| 3897 | btrfs_free_path(path); |
| 3898 | return ret; |
| 3899 | } |
| 3900 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3901 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 3902 | unsigned long nr) |
| 3903 | { |
| 3904 | return min(last, start + nr - 1); |
| 3905 | } |
| 3906 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3907 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
| 3908 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3909 | { |
| 3910 | u64 page_start; |
| 3911 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3912 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3913 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3914 | unsigned long i; |
| 3915 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3916 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3917 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3918 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3919 | unsigned int total_read = 0; |
| 3920 | unsigned int total_dirty = 0; |
| 3921 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3922 | |
| 3923 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3924 | |
| 3925 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3926 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3927 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 3928 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3929 | /* make sure the dirty trick played by the caller work */ |
| 3930 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 3931 | first_index, last_index); |
| 3932 | if (ret) |
| 3933 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3934 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3935 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3936 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3937 | for (i = first_index ; i <= last_index; i++) { |
| 3938 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3939 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3940 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3941 | } |
| 3942 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3943 | again: |
| 3944 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3945 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3946 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3947 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3948 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3949 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3950 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3951 | if (!PageUptodate(page)) { |
| 3952 | btrfs_readpage(NULL, page); |
| 3953 | lock_page(page); |
| 3954 | if (!PageUptodate(page)) { |
| 3955 | unlock_page(page); |
| 3956 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3957 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3958 | goto out_unlock; |
| 3959 | } |
| 3960 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3961 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3962 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3963 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 3964 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3965 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3966 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3967 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3968 | if (ordered) { |
| 3969 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3970 | unlock_page(page); |
| 3971 | page_cache_release(page); |
| 3972 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3973 | btrfs_put_ordered_extent(ordered); |
| 3974 | goto again; |
| 3975 | } |
| 3976 | set_page_extent_mapped(page); |
| 3977 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3978 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3979 | if (i == first_index) |
| 3980 | set_extent_bits(io_tree, page_start, page_end, |
| 3981 | EXTENT_BOUNDARY, GFP_NOFS); |
| 3982 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3983 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3984 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3985 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3986 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3987 | unlock_page(page); |
| 3988 | page_cache_release(page); |
| 3989 | } |
| 3990 | |
| 3991 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3992 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3993 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3994 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3995 | return ret; |
| 3996 | } |
| 3997 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3998 | static int noinline relocate_data_extent(struct inode *reloc_inode, |
| 3999 | struct btrfs_key *extent_key, |
| 4000 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4001 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4002 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4003 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 4004 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4005 | u64 start = extent_key->objectid - offset; |
| 4006 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4007 | |
| 4008 | em = alloc_extent_map(GFP_NOFS); |
| 4009 | BUG_ON(!em || IS_ERR(em)); |
| 4010 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4011 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4012 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4013 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4014 | em->block_start = extent_key->objectid; |
| 4015 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4016 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4017 | |
| 4018 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4019 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4020 | while (1) { |
| 4021 | int ret; |
| 4022 | spin_lock(&em_tree->lock); |
| 4023 | ret = add_extent_mapping(em_tree, em); |
| 4024 | spin_unlock(&em_tree->lock); |
| 4025 | if (ret != -EEXIST) { |
| 4026 | free_extent_map(em); |
| 4027 | break; |
| 4028 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4029 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4030 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4031 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4032 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4033 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | struct btrfs_ref_path { |
| 4037 | u64 extent_start; |
| 4038 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 4039 | u64 root_objectid; |
| 4040 | u64 root_generation; |
| 4041 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4042 | u32 num_refs; |
| 4043 | int lowest_level; |
| 4044 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4045 | int shared_level; |
| 4046 | |
| 4047 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 4048 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4049 | }; |
| 4050 | |
| 4051 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4052 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4053 | u64 disk_bytenr; |
| 4054 | u64 disk_num_bytes; |
| 4055 | u64 offset; |
| 4056 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4057 | u8 compression; |
| 4058 | u8 encryption; |
| 4059 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4060 | }; |
| 4061 | |
| 4062 | static int is_cowonly_root(u64 root_objectid) |
| 4063 | { |
| 4064 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 4065 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 4066 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 4067 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
| 4068 | root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 4069 | return 1; |
| 4070 | return 0; |
| 4071 | } |
| 4072 | |
| 4073 | static int noinline __next_ref_path(struct btrfs_trans_handle *trans, |
| 4074 | struct btrfs_root *extent_root, |
| 4075 | struct btrfs_ref_path *ref_path, |
| 4076 | int first_time) |
| 4077 | { |
| 4078 | struct extent_buffer *leaf; |
| 4079 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4080 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4081 | struct btrfs_key key; |
| 4082 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4083 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4084 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4085 | int level; |
| 4086 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4087 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4088 | path = btrfs_alloc_path(); |
| 4089 | if (!path) |
| 4090 | return -ENOMEM; |
| 4091 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4092 | if (first_time) { |
| 4093 | ref_path->lowest_level = -1; |
| 4094 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4095 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4096 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4097 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4098 | walk_down: |
| 4099 | level = ref_path->current_level - 1; |
| 4100 | while (level >= -1) { |
| 4101 | u64 parent; |
| 4102 | if (level < ref_path->lowest_level) |
| 4103 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4104 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4105 | if (level >= 0) { |
| 4106 | bytenr = ref_path->nodes[level]; |
| 4107 | } else { |
| 4108 | bytenr = ref_path->extent_start; |
| 4109 | } |
| 4110 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4111 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4112 | parent = ref_path->nodes[level + 1]; |
| 4113 | ref_path->nodes[level + 1] = 0; |
| 4114 | ref_path->current_level = level; |
| 4115 | BUG_ON(parent == 0); |
| 4116 | |
| 4117 | key.objectid = bytenr; |
| 4118 | key.offset = parent + 1; |
| 4119 | key.type = BTRFS_EXTENT_REF_KEY; |
| 4120 | |
| 4121 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4122 | if (ret < 0) |
| 4123 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4124 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4125 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4126 | leaf = path->nodes[0]; |
| 4127 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4128 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4129 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4130 | if (ret < 0) |
| 4131 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4132 | if (ret > 0) |
| 4133 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 4134 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4135 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4136 | |
| 4137 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4138 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4139 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 4140 | if (level < ref_path->shared_level) |
| 4141 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4142 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4143 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4144 | next: |
| 4145 | level--; |
| 4146 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4147 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4148 | } |
| 4149 | /* reached lowest level */ |
| 4150 | ret = 1; |
| 4151 | goto out; |
| 4152 | walk_up: |
| 4153 | level = ref_path->current_level; |
| 4154 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 4155 | u64 ref_objectid; |
| 4156 | if (level >= 0) { |
| 4157 | bytenr = ref_path->nodes[level]; |
| 4158 | } else { |
| 4159 | bytenr = ref_path->extent_start; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4160 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4161 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4162 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4163 | key.objectid = bytenr; |
| 4164 | key.offset = 0; |
| 4165 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4166 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4167 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 4168 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4169 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4170 | |
| 4171 | leaf = path->nodes[0]; |
| 4172 | nritems = btrfs_header_nritems(leaf); |
| 4173 | if (path->slots[0] >= nritems) { |
| 4174 | ret = btrfs_next_leaf(extent_root, path); |
| 4175 | if (ret < 0) |
| 4176 | goto out; |
| 4177 | if (ret > 0) { |
| 4178 | /* the extent was freed by someone */ |
| 4179 | if (ref_path->lowest_level == level) |
| 4180 | goto out; |
| 4181 | btrfs_release_path(extent_root, path); |
| 4182 | goto walk_down; |
| 4183 | } |
| 4184 | leaf = path->nodes[0]; |
| 4185 | } |
| 4186 | |
| 4187 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4188 | if (found_key.objectid != bytenr || |
| 4189 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 4190 | /* the extent was freed by someone */ |
| 4191 | if (ref_path->lowest_level == level) { |
| 4192 | ret = 1; |
| 4193 | goto out; |
| 4194 | } |
| 4195 | btrfs_release_path(extent_root, path); |
| 4196 | goto walk_down; |
| 4197 | } |
| 4198 | found: |
| 4199 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 4200 | struct btrfs_extent_ref); |
| 4201 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 4202 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4203 | if (first_time) { |
| 4204 | level = (int)ref_objectid; |
| 4205 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 4206 | ref_path->lowest_level = level; |
| 4207 | ref_path->current_level = level; |
| 4208 | ref_path->nodes[level] = bytenr; |
| 4209 | } else { |
| 4210 | WARN_ON(ref_objectid != level); |
| 4211 | } |
| 4212 | } else { |
| 4213 | WARN_ON(level != -1); |
| 4214 | } |
| 4215 | first_time = 0; |
| 4216 | |
| 4217 | if (ref_path->lowest_level == level) { |
| 4218 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4219 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 4220 | } |
| 4221 | |
| 4222 | /* |
| 4223 | * the block is tree root or the block isn't in reference |
| 4224 | * counted tree. |
| 4225 | */ |
| 4226 | if (found_key.objectid == found_key.offset || |
| 4227 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 4228 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4229 | ref_path->root_generation = |
| 4230 | btrfs_ref_generation(leaf, ref); |
| 4231 | if (level < 0) { |
| 4232 | /* special reference from the tree log */ |
| 4233 | ref_path->nodes[0] = found_key.offset; |
| 4234 | ref_path->current_level = 0; |
| 4235 | } |
| 4236 | ret = 0; |
| 4237 | goto out; |
| 4238 | } |
| 4239 | |
| 4240 | level++; |
| 4241 | BUG_ON(ref_path->nodes[level] != 0); |
| 4242 | ref_path->nodes[level] = found_key.offset; |
| 4243 | ref_path->current_level = level; |
| 4244 | |
| 4245 | /* |
| 4246 | * the reference was created in the running transaction, |
| 4247 | * no need to continue walking up. |
| 4248 | */ |
| 4249 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 4250 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4251 | ref_path->root_generation = |
| 4252 | btrfs_ref_generation(leaf, ref); |
| 4253 | ret = 0; |
| 4254 | goto out; |
| 4255 | } |
| 4256 | |
| 4257 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4258 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4259 | } |
| 4260 | /* reached max tree level, but no tree root found. */ |
| 4261 | BUG(); |
| 4262 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4263 | btrfs_free_path(path); |
| 4264 | return ret; |
| 4265 | } |
| 4266 | |
| 4267 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 4268 | struct btrfs_root *extent_root, |
| 4269 | struct btrfs_ref_path *ref_path, |
| 4270 | u64 extent_start) |
| 4271 | { |
| 4272 | memset(ref_path, 0, sizeof(*ref_path)); |
| 4273 | ref_path->extent_start = extent_start; |
| 4274 | |
| 4275 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 4276 | } |
| 4277 | |
| 4278 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 4279 | struct btrfs_root *extent_root, |
| 4280 | struct btrfs_ref_path *ref_path) |
| 4281 | { |
| 4282 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 4283 | } |
| 4284 | |
| 4285 | static int noinline get_new_locations(struct inode *reloc_inode, |
| 4286 | struct btrfs_key *extent_key, |
| 4287 | u64 offset, int no_fragment, |
| 4288 | struct disk_extent **extents, |
| 4289 | int *nr_extents) |
| 4290 | { |
| 4291 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4292 | struct btrfs_path *path; |
| 4293 | struct btrfs_file_extent_item *fi; |
| 4294 | struct extent_buffer *leaf; |
| 4295 | struct disk_extent *exts = *extents; |
| 4296 | struct btrfs_key found_key; |
| 4297 | u64 cur_pos; |
| 4298 | u64 last_byte; |
| 4299 | u32 nritems; |
| 4300 | int nr = 0; |
| 4301 | int max = *nr_extents; |
| 4302 | int ret; |
| 4303 | |
| 4304 | WARN_ON(!no_fragment && *extents); |
| 4305 | if (!exts) { |
| 4306 | max = 1; |
| 4307 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4308 | if (!exts) |
| 4309 | return -ENOMEM; |
| 4310 | } |
| 4311 | |
| 4312 | path = btrfs_alloc_path(); |
| 4313 | BUG_ON(!path); |
| 4314 | |
| 4315 | cur_pos = extent_key->objectid - offset; |
| 4316 | last_byte = extent_key->objectid + extent_key->offset; |
| 4317 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 4318 | cur_pos, 0); |
| 4319 | if (ret < 0) |
| 4320 | goto out; |
| 4321 | if (ret > 0) { |
| 4322 | ret = -ENOENT; |
| 4323 | goto out; |
| 4324 | } |
| 4325 | |
| 4326 | while (1) { |
| 4327 | leaf = path->nodes[0]; |
| 4328 | nritems = btrfs_header_nritems(leaf); |
| 4329 | if (path->slots[0] >= nritems) { |
| 4330 | ret = btrfs_next_leaf(root, path); |
| 4331 | if (ret < 0) |
| 4332 | goto out; |
| 4333 | if (ret > 0) |
| 4334 | break; |
| 4335 | leaf = path->nodes[0]; |
| 4336 | } |
| 4337 | |
| 4338 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4339 | if (found_key.offset != cur_pos || |
| 4340 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 4341 | found_key.objectid != reloc_inode->i_ino) |
| 4342 | break; |
| 4343 | |
| 4344 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4345 | struct btrfs_file_extent_item); |
| 4346 | if (btrfs_file_extent_type(leaf, fi) != |
| 4347 | BTRFS_FILE_EXTENT_REG || |
| 4348 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4349 | break; |
| 4350 | |
| 4351 | if (nr == max) { |
| 4352 | struct disk_extent *old = exts; |
| 4353 | max *= 2; |
| 4354 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4355 | memcpy(exts, old, sizeof(*exts) * nr); |
| 4356 | if (old != *extents) |
| 4357 | kfree(old); |
| 4358 | } |
| 4359 | |
| 4360 | exts[nr].disk_bytenr = |
| 4361 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4362 | exts[nr].disk_num_bytes = |
| 4363 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4364 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 4365 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4366 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 4367 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 4368 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 4369 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 4370 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4371 | BUG_ON(exts[nr].offset > 0); |
| 4372 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 4373 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4374 | |
| 4375 | cur_pos += exts[nr].num_bytes; |
| 4376 | nr++; |
| 4377 | |
| 4378 | if (cur_pos + offset >= last_byte) |
| 4379 | break; |
| 4380 | |
| 4381 | if (no_fragment) { |
| 4382 | ret = 1; |
| 4383 | goto out; |
| 4384 | } |
| 4385 | path->slots[0]++; |
| 4386 | } |
| 4387 | |
| 4388 | WARN_ON(cur_pos + offset > last_byte); |
| 4389 | if (cur_pos + offset < last_byte) { |
| 4390 | ret = -ENOENT; |
| 4391 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4392 | } |
| 4393 | ret = 0; |
| 4394 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4395 | btrfs_free_path(path); |
| 4396 | if (ret) { |
| 4397 | if (exts != *extents) |
| 4398 | kfree(exts); |
| 4399 | } else { |
| 4400 | *extents = exts; |
| 4401 | *nr_extents = nr; |
| 4402 | } |
| 4403 | return ret; |
| 4404 | } |
| 4405 | |
| 4406 | static int noinline replace_one_extent(struct btrfs_trans_handle *trans, |
| 4407 | struct btrfs_root *root, |
| 4408 | struct btrfs_path *path, |
| 4409 | struct btrfs_key *extent_key, |
| 4410 | struct btrfs_key *leaf_key, |
| 4411 | struct btrfs_ref_path *ref_path, |
| 4412 | struct disk_extent *new_extents, |
| 4413 | int nr_extents) |
| 4414 | { |
| 4415 | struct extent_buffer *leaf; |
| 4416 | struct btrfs_file_extent_item *fi; |
| 4417 | struct inode *inode = NULL; |
| 4418 | struct btrfs_key key; |
| 4419 | u64 lock_start = 0; |
| 4420 | u64 lock_end = 0; |
| 4421 | u64 num_bytes; |
| 4422 | u64 ext_offset; |
| 4423 | u64 first_pos; |
| 4424 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4425 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4426 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4427 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4428 | int ret; |
| 4429 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4430 | memcpy(&key, leaf_key, sizeof(key)); |
| 4431 | first_pos = INT_LIMIT(loff_t) - extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4432 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4433 | if (key.objectid < ref_path->owner_objectid || |
| 4434 | (key.objectid == ref_path->owner_objectid && |
| 4435 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 4436 | key.objectid = ref_path->owner_objectid; |
| 4437 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4438 | key.offset = 0; |
| 4439 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | while (1) { |
| 4443 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 4444 | if (ret < 0) |
| 4445 | goto out; |
| 4446 | |
| 4447 | leaf = path->nodes[0]; |
| 4448 | nritems = btrfs_header_nritems(leaf); |
| 4449 | next: |
| 4450 | if (extent_locked && ret > 0) { |
| 4451 | /* |
| 4452 | * the file extent item was modified by someone |
| 4453 | * before the extent got locked. |
| 4454 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4455 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4456 | lock_end, GFP_NOFS); |
| 4457 | extent_locked = 0; |
| 4458 | } |
| 4459 | |
| 4460 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4461 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4462 | break; |
| 4463 | |
| 4464 | BUG_ON(extent_locked); |
| 4465 | ret = btrfs_next_leaf(root, path); |
| 4466 | if (ret < 0) |
| 4467 | goto out; |
| 4468 | if (ret > 0) |
| 4469 | break; |
| 4470 | leaf = path->nodes[0]; |
| 4471 | nritems = btrfs_header_nritems(leaf); |
| 4472 | } |
| 4473 | |
| 4474 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4475 | |
| 4476 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 4477 | if ((key.objectid > ref_path->owner_objectid) || |
| 4478 | (key.objectid == ref_path->owner_objectid && |
| 4479 | key.type > BTRFS_EXTENT_DATA_KEY) || |
| 4480 | (key.offset >= first_pos + extent_key->offset)) |
| 4481 | break; |
| 4482 | } |
| 4483 | |
| 4484 | if (inode && key.objectid != inode->i_ino) { |
| 4485 | BUG_ON(extent_locked); |
| 4486 | btrfs_release_path(root, path); |
| 4487 | mutex_unlock(&inode->i_mutex); |
| 4488 | iput(inode); |
| 4489 | inode = NULL; |
| 4490 | continue; |
| 4491 | } |
| 4492 | |
| 4493 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4494 | path->slots[0]++; |
| 4495 | ret = 1; |
| 4496 | goto next; |
| 4497 | } |
| 4498 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4499 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4500 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4501 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 4502 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4503 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 4504 | extent_key->objectid)) { |
| 4505 | path->slots[0]++; |
| 4506 | ret = 1; |
| 4507 | goto next; |
| 4508 | } |
| 4509 | |
| 4510 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4511 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 4512 | |
| 4513 | if (first_pos > key.offset - ext_offset) |
| 4514 | first_pos = key.offset - ext_offset; |
| 4515 | |
| 4516 | if (!extent_locked) { |
| 4517 | lock_start = key.offset; |
| 4518 | lock_end = lock_start + num_bytes - 1; |
| 4519 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4520 | if (lock_start > key.offset || |
| 4521 | lock_end + 1 < key.offset + num_bytes) { |
| 4522 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4523 | lock_start, lock_end, GFP_NOFS); |
| 4524 | extent_locked = 0; |
| 4525 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | if (!inode) { |
| 4529 | btrfs_release_path(root, path); |
| 4530 | |
| 4531 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 4532 | key.objectid, root); |
| 4533 | if (inode->i_state & I_NEW) { |
| 4534 | BTRFS_I(inode)->root = root; |
| 4535 | BTRFS_I(inode)->location.objectid = |
| 4536 | key.objectid; |
| 4537 | BTRFS_I(inode)->location.type = |
| 4538 | BTRFS_INODE_ITEM_KEY; |
| 4539 | BTRFS_I(inode)->location.offset = 0; |
| 4540 | btrfs_read_locked_inode(inode); |
| 4541 | unlock_new_inode(inode); |
| 4542 | } |
| 4543 | /* |
| 4544 | * some code call btrfs_commit_transaction while |
| 4545 | * holding the i_mutex, so we can't use mutex_lock |
| 4546 | * here. |
| 4547 | */ |
| 4548 | if (is_bad_inode(inode) || |
| 4549 | !mutex_trylock(&inode->i_mutex)) { |
| 4550 | iput(inode); |
| 4551 | inode = NULL; |
| 4552 | key.offset = (u64)-1; |
| 4553 | goto skip; |
| 4554 | } |
| 4555 | } |
| 4556 | |
| 4557 | if (!extent_locked) { |
| 4558 | struct btrfs_ordered_extent *ordered; |
| 4559 | |
| 4560 | btrfs_release_path(root, path); |
| 4561 | |
| 4562 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4563 | lock_end, GFP_NOFS); |
| 4564 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 4565 | lock_end); |
| 4566 | if (ordered && |
| 4567 | ordered->file_offset <= lock_end && |
| 4568 | ordered->file_offset + ordered->len > lock_start) { |
| 4569 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4570 | lock_start, lock_end, GFP_NOFS); |
| 4571 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 4572 | btrfs_put_ordered_extent(ordered); |
| 4573 | key.offset += num_bytes; |
| 4574 | goto skip; |
| 4575 | } |
| 4576 | if (ordered) |
| 4577 | btrfs_put_ordered_extent(ordered); |
| 4578 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4579 | extent_locked = 1; |
| 4580 | continue; |
| 4581 | } |
| 4582 | |
| 4583 | if (nr_extents == 1) { |
| 4584 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4585 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4586 | new_extents[0].disk_bytenr); |
| 4587 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4588 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4589 | btrfs_mark_buffer_dirty(leaf); |
| 4590 | |
| 4591 | btrfs_drop_extent_cache(inode, key.offset, |
| 4592 | key.offset + num_bytes - 1, 0); |
| 4593 | |
| 4594 | ret = btrfs_inc_extent_ref(trans, root, |
| 4595 | new_extents[0].disk_bytenr, |
| 4596 | new_extents[0].disk_num_bytes, |
| 4597 | leaf->start, |
| 4598 | root->root_key.objectid, |
| 4599 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4600 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4601 | BUG_ON(ret); |
| 4602 | |
| 4603 | ret = btrfs_free_extent(trans, root, |
| 4604 | extent_key->objectid, |
| 4605 | extent_key->offset, |
| 4606 | leaf->start, |
| 4607 | btrfs_header_owner(leaf), |
| 4608 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4609 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4610 | BUG_ON(ret); |
| 4611 | |
| 4612 | btrfs_release_path(root, path); |
| 4613 | key.offset += num_bytes; |
| 4614 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4615 | BUG_ON(1); |
| 4616 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4617 | u64 alloc_hint; |
| 4618 | u64 extent_len; |
| 4619 | int i; |
| 4620 | /* |
| 4621 | * drop old extent pointer at first, then insert the |
| 4622 | * new pointers one bye one |
| 4623 | */ |
| 4624 | btrfs_release_path(root, path); |
| 4625 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 4626 | key.offset + num_bytes, |
| 4627 | key.offset, &alloc_hint); |
| 4628 | BUG_ON(ret); |
| 4629 | |
| 4630 | for (i = 0; i < nr_extents; i++) { |
| 4631 | if (ext_offset >= new_extents[i].num_bytes) { |
| 4632 | ext_offset -= new_extents[i].num_bytes; |
| 4633 | continue; |
| 4634 | } |
| 4635 | extent_len = min(new_extents[i].num_bytes - |
| 4636 | ext_offset, num_bytes); |
| 4637 | |
| 4638 | ret = btrfs_insert_empty_item(trans, root, |
| 4639 | path, &key, |
| 4640 | sizeof(*fi)); |
| 4641 | BUG_ON(ret); |
| 4642 | |
| 4643 | leaf = path->nodes[0]; |
| 4644 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4645 | struct btrfs_file_extent_item); |
| 4646 | btrfs_set_file_extent_generation(leaf, fi, |
| 4647 | trans->transid); |
| 4648 | btrfs_set_file_extent_type(leaf, fi, |
| 4649 | BTRFS_FILE_EXTENT_REG); |
| 4650 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4651 | new_extents[i].disk_bytenr); |
| 4652 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4653 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4654 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 4655 | new_extents[i].ram_bytes); |
| 4656 | |
| 4657 | btrfs_set_file_extent_compression(leaf, fi, |
| 4658 | new_extents[i].compression); |
| 4659 | btrfs_set_file_extent_encryption(leaf, fi, |
| 4660 | new_extents[i].encryption); |
| 4661 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 4662 | new_extents[i].other_encoding); |
| 4663 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4664 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4665 | extent_len); |
| 4666 | ext_offset += new_extents[i].offset; |
| 4667 | btrfs_set_file_extent_offset(leaf, fi, |
| 4668 | ext_offset); |
| 4669 | btrfs_mark_buffer_dirty(leaf); |
| 4670 | |
| 4671 | btrfs_drop_extent_cache(inode, key.offset, |
| 4672 | key.offset + extent_len - 1, 0); |
| 4673 | |
| 4674 | ret = btrfs_inc_extent_ref(trans, root, |
| 4675 | new_extents[i].disk_bytenr, |
| 4676 | new_extents[i].disk_num_bytes, |
| 4677 | leaf->start, |
| 4678 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4679 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4680 | BUG_ON(ret); |
| 4681 | btrfs_release_path(root, path); |
| 4682 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4683 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4684 | |
| 4685 | ext_offset = 0; |
| 4686 | num_bytes -= extent_len; |
| 4687 | key.offset += extent_len; |
| 4688 | |
| 4689 | if (num_bytes == 0) |
| 4690 | break; |
| 4691 | } |
| 4692 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4693 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4697 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4698 | lock_end, GFP_NOFS); |
| 4699 | extent_locked = 0; |
| 4700 | } |
| 4701 | skip: |
| 4702 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
| 4703 | key.offset >= first_pos + extent_key->offset) |
| 4704 | break; |
| 4705 | |
| 4706 | cond_resched(); |
| 4707 | } |
| 4708 | ret = 0; |
| 4709 | out: |
| 4710 | btrfs_release_path(root, path); |
| 4711 | if (inode) { |
| 4712 | mutex_unlock(&inode->i_mutex); |
| 4713 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4714 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4715 | lock_end, GFP_NOFS); |
| 4716 | } |
| 4717 | iput(inode); |
| 4718 | } |
| 4719 | return ret; |
| 4720 | } |
| 4721 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4722 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 4723 | struct btrfs_root *root, |
| 4724 | struct extent_buffer *buf, u64 orig_start) |
| 4725 | { |
| 4726 | int level; |
| 4727 | int ret; |
| 4728 | |
| 4729 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 4730 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 4731 | |
| 4732 | level = btrfs_header_level(buf); |
| 4733 | if (level == 0) { |
| 4734 | struct btrfs_leaf_ref *ref; |
| 4735 | struct btrfs_leaf_ref *orig_ref; |
| 4736 | |
| 4737 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 4738 | if (!orig_ref) |
| 4739 | return -ENOENT; |
| 4740 | |
| 4741 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 4742 | if (!ref) { |
| 4743 | btrfs_free_leaf_ref(root, orig_ref); |
| 4744 | return -ENOMEM; |
| 4745 | } |
| 4746 | |
| 4747 | ref->nritems = orig_ref->nritems; |
| 4748 | memcpy(ref->extents, orig_ref->extents, |
| 4749 | sizeof(ref->extents[0]) * ref->nritems); |
| 4750 | |
| 4751 | btrfs_free_leaf_ref(root, orig_ref); |
| 4752 | |
| 4753 | ref->root_gen = trans->transid; |
| 4754 | ref->bytenr = buf->start; |
| 4755 | ref->owner = btrfs_header_owner(buf); |
| 4756 | ref->generation = btrfs_header_generation(buf); |
| 4757 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 4758 | WARN_ON(ret); |
| 4759 | btrfs_free_leaf_ref(root, ref); |
| 4760 | } |
| 4761 | return 0; |
| 4762 | } |
| 4763 | |
| 4764 | static int noinline invalidate_extent_cache(struct btrfs_root *root, |
| 4765 | struct extent_buffer *leaf, |
| 4766 | struct btrfs_block_group_cache *group, |
| 4767 | struct btrfs_root *target_root) |
| 4768 | { |
| 4769 | struct btrfs_key key; |
| 4770 | struct inode *inode = NULL; |
| 4771 | struct btrfs_file_extent_item *fi; |
| 4772 | u64 num_bytes; |
| 4773 | u64 skip_objectid = 0; |
| 4774 | u32 nritems; |
| 4775 | u32 i; |
| 4776 | |
| 4777 | nritems = btrfs_header_nritems(leaf); |
| 4778 | for (i = 0; i < nritems; i++) { |
| 4779 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4780 | if (key.objectid == skip_objectid || |
| 4781 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 4782 | continue; |
| 4783 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4784 | if (btrfs_file_extent_type(leaf, fi) == |
| 4785 | BTRFS_FILE_EXTENT_INLINE) |
| 4786 | continue; |
| 4787 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4788 | continue; |
| 4789 | if (!inode || inode->i_ino != key.objectid) { |
| 4790 | iput(inode); |
| 4791 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 4792 | key.objectid, target_root, 1); |
| 4793 | } |
| 4794 | if (!inode) { |
| 4795 | skip_objectid = key.objectid; |
| 4796 | continue; |
| 4797 | } |
| 4798 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4799 | |
| 4800 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4801 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4802 | btrfs_drop_extent_cache(inode, key.offset, |
| 4803 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4804 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4805 | key.offset + num_bytes - 1, GFP_NOFS); |
| 4806 | cond_resched(); |
| 4807 | } |
| 4808 | iput(inode); |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
| 4812 | static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
| 4813 | struct btrfs_root *root, |
| 4814 | struct extent_buffer *leaf, |
| 4815 | struct btrfs_block_group_cache *group, |
| 4816 | struct inode *reloc_inode) |
| 4817 | { |
| 4818 | struct btrfs_key key; |
| 4819 | struct btrfs_key extent_key; |
| 4820 | struct btrfs_file_extent_item *fi; |
| 4821 | struct btrfs_leaf_ref *ref; |
| 4822 | struct disk_extent *new_extent; |
| 4823 | u64 bytenr; |
| 4824 | u64 num_bytes; |
| 4825 | u32 nritems; |
| 4826 | u32 i; |
| 4827 | int ext_index; |
| 4828 | int nr_extent; |
| 4829 | int ret; |
| 4830 | |
| 4831 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 4832 | BUG_ON(!new_extent); |
| 4833 | |
| 4834 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 4835 | BUG_ON(!ref); |
| 4836 | |
| 4837 | ext_index = -1; |
| 4838 | nritems = btrfs_header_nritems(leaf); |
| 4839 | for (i = 0; i < nritems; i++) { |
| 4840 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4841 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 4842 | continue; |
| 4843 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4844 | if (btrfs_file_extent_type(leaf, fi) == |
| 4845 | BTRFS_FILE_EXTENT_INLINE) |
| 4846 | continue; |
| 4847 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4848 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4849 | if (bytenr == 0) |
| 4850 | continue; |
| 4851 | |
| 4852 | ext_index++; |
| 4853 | if (bytenr >= group->key.objectid + group->key.offset || |
| 4854 | bytenr + num_bytes <= group->key.objectid) |
| 4855 | continue; |
| 4856 | |
| 4857 | extent_key.objectid = bytenr; |
| 4858 | extent_key.offset = num_bytes; |
| 4859 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4860 | nr_extent = 1; |
| 4861 | ret = get_new_locations(reloc_inode, &extent_key, |
| 4862 | group->key.objectid, 1, |
| 4863 | &new_extent, &nr_extent); |
| 4864 | if (ret > 0) |
| 4865 | continue; |
| 4866 | BUG_ON(ret < 0); |
| 4867 | |
| 4868 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 4869 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 4870 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 4871 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 4872 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4873 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4874 | new_extent->disk_bytenr); |
| 4875 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4876 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4877 | btrfs_mark_buffer_dirty(leaf); |
| 4878 | |
| 4879 | ret = btrfs_inc_extent_ref(trans, root, |
| 4880 | new_extent->disk_bytenr, |
| 4881 | new_extent->disk_num_bytes, |
| 4882 | leaf->start, |
| 4883 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4884 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4885 | BUG_ON(ret); |
| 4886 | ret = btrfs_free_extent(trans, root, |
| 4887 | bytenr, num_bytes, leaf->start, |
| 4888 | btrfs_header_owner(leaf), |
| 4889 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4890 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4891 | BUG_ON(ret); |
| 4892 | cond_resched(); |
| 4893 | } |
| 4894 | kfree(new_extent); |
| 4895 | BUG_ON(ext_index + 1 != ref->nritems); |
| 4896 | btrfs_free_leaf_ref(root, ref); |
| 4897 | return 0; |
| 4898 | } |
| 4899 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4900 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 4901 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4902 | { |
| 4903 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4904 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4905 | |
| 4906 | if (root->reloc_root) { |
| 4907 | reloc_root = root->reloc_root; |
| 4908 | root->reloc_root = NULL; |
| 4909 | list_add(&reloc_root->dead_list, |
| 4910 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4911 | |
| 4912 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 4913 | reloc_root->node->start); |
| 4914 | btrfs_set_root_level(&root->root_item, |
| 4915 | btrfs_header_level(reloc_root->node)); |
| 4916 | memset(&reloc_root->root_item.drop_progress, 0, |
| 4917 | sizeof(struct btrfs_disk_key)); |
| 4918 | reloc_root->root_item.drop_level = 0; |
| 4919 | |
| 4920 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 4921 | &reloc_root->root_key, |
| 4922 | &reloc_root->root_item); |
| 4923 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4924 | } |
| 4925 | return 0; |
| 4926 | } |
| 4927 | |
| 4928 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 4929 | { |
| 4930 | struct btrfs_trans_handle *trans; |
| 4931 | struct btrfs_root *reloc_root; |
| 4932 | struct btrfs_root *prev_root = NULL; |
| 4933 | struct list_head dead_roots; |
| 4934 | int ret; |
| 4935 | unsigned long nr; |
| 4936 | |
| 4937 | INIT_LIST_HEAD(&dead_roots); |
| 4938 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 4939 | |
| 4940 | while (!list_empty(&dead_roots)) { |
| 4941 | reloc_root = list_entry(dead_roots.prev, |
| 4942 | struct btrfs_root, dead_list); |
| 4943 | list_del_init(&reloc_root->dead_list); |
| 4944 | |
| 4945 | BUG_ON(reloc_root->commit_root != NULL); |
| 4946 | while (1) { |
| 4947 | trans = btrfs_join_transaction(root, 1); |
| 4948 | BUG_ON(!trans); |
| 4949 | |
| 4950 | mutex_lock(&root->fs_info->drop_mutex); |
| 4951 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 4952 | if (ret != -EAGAIN) |
| 4953 | break; |
| 4954 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4955 | |
| 4956 | nr = trans->blocks_used; |
| 4957 | ret = btrfs_end_transaction(trans, root); |
| 4958 | BUG_ON(ret); |
| 4959 | btrfs_btree_balance_dirty(root, nr); |
| 4960 | } |
| 4961 | |
| 4962 | free_extent_buffer(reloc_root->node); |
| 4963 | |
| 4964 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 4965 | &reloc_root->root_key); |
| 4966 | BUG_ON(ret); |
| 4967 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4968 | |
| 4969 | nr = trans->blocks_used; |
| 4970 | ret = btrfs_end_transaction(trans, root); |
| 4971 | BUG_ON(ret); |
| 4972 | btrfs_btree_balance_dirty(root, nr); |
| 4973 | |
| 4974 | kfree(prev_root); |
| 4975 | prev_root = reloc_root; |
| 4976 | } |
| 4977 | if (prev_root) { |
| 4978 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 4979 | kfree(prev_root); |
| 4980 | } |
| 4981 | return 0; |
| 4982 | } |
| 4983 | |
| 4984 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 4985 | { |
| 4986 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 4987 | return 0; |
| 4988 | } |
| 4989 | |
| 4990 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 4991 | { |
| 4992 | struct btrfs_root *reloc_root; |
| 4993 | struct btrfs_trans_handle *trans; |
| 4994 | struct btrfs_key location; |
| 4995 | int found; |
| 4996 | int ret; |
| 4997 | |
| 4998 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 4999 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 5000 | BUG_ON(ret); |
| 5001 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 5002 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 5003 | |
| 5004 | if (found) { |
| 5005 | trans = btrfs_start_transaction(root, 1); |
| 5006 | BUG_ON(!trans); |
| 5007 | ret = btrfs_commit_transaction(trans, root); |
| 5008 | BUG_ON(ret); |
| 5009 | } |
| 5010 | |
| 5011 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5012 | location.offset = (u64)-1; |
| 5013 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 5014 | |
| 5015 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 5016 | BUG_ON(!reloc_root); |
| 5017 | btrfs_orphan_cleanup(reloc_root); |
| 5018 | return 0; |
| 5019 | } |
| 5020 | |
| 5021 | static int noinline init_reloc_tree(struct btrfs_trans_handle *trans, |
| 5022 | struct btrfs_root *root) |
| 5023 | { |
| 5024 | struct btrfs_root *reloc_root; |
| 5025 | struct extent_buffer *eb; |
| 5026 | struct btrfs_root_item *root_item; |
| 5027 | struct btrfs_key root_key; |
| 5028 | int ret; |
| 5029 | |
| 5030 | BUG_ON(!root->ref_cows); |
| 5031 | if (root->reloc_root) |
| 5032 | return 0; |
| 5033 | |
| 5034 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 5035 | BUG_ON(!root_item); |
| 5036 | |
| 5037 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 5038 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 5039 | BUG_ON(ret); |
| 5040 | |
| 5041 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 5042 | root_key.offset = root->root_key.objectid; |
| 5043 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5044 | |
| 5045 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 5046 | btrfs_set_root_refs(root_item, 0); |
| 5047 | btrfs_set_root_bytenr(root_item, eb->start); |
| 5048 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5049 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5050 | |
| 5051 | btrfs_tree_unlock(eb); |
| 5052 | free_extent_buffer(eb); |
| 5053 | |
| 5054 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 5055 | &root_key, root_item); |
| 5056 | BUG_ON(ret); |
| 5057 | kfree(root_item); |
| 5058 | |
| 5059 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 5060 | &root_key); |
| 5061 | BUG_ON(!reloc_root); |
| 5062 | reloc_root->last_trans = trans->transid; |
| 5063 | reloc_root->commit_root = NULL; |
| 5064 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 5065 | |
| 5066 | root->reloc_root = reloc_root; |
| 5067 | return 0; |
| 5068 | } |
| 5069 | |
| 5070 | /* |
| 5071 | * Core function of space balance. |
| 5072 | * |
| 5073 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5074 | * counted roots. There is one reloc tree for each subvol, and all |
| 5075 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 5076 | * of the latest committed roots of subvols (root->commit_root). |
| 5077 | * |
| 5078 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 5079 | * COW the block through subvol's reloc tree, then update block pointer |
| 5080 | * in the subvol to point to the new block. Since all reloc trees share |
| 5081 | * same root key objectid, doing special handing for tree blocks owned |
| 5082 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 5083 | * we can use the resulting new block directly when the same block is |
| 5084 | * required to COW again through other reloc trees. By this way, relocated |
| 5085 | * tree blocks are shared between reloc trees, so they are also shared |
| 5086 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5087 | */ |
| 5088 | static int noinline relocate_one_path(struct btrfs_trans_handle *trans, |
| 5089 | struct btrfs_root *root, |
| 5090 | struct btrfs_path *path, |
| 5091 | struct btrfs_key *first_key, |
| 5092 | struct btrfs_ref_path *ref_path, |
| 5093 | struct btrfs_block_group_cache *group, |
| 5094 | struct inode *reloc_inode) |
| 5095 | { |
| 5096 | struct btrfs_root *reloc_root; |
| 5097 | struct extent_buffer *eb = NULL; |
| 5098 | struct btrfs_key *keys; |
| 5099 | u64 *nodes; |
| 5100 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5101 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5102 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5103 | int ret; |
| 5104 | |
| 5105 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 5106 | lowest_level = ref_path->owner_objectid; |
| 5107 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5108 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5109 | path->lowest_level = lowest_level; |
| 5110 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 5111 | BUG_ON(ret < 0); |
| 5112 | path->lowest_level = 0; |
| 5113 | btrfs_release_path(root, path); |
| 5114 | return 0; |
| 5115 | } |
| 5116 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5117 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5118 | ret = init_reloc_tree(trans, root); |
| 5119 | BUG_ON(ret); |
| 5120 | reloc_root = root->reloc_root; |
| 5121 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5122 | shared_level = ref_path->shared_level; |
| 5123 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5124 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5125 | keys = ref_path->node_keys; |
| 5126 | nodes = ref_path->new_nodes; |
| 5127 | memset(&keys[shared_level + 1], 0, |
| 5128 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 5129 | memset(&nodes[shared_level + 1], 0, |
| 5130 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5131 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5132 | if (nodes[lowest_level] == 0) { |
| 5133 | path->lowest_level = lowest_level; |
| 5134 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5135 | 0, 1); |
| 5136 | BUG_ON(ret); |
| 5137 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 5138 | eb = path->nodes[level]; |
| 5139 | if (!eb || eb == reloc_root->node) |
| 5140 | break; |
| 5141 | nodes[level] = eb->start; |
| 5142 | if (level == 0) |
| 5143 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 5144 | else |
| 5145 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 5146 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5147 | if (nodes[0] && |
| 5148 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5149 | eb = path->nodes[0]; |
| 5150 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 5151 | group, reloc_inode); |
| 5152 | BUG_ON(ret); |
| 5153 | } |
| 5154 | btrfs_release_path(reloc_root, path); |
| 5155 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5156 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5157 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5158 | BUG_ON(ret); |
| 5159 | } |
| 5160 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5161 | /* |
| 5162 | * replace tree blocks in the fs tree with tree blocks in |
| 5163 | * the reloc tree. |
| 5164 | */ |
| 5165 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 5166 | BUG_ON(ret < 0); |
| 5167 | |
| 5168 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5169 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5170 | 0, 0); |
| 5171 | BUG_ON(ret); |
| 5172 | extent_buffer_get(path->nodes[0]); |
| 5173 | eb = path->nodes[0]; |
| 5174 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5175 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 5176 | BUG_ON(ret); |
| 5177 | free_extent_buffer(eb); |
| 5178 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5179 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5180 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5181 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5182 | return 0; |
| 5183 | } |
| 5184 | |
| 5185 | static int noinline relocate_tree_block(struct btrfs_trans_handle *trans, |
| 5186 | struct btrfs_root *root, |
| 5187 | struct btrfs_path *path, |
| 5188 | struct btrfs_key *first_key, |
| 5189 | struct btrfs_ref_path *ref_path) |
| 5190 | { |
| 5191 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5192 | |
| 5193 | ret = relocate_one_path(trans, root, path, first_key, |
| 5194 | ref_path, NULL, NULL); |
| 5195 | BUG_ON(ret); |
| 5196 | |
| 5197 | if (root == root->fs_info->extent_root) |
| 5198 | btrfs_extent_post_op(trans, root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5199 | |
| 5200 | return 0; |
| 5201 | } |
| 5202 | |
| 5203 | static int noinline del_extent_zero(struct btrfs_trans_handle *trans, |
| 5204 | struct btrfs_root *extent_root, |
| 5205 | struct btrfs_path *path, |
| 5206 | struct btrfs_key *extent_key) |
| 5207 | { |
| 5208 | int ret; |
| 5209 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5210 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 5211 | if (ret) |
| 5212 | goto out; |
| 5213 | ret = btrfs_del_item(trans, extent_root, path); |
| 5214 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5215 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5216 | return ret; |
| 5217 | } |
| 5218 | |
| 5219 | static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info, |
| 5220 | struct btrfs_ref_path *ref_path) |
| 5221 | { |
| 5222 | struct btrfs_key root_key; |
| 5223 | |
| 5224 | root_key.objectid = ref_path->root_objectid; |
| 5225 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5226 | if (is_cowonly_root(ref_path->root_objectid)) |
| 5227 | root_key.offset = 0; |
| 5228 | else |
| 5229 | root_key.offset = (u64)-1; |
| 5230 | |
| 5231 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5232 | } |
| 5233 | |
| 5234 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
| 5235 | struct btrfs_path *path, |
| 5236 | struct btrfs_key *extent_key, |
| 5237 | struct btrfs_block_group_cache *group, |
| 5238 | struct inode *reloc_inode, int pass) |
| 5239 | { |
| 5240 | struct btrfs_trans_handle *trans; |
| 5241 | struct btrfs_root *found_root; |
| 5242 | struct btrfs_ref_path *ref_path = NULL; |
| 5243 | struct disk_extent *new_extents = NULL; |
| 5244 | int nr_extents = 0; |
| 5245 | int loops; |
| 5246 | int ret; |
| 5247 | int level; |
| 5248 | struct btrfs_key first_key; |
| 5249 | u64 prev_block = 0; |
| 5250 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5251 | |
| 5252 | trans = btrfs_start_transaction(extent_root, 1); |
| 5253 | BUG_ON(!trans); |
| 5254 | |
| 5255 | if (extent_key->objectid == 0) { |
| 5256 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 5257 | goto out; |
| 5258 | } |
| 5259 | |
| 5260 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 5261 | if (!ref_path) { |
| 5262 | ret = -ENOMEM; |
| 5263 | goto out; |
| 5264 | } |
| 5265 | |
| 5266 | for (loops = 0; ; loops++) { |
| 5267 | if (loops == 0) { |
| 5268 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 5269 | extent_key->objectid); |
| 5270 | } else { |
| 5271 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 5272 | } |
| 5273 | if (ret < 0) |
| 5274 | goto out; |
| 5275 | if (ret > 0) |
| 5276 | break; |
| 5277 | |
| 5278 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5279 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 5280 | continue; |
| 5281 | |
| 5282 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 5283 | BUG_ON(!found_root); |
| 5284 | /* |
| 5285 | * for reference counted tree, only process reference paths |
| 5286 | * rooted at the latest committed root. |
| 5287 | */ |
| 5288 | if (found_root->ref_cows && |
| 5289 | ref_path->root_generation != found_root->root_key.offset) |
| 5290 | continue; |
| 5291 | |
| 5292 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5293 | if (pass == 0) { |
| 5294 | /* |
| 5295 | * copy data extents to new locations |
| 5296 | */ |
| 5297 | u64 group_start = group->key.objectid; |
| 5298 | ret = relocate_data_extent(reloc_inode, |
| 5299 | extent_key, |
| 5300 | group_start); |
| 5301 | if (ret < 0) |
| 5302 | goto out; |
| 5303 | break; |
| 5304 | } |
| 5305 | level = 0; |
| 5306 | } else { |
| 5307 | level = ref_path->owner_objectid; |
| 5308 | } |
| 5309 | |
| 5310 | if (prev_block != ref_path->nodes[level]) { |
| 5311 | struct extent_buffer *eb; |
| 5312 | u64 block_start = ref_path->nodes[level]; |
| 5313 | u64 block_size = btrfs_level_size(found_root, level); |
| 5314 | |
| 5315 | eb = read_tree_block(found_root, block_start, |
| 5316 | block_size, 0); |
| 5317 | btrfs_tree_lock(eb); |
| 5318 | BUG_ON(level != btrfs_header_level(eb)); |
| 5319 | |
| 5320 | if (level == 0) |
| 5321 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 5322 | else |
| 5323 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 5324 | |
| 5325 | btrfs_tree_unlock(eb); |
| 5326 | free_extent_buffer(eb); |
| 5327 | prev_block = block_start; |
| 5328 | } |
| 5329 | |
| 5330 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID && |
| 5331 | pass >= 2) { |
| 5332 | /* |
| 5333 | * use fallback method to process the remaining |
| 5334 | * references. |
| 5335 | */ |
| 5336 | if (!new_extents) { |
| 5337 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5338 | new_extents = kmalloc(sizeof(*new_extents), |
| 5339 | GFP_NOFS); |
| 5340 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5341 | ret = get_new_locations(reloc_inode, |
| 5342 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5343 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5344 | &new_extents, |
| 5345 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5346 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5347 | goto out; |
| 5348 | } |
| 5349 | btrfs_record_root_in_trans(found_root); |
| 5350 | ret = replace_one_extent(trans, found_root, |
| 5351 | path, extent_key, |
| 5352 | &first_key, ref_path, |
| 5353 | new_extents, nr_extents); |
| 5354 | if (ret < 0) |
| 5355 | goto out; |
| 5356 | continue; |
| 5357 | } |
| 5358 | |
| 5359 | btrfs_record_root_in_trans(found_root); |
| 5360 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5361 | ret = relocate_tree_block(trans, found_root, path, |
| 5362 | &first_key, ref_path); |
| 5363 | } else { |
| 5364 | /* |
| 5365 | * try to update data extent references while |
| 5366 | * keeping metadata shared between snapshots. |
| 5367 | */ |
| 5368 | ret = relocate_one_path(trans, found_root, path, |
| 5369 | &first_key, ref_path, |
| 5370 | group, reloc_inode); |
| 5371 | } |
| 5372 | if (ret < 0) |
| 5373 | goto out; |
| 5374 | } |
| 5375 | ret = 0; |
| 5376 | out: |
| 5377 | btrfs_end_transaction(trans, extent_root); |
| 5378 | kfree(new_extents); |
| 5379 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5380 | return ret; |
| 5381 | } |
| 5382 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5383 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 5384 | { |
| 5385 | u64 num_devices; |
| 5386 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 5387 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 5388 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5389 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5390 | if (num_devices == 1) { |
| 5391 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5392 | stripped = flags & ~stripped; |
| 5393 | |
| 5394 | /* turn raid0 into single device chunks */ |
| 5395 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 5396 | return stripped; |
| 5397 | |
| 5398 | /* turn mirroring into duplication */ |
| 5399 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5400 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5401 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 5402 | return flags; |
| 5403 | } else { |
| 5404 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5405 | if (flags & stripped) |
| 5406 | return flags; |
| 5407 | |
| 5408 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5409 | stripped = flags & ~stripped; |
| 5410 | |
| 5411 | /* switch duplicated blocks with raid1 */ |
| 5412 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 5413 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 5414 | |
| 5415 | /* turn single device chunks into raid0 */ |
| 5416 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 5417 | } |
| 5418 | return flags; |
| 5419 | } |
| 5420 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5421 | int __alloc_chunk_for_shrink(struct btrfs_root *root, |
| 5422 | struct btrfs_block_group_cache *shrink_block_group, |
| 5423 | int force) |
| 5424 | { |
| 5425 | struct btrfs_trans_handle *trans; |
| 5426 | u64 new_alloc_flags; |
| 5427 | u64 calc; |
| 5428 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5429 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5430 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5431 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5432 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5433 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5434 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5435 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5436 | new_alloc_flags = update_block_group_flags(root, |
| 5437 | shrink_block_group->flags); |
| 5438 | if (new_alloc_flags != shrink_block_group->flags) { |
| 5439 | calc = |
| 5440 | btrfs_block_group_used(&shrink_block_group->item); |
| 5441 | } else { |
| 5442 | calc = shrink_block_group->key.offset; |
| 5443 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5444 | spin_unlock(&shrink_block_group->lock); |
| 5445 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5446 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5447 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5448 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5449 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5450 | } else |
| 5451 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5452 | return 0; |
| 5453 | } |
| 5454 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5455 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 5456 | struct btrfs_root *root, |
| 5457 | u64 objectid, u64 size) |
| 5458 | { |
| 5459 | struct btrfs_path *path; |
| 5460 | struct btrfs_inode_item *item; |
| 5461 | struct extent_buffer *leaf; |
| 5462 | int ret; |
| 5463 | |
| 5464 | path = btrfs_alloc_path(); |
| 5465 | if (!path) |
| 5466 | return -ENOMEM; |
| 5467 | |
| 5468 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 5469 | if (ret) |
| 5470 | goto out; |
| 5471 | |
| 5472 | leaf = path->nodes[0]; |
| 5473 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 5474 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 5475 | btrfs_set_inode_generation(leaf, item, 1); |
| 5476 | btrfs_set_inode_size(leaf, item, size); |
| 5477 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5478 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM | |
| 5479 | BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5480 | btrfs_mark_buffer_dirty(leaf); |
| 5481 | btrfs_release_path(root, path); |
| 5482 | out: |
| 5483 | btrfs_free_path(path); |
| 5484 | return ret; |
| 5485 | } |
| 5486 | |
| 5487 | static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info, |
| 5488 | struct btrfs_block_group_cache *group) |
| 5489 | { |
| 5490 | struct inode *inode = NULL; |
| 5491 | struct btrfs_trans_handle *trans; |
| 5492 | struct btrfs_root *root; |
| 5493 | struct btrfs_key root_key; |
| 5494 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 5495 | int err = 0; |
| 5496 | |
| 5497 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5498 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5499 | root_key.offset = (u64)-1; |
| 5500 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5501 | if (IS_ERR(root)) |
| 5502 | return ERR_CAST(root); |
| 5503 | |
| 5504 | trans = btrfs_start_transaction(root, 1); |
| 5505 | BUG_ON(!trans); |
| 5506 | |
| 5507 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 5508 | if (err) |
| 5509 | goto out; |
| 5510 | |
| 5511 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 5512 | BUG_ON(err); |
| 5513 | |
| 5514 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5515 | group->key.offset, 0, group->key.offset, |
| 5516 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5517 | BUG_ON(err); |
| 5518 | |
| 5519 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 5520 | if (inode->i_state & I_NEW) { |
| 5521 | BTRFS_I(inode)->root = root; |
| 5522 | BTRFS_I(inode)->location.objectid = objectid; |
| 5523 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5524 | BTRFS_I(inode)->location.offset = 0; |
| 5525 | btrfs_read_locked_inode(inode); |
| 5526 | unlock_new_inode(inode); |
| 5527 | BUG_ON(is_bad_inode(inode)); |
| 5528 | } else { |
| 5529 | BUG_ON(1); |
| 5530 | } |
| 5531 | |
| 5532 | err = btrfs_orphan_add(trans, inode); |
| 5533 | out: |
| 5534 | btrfs_end_transaction(trans, root); |
| 5535 | if (err) { |
| 5536 | if (inode) |
| 5537 | iput(inode); |
| 5538 | inode = ERR_PTR(err); |
| 5539 | } |
| 5540 | return inode; |
| 5541 | } |
| 5542 | |
| 5543 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5544 | { |
| 5545 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5546 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5547 | struct btrfs_fs_info *info = root->fs_info; |
| 5548 | struct extent_buffer *leaf; |
| 5549 | struct inode *reloc_inode; |
| 5550 | struct btrfs_block_group_cache *block_group; |
| 5551 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5552 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5553 | u64 cur_byte; |
| 5554 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5555 | u32 nritems; |
| 5556 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5557 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5558 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5559 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5560 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5561 | |
| 5562 | block_group = btrfs_lookup_block_group(info, group_start); |
| 5563 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5564 | |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5565 | printk("btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5566 | (unsigned long long)block_group->key.objectid, |
| 5567 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5568 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5569 | path = btrfs_alloc_path(); |
| 5570 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5571 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5572 | reloc_inode = create_reloc_inode(info, block_group); |
| 5573 | BUG_ON(IS_ERR(reloc_inode)); |
| 5574 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5575 | __alloc_chunk_for_shrink(root, block_group, 1); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5576 | set_block_group_readonly(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5577 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5578 | btrfs_start_delalloc_inodes(info->tree_root); |
| 5579 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5580 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5581 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5582 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5583 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5584 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5585 | key.offset = 0; |
| 5586 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5587 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5588 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5589 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5590 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5591 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5592 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 5593 | btrfs_clean_old_snapshots(info->tree_root); |
| 5594 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 5595 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5596 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5597 | while(1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5598 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5599 | if (ret < 0) |
| 5600 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5601 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5602 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5603 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5604 | if (path->slots[0] >= nritems) { |
| 5605 | ret = btrfs_next_leaf(root, path); |
| 5606 | if (ret < 0) |
| 5607 | goto out; |
| 5608 | if (ret == 1) { |
| 5609 | ret = 0; |
| 5610 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5611 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5612 | leaf = path->nodes[0]; |
| 5613 | nritems = btrfs_header_nritems(leaf); |
| 5614 | } |
| 5615 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5616 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5617 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5618 | if (key.objectid >= block_group->key.objectid + |
| 5619 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5620 | break; |
| 5621 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5622 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5623 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5624 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5625 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5626 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5627 | } |
| 5628 | progress = 1; |
| 5629 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5630 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 5631 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5632 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5633 | goto next; |
| 5634 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5635 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5636 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5637 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5638 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5639 | |
| 5640 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 5641 | ret = relocate_one_extent(root, path, &key, block_group, |
| 5642 | reloc_inode, pass); |
| 5643 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5644 | if (ret > 0) |
| 5645 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5646 | |
| 5647 | key.objectid = cur_byte; |
| 5648 | key.type = 0; |
| 5649 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5653 | |
| 5654 | if (pass == 0) { |
| 5655 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 5656 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
| 5657 | WARN_ON(reloc_inode->i_mapping->nrpages); |
| 5658 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5659 | |
| 5660 | if (total_found > 0) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5661 | printk("btrfs found %llu extents in pass %d\n", |
| 5662 | (unsigned long long)total_found, pass); |
| 5663 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5664 | if (total_found == skipped && pass > 2) { |
| 5665 | iput(reloc_inode); |
| 5666 | reloc_inode = create_reloc_inode(info, block_group); |
| 5667 | pass = 0; |
| 5668 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5669 | goto again; |
| 5670 | } |
| 5671 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5672 | /* delete reloc_inode */ |
| 5673 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5674 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5675 | /* unpin extents in this range */ |
| 5676 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5677 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5678 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5679 | spin_lock(&block_group->lock); |
| 5680 | WARN_ON(block_group->pinned > 0); |
| 5681 | WARN_ON(block_group->reserved > 0); |
| 5682 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 5683 | spin_unlock(&block_group->lock); |
| 5684 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5685 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5686 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5687 | return ret; |
| 5688 | } |
| 5689 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5690 | int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, |
| 5691 | struct btrfs_key *key) |
| 5692 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5693 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5694 | struct btrfs_key found_key; |
| 5695 | struct extent_buffer *leaf; |
| 5696 | int slot; |
| 5697 | |
| 5698 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 5699 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5700 | goto out; |
| 5701 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5702 | while(1) { |
| 5703 | slot = path->slots[0]; |
| 5704 | leaf = path->nodes[0]; |
| 5705 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5706 | ret = btrfs_next_leaf(root, path); |
| 5707 | if (ret == 0) |
| 5708 | continue; |
| 5709 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5710 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5711 | break; |
| 5712 | } |
| 5713 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5714 | |
| 5715 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5716 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 5717 | ret = 0; |
| 5718 | goto out; |
| 5719 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5720 | path->slots[0]++; |
| 5721 | } |
| 5722 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5723 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5724 | return ret; |
| 5725 | } |
| 5726 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5727 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 5728 | { |
| 5729 | struct btrfs_block_group_cache *block_group; |
| 5730 | struct rb_node *n; |
| 5731 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5732 | spin_lock(&info->block_group_cache_lock); |
| 5733 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 5734 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 5735 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5736 | rb_erase(&block_group->cache_node, |
| 5737 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5738 | spin_unlock(&info->block_group_cache_lock); |
| 5739 | |
| 5740 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5741 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5742 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5743 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5744 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5745 | |
| 5746 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5747 | } |
| 5748 | spin_unlock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5749 | return 0; |
| 5750 | } |
| 5751 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5752 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 5753 | { |
| 5754 | struct btrfs_path *path; |
| 5755 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5756 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5757 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5758 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5759 | struct btrfs_key key; |
| 5760 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5761 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5762 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5763 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5764 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5765 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5766 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5767 | path = btrfs_alloc_path(); |
| 5768 | if (!path) |
| 5769 | return -ENOMEM; |
| 5770 | |
| 5771 | while(1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5772 | ret = find_first_block_group(root, path, &key); |
| 5773 | if (ret > 0) { |
| 5774 | ret = 0; |
| 5775 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5776 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5777 | if (ret != 0) |
| 5778 | goto error; |
| 5779 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5780 | leaf = path->nodes[0]; |
| 5781 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5782 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5783 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5784 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5785 | break; |
| 5786 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5787 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5788 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5789 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5790 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5791 | read_extent_buffer(leaf, &cache->item, |
| 5792 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 5793 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5794 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5795 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5796 | key.objectid = found_key.objectid + found_key.offset; |
| 5797 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5798 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5799 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5800 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 5801 | btrfs_block_group_used(&cache->item), |
| 5802 | &space_info); |
| 5803 | BUG_ON(ret); |
| 5804 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5805 | down_write(&space_info->groups_sem); |
| 5806 | list_add_tail(&cache->list, &space_info->block_groups); |
| 5807 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5808 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5809 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5810 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 5811 | |
| 5812 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5813 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 5814 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5815 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5816 | ret = 0; |
| 5817 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5818 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5819 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5820 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5821 | |
| 5822 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 5823 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5824 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5825 | u64 size) |
| 5826 | { |
| 5827 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5828 | struct btrfs_root *extent_root; |
| 5829 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5830 | |
| 5831 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5832 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5833 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
| 5834 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5835 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5836 | if (!cache) |
| 5837 | return -ENOMEM; |
| 5838 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5839 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5840 | cache->key.offset = size; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5841 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5842 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5843 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5844 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5845 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5846 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5847 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 5848 | cache->flags = type; |
| 5849 | btrfs_set_block_group_flags(&cache->item, type); |
| 5850 | |
| 5851 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 5852 | &cache->space_info); |
| 5853 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5854 | down_write(&cache->space_info->groups_sem); |
| 5855 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 5856 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5857 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5858 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5859 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5860 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5861 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 5862 | sizeof(cache->item)); |
| 5863 | BUG_ON(ret); |
| 5864 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 5865 | finish_current_insert(trans, extent_root, 0); |
| 5866 | ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5867 | BUG_ON(ret); |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 5868 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5869 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5870 | return 0; |
| 5871 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5872 | |
| 5873 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 5874 | struct btrfs_root *root, u64 group_start) |
| 5875 | { |
| 5876 | struct btrfs_path *path; |
| 5877 | struct btrfs_block_group_cache *block_group; |
| 5878 | struct btrfs_key key; |
| 5879 | int ret; |
| 5880 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5881 | root = root->fs_info->extent_root; |
| 5882 | |
| 5883 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 5884 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5885 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5886 | |
| 5887 | memcpy(&key, &block_group->key, sizeof(key)); |
| 5888 | |
| 5889 | path = btrfs_alloc_path(); |
| 5890 | BUG_ON(!path); |
| 5891 | |
| 5892 | btrfs_remove_free_space_cache(block_group); |
| 5893 | rb_erase(&block_group->cache_node, |
| 5894 | &root->fs_info->block_group_cache_tree); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5895 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5896 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5897 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5898 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5899 | spin_lock(&block_group->space_info->lock); |
| 5900 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 5901 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 5902 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5903 | block_group->space_info->full = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5904 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5905 | /* |
| 5906 | memset(shrink_block_group, 0, sizeof(*shrink_block_group)); |
| 5907 | kfree(shrink_block_group); |
| 5908 | */ |
| 5909 | |
| 5910 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 5911 | if (ret > 0) |
| 5912 | ret = -EIO; |
| 5913 | if (ret < 0) |
| 5914 | goto out; |
| 5915 | |
| 5916 | ret = btrfs_del_item(trans, root, path); |
| 5917 | out: |
| 5918 | btrfs_free_path(path); |
| 5919 | return ret; |
| 5920 | } |