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; |
| 801 | cur = insert_list->next; |
| 802 | op = list_entry(cur, struct pending_extent_op, list); |
| 803 | total--; |
| 804 | } else { |
| 805 | i += ret; |
| 806 | } |
| 807 | |
| 808 | cond_resched(); |
| 809 | } |
| 810 | ret = 0; |
| 811 | kfree(keys); |
| 812 | kfree(data_size); |
| 813 | return ret; |
| 814 | } |
| 815 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 816 | static int noinline insert_extent_backref(struct btrfs_trans_handle *trans, |
| 817 | struct btrfs_root *root, |
| 818 | struct btrfs_path *path, |
| 819 | u64 bytenr, u64 parent, |
| 820 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 821 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 822 | { |
| 823 | struct btrfs_key key; |
| 824 | struct extent_buffer *leaf; |
| 825 | struct btrfs_extent_ref *ref; |
| 826 | u32 num_refs; |
| 827 | int ret; |
| 828 | |
| 829 | key.objectid = bytenr; |
| 830 | key.type = BTRFS_EXTENT_REF_KEY; |
| 831 | key.offset = parent; |
| 832 | |
| 833 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
| 834 | if (ret == 0) { |
| 835 | leaf = path->nodes[0]; |
| 836 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 837 | struct btrfs_extent_ref); |
| 838 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 839 | btrfs_set_ref_generation(leaf, ref, ref_generation); |
| 840 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 841 | btrfs_set_ref_num_refs(leaf, ref, 1); |
| 842 | } else if (ret == -EEXIST) { |
| 843 | u64 existing_owner; |
| 844 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); |
| 845 | leaf = path->nodes[0]; |
| 846 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 847 | struct btrfs_extent_ref); |
| 848 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 849 | btrfs_ref_generation(leaf, ref) != ref_generation) { |
| 850 | ret = -EIO; |
| 851 | WARN_ON(1); |
| 852 | goto out; |
| 853 | } |
| 854 | |
| 855 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 856 | BUG_ON(num_refs == 0); |
| 857 | btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); |
| 858 | |
| 859 | existing_owner = btrfs_ref_objectid(leaf, ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 860 | if (existing_owner != owner_objectid && |
| 861 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 862 | btrfs_set_ref_objectid(leaf, ref, |
| 863 | BTRFS_MULTIPLE_OBJECTIDS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 864 | } |
| 865 | ret = 0; |
| 866 | } else { |
| 867 | goto out; |
| 868 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 869 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 870 | out: |
| 871 | btrfs_release_path(root, path); |
| 872 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 873 | } |
| 874 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 875 | static int noinline remove_extent_backref(struct btrfs_trans_handle *trans, |
| 876 | struct btrfs_root *root, |
| 877 | struct btrfs_path *path) |
| 878 | { |
| 879 | struct extent_buffer *leaf; |
| 880 | struct btrfs_extent_ref *ref; |
| 881 | u32 num_refs; |
| 882 | int ret = 0; |
| 883 | |
| 884 | leaf = path->nodes[0]; |
| 885 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 886 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 887 | BUG_ON(num_refs == 0); |
| 888 | num_refs -= 1; |
| 889 | if (num_refs == 0) { |
| 890 | ret = btrfs_del_item(trans, root, path); |
| 891 | } else { |
| 892 | btrfs_set_ref_num_refs(leaf, ref, num_refs); |
| 893 | btrfs_mark_buffer_dirty(leaf); |
| 894 | } |
| 895 | btrfs_release_path(root, path); |
| 896 | return ret; |
| 897 | } |
| 898 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 899 | static int noinline free_extents(struct btrfs_trans_handle *trans, |
| 900 | struct btrfs_root *extent_root, |
| 901 | struct list_head *del_list) |
| 902 | { |
| 903 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 904 | struct btrfs_path *path; |
| 905 | struct btrfs_key key, found_key; |
| 906 | struct extent_buffer *leaf; |
| 907 | struct list_head *cur; |
| 908 | struct pending_extent_op *op; |
| 909 | struct btrfs_extent_item *ei; |
| 910 | int ret, num_to_del, extent_slot = 0, found_extent = 0; |
| 911 | u32 refs; |
| 912 | u64 bytes_freed = 0; |
| 913 | |
| 914 | path = btrfs_alloc_path(); |
| 915 | if (!path) |
| 916 | return -ENOMEM; |
| 917 | path->reada = 1; |
| 918 | |
| 919 | search: |
| 920 | /* search for the backref for the current ref we want to delete */ |
| 921 | cur = del_list->next; |
| 922 | op = list_entry(cur, struct pending_extent_op, list); |
| 923 | ret = lookup_extent_backref(trans, extent_root, path, op->bytenr, |
| 924 | op->orig_parent, |
| 925 | extent_root->root_key.objectid, |
| 926 | op->orig_generation, op->level, 1); |
| 927 | if (ret) { |
| 928 | printk("Unable to find backref byte nr %Lu root %Lu gen %Lu " |
| 929 | "owner %u\n", op->bytenr, |
| 930 | extent_root->root_key.objectid, op->orig_generation, |
| 931 | op->level); |
| 932 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 933 | WARN_ON(1); |
| 934 | goto out; |
| 935 | } |
| 936 | |
| 937 | extent_slot = path->slots[0]; |
| 938 | num_to_del = 1; |
| 939 | found_extent = 0; |
| 940 | |
| 941 | /* |
| 942 | * if we aren't the first item on the leaf we can move back one and see |
| 943 | * if our ref is right next to our extent item |
| 944 | */ |
| 945 | if (likely(extent_slot)) { |
| 946 | extent_slot--; |
| 947 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 948 | extent_slot); |
| 949 | if (found_key.objectid == op->bytenr && |
| 950 | found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 951 | found_key.offset == op->num_bytes) { |
| 952 | num_to_del++; |
| 953 | found_extent = 1; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * if we didn't find the extent we need to delete the backref and then |
| 959 | * search for the extent item key so we can update its ref count |
| 960 | */ |
| 961 | if (!found_extent) { |
| 962 | key.objectid = op->bytenr; |
| 963 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 964 | key.offset = op->num_bytes; |
| 965 | |
| 966 | ret = remove_extent_backref(trans, extent_root, path); |
| 967 | BUG_ON(ret); |
| 968 | btrfs_release_path(extent_root, path); |
| 969 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
| 970 | BUG_ON(ret); |
| 971 | extent_slot = path->slots[0]; |
| 972 | } |
| 973 | |
| 974 | /* this is where we update the ref count for the extent */ |
| 975 | leaf = path->nodes[0]; |
| 976 | ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item); |
| 977 | refs = btrfs_extent_refs(leaf, ei); |
| 978 | BUG_ON(refs == 0); |
| 979 | refs--; |
| 980 | btrfs_set_extent_refs(leaf, ei, refs); |
| 981 | |
| 982 | btrfs_mark_buffer_dirty(leaf); |
| 983 | |
| 984 | /* |
| 985 | * This extent needs deleting. The reason cur_slot is extent_slot + |
| 986 | * num_to_del is because extent_slot points to the slot where the extent |
| 987 | * is, and if the backref was not right next to the extent we will be |
| 988 | * deleting at least 1 item, and will want to start searching at the |
| 989 | * slot directly next to extent_slot. However if we did find the |
| 990 | * backref next to the extent item them we will be deleting at least 2 |
| 991 | * items and will want to start searching directly after the ref slot |
| 992 | */ |
| 993 | if (!refs) { |
| 994 | struct list_head *pos, *n, *end; |
| 995 | int cur_slot = extent_slot+num_to_del; |
| 996 | u64 super_used; |
| 997 | u64 root_used; |
| 998 | |
| 999 | path->slots[0] = extent_slot; |
| 1000 | bytes_freed = op->num_bytes; |
| 1001 | |
| 1002 | /* |
| 1003 | * we need to see if we can delete multiple things at once, so |
| 1004 | * start looping through the list of extents we are wanting to |
| 1005 | * delete and see if their extent/backref's are right next to |
| 1006 | * eachother and the extents only have 1 ref |
| 1007 | */ |
| 1008 | for (pos = cur->next; pos != del_list; pos = pos->next) { |
| 1009 | struct pending_extent_op *tmp; |
| 1010 | |
| 1011 | tmp = list_entry(pos, struct pending_extent_op, list); |
| 1012 | |
| 1013 | /* we only want to delete extent+ref at this stage */ |
| 1014 | if (cur_slot >= btrfs_header_nritems(leaf) - 1) |
| 1015 | break; |
| 1016 | |
| 1017 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot); |
| 1018 | if (found_key.objectid != tmp->bytenr || |
| 1019 | found_key.type != BTRFS_EXTENT_ITEM_KEY || |
| 1020 | found_key.offset != tmp->num_bytes) |
| 1021 | break; |
| 1022 | |
| 1023 | /* check to make sure this extent only has one ref */ |
| 1024 | ei = btrfs_item_ptr(leaf, cur_slot, |
| 1025 | struct btrfs_extent_item); |
| 1026 | if (btrfs_extent_refs(leaf, ei) != 1) |
| 1027 | break; |
| 1028 | |
| 1029 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1); |
| 1030 | if (found_key.objectid != tmp->bytenr || |
| 1031 | found_key.type != BTRFS_EXTENT_REF_KEY || |
| 1032 | found_key.offset != tmp->orig_parent) |
| 1033 | break; |
| 1034 | |
| 1035 | /* |
| 1036 | * the ref is right next to the extent, we can set the |
| 1037 | * ref count to 0 since we will delete them both now |
| 1038 | */ |
| 1039 | btrfs_set_extent_refs(leaf, ei, 0); |
| 1040 | |
| 1041 | /* pin down the bytes for this extent */ |
| 1042 | mutex_lock(&info->pinned_mutex); |
| 1043 | ret = pin_down_bytes(trans, extent_root, tmp->bytenr, |
| 1044 | tmp->num_bytes, tmp->level >= |
| 1045 | BTRFS_FIRST_FREE_OBJECTID); |
| 1046 | mutex_unlock(&info->pinned_mutex); |
| 1047 | BUG_ON(ret < 0); |
| 1048 | |
| 1049 | /* |
| 1050 | * use the del field to tell if we need to go ahead and |
| 1051 | * free up the extent when we delete the item or not. |
| 1052 | */ |
| 1053 | tmp->del = ret; |
| 1054 | bytes_freed += tmp->num_bytes; |
| 1055 | |
| 1056 | num_to_del += 2; |
| 1057 | cur_slot += 2; |
| 1058 | } |
| 1059 | end = pos; |
| 1060 | |
| 1061 | /* update the free space counters */ |
| 1062 | spin_lock_irq(&info->delalloc_lock); |
| 1063 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1064 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1065 | super_used - bytes_freed); |
| 1066 | spin_unlock_irq(&info->delalloc_lock); |
| 1067 | |
| 1068 | root_used = btrfs_root_used(&extent_root->root_item); |
| 1069 | btrfs_set_root_used(&extent_root->root_item, |
| 1070 | root_used - bytes_freed); |
| 1071 | |
| 1072 | /* delete the items */ |
| 1073 | ret = btrfs_del_items(trans, extent_root, path, |
| 1074 | path->slots[0], num_to_del); |
| 1075 | BUG_ON(ret); |
| 1076 | |
| 1077 | /* |
| 1078 | * loop through the extents we deleted and do the cleanup work |
| 1079 | * on them |
| 1080 | */ |
| 1081 | for (pos = cur, n = pos->next; pos != end; |
| 1082 | pos = n, n = pos->next) { |
| 1083 | struct pending_extent_op *tmp; |
| 1084 | #ifdef BIO_RW_DISCARD |
| 1085 | u64 map_length; |
| 1086 | struct btrfs_multi_bio *multi = NULL; |
| 1087 | #endif |
| 1088 | tmp = list_entry(pos, struct pending_extent_op, list); |
| 1089 | |
| 1090 | /* |
| 1091 | * remember tmp->del tells us wether or not we pinned |
| 1092 | * down the extent |
| 1093 | */ |
| 1094 | ret = update_block_group(trans, extent_root, |
| 1095 | tmp->bytenr, tmp->num_bytes, 0, |
| 1096 | tmp->del); |
| 1097 | BUG_ON(ret); |
| 1098 | |
| 1099 | #ifdef BIO_RW_DISCARD |
| 1100 | ret = btrfs_map_block(&info->mapping_tree, READ, |
| 1101 | tmp->bytenr, &map_length, &multi, |
| 1102 | 0); |
| 1103 | if (!ret) { |
| 1104 | struct btrfs_bio_stripe *stripe; |
| 1105 | int i; |
| 1106 | |
| 1107 | stripe = multi->stripe; |
| 1108 | |
| 1109 | if (map_length > tmp->num_bytes) |
| 1110 | map_length = tmp->num_bytes; |
| 1111 | |
| 1112 | for (i = 0; i < multi->num_stripes; |
| 1113 | i++, stripe++) |
| 1114 | blkdev_issue_discard(stripe->dev->bdev, |
| 1115 | stripe->physical >> 9, |
| 1116 | map_length >> 9); |
| 1117 | kfree(multi); |
| 1118 | } |
| 1119 | #endif |
| 1120 | list_del_init(&tmp->list); |
| 1121 | unlock_extent(&info->extent_ins, tmp->bytenr, |
| 1122 | tmp->bytenr + tmp->num_bytes - 1, |
| 1123 | GFP_NOFS); |
| 1124 | kfree(tmp); |
| 1125 | } |
| 1126 | } else if (refs && found_extent) { |
| 1127 | /* |
| 1128 | * the ref and extent were right next to eachother, but the |
| 1129 | * extent still has a ref, so just free the backref and keep |
| 1130 | * going |
| 1131 | */ |
| 1132 | ret = remove_extent_backref(trans, extent_root, path); |
| 1133 | BUG_ON(ret); |
| 1134 | |
| 1135 | list_del_init(&op->list); |
| 1136 | unlock_extent(&info->extent_ins, op->bytenr, |
| 1137 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 1138 | kfree(op); |
| 1139 | } else { |
| 1140 | /* |
| 1141 | * the extent has multiple refs and the backref we were looking |
| 1142 | * for was not right next to it, so just unlock and go next, |
| 1143 | * we're good to go |
| 1144 | */ |
| 1145 | list_del_init(&op->list); |
| 1146 | unlock_extent(&info->extent_ins, op->bytenr, |
| 1147 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 1148 | kfree(op); |
| 1149 | } |
| 1150 | |
| 1151 | btrfs_release_path(extent_root, path); |
| 1152 | if (!list_empty(del_list)) |
| 1153 | goto search; |
| 1154 | |
| 1155 | out: |
| 1156 | btrfs_free_path(path); |
| 1157 | return ret; |
| 1158 | } |
| 1159 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1160 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 1161 | struct btrfs_root *root, u64 bytenr, |
| 1162 | u64 orig_parent, u64 parent, |
| 1163 | u64 orig_root, u64 ref_root, |
| 1164 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1165 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1166 | { |
| 1167 | int ret; |
| 1168 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1169 | struct btrfs_path *path; |
| 1170 | |
| 1171 | if (root == root->fs_info->extent_root) { |
| 1172 | struct pending_extent_op *extent_op; |
| 1173 | u64 num_bytes; |
| 1174 | |
| 1175 | BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL); |
| 1176 | num_bytes = btrfs_level_size(root, (int)owner_objectid); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1177 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1178 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1179 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1180 | u64 priv; |
| 1181 | ret = get_state_private(&root->fs_info->extent_ins, |
| 1182 | bytenr, &priv); |
| 1183 | BUG_ON(ret); |
| 1184 | extent_op = (struct pending_extent_op *) |
| 1185 | (unsigned long)priv; |
| 1186 | BUG_ON(extent_op->parent != orig_parent); |
| 1187 | BUG_ON(extent_op->generation != orig_generation); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1188 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1189 | extent_op->parent = parent; |
| 1190 | extent_op->generation = ref_generation; |
| 1191 | } else { |
| 1192 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 1193 | BUG_ON(!extent_op); |
| 1194 | |
| 1195 | extent_op->type = PENDING_BACKREF_UPDATE; |
| 1196 | extent_op->bytenr = bytenr; |
| 1197 | extent_op->num_bytes = num_bytes; |
| 1198 | extent_op->parent = parent; |
| 1199 | extent_op->orig_parent = orig_parent; |
| 1200 | extent_op->generation = ref_generation; |
| 1201 | extent_op->orig_generation = orig_generation; |
| 1202 | extent_op->level = (int)owner_objectid; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1203 | INIT_LIST_HEAD(&extent_op->list); |
| 1204 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1205 | |
| 1206 | set_extent_bits(&root->fs_info->extent_ins, |
| 1207 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1208 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1209 | set_state_private(&root->fs_info->extent_ins, |
| 1210 | bytenr, (unsigned long)extent_op); |
| 1211 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1212 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | path = btrfs_alloc_path(); |
| 1217 | if (!path) |
| 1218 | return -ENOMEM; |
| 1219 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1220 | bytenr, orig_parent, orig_root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1221 | orig_generation, owner_objectid, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1222 | if (ret) |
| 1223 | goto out; |
| 1224 | ret = remove_extent_backref(trans, extent_root, path); |
| 1225 | if (ret) |
| 1226 | goto out; |
| 1227 | ret = insert_extent_backref(trans, extent_root, path, bytenr, |
| 1228 | parent, ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1229 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1230 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1231 | finish_current_insert(trans, extent_root, 0); |
| 1232 | del_pending_extents(trans, extent_root, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1233 | out: |
| 1234 | btrfs_free_path(path); |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
| 1238 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 1239 | struct btrfs_root *root, u64 bytenr, |
| 1240 | u64 orig_parent, u64 parent, |
| 1241 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1242 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1243 | { |
| 1244 | int ret; |
| 1245 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 1246 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 1247 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1248 | ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, |
| 1249 | parent, ref_root, ref_root, |
| 1250 | ref_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1251 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1252 | return ret; |
| 1253 | } |
| 1254 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1255 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1256 | struct btrfs_root *root, u64 bytenr, |
| 1257 | u64 orig_parent, u64 parent, |
| 1258 | u64 orig_root, u64 ref_root, |
| 1259 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1260 | u64 owner_objectid) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1261 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1262 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1263 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1264 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1265 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1266 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1267 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1268 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1269 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1270 | if (!path) |
| 1271 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1272 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1273 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1274 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1275 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1276 | key.offset = (u64)-1; |
| 1277 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1278 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1279 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1280 | if (ret < 0) |
| 1281 | return ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1282 | BUG_ON(ret == 0 || path->slots[0] == 0); |
| 1283 | |
| 1284 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1285 | l = path->nodes[0]; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1286 | |
| 1287 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1288 | if (key.objectid != bytenr) { |
| 1289 | btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]); |
| 1290 | printk("wanted %Lu found %Lu\n", bytenr, key.objectid); |
| 1291 | BUG(); |
| 1292 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1293 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
| 1294 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1295 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1296 | refs = btrfs_extent_refs(l, item); |
| 1297 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1298 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1299 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1300 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1301 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1302 | path->reada = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1303 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
| 1304 | path, bytenr, parent, |
| 1305 | ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1306 | owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1307 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1308 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 1309 | del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1310 | |
| 1311 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1312 | return 0; |
| 1313 | } |
| 1314 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1315 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1316 | struct btrfs_root *root, |
| 1317 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1318 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1319 | u64 owner_objectid) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1320 | { |
| 1321 | int ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1322 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 1323 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 1324 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1325 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, |
| 1326 | 0, ref_root, 0, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1327 | owner_objectid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1328 | return ret; |
| 1329 | } |
| 1330 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1331 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 1332 | struct btrfs_root *root) |
| 1333 | { |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1334 | finish_current_insert(trans, root->fs_info->extent_root, 1); |
| 1335 | del_pending_extents(trans, root->fs_info->extent_root, 1); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1336 | return 0; |
| 1337 | } |
| 1338 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1339 | int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, |
| 1340 | struct btrfs_root *root, u64 bytenr, |
| 1341 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1342 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1343 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1344 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1345 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1346 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1347 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1348 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1349 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1350 | path = btrfs_alloc_path(); |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1351 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1352 | key.objectid = bytenr; |
| 1353 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1354 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1355 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1356 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1357 | if (ret < 0) |
| 1358 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1359 | if (ret != 0) { |
| 1360 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1361 | printk("failed to find block number %Lu\n", bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1362 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1363 | } |
| 1364 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1365 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1366 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1367 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1368 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1369 | return 0; |
| 1370 | } |
| 1371 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1372 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 1373 | struct btrfs_root *root, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1374 | { |
| 1375 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1376 | struct btrfs_path *path; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1377 | struct extent_buffer *leaf; |
| 1378 | struct btrfs_extent_ref *ref_item; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1379 | struct btrfs_key key; |
| 1380 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1381 | u64 ref_root; |
| 1382 | u64 last_snapshot; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1383 | u32 nritems; |
| 1384 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1385 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1386 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1387 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1388 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1389 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1390 | path = btrfs_alloc_path(); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1391 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 1392 | if (ret < 0) |
| 1393 | goto out; |
| 1394 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1395 | |
| 1396 | ret = -ENOENT; |
| 1397 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1398 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1399 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1400 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1401 | leaf = path->nodes[0]; |
| 1402 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1403 | |
| 1404 | if (found_key.objectid != bytenr || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1405 | found_key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1406 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1407 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1408 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1409 | while (1) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1410 | leaf = path->nodes[0]; |
| 1411 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1412 | if (path->slots[0] >= nritems) { |
| 1413 | ret = btrfs_next_leaf(extent_root, path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1414 | if (ret < 0) |
| 1415 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1416 | if (ret == 0) |
| 1417 | continue; |
| 1418 | break; |
| 1419 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1420 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1421 | if (found_key.objectid != bytenr) |
| 1422 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 1423 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1424 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 1425 | path->slots[0]++; |
| 1426 | continue; |
| 1427 | } |
| 1428 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1429 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1430 | struct btrfs_extent_ref); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1431 | ref_root = btrfs_ref_root(leaf, ref_item); |
| 1432 | if (ref_root != root->root_key.objectid && |
| 1433 | ref_root != BTRFS_TREE_LOG_OBJECTID) { |
| 1434 | ret = 1; |
| 1435 | goto out; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1436 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1437 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { |
| 1438 | ret = 1; |
| 1439 | goto out; |
| 1440 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1441 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1442 | path->slots[0]++; |
| 1443 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1444 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1445 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1446 | btrfs_free_path(path); |
| 1447 | return ret; |
| 1448 | } |
| 1449 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1450 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1451 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1452 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1453 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1454 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1455 | u64 root_gen; |
| 1456 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1457 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1458 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1459 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1460 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1461 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1462 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1463 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1464 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1465 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 1466 | shared = 0; |
| 1467 | root_gen = root->root_key.offset; |
| 1468 | } else { |
| 1469 | shared = 1; |
| 1470 | root_gen = trans->transid - 1; |
| 1471 | } |
| 1472 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1473 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1474 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1475 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1476 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1477 | struct btrfs_leaf_ref *ref; |
| 1478 | struct btrfs_extent_info *info; |
| 1479 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1480 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1481 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1482 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1483 | goto out; |
| 1484 | } |
| 1485 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1486 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1487 | ref->bytenr = buf->start; |
| 1488 | ref->owner = btrfs_header_owner(buf); |
| 1489 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1490 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1491 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1492 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1493 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1494 | u64 disk_bytenr; |
| 1495 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1496 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1497 | continue; |
| 1498 | fi = btrfs_item_ptr(buf, i, |
| 1499 | struct btrfs_file_extent_item); |
| 1500 | if (btrfs_file_extent_type(buf, fi) == |
| 1501 | BTRFS_FILE_EXTENT_INLINE) |
| 1502 | continue; |
| 1503 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1504 | if (disk_bytenr == 0) |
| 1505 | continue; |
| 1506 | |
| 1507 | info->bytenr = disk_bytenr; |
| 1508 | info->num_bytes = |
| 1509 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 1510 | info->objectid = key.objectid; |
| 1511 | info->offset = key.offset; |
| 1512 | info++; |
| 1513 | } |
| 1514 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1515 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1516 | if (ret == -EEXIST && shared) { |
| 1517 | struct btrfs_leaf_ref *old; |
| 1518 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 1519 | BUG_ON(!old); |
| 1520 | btrfs_remove_leaf_ref(root, old); |
| 1521 | btrfs_free_leaf_ref(root, old); |
| 1522 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 1523 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1524 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1525 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1526 | } |
| 1527 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1528 | return ret; |
| 1529 | } |
| 1530 | |
| 1531 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1532 | struct extent_buffer *orig_buf, struct extent_buffer *buf, |
| 1533 | u32 *nr_extents) |
| 1534 | { |
| 1535 | u64 bytenr; |
| 1536 | u64 ref_root; |
| 1537 | u64 orig_root; |
| 1538 | u64 ref_generation; |
| 1539 | u64 orig_generation; |
| 1540 | u32 nritems; |
| 1541 | u32 nr_file_extents = 0; |
| 1542 | struct btrfs_key key; |
| 1543 | struct btrfs_file_extent_item *fi; |
| 1544 | int i; |
| 1545 | int level; |
| 1546 | int ret = 0; |
| 1547 | int faili = 0; |
| 1548 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1549 | u64, u64, u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1550 | |
| 1551 | ref_root = btrfs_header_owner(buf); |
| 1552 | ref_generation = btrfs_header_generation(buf); |
| 1553 | orig_root = btrfs_header_owner(orig_buf); |
| 1554 | orig_generation = btrfs_header_generation(orig_buf); |
| 1555 | |
| 1556 | nritems = btrfs_header_nritems(buf); |
| 1557 | level = btrfs_header_level(buf); |
| 1558 | |
| 1559 | if (root->ref_cows) { |
| 1560 | process_func = __btrfs_inc_extent_ref; |
| 1561 | } else { |
| 1562 | if (level == 0 && |
| 1563 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1564 | goto out; |
| 1565 | if (level != 0 && |
| 1566 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1567 | goto out; |
| 1568 | process_func = __btrfs_update_extent_ref; |
| 1569 | } |
| 1570 | |
| 1571 | for (i = 0; i < nritems; i++) { |
| 1572 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1573 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1574 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1575 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1576 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1577 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1578 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1579 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1580 | BTRFS_FILE_EXTENT_INLINE) |
| 1581 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1582 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1583 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1584 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1585 | |
| 1586 | nr_file_extents++; |
| 1587 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1588 | ret = process_func(trans, root, bytenr, |
| 1589 | orig_buf->start, buf->start, |
| 1590 | orig_root, ref_root, |
| 1591 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1592 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1593 | |
| 1594 | if (ret) { |
| 1595 | faili = i; |
| 1596 | WARN_ON(1); |
| 1597 | goto fail; |
| 1598 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1599 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1600 | bytenr = btrfs_node_blockptr(buf, i); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1601 | ret = process_func(trans, root, bytenr, |
| 1602 | orig_buf->start, buf->start, |
| 1603 | orig_root, ref_root, |
| 1604 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1605 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1606 | if (ret) { |
| 1607 | faili = i; |
| 1608 | WARN_ON(1); |
| 1609 | goto fail; |
| 1610 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1611 | } |
| 1612 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1613 | out: |
| 1614 | if (nr_extents) { |
| 1615 | if (level == 0) |
| 1616 | *nr_extents = nr_file_extents; |
| 1617 | else |
| 1618 | *nr_extents = nritems; |
| 1619 | } |
| 1620 | return 0; |
| 1621 | fail: |
| 1622 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1623 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1624 | } |
| 1625 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1626 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
| 1627 | struct btrfs_root *root, struct extent_buffer *orig_buf, |
| 1628 | struct extent_buffer *buf, int start_slot, int nr) |
| 1629 | |
| 1630 | { |
| 1631 | u64 bytenr; |
| 1632 | u64 ref_root; |
| 1633 | u64 orig_root; |
| 1634 | u64 ref_generation; |
| 1635 | u64 orig_generation; |
| 1636 | struct btrfs_key key; |
| 1637 | struct btrfs_file_extent_item *fi; |
| 1638 | int i; |
| 1639 | int ret; |
| 1640 | int slot; |
| 1641 | int level; |
| 1642 | |
| 1643 | BUG_ON(start_slot < 0); |
| 1644 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); |
| 1645 | |
| 1646 | ref_root = btrfs_header_owner(buf); |
| 1647 | ref_generation = btrfs_header_generation(buf); |
| 1648 | orig_root = btrfs_header_owner(orig_buf); |
| 1649 | orig_generation = btrfs_header_generation(orig_buf); |
| 1650 | level = btrfs_header_level(buf); |
| 1651 | |
| 1652 | if (!root->ref_cows) { |
| 1653 | if (level == 0 && |
| 1654 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1655 | return 0; |
| 1656 | if (level != 0 && |
| 1657 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1658 | return 0; |
| 1659 | } |
| 1660 | |
| 1661 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { |
| 1662 | cond_resched(); |
| 1663 | if (level == 0) { |
| 1664 | btrfs_item_key_to_cpu(buf, &key, slot); |
| 1665 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1666 | continue; |
| 1667 | fi = btrfs_item_ptr(buf, slot, |
| 1668 | struct btrfs_file_extent_item); |
| 1669 | if (btrfs_file_extent_type(buf, fi) == |
| 1670 | BTRFS_FILE_EXTENT_INLINE) |
| 1671 | continue; |
| 1672 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1673 | if (bytenr == 0) |
| 1674 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1675 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1676 | orig_buf->start, buf->start, |
| 1677 | orig_root, ref_root, |
| 1678 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1679 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1680 | if (ret) |
| 1681 | goto fail; |
| 1682 | } else { |
| 1683 | bytenr = btrfs_node_blockptr(buf, slot); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1684 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1685 | orig_buf->start, buf->start, |
| 1686 | orig_root, ref_root, |
| 1687 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1688 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1689 | if (ret) |
| 1690 | goto fail; |
| 1691 | } |
| 1692 | } |
| 1693 | return 0; |
| 1694 | fail: |
| 1695 | WARN_ON(1); |
| 1696 | return -1; |
| 1697 | } |
| 1698 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1699 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1700 | struct btrfs_root *root, |
| 1701 | struct btrfs_path *path, |
| 1702 | struct btrfs_block_group_cache *cache) |
| 1703 | { |
| 1704 | int ret; |
| 1705 | int pending_ret; |
| 1706 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1707 | unsigned long bi; |
| 1708 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1709 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1710 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1711 | if (ret < 0) |
| 1712 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1713 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1714 | |
| 1715 | leaf = path->nodes[0]; |
| 1716 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1717 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1718 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1719 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1720 | fail: |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1721 | finish_current_insert(trans, extent_root, 0); |
| 1722 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1723 | if (ret) |
| 1724 | return ret; |
| 1725 | if (pending_ret) |
| 1726 | return pending_ret; |
| 1727 | return 0; |
| 1728 | |
| 1729 | } |
| 1730 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1731 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1732 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1733 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1734 | struct btrfs_block_group_cache *cache, *entry; |
| 1735 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1736 | int err = 0; |
| 1737 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1738 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1739 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1740 | |
| 1741 | path = btrfs_alloc_path(); |
| 1742 | if (!path) |
| 1743 | return -ENOMEM; |
| 1744 | |
| 1745 | while(1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1746 | cache = NULL; |
| 1747 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1748 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1749 | n; n = rb_next(n)) { |
| 1750 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1751 | cache_node); |
| 1752 | if (entry->dirty) { |
| 1753 | cache = entry; |
| 1754 | break; |
| 1755 | } |
| 1756 | } |
| 1757 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1758 | |
| 1759 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1760 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1761 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1762 | cache->dirty = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1763 | last += cache->key.offset; |
| 1764 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1765 | err = write_one_cache_group(trans, root, |
| 1766 | path, cache); |
| 1767 | /* |
| 1768 | * if we fail to write the cache group, we want |
| 1769 | * to keep it marked dirty in hopes that a later |
| 1770 | * write will work |
| 1771 | */ |
| 1772 | if (err) { |
| 1773 | werr = err; |
| 1774 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | btrfs_free_path(path); |
| 1778 | return werr; |
| 1779 | } |
| 1780 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1781 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1782 | u64 total_bytes, u64 bytes_used, |
| 1783 | struct btrfs_space_info **space_info) |
| 1784 | { |
| 1785 | struct btrfs_space_info *found; |
| 1786 | |
| 1787 | found = __find_space_info(info, flags); |
| 1788 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1789 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1790 | found->total_bytes += total_bytes; |
| 1791 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1792 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1793 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1794 | *space_info = found; |
| 1795 | return 0; |
| 1796 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1797 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1798 | if (!found) |
| 1799 | return -ENOMEM; |
| 1800 | |
| 1801 | list_add(&found->list, &info->space_info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1802 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1803 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1804 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1805 | found->flags = flags; |
| 1806 | found->total_bytes = total_bytes; |
| 1807 | found->bytes_used = bytes_used; |
| 1808 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1809 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1810 | found->bytes_readonly = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1811 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1812 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1813 | *space_info = found; |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1817 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1818 | { |
| 1819 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1820 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1821 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1822 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1823 | if (extra_flags) { |
| 1824 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1825 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1826 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1827 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1828 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1829 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1830 | } |
| 1831 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1832 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1833 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 1834 | { |
| 1835 | spin_lock(&cache->space_info->lock); |
| 1836 | spin_lock(&cache->lock); |
| 1837 | if (!cache->ro) { |
| 1838 | cache->space_info->bytes_readonly += cache->key.offset - |
| 1839 | btrfs_block_group_used(&cache->item); |
| 1840 | cache->ro = 1; |
| 1841 | } |
| 1842 | spin_unlock(&cache->lock); |
| 1843 | spin_unlock(&cache->space_info->lock); |
| 1844 | } |
| 1845 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1846 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1847 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1848 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1849 | |
| 1850 | if (num_devices == 1) |
| 1851 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1852 | if (num_devices < 4) |
| 1853 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1854 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1855 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1856 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1857 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1858 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1859 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1860 | |
| 1861 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1862 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1863 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1864 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1865 | |
| 1866 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1867 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1868 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1869 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1870 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1871 | return flags; |
| 1872 | } |
| 1873 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1874 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1875 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1876 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1877 | { |
| 1878 | struct btrfs_space_info *space_info; |
| 1879 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1880 | int ret = 0; |
| 1881 | |
| 1882 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1883 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1884 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1885 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1886 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1887 | if (!space_info) { |
| 1888 | ret = update_space_info(extent_root->fs_info, flags, |
| 1889 | 0, 0, &space_info); |
| 1890 | BUG_ON(ret); |
| 1891 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1892 | BUG_ON(!space_info); |
| 1893 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1894 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1895 | if (space_info->force_alloc) { |
| 1896 | force = 1; |
| 1897 | space_info->force_alloc = 0; |
| 1898 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1899 | if (space_info->full) { |
| 1900 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1901 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1902 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1903 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1904 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
| 1905 | thresh = div_factor(thresh, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1906 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1907 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1908 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 1909 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1910 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1911 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1912 | spin_unlock(&space_info->lock); |
| 1913 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1914 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1915 | if (ret) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1916 | printk("space info full %Lu\n", flags); |
| 1917 | space_info->full = 1; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1918 | } |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1919 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1920 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1921 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1922 | } |
| 1923 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1924 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1925 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1926 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1927 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1928 | { |
| 1929 | struct btrfs_block_group_cache *cache; |
| 1930 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1931 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1932 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1933 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1934 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1935 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1936 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1937 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1938 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1939 | byte_in_group = bytenr - cache->key.objectid; |
| 1940 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1941 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1942 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1943 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1944 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1945 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1946 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1947 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1948 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1949 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1950 | if (cache->ro) { |
| 1951 | cache->space_info->bytes_readonly -= num_bytes; |
| 1952 | WARN_ON(1); |
| 1953 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1954 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1955 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1956 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1957 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1958 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1959 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1960 | if (cache->ro) |
| 1961 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1962 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1963 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1964 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1965 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1966 | int ret; |
| 1967 | ret = btrfs_add_free_space(cache, bytenr, |
| 1968 | num_bytes); |
| 1969 | if (ret) |
| 1970 | return -1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1971 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1972 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1973 | total -= num_bytes; |
| 1974 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1975 | } |
| 1976 | return 0; |
| 1977 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1978 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1979 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 1980 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1981 | struct btrfs_block_group_cache *cache; |
| 1982 | |
| 1983 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 1984 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1985 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1986 | |
| 1987 | return cache->key.objectid; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1988 | } |
| 1989 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1990 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1991 | u64 bytenr, u64 num, int pin) |
| 1992 | { |
| 1993 | u64 len; |
| 1994 | struct btrfs_block_group_cache *cache; |
| 1995 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1996 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1997 | WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1998 | if (pin) { |
| 1999 | set_extent_dirty(&fs_info->pinned_extents, |
| 2000 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2001 | } else { |
| 2002 | clear_extent_dirty(&fs_info->pinned_extents, |
| 2003 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2004 | } |
| 2005 | while (num > 0) { |
| 2006 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2007 | BUG_ON(!cache); |
| 2008 | len = min(num, cache->key.offset - |
| 2009 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2010 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2011 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2012 | spin_lock(&cache->lock); |
| 2013 | cache->pinned += len; |
| 2014 | cache->space_info->bytes_pinned += len; |
| 2015 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2016 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2017 | fs_info->total_pinned += len; |
| 2018 | } else { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2019 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2020 | spin_lock(&cache->lock); |
| 2021 | cache->pinned -= len; |
| 2022 | cache->space_info->bytes_pinned -= len; |
| 2023 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2024 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2025 | fs_info->total_pinned -= len; |
| 2026 | } |
| 2027 | bytenr += len; |
| 2028 | num -= len; |
| 2029 | } |
| 2030 | return 0; |
| 2031 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2032 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2033 | static int update_reserved_extents(struct btrfs_root *root, |
| 2034 | u64 bytenr, u64 num, int reserve) |
| 2035 | { |
| 2036 | u64 len; |
| 2037 | struct btrfs_block_group_cache *cache; |
| 2038 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2039 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2040 | while (num > 0) { |
| 2041 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 2042 | BUG_ON(!cache); |
| 2043 | len = min(num, cache->key.offset - |
| 2044 | (bytenr - cache->key.objectid)); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2045 | |
| 2046 | spin_lock(&cache->space_info->lock); |
| 2047 | spin_lock(&cache->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2048 | if (reserve) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2049 | cache->reserved += len; |
| 2050 | cache->space_info->bytes_reserved += len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2051 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2052 | cache->reserved -= len; |
| 2053 | cache->space_info->bytes_reserved -= len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2054 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2055 | spin_unlock(&cache->lock); |
| 2056 | spin_unlock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2057 | bytenr += len; |
| 2058 | num -= len; |
| 2059 | } |
| 2060 | return 0; |
| 2061 | } |
| 2062 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2063 | 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] | 2064 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2065 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2066 | u64 start; |
| 2067 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2068 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2069 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2070 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2071 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2072 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2073 | ret = find_first_extent_bit(pinned_extents, last, |
| 2074 | &start, &end, EXTENT_DIRTY); |
| 2075 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2076 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2077 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 2078 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2079 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2080 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2081 | return 0; |
| 2082 | } |
| 2083 | |
| 2084 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 2085 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2086 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2087 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2088 | u64 start; |
| 2089 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2090 | int ret; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2091 | struct btrfs_block_group_cache *cache; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2092 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2093 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2094 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2095 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 2096 | EXTENT_DIRTY); |
| 2097 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2098 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2099 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2100 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2101 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 2102 | if (cache->cached) |
| 2103 | btrfs_add_free_space(cache, start, end - start + 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2104 | if (need_resched()) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2105 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2106 | cond_resched(); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2107 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2108 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2109 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2110 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2111 | return 0; |
| 2112 | } |
| 2113 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2114 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2115 | struct btrfs_root *extent_root, int all) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2116 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2117 | u64 start; |
| 2118 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2119 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2120 | u64 search = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2121 | u64 skipped = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2122 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 2123 | struct btrfs_path *path; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2124 | struct pending_extent_op *extent_op, *tmp; |
| 2125 | struct list_head insert_list, update_list; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2126 | int ret; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2127 | int num_inserts = 0, max_inserts; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2128 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2129 | path = btrfs_alloc_path(); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2130 | INIT_LIST_HEAD(&insert_list); |
| 2131 | INIT_LIST_HEAD(&update_list); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2132 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2133 | max_inserts = extent_root->leafsize / |
| 2134 | (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) + |
| 2135 | sizeof(struct btrfs_extent_ref) + |
| 2136 | sizeof(struct btrfs_extent_item)); |
| 2137 | again: |
| 2138 | mutex_lock(&info->extent_ins_mutex); |
| 2139 | while (1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2140 | ret = find_first_extent_bit(&info->extent_ins, search, &start, |
| 2141 | &end, EXTENT_WRITEBACK); |
| 2142 | if (ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2143 | if (skipped && all && !num_inserts) { |
| 2144 | skipped = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2145 | continue; |
| 2146 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2147 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2148 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2149 | } |
| 2150 | |
| 2151 | ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS); |
| 2152 | if (!ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2153 | skipped = 1; |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2154 | search = end + 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2155 | if (need_resched()) { |
| 2156 | mutex_unlock(&info->extent_ins_mutex); |
| 2157 | cond_resched(); |
| 2158 | mutex_lock(&info->extent_ins_mutex); |
| 2159 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2160 | continue; |
| 2161 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2162 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2163 | ret = get_state_private(&info->extent_ins, start, &priv); |
| 2164 | BUG_ON(ret); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2165 | extent_op = (struct pending_extent_op *)(unsigned long) priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2166 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2167 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2168 | num_inserts++; |
| 2169 | list_add_tail(&extent_op->list, &insert_list); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2170 | search = end + 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2171 | if (num_inserts == max_inserts) { |
| 2172 | mutex_unlock(&info->extent_ins_mutex); |
| 2173 | break; |
| 2174 | } |
| 2175 | } else if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 2176 | list_add_tail(&extent_op->list, &update_list); |
| 2177 | search = end + 1; |
| 2178 | } else { |
| 2179 | BUG(); |
| 2180 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2181 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2182 | |
| 2183 | /* |
| 2184 | * process teh update list, clear the writeback bit for it, and if |
| 2185 | * somebody marked this thing for deletion then just unlock it and be |
| 2186 | * done, the free_extents will handle it |
| 2187 | */ |
| 2188 | mutex_lock(&info->extent_ins_mutex); |
| 2189 | list_for_each_entry_safe(extent_op, tmp, &update_list, list) { |
| 2190 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, |
| 2191 | extent_op->bytenr + extent_op->num_bytes - 1, |
| 2192 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2193 | if (extent_op->del) { |
| 2194 | list_del_init(&extent_op->list); |
| 2195 | unlock_extent(&info->extent_ins, extent_op->bytenr, |
| 2196 | extent_op->bytenr + extent_op->num_bytes |
| 2197 | - 1, GFP_NOFS); |
| 2198 | kfree(extent_op); |
| 2199 | } |
| 2200 | } |
| 2201 | mutex_unlock(&info->extent_ins_mutex); |
| 2202 | |
| 2203 | /* |
| 2204 | * still have things left on the update list, go ahead an update |
| 2205 | * everything |
| 2206 | */ |
| 2207 | if (!list_empty(&update_list)) { |
| 2208 | ret = update_backrefs(trans, extent_root, path, &update_list); |
| 2209 | BUG_ON(ret); |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * if no inserts need to be done, but we skipped some extents and we |
| 2214 | * need to make sure everything is cleaned then reset everything and |
| 2215 | * go back to the beginning |
| 2216 | */ |
| 2217 | if (!num_inserts && all && skipped) { |
| 2218 | search = 0; |
| 2219 | skipped = 0; |
| 2220 | INIT_LIST_HEAD(&update_list); |
| 2221 | INIT_LIST_HEAD(&insert_list); |
| 2222 | goto again; |
| 2223 | } else if (!num_inserts) { |
| 2224 | goto out; |
| 2225 | } |
| 2226 | |
| 2227 | /* |
| 2228 | * process the insert extents list. Again if we are deleting this |
| 2229 | * extent, then just unlock it, pin down the bytes if need be, and be |
| 2230 | * done with it. Saves us from having to actually insert the extent |
| 2231 | * into the tree and then subsequently come along and delete it |
| 2232 | */ |
| 2233 | mutex_lock(&info->extent_ins_mutex); |
| 2234 | list_for_each_entry_safe(extent_op, tmp, &insert_list, list) { |
| 2235 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, |
| 2236 | extent_op->bytenr + extent_op->num_bytes - 1, |
| 2237 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2238 | if (extent_op->del) { |
| 2239 | list_del_init(&extent_op->list); |
| 2240 | unlock_extent(&info->extent_ins, extent_op->bytenr, |
| 2241 | extent_op->bytenr + extent_op->num_bytes |
| 2242 | - 1, GFP_NOFS); |
| 2243 | |
| 2244 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 2245 | ret = pin_down_bytes(trans, extent_root, |
| 2246 | extent_op->bytenr, |
| 2247 | extent_op->num_bytes, 0); |
| 2248 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 2249 | |
| 2250 | ret = update_block_group(trans, extent_root, |
| 2251 | extent_op->bytenr, |
| 2252 | extent_op->num_bytes, |
| 2253 | 0, ret > 0); |
| 2254 | BUG_ON(ret); |
| 2255 | kfree(extent_op); |
| 2256 | num_inserts--; |
| 2257 | } |
| 2258 | } |
| 2259 | mutex_unlock(&info->extent_ins_mutex); |
| 2260 | |
| 2261 | ret = insert_extents(trans, extent_root, path, &insert_list, |
| 2262 | num_inserts); |
| 2263 | BUG_ON(ret); |
| 2264 | |
| 2265 | /* |
| 2266 | * if we broke out of the loop in order to insert stuff because we hit |
| 2267 | * the maximum number of inserts at a time we can handle, then loop |
| 2268 | * back and pick up where we left off |
| 2269 | */ |
| 2270 | if (num_inserts == max_inserts) { |
| 2271 | INIT_LIST_HEAD(&insert_list); |
| 2272 | INIT_LIST_HEAD(&update_list); |
| 2273 | num_inserts = 0; |
| 2274 | goto again; |
| 2275 | } |
| 2276 | |
| 2277 | /* |
| 2278 | * again, if we need to make absolutely sure there are no more pending |
| 2279 | * extent operations left and we know that we skipped some, go back to |
| 2280 | * the beginning and do it all again |
| 2281 | */ |
| 2282 | if (all && skipped) { |
| 2283 | INIT_LIST_HEAD(&insert_list); |
| 2284 | INIT_LIST_HEAD(&update_list); |
| 2285 | search = 0; |
| 2286 | skipped = 0; |
| 2287 | num_inserts = 0; |
| 2288 | goto again; |
| 2289 | } |
| 2290 | out: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2291 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2292 | return 0; |
| 2293 | } |
| 2294 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2295 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 2296 | struct btrfs_root *root, |
| 2297 | u64 bytenr, u64 num_bytes, int is_data) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2298 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2299 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2300 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2301 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2302 | if (is_data) |
| 2303 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2304 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2305 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 2306 | if (!buf) |
| 2307 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2308 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2309 | /* we can reuse a block if it hasn't been written |
| 2310 | * and it is from this transaction. We can't |
| 2311 | * reuse anything from the tree log root because |
| 2312 | * it has tiny sub-transactions. |
| 2313 | */ |
| 2314 | if (btrfs_buffer_uptodate(buf, 0) && |
| 2315 | btrfs_try_tree_lock(buf)) { |
| 2316 | u64 header_owner = btrfs_header_owner(buf); |
| 2317 | u64 header_transid = btrfs_header_generation(buf); |
| 2318 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2319 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2320 | header_transid == trans->transid && |
| 2321 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 2322 | clean_tree_block(NULL, root, buf); |
| 2323 | btrfs_tree_unlock(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2324 | free_extent_buffer(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2325 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2326 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2327 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2328 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2329 | free_extent_buffer(buf); |
| 2330 | pinit: |
| 2331 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 2332 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2333 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2334 | return 0; |
| 2335 | } |
| 2336 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2337 | /* |
| 2338 | * remove an extent from the root, returns 0 on success |
| 2339 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2340 | static int __free_extent(struct btrfs_trans_handle *trans, |
| 2341 | struct btrfs_root *root, |
| 2342 | u64 bytenr, u64 num_bytes, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2343 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2344 | u64 owner_objectid, int pin, int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2345 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2346 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2347 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2348 | struct btrfs_fs_info *info = root->fs_info; |
| 2349 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2350 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2351 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2352 | int extent_slot = 0; |
| 2353 | int found_extent = 0; |
| 2354 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2355 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2356 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2357 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2358 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2359 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2360 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2361 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2362 | if (!path) |
| 2363 | return -ENOMEM; |
| 2364 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2365 | path->reada = 1; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2366 | ret = lookup_extent_backref(trans, extent_root, path, |
| 2367 | bytenr, parent, root_objectid, |
| 2368 | ref_generation, owner_objectid, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2369 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2370 | struct btrfs_key found_key; |
| 2371 | extent_slot = path->slots[0]; |
| 2372 | while(extent_slot > 0) { |
| 2373 | extent_slot--; |
| 2374 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2375 | extent_slot); |
| 2376 | if (found_key.objectid != bytenr) |
| 2377 | break; |
| 2378 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2379 | found_key.offset == num_bytes) { |
| 2380 | found_extent = 1; |
| 2381 | break; |
| 2382 | } |
| 2383 | if (path->slots[0] - extent_slot > 5) |
| 2384 | break; |
| 2385 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2386 | if (!found_extent) { |
| 2387 | ret = remove_extent_backref(trans, extent_root, path); |
| 2388 | BUG_ON(ret); |
| 2389 | btrfs_release_path(extent_root, path); |
| 2390 | ret = btrfs_search_slot(trans, extent_root, |
| 2391 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2392 | if (ret) { |
| 2393 | printk(KERN_ERR "umm, got %d back from search" |
| 2394 | ", was looking for %Lu\n", ret, |
| 2395 | bytenr); |
| 2396 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2397 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2398 | BUG_ON(ret); |
| 2399 | extent_slot = path->slots[0]; |
| 2400 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2401 | } else { |
| 2402 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2403 | WARN_ON(1); |
| 2404 | printk("Unable to find ref byte nr %Lu root %Lu " |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2405 | "gen %Lu owner %Lu\n", bytenr, |
| 2406 | root_objectid, ref_generation, owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2407 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2408 | |
| 2409 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2410 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2411 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2412 | refs = btrfs_extent_refs(leaf, ei); |
| 2413 | BUG_ON(refs == 0); |
| 2414 | refs -= 1; |
| 2415 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2416 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2417 | btrfs_mark_buffer_dirty(leaf); |
| 2418 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2419 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2420 | struct btrfs_extent_ref *ref; |
| 2421 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 2422 | struct btrfs_extent_ref); |
| 2423 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2424 | /* if the back ref and the extent are next to each other |
| 2425 | * they get deleted below in one shot |
| 2426 | */ |
| 2427 | path->slots[0] = extent_slot; |
| 2428 | num_to_del = 2; |
| 2429 | } else if (found_extent) { |
| 2430 | /* otherwise delete the extent back ref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2431 | ret = remove_extent_backref(trans, extent_root, path); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2432 | BUG_ON(ret); |
| 2433 | /* if refs are 0, we need to setup the path for deletion */ |
| 2434 | if (refs == 0) { |
| 2435 | btrfs_release_path(extent_root, path); |
| 2436 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 2437 | -1, 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2438 | BUG_ON(ret); |
| 2439 | } |
| 2440 | } |
| 2441 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2442 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2443 | u64 super_used; |
| 2444 | u64 root_used; |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2445 | #ifdef BIO_RW_DISCARD |
| 2446 | u64 map_length = num_bytes; |
| 2447 | struct btrfs_multi_bio *multi = NULL; |
| 2448 | #endif |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2449 | |
| 2450 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2451 | mutex_lock(&root->fs_info->pinned_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2452 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, |
| 2453 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2454 | mutex_unlock(&root->fs_info->pinned_mutex); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 2455 | if (ret > 0) |
| 2456 | mark_free = 1; |
| 2457 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2458 | } |
| 2459 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2460 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2461 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2462 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2463 | btrfs_set_super_bytes_used(&info->super_copy, |
| 2464 | super_used - num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2465 | spin_unlock_irq(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2466 | |
| 2467 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2468 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2469 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2470 | root_used - num_bytes); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2471 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 2472 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2473 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2474 | btrfs_release_path(extent_root, path); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2475 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2476 | mark_free); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2477 | BUG_ON(ret); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2478 | |
| 2479 | #ifdef BIO_RW_DISCARD |
| 2480 | /* Tell the block device(s) that the sectors can be discarded */ |
| 2481 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 2482 | bytenr, &map_length, &multi, 0); |
| 2483 | if (!ret) { |
| 2484 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 2485 | int i; |
| 2486 | |
| 2487 | if (map_length > num_bytes) |
| 2488 | map_length = num_bytes; |
| 2489 | |
| 2490 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 2491 | blkdev_issue_discard(stripe->dev->bdev, |
| 2492 | stripe->physical >> 9, |
| 2493 | map_length >> 9); |
| 2494 | } |
| 2495 | kfree(multi); |
| 2496 | } |
| 2497 | #endif |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2498 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2499 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2500 | finish_current_insert(trans, extent_root, 0); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2501 | return ret; |
| 2502 | } |
| 2503 | |
| 2504 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2505 | * find all the blocks marked as pending in the radix tree and remove |
| 2506 | * them from the extent map |
| 2507 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2508 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2509 | btrfs_root *extent_root, int all) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2510 | { |
| 2511 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2512 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2513 | u64 start; |
| 2514 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2515 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2516 | u64 search = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2517 | int nr = 0, skipped = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2518 | struct extent_io_tree *pending_del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2519 | struct extent_io_tree *extent_ins; |
| 2520 | struct pending_extent_op *extent_op; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2521 | struct btrfs_fs_info *info = extent_root->fs_info; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2522 | struct list_head delete_list; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2523 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2524 | INIT_LIST_HEAD(&delete_list); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2525 | extent_ins = &extent_root->fs_info->extent_ins; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2526 | pending_del = &extent_root->fs_info->pending_del; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2527 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2528 | again: |
| 2529 | mutex_lock(&info->extent_ins_mutex); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2530 | while(1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2531 | ret = find_first_extent_bit(pending_del, search, &start, &end, |
| 2532 | EXTENT_WRITEBACK); |
| 2533 | if (ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2534 | if (all && skipped && !nr) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2535 | search = 0; |
| 2536 | continue; |
| 2537 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2538 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2539 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | ret = try_lock_extent(extent_ins, start, end, GFP_NOFS); |
| 2543 | if (!ret) { |
| 2544 | search = end+1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2545 | skipped = 1; |
| 2546 | |
| 2547 | if (need_resched()) { |
| 2548 | mutex_unlock(&info->extent_ins_mutex); |
| 2549 | cond_resched(); |
| 2550 | mutex_lock(&info->extent_ins_mutex); |
| 2551 | } |
| 2552 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2553 | continue; |
| 2554 | } |
| 2555 | BUG_ON(ret < 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2556 | |
| 2557 | ret = get_state_private(pending_del, start, &priv); |
| 2558 | BUG_ON(ret); |
| 2559 | extent_op = (struct pending_extent_op *)(unsigned long)priv; |
| 2560 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2561 | clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2562 | GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2563 | if (!test_range_bit(extent_ins, start, end, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2564 | EXTENT_WRITEBACK, 0)) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2565 | list_add_tail(&extent_op->list, &delete_list); |
| 2566 | nr++; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2567 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2568 | kfree(extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2569 | |
| 2570 | ret = get_state_private(&info->extent_ins, start, |
| 2571 | &priv); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2572 | BUG_ON(ret); |
| 2573 | extent_op = (struct pending_extent_op *) |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2574 | (unsigned long)priv; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2575 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2576 | clear_extent_bits(&info->extent_ins, start, end, |
| 2577 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2578 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2579 | if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 2580 | list_add_tail(&extent_op->list, &delete_list); |
| 2581 | search = end + 1; |
| 2582 | nr++; |
| 2583 | continue; |
| 2584 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2585 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2586 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 2587 | ret = pin_down_bytes(trans, extent_root, start, |
| 2588 | end + 1 - start, 0); |
| 2589 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 2590 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2591 | ret = update_block_group(trans, extent_root, start, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2592 | end + 1 - start, 0, ret > 0); |
| 2593 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2594 | unlock_extent(extent_ins, start, end, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2595 | BUG_ON(ret); |
| 2596 | kfree(extent_op); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2597 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2598 | if (ret) |
| 2599 | err = ret; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2600 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2601 | search = end + 1; |
| 2602 | |
| 2603 | if (need_resched()) { |
| 2604 | mutex_unlock(&info->extent_ins_mutex); |
| 2605 | cond_resched(); |
| 2606 | mutex_lock(&info->extent_ins_mutex); |
| 2607 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2608 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2609 | |
| 2610 | if (nr) { |
| 2611 | ret = free_extents(trans, extent_root, &delete_list); |
| 2612 | BUG_ON(ret); |
| 2613 | } |
| 2614 | |
| 2615 | if (all && skipped) { |
| 2616 | INIT_LIST_HEAD(&delete_list); |
| 2617 | search = 0; |
| 2618 | nr = 0; |
| 2619 | goto again; |
| 2620 | } |
| 2621 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2622 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | * remove an extent from the root, returns 0 on success |
| 2627 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2628 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2629 | struct btrfs_root *root, |
| 2630 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2631 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2632 | u64 owner_objectid, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2633 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2634 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2635 | int pending_ret; |
| 2636 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2637 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2638 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2639 | if (root == extent_root) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2640 | struct pending_extent_op *extent_op = NULL; |
| 2641 | |
| 2642 | mutex_lock(&root->fs_info->extent_ins_mutex); |
| 2643 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
| 2644 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
| 2645 | u64 priv; |
| 2646 | ret = get_state_private(&root->fs_info->extent_ins, |
| 2647 | bytenr, &priv); |
| 2648 | BUG_ON(ret); |
| 2649 | extent_op = (struct pending_extent_op *) |
| 2650 | (unsigned long)priv; |
| 2651 | |
| 2652 | extent_op->del = 1; |
| 2653 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
| 2654 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
| 2655 | return 0; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | if (extent_op) { |
| 2660 | ref_generation = extent_op->orig_generation; |
| 2661 | parent = extent_op->orig_parent; |
| 2662 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2663 | |
| 2664 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2665 | BUG_ON(!extent_op); |
| 2666 | |
| 2667 | extent_op->type = PENDING_EXTENT_DELETE; |
| 2668 | extent_op->bytenr = bytenr; |
| 2669 | extent_op->num_bytes = num_bytes; |
| 2670 | extent_op->parent = parent; |
| 2671 | extent_op->orig_parent = parent; |
| 2672 | extent_op->generation = ref_generation; |
| 2673 | extent_op->orig_generation = ref_generation; |
| 2674 | extent_op->level = (int)owner_objectid; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2675 | INIT_LIST_HEAD(&extent_op->list); |
| 2676 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2677 | |
| 2678 | set_extent_bits(&root->fs_info->pending_del, |
| 2679 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2680 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2681 | set_state_private(&root->fs_info->pending_del, |
| 2682 | bytenr, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2683 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2684 | return 0; |
| 2685 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2686 | /* if metadata always pin */ |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2687 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2688 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2689 | struct btrfs_block_group_cache *cache; |
| 2690 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2691 | /* btrfs_free_reserved_extent */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2692 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2693 | BUG_ON(!cache); |
| 2694 | btrfs_add_free_space(cache, bytenr, num_bytes); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2695 | update_reserved_extents(root, bytenr, num_bytes, 0); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2696 | return 0; |
| 2697 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2698 | pin = 1; |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2699 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2700 | |
| 2701 | /* if data pin when any transaction has committed this */ |
| 2702 | if (ref_generation != trans->transid) |
| 2703 | pin = 1; |
| 2704 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2705 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2706 | root_objectid, ref_generation, |
| 2707 | owner_objectid, pin, pin == 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 2708 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2709 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 2710 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2711 | return ret ? ret : pending_ret; |
| 2712 | } |
| 2713 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2714 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2715 | struct btrfs_root *root, |
| 2716 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2717 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2718 | u64 owner_objectid, int pin) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2719 | { |
| 2720 | int ret; |
| 2721 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2722 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2723 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2724 | owner_objectid, pin); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2725 | return ret; |
| 2726 | } |
| 2727 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2728 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 2729 | { |
| 2730 | u64 mask = ((u64)root->stripesize - 1); |
| 2731 | u64 ret = (val + mask) & ~mask; |
| 2732 | return ret; |
| 2733 | } |
| 2734 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2735 | /* |
| 2736 | * walks the btree of allocated extents and find a hole of a given size. |
| 2737 | * The key ins is changed to record the hole: |
| 2738 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2739 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2740 | * ins->offset == number of blocks |
| 2741 | * Any available blocks before search_start are skipped. |
| 2742 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2743 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
| 2744 | struct btrfs_root *orig_root, |
| 2745 | u64 num_bytes, u64 empty_size, |
| 2746 | u64 search_start, u64 search_end, |
| 2747 | u64 hint_byte, struct btrfs_key *ins, |
| 2748 | u64 exclude_start, u64 exclude_nr, |
| 2749 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2750 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2751 | int ret = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2752 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2753 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2754 | u64 *last_ptr = NULL; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2755 | u64 last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2756 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2757 | int chunk_alloc_done = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2758 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2759 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2760 | struct list_head *head = NULL, *cur = NULL; |
| 2761 | int loop = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2762 | int extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2763 | struct btrfs_space_info *space_info; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2764 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2765 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2766 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2767 | ins->objectid = 0; |
| 2768 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2769 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2770 | if (orig_root->ref_cows || empty_size) |
| 2771 | allowed_chunk_alloc = 1; |
| 2772 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2773 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 2774 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2775 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2776 | } |
| 2777 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2778 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2779 | last_ptr = &root->fs_info->last_data_alloc; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2780 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2781 | if (last_ptr) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2782 | if (*last_ptr) { |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2783 | hint_byte = *last_ptr; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2784 | last_wanted = *last_ptr; |
| 2785 | } else |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2786 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2787 | } else { |
| 2788 | empty_cluster = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2789 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2790 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2791 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2792 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2793 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2794 | last_wanted = 0; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2795 | empty_size += empty_cluster; |
| 2796 | } |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2797 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2798 | total_needed += empty_size; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2799 | block_group = btrfs_lookup_block_group(root->fs_info, search_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2800 | if (!block_group) |
| 2801 | block_group = btrfs_lookup_first_block_group(root->fs_info, |
| 2802 | search_start); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2803 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2804 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2805 | down_read(&space_info->groups_sem); |
| 2806 | while (1) { |
| 2807 | struct btrfs_free_space *free_space; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2808 | /* |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2809 | * the only way this happens if our hint points to a block |
| 2810 | * group thats not of the proper type, while looping this |
| 2811 | * should never happen |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2812 | */ |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2813 | if (empty_size) |
| 2814 | extra_loop = 1; |
| 2815 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2816 | if (!block_group) |
| 2817 | goto new_group_no_lock; |
| 2818 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2819 | mutex_lock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2820 | if (unlikely(!block_group_bits(block_group, data))) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2821 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2822 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2823 | ret = cache_block_group(root, block_group); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2824 | if (ret) { |
| 2825 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2826 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2827 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2828 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2829 | if (block_group->ro) |
| 2830 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2831 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2832 | free_space = btrfs_find_free_space(block_group, search_start, |
| 2833 | total_needed); |
| 2834 | if (free_space) { |
| 2835 | u64 start = block_group->key.objectid; |
| 2836 | u64 end = block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2837 | block_group->key.offset; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2838 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2839 | search_start = stripe_align(root, free_space->offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2840 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2841 | /* move on to the next group */ |
| 2842 | if (search_start + num_bytes >= search_end) |
| 2843 | goto new_group; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2844 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2845 | /* move on to the next group */ |
| 2846 | if (search_start + num_bytes > end) |
| 2847 | goto new_group; |
| 2848 | |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2849 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 2850 | total_needed += empty_cluster; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2851 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2852 | last_wanted = 0; |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 2853 | /* |
| 2854 | * if search_start is still in this block group |
| 2855 | * then we just re-search this block group |
| 2856 | */ |
| 2857 | if (search_start >= start && |
| 2858 | search_start < end) { |
| 2859 | mutex_unlock(&block_group->alloc_mutex); |
| 2860 | continue; |
| 2861 | } |
| 2862 | |
| 2863 | /* else we go to the next block group */ |
| 2864 | goto new_group; |
| 2865 | } |
| 2866 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2867 | if (exclude_nr > 0 && |
| 2868 | (search_start + num_bytes > exclude_start && |
| 2869 | search_start < exclude_start + exclude_nr)) { |
| 2870 | search_start = exclude_start + exclude_nr; |
| 2871 | /* |
| 2872 | * if search_start is still in this block group |
| 2873 | * then we just re-search this block group |
| 2874 | */ |
| 2875 | if (search_start >= start && |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2876 | search_start < end) { |
| 2877 | mutex_unlock(&block_group->alloc_mutex); |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2878 | last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2879 | continue; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2880 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2881 | |
| 2882 | /* else we go to the next block group */ |
| 2883 | goto new_group; |
| 2884 | } |
| 2885 | |
| 2886 | ins->objectid = search_start; |
| 2887 | ins->offset = num_bytes; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2888 | |
| 2889 | btrfs_remove_free_space_lock(block_group, search_start, |
| 2890 | num_bytes); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2891 | /* we are all good, lets return */ |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2892 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2893 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2894 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2895 | new_group: |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2896 | mutex_unlock(&block_group->alloc_mutex); |
| 2897 | new_group_no_lock: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2898 | /* don't try to compare new allocations against the |
| 2899 | * last allocation any more |
| 2900 | */ |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2901 | last_wanted = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2902 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2903 | /* |
| 2904 | * Here's how this works. |
| 2905 | * loop == 0: we were searching a block group via a hint |
| 2906 | * and didn't find anything, so we start at |
| 2907 | * the head of the block groups and keep searching |
| 2908 | * loop == 1: we're searching through all of the block groups |
| 2909 | * if we hit the head again we have searched |
| 2910 | * all of the block groups for this space and we |
| 2911 | * need to try and allocate, if we cant error out. |
| 2912 | * loop == 2: we allocated more space and are looping through |
| 2913 | * all of the block groups again. |
| 2914 | */ |
| 2915 | if (loop == 0) { |
| 2916 | head = &space_info->block_groups; |
| 2917 | cur = head->next; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2918 | loop++; |
| 2919 | } else if (loop == 1 && cur == head) { |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2920 | int keep_going; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2921 | |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2922 | /* at this point we give up on the empty_size |
| 2923 | * allocations and just try to allocate the min |
| 2924 | * space. |
| 2925 | * |
| 2926 | * The extra_loop field was set if an empty_size |
| 2927 | * allocation was attempted above, and if this |
| 2928 | * is try we need to try the loop again without |
| 2929 | * the additional empty_size. |
| 2930 | */ |
Chris Mason | 5b7c3fc | 2008-11-10 07:26:33 -0500 | [diff] [blame] | 2931 | total_needed -= empty_size; |
| 2932 | empty_size = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2933 | keep_going = extra_loop; |
| 2934 | loop++; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2935 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2936 | if (allowed_chunk_alloc && !chunk_alloc_done) { |
| 2937 | up_read(&space_info->groups_sem); |
| 2938 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 2939 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2940 | down_read(&space_info->groups_sem); |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 2941 | if (ret < 0) |
| 2942 | goto loop_check; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2943 | head = &space_info->block_groups; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2944 | /* |
| 2945 | * we've allocated a new chunk, keep |
| 2946 | * trying |
| 2947 | */ |
| 2948 | keep_going = 1; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2949 | chunk_alloc_done = 1; |
| 2950 | } else if (!allowed_chunk_alloc) { |
| 2951 | space_info->force_alloc = 1; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2952 | } |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 2953 | loop_check: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2954 | if (keep_going) { |
| 2955 | cur = head->next; |
| 2956 | extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2957 | } else { |
| 2958 | break; |
| 2959 | } |
| 2960 | } else if (cur == head) { |
| 2961 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2962 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2963 | |
| 2964 | block_group = list_entry(cur, struct btrfs_block_group_cache, |
| 2965 | list); |
| 2966 | search_start = block_group->key.objectid; |
| 2967 | cur = cur->next; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 2968 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2969 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2970 | /* we found what we needed */ |
| 2971 | if (ins->objectid) { |
| 2972 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
| 2973 | trans->block_group = block_group; |
| 2974 | |
| 2975 | if (last_ptr) |
| 2976 | *last_ptr = ins->objectid + ins->offset; |
| 2977 | ret = 0; |
| 2978 | } else if (!ret) { |
| 2979 | ret = -ENOSPC; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -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 | up_read(&space_info->groups_sem); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 2983 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2984 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2985 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2986 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 2987 | { |
| 2988 | struct btrfs_block_group_cache *cache; |
| 2989 | struct list_head *l; |
| 2990 | |
| 2991 | printk(KERN_INFO "space_info has %Lu free, is %sfull\n", |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2992 | info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 2993 | info->bytes_reserved, (info->full) ? "" : "not "); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2994 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2995 | down_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2996 | list_for_each(l, &info->block_groups) { |
| 2997 | cache = list_entry(l, struct btrfs_block_group_cache, list); |
| 2998 | spin_lock(&cache->lock); |
| 2999 | printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used " |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3000 | "%Lu pinned %Lu reserved\n", |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3001 | cache->key.objectid, cache->key.offset, |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3002 | btrfs_block_group_used(&cache->item), |
| 3003 | cache->pinned, cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3004 | btrfs_dump_free_space(cache, bytes); |
| 3005 | spin_unlock(&cache->lock); |
| 3006 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3007 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3008 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3009 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3010 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3011 | struct btrfs_root *root, |
| 3012 | u64 num_bytes, u64 min_alloc_size, |
| 3013 | u64 empty_size, u64 hint_byte, |
| 3014 | u64 search_end, struct btrfs_key *ins, |
| 3015 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3016 | { |
| 3017 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 3018 | u64 search_start = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3019 | u64 alloc_profile; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3020 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3021 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3022 | if (data) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3023 | alloc_profile = info->avail_data_alloc_bits & |
| 3024 | info->data_alloc_profile; |
| 3025 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3026 | } else if (root == root->fs_info->chunk_root) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3027 | alloc_profile = info->avail_system_alloc_bits & |
| 3028 | info->system_alloc_profile; |
| 3029 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3030 | } else { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3031 | alloc_profile = info->avail_metadata_alloc_bits & |
| 3032 | info->metadata_alloc_profile; |
| 3033 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3034 | } |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3035 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3036 | data = btrfs_reduce_alloc_profile(root, data); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3037 | /* |
| 3038 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 3039 | * is not called recursively on allocations |
| 3040 | */ |
| 3041 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3042 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3043 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3044 | 2 * 1024 * 1024, |
| 3045 | BTRFS_BLOCK_GROUP_METADATA | |
| 3046 | (info->metadata_alloc_profile & |
| 3047 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3048 | } |
| 3049 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3050 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3051 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3052 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3053 | WARN_ON(num_bytes < root->sectorsize); |
| 3054 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 3055 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3056 | trans->alloc_exclude_start, |
| 3057 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3058 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3059 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 3060 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3061 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3062 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3063 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3064 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3065 | goto again; |
| 3066 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3067 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3068 | struct btrfs_space_info *sinfo; |
| 3069 | |
| 3070 | sinfo = __find_space_info(root->fs_info, data); |
| 3071 | printk("allocation failed flags %Lu, wanted %Lu\n", |
| 3072 | data, num_bytes); |
| 3073 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3074 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3075 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3076 | |
| 3077 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3078 | } |
| 3079 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3080 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 3081 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3082 | struct btrfs_block_group_cache *cache; |
| 3083 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3084 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 3085 | if (!cache) { |
| 3086 | printk(KERN_ERR "Unable to find block group for %Lu\n", start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3087 | return -ENOSPC; |
| 3088 | } |
| 3089 | btrfs_add_free_space(cache, start, len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3090 | update_reserved_extents(root, start, len, 0); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3091 | return 0; |
| 3092 | } |
| 3093 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3094 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3095 | struct btrfs_root *root, |
| 3096 | u64 num_bytes, u64 min_alloc_size, |
| 3097 | u64 empty_size, u64 hint_byte, |
| 3098 | u64 search_end, struct btrfs_key *ins, |
| 3099 | u64 data) |
| 3100 | { |
| 3101 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3102 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 3103 | empty_size, hint_byte, search_end, ins, |
| 3104 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3105 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3106 | return ret; |
| 3107 | } |
| 3108 | |
| 3109 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3110 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3111 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3112 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3113 | { |
| 3114 | int ret; |
| 3115 | int pending_ret; |
| 3116 | u64 super_used; |
| 3117 | u64 root_used; |
| 3118 | u64 num_bytes = ins->offset; |
| 3119 | u32 sizes[2]; |
| 3120 | struct btrfs_fs_info *info = root->fs_info; |
| 3121 | struct btrfs_root *extent_root = info->extent_root; |
| 3122 | struct btrfs_extent_item *extent_item; |
| 3123 | struct btrfs_extent_ref *ref; |
| 3124 | struct btrfs_path *path; |
| 3125 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 3126 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3127 | if (parent == 0) |
| 3128 | parent = ins->objectid; |
| 3129 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3130 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3131 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3132 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 3133 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3134 | spin_unlock_irq(&info->delalloc_lock); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3135 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3136 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3137 | root_used = btrfs_root_used(&root->root_item); |
| 3138 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3139 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3140 | if (root == extent_root) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3141 | struct pending_extent_op *extent_op; |
| 3142 | |
| 3143 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 3144 | BUG_ON(!extent_op); |
| 3145 | |
| 3146 | extent_op->type = PENDING_EXTENT_INSERT; |
| 3147 | extent_op->bytenr = ins->objectid; |
| 3148 | extent_op->num_bytes = ins->offset; |
| 3149 | extent_op->parent = parent; |
| 3150 | extent_op->orig_parent = 0; |
| 3151 | extent_op->generation = ref_generation; |
| 3152 | extent_op->orig_generation = 0; |
| 3153 | extent_op->level = (int)owner; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3154 | INIT_LIST_HEAD(&extent_op->list); |
| 3155 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3156 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3157 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3158 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 3159 | ins->objectid + ins->offset - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3160 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3161 | set_state_private(&root->fs_info->extent_ins, |
| 3162 | ins->objectid, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3163 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3164 | goto update_block; |
| 3165 | } |
| 3166 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3167 | memcpy(&keys[0], ins, sizeof(*ins)); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3168 | keys[1].objectid = ins->objectid; |
| 3169 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3170 | keys[1].offset = parent; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3171 | sizes[0] = sizeof(*extent_item); |
| 3172 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3173 | |
| 3174 | path = btrfs_alloc_path(); |
| 3175 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3176 | |
| 3177 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 3178 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3179 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3180 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3181 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3182 | struct btrfs_extent_item); |
| 3183 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); |
| 3184 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 3185 | struct btrfs_extent_ref); |
| 3186 | |
| 3187 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 3188 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 3189 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3190 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3191 | |
| 3192 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3193 | |
| 3194 | trans->alloc_exclude_start = 0; |
| 3195 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3196 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 3197 | finish_current_insert(trans, extent_root, 0); |
| 3198 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3199 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3200 | if (ret) |
| 3201 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3202 | if (pending_ret) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3203 | ret = pending_ret; |
| 3204 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3205 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3206 | |
| 3207 | update_block: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3208 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 3209 | if (ret) { |
| 3210 | printk("update block group failed for %Lu %Lu\n", |
| 3211 | ins->objectid, ins->offset); |
| 3212 | BUG(); |
| 3213 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3214 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3215 | return ret; |
| 3216 | } |
| 3217 | |
| 3218 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3219 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3220 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3221 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3222 | { |
| 3223 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 3224 | |
| 3225 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 3226 | return 0; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3227 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 3228 | ref_generation, owner, ins); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3229 | update_reserved_extents(root, ins->objectid, ins->offset, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3230 | return ret; |
| 3231 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3232 | |
| 3233 | /* |
| 3234 | * this is used by the tree logging recovery code. It records that |
| 3235 | * an extent has been allocated and makes sure to clear the free |
| 3236 | * space cache bits as well |
| 3237 | */ |
| 3238 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3239 | struct btrfs_root *root, u64 parent, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3240 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3241 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3242 | { |
| 3243 | int ret; |
| 3244 | struct btrfs_block_group_cache *block_group; |
| 3245 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3246 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3247 | mutex_lock(&block_group->alloc_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3248 | cache_block_group(root, block_group); |
| 3249 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3250 | ret = btrfs_remove_free_space_lock(block_group, ins->objectid, |
| 3251 | ins->offset); |
| 3252 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3253 | BUG_ON(ret); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3254 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 3255 | ref_generation, owner, ins); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3256 | return ret; |
| 3257 | } |
| 3258 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3259 | /* |
| 3260 | * finds a free extent and does all the dirty work required for allocation |
| 3261 | * returns the key for the extent through ins, and a tree buffer for |
| 3262 | * the first block of the extent through buf. |
| 3263 | * |
| 3264 | * returns 0 if everything worked, non-zero otherwise. |
| 3265 | */ |
| 3266 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 3267 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3268 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3269 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3270 | u64 owner_objectid, u64 empty_size, u64 hint_byte, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3271 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 3272 | { |
| 3273 | int ret; |
| 3274 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3275 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 3276 | min_alloc_size, empty_size, hint_byte, |
| 3277 | search_end, ins, data); |
| 3278 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3279 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3280 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
| 3281 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3282 | owner_objectid, ins); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3283 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3284 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3285 | } else { |
| 3286 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3287 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3288 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3289 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3290 | |
| 3291 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 3292 | struct btrfs_root *root, |
| 3293 | u64 bytenr, u32 blocksize) |
| 3294 | { |
| 3295 | struct extent_buffer *buf; |
| 3296 | |
| 3297 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 3298 | if (!buf) |
| 3299 | return ERR_PTR(-ENOMEM); |
| 3300 | btrfs_set_header_generation(buf, trans->transid); |
| 3301 | btrfs_tree_lock(buf); |
| 3302 | clean_tree_block(trans, root, buf); |
| 3303 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3304 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 3305 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3306 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3307 | } else { |
| 3308 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 3309 | buf->start + buf->len - 1, GFP_NOFS); |
| 3310 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3311 | trans->blocks_used++; |
| 3312 | return buf; |
| 3313 | } |
| 3314 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3315 | /* |
| 3316 | * helper function to allocate a block for a given tree |
| 3317 | * returns the tree buffer or NULL. |
| 3318 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3319 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3320 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3321 | u32 blocksize, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3322 | u64 root_objectid, |
| 3323 | u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3324 | int level, |
| 3325 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3326 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3327 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3328 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3329 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3330 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3331 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3332 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3333 | root_objectid, ref_generation, level, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3334 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3335 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3336 | BUG_ON(ret > 0); |
| 3337 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3338 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 3339 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3340 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3341 | return buf; |
| 3342 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3343 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3344 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3345 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3346 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3347 | u64 leaf_owner; |
| 3348 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3349 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3350 | struct btrfs_file_extent_item *fi; |
| 3351 | int i; |
| 3352 | int nritems; |
| 3353 | int ret; |
| 3354 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3355 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 3356 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3357 | leaf_owner = btrfs_header_owner(leaf); |
| 3358 | leaf_generation = btrfs_header_generation(leaf); |
| 3359 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3360 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3361 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 3362 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3363 | |
| 3364 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 3365 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3366 | continue; |
| 3367 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3368 | if (btrfs_file_extent_type(leaf, fi) == |
| 3369 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 3370 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3371 | /* |
| 3372 | * FIXME make sure to insert a trans record that |
| 3373 | * repeats the snapshot del on crash |
| 3374 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3375 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 3376 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 3377 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3378 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3379 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3380 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3381 | leaf->start, leaf_owner, leaf_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3382 | key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3383 | BUG_ON(ret); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3384 | |
| 3385 | atomic_inc(&root->fs_info->throttle_gen); |
| 3386 | wake_up(&root->fs_info->transaction_throttle); |
| 3387 | cond_resched(); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3388 | } |
| 3389 | return 0; |
| 3390 | } |
| 3391 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3392 | static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3393 | struct btrfs_root *root, |
| 3394 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3395 | { |
| 3396 | int i; |
| 3397 | int ret; |
| 3398 | struct btrfs_extent_info *info = ref->extents; |
| 3399 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3400 | for (i = 0; i < ref->nritems; i++) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3401 | ret = __btrfs_free_extent(trans, root, info->bytenr, |
| 3402 | info->num_bytes, ref->bytenr, |
| 3403 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3404 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3405 | |
| 3406 | atomic_inc(&root->fs_info->throttle_gen); |
| 3407 | wake_up(&root->fs_info->transaction_throttle); |
| 3408 | cond_resched(); |
| 3409 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3410 | BUG_ON(ret); |
| 3411 | info++; |
| 3412 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3413 | |
| 3414 | return 0; |
| 3415 | } |
| 3416 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3417 | int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len, |
| 3418 | u32 *refs) |
| 3419 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3420 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3421 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3422 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3423 | BUG_ON(ret); |
| 3424 | |
| 3425 | #if 0 // some debugging code in case we see problems here |
| 3426 | /* if the refs count is one, it won't get increased again. But |
| 3427 | * if the ref count is > 1, someone may be decreasing it at |
| 3428 | * the same time we are. |
| 3429 | */ |
| 3430 | if (*refs != 1) { |
| 3431 | struct extent_buffer *eb = NULL; |
| 3432 | eb = btrfs_find_create_tree_block(root, start, len); |
| 3433 | if (eb) |
| 3434 | btrfs_tree_lock(eb); |
| 3435 | |
| 3436 | mutex_lock(&root->fs_info->alloc_mutex); |
| 3437 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 3438 | BUG_ON(ret); |
| 3439 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 3440 | |
| 3441 | if (eb) { |
| 3442 | btrfs_tree_unlock(eb); |
| 3443 | free_extent_buffer(eb); |
| 3444 | } |
| 3445 | if (*refs == 1) { |
| 3446 | printk("block %llu went down to one during drop_snap\n", |
| 3447 | (unsigned long long)start); |
| 3448 | } |
| 3449 | |
| 3450 | } |
| 3451 | #endif |
| 3452 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3453 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3454 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3458 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 3459 | * counts as it goes. |
| 3460 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3461 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
| 3462 | struct btrfs_root *root, |
| 3463 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3464 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3465 | u64 root_owner; |
| 3466 | u64 root_gen; |
| 3467 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3468 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3469 | struct extent_buffer *next; |
| 3470 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3471 | struct extent_buffer *parent; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3472 | struct btrfs_leaf_ref *ref; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3473 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3474 | int ret; |
| 3475 | u32 refs; |
| 3476 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3477 | WARN_ON(*level < 0); |
| 3478 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3479 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3480 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3481 | BUG_ON(ret); |
| 3482 | if (refs > 1) |
| 3483 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3484 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3485 | /* |
| 3486 | * walk down to the last node level and free all the leaves |
| 3487 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3488 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3489 | WARN_ON(*level < 0); |
| 3490 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3491 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3492 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3493 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3494 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3495 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3496 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3497 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3498 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3499 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3500 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3501 | BUG_ON(ret); |
| 3502 | break; |
| 3503 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3504 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3505 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3506 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3507 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3508 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3509 | BUG_ON(ret); |
| 3510 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3511 | parent = path->nodes[*level]; |
| 3512 | root_owner = btrfs_header_owner(parent); |
| 3513 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3514 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3515 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3516 | ret = __btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3517 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3518 | root_owner, root_gen, |
| 3519 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3520 | BUG_ON(ret); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3521 | |
| 3522 | atomic_inc(&root->fs_info->throttle_gen); |
| 3523 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3524 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3525 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3526 | continue; |
| 3527 | } |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3528 | /* |
| 3529 | * at this point, we have a single ref, and since the |
| 3530 | * only place referencing this extent is a dead root |
| 3531 | * the reference count should never go higher. |
| 3532 | * So, we don't need to check it again |
| 3533 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3534 | if (*level == 1) { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3535 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3536 | if (ref && ref->generation != ptr_gen) { |
| 3537 | btrfs_free_leaf_ref(root, ref); |
| 3538 | ref = NULL; |
| 3539 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3540 | if (ref) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3541 | ret = cache_drop_leaf_ref(trans, root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3542 | BUG_ON(ret); |
| 3543 | btrfs_remove_leaf_ref(root, ref); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 3544 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3545 | *level = 0; |
| 3546 | break; |
| 3547 | } |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3548 | if (printk_ratelimit()) { |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 3549 | printk("leaf ref miss for bytenr %llu\n", |
| 3550 | (unsigned long long)bytenr); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3551 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3552 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3553 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3554 | if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3555 | free_extent_buffer(next); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3556 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3557 | next = read_tree_block(root, bytenr, blocksize, |
| 3558 | ptr_gen); |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3559 | cond_resched(); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3560 | #if 0 |
| 3561 | /* |
| 3562 | * this is a debugging check and can go away |
| 3563 | * the ref should never go all the way down to 1 |
| 3564 | * at this point |
| 3565 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3566 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, |
| 3567 | &refs); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 3568 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3569 | WARN_ON(refs != 1); |
| 3570 | #endif |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 3571 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3572 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3573 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3574 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3575 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3576 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3577 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3578 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3579 | } |
| 3580 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3581 | WARN_ON(*level < 0); |
| 3582 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3583 | |
| 3584 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3585 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3586 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3587 | } else { |
| 3588 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3589 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3590 | } |
| 3591 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3592 | blocksize = btrfs_level_size(root, *level); |
| 3593 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3594 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3595 | |
| 3596 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3597 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3598 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3599 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3600 | path->nodes[*level] = NULL; |
| 3601 | *level += 1; |
| 3602 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3603 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3604 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3605 | return 0; |
| 3606 | } |
| 3607 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3608 | /* |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3609 | * helper function for drop_subtree, this function is similar to |
| 3610 | * walk_down_tree. The main difference is that it checks reference |
| 3611 | * counts while tree blocks are locked. |
| 3612 | */ |
| 3613 | static int noinline walk_down_subtree(struct btrfs_trans_handle *trans, |
| 3614 | struct btrfs_root *root, |
| 3615 | struct btrfs_path *path, int *level) |
| 3616 | { |
| 3617 | struct extent_buffer *next; |
| 3618 | struct extent_buffer *cur; |
| 3619 | struct extent_buffer *parent; |
| 3620 | u64 bytenr; |
| 3621 | u64 ptr_gen; |
| 3622 | u32 blocksize; |
| 3623 | u32 refs; |
| 3624 | int ret; |
| 3625 | |
| 3626 | cur = path->nodes[*level]; |
| 3627 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, |
| 3628 | &refs); |
| 3629 | BUG_ON(ret); |
| 3630 | if (refs > 1) |
| 3631 | goto out; |
| 3632 | |
| 3633 | while (*level >= 0) { |
| 3634 | cur = path->nodes[*level]; |
| 3635 | if (*level == 0) { |
| 3636 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
| 3637 | BUG_ON(ret); |
| 3638 | clean_tree_block(trans, root, cur); |
| 3639 | break; |
| 3640 | } |
| 3641 | if (path->slots[*level] >= btrfs_header_nritems(cur)) { |
| 3642 | clean_tree_block(trans, root, cur); |
| 3643 | break; |
| 3644 | } |
| 3645 | |
| 3646 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 3647 | blocksize = btrfs_level_size(root, *level - 1); |
| 3648 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
| 3649 | |
| 3650 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
| 3651 | btrfs_tree_lock(next); |
| 3652 | |
| 3653 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, |
| 3654 | &refs); |
| 3655 | BUG_ON(ret); |
| 3656 | if (refs > 1) { |
| 3657 | parent = path->nodes[*level]; |
| 3658 | ret = btrfs_free_extent(trans, root, bytenr, |
| 3659 | blocksize, parent->start, |
| 3660 | btrfs_header_owner(parent), |
| 3661 | btrfs_header_generation(parent), |
| 3662 | *level - 1, 1); |
| 3663 | BUG_ON(ret); |
| 3664 | path->slots[*level]++; |
| 3665 | btrfs_tree_unlock(next); |
| 3666 | free_extent_buffer(next); |
| 3667 | continue; |
| 3668 | } |
| 3669 | |
| 3670 | *level = btrfs_header_level(next); |
| 3671 | path->nodes[*level] = next; |
| 3672 | path->slots[*level] = 0; |
| 3673 | path->locks[*level] = 1; |
| 3674 | cond_resched(); |
| 3675 | } |
| 3676 | out: |
| 3677 | parent = path->nodes[*level + 1]; |
| 3678 | bytenr = path->nodes[*level]->start; |
| 3679 | blocksize = path->nodes[*level]->len; |
| 3680 | |
| 3681 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
| 3682 | parent->start, btrfs_header_owner(parent), |
| 3683 | btrfs_header_generation(parent), *level, 1); |
| 3684 | BUG_ON(ret); |
| 3685 | |
| 3686 | if (path->locks[*level]) { |
| 3687 | btrfs_tree_unlock(path->nodes[*level]); |
| 3688 | path->locks[*level] = 0; |
| 3689 | } |
| 3690 | free_extent_buffer(path->nodes[*level]); |
| 3691 | path->nodes[*level] = NULL; |
| 3692 | *level += 1; |
| 3693 | cond_resched(); |
| 3694 | return 0; |
| 3695 | } |
| 3696 | |
| 3697 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3698 | * helper for dropping snapshots. This walks back up the tree in the path |
| 3699 | * to find the first node higher up where we haven't yet gone through |
| 3700 | * all the slots |
| 3701 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3702 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
| 3703 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3704 | struct btrfs_path *path, |
| 3705 | int *level, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3706 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3707 | u64 root_owner; |
| 3708 | u64 root_gen; |
| 3709 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3710 | int i; |
| 3711 | int slot; |
| 3712 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3713 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3714 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3715 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3716 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 3717 | struct extent_buffer *node; |
| 3718 | struct btrfs_disk_key disk_key; |
| 3719 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3720 | path->slots[i]++; |
| 3721 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3722 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3723 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3724 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3725 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3726 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3727 | return 0; |
| 3728 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3729 | struct extent_buffer *parent; |
| 3730 | if (path->nodes[*level] == root->node) |
| 3731 | parent = path->nodes[*level]; |
| 3732 | else |
| 3733 | parent = path->nodes[*level + 1]; |
| 3734 | |
| 3735 | root_owner = btrfs_header_owner(parent); |
| 3736 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3737 | |
| 3738 | clean_tree_block(trans, root, path->nodes[*level]); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3739 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3740 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3741 | path->nodes[*level]->len, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3742 | parent->start, root_owner, |
| 3743 | root_gen, *level, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3744 | BUG_ON(ret); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3745 | if (path->locks[*level]) { |
| 3746 | btrfs_tree_unlock(path->nodes[*level]); |
| 3747 | path->locks[*level] = 0; |
| 3748 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3749 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3750 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3751 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3752 | } |
| 3753 | } |
| 3754 | return 1; |
| 3755 | } |
| 3756 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3757 | /* |
| 3758 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 3759 | * the tree freeing any blocks that have a ref count of zero after being |
| 3760 | * decremented. |
| 3761 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3762 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3763 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3764 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 3765 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3766 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3767 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3768 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3769 | int i; |
| 3770 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3771 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3772 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3773 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3774 | path = btrfs_alloc_path(); |
| 3775 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3776 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3777 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3778 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3779 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 3780 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3781 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3782 | path->slots[level] = 0; |
| 3783 | } else { |
| 3784 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3785 | struct btrfs_disk_key found_key; |
| 3786 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3787 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3788 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3789 | level = root_item->drop_level; |
| 3790 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3791 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3792 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3793 | ret = wret; |
| 3794 | goto out; |
| 3795 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3796 | node = path->nodes[level]; |
| 3797 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 3798 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 3799 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3800 | /* |
| 3801 | * unlock our path, this is safe because only this |
| 3802 | * function is allowed to delete this snapshot |
| 3803 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3804 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 3805 | if (path->nodes[i] && path->locks[i]) { |
| 3806 | path->locks[i] = 0; |
| 3807 | btrfs_tree_unlock(path->nodes[i]); |
| 3808 | } |
| 3809 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3810 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3811 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3812 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3813 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3814 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3815 | if (wret < 0) |
| 3816 | ret = wret; |
| 3817 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3818 | wret = walk_up_tree(trans, root, path, &level, |
| 3819 | BTRFS_MAX_LEVEL); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3820 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3821 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3822 | if (wret < 0) |
| 3823 | ret = wret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3824 | if (trans->transaction->in_commit) { |
| 3825 | ret = -EAGAIN; |
| 3826 | break; |
| 3827 | } |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3828 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3829 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3830 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3831 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3832 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3833 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 3834 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3835 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3836 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3837 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3838 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3839 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3840 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3841 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3842 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 3843 | struct btrfs_root *root, |
| 3844 | struct extent_buffer *node, |
| 3845 | struct extent_buffer *parent) |
| 3846 | { |
| 3847 | struct btrfs_path *path; |
| 3848 | int level; |
| 3849 | int parent_level; |
| 3850 | int ret = 0; |
| 3851 | int wret; |
| 3852 | |
| 3853 | path = btrfs_alloc_path(); |
| 3854 | BUG_ON(!path); |
| 3855 | |
| 3856 | BUG_ON(!btrfs_tree_locked(parent)); |
| 3857 | parent_level = btrfs_header_level(parent); |
| 3858 | extent_buffer_get(parent); |
| 3859 | path->nodes[parent_level] = parent; |
| 3860 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 3861 | |
| 3862 | BUG_ON(!btrfs_tree_locked(node)); |
| 3863 | level = btrfs_header_level(node); |
| 3864 | extent_buffer_get(node); |
| 3865 | path->nodes[level] = node; |
| 3866 | path->slots[level] = 0; |
| 3867 | |
| 3868 | while (1) { |
| 3869 | wret = walk_down_subtree(trans, root, path, &level); |
| 3870 | if (wret < 0) |
| 3871 | ret = wret; |
| 3872 | if (wret != 0) |
| 3873 | break; |
| 3874 | |
| 3875 | wret = walk_up_tree(trans, root, path, &level, parent_level); |
| 3876 | if (wret < 0) |
| 3877 | ret = wret; |
| 3878 | if (wret != 0) |
| 3879 | break; |
| 3880 | } |
| 3881 | |
| 3882 | btrfs_free_path(path); |
| 3883 | return ret; |
| 3884 | } |
| 3885 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3886 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 3887 | unsigned long nr) |
| 3888 | { |
| 3889 | return min(last, start + nr - 1); |
| 3890 | } |
| 3891 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3892 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
| 3893 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3894 | { |
| 3895 | u64 page_start; |
| 3896 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3897 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3898 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3899 | unsigned long i; |
| 3900 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3901 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3902 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3903 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3904 | unsigned int total_read = 0; |
| 3905 | unsigned int total_dirty = 0; |
| 3906 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3907 | |
| 3908 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3909 | |
| 3910 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3911 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3912 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 3913 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3914 | /* make sure the dirty trick played by the caller work */ |
| 3915 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 3916 | first_index, last_index); |
| 3917 | if (ret) |
| 3918 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3919 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3920 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3921 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3922 | for (i = first_index ; i <= last_index; i++) { |
| 3923 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3924 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3925 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3926 | } |
| 3927 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3928 | again: |
| 3929 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3930 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3931 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3932 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3933 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3934 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3935 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3936 | if (!PageUptodate(page)) { |
| 3937 | btrfs_readpage(NULL, page); |
| 3938 | lock_page(page); |
| 3939 | if (!PageUptodate(page)) { |
| 3940 | unlock_page(page); |
| 3941 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3942 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3943 | goto out_unlock; |
| 3944 | } |
| 3945 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3946 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3947 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3948 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 3949 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3950 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3951 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3952 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3953 | if (ordered) { |
| 3954 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3955 | unlock_page(page); |
| 3956 | page_cache_release(page); |
| 3957 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3958 | btrfs_put_ordered_extent(ordered); |
| 3959 | goto again; |
| 3960 | } |
| 3961 | set_page_extent_mapped(page); |
| 3962 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3963 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3964 | if (i == first_index) |
| 3965 | set_extent_bits(io_tree, page_start, page_end, |
| 3966 | EXTENT_BOUNDARY, GFP_NOFS); |
| 3967 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3968 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3969 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3970 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3971 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3972 | unlock_page(page); |
| 3973 | page_cache_release(page); |
| 3974 | } |
| 3975 | |
| 3976 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3977 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3978 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3979 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 3980 | return ret; |
| 3981 | } |
| 3982 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3983 | static int noinline relocate_data_extent(struct inode *reloc_inode, |
| 3984 | struct btrfs_key *extent_key, |
| 3985 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3986 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3987 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 3988 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 3989 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3990 | u64 start = extent_key->objectid - offset; |
| 3991 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3992 | |
| 3993 | em = alloc_extent_map(GFP_NOFS); |
| 3994 | BUG_ON(!em || IS_ERR(em)); |
| 3995 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 3996 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3997 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 3998 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3999 | em->block_start = extent_key->objectid; |
| 4000 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4001 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4002 | |
| 4003 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4004 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4005 | while (1) { |
| 4006 | int ret; |
| 4007 | spin_lock(&em_tree->lock); |
| 4008 | ret = add_extent_mapping(em_tree, em); |
| 4009 | spin_unlock(&em_tree->lock); |
| 4010 | if (ret != -EEXIST) { |
| 4011 | free_extent_map(em); |
| 4012 | break; |
| 4013 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4014 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4015 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4016 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4017 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4018 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | struct btrfs_ref_path { |
| 4022 | u64 extent_start; |
| 4023 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 4024 | u64 root_objectid; |
| 4025 | u64 root_generation; |
| 4026 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4027 | u32 num_refs; |
| 4028 | int lowest_level; |
| 4029 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4030 | int shared_level; |
| 4031 | |
| 4032 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 4033 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4034 | }; |
| 4035 | |
| 4036 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4037 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4038 | u64 disk_bytenr; |
| 4039 | u64 disk_num_bytes; |
| 4040 | u64 offset; |
| 4041 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4042 | u8 compression; |
| 4043 | u8 encryption; |
| 4044 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4045 | }; |
| 4046 | |
| 4047 | static int is_cowonly_root(u64 root_objectid) |
| 4048 | { |
| 4049 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 4050 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 4051 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 4052 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
| 4053 | root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 4054 | return 1; |
| 4055 | return 0; |
| 4056 | } |
| 4057 | |
| 4058 | static int noinline __next_ref_path(struct btrfs_trans_handle *trans, |
| 4059 | struct btrfs_root *extent_root, |
| 4060 | struct btrfs_ref_path *ref_path, |
| 4061 | int first_time) |
| 4062 | { |
| 4063 | struct extent_buffer *leaf; |
| 4064 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4065 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4066 | struct btrfs_key key; |
| 4067 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4068 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4069 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4070 | int level; |
| 4071 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4072 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4073 | path = btrfs_alloc_path(); |
| 4074 | if (!path) |
| 4075 | return -ENOMEM; |
| 4076 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4077 | if (first_time) { |
| 4078 | ref_path->lowest_level = -1; |
| 4079 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4080 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4081 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4082 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4083 | walk_down: |
| 4084 | level = ref_path->current_level - 1; |
| 4085 | while (level >= -1) { |
| 4086 | u64 parent; |
| 4087 | if (level < ref_path->lowest_level) |
| 4088 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4089 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4090 | if (level >= 0) { |
| 4091 | bytenr = ref_path->nodes[level]; |
| 4092 | } else { |
| 4093 | bytenr = ref_path->extent_start; |
| 4094 | } |
| 4095 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4096 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4097 | parent = ref_path->nodes[level + 1]; |
| 4098 | ref_path->nodes[level + 1] = 0; |
| 4099 | ref_path->current_level = level; |
| 4100 | BUG_ON(parent == 0); |
| 4101 | |
| 4102 | key.objectid = bytenr; |
| 4103 | key.offset = parent + 1; |
| 4104 | key.type = BTRFS_EXTENT_REF_KEY; |
| 4105 | |
| 4106 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4107 | if (ret < 0) |
| 4108 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4109 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4110 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4111 | leaf = path->nodes[0]; |
| 4112 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4113 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4114 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4115 | if (ret < 0) |
| 4116 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4117 | if (ret > 0) |
| 4118 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 4119 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4120 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4121 | |
| 4122 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4123 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4124 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 4125 | if (level < ref_path->shared_level) |
| 4126 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4127 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4128 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4129 | next: |
| 4130 | level--; |
| 4131 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4132 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4133 | } |
| 4134 | /* reached lowest level */ |
| 4135 | ret = 1; |
| 4136 | goto out; |
| 4137 | walk_up: |
| 4138 | level = ref_path->current_level; |
| 4139 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 4140 | u64 ref_objectid; |
| 4141 | if (level >= 0) { |
| 4142 | bytenr = ref_path->nodes[level]; |
| 4143 | } else { |
| 4144 | bytenr = ref_path->extent_start; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4145 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4146 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4147 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4148 | key.objectid = bytenr; |
| 4149 | key.offset = 0; |
| 4150 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4151 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4152 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 4153 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4154 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4155 | |
| 4156 | leaf = path->nodes[0]; |
| 4157 | nritems = btrfs_header_nritems(leaf); |
| 4158 | if (path->slots[0] >= nritems) { |
| 4159 | ret = btrfs_next_leaf(extent_root, path); |
| 4160 | if (ret < 0) |
| 4161 | goto out; |
| 4162 | if (ret > 0) { |
| 4163 | /* the extent was freed by someone */ |
| 4164 | if (ref_path->lowest_level == level) |
| 4165 | goto out; |
| 4166 | btrfs_release_path(extent_root, path); |
| 4167 | goto walk_down; |
| 4168 | } |
| 4169 | leaf = path->nodes[0]; |
| 4170 | } |
| 4171 | |
| 4172 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4173 | if (found_key.objectid != bytenr || |
| 4174 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 4175 | /* the extent was freed by someone */ |
| 4176 | if (ref_path->lowest_level == level) { |
| 4177 | ret = 1; |
| 4178 | goto out; |
| 4179 | } |
| 4180 | btrfs_release_path(extent_root, path); |
| 4181 | goto walk_down; |
| 4182 | } |
| 4183 | found: |
| 4184 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 4185 | struct btrfs_extent_ref); |
| 4186 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 4187 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4188 | if (first_time) { |
| 4189 | level = (int)ref_objectid; |
| 4190 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 4191 | ref_path->lowest_level = level; |
| 4192 | ref_path->current_level = level; |
| 4193 | ref_path->nodes[level] = bytenr; |
| 4194 | } else { |
| 4195 | WARN_ON(ref_objectid != level); |
| 4196 | } |
| 4197 | } else { |
| 4198 | WARN_ON(level != -1); |
| 4199 | } |
| 4200 | first_time = 0; |
| 4201 | |
| 4202 | if (ref_path->lowest_level == level) { |
| 4203 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4204 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 4205 | } |
| 4206 | |
| 4207 | /* |
| 4208 | * the block is tree root or the block isn't in reference |
| 4209 | * counted tree. |
| 4210 | */ |
| 4211 | if (found_key.objectid == found_key.offset || |
| 4212 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 4213 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4214 | ref_path->root_generation = |
| 4215 | btrfs_ref_generation(leaf, ref); |
| 4216 | if (level < 0) { |
| 4217 | /* special reference from the tree log */ |
| 4218 | ref_path->nodes[0] = found_key.offset; |
| 4219 | ref_path->current_level = 0; |
| 4220 | } |
| 4221 | ret = 0; |
| 4222 | goto out; |
| 4223 | } |
| 4224 | |
| 4225 | level++; |
| 4226 | BUG_ON(ref_path->nodes[level] != 0); |
| 4227 | ref_path->nodes[level] = found_key.offset; |
| 4228 | ref_path->current_level = level; |
| 4229 | |
| 4230 | /* |
| 4231 | * the reference was created in the running transaction, |
| 4232 | * no need to continue walking up. |
| 4233 | */ |
| 4234 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 4235 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4236 | ref_path->root_generation = |
| 4237 | btrfs_ref_generation(leaf, ref); |
| 4238 | ret = 0; |
| 4239 | goto out; |
| 4240 | } |
| 4241 | |
| 4242 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4243 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4244 | } |
| 4245 | /* reached max tree level, but no tree root found. */ |
| 4246 | BUG(); |
| 4247 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4248 | btrfs_free_path(path); |
| 4249 | return ret; |
| 4250 | } |
| 4251 | |
| 4252 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 4253 | struct btrfs_root *extent_root, |
| 4254 | struct btrfs_ref_path *ref_path, |
| 4255 | u64 extent_start) |
| 4256 | { |
| 4257 | memset(ref_path, 0, sizeof(*ref_path)); |
| 4258 | ref_path->extent_start = extent_start; |
| 4259 | |
| 4260 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 4261 | } |
| 4262 | |
| 4263 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 4264 | struct btrfs_root *extent_root, |
| 4265 | struct btrfs_ref_path *ref_path) |
| 4266 | { |
| 4267 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 4268 | } |
| 4269 | |
| 4270 | static int noinline get_new_locations(struct inode *reloc_inode, |
| 4271 | struct btrfs_key *extent_key, |
| 4272 | u64 offset, int no_fragment, |
| 4273 | struct disk_extent **extents, |
| 4274 | int *nr_extents) |
| 4275 | { |
| 4276 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4277 | struct btrfs_path *path; |
| 4278 | struct btrfs_file_extent_item *fi; |
| 4279 | struct extent_buffer *leaf; |
| 4280 | struct disk_extent *exts = *extents; |
| 4281 | struct btrfs_key found_key; |
| 4282 | u64 cur_pos; |
| 4283 | u64 last_byte; |
| 4284 | u32 nritems; |
| 4285 | int nr = 0; |
| 4286 | int max = *nr_extents; |
| 4287 | int ret; |
| 4288 | |
| 4289 | WARN_ON(!no_fragment && *extents); |
| 4290 | if (!exts) { |
| 4291 | max = 1; |
| 4292 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4293 | if (!exts) |
| 4294 | return -ENOMEM; |
| 4295 | } |
| 4296 | |
| 4297 | path = btrfs_alloc_path(); |
| 4298 | BUG_ON(!path); |
| 4299 | |
| 4300 | cur_pos = extent_key->objectid - offset; |
| 4301 | last_byte = extent_key->objectid + extent_key->offset; |
| 4302 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 4303 | cur_pos, 0); |
| 4304 | if (ret < 0) |
| 4305 | goto out; |
| 4306 | if (ret > 0) { |
| 4307 | ret = -ENOENT; |
| 4308 | goto out; |
| 4309 | } |
| 4310 | |
| 4311 | while (1) { |
| 4312 | leaf = path->nodes[0]; |
| 4313 | nritems = btrfs_header_nritems(leaf); |
| 4314 | if (path->slots[0] >= nritems) { |
| 4315 | ret = btrfs_next_leaf(root, path); |
| 4316 | if (ret < 0) |
| 4317 | goto out; |
| 4318 | if (ret > 0) |
| 4319 | break; |
| 4320 | leaf = path->nodes[0]; |
| 4321 | } |
| 4322 | |
| 4323 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4324 | if (found_key.offset != cur_pos || |
| 4325 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 4326 | found_key.objectid != reloc_inode->i_ino) |
| 4327 | break; |
| 4328 | |
| 4329 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4330 | struct btrfs_file_extent_item); |
| 4331 | if (btrfs_file_extent_type(leaf, fi) != |
| 4332 | BTRFS_FILE_EXTENT_REG || |
| 4333 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4334 | break; |
| 4335 | |
| 4336 | if (nr == max) { |
| 4337 | struct disk_extent *old = exts; |
| 4338 | max *= 2; |
| 4339 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4340 | memcpy(exts, old, sizeof(*exts) * nr); |
| 4341 | if (old != *extents) |
| 4342 | kfree(old); |
| 4343 | } |
| 4344 | |
| 4345 | exts[nr].disk_bytenr = |
| 4346 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4347 | exts[nr].disk_num_bytes = |
| 4348 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4349 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 4350 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4351 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 4352 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 4353 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 4354 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 4355 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4356 | BUG_ON(exts[nr].offset > 0); |
| 4357 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 4358 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4359 | |
| 4360 | cur_pos += exts[nr].num_bytes; |
| 4361 | nr++; |
| 4362 | |
| 4363 | if (cur_pos + offset >= last_byte) |
| 4364 | break; |
| 4365 | |
| 4366 | if (no_fragment) { |
| 4367 | ret = 1; |
| 4368 | goto out; |
| 4369 | } |
| 4370 | path->slots[0]++; |
| 4371 | } |
| 4372 | |
| 4373 | WARN_ON(cur_pos + offset > last_byte); |
| 4374 | if (cur_pos + offset < last_byte) { |
| 4375 | ret = -ENOENT; |
| 4376 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4377 | } |
| 4378 | ret = 0; |
| 4379 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4380 | btrfs_free_path(path); |
| 4381 | if (ret) { |
| 4382 | if (exts != *extents) |
| 4383 | kfree(exts); |
| 4384 | } else { |
| 4385 | *extents = exts; |
| 4386 | *nr_extents = nr; |
| 4387 | } |
| 4388 | return ret; |
| 4389 | } |
| 4390 | |
| 4391 | static int noinline replace_one_extent(struct btrfs_trans_handle *trans, |
| 4392 | struct btrfs_root *root, |
| 4393 | struct btrfs_path *path, |
| 4394 | struct btrfs_key *extent_key, |
| 4395 | struct btrfs_key *leaf_key, |
| 4396 | struct btrfs_ref_path *ref_path, |
| 4397 | struct disk_extent *new_extents, |
| 4398 | int nr_extents) |
| 4399 | { |
| 4400 | struct extent_buffer *leaf; |
| 4401 | struct btrfs_file_extent_item *fi; |
| 4402 | struct inode *inode = NULL; |
| 4403 | struct btrfs_key key; |
| 4404 | u64 lock_start = 0; |
| 4405 | u64 lock_end = 0; |
| 4406 | u64 num_bytes; |
| 4407 | u64 ext_offset; |
| 4408 | u64 first_pos; |
| 4409 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4410 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4411 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4412 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4413 | int ret; |
| 4414 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4415 | memcpy(&key, leaf_key, sizeof(key)); |
| 4416 | first_pos = INT_LIMIT(loff_t) - extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4417 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4418 | if (key.objectid < ref_path->owner_objectid || |
| 4419 | (key.objectid == ref_path->owner_objectid && |
| 4420 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 4421 | key.objectid = ref_path->owner_objectid; |
| 4422 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4423 | key.offset = 0; |
| 4424 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | while (1) { |
| 4428 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 4429 | if (ret < 0) |
| 4430 | goto out; |
| 4431 | |
| 4432 | leaf = path->nodes[0]; |
| 4433 | nritems = btrfs_header_nritems(leaf); |
| 4434 | next: |
| 4435 | if (extent_locked && ret > 0) { |
| 4436 | /* |
| 4437 | * the file extent item was modified by someone |
| 4438 | * before the extent got locked. |
| 4439 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4440 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4441 | lock_end, GFP_NOFS); |
| 4442 | extent_locked = 0; |
| 4443 | } |
| 4444 | |
| 4445 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4446 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4447 | break; |
| 4448 | |
| 4449 | BUG_ON(extent_locked); |
| 4450 | ret = btrfs_next_leaf(root, path); |
| 4451 | if (ret < 0) |
| 4452 | goto out; |
| 4453 | if (ret > 0) |
| 4454 | break; |
| 4455 | leaf = path->nodes[0]; |
| 4456 | nritems = btrfs_header_nritems(leaf); |
| 4457 | } |
| 4458 | |
| 4459 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4460 | |
| 4461 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 4462 | if ((key.objectid > ref_path->owner_objectid) || |
| 4463 | (key.objectid == ref_path->owner_objectid && |
| 4464 | key.type > BTRFS_EXTENT_DATA_KEY) || |
| 4465 | (key.offset >= first_pos + extent_key->offset)) |
| 4466 | break; |
| 4467 | } |
| 4468 | |
| 4469 | if (inode && key.objectid != inode->i_ino) { |
| 4470 | BUG_ON(extent_locked); |
| 4471 | btrfs_release_path(root, path); |
| 4472 | mutex_unlock(&inode->i_mutex); |
| 4473 | iput(inode); |
| 4474 | inode = NULL; |
| 4475 | continue; |
| 4476 | } |
| 4477 | |
| 4478 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4479 | path->slots[0]++; |
| 4480 | ret = 1; |
| 4481 | goto next; |
| 4482 | } |
| 4483 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4484 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4485 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4486 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 4487 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4488 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 4489 | extent_key->objectid)) { |
| 4490 | path->slots[0]++; |
| 4491 | ret = 1; |
| 4492 | goto next; |
| 4493 | } |
| 4494 | |
| 4495 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4496 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 4497 | |
| 4498 | if (first_pos > key.offset - ext_offset) |
| 4499 | first_pos = key.offset - ext_offset; |
| 4500 | |
| 4501 | if (!extent_locked) { |
| 4502 | lock_start = key.offset; |
| 4503 | lock_end = lock_start + num_bytes - 1; |
| 4504 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4505 | if (lock_start > key.offset || |
| 4506 | lock_end + 1 < key.offset + num_bytes) { |
| 4507 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4508 | lock_start, lock_end, GFP_NOFS); |
| 4509 | extent_locked = 0; |
| 4510 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | if (!inode) { |
| 4514 | btrfs_release_path(root, path); |
| 4515 | |
| 4516 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 4517 | key.objectid, root); |
| 4518 | if (inode->i_state & I_NEW) { |
| 4519 | BTRFS_I(inode)->root = root; |
| 4520 | BTRFS_I(inode)->location.objectid = |
| 4521 | key.objectid; |
| 4522 | BTRFS_I(inode)->location.type = |
| 4523 | BTRFS_INODE_ITEM_KEY; |
| 4524 | BTRFS_I(inode)->location.offset = 0; |
| 4525 | btrfs_read_locked_inode(inode); |
| 4526 | unlock_new_inode(inode); |
| 4527 | } |
| 4528 | /* |
| 4529 | * some code call btrfs_commit_transaction while |
| 4530 | * holding the i_mutex, so we can't use mutex_lock |
| 4531 | * here. |
| 4532 | */ |
| 4533 | if (is_bad_inode(inode) || |
| 4534 | !mutex_trylock(&inode->i_mutex)) { |
| 4535 | iput(inode); |
| 4536 | inode = NULL; |
| 4537 | key.offset = (u64)-1; |
| 4538 | goto skip; |
| 4539 | } |
| 4540 | } |
| 4541 | |
| 4542 | if (!extent_locked) { |
| 4543 | struct btrfs_ordered_extent *ordered; |
| 4544 | |
| 4545 | btrfs_release_path(root, path); |
| 4546 | |
| 4547 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4548 | lock_end, GFP_NOFS); |
| 4549 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 4550 | lock_end); |
| 4551 | if (ordered && |
| 4552 | ordered->file_offset <= lock_end && |
| 4553 | ordered->file_offset + ordered->len > lock_start) { |
| 4554 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4555 | lock_start, lock_end, GFP_NOFS); |
| 4556 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 4557 | btrfs_put_ordered_extent(ordered); |
| 4558 | key.offset += num_bytes; |
| 4559 | goto skip; |
| 4560 | } |
| 4561 | if (ordered) |
| 4562 | btrfs_put_ordered_extent(ordered); |
| 4563 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4564 | extent_locked = 1; |
| 4565 | continue; |
| 4566 | } |
| 4567 | |
| 4568 | if (nr_extents == 1) { |
| 4569 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4570 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4571 | new_extents[0].disk_bytenr); |
| 4572 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4573 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4574 | btrfs_mark_buffer_dirty(leaf); |
| 4575 | |
| 4576 | btrfs_drop_extent_cache(inode, key.offset, |
| 4577 | key.offset + num_bytes - 1, 0); |
| 4578 | |
| 4579 | ret = btrfs_inc_extent_ref(trans, root, |
| 4580 | new_extents[0].disk_bytenr, |
| 4581 | new_extents[0].disk_num_bytes, |
| 4582 | leaf->start, |
| 4583 | root->root_key.objectid, |
| 4584 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4585 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4586 | BUG_ON(ret); |
| 4587 | |
| 4588 | ret = btrfs_free_extent(trans, root, |
| 4589 | extent_key->objectid, |
| 4590 | extent_key->offset, |
| 4591 | leaf->start, |
| 4592 | btrfs_header_owner(leaf), |
| 4593 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4594 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4595 | BUG_ON(ret); |
| 4596 | |
| 4597 | btrfs_release_path(root, path); |
| 4598 | key.offset += num_bytes; |
| 4599 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4600 | BUG_ON(1); |
| 4601 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4602 | u64 alloc_hint; |
| 4603 | u64 extent_len; |
| 4604 | int i; |
| 4605 | /* |
| 4606 | * drop old extent pointer at first, then insert the |
| 4607 | * new pointers one bye one |
| 4608 | */ |
| 4609 | btrfs_release_path(root, path); |
| 4610 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 4611 | key.offset + num_bytes, |
| 4612 | key.offset, &alloc_hint); |
| 4613 | BUG_ON(ret); |
| 4614 | |
| 4615 | for (i = 0; i < nr_extents; i++) { |
| 4616 | if (ext_offset >= new_extents[i].num_bytes) { |
| 4617 | ext_offset -= new_extents[i].num_bytes; |
| 4618 | continue; |
| 4619 | } |
| 4620 | extent_len = min(new_extents[i].num_bytes - |
| 4621 | ext_offset, num_bytes); |
| 4622 | |
| 4623 | ret = btrfs_insert_empty_item(trans, root, |
| 4624 | path, &key, |
| 4625 | sizeof(*fi)); |
| 4626 | BUG_ON(ret); |
| 4627 | |
| 4628 | leaf = path->nodes[0]; |
| 4629 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4630 | struct btrfs_file_extent_item); |
| 4631 | btrfs_set_file_extent_generation(leaf, fi, |
| 4632 | trans->transid); |
| 4633 | btrfs_set_file_extent_type(leaf, fi, |
| 4634 | BTRFS_FILE_EXTENT_REG); |
| 4635 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4636 | new_extents[i].disk_bytenr); |
| 4637 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4638 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4639 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 4640 | new_extents[i].ram_bytes); |
| 4641 | |
| 4642 | btrfs_set_file_extent_compression(leaf, fi, |
| 4643 | new_extents[i].compression); |
| 4644 | btrfs_set_file_extent_encryption(leaf, fi, |
| 4645 | new_extents[i].encryption); |
| 4646 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 4647 | new_extents[i].other_encoding); |
| 4648 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4649 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4650 | extent_len); |
| 4651 | ext_offset += new_extents[i].offset; |
| 4652 | btrfs_set_file_extent_offset(leaf, fi, |
| 4653 | ext_offset); |
| 4654 | btrfs_mark_buffer_dirty(leaf); |
| 4655 | |
| 4656 | btrfs_drop_extent_cache(inode, key.offset, |
| 4657 | key.offset + extent_len - 1, 0); |
| 4658 | |
| 4659 | ret = btrfs_inc_extent_ref(trans, root, |
| 4660 | new_extents[i].disk_bytenr, |
| 4661 | new_extents[i].disk_num_bytes, |
| 4662 | leaf->start, |
| 4663 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4664 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4665 | BUG_ON(ret); |
| 4666 | btrfs_release_path(root, path); |
| 4667 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4668 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4669 | |
| 4670 | ext_offset = 0; |
| 4671 | num_bytes -= extent_len; |
| 4672 | key.offset += extent_len; |
| 4673 | |
| 4674 | if (num_bytes == 0) |
| 4675 | break; |
| 4676 | } |
| 4677 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4678 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4679 | } |
| 4680 | |
| 4681 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4682 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4683 | lock_end, GFP_NOFS); |
| 4684 | extent_locked = 0; |
| 4685 | } |
| 4686 | skip: |
| 4687 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
| 4688 | key.offset >= first_pos + extent_key->offset) |
| 4689 | break; |
| 4690 | |
| 4691 | cond_resched(); |
| 4692 | } |
| 4693 | ret = 0; |
| 4694 | out: |
| 4695 | btrfs_release_path(root, path); |
| 4696 | if (inode) { |
| 4697 | mutex_unlock(&inode->i_mutex); |
| 4698 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4699 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4700 | lock_end, GFP_NOFS); |
| 4701 | } |
| 4702 | iput(inode); |
| 4703 | } |
| 4704 | return ret; |
| 4705 | } |
| 4706 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4707 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 4708 | struct btrfs_root *root, |
| 4709 | struct extent_buffer *buf, u64 orig_start) |
| 4710 | { |
| 4711 | int level; |
| 4712 | int ret; |
| 4713 | |
| 4714 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 4715 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 4716 | |
| 4717 | level = btrfs_header_level(buf); |
| 4718 | if (level == 0) { |
| 4719 | struct btrfs_leaf_ref *ref; |
| 4720 | struct btrfs_leaf_ref *orig_ref; |
| 4721 | |
| 4722 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 4723 | if (!orig_ref) |
| 4724 | return -ENOENT; |
| 4725 | |
| 4726 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 4727 | if (!ref) { |
| 4728 | btrfs_free_leaf_ref(root, orig_ref); |
| 4729 | return -ENOMEM; |
| 4730 | } |
| 4731 | |
| 4732 | ref->nritems = orig_ref->nritems; |
| 4733 | memcpy(ref->extents, orig_ref->extents, |
| 4734 | sizeof(ref->extents[0]) * ref->nritems); |
| 4735 | |
| 4736 | btrfs_free_leaf_ref(root, orig_ref); |
| 4737 | |
| 4738 | ref->root_gen = trans->transid; |
| 4739 | ref->bytenr = buf->start; |
| 4740 | ref->owner = btrfs_header_owner(buf); |
| 4741 | ref->generation = btrfs_header_generation(buf); |
| 4742 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 4743 | WARN_ON(ret); |
| 4744 | btrfs_free_leaf_ref(root, ref); |
| 4745 | } |
| 4746 | return 0; |
| 4747 | } |
| 4748 | |
| 4749 | static int noinline invalidate_extent_cache(struct btrfs_root *root, |
| 4750 | struct extent_buffer *leaf, |
| 4751 | struct btrfs_block_group_cache *group, |
| 4752 | struct btrfs_root *target_root) |
| 4753 | { |
| 4754 | struct btrfs_key key; |
| 4755 | struct inode *inode = NULL; |
| 4756 | struct btrfs_file_extent_item *fi; |
| 4757 | u64 num_bytes; |
| 4758 | u64 skip_objectid = 0; |
| 4759 | u32 nritems; |
| 4760 | u32 i; |
| 4761 | |
| 4762 | nritems = btrfs_header_nritems(leaf); |
| 4763 | for (i = 0; i < nritems; i++) { |
| 4764 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4765 | if (key.objectid == skip_objectid || |
| 4766 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 4767 | continue; |
| 4768 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4769 | if (btrfs_file_extent_type(leaf, fi) == |
| 4770 | BTRFS_FILE_EXTENT_INLINE) |
| 4771 | continue; |
| 4772 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4773 | continue; |
| 4774 | if (!inode || inode->i_ino != key.objectid) { |
| 4775 | iput(inode); |
| 4776 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 4777 | key.objectid, target_root, 1); |
| 4778 | } |
| 4779 | if (!inode) { |
| 4780 | skip_objectid = key.objectid; |
| 4781 | continue; |
| 4782 | } |
| 4783 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4784 | |
| 4785 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4786 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4787 | btrfs_drop_extent_cache(inode, key.offset, |
| 4788 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4789 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4790 | key.offset + num_bytes - 1, GFP_NOFS); |
| 4791 | cond_resched(); |
| 4792 | } |
| 4793 | iput(inode); |
| 4794 | return 0; |
| 4795 | } |
| 4796 | |
| 4797 | static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
| 4798 | struct btrfs_root *root, |
| 4799 | struct extent_buffer *leaf, |
| 4800 | struct btrfs_block_group_cache *group, |
| 4801 | struct inode *reloc_inode) |
| 4802 | { |
| 4803 | struct btrfs_key key; |
| 4804 | struct btrfs_key extent_key; |
| 4805 | struct btrfs_file_extent_item *fi; |
| 4806 | struct btrfs_leaf_ref *ref; |
| 4807 | struct disk_extent *new_extent; |
| 4808 | u64 bytenr; |
| 4809 | u64 num_bytes; |
| 4810 | u32 nritems; |
| 4811 | u32 i; |
| 4812 | int ext_index; |
| 4813 | int nr_extent; |
| 4814 | int ret; |
| 4815 | |
| 4816 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 4817 | BUG_ON(!new_extent); |
| 4818 | |
| 4819 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 4820 | BUG_ON(!ref); |
| 4821 | |
| 4822 | ext_index = -1; |
| 4823 | nritems = btrfs_header_nritems(leaf); |
| 4824 | for (i = 0; i < nritems; i++) { |
| 4825 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4826 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 4827 | continue; |
| 4828 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4829 | if (btrfs_file_extent_type(leaf, fi) == |
| 4830 | BTRFS_FILE_EXTENT_INLINE) |
| 4831 | continue; |
| 4832 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4833 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4834 | if (bytenr == 0) |
| 4835 | continue; |
| 4836 | |
| 4837 | ext_index++; |
| 4838 | if (bytenr >= group->key.objectid + group->key.offset || |
| 4839 | bytenr + num_bytes <= group->key.objectid) |
| 4840 | continue; |
| 4841 | |
| 4842 | extent_key.objectid = bytenr; |
| 4843 | extent_key.offset = num_bytes; |
| 4844 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4845 | nr_extent = 1; |
| 4846 | ret = get_new_locations(reloc_inode, &extent_key, |
| 4847 | group->key.objectid, 1, |
| 4848 | &new_extent, &nr_extent); |
| 4849 | if (ret > 0) |
| 4850 | continue; |
| 4851 | BUG_ON(ret < 0); |
| 4852 | |
| 4853 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 4854 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 4855 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 4856 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 4857 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4858 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4859 | new_extent->disk_bytenr); |
| 4860 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4861 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4862 | btrfs_mark_buffer_dirty(leaf); |
| 4863 | |
| 4864 | ret = btrfs_inc_extent_ref(trans, root, |
| 4865 | new_extent->disk_bytenr, |
| 4866 | new_extent->disk_num_bytes, |
| 4867 | leaf->start, |
| 4868 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4869 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4870 | BUG_ON(ret); |
| 4871 | ret = btrfs_free_extent(trans, root, |
| 4872 | bytenr, num_bytes, leaf->start, |
| 4873 | btrfs_header_owner(leaf), |
| 4874 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4875 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4876 | BUG_ON(ret); |
| 4877 | cond_resched(); |
| 4878 | } |
| 4879 | kfree(new_extent); |
| 4880 | BUG_ON(ext_index + 1 != ref->nritems); |
| 4881 | btrfs_free_leaf_ref(root, ref); |
| 4882 | return 0; |
| 4883 | } |
| 4884 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4885 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 4886 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4887 | { |
| 4888 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4889 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4890 | |
| 4891 | if (root->reloc_root) { |
| 4892 | reloc_root = root->reloc_root; |
| 4893 | root->reloc_root = NULL; |
| 4894 | list_add(&reloc_root->dead_list, |
| 4895 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4896 | |
| 4897 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 4898 | reloc_root->node->start); |
| 4899 | btrfs_set_root_level(&root->root_item, |
| 4900 | btrfs_header_level(reloc_root->node)); |
| 4901 | memset(&reloc_root->root_item.drop_progress, 0, |
| 4902 | sizeof(struct btrfs_disk_key)); |
| 4903 | reloc_root->root_item.drop_level = 0; |
| 4904 | |
| 4905 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 4906 | &reloc_root->root_key, |
| 4907 | &reloc_root->root_item); |
| 4908 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4909 | } |
| 4910 | return 0; |
| 4911 | } |
| 4912 | |
| 4913 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 4914 | { |
| 4915 | struct btrfs_trans_handle *trans; |
| 4916 | struct btrfs_root *reloc_root; |
| 4917 | struct btrfs_root *prev_root = NULL; |
| 4918 | struct list_head dead_roots; |
| 4919 | int ret; |
| 4920 | unsigned long nr; |
| 4921 | |
| 4922 | INIT_LIST_HEAD(&dead_roots); |
| 4923 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 4924 | |
| 4925 | while (!list_empty(&dead_roots)) { |
| 4926 | reloc_root = list_entry(dead_roots.prev, |
| 4927 | struct btrfs_root, dead_list); |
| 4928 | list_del_init(&reloc_root->dead_list); |
| 4929 | |
| 4930 | BUG_ON(reloc_root->commit_root != NULL); |
| 4931 | while (1) { |
| 4932 | trans = btrfs_join_transaction(root, 1); |
| 4933 | BUG_ON(!trans); |
| 4934 | |
| 4935 | mutex_lock(&root->fs_info->drop_mutex); |
| 4936 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 4937 | if (ret != -EAGAIN) |
| 4938 | break; |
| 4939 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4940 | |
| 4941 | nr = trans->blocks_used; |
| 4942 | ret = btrfs_end_transaction(trans, root); |
| 4943 | BUG_ON(ret); |
| 4944 | btrfs_btree_balance_dirty(root, nr); |
| 4945 | } |
| 4946 | |
| 4947 | free_extent_buffer(reloc_root->node); |
| 4948 | |
| 4949 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 4950 | &reloc_root->root_key); |
| 4951 | BUG_ON(ret); |
| 4952 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4953 | |
| 4954 | nr = trans->blocks_used; |
| 4955 | ret = btrfs_end_transaction(trans, root); |
| 4956 | BUG_ON(ret); |
| 4957 | btrfs_btree_balance_dirty(root, nr); |
| 4958 | |
| 4959 | kfree(prev_root); |
| 4960 | prev_root = reloc_root; |
| 4961 | } |
| 4962 | if (prev_root) { |
| 4963 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 4964 | kfree(prev_root); |
| 4965 | } |
| 4966 | return 0; |
| 4967 | } |
| 4968 | |
| 4969 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 4970 | { |
| 4971 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 4972 | return 0; |
| 4973 | } |
| 4974 | |
| 4975 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 4976 | { |
| 4977 | struct btrfs_root *reloc_root; |
| 4978 | struct btrfs_trans_handle *trans; |
| 4979 | struct btrfs_key location; |
| 4980 | int found; |
| 4981 | int ret; |
| 4982 | |
| 4983 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 4984 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 4985 | BUG_ON(ret); |
| 4986 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 4987 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 4988 | |
| 4989 | if (found) { |
| 4990 | trans = btrfs_start_transaction(root, 1); |
| 4991 | BUG_ON(!trans); |
| 4992 | ret = btrfs_commit_transaction(trans, root); |
| 4993 | BUG_ON(ret); |
| 4994 | } |
| 4995 | |
| 4996 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 4997 | location.offset = (u64)-1; |
| 4998 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 4999 | |
| 5000 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 5001 | BUG_ON(!reloc_root); |
| 5002 | btrfs_orphan_cleanup(reloc_root); |
| 5003 | return 0; |
| 5004 | } |
| 5005 | |
| 5006 | static int noinline init_reloc_tree(struct btrfs_trans_handle *trans, |
| 5007 | struct btrfs_root *root) |
| 5008 | { |
| 5009 | struct btrfs_root *reloc_root; |
| 5010 | struct extent_buffer *eb; |
| 5011 | struct btrfs_root_item *root_item; |
| 5012 | struct btrfs_key root_key; |
| 5013 | int ret; |
| 5014 | |
| 5015 | BUG_ON(!root->ref_cows); |
| 5016 | if (root->reloc_root) |
| 5017 | return 0; |
| 5018 | |
| 5019 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 5020 | BUG_ON(!root_item); |
| 5021 | |
| 5022 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 5023 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 5024 | BUG_ON(ret); |
| 5025 | |
| 5026 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 5027 | root_key.offset = root->root_key.objectid; |
| 5028 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5029 | |
| 5030 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 5031 | btrfs_set_root_refs(root_item, 0); |
| 5032 | btrfs_set_root_bytenr(root_item, eb->start); |
| 5033 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5034 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5035 | |
| 5036 | btrfs_tree_unlock(eb); |
| 5037 | free_extent_buffer(eb); |
| 5038 | |
| 5039 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 5040 | &root_key, root_item); |
| 5041 | BUG_ON(ret); |
| 5042 | kfree(root_item); |
| 5043 | |
| 5044 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 5045 | &root_key); |
| 5046 | BUG_ON(!reloc_root); |
| 5047 | reloc_root->last_trans = trans->transid; |
| 5048 | reloc_root->commit_root = NULL; |
| 5049 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 5050 | |
| 5051 | root->reloc_root = reloc_root; |
| 5052 | return 0; |
| 5053 | } |
| 5054 | |
| 5055 | /* |
| 5056 | * Core function of space balance. |
| 5057 | * |
| 5058 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5059 | * counted roots. There is one reloc tree for each subvol, and all |
| 5060 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 5061 | * of the latest committed roots of subvols (root->commit_root). |
| 5062 | * |
| 5063 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 5064 | * COW the block through subvol's reloc tree, then update block pointer |
| 5065 | * in the subvol to point to the new block. Since all reloc trees share |
| 5066 | * same root key objectid, doing special handing for tree blocks owned |
| 5067 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 5068 | * we can use the resulting new block directly when the same block is |
| 5069 | * required to COW again through other reloc trees. By this way, relocated |
| 5070 | * tree blocks are shared between reloc trees, so they are also shared |
| 5071 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5072 | */ |
| 5073 | static int noinline relocate_one_path(struct btrfs_trans_handle *trans, |
| 5074 | struct btrfs_root *root, |
| 5075 | struct btrfs_path *path, |
| 5076 | struct btrfs_key *first_key, |
| 5077 | struct btrfs_ref_path *ref_path, |
| 5078 | struct btrfs_block_group_cache *group, |
| 5079 | struct inode *reloc_inode) |
| 5080 | { |
| 5081 | struct btrfs_root *reloc_root; |
| 5082 | struct extent_buffer *eb = NULL; |
| 5083 | struct btrfs_key *keys; |
| 5084 | u64 *nodes; |
| 5085 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5086 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5087 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5088 | int ret; |
| 5089 | |
| 5090 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 5091 | lowest_level = ref_path->owner_objectid; |
| 5092 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5093 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5094 | path->lowest_level = lowest_level; |
| 5095 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 5096 | BUG_ON(ret < 0); |
| 5097 | path->lowest_level = 0; |
| 5098 | btrfs_release_path(root, path); |
| 5099 | return 0; |
| 5100 | } |
| 5101 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5102 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5103 | ret = init_reloc_tree(trans, root); |
| 5104 | BUG_ON(ret); |
| 5105 | reloc_root = root->reloc_root; |
| 5106 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5107 | shared_level = ref_path->shared_level; |
| 5108 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5109 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5110 | keys = ref_path->node_keys; |
| 5111 | nodes = ref_path->new_nodes; |
| 5112 | memset(&keys[shared_level + 1], 0, |
| 5113 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 5114 | memset(&nodes[shared_level + 1], 0, |
| 5115 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5116 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5117 | if (nodes[lowest_level] == 0) { |
| 5118 | path->lowest_level = lowest_level; |
| 5119 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5120 | 0, 1); |
| 5121 | BUG_ON(ret); |
| 5122 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 5123 | eb = path->nodes[level]; |
| 5124 | if (!eb || eb == reloc_root->node) |
| 5125 | break; |
| 5126 | nodes[level] = eb->start; |
| 5127 | if (level == 0) |
| 5128 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 5129 | else |
| 5130 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 5131 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5132 | if (nodes[0] && |
| 5133 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5134 | eb = path->nodes[0]; |
| 5135 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 5136 | group, reloc_inode); |
| 5137 | BUG_ON(ret); |
| 5138 | } |
| 5139 | btrfs_release_path(reloc_root, path); |
| 5140 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5141 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5142 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5143 | BUG_ON(ret); |
| 5144 | } |
| 5145 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5146 | /* |
| 5147 | * replace tree blocks in the fs tree with tree blocks in |
| 5148 | * the reloc tree. |
| 5149 | */ |
| 5150 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 5151 | BUG_ON(ret < 0); |
| 5152 | |
| 5153 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5154 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5155 | 0, 0); |
| 5156 | BUG_ON(ret); |
| 5157 | extent_buffer_get(path->nodes[0]); |
| 5158 | eb = path->nodes[0]; |
| 5159 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5160 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 5161 | BUG_ON(ret); |
| 5162 | free_extent_buffer(eb); |
| 5163 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5164 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5165 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5166 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5167 | return 0; |
| 5168 | } |
| 5169 | |
| 5170 | static int noinline relocate_tree_block(struct btrfs_trans_handle *trans, |
| 5171 | struct btrfs_root *root, |
| 5172 | struct btrfs_path *path, |
| 5173 | struct btrfs_key *first_key, |
| 5174 | struct btrfs_ref_path *ref_path) |
| 5175 | { |
| 5176 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5177 | |
| 5178 | ret = relocate_one_path(trans, root, path, first_key, |
| 5179 | ref_path, NULL, NULL); |
| 5180 | BUG_ON(ret); |
| 5181 | |
| 5182 | if (root == root->fs_info->extent_root) |
| 5183 | btrfs_extent_post_op(trans, root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5184 | |
| 5185 | return 0; |
| 5186 | } |
| 5187 | |
| 5188 | static int noinline del_extent_zero(struct btrfs_trans_handle *trans, |
| 5189 | struct btrfs_root *extent_root, |
| 5190 | struct btrfs_path *path, |
| 5191 | struct btrfs_key *extent_key) |
| 5192 | { |
| 5193 | int ret; |
| 5194 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5195 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 5196 | if (ret) |
| 5197 | goto out; |
| 5198 | ret = btrfs_del_item(trans, extent_root, path); |
| 5199 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5200 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5201 | return ret; |
| 5202 | } |
| 5203 | |
| 5204 | static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info, |
| 5205 | struct btrfs_ref_path *ref_path) |
| 5206 | { |
| 5207 | struct btrfs_key root_key; |
| 5208 | |
| 5209 | root_key.objectid = ref_path->root_objectid; |
| 5210 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5211 | if (is_cowonly_root(ref_path->root_objectid)) |
| 5212 | root_key.offset = 0; |
| 5213 | else |
| 5214 | root_key.offset = (u64)-1; |
| 5215 | |
| 5216 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5217 | } |
| 5218 | |
| 5219 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
| 5220 | struct btrfs_path *path, |
| 5221 | struct btrfs_key *extent_key, |
| 5222 | struct btrfs_block_group_cache *group, |
| 5223 | struct inode *reloc_inode, int pass) |
| 5224 | { |
| 5225 | struct btrfs_trans_handle *trans; |
| 5226 | struct btrfs_root *found_root; |
| 5227 | struct btrfs_ref_path *ref_path = NULL; |
| 5228 | struct disk_extent *new_extents = NULL; |
| 5229 | int nr_extents = 0; |
| 5230 | int loops; |
| 5231 | int ret; |
| 5232 | int level; |
| 5233 | struct btrfs_key first_key; |
| 5234 | u64 prev_block = 0; |
| 5235 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5236 | |
| 5237 | trans = btrfs_start_transaction(extent_root, 1); |
| 5238 | BUG_ON(!trans); |
| 5239 | |
| 5240 | if (extent_key->objectid == 0) { |
| 5241 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 5242 | goto out; |
| 5243 | } |
| 5244 | |
| 5245 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 5246 | if (!ref_path) { |
| 5247 | ret = -ENOMEM; |
| 5248 | goto out; |
| 5249 | } |
| 5250 | |
| 5251 | for (loops = 0; ; loops++) { |
| 5252 | if (loops == 0) { |
| 5253 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 5254 | extent_key->objectid); |
| 5255 | } else { |
| 5256 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 5257 | } |
| 5258 | if (ret < 0) |
| 5259 | goto out; |
| 5260 | if (ret > 0) |
| 5261 | break; |
| 5262 | |
| 5263 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5264 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 5265 | continue; |
| 5266 | |
| 5267 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 5268 | BUG_ON(!found_root); |
| 5269 | /* |
| 5270 | * for reference counted tree, only process reference paths |
| 5271 | * rooted at the latest committed root. |
| 5272 | */ |
| 5273 | if (found_root->ref_cows && |
| 5274 | ref_path->root_generation != found_root->root_key.offset) |
| 5275 | continue; |
| 5276 | |
| 5277 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5278 | if (pass == 0) { |
| 5279 | /* |
| 5280 | * copy data extents to new locations |
| 5281 | */ |
| 5282 | u64 group_start = group->key.objectid; |
| 5283 | ret = relocate_data_extent(reloc_inode, |
| 5284 | extent_key, |
| 5285 | group_start); |
| 5286 | if (ret < 0) |
| 5287 | goto out; |
| 5288 | break; |
| 5289 | } |
| 5290 | level = 0; |
| 5291 | } else { |
| 5292 | level = ref_path->owner_objectid; |
| 5293 | } |
| 5294 | |
| 5295 | if (prev_block != ref_path->nodes[level]) { |
| 5296 | struct extent_buffer *eb; |
| 5297 | u64 block_start = ref_path->nodes[level]; |
| 5298 | u64 block_size = btrfs_level_size(found_root, level); |
| 5299 | |
| 5300 | eb = read_tree_block(found_root, block_start, |
| 5301 | block_size, 0); |
| 5302 | btrfs_tree_lock(eb); |
| 5303 | BUG_ON(level != btrfs_header_level(eb)); |
| 5304 | |
| 5305 | if (level == 0) |
| 5306 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 5307 | else |
| 5308 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 5309 | |
| 5310 | btrfs_tree_unlock(eb); |
| 5311 | free_extent_buffer(eb); |
| 5312 | prev_block = block_start; |
| 5313 | } |
| 5314 | |
| 5315 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID && |
| 5316 | pass >= 2) { |
| 5317 | /* |
| 5318 | * use fallback method to process the remaining |
| 5319 | * references. |
| 5320 | */ |
| 5321 | if (!new_extents) { |
| 5322 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5323 | new_extents = kmalloc(sizeof(*new_extents), |
| 5324 | GFP_NOFS); |
| 5325 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5326 | ret = get_new_locations(reloc_inode, |
| 5327 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5328 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5329 | &new_extents, |
| 5330 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5331 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5332 | goto out; |
| 5333 | } |
| 5334 | btrfs_record_root_in_trans(found_root); |
| 5335 | ret = replace_one_extent(trans, found_root, |
| 5336 | path, extent_key, |
| 5337 | &first_key, ref_path, |
| 5338 | new_extents, nr_extents); |
| 5339 | if (ret < 0) |
| 5340 | goto out; |
| 5341 | continue; |
| 5342 | } |
| 5343 | |
| 5344 | btrfs_record_root_in_trans(found_root); |
| 5345 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5346 | ret = relocate_tree_block(trans, found_root, path, |
| 5347 | &first_key, ref_path); |
| 5348 | } else { |
| 5349 | /* |
| 5350 | * try to update data extent references while |
| 5351 | * keeping metadata shared between snapshots. |
| 5352 | */ |
| 5353 | ret = relocate_one_path(trans, found_root, path, |
| 5354 | &first_key, ref_path, |
| 5355 | group, reloc_inode); |
| 5356 | } |
| 5357 | if (ret < 0) |
| 5358 | goto out; |
| 5359 | } |
| 5360 | ret = 0; |
| 5361 | out: |
| 5362 | btrfs_end_transaction(trans, extent_root); |
| 5363 | kfree(new_extents); |
| 5364 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5365 | return ret; |
| 5366 | } |
| 5367 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5368 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 5369 | { |
| 5370 | u64 num_devices; |
| 5371 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 5372 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 5373 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5374 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5375 | if (num_devices == 1) { |
| 5376 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5377 | stripped = flags & ~stripped; |
| 5378 | |
| 5379 | /* turn raid0 into single device chunks */ |
| 5380 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 5381 | return stripped; |
| 5382 | |
| 5383 | /* turn mirroring into duplication */ |
| 5384 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5385 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5386 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 5387 | return flags; |
| 5388 | } else { |
| 5389 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5390 | if (flags & stripped) |
| 5391 | return flags; |
| 5392 | |
| 5393 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5394 | stripped = flags & ~stripped; |
| 5395 | |
| 5396 | /* switch duplicated blocks with raid1 */ |
| 5397 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 5398 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 5399 | |
| 5400 | /* turn single device chunks into raid0 */ |
| 5401 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 5402 | } |
| 5403 | return flags; |
| 5404 | } |
| 5405 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5406 | int __alloc_chunk_for_shrink(struct btrfs_root *root, |
| 5407 | struct btrfs_block_group_cache *shrink_block_group, |
| 5408 | int force) |
| 5409 | { |
| 5410 | struct btrfs_trans_handle *trans; |
| 5411 | u64 new_alloc_flags; |
| 5412 | u64 calc; |
| 5413 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5414 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5415 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5416 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5417 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5418 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5419 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5420 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5421 | new_alloc_flags = update_block_group_flags(root, |
| 5422 | shrink_block_group->flags); |
| 5423 | if (new_alloc_flags != shrink_block_group->flags) { |
| 5424 | calc = |
| 5425 | btrfs_block_group_used(&shrink_block_group->item); |
| 5426 | } else { |
| 5427 | calc = shrink_block_group->key.offset; |
| 5428 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5429 | spin_unlock(&shrink_block_group->lock); |
| 5430 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5431 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5432 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5433 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5434 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5435 | } else |
| 5436 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5437 | return 0; |
| 5438 | } |
| 5439 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5440 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 5441 | struct btrfs_root *root, |
| 5442 | u64 objectid, u64 size) |
| 5443 | { |
| 5444 | struct btrfs_path *path; |
| 5445 | struct btrfs_inode_item *item; |
| 5446 | struct extent_buffer *leaf; |
| 5447 | int ret; |
| 5448 | |
| 5449 | path = btrfs_alloc_path(); |
| 5450 | if (!path) |
| 5451 | return -ENOMEM; |
| 5452 | |
| 5453 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 5454 | if (ret) |
| 5455 | goto out; |
| 5456 | |
| 5457 | leaf = path->nodes[0]; |
| 5458 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 5459 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 5460 | btrfs_set_inode_generation(leaf, item, 1); |
| 5461 | btrfs_set_inode_size(leaf, item, size); |
| 5462 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5463 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM | |
| 5464 | BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5465 | btrfs_mark_buffer_dirty(leaf); |
| 5466 | btrfs_release_path(root, path); |
| 5467 | out: |
| 5468 | btrfs_free_path(path); |
| 5469 | return ret; |
| 5470 | } |
| 5471 | |
| 5472 | static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info, |
| 5473 | struct btrfs_block_group_cache *group) |
| 5474 | { |
| 5475 | struct inode *inode = NULL; |
| 5476 | struct btrfs_trans_handle *trans; |
| 5477 | struct btrfs_root *root; |
| 5478 | struct btrfs_key root_key; |
| 5479 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 5480 | int err = 0; |
| 5481 | |
| 5482 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5483 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5484 | root_key.offset = (u64)-1; |
| 5485 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5486 | if (IS_ERR(root)) |
| 5487 | return ERR_CAST(root); |
| 5488 | |
| 5489 | trans = btrfs_start_transaction(root, 1); |
| 5490 | BUG_ON(!trans); |
| 5491 | |
| 5492 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 5493 | if (err) |
| 5494 | goto out; |
| 5495 | |
| 5496 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 5497 | BUG_ON(err); |
| 5498 | |
| 5499 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5500 | group->key.offset, 0, group->key.offset, |
| 5501 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5502 | BUG_ON(err); |
| 5503 | |
| 5504 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 5505 | if (inode->i_state & I_NEW) { |
| 5506 | BTRFS_I(inode)->root = root; |
| 5507 | BTRFS_I(inode)->location.objectid = objectid; |
| 5508 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5509 | BTRFS_I(inode)->location.offset = 0; |
| 5510 | btrfs_read_locked_inode(inode); |
| 5511 | unlock_new_inode(inode); |
| 5512 | BUG_ON(is_bad_inode(inode)); |
| 5513 | } else { |
| 5514 | BUG_ON(1); |
| 5515 | } |
| 5516 | |
| 5517 | err = btrfs_orphan_add(trans, inode); |
| 5518 | out: |
| 5519 | btrfs_end_transaction(trans, root); |
| 5520 | if (err) { |
| 5521 | if (inode) |
| 5522 | iput(inode); |
| 5523 | inode = ERR_PTR(err); |
| 5524 | } |
| 5525 | return inode; |
| 5526 | } |
| 5527 | |
| 5528 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5529 | { |
| 5530 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5531 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5532 | struct btrfs_fs_info *info = root->fs_info; |
| 5533 | struct extent_buffer *leaf; |
| 5534 | struct inode *reloc_inode; |
| 5535 | struct btrfs_block_group_cache *block_group; |
| 5536 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5537 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5538 | u64 cur_byte; |
| 5539 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5540 | u32 nritems; |
| 5541 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5542 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5543 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5544 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5545 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5546 | |
| 5547 | block_group = btrfs_lookup_block_group(info, group_start); |
| 5548 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5549 | |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5550 | printk("btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5551 | (unsigned long long)block_group->key.objectid, |
| 5552 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5553 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5554 | path = btrfs_alloc_path(); |
| 5555 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5556 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5557 | reloc_inode = create_reloc_inode(info, block_group); |
| 5558 | BUG_ON(IS_ERR(reloc_inode)); |
| 5559 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5560 | __alloc_chunk_for_shrink(root, block_group, 1); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5561 | set_block_group_readonly(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5562 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5563 | btrfs_start_delalloc_inodes(info->tree_root); |
| 5564 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5565 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5566 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5567 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5568 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5569 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5570 | key.offset = 0; |
| 5571 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5572 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5573 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5574 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5575 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5576 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5577 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 5578 | btrfs_clean_old_snapshots(info->tree_root); |
| 5579 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 5580 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5581 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5582 | while(1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5583 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5584 | if (ret < 0) |
| 5585 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5586 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5587 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5588 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5589 | if (path->slots[0] >= nritems) { |
| 5590 | ret = btrfs_next_leaf(root, path); |
| 5591 | if (ret < 0) |
| 5592 | goto out; |
| 5593 | if (ret == 1) { |
| 5594 | ret = 0; |
| 5595 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5596 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5597 | leaf = path->nodes[0]; |
| 5598 | nritems = btrfs_header_nritems(leaf); |
| 5599 | } |
| 5600 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5601 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5602 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5603 | if (key.objectid >= block_group->key.objectid + |
| 5604 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5605 | break; |
| 5606 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5607 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5608 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5609 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5610 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5611 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5612 | } |
| 5613 | progress = 1; |
| 5614 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5615 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 5616 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5617 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5618 | goto next; |
| 5619 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5620 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5621 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5622 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5623 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5624 | |
| 5625 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 5626 | ret = relocate_one_extent(root, path, &key, block_group, |
| 5627 | reloc_inode, pass); |
| 5628 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5629 | if (ret > 0) |
| 5630 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5631 | |
| 5632 | key.objectid = cur_byte; |
| 5633 | key.type = 0; |
| 5634 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5635 | } |
| 5636 | |
| 5637 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5638 | |
| 5639 | if (pass == 0) { |
| 5640 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 5641 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
| 5642 | WARN_ON(reloc_inode->i_mapping->nrpages); |
| 5643 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5644 | |
| 5645 | if (total_found > 0) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5646 | printk("btrfs found %llu extents in pass %d\n", |
| 5647 | (unsigned long long)total_found, pass); |
| 5648 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5649 | if (total_found == skipped && pass > 2) { |
| 5650 | iput(reloc_inode); |
| 5651 | reloc_inode = create_reloc_inode(info, block_group); |
| 5652 | pass = 0; |
| 5653 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5654 | goto again; |
| 5655 | } |
| 5656 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5657 | /* delete reloc_inode */ |
| 5658 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5659 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5660 | /* unpin extents in this range */ |
| 5661 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5662 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5663 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5664 | spin_lock(&block_group->lock); |
| 5665 | WARN_ON(block_group->pinned > 0); |
| 5666 | WARN_ON(block_group->reserved > 0); |
| 5667 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 5668 | spin_unlock(&block_group->lock); |
| 5669 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5670 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5671 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5672 | return ret; |
| 5673 | } |
| 5674 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5675 | int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, |
| 5676 | struct btrfs_key *key) |
| 5677 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5678 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5679 | struct btrfs_key found_key; |
| 5680 | struct extent_buffer *leaf; |
| 5681 | int slot; |
| 5682 | |
| 5683 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 5684 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5685 | goto out; |
| 5686 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5687 | while(1) { |
| 5688 | slot = path->slots[0]; |
| 5689 | leaf = path->nodes[0]; |
| 5690 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5691 | ret = btrfs_next_leaf(root, path); |
| 5692 | if (ret == 0) |
| 5693 | continue; |
| 5694 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5695 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5696 | break; |
| 5697 | } |
| 5698 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5699 | |
| 5700 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5701 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 5702 | ret = 0; |
| 5703 | goto out; |
| 5704 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5705 | path->slots[0]++; |
| 5706 | } |
| 5707 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5708 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5709 | return ret; |
| 5710 | } |
| 5711 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5712 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 5713 | { |
| 5714 | struct btrfs_block_group_cache *block_group; |
| 5715 | struct rb_node *n; |
| 5716 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5717 | spin_lock(&info->block_group_cache_lock); |
| 5718 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 5719 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 5720 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5721 | rb_erase(&block_group->cache_node, |
| 5722 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5723 | spin_unlock(&info->block_group_cache_lock); |
| 5724 | |
| 5725 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5726 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5727 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5728 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5729 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5730 | |
| 5731 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5732 | } |
| 5733 | spin_unlock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5734 | return 0; |
| 5735 | } |
| 5736 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5737 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 5738 | { |
| 5739 | struct btrfs_path *path; |
| 5740 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5741 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5742 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5743 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5744 | struct btrfs_key key; |
| 5745 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5746 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5747 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5748 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5749 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5750 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5751 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5752 | path = btrfs_alloc_path(); |
| 5753 | if (!path) |
| 5754 | return -ENOMEM; |
| 5755 | |
| 5756 | while(1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5757 | ret = find_first_block_group(root, path, &key); |
| 5758 | if (ret > 0) { |
| 5759 | ret = 0; |
| 5760 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5761 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5762 | if (ret != 0) |
| 5763 | goto error; |
| 5764 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5765 | leaf = path->nodes[0]; |
| 5766 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5767 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5768 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5769 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5770 | break; |
| 5771 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5772 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5773 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5774 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5775 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5776 | read_extent_buffer(leaf, &cache->item, |
| 5777 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 5778 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5779 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5780 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5781 | key.objectid = found_key.objectid + found_key.offset; |
| 5782 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5783 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5784 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5785 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 5786 | btrfs_block_group_used(&cache->item), |
| 5787 | &space_info); |
| 5788 | BUG_ON(ret); |
| 5789 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5790 | down_write(&space_info->groups_sem); |
| 5791 | list_add_tail(&cache->list, &space_info->block_groups); |
| 5792 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5793 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5794 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5795 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 5796 | |
| 5797 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5798 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 5799 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5800 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5801 | ret = 0; |
| 5802 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5803 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5804 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5805 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5806 | |
| 5807 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 5808 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5809 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5810 | u64 size) |
| 5811 | { |
| 5812 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5813 | struct btrfs_root *extent_root; |
| 5814 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5815 | |
| 5816 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5817 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5818 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
| 5819 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5820 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5821 | if (!cache) |
| 5822 | return -ENOMEM; |
| 5823 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5824 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5825 | cache->key.offset = size; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5826 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5827 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5828 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5829 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5830 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5831 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5832 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 5833 | cache->flags = type; |
| 5834 | btrfs_set_block_group_flags(&cache->item, type); |
| 5835 | |
| 5836 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 5837 | &cache->space_info); |
| 5838 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5839 | down_write(&cache->space_info->groups_sem); |
| 5840 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 5841 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5842 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5843 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5844 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5845 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5846 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 5847 | sizeof(cache->item)); |
| 5848 | BUG_ON(ret); |
| 5849 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 5850 | finish_current_insert(trans, extent_root, 0); |
| 5851 | ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5852 | BUG_ON(ret); |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 5853 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5854 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5855 | return 0; |
| 5856 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5857 | |
| 5858 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 5859 | struct btrfs_root *root, u64 group_start) |
| 5860 | { |
| 5861 | struct btrfs_path *path; |
| 5862 | struct btrfs_block_group_cache *block_group; |
| 5863 | struct btrfs_key key; |
| 5864 | int ret; |
| 5865 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5866 | root = root->fs_info->extent_root; |
| 5867 | |
| 5868 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 5869 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5870 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5871 | |
| 5872 | memcpy(&key, &block_group->key, sizeof(key)); |
| 5873 | |
| 5874 | path = btrfs_alloc_path(); |
| 5875 | BUG_ON(!path); |
| 5876 | |
| 5877 | btrfs_remove_free_space_cache(block_group); |
| 5878 | rb_erase(&block_group->cache_node, |
| 5879 | &root->fs_info->block_group_cache_tree); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5880 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5881 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5882 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5883 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5884 | spin_lock(&block_group->space_info->lock); |
| 5885 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 5886 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 5887 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5888 | block_group->space_info->full = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5889 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5890 | /* |
| 5891 | memset(shrink_block_group, 0, sizeof(*shrink_block_group)); |
| 5892 | kfree(shrink_block_group); |
| 5893 | */ |
| 5894 | |
| 5895 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 5896 | if (ret > 0) |
| 5897 | ret = -EIO; |
| 5898 | if (ret < 0) |
| 5899 | goto out; |
| 5900 | |
| 5901 | ret = btrfs_del_item(trans, root, path); |
| 5902 | out: |
| 5903 | btrfs_free_path(path); |
| 5904 | return ret; |
| 5905 | } |