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 | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 22 | #include <linux/version.h> |
| 23 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 24 | #include "hash.h" |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 25 | #include "crc32c.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 26 | #include "ctree.h" |
| 27 | #include "disk-io.h" |
| 28 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 29 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 30 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 31 | #include "locking.h" |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 32 | #include "ref-cache.h" |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 33 | #include "compat.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 34 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 35 | #define PENDING_EXTENT_INSERT 0 |
| 36 | #define PENDING_EXTENT_DELETE 1 |
| 37 | #define PENDING_BACKREF_UPDATE 2 |
| 38 | |
| 39 | struct pending_extent_op { |
| 40 | int type; |
| 41 | u64 bytenr; |
| 42 | u64 num_bytes; |
| 43 | u64 parent; |
| 44 | u64 orig_parent; |
| 45 | u64 generation; |
| 46 | u64 orig_generation; |
| 47 | int level; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 48 | struct list_head list; |
| 49 | int del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 50 | }; |
| 51 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 52 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 53 | btrfs_root *extent_root, int all); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 54 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 55 | btrfs_root *extent_root, int all); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 56 | static struct btrfs_block_group_cache * |
| 57 | __btrfs_find_block_group(struct btrfs_root *root, |
| 58 | struct btrfs_block_group_cache *hint, |
| 59 | u64 search_start, int data, int owner); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 60 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 61 | struct btrfs_root *root, |
| 62 | u64 bytenr, u64 num_bytes, int is_data); |
| 63 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 64 | struct btrfs_root *root, |
| 65 | u64 bytenr, u64 num_bytes, int alloc, |
| 66 | int mark_free); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 67 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 68 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 69 | { |
| 70 | return (cache->flags & bits) == bits; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * this adds the block group to the fs_info rb tree for the block group |
| 75 | * cache |
| 76 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 77 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 78 | struct btrfs_block_group_cache *block_group) |
| 79 | { |
| 80 | struct rb_node **p; |
| 81 | struct rb_node *parent = NULL; |
| 82 | struct btrfs_block_group_cache *cache; |
| 83 | |
| 84 | spin_lock(&info->block_group_cache_lock); |
| 85 | p = &info->block_group_cache_tree.rb_node; |
| 86 | |
| 87 | while (*p) { |
| 88 | parent = *p; |
| 89 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 90 | cache_node); |
| 91 | if (block_group->key.objectid < cache->key.objectid) { |
| 92 | p = &(*p)->rb_left; |
| 93 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 94 | p = &(*p)->rb_right; |
| 95 | } else { |
| 96 | spin_unlock(&info->block_group_cache_lock); |
| 97 | return -EEXIST; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | rb_link_node(&block_group->cache_node, parent, p); |
| 102 | rb_insert_color(&block_group->cache_node, |
| 103 | &info->block_group_cache_tree); |
| 104 | spin_unlock(&info->block_group_cache_lock); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * This will return the block group at or after bytenr if contains is 0, else |
| 111 | * it will return the block group that contains the bytenr |
| 112 | */ |
| 113 | static struct btrfs_block_group_cache * |
| 114 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 115 | int contains) |
| 116 | { |
| 117 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 118 | struct rb_node *n; |
| 119 | u64 end, start; |
| 120 | |
| 121 | spin_lock(&info->block_group_cache_lock); |
| 122 | n = info->block_group_cache_tree.rb_node; |
| 123 | |
| 124 | while (n) { |
| 125 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 126 | cache_node); |
| 127 | end = cache->key.objectid + cache->key.offset - 1; |
| 128 | start = cache->key.objectid; |
| 129 | |
| 130 | if (bytenr < start) { |
| 131 | if (!contains && (!ret || start < ret->key.objectid)) |
| 132 | ret = cache; |
| 133 | n = n->rb_left; |
| 134 | } else if (bytenr > start) { |
| 135 | if (contains && bytenr <= end) { |
| 136 | ret = cache; |
| 137 | break; |
| 138 | } |
| 139 | n = n->rb_right; |
| 140 | } else { |
| 141 | ret = cache; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | spin_unlock(&info->block_group_cache_lock); |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * this is only called by cache_block_group, since we could have freed extents |
| 152 | * we need to check the pinned_extents for any extents that can't be used yet |
| 153 | * since their free space will be released as soon as the transaction commits. |
| 154 | */ |
| 155 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 156 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 157 | { |
| 158 | u64 extent_start, extent_end, size; |
| 159 | int ret; |
| 160 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 161 | mutex_lock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 162 | while (start < end) { |
| 163 | ret = find_first_extent_bit(&info->pinned_extents, start, |
| 164 | &extent_start, &extent_end, |
| 165 | EXTENT_DIRTY); |
| 166 | if (ret) |
| 167 | break; |
| 168 | |
| 169 | if (extent_start == start) { |
| 170 | start = extent_end + 1; |
| 171 | } else if (extent_start > start && extent_start < end) { |
| 172 | size = extent_start - start; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 173 | ret = btrfs_add_free_space(block_group, start, |
| 174 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 175 | BUG_ON(ret); |
| 176 | start = extent_end + 1; |
| 177 | } else { |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (start < end) { |
| 183 | size = end - start; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 184 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 185 | BUG_ON(ret); |
| 186 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 187 | mutex_unlock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame^] | 192 | static int remove_sb_from_cache(struct btrfs_root *root, |
| 193 | struct btrfs_block_group_cache *cache) |
| 194 | { |
| 195 | u64 bytenr; |
| 196 | u64 *logical; |
| 197 | int stripe_len; |
| 198 | int i, nr, ret; |
| 199 | |
| 200 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 201 | bytenr = btrfs_sb_offset(i); |
| 202 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 203 | cache->key.objectid, bytenr, 0, |
| 204 | &logical, &nr, &stripe_len); |
| 205 | BUG_ON(ret); |
| 206 | while (nr--) { |
| 207 | btrfs_remove_free_space(cache, logical[nr], |
| 208 | stripe_len); |
| 209 | } |
| 210 | kfree(logical); |
| 211 | } |
| 212 | return 0; |
| 213 | } |
| 214 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 215 | static int cache_block_group(struct btrfs_root *root, |
| 216 | struct btrfs_block_group_cache *block_group) |
| 217 | { |
| 218 | struct btrfs_path *path; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 219 | int ret = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 220 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 221 | struct extent_buffer *leaf; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 222 | int slot; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame^] | 223 | u64 last = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 224 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 225 | if (!block_group) |
| 226 | return 0; |
| 227 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 228 | root = root->fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 229 | |
| 230 | if (block_group->cached) |
| 231 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 232 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 233 | path = btrfs_alloc_path(); |
| 234 | if (!path) |
| 235 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 236 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 237 | path->reada = 2; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 238 | /* |
| 239 | * we get into deadlocks with paths held by callers of this function. |
| 240 | * since the alloc_mutex is protecting things right now, just |
| 241 | * skip the locking here |
| 242 | */ |
| 243 | path->skip_locking = 1; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame^] | 244 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 245 | key.offset = 0; |
| 246 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 247 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 248 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 249 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame^] | 250 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 251 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 252 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 253 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 254 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 255 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 256 | if (ret < 0) |
| 257 | goto err; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 258 | if (ret == 0) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 259 | continue; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 260 | else |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 261 | break; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 262 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 263 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 264 | if (key.objectid < block_group->key.objectid) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 265 | goto next; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 266 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 267 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 268 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 269 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 270 | |
| 271 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 272 | add_new_free_space(block_group, root->fs_info, last, |
| 273 | key.objectid); |
| 274 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 275 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 276 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 277 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 278 | path->slots[0]++; |
| 279 | } |
| 280 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 281 | add_new_free_space(block_group, root->fs_info, last, |
| 282 | block_group->key.objectid + |
| 283 | block_group->key.offset); |
| 284 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame^] | 285 | remove_sb_from_cache(root, block_group); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 286 | block_group->cached = 1; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 287 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 288 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 289 | btrfs_free_path(path); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 290 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 293 | /* |
| 294 | * return the block group that starts at or after bytenr |
| 295 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 296 | static struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 297 | btrfs_fs_info *info, |
| 298 | u64 bytenr) |
| 299 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 300 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 301 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 302 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 303 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 304 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 307 | /* |
| 308 | * return the block group that contains teh given bytenr |
| 309 | */ |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 310 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
| 311 | btrfs_fs_info *info, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 312 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 313 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 314 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 315 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 316 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 317 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 318 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 319 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 320 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 321 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 322 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 323 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 324 | struct list_head *head = &info->space_info; |
| 325 | struct list_head *cur; |
| 326 | struct btrfs_space_info *found; |
| 327 | list_for_each(cur, head) { |
| 328 | found = list_entry(cur, struct btrfs_space_info, list); |
| 329 | if (found->flags == flags) |
| 330 | return found; |
| 331 | } |
| 332 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 335 | static u64 div_factor(u64 num, int factor) |
| 336 | { |
| 337 | if (factor == 10) |
| 338 | return num; |
| 339 | num *= factor; |
| 340 | do_div(num, 10); |
| 341 | return num; |
| 342 | } |
| 343 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 344 | static struct btrfs_block_group_cache * |
| 345 | __btrfs_find_block_group(struct btrfs_root *root, |
| 346 | struct btrfs_block_group_cache *hint, |
| 347 | u64 search_start, int data, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 348 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 349 | struct btrfs_block_group_cache *cache; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 350 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 351 | struct btrfs_fs_info *info = root->fs_info; |
| 352 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 353 | u64 last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 354 | u64 free_check; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 355 | int full_search = 0; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 356 | int factor = 10; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 357 | int wrapped = 0; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 358 | |
Chris Mason | a236aed | 2008-04-29 09:38:00 -0400 | [diff] [blame] | 359 | if (data & BTRFS_BLOCK_GROUP_METADATA) |
| 360 | factor = 9; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 361 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 362 | if (search_start) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 363 | struct btrfs_block_group_cache *shint; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 364 | shint = btrfs_lookup_first_block_group(info, search_start); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 365 | if (shint && block_group_bits(shint, data)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 366 | spin_lock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 367 | used = btrfs_block_group_used(&shint->item); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 368 | if (used + shint->pinned + shint->reserved < |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 369 | div_factor(shint->key.offset, factor)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 370 | spin_unlock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 371 | return shint; |
| 372 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 373 | spin_unlock(&shint->lock); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 374 | } |
| 375 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 376 | if (hint && block_group_bits(hint, data)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 377 | spin_lock(&hint->lock); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 378 | used = btrfs_block_group_used(&hint->item); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 379 | if (used + hint->pinned + hint->reserved < |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 380 | div_factor(hint->key.offset, factor)) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 381 | spin_unlock(&hint->lock); |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 382 | return hint; |
| 383 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 384 | spin_unlock(&hint->lock); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 385 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 386 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 387 | if (hint) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 388 | last = max(hint->key.objectid, search_start); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 389 | else |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 390 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 391 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 392 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 393 | while (1) { |
| 394 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 395 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 396 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 397 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 398 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 399 | last = cache->key.objectid + cache->key.offset; |
| 400 | used = btrfs_block_group_used(&cache->item); |
| 401 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 402 | if (block_group_bits(cache, data)) { |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 403 | free_check = div_factor(cache->key.offset, factor); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 404 | if (used + cache->pinned + cache->reserved < |
| 405 | free_check) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 406 | found_group = cache; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 407 | spin_unlock(&cache->lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 408 | goto found; |
| 409 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 410 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 411 | spin_unlock(&cache->lock); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 412 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 413 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 414 | if (!wrapped) { |
| 415 | last = search_start; |
| 416 | wrapped = 1; |
| 417 | goto again; |
| 418 | } |
| 419 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 420 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 421 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 422 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 423 | goto again; |
| 424 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 425 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 426 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 427 | } |
| 428 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 429 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 430 | struct btrfs_block_group_cache |
| 431 | *hint, u64 search_start, |
| 432 | int data, int owner) |
| 433 | { |
| 434 | |
| 435 | struct btrfs_block_group_cache *ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 436 | ret = __btrfs_find_block_group(root, hint, search_start, data, owner); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 437 | return ret; |
| 438 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 439 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 440 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 441 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 442 | { |
| 443 | int ret; |
| 444 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 445 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 446 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 447 | path = btrfs_alloc_path(); |
| 448 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 449 | key.objectid = start; |
| 450 | key.offset = len; |
| 451 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 452 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 453 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 454 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 455 | return ret; |
| 456 | } |
| 457 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 458 | /* |
| 459 | * Back reference rules. Back refs have three main goals: |
| 460 | * |
| 461 | * 1) differentiate between all holders of references to an extent so that |
| 462 | * when a reference is dropped we can make sure it was a valid reference |
| 463 | * before freeing the extent. |
| 464 | * |
| 465 | * 2) Provide enough information to quickly find the holders of an extent |
| 466 | * if we notice a given block is corrupted or bad. |
| 467 | * |
| 468 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 469 | * maintenance. This is actually the same as #2, but with a slightly |
| 470 | * different use case. |
| 471 | * |
| 472 | * File extents can be referenced by: |
| 473 | * |
| 474 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 475 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 476 | * - different offsets inside a file (bookend extents in file.c) |
| 477 | * |
| 478 | * The extent ref structure has fields for: |
| 479 | * |
| 480 | * - Objectid of the subvolume root |
| 481 | * - Generation number of the tree holding the reference |
| 482 | * - objectid of the file holding the reference |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 483 | * - number of references holding by parent node (alway 1 for tree blocks) |
| 484 | * |
| 485 | * Btree leaf may hold multiple references to a file extent. In most cases, |
| 486 | * these references are from same file and the corresponding offsets inside |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 487 | * the file are close together. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 488 | * |
| 489 | * When a file extent is allocated the fields are filled in: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 490 | * (root_key.objectid, trans->transid, inode objectid, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 491 | * |
| 492 | * 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] | 493 | * in the leaf. It looks similar to the create case, but trans->transid will |
| 494 | * be different when the block is cow'd. |
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 | * (root_key.objectid, trans->transid, inode objectid, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 497 | * number of references in the leaf) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 498 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 499 | * When a file extent is removed either during snapshot deletion or |
| 500 | * file truncation, we find the corresponding back reference and check |
| 501 | * the following fields: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 502 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 503 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 504 | * inode objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 505 | * |
| 506 | * Btree extents can be referenced by: |
| 507 | * |
| 508 | * - Different subvolumes |
| 509 | * - Different generations of the same subvolume |
| 510 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 511 | * When a tree block is created, back references are inserted: |
| 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 tree block is cow'd, new back references are added for all the |
| 516 | * blocks it points to. If the tree block isn't in reference counted root, |
| 517 | * the old back references are removed. These new back references are of |
| 518 | * the form (trans->transid will have increased since creation): |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 519 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 520 | * (root->root_key.objectid, trans->transid, level, 1) |
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 | * When a backref is in deleting, the following fields are checked: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 523 | * |
| 524 | * if backref was for a tree root: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 525 | * (btrfs_header_owner(itself), btrfs_header_generation(itself), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 526 | * else |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 527 | * (btrfs_header_owner(parent), btrfs_header_generation(parent), level) |
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 | * Back Reference Key composing: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 530 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 531 | * The key objectid corresponds to the first byte in the extent, the key |
| 532 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first |
| 533 | * byte of parent extent. If a extent is tree root, the key offset is set |
| 534 | * to the key objectid. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 535 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 536 | |
| 537 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 538 | struct btrfs_root *root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 539 | struct btrfs_path *path, |
| 540 | u64 bytenr, u64 parent, |
| 541 | u64 ref_root, u64 ref_generation, |
| 542 | u64 owner_objectid, int del) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 543 | { |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 544 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 545 | struct btrfs_extent_ref *ref; |
| 546 | struct extent_buffer *leaf; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 547 | u64 ref_objectid; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 548 | int ret; |
| 549 | |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 550 | key.objectid = bytenr; |
| 551 | key.type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 552 | key.offset = parent; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 553 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 554 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); |
| 555 | if (ret < 0) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 556 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 557 | if (ret > 0) { |
| 558 | ret = -ENOENT; |
| 559 | goto out; |
| 560 | } |
| 561 | |
| 562 | leaf = path->nodes[0]; |
| 563 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 564 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 565 | if (btrfs_ref_root(leaf, ref) != ref_root || |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 566 | btrfs_ref_generation(leaf, ref) != ref_generation || |
| 567 | (ref_objectid != owner_objectid && |
| 568 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 569 | ret = -EIO; |
| 570 | WARN_ON(1); |
| 571 | goto out; |
| 572 | } |
| 573 | ret = 0; |
| 574 | out: |
| 575 | return ret; |
| 576 | } |
| 577 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 578 | /* |
| 579 | * updates all the backrefs that are pending on update_list for the |
| 580 | * extent_root |
| 581 | */ |
| 582 | static int noinline update_backrefs(struct btrfs_trans_handle *trans, |
| 583 | struct btrfs_root *extent_root, |
| 584 | struct btrfs_path *path, |
| 585 | struct list_head *update_list) |
| 586 | { |
| 587 | struct btrfs_key key; |
| 588 | struct btrfs_extent_ref *ref; |
| 589 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 590 | struct pending_extent_op *op; |
| 591 | struct extent_buffer *leaf; |
| 592 | int ret = 0; |
| 593 | struct list_head *cur = update_list->next; |
| 594 | u64 ref_objectid; |
| 595 | u64 ref_root = extent_root->root_key.objectid; |
| 596 | |
| 597 | op = list_entry(cur, struct pending_extent_op, list); |
| 598 | |
| 599 | search: |
| 600 | key.objectid = op->bytenr; |
| 601 | key.type = BTRFS_EXTENT_REF_KEY; |
| 602 | key.offset = op->orig_parent; |
| 603 | |
| 604 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1); |
| 605 | BUG_ON(ret); |
| 606 | |
| 607 | leaf = path->nodes[0]; |
| 608 | |
| 609 | loop: |
| 610 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 611 | |
| 612 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 613 | |
| 614 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 615 | btrfs_ref_generation(leaf, ref) != op->orig_generation || |
| 616 | (ref_objectid != op->level && |
| 617 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
| 618 | printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, " |
| 619 | "owner %u\n", op->bytenr, op->orig_parent, |
| 620 | ref_root, op->level); |
| 621 | btrfs_print_leaf(extent_root, leaf); |
| 622 | BUG(); |
| 623 | } |
| 624 | |
| 625 | key.objectid = op->bytenr; |
| 626 | key.offset = op->parent; |
| 627 | key.type = BTRFS_EXTENT_REF_KEY; |
| 628 | ret = btrfs_set_item_key_safe(trans, extent_root, path, &key); |
| 629 | BUG_ON(ret); |
| 630 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 631 | btrfs_set_ref_generation(leaf, ref, op->generation); |
| 632 | |
| 633 | cur = cur->next; |
| 634 | |
| 635 | list_del_init(&op->list); |
| 636 | unlock_extent(&info->extent_ins, op->bytenr, |
| 637 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 638 | kfree(op); |
| 639 | |
| 640 | if (cur == update_list) { |
| 641 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 642 | btrfs_release_path(extent_root, path); |
| 643 | goto out; |
| 644 | } |
| 645 | |
| 646 | op = list_entry(cur, struct pending_extent_op, list); |
| 647 | |
| 648 | path->slots[0]++; |
| 649 | while (path->slots[0] < btrfs_header_nritems(leaf)) { |
| 650 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 651 | if (key.objectid == op->bytenr && |
| 652 | key.type == BTRFS_EXTENT_REF_KEY) |
| 653 | goto loop; |
| 654 | path->slots[0]++; |
| 655 | } |
| 656 | |
| 657 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 658 | btrfs_release_path(extent_root, path); |
| 659 | goto search; |
| 660 | |
| 661 | out: |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static int noinline insert_extents(struct btrfs_trans_handle *trans, |
| 666 | struct btrfs_root *extent_root, |
| 667 | struct btrfs_path *path, |
| 668 | struct list_head *insert_list, int nr) |
| 669 | { |
| 670 | struct btrfs_key *keys; |
| 671 | u32 *data_size; |
| 672 | struct pending_extent_op *op; |
| 673 | struct extent_buffer *leaf; |
| 674 | struct list_head *cur = insert_list->next; |
| 675 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 676 | u64 ref_root = extent_root->root_key.objectid; |
| 677 | int i = 0, last = 0, ret; |
| 678 | int total = nr * 2; |
| 679 | |
| 680 | if (!nr) |
| 681 | return 0; |
| 682 | |
| 683 | keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS); |
| 684 | if (!keys) |
| 685 | return -ENOMEM; |
| 686 | |
| 687 | data_size = kzalloc(total * sizeof(u32), GFP_NOFS); |
| 688 | if (!data_size) { |
| 689 | kfree(keys); |
| 690 | return -ENOMEM; |
| 691 | } |
| 692 | |
| 693 | list_for_each_entry(op, insert_list, list) { |
| 694 | keys[i].objectid = op->bytenr; |
| 695 | keys[i].offset = op->num_bytes; |
| 696 | keys[i].type = BTRFS_EXTENT_ITEM_KEY; |
| 697 | data_size[i] = sizeof(struct btrfs_extent_item); |
| 698 | i++; |
| 699 | |
| 700 | keys[i].objectid = op->bytenr; |
| 701 | keys[i].offset = op->parent; |
| 702 | keys[i].type = BTRFS_EXTENT_REF_KEY; |
| 703 | data_size[i] = sizeof(struct btrfs_extent_ref); |
| 704 | i++; |
| 705 | } |
| 706 | |
| 707 | op = list_entry(cur, struct pending_extent_op, list); |
| 708 | i = 0; |
| 709 | while (i < total) { |
| 710 | int c; |
| 711 | ret = btrfs_insert_some_items(trans, extent_root, path, |
| 712 | keys+i, data_size+i, total-i); |
| 713 | BUG_ON(ret < 0); |
| 714 | |
| 715 | if (last && ret > 1) |
| 716 | BUG(); |
| 717 | |
| 718 | leaf = path->nodes[0]; |
| 719 | for (c = 0; c < ret; c++) { |
| 720 | int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY; |
| 721 | |
| 722 | /* |
| 723 | * if the first item we inserted was a backref, then |
| 724 | * the EXTENT_ITEM will be the odd c's, else it will |
| 725 | * be the even c's |
| 726 | */ |
| 727 | if ((ref_first && (c % 2)) || |
| 728 | (!ref_first && !(c % 2))) { |
| 729 | struct btrfs_extent_item *itm; |
| 730 | |
| 731 | itm = btrfs_item_ptr(leaf, path->slots[0] + c, |
| 732 | struct btrfs_extent_item); |
| 733 | btrfs_set_extent_refs(path->nodes[0], itm, 1); |
| 734 | op->del++; |
| 735 | } else { |
| 736 | struct btrfs_extent_ref *ref; |
| 737 | |
| 738 | ref = btrfs_item_ptr(leaf, path->slots[0] + c, |
| 739 | struct btrfs_extent_ref); |
| 740 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 741 | btrfs_set_ref_generation(leaf, ref, |
| 742 | op->generation); |
| 743 | btrfs_set_ref_objectid(leaf, ref, op->level); |
| 744 | btrfs_set_ref_num_refs(leaf, ref, 1); |
| 745 | op->del++; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * using del to see when its ok to free up the |
| 750 | * pending_extent_op. In the case where we insert the |
| 751 | * last item on the list in order to help do batching |
| 752 | * we need to not free the extent op until we actually |
| 753 | * insert the extent_item |
| 754 | */ |
| 755 | if (op->del == 2) { |
| 756 | unlock_extent(&info->extent_ins, op->bytenr, |
| 757 | op->bytenr + op->num_bytes - 1, |
| 758 | GFP_NOFS); |
| 759 | cur = cur->next; |
| 760 | list_del_init(&op->list); |
| 761 | kfree(op); |
| 762 | if (cur != insert_list) |
| 763 | op = list_entry(cur, |
| 764 | struct pending_extent_op, |
| 765 | list); |
| 766 | } |
| 767 | } |
| 768 | btrfs_mark_buffer_dirty(leaf); |
| 769 | btrfs_release_path(extent_root, path); |
| 770 | |
| 771 | /* |
| 772 | * Ok backref's and items usually go right next to eachother, |
| 773 | * but if we could only insert 1 item that means that we |
| 774 | * inserted on the end of a leaf, and we have no idea what may |
| 775 | * be on the next leaf so we just play it safe. In order to |
| 776 | * try and help this case we insert the last thing on our |
| 777 | * insert list so hopefully it will end up being the last |
| 778 | * thing on the leaf and everything else will be before it, |
| 779 | * which will let us insert a whole bunch of items at the same |
| 780 | * time. |
| 781 | */ |
| 782 | if (ret == 1 && !last && (i + ret < total)) { |
| 783 | /* |
| 784 | * last: where we will pick up the next time around |
| 785 | * i: our current key to insert, will be total - 1 |
| 786 | * cur: the current op we are screwing with |
| 787 | * op: duh |
| 788 | */ |
| 789 | last = i + ret; |
| 790 | i = total - 1; |
| 791 | cur = insert_list->prev; |
| 792 | op = list_entry(cur, struct pending_extent_op, list); |
| 793 | } else if (last) { |
| 794 | /* |
| 795 | * ok we successfully inserted the last item on the |
| 796 | * list, lets reset everything |
| 797 | * |
| 798 | * i: our current key to insert, so where we left off |
| 799 | * last time |
| 800 | * last: done with this |
| 801 | * cur: the op we are messing with |
| 802 | * op: duh |
| 803 | * total: since we inserted the last key, we need to |
| 804 | * decrement total so we dont overflow |
| 805 | */ |
| 806 | i = last; |
| 807 | last = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 808 | total--; |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame] | 809 | if (i < total) { |
| 810 | cur = insert_list->next; |
| 811 | op = list_entry(cur, struct pending_extent_op, |
| 812 | list); |
| 813 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 814 | } else { |
| 815 | i += ret; |
| 816 | } |
| 817 | |
| 818 | cond_resched(); |
| 819 | } |
| 820 | ret = 0; |
| 821 | kfree(keys); |
| 822 | kfree(data_size); |
| 823 | return ret; |
| 824 | } |
| 825 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 826 | static int noinline insert_extent_backref(struct btrfs_trans_handle *trans, |
| 827 | struct btrfs_root *root, |
| 828 | struct btrfs_path *path, |
| 829 | u64 bytenr, u64 parent, |
| 830 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 831 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 832 | { |
| 833 | struct btrfs_key key; |
| 834 | struct extent_buffer *leaf; |
| 835 | struct btrfs_extent_ref *ref; |
| 836 | u32 num_refs; |
| 837 | int ret; |
| 838 | |
| 839 | key.objectid = bytenr; |
| 840 | key.type = BTRFS_EXTENT_REF_KEY; |
| 841 | key.offset = parent; |
| 842 | |
| 843 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
| 844 | if (ret == 0) { |
| 845 | leaf = path->nodes[0]; |
| 846 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 847 | struct btrfs_extent_ref); |
| 848 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 849 | btrfs_set_ref_generation(leaf, ref, ref_generation); |
| 850 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 851 | btrfs_set_ref_num_refs(leaf, ref, 1); |
| 852 | } else if (ret == -EEXIST) { |
| 853 | u64 existing_owner; |
| 854 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); |
| 855 | leaf = path->nodes[0]; |
| 856 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 857 | struct btrfs_extent_ref); |
| 858 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 859 | btrfs_ref_generation(leaf, ref) != ref_generation) { |
| 860 | ret = -EIO; |
| 861 | WARN_ON(1); |
| 862 | goto out; |
| 863 | } |
| 864 | |
| 865 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 866 | BUG_ON(num_refs == 0); |
| 867 | btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); |
| 868 | |
| 869 | existing_owner = btrfs_ref_objectid(leaf, ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 870 | if (existing_owner != owner_objectid && |
| 871 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 872 | btrfs_set_ref_objectid(leaf, ref, |
| 873 | BTRFS_MULTIPLE_OBJECTIDS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 874 | } |
| 875 | ret = 0; |
| 876 | } else { |
| 877 | goto out; |
| 878 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 879 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 880 | out: |
| 881 | btrfs_release_path(root, path); |
| 882 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 883 | } |
| 884 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 885 | static int noinline remove_extent_backref(struct btrfs_trans_handle *trans, |
| 886 | struct btrfs_root *root, |
| 887 | struct btrfs_path *path) |
| 888 | { |
| 889 | struct extent_buffer *leaf; |
| 890 | struct btrfs_extent_ref *ref; |
| 891 | u32 num_refs; |
| 892 | int ret = 0; |
| 893 | |
| 894 | leaf = path->nodes[0]; |
| 895 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 896 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 897 | BUG_ON(num_refs == 0); |
| 898 | num_refs -= 1; |
| 899 | if (num_refs == 0) { |
| 900 | ret = btrfs_del_item(trans, root, path); |
| 901 | } else { |
| 902 | btrfs_set_ref_num_refs(leaf, ref, num_refs); |
| 903 | btrfs_mark_buffer_dirty(leaf); |
| 904 | } |
| 905 | btrfs_release_path(root, path); |
| 906 | return ret; |
| 907 | } |
| 908 | |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 909 | #ifdef BIO_RW_DISCARD |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 910 | static void btrfs_issue_discard(struct block_device *bdev, |
| 911 | u64 start, u64 len) |
| 912 | { |
| 913 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28) |
| 914 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
| 915 | #else |
| 916 | blkdev_issue_discard(bdev, start >> 9, len >> 9); |
| 917 | #endif |
| 918 | } |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 919 | #endif |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 920 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 921 | static int noinline free_extents(struct btrfs_trans_handle *trans, |
| 922 | struct btrfs_root *extent_root, |
| 923 | struct list_head *del_list) |
| 924 | { |
| 925 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 926 | struct btrfs_path *path; |
| 927 | struct btrfs_key key, found_key; |
| 928 | struct extent_buffer *leaf; |
| 929 | struct list_head *cur; |
| 930 | struct pending_extent_op *op; |
| 931 | struct btrfs_extent_item *ei; |
| 932 | int ret, num_to_del, extent_slot = 0, found_extent = 0; |
| 933 | u32 refs; |
| 934 | u64 bytes_freed = 0; |
| 935 | |
| 936 | path = btrfs_alloc_path(); |
| 937 | if (!path) |
| 938 | return -ENOMEM; |
| 939 | path->reada = 1; |
| 940 | |
| 941 | search: |
| 942 | /* search for the backref for the current ref we want to delete */ |
| 943 | cur = del_list->next; |
| 944 | op = list_entry(cur, struct pending_extent_op, list); |
| 945 | ret = lookup_extent_backref(trans, extent_root, path, op->bytenr, |
| 946 | op->orig_parent, |
| 947 | extent_root->root_key.objectid, |
| 948 | op->orig_generation, op->level, 1); |
| 949 | if (ret) { |
| 950 | printk("Unable to find backref byte nr %Lu root %Lu gen %Lu " |
| 951 | "owner %u\n", op->bytenr, |
| 952 | extent_root->root_key.objectid, op->orig_generation, |
| 953 | op->level); |
| 954 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 955 | WARN_ON(1); |
| 956 | goto out; |
| 957 | } |
| 958 | |
| 959 | extent_slot = path->slots[0]; |
| 960 | num_to_del = 1; |
| 961 | found_extent = 0; |
| 962 | |
| 963 | /* |
| 964 | * if we aren't the first item on the leaf we can move back one and see |
| 965 | * if our ref is right next to our extent item |
| 966 | */ |
| 967 | if (likely(extent_slot)) { |
| 968 | extent_slot--; |
| 969 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 970 | extent_slot); |
| 971 | if (found_key.objectid == op->bytenr && |
| 972 | found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 973 | found_key.offset == op->num_bytes) { |
| 974 | num_to_del++; |
| 975 | found_extent = 1; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | /* |
| 980 | * if we didn't find the extent we need to delete the backref and then |
| 981 | * search for the extent item key so we can update its ref count |
| 982 | */ |
| 983 | if (!found_extent) { |
| 984 | key.objectid = op->bytenr; |
| 985 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 986 | key.offset = op->num_bytes; |
| 987 | |
| 988 | ret = remove_extent_backref(trans, extent_root, path); |
| 989 | BUG_ON(ret); |
| 990 | btrfs_release_path(extent_root, path); |
| 991 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
| 992 | BUG_ON(ret); |
| 993 | extent_slot = path->slots[0]; |
| 994 | } |
| 995 | |
| 996 | /* this is where we update the ref count for the extent */ |
| 997 | leaf = path->nodes[0]; |
| 998 | ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item); |
| 999 | refs = btrfs_extent_refs(leaf, ei); |
| 1000 | BUG_ON(refs == 0); |
| 1001 | refs--; |
| 1002 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1003 | |
| 1004 | btrfs_mark_buffer_dirty(leaf); |
| 1005 | |
| 1006 | /* |
| 1007 | * This extent needs deleting. The reason cur_slot is extent_slot + |
| 1008 | * num_to_del is because extent_slot points to the slot where the extent |
| 1009 | * is, and if the backref was not right next to the extent we will be |
| 1010 | * deleting at least 1 item, and will want to start searching at the |
| 1011 | * slot directly next to extent_slot. However if we did find the |
| 1012 | * backref next to the extent item them we will be deleting at least 2 |
| 1013 | * items and will want to start searching directly after the ref slot |
| 1014 | */ |
| 1015 | if (!refs) { |
| 1016 | struct list_head *pos, *n, *end; |
| 1017 | int cur_slot = extent_slot+num_to_del; |
| 1018 | u64 super_used; |
| 1019 | u64 root_used; |
| 1020 | |
| 1021 | path->slots[0] = extent_slot; |
| 1022 | bytes_freed = op->num_bytes; |
| 1023 | |
Josef Bacik | e3e469f | 2008-11-17 21:11:49 -0500 | [diff] [blame] | 1024 | mutex_lock(&info->pinned_mutex); |
| 1025 | ret = pin_down_bytes(trans, extent_root, op->bytenr, |
| 1026 | op->num_bytes, op->level >= |
| 1027 | BTRFS_FIRST_FREE_OBJECTID); |
| 1028 | mutex_unlock(&info->pinned_mutex); |
| 1029 | BUG_ON(ret < 0); |
| 1030 | op->del = ret; |
| 1031 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1032 | /* |
| 1033 | * we need to see if we can delete multiple things at once, so |
| 1034 | * start looping through the list of extents we are wanting to |
| 1035 | * delete and see if their extent/backref's are right next to |
| 1036 | * eachother and the extents only have 1 ref |
| 1037 | */ |
| 1038 | for (pos = cur->next; pos != del_list; pos = pos->next) { |
| 1039 | struct pending_extent_op *tmp; |
| 1040 | |
| 1041 | tmp = list_entry(pos, struct pending_extent_op, list); |
| 1042 | |
| 1043 | /* we only want to delete extent+ref at this stage */ |
| 1044 | if (cur_slot >= btrfs_header_nritems(leaf) - 1) |
| 1045 | break; |
| 1046 | |
| 1047 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot); |
| 1048 | if (found_key.objectid != tmp->bytenr || |
| 1049 | found_key.type != BTRFS_EXTENT_ITEM_KEY || |
| 1050 | found_key.offset != tmp->num_bytes) |
| 1051 | break; |
| 1052 | |
| 1053 | /* check to make sure this extent only has one ref */ |
| 1054 | ei = btrfs_item_ptr(leaf, cur_slot, |
| 1055 | struct btrfs_extent_item); |
| 1056 | if (btrfs_extent_refs(leaf, ei) != 1) |
| 1057 | break; |
| 1058 | |
| 1059 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1); |
| 1060 | if (found_key.objectid != tmp->bytenr || |
| 1061 | found_key.type != BTRFS_EXTENT_REF_KEY || |
| 1062 | found_key.offset != tmp->orig_parent) |
| 1063 | break; |
| 1064 | |
| 1065 | /* |
| 1066 | * the ref is right next to the extent, we can set the |
| 1067 | * ref count to 0 since we will delete them both now |
| 1068 | */ |
| 1069 | btrfs_set_extent_refs(leaf, ei, 0); |
| 1070 | |
| 1071 | /* pin down the bytes for this extent */ |
| 1072 | mutex_lock(&info->pinned_mutex); |
| 1073 | ret = pin_down_bytes(trans, extent_root, tmp->bytenr, |
| 1074 | tmp->num_bytes, tmp->level >= |
| 1075 | BTRFS_FIRST_FREE_OBJECTID); |
| 1076 | mutex_unlock(&info->pinned_mutex); |
| 1077 | BUG_ON(ret < 0); |
| 1078 | |
| 1079 | /* |
| 1080 | * use the del field to tell if we need to go ahead and |
| 1081 | * free up the extent when we delete the item or not. |
| 1082 | */ |
| 1083 | tmp->del = ret; |
| 1084 | bytes_freed += tmp->num_bytes; |
| 1085 | |
| 1086 | num_to_del += 2; |
| 1087 | cur_slot += 2; |
| 1088 | } |
| 1089 | end = pos; |
| 1090 | |
| 1091 | /* update the free space counters */ |
| 1092 | spin_lock_irq(&info->delalloc_lock); |
| 1093 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1094 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1095 | super_used - bytes_freed); |
| 1096 | spin_unlock_irq(&info->delalloc_lock); |
| 1097 | |
| 1098 | root_used = btrfs_root_used(&extent_root->root_item); |
| 1099 | btrfs_set_root_used(&extent_root->root_item, |
| 1100 | root_used - bytes_freed); |
| 1101 | |
| 1102 | /* delete the items */ |
| 1103 | ret = btrfs_del_items(trans, extent_root, path, |
| 1104 | path->slots[0], num_to_del); |
| 1105 | BUG_ON(ret); |
| 1106 | |
| 1107 | /* |
| 1108 | * loop through the extents we deleted and do the cleanup work |
| 1109 | * on them |
| 1110 | */ |
| 1111 | for (pos = cur, n = pos->next; pos != end; |
| 1112 | pos = n, n = pos->next) { |
| 1113 | struct pending_extent_op *tmp; |
| 1114 | #ifdef BIO_RW_DISCARD |
| 1115 | u64 map_length; |
| 1116 | struct btrfs_multi_bio *multi = NULL; |
| 1117 | #endif |
| 1118 | tmp = list_entry(pos, struct pending_extent_op, list); |
| 1119 | |
| 1120 | /* |
| 1121 | * remember tmp->del tells us wether or not we pinned |
| 1122 | * down the extent |
| 1123 | */ |
| 1124 | ret = update_block_group(trans, extent_root, |
| 1125 | tmp->bytenr, tmp->num_bytes, 0, |
| 1126 | tmp->del); |
| 1127 | BUG_ON(ret); |
| 1128 | |
| 1129 | #ifdef BIO_RW_DISCARD |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1130 | map_length = tmp->num_bytes; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1131 | ret = btrfs_map_block(&info->mapping_tree, READ, |
| 1132 | tmp->bytenr, &map_length, &multi, |
| 1133 | 0); |
| 1134 | if (!ret) { |
| 1135 | struct btrfs_bio_stripe *stripe; |
| 1136 | int i; |
| 1137 | |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1138 | stripe = multi->stripes; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1139 | |
| 1140 | if (map_length > tmp->num_bytes) |
| 1141 | map_length = tmp->num_bytes; |
| 1142 | |
| 1143 | for (i = 0; i < multi->num_stripes; |
| 1144 | i++, stripe++) |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1145 | btrfs_issue_discard(stripe->dev->bdev, |
| 1146 | stripe->physical, |
| 1147 | map_length); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1148 | kfree(multi); |
| 1149 | } |
| 1150 | #endif |
| 1151 | list_del_init(&tmp->list); |
| 1152 | unlock_extent(&info->extent_ins, tmp->bytenr, |
| 1153 | tmp->bytenr + tmp->num_bytes - 1, |
| 1154 | GFP_NOFS); |
| 1155 | kfree(tmp); |
| 1156 | } |
| 1157 | } else if (refs && found_extent) { |
| 1158 | /* |
| 1159 | * the ref and extent were right next to eachother, but the |
| 1160 | * extent still has a ref, so just free the backref and keep |
| 1161 | * going |
| 1162 | */ |
| 1163 | ret = remove_extent_backref(trans, extent_root, path); |
| 1164 | BUG_ON(ret); |
| 1165 | |
| 1166 | list_del_init(&op->list); |
| 1167 | unlock_extent(&info->extent_ins, op->bytenr, |
| 1168 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 1169 | kfree(op); |
| 1170 | } else { |
| 1171 | /* |
| 1172 | * the extent has multiple refs and the backref we were looking |
| 1173 | * for was not right next to it, so just unlock and go next, |
| 1174 | * we're good to go |
| 1175 | */ |
| 1176 | list_del_init(&op->list); |
| 1177 | unlock_extent(&info->extent_ins, op->bytenr, |
| 1178 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 1179 | kfree(op); |
| 1180 | } |
| 1181 | |
| 1182 | btrfs_release_path(extent_root, path); |
| 1183 | if (!list_empty(del_list)) |
| 1184 | goto search; |
| 1185 | |
| 1186 | out: |
| 1187 | btrfs_free_path(path); |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1191 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 1192 | struct btrfs_root *root, u64 bytenr, |
| 1193 | u64 orig_parent, u64 parent, |
| 1194 | u64 orig_root, u64 ref_root, |
| 1195 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1196 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1197 | { |
| 1198 | int ret; |
| 1199 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1200 | struct btrfs_path *path; |
| 1201 | |
| 1202 | if (root == root->fs_info->extent_root) { |
| 1203 | struct pending_extent_op *extent_op; |
| 1204 | u64 num_bytes; |
| 1205 | |
| 1206 | BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL); |
| 1207 | num_bytes = btrfs_level_size(root, (int)owner_objectid); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1208 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1209 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1210 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1211 | u64 priv; |
| 1212 | ret = get_state_private(&root->fs_info->extent_ins, |
| 1213 | bytenr, &priv); |
| 1214 | BUG_ON(ret); |
| 1215 | extent_op = (struct pending_extent_op *) |
| 1216 | (unsigned long)priv; |
| 1217 | BUG_ON(extent_op->parent != orig_parent); |
| 1218 | BUG_ON(extent_op->generation != orig_generation); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1219 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1220 | extent_op->parent = parent; |
| 1221 | extent_op->generation = ref_generation; |
| 1222 | } else { |
| 1223 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 1224 | BUG_ON(!extent_op); |
| 1225 | |
| 1226 | extent_op->type = PENDING_BACKREF_UPDATE; |
| 1227 | extent_op->bytenr = bytenr; |
| 1228 | extent_op->num_bytes = num_bytes; |
| 1229 | extent_op->parent = parent; |
| 1230 | extent_op->orig_parent = orig_parent; |
| 1231 | extent_op->generation = ref_generation; |
| 1232 | extent_op->orig_generation = orig_generation; |
| 1233 | extent_op->level = (int)owner_objectid; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1234 | INIT_LIST_HEAD(&extent_op->list); |
| 1235 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1236 | |
| 1237 | set_extent_bits(&root->fs_info->extent_ins, |
| 1238 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1239 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1240 | set_state_private(&root->fs_info->extent_ins, |
| 1241 | bytenr, (unsigned long)extent_op); |
| 1242 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1243 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | path = btrfs_alloc_path(); |
| 1248 | if (!path) |
| 1249 | return -ENOMEM; |
| 1250 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1251 | bytenr, orig_parent, orig_root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1252 | orig_generation, owner_objectid, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1253 | if (ret) |
| 1254 | goto out; |
| 1255 | ret = remove_extent_backref(trans, extent_root, path); |
| 1256 | if (ret) |
| 1257 | goto out; |
| 1258 | ret = insert_extent_backref(trans, extent_root, path, bytenr, |
| 1259 | parent, ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1260 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1261 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1262 | finish_current_insert(trans, extent_root, 0); |
| 1263 | del_pending_extents(trans, extent_root, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1264 | out: |
| 1265 | btrfs_free_path(path); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 1270 | struct btrfs_root *root, u64 bytenr, |
| 1271 | u64 orig_parent, u64 parent, |
| 1272 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1273 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1274 | { |
| 1275 | int ret; |
| 1276 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 1277 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 1278 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1279 | ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, |
| 1280 | parent, ref_root, ref_root, |
| 1281 | ref_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1282 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1283 | return ret; |
| 1284 | } |
| 1285 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1286 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1287 | struct btrfs_root *root, u64 bytenr, |
| 1288 | u64 orig_parent, u64 parent, |
| 1289 | u64 orig_root, u64 ref_root, |
| 1290 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1291 | u64 owner_objectid) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1292 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1293 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1294 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1295 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1296 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1297 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1298 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1299 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1300 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1301 | if (!path) |
| 1302 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1303 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1304 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1305 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1306 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1307 | key.offset = (u64)-1; |
| 1308 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1309 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1310 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1311 | if (ret < 0) |
| 1312 | return ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1313 | BUG_ON(ret == 0 || path->slots[0] == 0); |
| 1314 | |
| 1315 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1316 | l = path->nodes[0]; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1317 | |
| 1318 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1319 | if (key.objectid != bytenr) { |
| 1320 | btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]); |
| 1321 | printk("wanted %Lu found %Lu\n", bytenr, key.objectid); |
| 1322 | BUG(); |
| 1323 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1324 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
| 1325 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1326 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1327 | refs = btrfs_extent_refs(l, item); |
| 1328 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1329 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1330 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1331 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1332 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1333 | path->reada = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1334 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
| 1335 | path, bytenr, parent, |
| 1336 | ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1337 | owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1338 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1339 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 1340 | del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1341 | |
| 1342 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
| 1345 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1346 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1347 | struct btrfs_root *root, |
| 1348 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1349 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1350 | u64 owner_objectid) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1351 | { |
| 1352 | int ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1353 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 1354 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 1355 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1356 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, |
| 1357 | 0, ref_root, 0, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1358 | owner_objectid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1359 | return ret; |
| 1360 | } |
| 1361 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1362 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 1363 | struct btrfs_root *root) |
| 1364 | { |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1365 | finish_current_insert(trans, root->fs_info->extent_root, 1); |
| 1366 | del_pending_extents(trans, root->fs_info->extent_root, 1); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1367 | return 0; |
| 1368 | } |
| 1369 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1370 | int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, |
| 1371 | struct btrfs_root *root, u64 bytenr, |
| 1372 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1373 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1374 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1375 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1376 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1377 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1378 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1379 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1380 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1381 | path = btrfs_alloc_path(); |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1382 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1383 | key.objectid = bytenr; |
| 1384 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1385 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1386 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1387 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1388 | if (ret < 0) |
| 1389 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1390 | if (ret != 0) { |
| 1391 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1392 | printk("failed to find block number %Lu\n", bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1393 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1394 | } |
| 1395 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1396 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1397 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1398 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1399 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1403 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 1404 | struct btrfs_root *root, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1405 | { |
| 1406 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1407 | struct btrfs_path *path; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1408 | struct extent_buffer *leaf; |
| 1409 | struct btrfs_extent_ref *ref_item; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1410 | struct btrfs_key key; |
| 1411 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1412 | u64 ref_root; |
| 1413 | u64 last_snapshot; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1414 | u32 nritems; |
| 1415 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1416 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1417 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1418 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1419 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1420 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1421 | path = btrfs_alloc_path(); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1422 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 1423 | if (ret < 0) |
| 1424 | goto out; |
| 1425 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1426 | |
| 1427 | ret = -ENOENT; |
| 1428 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1429 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1430 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1431 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1432 | leaf = path->nodes[0]; |
| 1433 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1434 | |
| 1435 | if (found_key.objectid != bytenr || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1436 | found_key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1437 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1438 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1439 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1440 | while (1) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1441 | leaf = path->nodes[0]; |
| 1442 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1443 | if (path->slots[0] >= nritems) { |
| 1444 | ret = btrfs_next_leaf(extent_root, path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1445 | if (ret < 0) |
| 1446 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1447 | if (ret == 0) |
| 1448 | continue; |
| 1449 | break; |
| 1450 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1451 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1452 | if (found_key.objectid != bytenr) |
| 1453 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 1454 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1455 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 1456 | path->slots[0]++; |
| 1457 | continue; |
| 1458 | } |
| 1459 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1460 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1461 | struct btrfs_extent_ref); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1462 | ref_root = btrfs_ref_root(leaf, ref_item); |
| 1463 | if (ref_root != root->root_key.objectid && |
| 1464 | ref_root != BTRFS_TREE_LOG_OBJECTID) { |
| 1465 | ret = 1; |
| 1466 | goto out; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1467 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1468 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { |
| 1469 | ret = 1; |
| 1470 | goto out; |
| 1471 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1472 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1473 | path->slots[0]++; |
| 1474 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1475 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1476 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1477 | btrfs_free_path(path); |
| 1478 | return ret; |
| 1479 | } |
| 1480 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1481 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1482 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1483 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1484 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1485 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1486 | u64 root_gen; |
| 1487 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1488 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1489 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1490 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1491 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1492 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1493 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1494 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1495 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1496 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 1497 | shared = 0; |
| 1498 | root_gen = root->root_key.offset; |
| 1499 | } else { |
| 1500 | shared = 1; |
| 1501 | root_gen = trans->transid - 1; |
| 1502 | } |
| 1503 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1504 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1505 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1506 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1507 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1508 | struct btrfs_leaf_ref *ref; |
| 1509 | struct btrfs_extent_info *info; |
| 1510 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1511 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1512 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1513 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1514 | goto out; |
| 1515 | } |
| 1516 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1517 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1518 | ref->bytenr = buf->start; |
| 1519 | ref->owner = btrfs_header_owner(buf); |
| 1520 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1521 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1522 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1523 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1524 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1525 | u64 disk_bytenr; |
| 1526 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1527 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1528 | continue; |
| 1529 | fi = btrfs_item_ptr(buf, i, |
| 1530 | struct btrfs_file_extent_item); |
| 1531 | if (btrfs_file_extent_type(buf, fi) == |
| 1532 | BTRFS_FILE_EXTENT_INLINE) |
| 1533 | continue; |
| 1534 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1535 | if (disk_bytenr == 0) |
| 1536 | continue; |
| 1537 | |
| 1538 | info->bytenr = disk_bytenr; |
| 1539 | info->num_bytes = |
| 1540 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 1541 | info->objectid = key.objectid; |
| 1542 | info->offset = key.offset; |
| 1543 | info++; |
| 1544 | } |
| 1545 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1546 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1547 | if (ret == -EEXIST && shared) { |
| 1548 | struct btrfs_leaf_ref *old; |
| 1549 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 1550 | BUG_ON(!old); |
| 1551 | btrfs_remove_leaf_ref(root, old); |
| 1552 | btrfs_free_leaf_ref(root, old); |
| 1553 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 1554 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1555 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1556 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1557 | } |
| 1558 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1559 | return ret; |
| 1560 | } |
| 1561 | |
| 1562 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1563 | struct extent_buffer *orig_buf, struct extent_buffer *buf, |
| 1564 | u32 *nr_extents) |
| 1565 | { |
| 1566 | u64 bytenr; |
| 1567 | u64 ref_root; |
| 1568 | u64 orig_root; |
| 1569 | u64 ref_generation; |
| 1570 | u64 orig_generation; |
| 1571 | u32 nritems; |
| 1572 | u32 nr_file_extents = 0; |
| 1573 | struct btrfs_key key; |
| 1574 | struct btrfs_file_extent_item *fi; |
| 1575 | int i; |
| 1576 | int level; |
| 1577 | int ret = 0; |
| 1578 | int faili = 0; |
| 1579 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1580 | u64, u64, u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1581 | |
| 1582 | ref_root = btrfs_header_owner(buf); |
| 1583 | ref_generation = btrfs_header_generation(buf); |
| 1584 | orig_root = btrfs_header_owner(orig_buf); |
| 1585 | orig_generation = btrfs_header_generation(orig_buf); |
| 1586 | |
| 1587 | nritems = btrfs_header_nritems(buf); |
| 1588 | level = btrfs_header_level(buf); |
| 1589 | |
| 1590 | if (root->ref_cows) { |
| 1591 | process_func = __btrfs_inc_extent_ref; |
| 1592 | } else { |
| 1593 | if (level == 0 && |
| 1594 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1595 | goto out; |
| 1596 | if (level != 0 && |
| 1597 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1598 | goto out; |
| 1599 | process_func = __btrfs_update_extent_ref; |
| 1600 | } |
| 1601 | |
| 1602 | for (i = 0; i < nritems; i++) { |
| 1603 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1604 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1605 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1606 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1607 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1608 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1609 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1610 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1611 | BTRFS_FILE_EXTENT_INLINE) |
| 1612 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1613 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1614 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1615 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1616 | |
| 1617 | nr_file_extents++; |
| 1618 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1619 | ret = process_func(trans, root, bytenr, |
| 1620 | orig_buf->start, buf->start, |
| 1621 | orig_root, ref_root, |
| 1622 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1623 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1624 | |
| 1625 | if (ret) { |
| 1626 | faili = i; |
| 1627 | WARN_ON(1); |
| 1628 | goto fail; |
| 1629 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1630 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1631 | bytenr = btrfs_node_blockptr(buf, i); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1632 | ret = process_func(trans, root, bytenr, |
| 1633 | orig_buf->start, buf->start, |
| 1634 | orig_root, ref_root, |
| 1635 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1636 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1637 | if (ret) { |
| 1638 | faili = i; |
| 1639 | WARN_ON(1); |
| 1640 | goto fail; |
| 1641 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1642 | } |
| 1643 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1644 | out: |
| 1645 | if (nr_extents) { |
| 1646 | if (level == 0) |
| 1647 | *nr_extents = nr_file_extents; |
| 1648 | else |
| 1649 | *nr_extents = nritems; |
| 1650 | } |
| 1651 | return 0; |
| 1652 | fail: |
| 1653 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1654 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1655 | } |
| 1656 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1657 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
| 1658 | struct btrfs_root *root, struct extent_buffer *orig_buf, |
| 1659 | struct extent_buffer *buf, int start_slot, int nr) |
| 1660 | |
| 1661 | { |
| 1662 | u64 bytenr; |
| 1663 | u64 ref_root; |
| 1664 | u64 orig_root; |
| 1665 | u64 ref_generation; |
| 1666 | u64 orig_generation; |
| 1667 | struct btrfs_key key; |
| 1668 | struct btrfs_file_extent_item *fi; |
| 1669 | int i; |
| 1670 | int ret; |
| 1671 | int slot; |
| 1672 | int level; |
| 1673 | |
| 1674 | BUG_ON(start_slot < 0); |
| 1675 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); |
| 1676 | |
| 1677 | ref_root = btrfs_header_owner(buf); |
| 1678 | ref_generation = btrfs_header_generation(buf); |
| 1679 | orig_root = btrfs_header_owner(orig_buf); |
| 1680 | orig_generation = btrfs_header_generation(orig_buf); |
| 1681 | level = btrfs_header_level(buf); |
| 1682 | |
| 1683 | if (!root->ref_cows) { |
| 1684 | if (level == 0 && |
| 1685 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1686 | return 0; |
| 1687 | if (level != 0 && |
| 1688 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { |
| 1693 | cond_resched(); |
| 1694 | if (level == 0) { |
| 1695 | btrfs_item_key_to_cpu(buf, &key, slot); |
| 1696 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1697 | continue; |
| 1698 | fi = btrfs_item_ptr(buf, slot, |
| 1699 | struct btrfs_file_extent_item); |
| 1700 | if (btrfs_file_extent_type(buf, fi) == |
| 1701 | BTRFS_FILE_EXTENT_INLINE) |
| 1702 | continue; |
| 1703 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1704 | if (bytenr == 0) |
| 1705 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1706 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1707 | orig_buf->start, buf->start, |
| 1708 | orig_root, ref_root, |
| 1709 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1710 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1711 | if (ret) |
| 1712 | goto fail; |
| 1713 | } else { |
| 1714 | bytenr = btrfs_node_blockptr(buf, slot); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1715 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1716 | orig_buf->start, buf->start, |
| 1717 | orig_root, ref_root, |
| 1718 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1719 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1720 | if (ret) |
| 1721 | goto fail; |
| 1722 | } |
| 1723 | } |
| 1724 | return 0; |
| 1725 | fail: |
| 1726 | WARN_ON(1); |
| 1727 | return -1; |
| 1728 | } |
| 1729 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1730 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1731 | struct btrfs_root *root, |
| 1732 | struct btrfs_path *path, |
| 1733 | struct btrfs_block_group_cache *cache) |
| 1734 | { |
| 1735 | int ret; |
| 1736 | int pending_ret; |
| 1737 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1738 | unsigned long bi; |
| 1739 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1740 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1741 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1742 | if (ret < 0) |
| 1743 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1744 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1745 | |
| 1746 | leaf = path->nodes[0]; |
| 1747 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1748 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1749 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1750 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1751 | fail: |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1752 | finish_current_insert(trans, extent_root, 0); |
| 1753 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1754 | if (ret) |
| 1755 | return ret; |
| 1756 | if (pending_ret) |
| 1757 | return pending_ret; |
| 1758 | return 0; |
| 1759 | |
| 1760 | } |
| 1761 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1762 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1763 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1764 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1765 | struct btrfs_block_group_cache *cache, *entry; |
| 1766 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1767 | int err = 0; |
| 1768 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1769 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1770 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1771 | |
| 1772 | path = btrfs_alloc_path(); |
| 1773 | if (!path) |
| 1774 | return -ENOMEM; |
| 1775 | |
| 1776 | while(1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1777 | cache = NULL; |
| 1778 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1779 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1780 | n; n = rb_next(n)) { |
| 1781 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1782 | cache_node); |
| 1783 | if (entry->dirty) { |
| 1784 | cache = entry; |
| 1785 | break; |
| 1786 | } |
| 1787 | } |
| 1788 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1789 | |
| 1790 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1791 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1792 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1793 | cache->dirty = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1794 | last += cache->key.offset; |
| 1795 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1796 | err = write_one_cache_group(trans, root, |
| 1797 | path, cache); |
| 1798 | /* |
| 1799 | * if we fail to write the cache group, we want |
| 1800 | * to keep it marked dirty in hopes that a later |
| 1801 | * write will work |
| 1802 | */ |
| 1803 | if (err) { |
| 1804 | werr = err; |
| 1805 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1806 | } |
| 1807 | } |
| 1808 | btrfs_free_path(path); |
| 1809 | return werr; |
| 1810 | } |
| 1811 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1812 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1813 | u64 total_bytes, u64 bytes_used, |
| 1814 | struct btrfs_space_info **space_info) |
| 1815 | { |
| 1816 | struct btrfs_space_info *found; |
| 1817 | |
| 1818 | found = __find_space_info(info, flags); |
| 1819 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1820 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1821 | found->total_bytes += total_bytes; |
| 1822 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1823 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1824 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1825 | *space_info = found; |
| 1826 | return 0; |
| 1827 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1828 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1829 | if (!found) |
| 1830 | return -ENOMEM; |
| 1831 | |
| 1832 | list_add(&found->list, &info->space_info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1833 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1834 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1835 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1836 | found->flags = flags; |
| 1837 | found->total_bytes = total_bytes; |
| 1838 | found->bytes_used = bytes_used; |
| 1839 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1840 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1841 | found->bytes_readonly = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1842 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1843 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1844 | *space_info = found; |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1848 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1849 | { |
| 1850 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1851 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1852 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1853 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1854 | if (extra_flags) { |
| 1855 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1856 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1857 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1858 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1859 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1860 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1861 | } |
| 1862 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1863 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1864 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 1865 | { |
| 1866 | spin_lock(&cache->space_info->lock); |
| 1867 | spin_lock(&cache->lock); |
| 1868 | if (!cache->ro) { |
| 1869 | cache->space_info->bytes_readonly += cache->key.offset - |
| 1870 | btrfs_block_group_used(&cache->item); |
| 1871 | cache->ro = 1; |
| 1872 | } |
| 1873 | spin_unlock(&cache->lock); |
| 1874 | spin_unlock(&cache->space_info->lock); |
| 1875 | } |
| 1876 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1877 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1878 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1879 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1880 | |
| 1881 | if (num_devices == 1) |
| 1882 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1883 | if (num_devices < 4) |
| 1884 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1885 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1886 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1887 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1888 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1889 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1890 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1891 | |
| 1892 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1893 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1894 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1895 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1896 | |
| 1897 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1898 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1899 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1900 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1901 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1902 | return flags; |
| 1903 | } |
| 1904 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1905 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1906 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1907 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1908 | { |
| 1909 | struct btrfs_space_info *space_info; |
| 1910 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1911 | int ret = 0; |
| 1912 | |
| 1913 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1914 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1915 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1916 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1917 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1918 | if (!space_info) { |
| 1919 | ret = update_space_info(extent_root->fs_info, flags, |
| 1920 | 0, 0, &space_info); |
| 1921 | BUG_ON(ret); |
| 1922 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1923 | BUG_ON(!space_info); |
| 1924 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1925 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1926 | if (space_info->force_alloc) { |
| 1927 | force = 1; |
| 1928 | space_info->force_alloc = 0; |
| 1929 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1930 | if (space_info->full) { |
| 1931 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1932 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1933 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1934 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1935 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
| 1936 | thresh = div_factor(thresh, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1937 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1938 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1939 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 1940 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1941 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1942 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1943 | spin_unlock(&space_info->lock); |
| 1944 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1945 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1946 | if (ret) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1947 | printk("space info full %Lu\n", flags); |
| 1948 | space_info->full = 1; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1949 | } |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1950 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1951 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1952 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1953 | } |
| 1954 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1955 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1956 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1957 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1958 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1959 | { |
| 1960 | struct btrfs_block_group_cache *cache; |
| 1961 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1962 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1963 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1964 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1965 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1966 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1967 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1968 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1969 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1970 | byte_in_group = bytenr - cache->key.objectid; |
| 1971 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1972 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1973 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1974 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1975 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1976 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1977 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1978 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1979 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1980 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame^] | 1981 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1982 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1983 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1984 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1985 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1986 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1987 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1988 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1989 | if (cache->ro) |
| 1990 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 1991 | btrfs_set_block_group_used(&cache->item, old_val); |
| 1992 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1993 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1994 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1995 | int ret; |
| 1996 | ret = btrfs_add_free_space(cache, bytenr, |
| 1997 | num_bytes); |
| 1998 | if (ret) |
| 1999 | return -1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2000 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2001 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2002 | total -= num_bytes; |
| 2003 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2004 | } |
| 2005 | return 0; |
| 2006 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2007 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2008 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 2009 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2010 | struct btrfs_block_group_cache *cache; |
| 2011 | |
| 2012 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 2013 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2014 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2015 | |
| 2016 | return cache->key.objectid; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2017 | } |
| 2018 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2019 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2020 | u64 bytenr, u64 num, int pin) |
| 2021 | { |
| 2022 | u64 len; |
| 2023 | struct btrfs_block_group_cache *cache; |
| 2024 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2025 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2026 | WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2027 | if (pin) { |
| 2028 | set_extent_dirty(&fs_info->pinned_extents, |
| 2029 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2030 | } else { |
| 2031 | clear_extent_dirty(&fs_info->pinned_extents, |
| 2032 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2033 | } |
| 2034 | while (num > 0) { |
| 2035 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2036 | BUG_ON(!cache); |
| 2037 | len = min(num, cache->key.offset - |
| 2038 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2039 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2040 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2041 | spin_lock(&cache->lock); |
| 2042 | cache->pinned += len; |
| 2043 | cache->space_info->bytes_pinned += len; |
| 2044 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2045 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2046 | fs_info->total_pinned += len; |
| 2047 | } else { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2048 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2049 | spin_lock(&cache->lock); |
| 2050 | cache->pinned -= len; |
| 2051 | cache->space_info->bytes_pinned -= len; |
| 2052 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2053 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2054 | fs_info->total_pinned -= len; |
Josef Bacik | 07103a3 | 2008-11-19 15:17:55 -0500 | [diff] [blame] | 2055 | if (cache->cached) |
| 2056 | btrfs_add_free_space(cache, bytenr, len); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2057 | } |
| 2058 | bytenr += len; |
| 2059 | num -= len; |
| 2060 | } |
| 2061 | return 0; |
| 2062 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2063 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2064 | static int update_reserved_extents(struct btrfs_root *root, |
| 2065 | u64 bytenr, u64 num, int reserve) |
| 2066 | { |
| 2067 | u64 len; |
| 2068 | struct btrfs_block_group_cache *cache; |
| 2069 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2070 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2071 | while (num > 0) { |
| 2072 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 2073 | BUG_ON(!cache); |
| 2074 | len = min(num, cache->key.offset - |
| 2075 | (bytenr - cache->key.objectid)); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2076 | |
| 2077 | spin_lock(&cache->space_info->lock); |
| 2078 | spin_lock(&cache->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2079 | if (reserve) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2080 | cache->reserved += len; |
| 2081 | cache->space_info->bytes_reserved += len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2082 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2083 | cache->reserved -= len; |
| 2084 | cache->space_info->bytes_reserved -= len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2085 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2086 | spin_unlock(&cache->lock); |
| 2087 | spin_unlock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2088 | bytenr += len; |
| 2089 | num -= len; |
| 2090 | } |
| 2091 | return 0; |
| 2092 | } |
| 2093 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2094 | 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] | 2095 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2096 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2097 | u64 start; |
| 2098 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2099 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2100 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2101 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2102 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2103 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2104 | ret = find_first_extent_bit(pinned_extents, last, |
| 2105 | &start, &end, EXTENT_DIRTY); |
| 2106 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2107 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2108 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 2109 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2110 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2111 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2112 | return 0; |
| 2113 | } |
| 2114 | |
| 2115 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 2116 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2117 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2118 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2119 | u64 start; |
| 2120 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2121 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2122 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2123 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2124 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2125 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 2126 | EXTENT_DIRTY); |
| 2127 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2128 | break; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2129 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2130 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2131 | if (need_resched()) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2132 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2133 | cond_resched(); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2134 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2135 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2136 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2137 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2138 | return 0; |
| 2139 | } |
| 2140 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2141 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2142 | struct btrfs_root *extent_root, int all) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2143 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2144 | u64 start; |
| 2145 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2146 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2147 | u64 search = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2148 | u64 skipped = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2149 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 2150 | struct btrfs_path *path; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2151 | struct pending_extent_op *extent_op, *tmp; |
| 2152 | struct list_head insert_list, update_list; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2153 | int ret; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2154 | int num_inserts = 0, max_inserts; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2155 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2156 | path = btrfs_alloc_path(); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2157 | INIT_LIST_HEAD(&insert_list); |
| 2158 | INIT_LIST_HEAD(&update_list); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2159 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2160 | max_inserts = extent_root->leafsize / |
| 2161 | (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) + |
| 2162 | sizeof(struct btrfs_extent_ref) + |
| 2163 | sizeof(struct btrfs_extent_item)); |
| 2164 | again: |
| 2165 | mutex_lock(&info->extent_ins_mutex); |
| 2166 | while (1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2167 | ret = find_first_extent_bit(&info->extent_ins, search, &start, |
| 2168 | &end, EXTENT_WRITEBACK); |
| 2169 | if (ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2170 | if (skipped && all && !num_inserts) { |
| 2171 | skipped = 0; |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame] | 2172 | search = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2173 | continue; |
| 2174 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2175 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2176 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS); |
| 2180 | if (!ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2181 | skipped = 1; |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2182 | search = end + 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2183 | if (need_resched()) { |
| 2184 | mutex_unlock(&info->extent_ins_mutex); |
| 2185 | cond_resched(); |
| 2186 | mutex_lock(&info->extent_ins_mutex); |
| 2187 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2188 | continue; |
| 2189 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2190 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2191 | ret = get_state_private(&info->extent_ins, start, &priv); |
| 2192 | BUG_ON(ret); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2193 | extent_op = (struct pending_extent_op *)(unsigned long) priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2194 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2195 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2196 | num_inserts++; |
| 2197 | list_add_tail(&extent_op->list, &insert_list); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2198 | search = end + 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2199 | if (num_inserts == max_inserts) { |
| 2200 | mutex_unlock(&info->extent_ins_mutex); |
| 2201 | break; |
| 2202 | } |
| 2203 | } else if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 2204 | list_add_tail(&extent_op->list, &update_list); |
| 2205 | search = end + 1; |
| 2206 | } else { |
| 2207 | BUG(); |
| 2208 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2209 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2210 | |
| 2211 | /* |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame] | 2212 | * process the update list, clear the writeback bit for it, and if |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2213 | * somebody marked this thing for deletion then just unlock it and be |
| 2214 | * done, the free_extents will handle it |
| 2215 | */ |
| 2216 | mutex_lock(&info->extent_ins_mutex); |
| 2217 | list_for_each_entry_safe(extent_op, tmp, &update_list, list) { |
| 2218 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, |
| 2219 | extent_op->bytenr + extent_op->num_bytes - 1, |
| 2220 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2221 | if (extent_op->del) { |
| 2222 | list_del_init(&extent_op->list); |
| 2223 | unlock_extent(&info->extent_ins, extent_op->bytenr, |
| 2224 | extent_op->bytenr + extent_op->num_bytes |
| 2225 | - 1, GFP_NOFS); |
| 2226 | kfree(extent_op); |
| 2227 | } |
| 2228 | } |
| 2229 | mutex_unlock(&info->extent_ins_mutex); |
| 2230 | |
| 2231 | /* |
| 2232 | * still have things left on the update list, go ahead an update |
| 2233 | * everything |
| 2234 | */ |
| 2235 | if (!list_empty(&update_list)) { |
| 2236 | ret = update_backrefs(trans, extent_root, path, &update_list); |
| 2237 | BUG_ON(ret); |
| 2238 | } |
| 2239 | |
| 2240 | /* |
| 2241 | * if no inserts need to be done, but we skipped some extents and we |
| 2242 | * need to make sure everything is cleaned then reset everything and |
| 2243 | * go back to the beginning |
| 2244 | */ |
| 2245 | if (!num_inserts && all && skipped) { |
| 2246 | search = 0; |
| 2247 | skipped = 0; |
| 2248 | INIT_LIST_HEAD(&update_list); |
| 2249 | INIT_LIST_HEAD(&insert_list); |
| 2250 | goto again; |
| 2251 | } else if (!num_inserts) { |
| 2252 | goto out; |
| 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * process the insert extents list. Again if we are deleting this |
| 2257 | * extent, then just unlock it, pin down the bytes if need be, and be |
| 2258 | * done with it. Saves us from having to actually insert the extent |
| 2259 | * into the tree and then subsequently come along and delete it |
| 2260 | */ |
| 2261 | mutex_lock(&info->extent_ins_mutex); |
| 2262 | list_for_each_entry_safe(extent_op, tmp, &insert_list, list) { |
| 2263 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, |
| 2264 | extent_op->bytenr + extent_op->num_bytes - 1, |
| 2265 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2266 | if (extent_op->del) { |
| 2267 | list_del_init(&extent_op->list); |
| 2268 | unlock_extent(&info->extent_ins, extent_op->bytenr, |
| 2269 | extent_op->bytenr + extent_op->num_bytes |
| 2270 | - 1, GFP_NOFS); |
| 2271 | |
| 2272 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 2273 | ret = pin_down_bytes(trans, extent_root, |
| 2274 | extent_op->bytenr, |
| 2275 | extent_op->num_bytes, 0); |
| 2276 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 2277 | |
| 2278 | ret = update_block_group(trans, extent_root, |
| 2279 | extent_op->bytenr, |
| 2280 | extent_op->num_bytes, |
| 2281 | 0, ret > 0); |
| 2282 | BUG_ON(ret); |
| 2283 | kfree(extent_op); |
| 2284 | num_inserts--; |
| 2285 | } |
| 2286 | } |
| 2287 | mutex_unlock(&info->extent_ins_mutex); |
| 2288 | |
| 2289 | ret = insert_extents(trans, extent_root, path, &insert_list, |
| 2290 | num_inserts); |
| 2291 | BUG_ON(ret); |
| 2292 | |
| 2293 | /* |
| 2294 | * if we broke out of the loop in order to insert stuff because we hit |
| 2295 | * the maximum number of inserts at a time we can handle, then loop |
| 2296 | * back and pick up where we left off |
| 2297 | */ |
| 2298 | if (num_inserts == max_inserts) { |
| 2299 | INIT_LIST_HEAD(&insert_list); |
| 2300 | INIT_LIST_HEAD(&update_list); |
| 2301 | num_inserts = 0; |
| 2302 | goto again; |
| 2303 | } |
| 2304 | |
| 2305 | /* |
| 2306 | * again, if we need to make absolutely sure there are no more pending |
| 2307 | * extent operations left and we know that we skipped some, go back to |
| 2308 | * the beginning and do it all again |
| 2309 | */ |
| 2310 | if (all && skipped) { |
| 2311 | INIT_LIST_HEAD(&insert_list); |
| 2312 | INIT_LIST_HEAD(&update_list); |
| 2313 | search = 0; |
| 2314 | skipped = 0; |
| 2315 | num_inserts = 0; |
| 2316 | goto again; |
| 2317 | } |
| 2318 | out: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2319 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2320 | return 0; |
| 2321 | } |
| 2322 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2323 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 2324 | struct btrfs_root *root, |
| 2325 | u64 bytenr, u64 num_bytes, int is_data) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2326 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2327 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2328 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2329 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2330 | if (is_data) |
| 2331 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2332 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2333 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 2334 | if (!buf) |
| 2335 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2336 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2337 | /* we can reuse a block if it hasn't been written |
| 2338 | * and it is from this transaction. We can't |
| 2339 | * reuse anything from the tree log root because |
| 2340 | * it has tiny sub-transactions. |
| 2341 | */ |
| 2342 | if (btrfs_buffer_uptodate(buf, 0) && |
| 2343 | btrfs_try_tree_lock(buf)) { |
| 2344 | u64 header_owner = btrfs_header_owner(buf); |
| 2345 | u64 header_transid = btrfs_header_generation(buf); |
| 2346 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2347 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2348 | header_transid == trans->transid && |
| 2349 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 2350 | clean_tree_block(NULL, root, buf); |
| 2351 | btrfs_tree_unlock(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2352 | free_extent_buffer(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2353 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2354 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2355 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2356 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2357 | free_extent_buffer(buf); |
| 2358 | pinit: |
| 2359 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 2360 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2361 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2362 | return 0; |
| 2363 | } |
| 2364 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2365 | /* |
| 2366 | * remove an extent from the root, returns 0 on success |
| 2367 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2368 | static int __free_extent(struct btrfs_trans_handle *trans, |
| 2369 | struct btrfs_root *root, |
| 2370 | u64 bytenr, u64 num_bytes, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2371 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2372 | u64 owner_objectid, int pin, int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2373 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2374 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2375 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2376 | struct btrfs_fs_info *info = root->fs_info; |
| 2377 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2378 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2379 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2380 | int extent_slot = 0; |
| 2381 | int found_extent = 0; |
| 2382 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2383 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2384 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2385 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2386 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2387 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2388 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2389 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2390 | if (!path) |
| 2391 | return -ENOMEM; |
| 2392 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2393 | path->reada = 1; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2394 | ret = lookup_extent_backref(trans, extent_root, path, |
| 2395 | bytenr, parent, root_objectid, |
| 2396 | ref_generation, owner_objectid, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2397 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2398 | struct btrfs_key found_key; |
| 2399 | extent_slot = path->slots[0]; |
| 2400 | while(extent_slot > 0) { |
| 2401 | extent_slot--; |
| 2402 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2403 | extent_slot); |
| 2404 | if (found_key.objectid != bytenr) |
| 2405 | break; |
| 2406 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2407 | found_key.offset == num_bytes) { |
| 2408 | found_extent = 1; |
| 2409 | break; |
| 2410 | } |
| 2411 | if (path->slots[0] - extent_slot > 5) |
| 2412 | break; |
| 2413 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2414 | if (!found_extent) { |
| 2415 | ret = remove_extent_backref(trans, extent_root, path); |
| 2416 | BUG_ON(ret); |
| 2417 | btrfs_release_path(extent_root, path); |
| 2418 | ret = btrfs_search_slot(trans, extent_root, |
| 2419 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2420 | if (ret) { |
| 2421 | printk(KERN_ERR "umm, got %d back from search" |
| 2422 | ", was looking for %Lu\n", ret, |
| 2423 | bytenr); |
| 2424 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2425 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2426 | BUG_ON(ret); |
| 2427 | extent_slot = path->slots[0]; |
| 2428 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2429 | } else { |
| 2430 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2431 | WARN_ON(1); |
| 2432 | printk("Unable to find ref byte nr %Lu root %Lu " |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2433 | "gen %Lu owner %Lu\n", bytenr, |
| 2434 | root_objectid, ref_generation, owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2435 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2436 | |
| 2437 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2438 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2439 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2440 | refs = btrfs_extent_refs(leaf, ei); |
| 2441 | BUG_ON(refs == 0); |
| 2442 | refs -= 1; |
| 2443 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2444 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2445 | btrfs_mark_buffer_dirty(leaf); |
| 2446 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2447 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2448 | struct btrfs_extent_ref *ref; |
| 2449 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 2450 | struct btrfs_extent_ref); |
| 2451 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2452 | /* if the back ref and the extent are next to each other |
| 2453 | * they get deleted below in one shot |
| 2454 | */ |
| 2455 | path->slots[0] = extent_slot; |
| 2456 | num_to_del = 2; |
| 2457 | } else if (found_extent) { |
| 2458 | /* otherwise delete the extent back ref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2459 | ret = remove_extent_backref(trans, extent_root, path); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2460 | BUG_ON(ret); |
| 2461 | /* if refs are 0, we need to setup the path for deletion */ |
| 2462 | if (refs == 0) { |
| 2463 | btrfs_release_path(extent_root, path); |
| 2464 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 2465 | -1, 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2466 | BUG_ON(ret); |
| 2467 | } |
| 2468 | } |
| 2469 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2470 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2471 | u64 super_used; |
| 2472 | u64 root_used; |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2473 | #ifdef BIO_RW_DISCARD |
| 2474 | u64 map_length = num_bytes; |
| 2475 | struct btrfs_multi_bio *multi = NULL; |
| 2476 | #endif |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2477 | |
| 2478 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2479 | mutex_lock(&root->fs_info->pinned_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2480 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, |
| 2481 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2482 | mutex_unlock(&root->fs_info->pinned_mutex); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 2483 | if (ret > 0) |
| 2484 | mark_free = 1; |
| 2485 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2486 | } |
| 2487 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2488 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2489 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2490 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2491 | btrfs_set_super_bytes_used(&info->super_copy, |
| 2492 | super_used - num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2493 | spin_unlock_irq(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2494 | |
| 2495 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2496 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2497 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2498 | root_used - num_bytes); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2499 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 2500 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2501 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2502 | btrfs_release_path(extent_root, path); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2503 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2504 | mark_free); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2505 | BUG_ON(ret); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2506 | |
| 2507 | #ifdef BIO_RW_DISCARD |
| 2508 | /* Tell the block device(s) that the sectors can be discarded */ |
| 2509 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 2510 | bytenr, &map_length, &multi, 0); |
| 2511 | if (!ret) { |
| 2512 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 2513 | int i; |
| 2514 | |
| 2515 | if (map_length > num_bytes) |
| 2516 | map_length = num_bytes; |
| 2517 | |
| 2518 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 2519 | btrfs_issue_discard(stripe->dev->bdev, |
| 2520 | stripe->physical, |
| 2521 | map_length); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2522 | } |
| 2523 | kfree(multi); |
| 2524 | } |
| 2525 | #endif |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2526 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2527 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2528 | finish_current_insert(trans, extent_root, 0); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2529 | return ret; |
| 2530 | } |
| 2531 | |
| 2532 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2533 | * find all the blocks marked as pending in the radix tree and remove |
| 2534 | * them from the extent map |
| 2535 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2536 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2537 | btrfs_root *extent_root, int all) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2538 | { |
| 2539 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2540 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2541 | u64 start; |
| 2542 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2543 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2544 | u64 search = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2545 | int nr = 0, skipped = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2546 | struct extent_io_tree *pending_del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2547 | struct extent_io_tree *extent_ins; |
| 2548 | struct pending_extent_op *extent_op; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2549 | struct btrfs_fs_info *info = extent_root->fs_info; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2550 | struct list_head delete_list; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2551 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2552 | INIT_LIST_HEAD(&delete_list); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2553 | extent_ins = &extent_root->fs_info->extent_ins; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2554 | pending_del = &extent_root->fs_info->pending_del; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2555 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2556 | again: |
| 2557 | mutex_lock(&info->extent_ins_mutex); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2558 | while(1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2559 | ret = find_first_extent_bit(pending_del, search, &start, &end, |
| 2560 | EXTENT_WRITEBACK); |
| 2561 | if (ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2562 | if (all && skipped && !nr) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2563 | search = 0; |
| 2564 | continue; |
| 2565 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2566 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2567 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | ret = try_lock_extent(extent_ins, start, end, GFP_NOFS); |
| 2571 | if (!ret) { |
| 2572 | search = end+1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2573 | skipped = 1; |
| 2574 | |
| 2575 | if (need_resched()) { |
| 2576 | mutex_unlock(&info->extent_ins_mutex); |
| 2577 | cond_resched(); |
| 2578 | mutex_lock(&info->extent_ins_mutex); |
| 2579 | } |
| 2580 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2581 | continue; |
| 2582 | } |
| 2583 | BUG_ON(ret < 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2584 | |
| 2585 | ret = get_state_private(pending_del, start, &priv); |
| 2586 | BUG_ON(ret); |
| 2587 | extent_op = (struct pending_extent_op *)(unsigned long)priv; |
| 2588 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2589 | clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2590 | GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2591 | if (!test_range_bit(extent_ins, start, end, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2592 | EXTENT_WRITEBACK, 0)) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2593 | list_add_tail(&extent_op->list, &delete_list); |
| 2594 | nr++; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2595 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2596 | kfree(extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2597 | |
| 2598 | ret = get_state_private(&info->extent_ins, start, |
| 2599 | &priv); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2600 | BUG_ON(ret); |
| 2601 | extent_op = (struct pending_extent_op *) |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2602 | (unsigned long)priv; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2603 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2604 | clear_extent_bits(&info->extent_ins, start, end, |
| 2605 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2606 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2607 | if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 2608 | list_add_tail(&extent_op->list, &delete_list); |
| 2609 | search = end + 1; |
| 2610 | nr++; |
| 2611 | continue; |
| 2612 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2613 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2614 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 2615 | ret = pin_down_bytes(trans, extent_root, start, |
| 2616 | end + 1 - start, 0); |
| 2617 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 2618 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2619 | ret = update_block_group(trans, extent_root, start, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2620 | end + 1 - start, 0, ret > 0); |
| 2621 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2622 | unlock_extent(extent_ins, start, end, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2623 | BUG_ON(ret); |
| 2624 | kfree(extent_op); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2625 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2626 | if (ret) |
| 2627 | err = ret; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2628 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2629 | search = end + 1; |
| 2630 | |
| 2631 | if (need_resched()) { |
| 2632 | mutex_unlock(&info->extent_ins_mutex); |
| 2633 | cond_resched(); |
| 2634 | mutex_lock(&info->extent_ins_mutex); |
| 2635 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2636 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2637 | |
| 2638 | if (nr) { |
| 2639 | ret = free_extents(trans, extent_root, &delete_list); |
| 2640 | BUG_ON(ret); |
| 2641 | } |
| 2642 | |
| 2643 | if (all && skipped) { |
| 2644 | INIT_LIST_HEAD(&delete_list); |
| 2645 | search = 0; |
| 2646 | nr = 0; |
| 2647 | goto again; |
| 2648 | } |
| 2649 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2650 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | /* |
| 2654 | * remove an extent from the root, returns 0 on success |
| 2655 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2656 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2657 | struct btrfs_root *root, |
| 2658 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2659 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2660 | u64 owner_objectid, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2661 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2662 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2663 | int pending_ret; |
| 2664 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2665 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2666 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2667 | if (root == extent_root) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2668 | struct pending_extent_op *extent_op = NULL; |
| 2669 | |
| 2670 | mutex_lock(&root->fs_info->extent_ins_mutex); |
| 2671 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
| 2672 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
| 2673 | u64 priv; |
| 2674 | ret = get_state_private(&root->fs_info->extent_ins, |
| 2675 | bytenr, &priv); |
| 2676 | BUG_ON(ret); |
| 2677 | extent_op = (struct pending_extent_op *) |
| 2678 | (unsigned long)priv; |
| 2679 | |
| 2680 | extent_op->del = 1; |
| 2681 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
| 2682 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
| 2683 | return 0; |
| 2684 | } |
| 2685 | } |
| 2686 | |
| 2687 | if (extent_op) { |
| 2688 | ref_generation = extent_op->orig_generation; |
| 2689 | parent = extent_op->orig_parent; |
| 2690 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2691 | |
| 2692 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2693 | BUG_ON(!extent_op); |
| 2694 | |
| 2695 | extent_op->type = PENDING_EXTENT_DELETE; |
| 2696 | extent_op->bytenr = bytenr; |
| 2697 | extent_op->num_bytes = num_bytes; |
| 2698 | extent_op->parent = parent; |
| 2699 | extent_op->orig_parent = parent; |
| 2700 | extent_op->generation = ref_generation; |
| 2701 | extent_op->orig_generation = ref_generation; |
| 2702 | extent_op->level = (int)owner_objectid; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2703 | INIT_LIST_HEAD(&extent_op->list); |
| 2704 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2705 | |
| 2706 | set_extent_bits(&root->fs_info->pending_del, |
| 2707 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2708 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2709 | set_state_private(&root->fs_info->pending_del, |
| 2710 | bytenr, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2711 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2712 | return 0; |
| 2713 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2714 | /* if metadata always pin */ |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2715 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2716 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2717 | struct btrfs_block_group_cache *cache; |
| 2718 | |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2719 | /* btrfs_free_reserved_extent */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2720 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2721 | BUG_ON(!cache); |
| 2722 | btrfs_add_free_space(cache, bytenr, num_bytes); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2723 | update_reserved_extents(root, bytenr, num_bytes, 0); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2724 | return 0; |
| 2725 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2726 | pin = 1; |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2727 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2728 | |
| 2729 | /* if data pin when any transaction has committed this */ |
| 2730 | if (ref_generation != trans->transid) |
| 2731 | pin = 1; |
| 2732 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2733 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2734 | root_objectid, ref_generation, |
| 2735 | owner_objectid, pin, pin == 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 2736 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2737 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 2738 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2739 | return ret ? ret : pending_ret; |
| 2740 | } |
| 2741 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2742 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2743 | struct btrfs_root *root, |
| 2744 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2745 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2746 | u64 owner_objectid, int pin) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2747 | { |
| 2748 | int ret; |
| 2749 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2750 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2751 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2752 | owner_objectid, pin); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2753 | return ret; |
| 2754 | } |
| 2755 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 2756 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 2757 | { |
| 2758 | u64 mask = ((u64)root->stripesize - 1); |
| 2759 | u64 ret = (val + mask) & ~mask; |
| 2760 | return ret; |
| 2761 | } |
| 2762 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2763 | /* |
| 2764 | * walks the btree of allocated extents and find a hole of a given size. |
| 2765 | * The key ins is changed to record the hole: |
| 2766 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2767 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2768 | * ins->offset == number of blocks |
| 2769 | * Any available blocks before search_start are skipped. |
| 2770 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2771 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
| 2772 | struct btrfs_root *orig_root, |
| 2773 | u64 num_bytes, u64 empty_size, |
| 2774 | u64 search_start, u64 search_end, |
| 2775 | u64 hint_byte, struct btrfs_key *ins, |
| 2776 | u64 exclude_start, u64 exclude_nr, |
| 2777 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2778 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2779 | int ret = 0; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2780 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2781 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2782 | u64 *last_ptr = NULL; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2783 | u64 last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2784 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2785 | int chunk_alloc_done = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2786 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2787 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2788 | struct list_head *head = NULL, *cur = NULL; |
| 2789 | int loop = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2790 | int extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2791 | struct btrfs_space_info *space_info; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2792 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2793 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2794 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2795 | ins->objectid = 0; |
| 2796 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 2797 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2798 | if (orig_root->ref_cows || empty_size) |
| 2799 | allowed_chunk_alloc = 1; |
| 2800 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2801 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 2802 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2803 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2804 | } |
| 2805 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2806 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2807 | last_ptr = &root->fs_info->last_data_alloc; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2808 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2809 | if (last_ptr) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2810 | if (*last_ptr) { |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2811 | hint_byte = *last_ptr; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2812 | last_wanted = *last_ptr; |
| 2813 | } else |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2814 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2815 | } else { |
| 2816 | empty_cluster = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2817 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2818 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 2819 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2820 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2821 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2822 | last_wanted = 0; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2823 | empty_size += empty_cluster; |
| 2824 | } |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2825 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2826 | total_needed += empty_size; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2827 | block_group = btrfs_lookup_block_group(root->fs_info, search_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 2828 | if (!block_group) |
| 2829 | block_group = btrfs_lookup_first_block_group(root->fs_info, |
| 2830 | search_start); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2831 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2832 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2833 | down_read(&space_info->groups_sem); |
| 2834 | while (1) { |
| 2835 | struct btrfs_free_space *free_space; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2836 | /* |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2837 | * the only way this happens if our hint points to a block |
| 2838 | * group thats not of the proper type, while looping this |
| 2839 | * should never happen |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2840 | */ |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2841 | if (empty_size) |
| 2842 | extra_loop = 1; |
| 2843 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2844 | if (!block_group) |
| 2845 | goto new_group_no_lock; |
| 2846 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 2847 | if (unlikely(!block_group->cached)) { |
| 2848 | mutex_lock(&block_group->cache_mutex); |
| 2849 | ret = cache_block_group(root, block_group); |
| 2850 | mutex_unlock(&block_group->cache_mutex); |
| 2851 | if (ret) |
| 2852 | break; |
| 2853 | } |
| 2854 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2855 | mutex_lock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2856 | if (unlikely(!block_group_bits(block_group, data))) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2857 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2858 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 2859 | if (unlikely(block_group->ro)) |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2860 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2861 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2862 | free_space = btrfs_find_free_space(block_group, search_start, |
| 2863 | total_needed); |
| 2864 | if (free_space) { |
| 2865 | u64 start = block_group->key.objectid; |
| 2866 | u64 end = block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2867 | block_group->key.offset; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2868 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2869 | search_start = stripe_align(root, free_space->offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2870 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2871 | /* move on to the next group */ |
| 2872 | if (search_start + num_bytes >= search_end) |
| 2873 | goto new_group; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2874 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2875 | /* move on to the next group */ |
| 2876 | if (search_start + num_bytes > end) |
| 2877 | goto new_group; |
| 2878 | |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2879 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 2880 | total_needed += empty_cluster; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 2881 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2882 | last_wanted = 0; |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 2883 | /* |
| 2884 | * if search_start is still in this block group |
| 2885 | * then we just re-search this block group |
| 2886 | */ |
| 2887 | if (search_start >= start && |
| 2888 | search_start < end) { |
| 2889 | mutex_unlock(&block_group->alloc_mutex); |
| 2890 | continue; |
| 2891 | } |
| 2892 | |
| 2893 | /* else we go to the next block group */ |
| 2894 | goto new_group; |
| 2895 | } |
| 2896 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2897 | if (exclude_nr > 0 && |
| 2898 | (search_start + num_bytes > exclude_start && |
| 2899 | search_start < exclude_start + exclude_nr)) { |
| 2900 | search_start = exclude_start + exclude_nr; |
| 2901 | /* |
| 2902 | * if search_start is still in this block group |
| 2903 | * then we just re-search this block group |
| 2904 | */ |
| 2905 | if (search_start >= start && |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2906 | search_start < end) { |
| 2907 | mutex_unlock(&block_group->alloc_mutex); |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2908 | last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2909 | continue; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2910 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2911 | |
| 2912 | /* else we go to the next block group */ |
| 2913 | goto new_group; |
| 2914 | } |
| 2915 | |
| 2916 | ins->objectid = search_start; |
| 2917 | ins->offset = num_bytes; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2918 | |
| 2919 | btrfs_remove_free_space_lock(block_group, search_start, |
| 2920 | num_bytes); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2921 | /* we are all good, lets return */ |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2922 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2923 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2924 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2925 | new_group: |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2926 | mutex_unlock(&block_group->alloc_mutex); |
| 2927 | new_group_no_lock: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2928 | /* don't try to compare new allocations against the |
| 2929 | * last allocation any more |
| 2930 | */ |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 2931 | last_wanted = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2932 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2933 | /* |
| 2934 | * Here's how this works. |
| 2935 | * loop == 0: we were searching a block group via a hint |
| 2936 | * and didn't find anything, so we start at |
| 2937 | * the head of the block groups and keep searching |
| 2938 | * loop == 1: we're searching through all of the block groups |
| 2939 | * if we hit the head again we have searched |
| 2940 | * all of the block groups for this space and we |
| 2941 | * need to try and allocate, if we cant error out. |
| 2942 | * loop == 2: we allocated more space and are looping through |
| 2943 | * all of the block groups again. |
| 2944 | */ |
| 2945 | if (loop == 0) { |
| 2946 | head = &space_info->block_groups; |
| 2947 | cur = head->next; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2948 | loop++; |
| 2949 | } else if (loop == 1 && cur == head) { |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2950 | int keep_going; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2951 | |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2952 | /* at this point we give up on the empty_size |
| 2953 | * allocations and just try to allocate the min |
| 2954 | * space. |
| 2955 | * |
| 2956 | * The extra_loop field was set if an empty_size |
| 2957 | * allocation was attempted above, and if this |
| 2958 | * is try we need to try the loop again without |
| 2959 | * the additional empty_size. |
| 2960 | */ |
Chris Mason | 5b7c3fc | 2008-11-10 07:26:33 -0500 | [diff] [blame] | 2961 | total_needed -= empty_size; |
| 2962 | empty_size = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2963 | keep_going = extra_loop; |
| 2964 | loop++; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 2965 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2966 | if (allowed_chunk_alloc && !chunk_alloc_done) { |
| 2967 | up_read(&space_info->groups_sem); |
| 2968 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 2969 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2970 | down_read(&space_info->groups_sem); |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 2971 | if (ret < 0) |
| 2972 | goto loop_check; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2973 | head = &space_info->block_groups; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2974 | /* |
| 2975 | * we've allocated a new chunk, keep |
| 2976 | * trying |
| 2977 | */ |
| 2978 | keep_going = 1; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2979 | chunk_alloc_done = 1; |
| 2980 | } else if (!allowed_chunk_alloc) { |
| 2981 | space_info->force_alloc = 1; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2982 | } |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 2983 | loop_check: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 2984 | if (keep_going) { |
| 2985 | cur = head->next; |
| 2986 | extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2987 | } else { |
| 2988 | break; |
| 2989 | } |
| 2990 | } else if (cur == head) { |
| 2991 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2992 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2993 | |
| 2994 | block_group = list_entry(cur, struct btrfs_block_group_cache, |
| 2995 | list); |
| 2996 | search_start = block_group->key.objectid; |
| 2997 | cur = cur->next; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 2998 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2999 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3000 | /* we found what we needed */ |
| 3001 | if (ins->objectid) { |
| 3002 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
| 3003 | trans->block_group = block_group; |
| 3004 | |
| 3005 | if (last_ptr) |
| 3006 | *last_ptr = ins->objectid + ins->offset; |
| 3007 | ret = 0; |
| 3008 | } else if (!ret) { |
Josef Bacik | 4ce4cb5 | 2008-11-17 21:12:00 -0500 | [diff] [blame] | 3009 | printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu," |
| 3010 | " loop %d, allowed_alloc %d\n", total_needed, num_bytes, |
| 3011 | loop, allowed_chunk_alloc); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3012 | ret = -ENOSPC; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 3013 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3014 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3015 | up_read(&space_info->groups_sem); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 3016 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3017 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3018 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3019 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 3020 | { |
| 3021 | struct btrfs_block_group_cache *cache; |
| 3022 | struct list_head *l; |
| 3023 | |
| 3024 | printk(KERN_INFO "space_info has %Lu free, is %sfull\n", |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3025 | info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 3026 | info->bytes_reserved, (info->full) ? "" : "not "); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3027 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3028 | down_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3029 | list_for_each(l, &info->block_groups) { |
| 3030 | cache = list_entry(l, struct btrfs_block_group_cache, list); |
| 3031 | spin_lock(&cache->lock); |
| 3032 | printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used " |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3033 | "%Lu pinned %Lu reserved\n", |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3034 | cache->key.objectid, cache->key.offset, |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3035 | btrfs_block_group_used(&cache->item), |
| 3036 | cache->pinned, cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3037 | btrfs_dump_free_space(cache, bytes); |
| 3038 | spin_unlock(&cache->lock); |
| 3039 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3040 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3041 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3042 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3043 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3044 | struct btrfs_root *root, |
| 3045 | u64 num_bytes, u64 min_alloc_size, |
| 3046 | u64 empty_size, u64 hint_byte, |
| 3047 | u64 search_end, struct btrfs_key *ins, |
| 3048 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3049 | { |
| 3050 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 3051 | u64 search_start = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3052 | u64 alloc_profile; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3053 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3054 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3055 | if (data) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3056 | alloc_profile = info->avail_data_alloc_bits & |
| 3057 | info->data_alloc_profile; |
| 3058 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3059 | } else if (root == root->fs_info->chunk_root) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3060 | alloc_profile = info->avail_system_alloc_bits & |
| 3061 | info->system_alloc_profile; |
| 3062 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3063 | } else { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3064 | alloc_profile = info->avail_metadata_alloc_bits & |
| 3065 | info->metadata_alloc_profile; |
| 3066 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3067 | } |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3068 | again: |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3069 | data = btrfs_reduce_alloc_profile(root, data); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3070 | /* |
| 3071 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 3072 | * is not called recursively on allocations |
| 3073 | */ |
| 3074 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3075 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3076 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3077 | 2 * 1024 * 1024, |
| 3078 | BTRFS_BLOCK_GROUP_METADATA | |
| 3079 | (info->metadata_alloc_profile & |
| 3080 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3081 | } |
| 3082 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3083 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3084 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3085 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3086 | WARN_ON(num_bytes < root->sectorsize); |
| 3087 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 3088 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3089 | trans->alloc_exclude_start, |
| 3090 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3091 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3092 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 3093 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3094 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3095 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3096 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3097 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3098 | goto again; |
| 3099 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3100 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3101 | struct btrfs_space_info *sinfo; |
| 3102 | |
| 3103 | sinfo = __find_space_info(root->fs_info, data); |
| 3104 | printk("allocation failed flags %Lu, wanted %Lu\n", |
| 3105 | data, num_bytes); |
| 3106 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3107 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3108 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3109 | |
| 3110 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3111 | } |
| 3112 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3113 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 3114 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3115 | struct btrfs_block_group_cache *cache; |
| 3116 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3117 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 3118 | if (!cache) { |
| 3119 | printk(KERN_ERR "Unable to find block group for %Lu\n", start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3120 | return -ENOSPC; |
| 3121 | } |
| 3122 | btrfs_add_free_space(cache, start, len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3123 | update_reserved_extents(root, start, len, 0); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3124 | return 0; |
| 3125 | } |
| 3126 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3127 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3128 | struct btrfs_root *root, |
| 3129 | u64 num_bytes, u64 min_alloc_size, |
| 3130 | u64 empty_size, u64 hint_byte, |
| 3131 | u64 search_end, struct btrfs_key *ins, |
| 3132 | u64 data) |
| 3133 | { |
| 3134 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3135 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 3136 | empty_size, hint_byte, search_end, ins, |
| 3137 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3138 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3139 | return ret; |
| 3140 | } |
| 3141 | |
| 3142 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3143 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3144 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3145 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3146 | { |
| 3147 | int ret; |
| 3148 | int pending_ret; |
| 3149 | u64 super_used; |
| 3150 | u64 root_used; |
| 3151 | u64 num_bytes = ins->offset; |
| 3152 | u32 sizes[2]; |
| 3153 | struct btrfs_fs_info *info = root->fs_info; |
| 3154 | struct btrfs_root *extent_root = info->extent_root; |
| 3155 | struct btrfs_extent_item *extent_item; |
| 3156 | struct btrfs_extent_ref *ref; |
| 3157 | struct btrfs_path *path; |
| 3158 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 3159 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3160 | if (parent == 0) |
| 3161 | parent = ins->objectid; |
| 3162 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3163 | /* block accounting for super block */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3164 | spin_lock_irq(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3165 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 3166 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3167 | spin_unlock_irq(&info->delalloc_lock); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3168 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3169 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3170 | root_used = btrfs_root_used(&root->root_item); |
| 3171 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3172 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3173 | if (root == extent_root) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3174 | struct pending_extent_op *extent_op; |
| 3175 | |
| 3176 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 3177 | BUG_ON(!extent_op); |
| 3178 | |
| 3179 | extent_op->type = PENDING_EXTENT_INSERT; |
| 3180 | extent_op->bytenr = ins->objectid; |
| 3181 | extent_op->num_bytes = ins->offset; |
| 3182 | extent_op->parent = parent; |
| 3183 | extent_op->orig_parent = 0; |
| 3184 | extent_op->generation = ref_generation; |
| 3185 | extent_op->orig_generation = 0; |
| 3186 | extent_op->level = (int)owner; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3187 | INIT_LIST_HEAD(&extent_op->list); |
| 3188 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3189 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3190 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3191 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 3192 | ins->objectid + ins->offset - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3193 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3194 | set_state_private(&root->fs_info->extent_ins, |
| 3195 | ins->objectid, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3196 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3197 | goto update_block; |
| 3198 | } |
| 3199 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3200 | memcpy(&keys[0], ins, sizeof(*ins)); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3201 | keys[1].objectid = ins->objectid; |
| 3202 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3203 | keys[1].offset = parent; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3204 | sizes[0] = sizeof(*extent_item); |
| 3205 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3206 | |
| 3207 | path = btrfs_alloc_path(); |
| 3208 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3209 | |
| 3210 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 3211 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3212 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3213 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3214 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3215 | struct btrfs_extent_item); |
| 3216 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); |
| 3217 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 3218 | struct btrfs_extent_ref); |
| 3219 | |
| 3220 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 3221 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 3222 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3223 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3224 | |
| 3225 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3226 | |
| 3227 | trans->alloc_exclude_start = 0; |
| 3228 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3229 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 3230 | finish_current_insert(trans, extent_root, 0); |
| 3231 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3232 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3233 | if (ret) |
| 3234 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3235 | if (pending_ret) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3236 | ret = pending_ret; |
| 3237 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3238 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3239 | |
| 3240 | update_block: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3241 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 3242 | if (ret) { |
| 3243 | printk("update block group failed for %Lu %Lu\n", |
| 3244 | ins->objectid, ins->offset); |
| 3245 | BUG(); |
| 3246 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3247 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3248 | return ret; |
| 3249 | } |
| 3250 | |
| 3251 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3252 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3253 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3254 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3255 | { |
| 3256 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 3257 | |
| 3258 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 3259 | return 0; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3260 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 3261 | ref_generation, owner, ins); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3262 | update_reserved_extents(root, ins->objectid, ins->offset, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3263 | return ret; |
| 3264 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3265 | |
| 3266 | /* |
| 3267 | * this is used by the tree logging recovery code. It records that |
| 3268 | * an extent has been allocated and makes sure to clear the free |
| 3269 | * space cache bits as well |
| 3270 | */ |
| 3271 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3272 | struct btrfs_root *root, u64 parent, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3273 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3274 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3275 | { |
| 3276 | int ret; |
| 3277 | struct btrfs_block_group_cache *block_group; |
| 3278 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3279 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3280 | mutex_lock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3281 | cache_block_group(root, block_group); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3282 | mutex_unlock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3283 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3284 | ret = btrfs_remove_free_space(block_group, ins->objectid, |
| 3285 | ins->offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3286 | BUG_ON(ret); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3287 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 3288 | ref_generation, owner, ins); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3289 | return ret; |
| 3290 | } |
| 3291 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3292 | /* |
| 3293 | * finds a free extent and does all the dirty work required for allocation |
| 3294 | * returns the key for the extent through ins, and a tree buffer for |
| 3295 | * the first block of the extent through buf. |
| 3296 | * |
| 3297 | * returns 0 if everything worked, non-zero otherwise. |
| 3298 | */ |
| 3299 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 3300 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3301 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3302 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3303 | u64 owner_objectid, u64 empty_size, u64 hint_byte, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3304 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 3305 | { |
| 3306 | int ret; |
| 3307 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3308 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 3309 | min_alloc_size, empty_size, hint_byte, |
| 3310 | search_end, ins, data); |
| 3311 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3312 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3313 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
| 3314 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3315 | owner_objectid, ins); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3316 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3317 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3318 | } else { |
| 3319 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3320 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3321 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3322 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3323 | |
| 3324 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 3325 | struct btrfs_root *root, |
| 3326 | u64 bytenr, u32 blocksize) |
| 3327 | { |
| 3328 | struct extent_buffer *buf; |
| 3329 | |
| 3330 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 3331 | if (!buf) |
| 3332 | return ERR_PTR(-ENOMEM); |
| 3333 | btrfs_set_header_generation(buf, trans->transid); |
| 3334 | btrfs_tree_lock(buf); |
| 3335 | clean_tree_block(trans, root, buf); |
| 3336 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3337 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 3338 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3339 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3340 | } else { |
| 3341 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 3342 | buf->start + buf->len - 1, GFP_NOFS); |
| 3343 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3344 | trans->blocks_used++; |
| 3345 | return buf; |
| 3346 | } |
| 3347 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3348 | /* |
| 3349 | * helper function to allocate a block for a given tree |
| 3350 | * returns the tree buffer or NULL. |
| 3351 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3352 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3353 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3354 | u32 blocksize, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3355 | u64 root_objectid, |
| 3356 | u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3357 | int level, |
| 3358 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3359 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3360 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3361 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3362 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3363 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3364 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3365 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3366 | root_objectid, ref_generation, level, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3367 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3368 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3369 | BUG_ON(ret > 0); |
| 3370 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3371 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 3372 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3373 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3374 | return buf; |
| 3375 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3376 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3377 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3378 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3379 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3380 | u64 leaf_owner; |
| 3381 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3382 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3383 | struct btrfs_file_extent_item *fi; |
| 3384 | int i; |
| 3385 | int nritems; |
| 3386 | int ret; |
| 3387 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3388 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 3389 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3390 | leaf_owner = btrfs_header_owner(leaf); |
| 3391 | leaf_generation = btrfs_header_generation(leaf); |
| 3392 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3393 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3394 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 3395 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3396 | |
| 3397 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 3398 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3399 | continue; |
| 3400 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3401 | if (btrfs_file_extent_type(leaf, fi) == |
| 3402 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 3403 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3404 | /* |
| 3405 | * FIXME make sure to insert a trans record that |
| 3406 | * repeats the snapshot del on crash |
| 3407 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3408 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 3409 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 3410 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3411 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3412 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3413 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3414 | leaf->start, leaf_owner, leaf_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3415 | key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3416 | BUG_ON(ret); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3417 | |
| 3418 | atomic_inc(&root->fs_info->throttle_gen); |
| 3419 | wake_up(&root->fs_info->transaction_throttle); |
| 3420 | cond_resched(); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3421 | } |
| 3422 | return 0; |
| 3423 | } |
| 3424 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3425 | static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3426 | struct btrfs_root *root, |
| 3427 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3428 | { |
| 3429 | int i; |
| 3430 | int ret; |
| 3431 | struct btrfs_extent_info *info = ref->extents; |
| 3432 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3433 | for (i = 0; i < ref->nritems; i++) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3434 | ret = __btrfs_free_extent(trans, root, info->bytenr, |
| 3435 | info->num_bytes, ref->bytenr, |
| 3436 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3437 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3438 | |
| 3439 | atomic_inc(&root->fs_info->throttle_gen); |
| 3440 | wake_up(&root->fs_info->transaction_throttle); |
| 3441 | cond_resched(); |
| 3442 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3443 | BUG_ON(ret); |
| 3444 | info++; |
| 3445 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3446 | |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 3450 | static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len, |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3451 | u32 *refs) |
| 3452 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3453 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3454 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3455 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3456 | BUG_ON(ret); |
| 3457 | |
| 3458 | #if 0 // some debugging code in case we see problems here |
| 3459 | /* if the refs count is one, it won't get increased again. But |
| 3460 | * if the ref count is > 1, someone may be decreasing it at |
| 3461 | * the same time we are. |
| 3462 | */ |
| 3463 | if (*refs != 1) { |
| 3464 | struct extent_buffer *eb = NULL; |
| 3465 | eb = btrfs_find_create_tree_block(root, start, len); |
| 3466 | if (eb) |
| 3467 | btrfs_tree_lock(eb); |
| 3468 | |
| 3469 | mutex_lock(&root->fs_info->alloc_mutex); |
| 3470 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 3471 | BUG_ON(ret); |
| 3472 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 3473 | |
| 3474 | if (eb) { |
| 3475 | btrfs_tree_unlock(eb); |
| 3476 | free_extent_buffer(eb); |
| 3477 | } |
| 3478 | if (*refs == 1) { |
| 3479 | printk("block %llu went down to one during drop_snap\n", |
| 3480 | (unsigned long long)start); |
| 3481 | } |
| 3482 | |
| 3483 | } |
| 3484 | #endif |
| 3485 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3486 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3487 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3491 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 3492 | * counts as it goes. |
| 3493 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3494 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
| 3495 | struct btrfs_root *root, |
| 3496 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3497 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3498 | u64 root_owner; |
| 3499 | u64 root_gen; |
| 3500 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3501 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3502 | struct extent_buffer *next; |
| 3503 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3504 | struct extent_buffer *parent; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3505 | struct btrfs_leaf_ref *ref; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3506 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3507 | int ret; |
| 3508 | u32 refs; |
| 3509 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3510 | WARN_ON(*level < 0); |
| 3511 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3512 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3513 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3514 | BUG_ON(ret); |
| 3515 | if (refs > 1) |
| 3516 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3517 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3518 | /* |
| 3519 | * walk down to the last node level and free all the leaves |
| 3520 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3521 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3522 | WARN_ON(*level < 0); |
| 3523 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3524 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3525 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3526 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 3527 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 3528 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3529 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3530 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3531 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3532 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3533 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3534 | BUG_ON(ret); |
| 3535 | break; |
| 3536 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3537 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3538 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3539 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3540 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3541 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3542 | BUG_ON(ret); |
| 3543 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3544 | parent = path->nodes[*level]; |
| 3545 | root_owner = btrfs_header_owner(parent); |
| 3546 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3547 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3548 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3549 | ret = __btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3550 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3551 | root_owner, root_gen, |
| 3552 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3553 | BUG_ON(ret); |
Chris Mason | 18e35e0 | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3554 | |
| 3555 | atomic_inc(&root->fs_info->throttle_gen); |
| 3556 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3557 | cond_resched(); |
Chris Mason | 18e35e0 | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3558 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3559 | continue; |
| 3560 | } |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3561 | /* |
| 3562 | * at this point, we have a single ref, and since the |
| 3563 | * only place referencing this extent is a dead root |
| 3564 | * the reference count should never go higher. |
| 3565 | * So, we don't need to check it again |
| 3566 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3567 | if (*level == 1) { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3568 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3569 | if (ref && ref->generation != ptr_gen) { |
| 3570 | btrfs_free_leaf_ref(root, ref); |
| 3571 | ref = NULL; |
| 3572 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3573 | if (ref) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3574 | ret = cache_drop_leaf_ref(trans, root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3575 | BUG_ON(ret); |
| 3576 | btrfs_remove_leaf_ref(root, ref); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 3577 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3578 | *level = 0; |
| 3579 | break; |
| 3580 | } |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3581 | if (printk_ratelimit()) { |
Chris Mason | 37d1aee | 2008-07-31 10:48:37 -0400 | [diff] [blame] | 3582 | printk("leaf ref miss for bytenr %llu\n", |
| 3583 | (unsigned long long)bytenr); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3584 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3585 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3586 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
Chris Mason | 1259ab7 | 2008-05-12 13:39:03 -0400 | [diff] [blame] | 3587 | if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3588 | free_extent_buffer(next); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3589 | |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3590 | next = read_tree_block(root, bytenr, blocksize, |
| 3591 | ptr_gen); |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3592 | cond_resched(); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3593 | #if 0 |
| 3594 | /* |
| 3595 | * this is a debugging check and can go away |
| 3596 | * the ref should never go all the way down to 1 |
| 3597 | * at this point |
| 3598 | */ |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3599 | ret = lookup_extent_ref(NULL, root, bytenr, blocksize, |
| 3600 | &refs); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 3601 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3602 | WARN_ON(refs != 1); |
| 3603 | #endif |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 3604 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3605 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3606 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3607 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3608 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3609 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3610 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3611 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3612 | } |
| 3613 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3614 | WARN_ON(*level < 0); |
| 3615 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3616 | |
| 3617 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3618 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3619 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3620 | } else { |
| 3621 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3622 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3623 | } |
| 3624 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3625 | blocksize = btrfs_level_size(root, *level); |
| 3626 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3627 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3628 | |
| 3629 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3630 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3631 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3632 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3633 | path->nodes[*level] = NULL; |
| 3634 | *level += 1; |
| 3635 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3636 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3637 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3638 | return 0; |
| 3639 | } |
| 3640 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3641 | /* |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3642 | * helper function for drop_subtree, this function is similar to |
| 3643 | * walk_down_tree. The main difference is that it checks reference |
| 3644 | * counts while tree blocks are locked. |
| 3645 | */ |
| 3646 | static int noinline walk_down_subtree(struct btrfs_trans_handle *trans, |
| 3647 | struct btrfs_root *root, |
| 3648 | struct btrfs_path *path, int *level) |
| 3649 | { |
| 3650 | struct extent_buffer *next; |
| 3651 | struct extent_buffer *cur; |
| 3652 | struct extent_buffer *parent; |
| 3653 | u64 bytenr; |
| 3654 | u64 ptr_gen; |
| 3655 | u32 blocksize; |
| 3656 | u32 refs; |
| 3657 | int ret; |
| 3658 | |
| 3659 | cur = path->nodes[*level]; |
| 3660 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, |
| 3661 | &refs); |
| 3662 | BUG_ON(ret); |
| 3663 | if (refs > 1) |
| 3664 | goto out; |
| 3665 | |
| 3666 | while (*level >= 0) { |
| 3667 | cur = path->nodes[*level]; |
| 3668 | if (*level == 0) { |
| 3669 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
| 3670 | BUG_ON(ret); |
| 3671 | clean_tree_block(trans, root, cur); |
| 3672 | break; |
| 3673 | } |
| 3674 | if (path->slots[*level] >= btrfs_header_nritems(cur)) { |
| 3675 | clean_tree_block(trans, root, cur); |
| 3676 | break; |
| 3677 | } |
| 3678 | |
| 3679 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 3680 | blocksize = btrfs_level_size(root, *level - 1); |
| 3681 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
| 3682 | |
| 3683 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
| 3684 | btrfs_tree_lock(next); |
| 3685 | |
| 3686 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, |
| 3687 | &refs); |
| 3688 | BUG_ON(ret); |
| 3689 | if (refs > 1) { |
| 3690 | parent = path->nodes[*level]; |
| 3691 | ret = btrfs_free_extent(trans, root, bytenr, |
| 3692 | blocksize, parent->start, |
| 3693 | btrfs_header_owner(parent), |
| 3694 | btrfs_header_generation(parent), |
| 3695 | *level - 1, 1); |
| 3696 | BUG_ON(ret); |
| 3697 | path->slots[*level]++; |
| 3698 | btrfs_tree_unlock(next); |
| 3699 | free_extent_buffer(next); |
| 3700 | continue; |
| 3701 | } |
| 3702 | |
| 3703 | *level = btrfs_header_level(next); |
| 3704 | path->nodes[*level] = next; |
| 3705 | path->slots[*level] = 0; |
| 3706 | path->locks[*level] = 1; |
| 3707 | cond_resched(); |
| 3708 | } |
| 3709 | out: |
| 3710 | parent = path->nodes[*level + 1]; |
| 3711 | bytenr = path->nodes[*level]->start; |
| 3712 | blocksize = path->nodes[*level]->len; |
| 3713 | |
| 3714 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
| 3715 | parent->start, btrfs_header_owner(parent), |
| 3716 | btrfs_header_generation(parent), *level, 1); |
| 3717 | BUG_ON(ret); |
| 3718 | |
| 3719 | if (path->locks[*level]) { |
| 3720 | btrfs_tree_unlock(path->nodes[*level]); |
| 3721 | path->locks[*level] = 0; |
| 3722 | } |
| 3723 | free_extent_buffer(path->nodes[*level]); |
| 3724 | path->nodes[*level] = NULL; |
| 3725 | *level += 1; |
| 3726 | cond_resched(); |
| 3727 | return 0; |
| 3728 | } |
| 3729 | |
| 3730 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3731 | * helper for dropping snapshots. This walks back up the tree in the path |
| 3732 | * to find the first node higher up where we haven't yet gone through |
| 3733 | * all the slots |
| 3734 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3735 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
| 3736 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3737 | struct btrfs_path *path, |
| 3738 | int *level, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3739 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3740 | u64 root_owner; |
| 3741 | u64 root_gen; |
| 3742 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3743 | int i; |
| 3744 | int slot; |
| 3745 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3746 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3747 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3748 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3749 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 3750 | struct extent_buffer *node; |
| 3751 | struct btrfs_disk_key disk_key; |
| 3752 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3753 | path->slots[i]++; |
| 3754 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3755 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3756 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3757 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3758 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3759 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3760 | return 0; |
| 3761 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3762 | struct extent_buffer *parent; |
| 3763 | if (path->nodes[*level] == root->node) |
| 3764 | parent = path->nodes[*level]; |
| 3765 | else |
| 3766 | parent = path->nodes[*level + 1]; |
| 3767 | |
| 3768 | root_owner = btrfs_header_owner(parent); |
| 3769 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3770 | |
| 3771 | clean_tree_block(trans, root, path->nodes[*level]); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3772 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3773 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3774 | path->nodes[*level]->len, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3775 | parent->start, root_owner, |
| 3776 | root_gen, *level, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3777 | BUG_ON(ret); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3778 | if (path->locks[*level]) { |
| 3779 | btrfs_tree_unlock(path->nodes[*level]); |
| 3780 | path->locks[*level] = 0; |
| 3781 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3782 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3783 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3784 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3785 | } |
| 3786 | } |
| 3787 | return 1; |
| 3788 | } |
| 3789 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3790 | /* |
| 3791 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 3792 | * the tree freeing any blocks that have a ref count of zero after being |
| 3793 | * decremented. |
| 3794 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 3795 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3796 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3797 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 3798 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3799 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3800 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3801 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3802 | int i; |
| 3803 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3804 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3805 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3806 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3807 | path = btrfs_alloc_path(); |
| 3808 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3809 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3810 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3811 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3812 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 3813 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3814 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3815 | path->slots[level] = 0; |
| 3816 | } else { |
| 3817 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3818 | struct btrfs_disk_key found_key; |
| 3819 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3820 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3821 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3822 | level = root_item->drop_level; |
| 3823 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3824 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 3825 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3826 | ret = wret; |
| 3827 | goto out; |
| 3828 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3829 | node = path->nodes[level]; |
| 3830 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 3831 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 3832 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 3833 | /* |
| 3834 | * unlock our path, this is safe because only this |
| 3835 | * function is allowed to delete this snapshot |
| 3836 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3837 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 3838 | if (path->nodes[i] && path->locks[i]) { |
| 3839 | path->locks[i] = 0; |
| 3840 | btrfs_tree_unlock(path->nodes[i]); |
| 3841 | } |
| 3842 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3843 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3844 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3845 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3846 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3847 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3848 | if (wret < 0) |
| 3849 | ret = wret; |
| 3850 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3851 | wret = walk_up_tree(trans, root, path, &level, |
| 3852 | BTRFS_MAX_LEVEL); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3853 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3854 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3855 | if (wret < 0) |
| 3856 | ret = wret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3857 | if (trans->transaction->in_commit) { |
| 3858 | ret = -EAGAIN; |
| 3859 | break; |
| 3860 | } |
Chris Mason | 18e35e0 | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 3861 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3862 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3863 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3864 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3865 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3866 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 3867 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 3868 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3869 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 3870 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3871 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3872 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3873 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3874 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3875 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 3876 | struct btrfs_root *root, |
| 3877 | struct extent_buffer *node, |
| 3878 | struct extent_buffer *parent) |
| 3879 | { |
| 3880 | struct btrfs_path *path; |
| 3881 | int level; |
| 3882 | int parent_level; |
| 3883 | int ret = 0; |
| 3884 | int wret; |
| 3885 | |
| 3886 | path = btrfs_alloc_path(); |
| 3887 | BUG_ON(!path); |
| 3888 | |
| 3889 | BUG_ON(!btrfs_tree_locked(parent)); |
| 3890 | parent_level = btrfs_header_level(parent); |
| 3891 | extent_buffer_get(parent); |
| 3892 | path->nodes[parent_level] = parent; |
| 3893 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 3894 | |
| 3895 | BUG_ON(!btrfs_tree_locked(node)); |
| 3896 | level = btrfs_header_level(node); |
| 3897 | extent_buffer_get(node); |
| 3898 | path->nodes[level] = node; |
| 3899 | path->slots[level] = 0; |
| 3900 | |
| 3901 | while (1) { |
| 3902 | wret = walk_down_subtree(trans, root, path, &level); |
| 3903 | if (wret < 0) |
| 3904 | ret = wret; |
| 3905 | if (wret != 0) |
| 3906 | break; |
| 3907 | |
| 3908 | wret = walk_up_tree(trans, root, path, &level, parent_level); |
| 3909 | if (wret < 0) |
| 3910 | ret = wret; |
| 3911 | if (wret != 0) |
| 3912 | break; |
| 3913 | } |
| 3914 | |
| 3915 | btrfs_free_path(path); |
| 3916 | return ret; |
| 3917 | } |
| 3918 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3919 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 3920 | unsigned long nr) |
| 3921 | { |
| 3922 | return min(last, start + nr - 1); |
| 3923 | } |
| 3924 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3925 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
| 3926 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3927 | { |
| 3928 | u64 page_start; |
| 3929 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3930 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3931 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3932 | unsigned long i; |
| 3933 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3934 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3935 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3936 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3937 | unsigned int total_read = 0; |
| 3938 | unsigned int total_dirty = 0; |
| 3939 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3940 | |
| 3941 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3942 | |
| 3943 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3944 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3945 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 3946 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3947 | /* make sure the dirty trick played by the caller work */ |
| 3948 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 3949 | first_index, last_index); |
| 3950 | if (ret) |
| 3951 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3952 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 3953 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3954 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3955 | for (i = first_index ; i <= last_index; i++) { |
| 3956 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3957 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3958 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 3959 | } |
| 3960 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3961 | again: |
| 3962 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3963 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3964 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3965 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3966 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3967 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3968 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3969 | if (!PageUptodate(page)) { |
| 3970 | btrfs_readpage(NULL, page); |
| 3971 | lock_page(page); |
| 3972 | if (!PageUptodate(page)) { |
| 3973 | unlock_page(page); |
| 3974 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3975 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3976 | goto out_unlock; |
| 3977 | } |
| 3978 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3979 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3980 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3981 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 3982 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 3983 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 3984 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 3985 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 3986 | if (ordered) { |
| 3987 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 3988 | unlock_page(page); |
| 3989 | page_cache_release(page); |
| 3990 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 3991 | btrfs_put_ordered_extent(ordered); |
| 3992 | goto again; |
| 3993 | } |
| 3994 | set_page_extent_mapped(page); |
| 3995 | |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 3996 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3997 | if (i == first_index) |
| 3998 | set_extent_bits(io_tree, page_start, page_end, |
| 3999 | EXTENT_BOUNDARY, GFP_NOFS); |
| 4000 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4001 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4002 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4003 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4004 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4005 | unlock_page(page); |
| 4006 | page_cache_release(page); |
| 4007 | } |
| 4008 | |
| 4009 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4010 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4011 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4012 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 4013 | return ret; |
| 4014 | } |
| 4015 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4016 | static int noinline relocate_data_extent(struct inode *reloc_inode, |
| 4017 | struct btrfs_key *extent_key, |
| 4018 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4019 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4020 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4021 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 4022 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4023 | u64 start = extent_key->objectid - offset; |
| 4024 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4025 | |
| 4026 | em = alloc_extent_map(GFP_NOFS); |
| 4027 | BUG_ON(!em || IS_ERR(em)); |
| 4028 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4029 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4030 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4031 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4032 | em->block_start = extent_key->objectid; |
| 4033 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4034 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4035 | |
| 4036 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4037 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4038 | while (1) { |
| 4039 | int ret; |
| 4040 | spin_lock(&em_tree->lock); |
| 4041 | ret = add_extent_mapping(em_tree, em); |
| 4042 | spin_unlock(&em_tree->lock); |
| 4043 | if (ret != -EEXIST) { |
| 4044 | free_extent_map(em); |
| 4045 | break; |
| 4046 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4047 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4048 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4049 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4050 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4051 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | struct btrfs_ref_path { |
| 4055 | u64 extent_start; |
| 4056 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 4057 | u64 root_objectid; |
| 4058 | u64 root_generation; |
| 4059 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4060 | u32 num_refs; |
| 4061 | int lowest_level; |
| 4062 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4063 | int shared_level; |
| 4064 | |
| 4065 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 4066 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4067 | }; |
| 4068 | |
| 4069 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4070 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4071 | u64 disk_bytenr; |
| 4072 | u64 disk_num_bytes; |
| 4073 | u64 offset; |
| 4074 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4075 | u8 compression; |
| 4076 | u8 encryption; |
| 4077 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4078 | }; |
| 4079 | |
| 4080 | static int is_cowonly_root(u64 root_objectid) |
| 4081 | { |
| 4082 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 4083 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 4084 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 4085 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
| 4086 | root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 4087 | return 1; |
| 4088 | return 0; |
| 4089 | } |
| 4090 | |
| 4091 | static int noinline __next_ref_path(struct btrfs_trans_handle *trans, |
| 4092 | struct btrfs_root *extent_root, |
| 4093 | struct btrfs_ref_path *ref_path, |
| 4094 | int first_time) |
| 4095 | { |
| 4096 | struct extent_buffer *leaf; |
| 4097 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4098 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4099 | struct btrfs_key key; |
| 4100 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4101 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4102 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4103 | int level; |
| 4104 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4105 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4106 | path = btrfs_alloc_path(); |
| 4107 | if (!path) |
| 4108 | return -ENOMEM; |
| 4109 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4110 | if (first_time) { |
| 4111 | ref_path->lowest_level = -1; |
| 4112 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4113 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4114 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4115 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4116 | walk_down: |
| 4117 | level = ref_path->current_level - 1; |
| 4118 | while (level >= -1) { |
| 4119 | u64 parent; |
| 4120 | if (level < ref_path->lowest_level) |
| 4121 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4122 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4123 | if (level >= 0) { |
| 4124 | bytenr = ref_path->nodes[level]; |
| 4125 | } else { |
| 4126 | bytenr = ref_path->extent_start; |
| 4127 | } |
| 4128 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4129 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4130 | parent = ref_path->nodes[level + 1]; |
| 4131 | ref_path->nodes[level + 1] = 0; |
| 4132 | ref_path->current_level = level; |
| 4133 | BUG_ON(parent == 0); |
| 4134 | |
| 4135 | key.objectid = bytenr; |
| 4136 | key.offset = parent + 1; |
| 4137 | key.type = BTRFS_EXTENT_REF_KEY; |
| 4138 | |
| 4139 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4140 | if (ret < 0) |
| 4141 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4142 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4143 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4144 | leaf = path->nodes[0]; |
| 4145 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4146 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4147 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4148 | if (ret < 0) |
| 4149 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4150 | if (ret > 0) |
| 4151 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 4152 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4153 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4154 | |
| 4155 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4156 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4157 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 4158 | if (level < ref_path->shared_level) |
| 4159 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4160 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4161 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4162 | next: |
| 4163 | level--; |
| 4164 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4165 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4166 | } |
| 4167 | /* reached lowest level */ |
| 4168 | ret = 1; |
| 4169 | goto out; |
| 4170 | walk_up: |
| 4171 | level = ref_path->current_level; |
| 4172 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 4173 | u64 ref_objectid; |
| 4174 | if (level >= 0) { |
| 4175 | bytenr = ref_path->nodes[level]; |
| 4176 | } else { |
| 4177 | bytenr = ref_path->extent_start; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4178 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4179 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4180 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4181 | key.objectid = bytenr; |
| 4182 | key.offset = 0; |
| 4183 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4184 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4185 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 4186 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4187 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4188 | |
| 4189 | leaf = path->nodes[0]; |
| 4190 | nritems = btrfs_header_nritems(leaf); |
| 4191 | if (path->slots[0] >= nritems) { |
| 4192 | ret = btrfs_next_leaf(extent_root, path); |
| 4193 | if (ret < 0) |
| 4194 | goto out; |
| 4195 | if (ret > 0) { |
| 4196 | /* the extent was freed by someone */ |
| 4197 | if (ref_path->lowest_level == level) |
| 4198 | goto out; |
| 4199 | btrfs_release_path(extent_root, path); |
| 4200 | goto walk_down; |
| 4201 | } |
| 4202 | leaf = path->nodes[0]; |
| 4203 | } |
| 4204 | |
| 4205 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4206 | if (found_key.objectid != bytenr || |
| 4207 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 4208 | /* the extent was freed by someone */ |
| 4209 | if (ref_path->lowest_level == level) { |
| 4210 | ret = 1; |
| 4211 | goto out; |
| 4212 | } |
| 4213 | btrfs_release_path(extent_root, path); |
| 4214 | goto walk_down; |
| 4215 | } |
| 4216 | found: |
| 4217 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 4218 | struct btrfs_extent_ref); |
| 4219 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 4220 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4221 | if (first_time) { |
| 4222 | level = (int)ref_objectid; |
| 4223 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 4224 | ref_path->lowest_level = level; |
| 4225 | ref_path->current_level = level; |
| 4226 | ref_path->nodes[level] = bytenr; |
| 4227 | } else { |
| 4228 | WARN_ON(ref_objectid != level); |
| 4229 | } |
| 4230 | } else { |
| 4231 | WARN_ON(level != -1); |
| 4232 | } |
| 4233 | first_time = 0; |
| 4234 | |
| 4235 | if (ref_path->lowest_level == level) { |
| 4236 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4237 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 4238 | } |
| 4239 | |
| 4240 | /* |
| 4241 | * the block is tree root or the block isn't in reference |
| 4242 | * counted tree. |
| 4243 | */ |
| 4244 | if (found_key.objectid == found_key.offset || |
| 4245 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 4246 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4247 | ref_path->root_generation = |
| 4248 | btrfs_ref_generation(leaf, ref); |
| 4249 | if (level < 0) { |
| 4250 | /* special reference from the tree log */ |
| 4251 | ref_path->nodes[0] = found_key.offset; |
| 4252 | ref_path->current_level = 0; |
| 4253 | } |
| 4254 | ret = 0; |
| 4255 | goto out; |
| 4256 | } |
| 4257 | |
| 4258 | level++; |
| 4259 | BUG_ON(ref_path->nodes[level] != 0); |
| 4260 | ref_path->nodes[level] = found_key.offset; |
| 4261 | ref_path->current_level = level; |
| 4262 | |
| 4263 | /* |
| 4264 | * the reference was created in the running transaction, |
| 4265 | * no need to continue walking up. |
| 4266 | */ |
| 4267 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 4268 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4269 | ref_path->root_generation = |
| 4270 | btrfs_ref_generation(leaf, ref); |
| 4271 | ret = 0; |
| 4272 | goto out; |
| 4273 | } |
| 4274 | |
| 4275 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4276 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4277 | } |
| 4278 | /* reached max tree level, but no tree root found. */ |
| 4279 | BUG(); |
| 4280 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4281 | btrfs_free_path(path); |
| 4282 | return ret; |
| 4283 | } |
| 4284 | |
| 4285 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 4286 | struct btrfs_root *extent_root, |
| 4287 | struct btrfs_ref_path *ref_path, |
| 4288 | u64 extent_start) |
| 4289 | { |
| 4290 | memset(ref_path, 0, sizeof(*ref_path)); |
| 4291 | ref_path->extent_start = extent_start; |
| 4292 | |
| 4293 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 4294 | } |
| 4295 | |
| 4296 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 4297 | struct btrfs_root *extent_root, |
| 4298 | struct btrfs_ref_path *ref_path) |
| 4299 | { |
| 4300 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 4301 | } |
| 4302 | |
| 4303 | static int noinline get_new_locations(struct inode *reloc_inode, |
| 4304 | struct btrfs_key *extent_key, |
| 4305 | u64 offset, int no_fragment, |
| 4306 | struct disk_extent **extents, |
| 4307 | int *nr_extents) |
| 4308 | { |
| 4309 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4310 | struct btrfs_path *path; |
| 4311 | struct btrfs_file_extent_item *fi; |
| 4312 | struct extent_buffer *leaf; |
| 4313 | struct disk_extent *exts = *extents; |
| 4314 | struct btrfs_key found_key; |
| 4315 | u64 cur_pos; |
| 4316 | u64 last_byte; |
| 4317 | u32 nritems; |
| 4318 | int nr = 0; |
| 4319 | int max = *nr_extents; |
| 4320 | int ret; |
| 4321 | |
| 4322 | WARN_ON(!no_fragment && *extents); |
| 4323 | if (!exts) { |
| 4324 | max = 1; |
| 4325 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4326 | if (!exts) |
| 4327 | return -ENOMEM; |
| 4328 | } |
| 4329 | |
| 4330 | path = btrfs_alloc_path(); |
| 4331 | BUG_ON(!path); |
| 4332 | |
| 4333 | cur_pos = extent_key->objectid - offset; |
| 4334 | last_byte = extent_key->objectid + extent_key->offset; |
| 4335 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 4336 | cur_pos, 0); |
| 4337 | if (ret < 0) |
| 4338 | goto out; |
| 4339 | if (ret > 0) { |
| 4340 | ret = -ENOENT; |
| 4341 | goto out; |
| 4342 | } |
| 4343 | |
| 4344 | while (1) { |
| 4345 | leaf = path->nodes[0]; |
| 4346 | nritems = btrfs_header_nritems(leaf); |
| 4347 | if (path->slots[0] >= nritems) { |
| 4348 | ret = btrfs_next_leaf(root, path); |
| 4349 | if (ret < 0) |
| 4350 | goto out; |
| 4351 | if (ret > 0) |
| 4352 | break; |
| 4353 | leaf = path->nodes[0]; |
| 4354 | } |
| 4355 | |
| 4356 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4357 | if (found_key.offset != cur_pos || |
| 4358 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 4359 | found_key.objectid != reloc_inode->i_ino) |
| 4360 | break; |
| 4361 | |
| 4362 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4363 | struct btrfs_file_extent_item); |
| 4364 | if (btrfs_file_extent_type(leaf, fi) != |
| 4365 | BTRFS_FILE_EXTENT_REG || |
| 4366 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4367 | break; |
| 4368 | |
| 4369 | if (nr == max) { |
| 4370 | struct disk_extent *old = exts; |
| 4371 | max *= 2; |
| 4372 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4373 | memcpy(exts, old, sizeof(*exts) * nr); |
| 4374 | if (old != *extents) |
| 4375 | kfree(old); |
| 4376 | } |
| 4377 | |
| 4378 | exts[nr].disk_bytenr = |
| 4379 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4380 | exts[nr].disk_num_bytes = |
| 4381 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4382 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 4383 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4384 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 4385 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 4386 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 4387 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 4388 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4389 | BUG_ON(exts[nr].offset > 0); |
| 4390 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 4391 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4392 | |
| 4393 | cur_pos += exts[nr].num_bytes; |
| 4394 | nr++; |
| 4395 | |
| 4396 | if (cur_pos + offset >= last_byte) |
| 4397 | break; |
| 4398 | |
| 4399 | if (no_fragment) { |
| 4400 | ret = 1; |
| 4401 | goto out; |
| 4402 | } |
| 4403 | path->slots[0]++; |
| 4404 | } |
| 4405 | |
| 4406 | WARN_ON(cur_pos + offset > last_byte); |
| 4407 | if (cur_pos + offset < last_byte) { |
| 4408 | ret = -ENOENT; |
| 4409 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4410 | } |
| 4411 | ret = 0; |
| 4412 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4413 | btrfs_free_path(path); |
| 4414 | if (ret) { |
| 4415 | if (exts != *extents) |
| 4416 | kfree(exts); |
| 4417 | } else { |
| 4418 | *extents = exts; |
| 4419 | *nr_extents = nr; |
| 4420 | } |
| 4421 | return ret; |
| 4422 | } |
| 4423 | |
| 4424 | static int noinline replace_one_extent(struct btrfs_trans_handle *trans, |
| 4425 | struct btrfs_root *root, |
| 4426 | struct btrfs_path *path, |
| 4427 | struct btrfs_key *extent_key, |
| 4428 | struct btrfs_key *leaf_key, |
| 4429 | struct btrfs_ref_path *ref_path, |
| 4430 | struct disk_extent *new_extents, |
| 4431 | int nr_extents) |
| 4432 | { |
| 4433 | struct extent_buffer *leaf; |
| 4434 | struct btrfs_file_extent_item *fi; |
| 4435 | struct inode *inode = NULL; |
| 4436 | struct btrfs_key key; |
| 4437 | u64 lock_start = 0; |
| 4438 | u64 lock_end = 0; |
| 4439 | u64 num_bytes; |
| 4440 | u64 ext_offset; |
| 4441 | u64 first_pos; |
| 4442 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4443 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4444 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4445 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4446 | int ret; |
| 4447 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4448 | memcpy(&key, leaf_key, sizeof(key)); |
| 4449 | first_pos = INT_LIMIT(loff_t) - extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4450 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4451 | if (key.objectid < ref_path->owner_objectid || |
| 4452 | (key.objectid == ref_path->owner_objectid && |
| 4453 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 4454 | key.objectid = ref_path->owner_objectid; |
| 4455 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4456 | key.offset = 0; |
| 4457 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | while (1) { |
| 4461 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 4462 | if (ret < 0) |
| 4463 | goto out; |
| 4464 | |
| 4465 | leaf = path->nodes[0]; |
| 4466 | nritems = btrfs_header_nritems(leaf); |
| 4467 | next: |
| 4468 | if (extent_locked && ret > 0) { |
| 4469 | /* |
| 4470 | * the file extent item was modified by someone |
| 4471 | * before the extent got locked. |
| 4472 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4473 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4474 | lock_end, GFP_NOFS); |
| 4475 | extent_locked = 0; |
| 4476 | } |
| 4477 | |
| 4478 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4479 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4480 | break; |
| 4481 | |
| 4482 | BUG_ON(extent_locked); |
| 4483 | ret = btrfs_next_leaf(root, path); |
| 4484 | if (ret < 0) |
| 4485 | goto out; |
| 4486 | if (ret > 0) |
| 4487 | break; |
| 4488 | leaf = path->nodes[0]; |
| 4489 | nritems = btrfs_header_nritems(leaf); |
| 4490 | } |
| 4491 | |
| 4492 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4493 | |
| 4494 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 4495 | if ((key.objectid > ref_path->owner_objectid) || |
| 4496 | (key.objectid == ref_path->owner_objectid && |
| 4497 | key.type > BTRFS_EXTENT_DATA_KEY) || |
| 4498 | (key.offset >= first_pos + extent_key->offset)) |
| 4499 | break; |
| 4500 | } |
| 4501 | |
| 4502 | if (inode && key.objectid != inode->i_ino) { |
| 4503 | BUG_ON(extent_locked); |
| 4504 | btrfs_release_path(root, path); |
| 4505 | mutex_unlock(&inode->i_mutex); |
| 4506 | iput(inode); |
| 4507 | inode = NULL; |
| 4508 | continue; |
| 4509 | } |
| 4510 | |
| 4511 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 4512 | path->slots[0]++; |
| 4513 | ret = 1; |
| 4514 | goto next; |
| 4515 | } |
| 4516 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4517 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4518 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 4519 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 4520 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4521 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 4522 | extent_key->objectid)) { |
| 4523 | path->slots[0]++; |
| 4524 | ret = 1; |
| 4525 | goto next; |
| 4526 | } |
| 4527 | |
| 4528 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4529 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 4530 | |
| 4531 | if (first_pos > key.offset - ext_offset) |
| 4532 | first_pos = key.offset - ext_offset; |
| 4533 | |
| 4534 | if (!extent_locked) { |
| 4535 | lock_start = key.offset; |
| 4536 | lock_end = lock_start + num_bytes - 1; |
| 4537 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4538 | if (lock_start > key.offset || |
| 4539 | lock_end + 1 < key.offset + num_bytes) { |
| 4540 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4541 | lock_start, lock_end, GFP_NOFS); |
| 4542 | extent_locked = 0; |
| 4543 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | if (!inode) { |
| 4547 | btrfs_release_path(root, path); |
| 4548 | |
| 4549 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 4550 | key.objectid, root); |
| 4551 | if (inode->i_state & I_NEW) { |
| 4552 | BTRFS_I(inode)->root = root; |
| 4553 | BTRFS_I(inode)->location.objectid = |
| 4554 | key.objectid; |
| 4555 | BTRFS_I(inode)->location.type = |
| 4556 | BTRFS_INODE_ITEM_KEY; |
| 4557 | BTRFS_I(inode)->location.offset = 0; |
| 4558 | btrfs_read_locked_inode(inode); |
| 4559 | unlock_new_inode(inode); |
| 4560 | } |
| 4561 | /* |
| 4562 | * some code call btrfs_commit_transaction while |
| 4563 | * holding the i_mutex, so we can't use mutex_lock |
| 4564 | * here. |
| 4565 | */ |
| 4566 | if (is_bad_inode(inode) || |
| 4567 | !mutex_trylock(&inode->i_mutex)) { |
| 4568 | iput(inode); |
| 4569 | inode = NULL; |
| 4570 | key.offset = (u64)-1; |
| 4571 | goto skip; |
| 4572 | } |
| 4573 | } |
| 4574 | |
| 4575 | if (!extent_locked) { |
| 4576 | struct btrfs_ordered_extent *ordered; |
| 4577 | |
| 4578 | btrfs_release_path(root, path); |
| 4579 | |
| 4580 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4581 | lock_end, GFP_NOFS); |
| 4582 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 4583 | lock_end); |
| 4584 | if (ordered && |
| 4585 | ordered->file_offset <= lock_end && |
| 4586 | ordered->file_offset + ordered->len > lock_start) { |
| 4587 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 4588 | lock_start, lock_end, GFP_NOFS); |
| 4589 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 4590 | btrfs_put_ordered_extent(ordered); |
| 4591 | key.offset += num_bytes; |
| 4592 | goto skip; |
| 4593 | } |
| 4594 | if (ordered) |
| 4595 | btrfs_put_ordered_extent(ordered); |
| 4596 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4597 | extent_locked = 1; |
| 4598 | continue; |
| 4599 | } |
| 4600 | |
| 4601 | if (nr_extents == 1) { |
| 4602 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4603 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4604 | new_extents[0].disk_bytenr); |
| 4605 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4606 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4607 | btrfs_mark_buffer_dirty(leaf); |
| 4608 | |
| 4609 | btrfs_drop_extent_cache(inode, key.offset, |
| 4610 | key.offset + num_bytes - 1, 0); |
| 4611 | |
| 4612 | ret = btrfs_inc_extent_ref(trans, root, |
| 4613 | new_extents[0].disk_bytenr, |
| 4614 | new_extents[0].disk_num_bytes, |
| 4615 | leaf->start, |
| 4616 | root->root_key.objectid, |
| 4617 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4618 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4619 | BUG_ON(ret); |
| 4620 | |
| 4621 | ret = btrfs_free_extent(trans, root, |
| 4622 | extent_key->objectid, |
| 4623 | extent_key->offset, |
| 4624 | leaf->start, |
| 4625 | btrfs_header_owner(leaf), |
| 4626 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4627 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4628 | BUG_ON(ret); |
| 4629 | |
| 4630 | btrfs_release_path(root, path); |
| 4631 | key.offset += num_bytes; |
| 4632 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4633 | BUG_ON(1); |
| 4634 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4635 | u64 alloc_hint; |
| 4636 | u64 extent_len; |
| 4637 | int i; |
| 4638 | /* |
| 4639 | * drop old extent pointer at first, then insert the |
| 4640 | * new pointers one bye one |
| 4641 | */ |
| 4642 | btrfs_release_path(root, path); |
| 4643 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 4644 | key.offset + num_bytes, |
| 4645 | key.offset, &alloc_hint); |
| 4646 | BUG_ON(ret); |
| 4647 | |
| 4648 | for (i = 0; i < nr_extents; i++) { |
| 4649 | if (ext_offset >= new_extents[i].num_bytes) { |
| 4650 | ext_offset -= new_extents[i].num_bytes; |
| 4651 | continue; |
| 4652 | } |
| 4653 | extent_len = min(new_extents[i].num_bytes - |
| 4654 | ext_offset, num_bytes); |
| 4655 | |
| 4656 | ret = btrfs_insert_empty_item(trans, root, |
| 4657 | path, &key, |
| 4658 | sizeof(*fi)); |
| 4659 | BUG_ON(ret); |
| 4660 | |
| 4661 | leaf = path->nodes[0]; |
| 4662 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4663 | struct btrfs_file_extent_item); |
| 4664 | btrfs_set_file_extent_generation(leaf, fi, |
| 4665 | trans->transid); |
| 4666 | btrfs_set_file_extent_type(leaf, fi, |
| 4667 | BTRFS_FILE_EXTENT_REG); |
| 4668 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4669 | new_extents[i].disk_bytenr); |
| 4670 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4671 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4672 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 4673 | new_extents[i].ram_bytes); |
| 4674 | |
| 4675 | btrfs_set_file_extent_compression(leaf, fi, |
| 4676 | new_extents[i].compression); |
| 4677 | btrfs_set_file_extent_encryption(leaf, fi, |
| 4678 | new_extents[i].encryption); |
| 4679 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 4680 | new_extents[i].other_encoding); |
| 4681 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4682 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 4683 | extent_len); |
| 4684 | ext_offset += new_extents[i].offset; |
| 4685 | btrfs_set_file_extent_offset(leaf, fi, |
| 4686 | ext_offset); |
| 4687 | btrfs_mark_buffer_dirty(leaf); |
| 4688 | |
| 4689 | btrfs_drop_extent_cache(inode, key.offset, |
| 4690 | key.offset + extent_len - 1, 0); |
| 4691 | |
| 4692 | ret = btrfs_inc_extent_ref(trans, root, |
| 4693 | new_extents[i].disk_bytenr, |
| 4694 | new_extents[i].disk_num_bytes, |
| 4695 | leaf->start, |
| 4696 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4697 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4698 | BUG_ON(ret); |
| 4699 | btrfs_release_path(root, path); |
| 4700 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 4701 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4702 | |
| 4703 | ext_offset = 0; |
| 4704 | num_bytes -= extent_len; |
| 4705 | key.offset += extent_len; |
| 4706 | |
| 4707 | if (num_bytes == 0) |
| 4708 | break; |
| 4709 | } |
| 4710 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4711 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4715 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4716 | lock_end, GFP_NOFS); |
| 4717 | extent_locked = 0; |
| 4718 | } |
| 4719 | skip: |
| 4720 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
| 4721 | key.offset >= first_pos + extent_key->offset) |
| 4722 | break; |
| 4723 | |
| 4724 | cond_resched(); |
| 4725 | } |
| 4726 | ret = 0; |
| 4727 | out: |
| 4728 | btrfs_release_path(root, path); |
| 4729 | if (inode) { |
| 4730 | mutex_unlock(&inode->i_mutex); |
| 4731 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4732 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4733 | lock_end, GFP_NOFS); |
| 4734 | } |
| 4735 | iput(inode); |
| 4736 | } |
| 4737 | return ret; |
| 4738 | } |
| 4739 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4740 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 4741 | struct btrfs_root *root, |
| 4742 | struct extent_buffer *buf, u64 orig_start) |
| 4743 | { |
| 4744 | int level; |
| 4745 | int ret; |
| 4746 | |
| 4747 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 4748 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 4749 | |
| 4750 | level = btrfs_header_level(buf); |
| 4751 | if (level == 0) { |
| 4752 | struct btrfs_leaf_ref *ref; |
| 4753 | struct btrfs_leaf_ref *orig_ref; |
| 4754 | |
| 4755 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 4756 | if (!orig_ref) |
| 4757 | return -ENOENT; |
| 4758 | |
| 4759 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 4760 | if (!ref) { |
| 4761 | btrfs_free_leaf_ref(root, orig_ref); |
| 4762 | return -ENOMEM; |
| 4763 | } |
| 4764 | |
| 4765 | ref->nritems = orig_ref->nritems; |
| 4766 | memcpy(ref->extents, orig_ref->extents, |
| 4767 | sizeof(ref->extents[0]) * ref->nritems); |
| 4768 | |
| 4769 | btrfs_free_leaf_ref(root, orig_ref); |
| 4770 | |
| 4771 | ref->root_gen = trans->transid; |
| 4772 | ref->bytenr = buf->start; |
| 4773 | ref->owner = btrfs_header_owner(buf); |
| 4774 | ref->generation = btrfs_header_generation(buf); |
| 4775 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 4776 | WARN_ON(ret); |
| 4777 | btrfs_free_leaf_ref(root, ref); |
| 4778 | } |
| 4779 | return 0; |
| 4780 | } |
| 4781 | |
| 4782 | static int noinline invalidate_extent_cache(struct btrfs_root *root, |
| 4783 | struct extent_buffer *leaf, |
| 4784 | struct btrfs_block_group_cache *group, |
| 4785 | struct btrfs_root *target_root) |
| 4786 | { |
| 4787 | struct btrfs_key key; |
| 4788 | struct inode *inode = NULL; |
| 4789 | struct btrfs_file_extent_item *fi; |
| 4790 | u64 num_bytes; |
| 4791 | u64 skip_objectid = 0; |
| 4792 | u32 nritems; |
| 4793 | u32 i; |
| 4794 | |
| 4795 | nritems = btrfs_header_nritems(leaf); |
| 4796 | for (i = 0; i < nritems; i++) { |
| 4797 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4798 | if (key.objectid == skip_objectid || |
| 4799 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 4800 | continue; |
| 4801 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4802 | if (btrfs_file_extent_type(leaf, fi) == |
| 4803 | BTRFS_FILE_EXTENT_INLINE) |
| 4804 | continue; |
| 4805 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4806 | continue; |
| 4807 | if (!inode || inode->i_ino != key.objectid) { |
| 4808 | iput(inode); |
| 4809 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 4810 | key.objectid, target_root, 1); |
| 4811 | } |
| 4812 | if (!inode) { |
| 4813 | skip_objectid = key.objectid; |
| 4814 | continue; |
| 4815 | } |
| 4816 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 4817 | |
| 4818 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4819 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4820 | btrfs_drop_extent_cache(inode, key.offset, |
| 4821 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4822 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 4823 | key.offset + num_bytes - 1, GFP_NOFS); |
| 4824 | cond_resched(); |
| 4825 | } |
| 4826 | iput(inode); |
| 4827 | return 0; |
| 4828 | } |
| 4829 | |
| 4830 | static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
| 4831 | struct btrfs_root *root, |
| 4832 | struct extent_buffer *leaf, |
| 4833 | struct btrfs_block_group_cache *group, |
| 4834 | struct inode *reloc_inode) |
| 4835 | { |
| 4836 | struct btrfs_key key; |
| 4837 | struct btrfs_key extent_key; |
| 4838 | struct btrfs_file_extent_item *fi; |
| 4839 | struct btrfs_leaf_ref *ref; |
| 4840 | struct disk_extent *new_extent; |
| 4841 | u64 bytenr; |
| 4842 | u64 num_bytes; |
| 4843 | u32 nritems; |
| 4844 | u32 i; |
| 4845 | int ext_index; |
| 4846 | int nr_extent; |
| 4847 | int ret; |
| 4848 | |
| 4849 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 4850 | BUG_ON(!new_extent); |
| 4851 | |
| 4852 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 4853 | BUG_ON(!ref); |
| 4854 | |
| 4855 | ext_index = -1; |
| 4856 | nritems = btrfs_header_nritems(leaf); |
| 4857 | for (i = 0; i < nritems; i++) { |
| 4858 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 4859 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 4860 | continue; |
| 4861 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 4862 | if (btrfs_file_extent_type(leaf, fi) == |
| 4863 | BTRFS_FILE_EXTENT_INLINE) |
| 4864 | continue; |
| 4865 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4866 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4867 | if (bytenr == 0) |
| 4868 | continue; |
| 4869 | |
| 4870 | ext_index++; |
| 4871 | if (bytenr >= group->key.objectid + group->key.offset || |
| 4872 | bytenr + num_bytes <= group->key.objectid) |
| 4873 | continue; |
| 4874 | |
| 4875 | extent_key.objectid = bytenr; |
| 4876 | extent_key.offset = num_bytes; |
| 4877 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4878 | nr_extent = 1; |
| 4879 | ret = get_new_locations(reloc_inode, &extent_key, |
| 4880 | group->key.objectid, 1, |
| 4881 | &new_extent, &nr_extent); |
| 4882 | if (ret > 0) |
| 4883 | continue; |
| 4884 | BUG_ON(ret < 0); |
| 4885 | |
| 4886 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 4887 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 4888 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 4889 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 4890 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4891 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 4892 | new_extent->disk_bytenr); |
| 4893 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 4894 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4895 | btrfs_mark_buffer_dirty(leaf); |
| 4896 | |
| 4897 | ret = btrfs_inc_extent_ref(trans, root, |
| 4898 | new_extent->disk_bytenr, |
| 4899 | new_extent->disk_num_bytes, |
| 4900 | leaf->start, |
| 4901 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4902 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4903 | BUG_ON(ret); |
| 4904 | ret = btrfs_free_extent(trans, root, |
| 4905 | bytenr, num_bytes, leaf->start, |
| 4906 | btrfs_header_owner(leaf), |
| 4907 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4908 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4909 | BUG_ON(ret); |
| 4910 | cond_resched(); |
| 4911 | } |
| 4912 | kfree(new_extent); |
| 4913 | BUG_ON(ext_index + 1 != ref->nritems); |
| 4914 | btrfs_free_leaf_ref(root, ref); |
| 4915 | return 0; |
| 4916 | } |
| 4917 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4918 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 4919 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4920 | { |
| 4921 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4922 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4923 | |
| 4924 | if (root->reloc_root) { |
| 4925 | reloc_root = root->reloc_root; |
| 4926 | root->reloc_root = NULL; |
| 4927 | list_add(&reloc_root->dead_list, |
| 4928 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4929 | |
| 4930 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 4931 | reloc_root->node->start); |
| 4932 | btrfs_set_root_level(&root->root_item, |
| 4933 | btrfs_header_level(reloc_root->node)); |
| 4934 | memset(&reloc_root->root_item.drop_progress, 0, |
| 4935 | sizeof(struct btrfs_disk_key)); |
| 4936 | reloc_root->root_item.drop_level = 0; |
| 4937 | |
| 4938 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 4939 | &reloc_root->root_key, |
| 4940 | &reloc_root->root_item); |
| 4941 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4942 | } |
| 4943 | return 0; |
| 4944 | } |
| 4945 | |
| 4946 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 4947 | { |
| 4948 | struct btrfs_trans_handle *trans; |
| 4949 | struct btrfs_root *reloc_root; |
| 4950 | struct btrfs_root *prev_root = NULL; |
| 4951 | struct list_head dead_roots; |
| 4952 | int ret; |
| 4953 | unsigned long nr; |
| 4954 | |
| 4955 | INIT_LIST_HEAD(&dead_roots); |
| 4956 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 4957 | |
| 4958 | while (!list_empty(&dead_roots)) { |
| 4959 | reloc_root = list_entry(dead_roots.prev, |
| 4960 | struct btrfs_root, dead_list); |
| 4961 | list_del_init(&reloc_root->dead_list); |
| 4962 | |
| 4963 | BUG_ON(reloc_root->commit_root != NULL); |
| 4964 | while (1) { |
| 4965 | trans = btrfs_join_transaction(root, 1); |
| 4966 | BUG_ON(!trans); |
| 4967 | |
| 4968 | mutex_lock(&root->fs_info->drop_mutex); |
| 4969 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 4970 | if (ret != -EAGAIN) |
| 4971 | break; |
| 4972 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4973 | |
| 4974 | nr = trans->blocks_used; |
| 4975 | ret = btrfs_end_transaction(trans, root); |
| 4976 | BUG_ON(ret); |
| 4977 | btrfs_btree_balance_dirty(root, nr); |
| 4978 | } |
| 4979 | |
| 4980 | free_extent_buffer(reloc_root->node); |
| 4981 | |
| 4982 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 4983 | &reloc_root->root_key); |
| 4984 | BUG_ON(ret); |
| 4985 | mutex_unlock(&root->fs_info->drop_mutex); |
| 4986 | |
| 4987 | nr = trans->blocks_used; |
| 4988 | ret = btrfs_end_transaction(trans, root); |
| 4989 | BUG_ON(ret); |
| 4990 | btrfs_btree_balance_dirty(root, nr); |
| 4991 | |
| 4992 | kfree(prev_root); |
| 4993 | prev_root = reloc_root; |
| 4994 | } |
| 4995 | if (prev_root) { |
| 4996 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 4997 | kfree(prev_root); |
| 4998 | } |
| 4999 | return 0; |
| 5000 | } |
| 5001 | |
| 5002 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 5003 | { |
| 5004 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 5005 | return 0; |
| 5006 | } |
| 5007 | |
| 5008 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 5009 | { |
| 5010 | struct btrfs_root *reloc_root; |
| 5011 | struct btrfs_trans_handle *trans; |
| 5012 | struct btrfs_key location; |
| 5013 | int found; |
| 5014 | int ret; |
| 5015 | |
| 5016 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5017 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 5018 | BUG_ON(ret); |
| 5019 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 5020 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 5021 | |
| 5022 | if (found) { |
| 5023 | trans = btrfs_start_transaction(root, 1); |
| 5024 | BUG_ON(!trans); |
| 5025 | ret = btrfs_commit_transaction(trans, root); |
| 5026 | BUG_ON(ret); |
| 5027 | } |
| 5028 | |
| 5029 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5030 | location.offset = (u64)-1; |
| 5031 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 5032 | |
| 5033 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 5034 | BUG_ON(!reloc_root); |
| 5035 | btrfs_orphan_cleanup(reloc_root); |
| 5036 | return 0; |
| 5037 | } |
| 5038 | |
| 5039 | static int noinline init_reloc_tree(struct btrfs_trans_handle *trans, |
| 5040 | struct btrfs_root *root) |
| 5041 | { |
| 5042 | struct btrfs_root *reloc_root; |
| 5043 | struct extent_buffer *eb; |
| 5044 | struct btrfs_root_item *root_item; |
| 5045 | struct btrfs_key root_key; |
| 5046 | int ret; |
| 5047 | |
| 5048 | BUG_ON(!root->ref_cows); |
| 5049 | if (root->reloc_root) |
| 5050 | return 0; |
| 5051 | |
| 5052 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 5053 | BUG_ON(!root_item); |
| 5054 | |
| 5055 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 5056 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 5057 | BUG_ON(ret); |
| 5058 | |
| 5059 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 5060 | root_key.offset = root->root_key.objectid; |
| 5061 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5062 | |
| 5063 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 5064 | btrfs_set_root_refs(root_item, 0); |
| 5065 | btrfs_set_root_bytenr(root_item, eb->start); |
| 5066 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5067 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5068 | |
| 5069 | btrfs_tree_unlock(eb); |
| 5070 | free_extent_buffer(eb); |
| 5071 | |
| 5072 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 5073 | &root_key, root_item); |
| 5074 | BUG_ON(ret); |
| 5075 | kfree(root_item); |
| 5076 | |
| 5077 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 5078 | &root_key); |
| 5079 | BUG_ON(!reloc_root); |
| 5080 | reloc_root->last_trans = trans->transid; |
| 5081 | reloc_root->commit_root = NULL; |
| 5082 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 5083 | |
| 5084 | root->reloc_root = reloc_root; |
| 5085 | return 0; |
| 5086 | } |
| 5087 | |
| 5088 | /* |
| 5089 | * Core function of space balance. |
| 5090 | * |
| 5091 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5092 | * counted roots. There is one reloc tree for each subvol, and all |
| 5093 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 5094 | * of the latest committed roots of subvols (root->commit_root). |
| 5095 | * |
| 5096 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 5097 | * COW the block through subvol's reloc tree, then update block pointer |
| 5098 | * in the subvol to point to the new block. Since all reloc trees share |
| 5099 | * same root key objectid, doing special handing for tree blocks owned |
| 5100 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 5101 | * we can use the resulting new block directly when the same block is |
| 5102 | * required to COW again through other reloc trees. By this way, relocated |
| 5103 | * tree blocks are shared between reloc trees, so they are also shared |
| 5104 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5105 | */ |
| 5106 | static int noinline relocate_one_path(struct btrfs_trans_handle *trans, |
| 5107 | struct btrfs_root *root, |
| 5108 | struct btrfs_path *path, |
| 5109 | struct btrfs_key *first_key, |
| 5110 | struct btrfs_ref_path *ref_path, |
| 5111 | struct btrfs_block_group_cache *group, |
| 5112 | struct inode *reloc_inode) |
| 5113 | { |
| 5114 | struct btrfs_root *reloc_root; |
| 5115 | struct extent_buffer *eb = NULL; |
| 5116 | struct btrfs_key *keys; |
| 5117 | u64 *nodes; |
| 5118 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5119 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5120 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5121 | int ret; |
| 5122 | |
| 5123 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 5124 | lowest_level = ref_path->owner_objectid; |
| 5125 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5126 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5127 | path->lowest_level = lowest_level; |
| 5128 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 5129 | BUG_ON(ret < 0); |
| 5130 | path->lowest_level = 0; |
| 5131 | btrfs_release_path(root, path); |
| 5132 | return 0; |
| 5133 | } |
| 5134 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5135 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5136 | ret = init_reloc_tree(trans, root); |
| 5137 | BUG_ON(ret); |
| 5138 | reloc_root = root->reloc_root; |
| 5139 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5140 | shared_level = ref_path->shared_level; |
| 5141 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5142 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5143 | keys = ref_path->node_keys; |
| 5144 | nodes = ref_path->new_nodes; |
| 5145 | memset(&keys[shared_level + 1], 0, |
| 5146 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 5147 | memset(&nodes[shared_level + 1], 0, |
| 5148 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5149 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5150 | if (nodes[lowest_level] == 0) { |
| 5151 | path->lowest_level = lowest_level; |
| 5152 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5153 | 0, 1); |
| 5154 | BUG_ON(ret); |
| 5155 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 5156 | eb = path->nodes[level]; |
| 5157 | if (!eb || eb == reloc_root->node) |
| 5158 | break; |
| 5159 | nodes[level] = eb->start; |
| 5160 | if (level == 0) |
| 5161 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 5162 | else |
| 5163 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 5164 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5165 | if (nodes[0] && |
| 5166 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5167 | eb = path->nodes[0]; |
| 5168 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 5169 | group, reloc_inode); |
| 5170 | BUG_ON(ret); |
| 5171 | } |
| 5172 | btrfs_release_path(reloc_root, path); |
| 5173 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5174 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5175 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5176 | BUG_ON(ret); |
| 5177 | } |
| 5178 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5179 | /* |
| 5180 | * replace tree blocks in the fs tree with tree blocks in |
| 5181 | * the reloc tree. |
| 5182 | */ |
| 5183 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 5184 | BUG_ON(ret < 0); |
| 5185 | |
| 5186 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5187 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5188 | 0, 0); |
| 5189 | BUG_ON(ret); |
| 5190 | extent_buffer_get(path->nodes[0]); |
| 5191 | eb = path->nodes[0]; |
| 5192 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5193 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 5194 | BUG_ON(ret); |
| 5195 | free_extent_buffer(eb); |
| 5196 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5197 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5198 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5199 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5200 | return 0; |
| 5201 | } |
| 5202 | |
| 5203 | static int noinline relocate_tree_block(struct btrfs_trans_handle *trans, |
| 5204 | struct btrfs_root *root, |
| 5205 | struct btrfs_path *path, |
| 5206 | struct btrfs_key *first_key, |
| 5207 | struct btrfs_ref_path *ref_path) |
| 5208 | { |
| 5209 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5210 | |
| 5211 | ret = relocate_one_path(trans, root, path, first_key, |
| 5212 | ref_path, NULL, NULL); |
| 5213 | BUG_ON(ret); |
| 5214 | |
| 5215 | if (root == root->fs_info->extent_root) |
| 5216 | btrfs_extent_post_op(trans, root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5217 | |
| 5218 | return 0; |
| 5219 | } |
| 5220 | |
| 5221 | static int noinline del_extent_zero(struct btrfs_trans_handle *trans, |
| 5222 | struct btrfs_root *extent_root, |
| 5223 | struct btrfs_path *path, |
| 5224 | struct btrfs_key *extent_key) |
| 5225 | { |
| 5226 | int ret; |
| 5227 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5228 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 5229 | if (ret) |
| 5230 | goto out; |
| 5231 | ret = btrfs_del_item(trans, extent_root, path); |
| 5232 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5233 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5234 | return ret; |
| 5235 | } |
| 5236 | |
| 5237 | static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info, |
| 5238 | struct btrfs_ref_path *ref_path) |
| 5239 | { |
| 5240 | struct btrfs_key root_key; |
| 5241 | |
| 5242 | root_key.objectid = ref_path->root_objectid; |
| 5243 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5244 | if (is_cowonly_root(ref_path->root_objectid)) |
| 5245 | root_key.offset = 0; |
| 5246 | else |
| 5247 | root_key.offset = (u64)-1; |
| 5248 | |
| 5249 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5250 | } |
| 5251 | |
| 5252 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
| 5253 | struct btrfs_path *path, |
| 5254 | struct btrfs_key *extent_key, |
| 5255 | struct btrfs_block_group_cache *group, |
| 5256 | struct inode *reloc_inode, int pass) |
| 5257 | { |
| 5258 | struct btrfs_trans_handle *trans; |
| 5259 | struct btrfs_root *found_root; |
| 5260 | struct btrfs_ref_path *ref_path = NULL; |
| 5261 | struct disk_extent *new_extents = NULL; |
| 5262 | int nr_extents = 0; |
| 5263 | int loops; |
| 5264 | int ret; |
| 5265 | int level; |
| 5266 | struct btrfs_key first_key; |
| 5267 | u64 prev_block = 0; |
| 5268 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5269 | |
| 5270 | trans = btrfs_start_transaction(extent_root, 1); |
| 5271 | BUG_ON(!trans); |
| 5272 | |
| 5273 | if (extent_key->objectid == 0) { |
| 5274 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 5275 | goto out; |
| 5276 | } |
| 5277 | |
| 5278 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 5279 | if (!ref_path) { |
| 5280 | ret = -ENOMEM; |
| 5281 | goto out; |
| 5282 | } |
| 5283 | |
| 5284 | for (loops = 0; ; loops++) { |
| 5285 | if (loops == 0) { |
| 5286 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 5287 | extent_key->objectid); |
| 5288 | } else { |
| 5289 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 5290 | } |
| 5291 | if (ret < 0) |
| 5292 | goto out; |
| 5293 | if (ret > 0) |
| 5294 | break; |
| 5295 | |
| 5296 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5297 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 5298 | continue; |
| 5299 | |
| 5300 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 5301 | BUG_ON(!found_root); |
| 5302 | /* |
| 5303 | * for reference counted tree, only process reference paths |
| 5304 | * rooted at the latest committed root. |
| 5305 | */ |
| 5306 | if (found_root->ref_cows && |
| 5307 | ref_path->root_generation != found_root->root_key.offset) |
| 5308 | continue; |
| 5309 | |
| 5310 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5311 | if (pass == 0) { |
| 5312 | /* |
| 5313 | * copy data extents to new locations |
| 5314 | */ |
| 5315 | u64 group_start = group->key.objectid; |
| 5316 | ret = relocate_data_extent(reloc_inode, |
| 5317 | extent_key, |
| 5318 | group_start); |
| 5319 | if (ret < 0) |
| 5320 | goto out; |
| 5321 | break; |
| 5322 | } |
| 5323 | level = 0; |
| 5324 | } else { |
| 5325 | level = ref_path->owner_objectid; |
| 5326 | } |
| 5327 | |
| 5328 | if (prev_block != ref_path->nodes[level]) { |
| 5329 | struct extent_buffer *eb; |
| 5330 | u64 block_start = ref_path->nodes[level]; |
| 5331 | u64 block_size = btrfs_level_size(found_root, level); |
| 5332 | |
| 5333 | eb = read_tree_block(found_root, block_start, |
| 5334 | block_size, 0); |
| 5335 | btrfs_tree_lock(eb); |
| 5336 | BUG_ON(level != btrfs_header_level(eb)); |
| 5337 | |
| 5338 | if (level == 0) |
| 5339 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 5340 | else |
| 5341 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 5342 | |
| 5343 | btrfs_tree_unlock(eb); |
| 5344 | free_extent_buffer(eb); |
| 5345 | prev_block = block_start; |
| 5346 | } |
| 5347 | |
| 5348 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID && |
| 5349 | pass >= 2) { |
| 5350 | /* |
| 5351 | * use fallback method to process the remaining |
| 5352 | * references. |
| 5353 | */ |
| 5354 | if (!new_extents) { |
| 5355 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5356 | new_extents = kmalloc(sizeof(*new_extents), |
| 5357 | GFP_NOFS); |
| 5358 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5359 | ret = get_new_locations(reloc_inode, |
| 5360 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5361 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5362 | &new_extents, |
| 5363 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5364 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5365 | goto out; |
| 5366 | } |
| 5367 | btrfs_record_root_in_trans(found_root); |
| 5368 | ret = replace_one_extent(trans, found_root, |
| 5369 | path, extent_key, |
| 5370 | &first_key, ref_path, |
| 5371 | new_extents, nr_extents); |
| 5372 | if (ret < 0) |
| 5373 | goto out; |
| 5374 | continue; |
| 5375 | } |
| 5376 | |
| 5377 | btrfs_record_root_in_trans(found_root); |
| 5378 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 5379 | ret = relocate_tree_block(trans, found_root, path, |
| 5380 | &first_key, ref_path); |
| 5381 | } else { |
| 5382 | /* |
| 5383 | * try to update data extent references while |
| 5384 | * keeping metadata shared between snapshots. |
| 5385 | */ |
| 5386 | ret = relocate_one_path(trans, found_root, path, |
| 5387 | &first_key, ref_path, |
| 5388 | group, reloc_inode); |
| 5389 | } |
| 5390 | if (ret < 0) |
| 5391 | goto out; |
| 5392 | } |
| 5393 | ret = 0; |
| 5394 | out: |
| 5395 | btrfs_end_transaction(trans, extent_root); |
| 5396 | kfree(new_extents); |
| 5397 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5398 | return ret; |
| 5399 | } |
| 5400 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5401 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 5402 | { |
| 5403 | u64 num_devices; |
| 5404 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 5405 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 5406 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5407 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5408 | if (num_devices == 1) { |
| 5409 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5410 | stripped = flags & ~stripped; |
| 5411 | |
| 5412 | /* turn raid0 into single device chunks */ |
| 5413 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 5414 | return stripped; |
| 5415 | |
| 5416 | /* turn mirroring into duplication */ |
| 5417 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5418 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5419 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 5420 | return flags; |
| 5421 | } else { |
| 5422 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5423 | if (flags & stripped) |
| 5424 | return flags; |
| 5425 | |
| 5426 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5427 | stripped = flags & ~stripped; |
| 5428 | |
| 5429 | /* switch duplicated blocks with raid1 */ |
| 5430 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 5431 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 5432 | |
| 5433 | /* turn single device chunks into raid0 */ |
| 5434 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 5435 | } |
| 5436 | return flags; |
| 5437 | } |
| 5438 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 5439 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5440 | struct btrfs_block_group_cache *shrink_block_group, |
| 5441 | int force) |
| 5442 | { |
| 5443 | struct btrfs_trans_handle *trans; |
| 5444 | u64 new_alloc_flags; |
| 5445 | u64 calc; |
| 5446 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5447 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5448 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5449 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5450 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5451 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5452 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5453 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5454 | new_alloc_flags = update_block_group_flags(root, |
| 5455 | shrink_block_group->flags); |
| 5456 | if (new_alloc_flags != shrink_block_group->flags) { |
| 5457 | calc = |
| 5458 | btrfs_block_group_used(&shrink_block_group->item); |
| 5459 | } else { |
| 5460 | calc = shrink_block_group->key.offset; |
| 5461 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5462 | spin_unlock(&shrink_block_group->lock); |
| 5463 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5464 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5465 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5466 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5467 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5468 | } else |
| 5469 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5470 | return 0; |
| 5471 | } |
| 5472 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5473 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 5474 | struct btrfs_root *root, |
| 5475 | u64 objectid, u64 size) |
| 5476 | { |
| 5477 | struct btrfs_path *path; |
| 5478 | struct btrfs_inode_item *item; |
| 5479 | struct extent_buffer *leaf; |
| 5480 | int ret; |
| 5481 | |
| 5482 | path = btrfs_alloc_path(); |
| 5483 | if (!path) |
| 5484 | return -ENOMEM; |
| 5485 | |
| 5486 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 5487 | if (ret) |
| 5488 | goto out; |
| 5489 | |
| 5490 | leaf = path->nodes[0]; |
| 5491 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 5492 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 5493 | btrfs_set_inode_generation(leaf, item, 1); |
| 5494 | btrfs_set_inode_size(leaf, item, size); |
| 5495 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5496 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM | |
| 5497 | BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5498 | btrfs_mark_buffer_dirty(leaf); |
| 5499 | btrfs_release_path(root, path); |
| 5500 | out: |
| 5501 | btrfs_free_path(path); |
| 5502 | return ret; |
| 5503 | } |
| 5504 | |
| 5505 | static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info, |
| 5506 | struct btrfs_block_group_cache *group) |
| 5507 | { |
| 5508 | struct inode *inode = NULL; |
| 5509 | struct btrfs_trans_handle *trans; |
| 5510 | struct btrfs_root *root; |
| 5511 | struct btrfs_key root_key; |
| 5512 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 5513 | int err = 0; |
| 5514 | |
| 5515 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5516 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5517 | root_key.offset = (u64)-1; |
| 5518 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5519 | if (IS_ERR(root)) |
| 5520 | return ERR_CAST(root); |
| 5521 | |
| 5522 | trans = btrfs_start_transaction(root, 1); |
| 5523 | BUG_ON(!trans); |
| 5524 | |
| 5525 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 5526 | if (err) |
| 5527 | goto out; |
| 5528 | |
| 5529 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 5530 | BUG_ON(err); |
| 5531 | |
| 5532 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5533 | group->key.offset, 0, group->key.offset, |
| 5534 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5535 | BUG_ON(err); |
| 5536 | |
| 5537 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 5538 | if (inode->i_state & I_NEW) { |
| 5539 | BTRFS_I(inode)->root = root; |
| 5540 | BTRFS_I(inode)->location.objectid = objectid; |
| 5541 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 5542 | BTRFS_I(inode)->location.offset = 0; |
| 5543 | btrfs_read_locked_inode(inode); |
| 5544 | unlock_new_inode(inode); |
| 5545 | BUG_ON(is_bad_inode(inode)); |
| 5546 | } else { |
| 5547 | BUG_ON(1); |
| 5548 | } |
| 5549 | |
| 5550 | err = btrfs_orphan_add(trans, inode); |
| 5551 | out: |
| 5552 | btrfs_end_transaction(trans, root); |
| 5553 | if (err) { |
| 5554 | if (inode) |
| 5555 | iput(inode); |
| 5556 | inode = ERR_PTR(err); |
| 5557 | } |
| 5558 | return inode; |
| 5559 | } |
| 5560 | |
| 5561 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5562 | { |
| 5563 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5564 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5565 | struct btrfs_fs_info *info = root->fs_info; |
| 5566 | struct extent_buffer *leaf; |
| 5567 | struct inode *reloc_inode; |
| 5568 | struct btrfs_block_group_cache *block_group; |
| 5569 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5570 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5571 | u64 cur_byte; |
| 5572 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5573 | u32 nritems; |
| 5574 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5575 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5576 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5577 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5578 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5579 | |
| 5580 | block_group = btrfs_lookup_block_group(info, group_start); |
| 5581 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5582 | |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5583 | printk("btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5584 | (unsigned long long)block_group->key.objectid, |
| 5585 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5586 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5587 | path = btrfs_alloc_path(); |
| 5588 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 5589 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5590 | reloc_inode = create_reloc_inode(info, block_group); |
| 5591 | BUG_ON(IS_ERR(reloc_inode)); |
| 5592 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5593 | __alloc_chunk_for_shrink(root, block_group, 1); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5594 | set_block_group_readonly(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5595 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5596 | btrfs_start_delalloc_inodes(info->tree_root); |
| 5597 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5598 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5599 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5600 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5601 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5602 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5603 | key.offset = 0; |
| 5604 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5605 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 5606 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5607 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5608 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5609 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5610 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 5611 | btrfs_clean_old_snapshots(info->tree_root); |
| 5612 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 5613 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 5614 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5615 | while(1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5616 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5617 | if (ret < 0) |
| 5618 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5619 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5620 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5621 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5622 | if (path->slots[0] >= nritems) { |
| 5623 | ret = btrfs_next_leaf(root, path); |
| 5624 | if (ret < 0) |
| 5625 | goto out; |
| 5626 | if (ret == 1) { |
| 5627 | ret = 0; |
| 5628 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5629 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5630 | leaf = path->nodes[0]; |
| 5631 | nritems = btrfs_header_nritems(leaf); |
| 5632 | } |
| 5633 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5634 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5635 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5636 | if (key.objectid >= block_group->key.objectid + |
| 5637 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5638 | break; |
| 5639 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5640 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5641 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5642 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5643 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5644 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 5645 | } |
| 5646 | progress = 1; |
| 5647 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5648 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 5649 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5650 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5651 | goto next; |
| 5652 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 5653 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5654 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5655 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5656 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5657 | |
| 5658 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 5659 | ret = relocate_one_extent(root, path, &key, block_group, |
| 5660 | reloc_inode, pass); |
| 5661 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5662 | if (ret > 0) |
| 5663 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5664 | |
| 5665 | key.objectid = cur_byte; |
| 5666 | key.type = 0; |
| 5667 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5668 | } |
| 5669 | |
| 5670 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5671 | |
| 5672 | if (pass == 0) { |
| 5673 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 5674 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
| 5675 | WARN_ON(reloc_inode->i_mapping->nrpages); |
| 5676 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5677 | |
| 5678 | if (total_found > 0) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5679 | printk("btrfs found %llu extents in pass %d\n", |
| 5680 | (unsigned long long)total_found, pass); |
| 5681 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5682 | if (total_found == skipped && pass > 2) { |
| 5683 | iput(reloc_inode); |
| 5684 | reloc_inode = create_reloc_inode(info, block_group); |
| 5685 | pass = 0; |
| 5686 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5687 | goto again; |
| 5688 | } |
| 5689 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5690 | /* delete reloc_inode */ |
| 5691 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5692 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5693 | /* unpin extents in this range */ |
| 5694 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 5695 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5696 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5697 | spin_lock(&block_group->lock); |
| 5698 | WARN_ON(block_group->pinned > 0); |
| 5699 | WARN_ON(block_group->reserved > 0); |
| 5700 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 5701 | spin_unlock(&block_group->lock); |
| 5702 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5703 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5704 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5705 | return ret; |
| 5706 | } |
| 5707 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 5708 | static int find_first_block_group(struct btrfs_root *root, |
| 5709 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5710 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5711 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5712 | struct btrfs_key found_key; |
| 5713 | struct extent_buffer *leaf; |
| 5714 | int slot; |
| 5715 | |
| 5716 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 5717 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5718 | goto out; |
| 5719 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5720 | while(1) { |
| 5721 | slot = path->slots[0]; |
| 5722 | leaf = path->nodes[0]; |
| 5723 | if (slot >= btrfs_header_nritems(leaf)) { |
| 5724 | ret = btrfs_next_leaf(root, path); |
| 5725 | if (ret == 0) |
| 5726 | continue; |
| 5727 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5728 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5729 | break; |
| 5730 | } |
| 5731 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 5732 | |
| 5733 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5734 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 5735 | ret = 0; |
| 5736 | goto out; |
| 5737 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5738 | path->slots[0]++; |
| 5739 | } |
| 5740 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5741 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5742 | return ret; |
| 5743 | } |
| 5744 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5745 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 5746 | { |
| 5747 | struct btrfs_block_group_cache *block_group; |
| 5748 | struct rb_node *n; |
| 5749 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5750 | spin_lock(&info->block_group_cache_lock); |
| 5751 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 5752 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 5753 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5754 | rb_erase(&block_group->cache_node, |
| 5755 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5756 | spin_unlock(&info->block_group_cache_lock); |
| 5757 | |
| 5758 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5759 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5760 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5761 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5762 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5763 | |
| 5764 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5765 | } |
| 5766 | spin_unlock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5767 | return 0; |
| 5768 | } |
| 5769 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5770 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 5771 | { |
| 5772 | struct btrfs_path *path; |
| 5773 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5774 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5775 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5776 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5777 | struct btrfs_key key; |
| 5778 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5779 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5780 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 5781 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5782 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5783 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5784 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5785 | path = btrfs_alloc_path(); |
| 5786 | if (!path) |
| 5787 | return -ENOMEM; |
| 5788 | |
| 5789 | while(1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5790 | ret = find_first_block_group(root, path, &key); |
| 5791 | if (ret > 0) { |
| 5792 | ret = 0; |
| 5793 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5794 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5795 | if (ret != 0) |
| 5796 | goto error; |
| 5797 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5798 | leaf = path->nodes[0]; |
| 5799 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5800 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5801 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5802 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5803 | break; |
| 5804 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5805 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5806 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5807 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5808 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5809 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5810 | read_extent_buffer(leaf, &cache->item, |
| 5811 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 5812 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5813 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5814 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5815 | key.objectid = found_key.objectid + found_key.offset; |
| 5816 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5817 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 5818 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5819 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 5820 | btrfs_block_group_used(&cache->item), |
| 5821 | &space_info); |
| 5822 | BUG_ON(ret); |
| 5823 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5824 | down_write(&space_info->groups_sem); |
| 5825 | list_add_tail(&cache->list, &space_info->block_groups); |
| 5826 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5827 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5828 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5829 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 5830 | |
| 5831 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5832 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 5833 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5834 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5835 | ret = 0; |
| 5836 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5837 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5838 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5839 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5840 | |
| 5841 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 5842 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5843 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5844 | u64 size) |
| 5845 | { |
| 5846 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5847 | struct btrfs_root *extent_root; |
| 5848 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5849 | |
| 5850 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5851 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5852 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
| 5853 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 5854 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5855 | if (!cache) |
| 5856 | return -ENOMEM; |
| 5857 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 5858 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5859 | cache->key.offset = size; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5860 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5861 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5862 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5863 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5864 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5865 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5866 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5867 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 5868 | cache->flags = type; |
| 5869 | btrfs_set_block_group_flags(&cache->item, type); |
| 5870 | |
| 5871 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 5872 | &cache->space_info); |
| 5873 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5874 | down_write(&cache->space_info->groups_sem); |
| 5875 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 5876 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5877 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5878 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 5879 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5880 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5881 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 5882 | sizeof(cache->item)); |
| 5883 | BUG_ON(ret); |
| 5884 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 5885 | finish_current_insert(trans, extent_root, 0); |
| 5886 | ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5887 | BUG_ON(ret); |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 5888 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5889 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5890 | return 0; |
| 5891 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5892 | |
| 5893 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 5894 | struct btrfs_root *root, u64 group_start) |
| 5895 | { |
| 5896 | struct btrfs_path *path; |
| 5897 | struct btrfs_block_group_cache *block_group; |
| 5898 | struct btrfs_key key; |
| 5899 | int ret; |
| 5900 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5901 | root = root->fs_info->extent_root; |
| 5902 | |
| 5903 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 5904 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5905 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5906 | |
| 5907 | memcpy(&key, &block_group->key, sizeof(key)); |
| 5908 | |
| 5909 | path = btrfs_alloc_path(); |
| 5910 | BUG_ON(!path); |
| 5911 | |
| 5912 | btrfs_remove_free_space_cache(block_group); |
| 5913 | rb_erase(&block_group->cache_node, |
| 5914 | &root->fs_info->block_group_cache_tree); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5915 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5916 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5917 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5918 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5919 | spin_lock(&block_group->space_info->lock); |
| 5920 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 5921 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 5922 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5923 | block_group->space_info->full = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 5924 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5925 | /* |
| 5926 | memset(shrink_block_group, 0, sizeof(*shrink_block_group)); |
| 5927 | kfree(shrink_block_group); |
| 5928 | */ |
| 5929 | |
| 5930 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 5931 | if (ret > 0) |
| 5932 | ret = -EIO; |
| 5933 | if (ret < 0) |
| 5934 | goto out; |
| 5935 | |
| 5936 | ret = btrfs_del_item(trans, root, path); |
| 5937 | out: |
| 5938 | btrfs_free_path(path); |
| 5939 | return ret; |
| 5940 | } |