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 | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 22 | #include <linux/sort.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 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 | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 33 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 34 | #define PENDING_EXTENT_INSERT 0 |
| 35 | #define PENDING_EXTENT_DELETE 1 |
| 36 | #define PENDING_BACKREF_UPDATE 2 |
| 37 | |
| 38 | struct pending_extent_op { |
| 39 | int type; |
| 40 | u64 bytenr; |
| 41 | u64 num_bytes; |
| 42 | u64 parent; |
| 43 | u64 orig_parent; |
| 44 | u64 generation; |
| 45 | u64 orig_generation; |
| 46 | int level; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 47 | struct list_head list; |
| 48 | int del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 51 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
| 52 | struct btrfs_root *extent_root, int all); |
| 53 | static int del_pending_extents(struct btrfs_trans_handle *trans, |
| 54 | struct btrfs_root *extent_root, int all); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 55 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 56 | struct btrfs_root *root, |
| 57 | u64 bytenr, u64 num_bytes, int is_data); |
| 58 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 59 | struct btrfs_root *root, |
| 60 | u64 bytenr, u64 num_bytes, int alloc, |
| 61 | int mark_free); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 62 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame^] | 63 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 64 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 65 | u64 flags, int force); |
| 66 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 67 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 68 | { |
| 69 | return (cache->flags & bits) == bits; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * this adds the block group to the fs_info rb tree for the block group |
| 74 | * cache |
| 75 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 76 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 77 | struct btrfs_block_group_cache *block_group) |
| 78 | { |
| 79 | struct rb_node **p; |
| 80 | struct rb_node *parent = NULL; |
| 81 | struct btrfs_block_group_cache *cache; |
| 82 | |
| 83 | spin_lock(&info->block_group_cache_lock); |
| 84 | p = &info->block_group_cache_tree.rb_node; |
| 85 | |
| 86 | while (*p) { |
| 87 | parent = *p; |
| 88 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 89 | cache_node); |
| 90 | if (block_group->key.objectid < cache->key.objectid) { |
| 91 | p = &(*p)->rb_left; |
| 92 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 93 | p = &(*p)->rb_right; |
| 94 | } else { |
| 95 | spin_unlock(&info->block_group_cache_lock); |
| 96 | return -EEXIST; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | rb_link_node(&block_group->cache_node, parent, p); |
| 101 | rb_insert_color(&block_group->cache_node, |
| 102 | &info->block_group_cache_tree); |
| 103 | spin_unlock(&info->block_group_cache_lock); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * This will return the block group at or after bytenr if contains is 0, else |
| 110 | * it will return the block group that contains the bytenr |
| 111 | */ |
| 112 | static struct btrfs_block_group_cache * |
| 113 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 114 | int contains) |
| 115 | { |
| 116 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 117 | struct rb_node *n; |
| 118 | u64 end, start; |
| 119 | |
| 120 | spin_lock(&info->block_group_cache_lock); |
| 121 | n = info->block_group_cache_tree.rb_node; |
| 122 | |
| 123 | while (n) { |
| 124 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 125 | cache_node); |
| 126 | end = cache->key.objectid + cache->key.offset - 1; |
| 127 | start = cache->key.objectid; |
| 128 | |
| 129 | if (bytenr < start) { |
| 130 | if (!contains && (!ret || start < ret->key.objectid)) |
| 131 | ret = cache; |
| 132 | n = n->rb_left; |
| 133 | } else if (bytenr > start) { |
| 134 | if (contains && bytenr <= end) { |
| 135 | ret = cache; |
| 136 | break; |
| 137 | } |
| 138 | n = n->rb_right; |
| 139 | } else { |
| 140 | ret = cache; |
| 141 | break; |
| 142 | } |
| 143 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 144 | if (ret) |
| 145 | atomic_inc(&ret->count); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 146 | spin_unlock(&info->block_group_cache_lock); |
| 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * this is only called by cache_block_group, since we could have freed extents |
| 153 | * we need to check the pinned_extents for any extents that can't be used yet |
| 154 | * since their free space will be released as soon as the transaction commits. |
| 155 | */ |
| 156 | static int add_new_free_space(struct btrfs_block_group_cache *block_group, |
| 157 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 158 | { |
| 159 | u64 extent_start, extent_end, size; |
| 160 | int ret; |
| 161 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 162 | mutex_lock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 163 | while (start < end) { |
| 164 | ret = find_first_extent_bit(&info->pinned_extents, start, |
| 165 | &extent_start, &extent_end, |
| 166 | EXTENT_DIRTY); |
| 167 | if (ret) |
| 168 | break; |
| 169 | |
| 170 | if (extent_start == start) { |
| 171 | start = extent_end + 1; |
| 172 | } else if (extent_start > start && extent_start < end) { |
| 173 | size = extent_start - start; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 174 | ret = btrfs_add_free_space(block_group, start, |
| 175 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 176 | BUG_ON(ret); |
| 177 | start = extent_end + 1; |
| 178 | } else { |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (start < end) { |
| 184 | size = end - start; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 185 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 186 | BUG_ON(ret); |
| 187 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 188 | mutex_unlock(&info->pinned_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 193 | static int remove_sb_from_cache(struct btrfs_root *root, |
| 194 | struct btrfs_block_group_cache *cache) |
| 195 | { |
| 196 | u64 bytenr; |
| 197 | u64 *logical; |
| 198 | int stripe_len; |
| 199 | int i, nr, ret; |
| 200 | |
| 201 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 202 | bytenr = btrfs_sb_offset(i); |
| 203 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 204 | cache->key.objectid, bytenr, 0, |
| 205 | &logical, &nr, &stripe_len); |
| 206 | BUG_ON(ret); |
| 207 | while (nr--) { |
| 208 | btrfs_remove_free_space(cache, logical[nr], |
| 209 | stripe_len); |
| 210 | } |
| 211 | kfree(logical); |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 216 | static int cache_block_group(struct btrfs_root *root, |
| 217 | struct btrfs_block_group_cache *block_group) |
| 218 | { |
| 219 | struct btrfs_path *path; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 220 | int ret = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 221 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 222 | struct extent_buffer *leaf; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 223 | int slot; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 224 | u64 last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 225 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 226 | if (!block_group) |
| 227 | return 0; |
| 228 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 229 | root = root->fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 230 | |
| 231 | if (block_group->cached) |
| 232 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 233 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 234 | path = btrfs_alloc_path(); |
| 235 | if (!path) |
| 236 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 237 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 238 | path->reada = 2; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 239 | /* |
| 240 | * we get into deadlocks with paths held by callers of this function. |
| 241 | * since the alloc_mutex is protecting things right now, just |
| 242 | * skip the locking here |
| 243 | */ |
| 244 | path->skip_locking = 1; |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 245 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
| 246 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 247 | key.offset = 0; |
| 248 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 249 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 250 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 251 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 252 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 253 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 254 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 255 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 256 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 257 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 258 | if (ret < 0) |
| 259 | goto err; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 260 | if (ret == 0) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 261 | continue; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 262 | else |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 263 | break; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 264 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 265 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 266 | if (key.objectid < block_group->key.objectid) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 267 | goto next; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 268 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 269 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 270 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 271 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 272 | |
| 273 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 274 | add_new_free_space(block_group, root->fs_info, last, |
| 275 | key.objectid); |
| 276 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 277 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 278 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 279 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 280 | path->slots[0]++; |
| 281 | } |
| 282 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 283 | add_new_free_space(block_group, root->fs_info, last, |
| 284 | block_group->key.objectid + |
| 285 | block_group->key.offset); |
| 286 | |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 287 | remove_sb_from_cache(root, block_group); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 288 | block_group->cached = 1; |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 289 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 290 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 291 | btrfs_free_path(path); |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 292 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 295 | /* |
| 296 | * return the block group that starts at or after bytenr |
| 297 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 298 | static struct btrfs_block_group_cache * |
| 299 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 300 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 301 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 302 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 303 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 304 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 305 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 306 | } |
| 307 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 308 | /* |
| 309 | * return the block group that contains teh given bytenr |
| 310 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 311 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 312 | struct btrfs_fs_info *info, |
| 313 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 314 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 315 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 316 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 317 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 318 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 319 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 320 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 321 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 322 | static inline void put_block_group(struct btrfs_block_group_cache *cache) |
| 323 | { |
| 324 | if (atomic_dec_and_test(&cache->count)) |
| 325 | kfree(cache); |
| 326 | } |
| 327 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 328 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 329 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 330 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 331 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 332 | struct btrfs_space_info *found; |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 333 | list_for_each_entry(found, head, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 334 | if (found->flags == flags) |
| 335 | return found; |
| 336 | } |
| 337 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 338 | } |
| 339 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 340 | static u64 div_factor(u64 num, int factor) |
| 341 | { |
| 342 | if (factor == 10) |
| 343 | return num; |
| 344 | num *= factor; |
| 345 | do_div(num, 10); |
| 346 | return num; |
| 347 | } |
| 348 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 349 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 350 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 351 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 352 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 353 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 354 | u64 last = max(search_hint, search_start); |
| 355 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 356 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 357 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 358 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 359 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 360 | while (1) { |
| 361 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 362 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 363 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 364 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 365 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 366 | last = cache->key.objectid + cache->key.offset; |
| 367 | used = btrfs_block_group_used(&cache->item); |
| 368 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 369 | if ((full_search || !cache->ro) && |
| 370 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 371 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 372 | div_factor(cache->key.offset, factor)) { |
| 373 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 374 | spin_unlock(&cache->lock); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 375 | put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 376 | goto found; |
| 377 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 378 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 379 | spin_unlock(&cache->lock); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 380 | put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 381 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 382 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 383 | if (!wrapped) { |
| 384 | last = search_start; |
| 385 | wrapped = 1; |
| 386 | goto again; |
| 387 | } |
| 388 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 389 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 390 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 391 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 392 | goto again; |
| 393 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 394 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 395 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 396 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 397 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 398 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 399 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 400 | { |
| 401 | int ret; |
| 402 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 403 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 404 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 405 | path = btrfs_alloc_path(); |
| 406 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 407 | key.objectid = start; |
| 408 | key.offset = len; |
| 409 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 410 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 411 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 412 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 413 | return ret; |
| 414 | } |
| 415 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 416 | /* |
| 417 | * Back reference rules. Back refs have three main goals: |
| 418 | * |
| 419 | * 1) differentiate between all holders of references to an extent so that |
| 420 | * when a reference is dropped we can make sure it was a valid reference |
| 421 | * before freeing the extent. |
| 422 | * |
| 423 | * 2) Provide enough information to quickly find the holders of an extent |
| 424 | * if we notice a given block is corrupted or bad. |
| 425 | * |
| 426 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 427 | * maintenance. This is actually the same as #2, but with a slightly |
| 428 | * different use case. |
| 429 | * |
| 430 | * File extents can be referenced by: |
| 431 | * |
| 432 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 433 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 434 | * - different offsets inside a file (bookend extents in file.c) |
| 435 | * |
| 436 | * The extent ref structure has fields for: |
| 437 | * |
| 438 | * - Objectid of the subvolume root |
| 439 | * - Generation number of the tree holding the reference |
| 440 | * - objectid of the file holding the reference |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 441 | * - number of references holding by parent node (alway 1 for tree blocks) |
| 442 | * |
| 443 | * Btree leaf may hold multiple references to a file extent. In most cases, |
| 444 | * these references are from same file and the corresponding offsets inside |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 445 | * the file are close together. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 446 | * |
| 447 | * When a file extent is allocated the fields are filled in: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 448 | * (root_key.objectid, trans->transid, inode objectid, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 449 | * |
| 450 | * 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] | 451 | * in the leaf. It looks similar to the create case, but trans->transid will |
| 452 | * be different when the block is cow'd. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 453 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 454 | * (root_key.objectid, trans->transid, inode objectid, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 455 | * number of references in the leaf) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 456 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 457 | * When a file extent is removed either during snapshot deletion or |
| 458 | * file truncation, we find the corresponding back reference and check |
| 459 | * the following fields: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 460 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 461 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 462 | * inode objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 463 | * |
| 464 | * Btree extents can be referenced by: |
| 465 | * |
| 466 | * - Different subvolumes |
| 467 | * - Different generations of the same subvolume |
| 468 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 469 | * When a tree block is created, back references are inserted: |
| 470 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 471 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 472 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 473 | * When a tree block is cow'd, new back references are added for all the |
| 474 | * blocks it points to. If the tree block isn't in reference counted root, |
| 475 | * the old back references are removed. These new back references are of |
| 476 | * the form (trans->transid will have increased since creation): |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 477 | * |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 478 | * (root->root_key.objectid, trans->transid, level, 1) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 479 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 480 | * When a backref is in deleting, the following fields are checked: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 481 | * |
| 482 | * if backref was for a tree root: |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 483 | * (btrfs_header_owner(itself), btrfs_header_generation(itself), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 484 | * else |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 485 | * (btrfs_header_owner(parent), btrfs_header_generation(parent), level) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 486 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 487 | * Back Reference Key composing: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 488 | * |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 489 | * The key objectid corresponds to the first byte in the extent, the key |
| 490 | * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first |
| 491 | * byte of parent extent. If a extent is tree root, the key offset is set |
| 492 | * to the key objectid. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 493 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 494 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 495 | static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 496 | struct btrfs_root *root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 497 | struct btrfs_path *path, |
| 498 | u64 bytenr, u64 parent, |
| 499 | u64 ref_root, u64 ref_generation, |
| 500 | u64 owner_objectid, int del) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 501 | { |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 502 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 503 | struct btrfs_extent_ref *ref; |
| 504 | struct extent_buffer *leaf; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 505 | u64 ref_objectid; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 506 | int ret; |
| 507 | |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 508 | key.objectid = bytenr; |
| 509 | key.type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 510 | key.offset = parent; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 511 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 512 | ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1); |
| 513 | if (ret < 0) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 514 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 515 | if (ret > 0) { |
| 516 | ret = -ENOENT; |
| 517 | goto out; |
| 518 | } |
| 519 | |
| 520 | leaf = path->nodes[0]; |
| 521 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 522 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 523 | if (btrfs_ref_root(leaf, ref) != ref_root || |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 524 | btrfs_ref_generation(leaf, ref) != ref_generation || |
| 525 | (ref_objectid != owner_objectid && |
| 526 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 527 | ret = -EIO; |
| 528 | WARN_ON(1); |
| 529 | goto out; |
| 530 | } |
| 531 | ret = 0; |
| 532 | out: |
| 533 | return ret; |
| 534 | } |
| 535 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 536 | /* |
| 537 | * updates all the backrefs that are pending on update_list for the |
| 538 | * extent_root |
| 539 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 540 | static noinline int update_backrefs(struct btrfs_trans_handle *trans, |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 541 | struct btrfs_root *extent_root, |
| 542 | struct btrfs_path *path, |
| 543 | struct list_head *update_list) |
| 544 | { |
| 545 | struct btrfs_key key; |
| 546 | struct btrfs_extent_ref *ref; |
| 547 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 548 | struct pending_extent_op *op; |
| 549 | struct extent_buffer *leaf; |
| 550 | int ret = 0; |
| 551 | struct list_head *cur = update_list->next; |
| 552 | u64 ref_objectid; |
| 553 | u64 ref_root = extent_root->root_key.objectid; |
| 554 | |
| 555 | op = list_entry(cur, struct pending_extent_op, list); |
| 556 | |
| 557 | search: |
| 558 | key.objectid = op->bytenr; |
| 559 | key.type = BTRFS_EXTENT_REF_KEY; |
| 560 | key.offset = op->orig_parent; |
| 561 | |
| 562 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1); |
| 563 | BUG_ON(ret); |
| 564 | |
| 565 | leaf = path->nodes[0]; |
| 566 | |
| 567 | loop: |
| 568 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 569 | |
| 570 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 571 | |
| 572 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 573 | btrfs_ref_generation(leaf, ref) != op->orig_generation || |
| 574 | (ref_objectid != op->level && |
| 575 | ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 576 | printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, " |
| 577 | "root %llu, owner %u\n", |
| 578 | (unsigned long long)op->bytenr, |
| 579 | (unsigned long long)op->orig_parent, |
| 580 | (unsigned long long)ref_root, op->level); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 581 | btrfs_print_leaf(extent_root, leaf); |
| 582 | BUG(); |
| 583 | } |
| 584 | |
| 585 | key.objectid = op->bytenr; |
| 586 | key.offset = op->parent; |
| 587 | key.type = BTRFS_EXTENT_REF_KEY; |
| 588 | ret = btrfs_set_item_key_safe(trans, extent_root, path, &key); |
| 589 | BUG_ON(ret); |
| 590 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 591 | btrfs_set_ref_generation(leaf, ref, op->generation); |
| 592 | |
| 593 | cur = cur->next; |
| 594 | |
| 595 | list_del_init(&op->list); |
| 596 | unlock_extent(&info->extent_ins, op->bytenr, |
| 597 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 598 | kfree(op); |
| 599 | |
| 600 | if (cur == update_list) { |
| 601 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 602 | btrfs_release_path(extent_root, path); |
| 603 | goto out; |
| 604 | } |
| 605 | |
| 606 | op = list_entry(cur, struct pending_extent_op, list); |
| 607 | |
| 608 | path->slots[0]++; |
| 609 | while (path->slots[0] < btrfs_header_nritems(leaf)) { |
| 610 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 611 | if (key.objectid == op->bytenr && |
| 612 | key.type == BTRFS_EXTENT_REF_KEY) |
| 613 | goto loop; |
| 614 | path->slots[0]++; |
| 615 | } |
| 616 | |
| 617 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 618 | btrfs_release_path(extent_root, path); |
| 619 | goto search; |
| 620 | |
| 621 | out: |
| 622 | return 0; |
| 623 | } |
| 624 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 625 | static noinline int insert_extents(struct btrfs_trans_handle *trans, |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 626 | struct btrfs_root *extent_root, |
| 627 | struct btrfs_path *path, |
| 628 | struct list_head *insert_list, int nr) |
| 629 | { |
| 630 | struct btrfs_key *keys; |
| 631 | u32 *data_size; |
| 632 | struct pending_extent_op *op; |
| 633 | struct extent_buffer *leaf; |
| 634 | struct list_head *cur = insert_list->next; |
| 635 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 636 | u64 ref_root = extent_root->root_key.objectid; |
| 637 | int i = 0, last = 0, ret; |
| 638 | int total = nr * 2; |
| 639 | |
| 640 | if (!nr) |
| 641 | return 0; |
| 642 | |
| 643 | keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS); |
| 644 | if (!keys) |
| 645 | return -ENOMEM; |
| 646 | |
| 647 | data_size = kzalloc(total * sizeof(u32), GFP_NOFS); |
| 648 | if (!data_size) { |
| 649 | kfree(keys); |
| 650 | return -ENOMEM; |
| 651 | } |
| 652 | |
| 653 | list_for_each_entry(op, insert_list, list) { |
| 654 | keys[i].objectid = op->bytenr; |
| 655 | keys[i].offset = op->num_bytes; |
| 656 | keys[i].type = BTRFS_EXTENT_ITEM_KEY; |
| 657 | data_size[i] = sizeof(struct btrfs_extent_item); |
| 658 | i++; |
| 659 | |
| 660 | keys[i].objectid = op->bytenr; |
| 661 | keys[i].offset = op->parent; |
| 662 | keys[i].type = BTRFS_EXTENT_REF_KEY; |
| 663 | data_size[i] = sizeof(struct btrfs_extent_ref); |
| 664 | i++; |
| 665 | } |
| 666 | |
| 667 | op = list_entry(cur, struct pending_extent_op, list); |
| 668 | i = 0; |
| 669 | while (i < total) { |
| 670 | int c; |
| 671 | ret = btrfs_insert_some_items(trans, extent_root, path, |
| 672 | keys+i, data_size+i, total-i); |
| 673 | BUG_ON(ret < 0); |
| 674 | |
| 675 | if (last && ret > 1) |
| 676 | BUG(); |
| 677 | |
| 678 | leaf = path->nodes[0]; |
| 679 | for (c = 0; c < ret; c++) { |
| 680 | int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY; |
| 681 | |
| 682 | /* |
| 683 | * if the first item we inserted was a backref, then |
| 684 | * the EXTENT_ITEM will be the odd c's, else it will |
| 685 | * be the even c's |
| 686 | */ |
| 687 | if ((ref_first && (c % 2)) || |
| 688 | (!ref_first && !(c % 2))) { |
| 689 | struct btrfs_extent_item *itm; |
| 690 | |
| 691 | itm = btrfs_item_ptr(leaf, path->slots[0] + c, |
| 692 | struct btrfs_extent_item); |
| 693 | btrfs_set_extent_refs(path->nodes[0], itm, 1); |
| 694 | op->del++; |
| 695 | } else { |
| 696 | struct btrfs_extent_ref *ref; |
| 697 | |
| 698 | ref = btrfs_item_ptr(leaf, path->slots[0] + c, |
| 699 | struct btrfs_extent_ref); |
| 700 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 701 | btrfs_set_ref_generation(leaf, ref, |
| 702 | op->generation); |
| 703 | btrfs_set_ref_objectid(leaf, ref, op->level); |
| 704 | btrfs_set_ref_num_refs(leaf, ref, 1); |
| 705 | op->del++; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * using del to see when its ok to free up the |
| 710 | * pending_extent_op. In the case where we insert the |
| 711 | * last item on the list in order to help do batching |
| 712 | * we need to not free the extent op until we actually |
| 713 | * insert the extent_item |
| 714 | */ |
| 715 | if (op->del == 2) { |
| 716 | unlock_extent(&info->extent_ins, op->bytenr, |
| 717 | op->bytenr + op->num_bytes - 1, |
| 718 | GFP_NOFS); |
| 719 | cur = cur->next; |
| 720 | list_del_init(&op->list); |
| 721 | kfree(op); |
| 722 | if (cur != insert_list) |
| 723 | op = list_entry(cur, |
| 724 | struct pending_extent_op, |
| 725 | list); |
| 726 | } |
| 727 | } |
| 728 | btrfs_mark_buffer_dirty(leaf); |
| 729 | btrfs_release_path(extent_root, path); |
| 730 | |
| 731 | /* |
| 732 | * Ok backref's and items usually go right next to eachother, |
| 733 | * but if we could only insert 1 item that means that we |
| 734 | * inserted on the end of a leaf, and we have no idea what may |
| 735 | * be on the next leaf so we just play it safe. In order to |
| 736 | * try and help this case we insert the last thing on our |
| 737 | * insert list so hopefully it will end up being the last |
| 738 | * thing on the leaf and everything else will be before it, |
| 739 | * which will let us insert a whole bunch of items at the same |
| 740 | * time. |
| 741 | */ |
| 742 | if (ret == 1 && !last && (i + ret < total)) { |
| 743 | /* |
| 744 | * last: where we will pick up the next time around |
| 745 | * i: our current key to insert, will be total - 1 |
| 746 | * cur: the current op we are screwing with |
| 747 | * op: duh |
| 748 | */ |
| 749 | last = i + ret; |
| 750 | i = total - 1; |
| 751 | cur = insert_list->prev; |
| 752 | op = list_entry(cur, struct pending_extent_op, list); |
| 753 | } else if (last) { |
| 754 | /* |
| 755 | * ok we successfully inserted the last item on the |
| 756 | * list, lets reset everything |
| 757 | * |
| 758 | * i: our current key to insert, so where we left off |
| 759 | * last time |
| 760 | * last: done with this |
| 761 | * cur: the op we are messing with |
| 762 | * op: duh |
| 763 | * total: since we inserted the last key, we need to |
| 764 | * decrement total so we dont overflow |
| 765 | */ |
| 766 | i = last; |
| 767 | last = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 768 | total--; |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame] | 769 | if (i < total) { |
| 770 | cur = insert_list->next; |
| 771 | op = list_entry(cur, struct pending_extent_op, |
| 772 | list); |
| 773 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 774 | } else { |
| 775 | i += ret; |
| 776 | } |
| 777 | |
| 778 | cond_resched(); |
| 779 | } |
| 780 | ret = 0; |
| 781 | kfree(keys); |
| 782 | kfree(data_size); |
| 783 | return ret; |
| 784 | } |
| 785 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 786 | static noinline int insert_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 787 | struct btrfs_root *root, |
| 788 | struct btrfs_path *path, |
| 789 | u64 bytenr, u64 parent, |
| 790 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 791 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 792 | { |
| 793 | struct btrfs_key key; |
| 794 | struct extent_buffer *leaf; |
| 795 | struct btrfs_extent_ref *ref; |
| 796 | u32 num_refs; |
| 797 | int ret; |
| 798 | |
| 799 | key.objectid = bytenr; |
| 800 | key.type = BTRFS_EXTENT_REF_KEY; |
| 801 | key.offset = parent; |
| 802 | |
| 803 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref)); |
| 804 | if (ret == 0) { |
| 805 | leaf = path->nodes[0]; |
| 806 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 807 | struct btrfs_extent_ref); |
| 808 | btrfs_set_ref_root(leaf, ref, ref_root); |
| 809 | btrfs_set_ref_generation(leaf, ref, ref_generation); |
| 810 | btrfs_set_ref_objectid(leaf, ref, owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 811 | btrfs_set_ref_num_refs(leaf, ref, 1); |
| 812 | } else if (ret == -EEXIST) { |
| 813 | u64 existing_owner; |
| 814 | BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID); |
| 815 | leaf = path->nodes[0]; |
| 816 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 817 | struct btrfs_extent_ref); |
| 818 | if (btrfs_ref_root(leaf, ref) != ref_root || |
| 819 | btrfs_ref_generation(leaf, ref) != ref_generation) { |
| 820 | ret = -EIO; |
| 821 | WARN_ON(1); |
| 822 | goto out; |
| 823 | } |
| 824 | |
| 825 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 826 | BUG_ON(num_refs == 0); |
| 827 | btrfs_set_ref_num_refs(leaf, ref, num_refs + 1); |
| 828 | |
| 829 | existing_owner = btrfs_ref_objectid(leaf, ref); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 830 | if (existing_owner != owner_objectid && |
| 831 | existing_owner != BTRFS_MULTIPLE_OBJECTIDS) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 832 | btrfs_set_ref_objectid(leaf, ref, |
| 833 | BTRFS_MULTIPLE_OBJECTIDS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 834 | } |
| 835 | ret = 0; |
| 836 | } else { |
| 837 | goto out; |
| 838 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 839 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 840 | out: |
| 841 | btrfs_release_path(root, path); |
| 842 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 843 | } |
| 844 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 845 | static noinline int remove_extent_backref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 846 | struct btrfs_root *root, |
| 847 | struct btrfs_path *path) |
| 848 | { |
| 849 | struct extent_buffer *leaf; |
| 850 | struct btrfs_extent_ref *ref; |
| 851 | u32 num_refs; |
| 852 | int ret = 0; |
| 853 | |
| 854 | leaf = path->nodes[0]; |
| 855 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref); |
| 856 | num_refs = btrfs_ref_num_refs(leaf, ref); |
| 857 | BUG_ON(num_refs == 0); |
| 858 | num_refs -= 1; |
| 859 | if (num_refs == 0) { |
| 860 | ret = btrfs_del_item(trans, root, path); |
| 861 | } else { |
| 862 | btrfs_set_ref_num_refs(leaf, ref, num_refs); |
| 863 | btrfs_mark_buffer_dirty(leaf); |
| 864 | } |
| 865 | btrfs_release_path(root, path); |
| 866 | return ret; |
| 867 | } |
| 868 | |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 869 | #ifdef BIO_RW_DISCARD |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 870 | static void btrfs_issue_discard(struct block_device *bdev, |
| 871 | u64 start, u64 len) |
| 872 | { |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 873 | blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 874 | } |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 875 | #endif |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 876 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 877 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
| 878 | u64 num_bytes) |
| 879 | { |
| 880 | #ifdef BIO_RW_DISCARD |
| 881 | int ret; |
| 882 | u64 map_length = num_bytes; |
| 883 | struct btrfs_multi_bio *multi = NULL; |
| 884 | |
| 885 | /* Tell the block device(s) that the sectors can be discarded */ |
| 886 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, |
| 887 | bytenr, &map_length, &multi, 0); |
| 888 | if (!ret) { |
| 889 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 890 | int i; |
| 891 | |
| 892 | if (map_length > num_bytes) |
| 893 | map_length = num_bytes; |
| 894 | |
| 895 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
| 896 | btrfs_issue_discard(stripe->dev->bdev, |
| 897 | stripe->physical, |
| 898 | map_length); |
| 899 | } |
| 900 | kfree(multi); |
| 901 | } |
| 902 | |
| 903 | return ret; |
| 904 | #else |
| 905 | return 0; |
| 906 | #endif |
| 907 | } |
| 908 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 909 | static noinline int free_extents(struct btrfs_trans_handle *trans, |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 910 | struct btrfs_root *extent_root, |
| 911 | struct list_head *del_list) |
| 912 | { |
| 913 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 914 | struct btrfs_path *path; |
| 915 | struct btrfs_key key, found_key; |
| 916 | struct extent_buffer *leaf; |
| 917 | struct list_head *cur; |
| 918 | struct pending_extent_op *op; |
| 919 | struct btrfs_extent_item *ei; |
| 920 | int ret, num_to_del, extent_slot = 0, found_extent = 0; |
| 921 | u32 refs; |
| 922 | u64 bytes_freed = 0; |
| 923 | |
| 924 | path = btrfs_alloc_path(); |
| 925 | if (!path) |
| 926 | return -ENOMEM; |
| 927 | path->reada = 1; |
| 928 | |
| 929 | search: |
| 930 | /* search for the backref for the current ref we want to delete */ |
| 931 | cur = del_list->next; |
| 932 | op = list_entry(cur, struct pending_extent_op, list); |
| 933 | ret = lookup_extent_backref(trans, extent_root, path, op->bytenr, |
| 934 | op->orig_parent, |
| 935 | extent_root->root_key.objectid, |
| 936 | op->orig_generation, op->level, 1); |
| 937 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 938 | printk(KERN_ERR "btrfs unable to find backref byte nr %llu " |
| 939 | "root %llu gen %llu owner %u\n", |
| 940 | (unsigned long long)op->bytenr, |
| 941 | (unsigned long long)extent_root->root_key.objectid, |
| 942 | (unsigned long long)op->orig_generation, op->level); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 943 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 944 | WARN_ON(1); |
| 945 | goto out; |
| 946 | } |
| 947 | |
| 948 | extent_slot = path->slots[0]; |
| 949 | num_to_del = 1; |
| 950 | found_extent = 0; |
| 951 | |
| 952 | /* |
| 953 | * if we aren't the first item on the leaf we can move back one and see |
| 954 | * if our ref is right next to our extent item |
| 955 | */ |
| 956 | if (likely(extent_slot)) { |
| 957 | extent_slot--; |
| 958 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 959 | extent_slot); |
| 960 | if (found_key.objectid == op->bytenr && |
| 961 | found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 962 | found_key.offset == op->num_bytes) { |
| 963 | num_to_del++; |
| 964 | found_extent = 1; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * if we didn't find the extent we need to delete the backref and then |
| 970 | * search for the extent item key so we can update its ref count |
| 971 | */ |
| 972 | if (!found_extent) { |
| 973 | key.objectid = op->bytenr; |
| 974 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 975 | key.offset = op->num_bytes; |
| 976 | |
| 977 | ret = remove_extent_backref(trans, extent_root, path); |
| 978 | BUG_ON(ret); |
| 979 | btrfs_release_path(extent_root, path); |
| 980 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
| 981 | BUG_ON(ret); |
| 982 | extent_slot = path->slots[0]; |
| 983 | } |
| 984 | |
| 985 | /* this is where we update the ref count for the extent */ |
| 986 | leaf = path->nodes[0]; |
| 987 | ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item); |
| 988 | refs = btrfs_extent_refs(leaf, ei); |
| 989 | BUG_ON(refs == 0); |
| 990 | refs--; |
| 991 | btrfs_set_extent_refs(leaf, ei, refs); |
| 992 | |
| 993 | btrfs_mark_buffer_dirty(leaf); |
| 994 | |
| 995 | /* |
| 996 | * This extent needs deleting. The reason cur_slot is extent_slot + |
| 997 | * num_to_del is because extent_slot points to the slot where the extent |
| 998 | * is, and if the backref was not right next to the extent we will be |
| 999 | * deleting at least 1 item, and will want to start searching at the |
| 1000 | * slot directly next to extent_slot. However if we did find the |
| 1001 | * backref next to the extent item them we will be deleting at least 2 |
| 1002 | * items and will want to start searching directly after the ref slot |
| 1003 | */ |
| 1004 | if (!refs) { |
| 1005 | struct list_head *pos, *n, *end; |
| 1006 | int cur_slot = extent_slot+num_to_del; |
| 1007 | u64 super_used; |
| 1008 | u64 root_used; |
| 1009 | |
| 1010 | path->slots[0] = extent_slot; |
| 1011 | bytes_freed = op->num_bytes; |
| 1012 | |
Josef Bacik | e3e469f | 2008-11-17 21:11:49 -0500 | [diff] [blame] | 1013 | mutex_lock(&info->pinned_mutex); |
| 1014 | ret = pin_down_bytes(trans, extent_root, op->bytenr, |
| 1015 | op->num_bytes, op->level >= |
| 1016 | BTRFS_FIRST_FREE_OBJECTID); |
| 1017 | mutex_unlock(&info->pinned_mutex); |
| 1018 | BUG_ON(ret < 0); |
| 1019 | op->del = ret; |
| 1020 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1021 | /* |
| 1022 | * we need to see if we can delete multiple things at once, so |
| 1023 | * start looping through the list of extents we are wanting to |
| 1024 | * delete and see if their extent/backref's are right next to |
| 1025 | * eachother and the extents only have 1 ref |
| 1026 | */ |
| 1027 | for (pos = cur->next; pos != del_list; pos = pos->next) { |
| 1028 | struct pending_extent_op *tmp; |
| 1029 | |
| 1030 | tmp = list_entry(pos, struct pending_extent_op, list); |
| 1031 | |
| 1032 | /* we only want to delete extent+ref at this stage */ |
| 1033 | if (cur_slot >= btrfs_header_nritems(leaf) - 1) |
| 1034 | break; |
| 1035 | |
| 1036 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot); |
| 1037 | if (found_key.objectid != tmp->bytenr || |
| 1038 | found_key.type != BTRFS_EXTENT_ITEM_KEY || |
| 1039 | found_key.offset != tmp->num_bytes) |
| 1040 | break; |
| 1041 | |
| 1042 | /* check to make sure this extent only has one ref */ |
| 1043 | ei = btrfs_item_ptr(leaf, cur_slot, |
| 1044 | struct btrfs_extent_item); |
| 1045 | if (btrfs_extent_refs(leaf, ei) != 1) |
| 1046 | break; |
| 1047 | |
| 1048 | btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1); |
| 1049 | if (found_key.objectid != tmp->bytenr || |
| 1050 | found_key.type != BTRFS_EXTENT_REF_KEY || |
| 1051 | found_key.offset != tmp->orig_parent) |
| 1052 | break; |
| 1053 | |
| 1054 | /* |
| 1055 | * the ref is right next to the extent, we can set the |
| 1056 | * ref count to 0 since we will delete them both now |
| 1057 | */ |
| 1058 | btrfs_set_extent_refs(leaf, ei, 0); |
| 1059 | |
| 1060 | /* pin down the bytes for this extent */ |
| 1061 | mutex_lock(&info->pinned_mutex); |
| 1062 | ret = pin_down_bytes(trans, extent_root, tmp->bytenr, |
| 1063 | tmp->num_bytes, tmp->level >= |
| 1064 | BTRFS_FIRST_FREE_OBJECTID); |
| 1065 | mutex_unlock(&info->pinned_mutex); |
| 1066 | BUG_ON(ret < 0); |
| 1067 | |
| 1068 | /* |
| 1069 | * use the del field to tell if we need to go ahead and |
| 1070 | * free up the extent when we delete the item or not. |
| 1071 | */ |
| 1072 | tmp->del = ret; |
| 1073 | bytes_freed += tmp->num_bytes; |
| 1074 | |
| 1075 | num_to_del += 2; |
| 1076 | cur_slot += 2; |
| 1077 | } |
| 1078 | end = pos; |
| 1079 | |
| 1080 | /* update the free space counters */ |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 1081 | spin_lock(&info->delalloc_lock); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1082 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1083 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1084 | super_used - bytes_freed); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1085 | |
| 1086 | root_used = btrfs_root_used(&extent_root->root_item); |
| 1087 | btrfs_set_root_used(&extent_root->root_item, |
| 1088 | root_used - bytes_freed); |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 1089 | spin_unlock(&info->delalloc_lock); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1090 | |
| 1091 | /* delete the items */ |
| 1092 | ret = btrfs_del_items(trans, extent_root, path, |
| 1093 | path->slots[0], num_to_del); |
| 1094 | BUG_ON(ret); |
| 1095 | |
| 1096 | /* |
| 1097 | * loop through the extents we deleted and do the cleanup work |
| 1098 | * on them |
| 1099 | */ |
| 1100 | for (pos = cur, n = pos->next; pos != end; |
| 1101 | pos = n, n = pos->next) { |
| 1102 | struct pending_extent_op *tmp; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1103 | tmp = list_entry(pos, struct pending_extent_op, list); |
| 1104 | |
| 1105 | /* |
| 1106 | * remember tmp->del tells us wether or not we pinned |
| 1107 | * down the extent |
| 1108 | */ |
| 1109 | ret = update_block_group(trans, extent_root, |
| 1110 | tmp->bytenr, tmp->num_bytes, 0, |
| 1111 | tmp->del); |
| 1112 | BUG_ON(ret); |
| 1113 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1114 | list_del_init(&tmp->list); |
| 1115 | unlock_extent(&info->extent_ins, tmp->bytenr, |
| 1116 | tmp->bytenr + tmp->num_bytes - 1, |
| 1117 | GFP_NOFS); |
| 1118 | kfree(tmp); |
| 1119 | } |
| 1120 | } else if (refs && found_extent) { |
| 1121 | /* |
| 1122 | * the ref and extent were right next to eachother, but the |
| 1123 | * extent still has a ref, so just free the backref and keep |
| 1124 | * going |
| 1125 | */ |
| 1126 | ret = remove_extent_backref(trans, extent_root, path); |
| 1127 | BUG_ON(ret); |
| 1128 | |
| 1129 | list_del_init(&op->list); |
| 1130 | unlock_extent(&info->extent_ins, op->bytenr, |
| 1131 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 1132 | kfree(op); |
| 1133 | } else { |
| 1134 | /* |
| 1135 | * the extent has multiple refs and the backref we were looking |
| 1136 | * for was not right next to it, so just unlock and go next, |
| 1137 | * we're good to go |
| 1138 | */ |
| 1139 | list_del_init(&op->list); |
| 1140 | unlock_extent(&info->extent_ins, op->bytenr, |
| 1141 | op->bytenr + op->num_bytes - 1, GFP_NOFS); |
| 1142 | kfree(op); |
| 1143 | } |
| 1144 | |
| 1145 | btrfs_release_path(extent_root, path); |
| 1146 | if (!list_empty(del_list)) |
| 1147 | goto search; |
| 1148 | |
| 1149 | out: |
| 1150 | btrfs_free_path(path); |
| 1151 | return ret; |
| 1152 | } |
| 1153 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1154 | static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 1155 | struct btrfs_root *root, u64 bytenr, |
| 1156 | u64 orig_parent, u64 parent, |
| 1157 | u64 orig_root, u64 ref_root, |
| 1158 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1159 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1160 | { |
| 1161 | int ret; |
| 1162 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1163 | struct btrfs_path *path; |
| 1164 | |
| 1165 | if (root == root->fs_info->extent_root) { |
| 1166 | struct pending_extent_op *extent_op; |
| 1167 | u64 num_bytes; |
| 1168 | |
| 1169 | BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL); |
| 1170 | num_bytes = btrfs_level_size(root, (int)owner_objectid); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1171 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1172 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1173 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1174 | u64 priv; |
| 1175 | ret = get_state_private(&root->fs_info->extent_ins, |
| 1176 | bytenr, &priv); |
| 1177 | BUG_ON(ret); |
| 1178 | extent_op = (struct pending_extent_op *) |
| 1179 | (unsigned long)priv; |
| 1180 | BUG_ON(extent_op->parent != orig_parent); |
| 1181 | BUG_ON(extent_op->generation != orig_generation); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1182 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1183 | extent_op->parent = parent; |
| 1184 | extent_op->generation = ref_generation; |
| 1185 | } else { |
| 1186 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 1187 | BUG_ON(!extent_op); |
| 1188 | |
| 1189 | extent_op->type = PENDING_BACKREF_UPDATE; |
| 1190 | extent_op->bytenr = bytenr; |
| 1191 | extent_op->num_bytes = num_bytes; |
| 1192 | extent_op->parent = parent; |
| 1193 | extent_op->orig_parent = orig_parent; |
| 1194 | extent_op->generation = ref_generation; |
| 1195 | extent_op->orig_generation = orig_generation; |
| 1196 | extent_op->level = (int)owner_objectid; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1197 | INIT_LIST_HEAD(&extent_op->list); |
| 1198 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1199 | |
| 1200 | set_extent_bits(&root->fs_info->extent_ins, |
| 1201 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1202 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1203 | set_state_private(&root->fs_info->extent_ins, |
| 1204 | bytenr, (unsigned long)extent_op); |
| 1205 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1206 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | path = btrfs_alloc_path(); |
| 1211 | if (!path) |
| 1212 | return -ENOMEM; |
| 1213 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1214 | bytenr, orig_parent, orig_root, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1215 | orig_generation, owner_objectid, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1216 | if (ret) |
| 1217 | goto out; |
| 1218 | ret = remove_extent_backref(trans, extent_root, path); |
| 1219 | if (ret) |
| 1220 | goto out; |
| 1221 | ret = insert_extent_backref(trans, extent_root, path, bytenr, |
| 1222 | parent, ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1223 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1224 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1225 | finish_current_insert(trans, extent_root, 0); |
| 1226 | del_pending_extents(trans, extent_root, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1227 | out: |
| 1228 | btrfs_free_path(path); |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | int btrfs_update_extent_ref(struct btrfs_trans_handle *trans, |
| 1233 | struct btrfs_root *root, u64 bytenr, |
| 1234 | u64 orig_parent, u64 parent, |
| 1235 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1236 | u64 owner_objectid) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1237 | { |
| 1238 | int ret; |
| 1239 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 1240 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 1241 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1242 | ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent, |
| 1243 | parent, ref_root, ref_root, |
| 1244 | ref_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1245 | owner_objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1246 | return ret; |
| 1247 | } |
| 1248 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1249 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1250 | struct btrfs_root *root, u64 bytenr, |
| 1251 | u64 orig_parent, u64 parent, |
| 1252 | u64 orig_root, u64 ref_root, |
| 1253 | u64 orig_generation, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1254 | u64 owner_objectid) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1255 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1256 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1257 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1258 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1259 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1260 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1261 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1262 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1263 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1264 | if (!path) |
| 1265 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1266 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1267 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1268 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1269 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1270 | key.offset = (u64)-1; |
| 1271 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1272 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1273 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1274 | if (ret < 0) |
| 1275 | return ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1276 | BUG_ON(ret == 0 || path->slots[0] == 0); |
| 1277 | |
| 1278 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1279 | l = path->nodes[0]; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1280 | |
| 1281 | btrfs_item_key_to_cpu(l, &key, path->slots[0]); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1282 | if (key.objectid != bytenr) { |
| 1283 | btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1284 | printk(KERN_ERR "btrfs wanted %llu found %llu\n", |
| 1285 | (unsigned long long)bytenr, |
| 1286 | (unsigned long long)key.objectid); |
Chris Mason | 771ed68 | 2008-11-06 22:02:51 -0500 | [diff] [blame] | 1287 | BUG(); |
| 1288 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1289 | BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY); |
| 1290 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1291 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1292 | refs = btrfs_extent_refs(l, item); |
| 1293 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1294 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1295 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1296 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1297 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1298 | path->reada = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1299 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
| 1300 | path, bytenr, parent, |
| 1301 | ref_root, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1302 | owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1303 | BUG_ON(ret); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1304 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 1305 | del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1306 | |
| 1307 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1311 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1312 | struct btrfs_root *root, |
| 1313 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1314 | u64 ref_root, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1315 | u64 owner_objectid) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1316 | { |
| 1317 | int ret; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1318 | if (ref_root == BTRFS_TREE_LOG_OBJECTID && |
| 1319 | owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 1320 | return 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1321 | ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent, |
| 1322 | 0, ref_root, 0, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1323 | owner_objectid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1324 | return ret; |
| 1325 | } |
| 1326 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1327 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 1328 | struct btrfs_root *root) |
| 1329 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 1330 | u64 start; |
| 1331 | u64 end; |
| 1332 | int ret; |
| 1333 | |
| 1334 | while(1) { |
| 1335 | finish_current_insert(trans, root->fs_info->extent_root, 1); |
| 1336 | del_pending_extents(trans, root->fs_info->extent_root, 1); |
| 1337 | |
| 1338 | /* is there more work to do? */ |
| 1339 | ret = find_first_extent_bit(&root->fs_info->pending_del, |
| 1340 | 0, &start, &end, EXTENT_WRITEBACK); |
| 1341 | if (!ret) |
| 1342 | continue; |
| 1343 | ret = find_first_extent_bit(&root->fs_info->extent_ins, |
| 1344 | 0, &start, &end, EXTENT_WRITEBACK); |
| 1345 | if (!ret) |
| 1346 | continue; |
| 1347 | break; |
| 1348 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1349 | return 0; |
| 1350 | } |
| 1351 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1352 | int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans, |
| 1353 | struct btrfs_root *root, u64 bytenr, |
| 1354 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1355 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1356 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1357 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1358 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1359 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1360 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1361 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1362 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1363 | path = btrfs_alloc_path(); |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1364 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1365 | key.objectid = bytenr; |
| 1366 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1367 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1368 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1369 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1370 | if (ret < 0) |
| 1371 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1372 | if (ret != 0) { |
| 1373 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1374 | printk(KERN_INFO "btrfs failed to find block number %llu\n", |
| 1375 | (unsigned long long)bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1376 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1377 | } |
| 1378 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1379 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1380 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1381 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1382 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1383 | return 0; |
| 1384 | } |
| 1385 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1386 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1387 | struct btrfs_root *root, u64 objectid, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1388 | { |
| 1389 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 1390 | struct btrfs_path *path; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1391 | struct extent_buffer *leaf; |
| 1392 | struct btrfs_extent_ref *ref_item; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1393 | struct btrfs_key key; |
| 1394 | struct btrfs_key found_key; |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1395 | u64 ref_root; |
| 1396 | u64 last_snapshot; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1397 | u32 nritems; |
| 1398 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1399 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1400 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1401 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1402 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1403 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1404 | path = btrfs_alloc_path(); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1405 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 1406 | if (ret < 0) |
| 1407 | goto out; |
| 1408 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1409 | |
| 1410 | ret = -ENOENT; |
| 1411 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1412 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1413 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1414 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1415 | leaf = path->nodes[0]; |
| 1416 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1417 | |
| 1418 | if (found_key.objectid != bytenr || |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1419 | found_key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1420 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1421 | |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1422 | last_snapshot = btrfs_root_last_snapshot(&root->root_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1423 | while (1) { |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1424 | leaf = path->nodes[0]; |
| 1425 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1426 | if (path->slots[0] >= nritems) { |
| 1427 | ret = btrfs_next_leaf(extent_root, path); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1428 | if (ret < 0) |
| 1429 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1430 | if (ret == 0) |
| 1431 | continue; |
| 1432 | break; |
| 1433 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1434 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1435 | if (found_key.objectid != bytenr) |
| 1436 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 1437 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1438 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 1439 | path->slots[0]++; |
| 1440 | continue; |
| 1441 | } |
| 1442 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1443 | ref_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1444 | struct btrfs_extent_ref); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1445 | ref_root = btrfs_ref_root(leaf, ref_item); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 1446 | if ((ref_root != root->root_key.objectid && |
| 1447 | ref_root != BTRFS_TREE_LOG_OBJECTID) || |
| 1448 | objectid != btrfs_ref_objectid(leaf, ref_item)) { |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1449 | ret = 1; |
| 1450 | goto out; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1451 | } |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 1452 | if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) { |
| 1453 | ret = 1; |
| 1454 | goto out; |
| 1455 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1456 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1457 | path->slots[0]++; |
| 1458 | } |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1459 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 1460 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 1461 | btrfs_free_path(path); |
| 1462 | return ret; |
| 1463 | } |
| 1464 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1465 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 1466 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1467 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1468 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1469 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1470 | u64 root_gen; |
| 1471 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1472 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1473 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1474 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1475 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1476 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 1477 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1478 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1479 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1480 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 1481 | shared = 0; |
| 1482 | root_gen = root->root_key.offset; |
| 1483 | } else { |
| 1484 | shared = 1; |
| 1485 | root_gen = trans->transid - 1; |
| 1486 | } |
| 1487 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1488 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1489 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 1490 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1491 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1492 | struct btrfs_leaf_ref *ref; |
| 1493 | struct btrfs_extent_info *info; |
| 1494 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1495 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1496 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1497 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1498 | goto out; |
| 1499 | } |
| 1500 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1501 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1502 | ref->bytenr = buf->start; |
| 1503 | ref->owner = btrfs_header_owner(buf); |
| 1504 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1505 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1506 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1507 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1508 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1509 | u64 disk_bytenr; |
| 1510 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1511 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1512 | continue; |
| 1513 | fi = btrfs_item_ptr(buf, i, |
| 1514 | struct btrfs_file_extent_item); |
| 1515 | if (btrfs_file_extent_type(buf, fi) == |
| 1516 | BTRFS_FILE_EXTENT_INLINE) |
| 1517 | continue; |
| 1518 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1519 | if (disk_bytenr == 0) |
| 1520 | continue; |
| 1521 | |
| 1522 | info->bytenr = disk_bytenr; |
| 1523 | info->num_bytes = |
| 1524 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 1525 | info->objectid = key.objectid; |
| 1526 | info->offset = key.offset; |
| 1527 | info++; |
| 1528 | } |
| 1529 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 1530 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 1531 | if (ret == -EEXIST && shared) { |
| 1532 | struct btrfs_leaf_ref *old; |
| 1533 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 1534 | BUG_ON(!old); |
| 1535 | btrfs_remove_leaf_ref(root, old); |
| 1536 | btrfs_free_leaf_ref(root, old); |
| 1537 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 1538 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1539 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 1540 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1541 | } |
| 1542 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1543 | return ret; |
| 1544 | } |
| 1545 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1546 | /* when a block goes through cow, we update the reference counts of |
| 1547 | * everything that block points to. The internal pointers of the block |
| 1548 | * can be in just about any order, and it is likely to have clusters of |
| 1549 | * things that are close together and clusters of things that are not. |
| 1550 | * |
| 1551 | * To help reduce the seeks that come with updating all of these reference |
| 1552 | * counts, sort them by byte number before actual updates are done. |
| 1553 | * |
| 1554 | * struct refsort is used to match byte number to slot in the btree block. |
| 1555 | * we sort based on the byte number and then use the slot to actually |
| 1556 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 1557 | * |
| 1558 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 1559 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 1560 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 1561 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1562 | */ |
| 1563 | struct refsort { |
| 1564 | u64 bytenr; |
| 1565 | u32 slot; |
| 1566 | }; |
| 1567 | |
| 1568 | /* |
| 1569 | * for passing into sort() |
| 1570 | */ |
| 1571 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 1572 | { |
| 1573 | const struct refsort *a = a_void; |
| 1574 | const struct refsort *b = b_void; |
| 1575 | |
| 1576 | if (a->bytenr < b->bytenr) |
| 1577 | return -1; |
| 1578 | if (a->bytenr > b->bytenr) |
| 1579 | return 1; |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | |
| 1584 | noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans, |
| 1585 | struct btrfs_root *root, |
| 1586 | struct extent_buffer *orig_buf, |
| 1587 | struct extent_buffer *buf, u32 *nr_extents) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1588 | { |
| 1589 | u64 bytenr; |
| 1590 | u64 ref_root; |
| 1591 | u64 orig_root; |
| 1592 | u64 ref_generation; |
| 1593 | u64 orig_generation; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1594 | struct refsort *sorted; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1595 | u32 nritems; |
| 1596 | u32 nr_file_extents = 0; |
| 1597 | struct btrfs_key key; |
| 1598 | struct btrfs_file_extent_item *fi; |
| 1599 | int i; |
| 1600 | int level; |
| 1601 | int ret = 0; |
| 1602 | int faili = 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1603 | int refi = 0; |
| 1604 | int slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1605 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1606 | u64, u64, u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1607 | |
| 1608 | ref_root = btrfs_header_owner(buf); |
| 1609 | ref_generation = btrfs_header_generation(buf); |
| 1610 | orig_root = btrfs_header_owner(orig_buf); |
| 1611 | orig_generation = btrfs_header_generation(orig_buf); |
| 1612 | |
| 1613 | nritems = btrfs_header_nritems(buf); |
| 1614 | level = btrfs_header_level(buf); |
| 1615 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1616 | sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS); |
| 1617 | BUG_ON(!sorted); |
| 1618 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1619 | if (root->ref_cows) { |
| 1620 | process_func = __btrfs_inc_extent_ref; |
| 1621 | } else { |
| 1622 | if (level == 0 && |
| 1623 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1624 | goto out; |
| 1625 | if (level != 0 && |
| 1626 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1627 | goto out; |
| 1628 | process_func = __btrfs_update_extent_ref; |
| 1629 | } |
| 1630 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1631 | /* |
| 1632 | * we make two passes through the items. In the first pass we |
| 1633 | * only record the byte number and slot. Then we sort based on |
| 1634 | * byte number and do the actual work based on the sorted results |
| 1635 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1636 | for (i = 0; i < nritems; i++) { |
| 1637 | cond_resched(); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1638 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1639 | btrfs_item_key_to_cpu(buf, &key, i); |
| 1640 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1641 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1642 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1643 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1644 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1645 | BTRFS_FILE_EXTENT_INLINE) |
| 1646 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1647 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1648 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1649 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1650 | |
| 1651 | nr_file_extents++; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1652 | sorted[refi].bytenr = bytenr; |
| 1653 | sorted[refi].slot = i; |
| 1654 | refi++; |
| 1655 | } else { |
| 1656 | bytenr = btrfs_node_blockptr(buf, i); |
| 1657 | sorted[refi].bytenr = bytenr; |
| 1658 | sorted[refi].slot = i; |
| 1659 | refi++; |
| 1660 | } |
| 1661 | } |
| 1662 | /* |
| 1663 | * if refi == 0, we didn't actually put anything into the sorted |
| 1664 | * array and we're done |
| 1665 | */ |
| 1666 | if (refi == 0) |
| 1667 | goto out; |
| 1668 | |
| 1669 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 1670 | |
| 1671 | for (i = 0; i < refi; i++) { |
| 1672 | cond_resched(); |
| 1673 | slot = sorted[i].slot; |
| 1674 | bytenr = sorted[i].bytenr; |
| 1675 | |
| 1676 | if (level == 0) { |
| 1677 | btrfs_item_key_to_cpu(buf, &key, slot); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1678 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1679 | ret = process_func(trans, root, bytenr, |
| 1680 | orig_buf->start, buf->start, |
| 1681 | orig_root, ref_root, |
| 1682 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1683 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1684 | |
| 1685 | if (ret) { |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1686 | faili = slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1687 | WARN_ON(1); |
| 1688 | goto fail; |
| 1689 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1690 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1691 | ret = process_func(trans, root, bytenr, |
| 1692 | orig_buf->start, buf->start, |
| 1693 | orig_root, ref_root, |
| 1694 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1695 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1696 | if (ret) { |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1697 | faili = slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1698 | WARN_ON(1); |
| 1699 | goto fail; |
| 1700 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1701 | } |
| 1702 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1703 | out: |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1704 | kfree(sorted); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1705 | if (nr_extents) { |
| 1706 | if (level == 0) |
| 1707 | *nr_extents = nr_file_extents; |
| 1708 | else |
| 1709 | *nr_extents = nritems; |
| 1710 | } |
| 1711 | return 0; |
| 1712 | fail: |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 1713 | kfree(sorted); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1714 | WARN_ON(1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1715 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1716 | } |
| 1717 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1718 | int btrfs_update_ref(struct btrfs_trans_handle *trans, |
| 1719 | struct btrfs_root *root, struct extent_buffer *orig_buf, |
| 1720 | struct extent_buffer *buf, int start_slot, int nr) |
| 1721 | |
| 1722 | { |
| 1723 | u64 bytenr; |
| 1724 | u64 ref_root; |
| 1725 | u64 orig_root; |
| 1726 | u64 ref_generation; |
| 1727 | u64 orig_generation; |
| 1728 | struct btrfs_key key; |
| 1729 | struct btrfs_file_extent_item *fi; |
| 1730 | int i; |
| 1731 | int ret; |
| 1732 | int slot; |
| 1733 | int level; |
| 1734 | |
| 1735 | BUG_ON(start_slot < 0); |
| 1736 | BUG_ON(start_slot + nr > btrfs_header_nritems(buf)); |
| 1737 | |
| 1738 | ref_root = btrfs_header_owner(buf); |
| 1739 | ref_generation = btrfs_header_generation(buf); |
| 1740 | orig_root = btrfs_header_owner(orig_buf); |
| 1741 | orig_generation = btrfs_header_generation(orig_buf); |
| 1742 | level = btrfs_header_level(buf); |
| 1743 | |
| 1744 | if (!root->ref_cows) { |
| 1745 | if (level == 0 && |
| 1746 | root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) |
| 1747 | return 0; |
| 1748 | if (level != 0 && |
| 1749 | root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
| 1753 | for (i = 0, slot = start_slot; i < nr; i++, slot++) { |
| 1754 | cond_resched(); |
| 1755 | if (level == 0) { |
| 1756 | btrfs_item_key_to_cpu(buf, &key, slot); |
| 1757 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 1758 | continue; |
| 1759 | fi = btrfs_item_ptr(buf, slot, |
| 1760 | struct btrfs_file_extent_item); |
| 1761 | if (btrfs_file_extent_type(buf, fi) == |
| 1762 | BTRFS_FILE_EXTENT_INLINE) |
| 1763 | continue; |
| 1764 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 1765 | if (bytenr == 0) |
| 1766 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1767 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1768 | orig_buf->start, buf->start, |
| 1769 | orig_root, ref_root, |
| 1770 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1771 | key.objectid); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1772 | if (ret) |
| 1773 | goto fail; |
| 1774 | } else { |
| 1775 | bytenr = btrfs_node_blockptr(buf, slot); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1776 | ret = __btrfs_update_extent_ref(trans, root, bytenr, |
| 1777 | orig_buf->start, buf->start, |
| 1778 | orig_root, ref_root, |
| 1779 | orig_generation, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 1780 | level - 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1781 | if (ret) |
| 1782 | goto fail; |
| 1783 | } |
| 1784 | } |
| 1785 | return 0; |
| 1786 | fail: |
| 1787 | WARN_ON(1); |
| 1788 | return -1; |
| 1789 | } |
| 1790 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1791 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 1792 | struct btrfs_root *root, |
| 1793 | struct btrfs_path *path, |
| 1794 | struct btrfs_block_group_cache *cache) |
| 1795 | { |
| 1796 | int ret; |
| 1797 | int pending_ret; |
| 1798 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1799 | unsigned long bi; |
| 1800 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1801 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1802 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1803 | if (ret < 0) |
| 1804 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1805 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1806 | |
| 1807 | leaf = path->nodes[0]; |
| 1808 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 1809 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 1810 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1811 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1812 | fail: |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 1813 | finish_current_insert(trans, extent_root, 0); |
| 1814 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1815 | if (ret) |
| 1816 | return ret; |
| 1817 | if (pending_ret) |
| 1818 | return pending_ret; |
| 1819 | return 0; |
| 1820 | |
| 1821 | } |
| 1822 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1823 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 1824 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1825 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1826 | struct btrfs_block_group_cache *cache, *entry; |
| 1827 | struct rb_node *n; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1828 | int err = 0; |
| 1829 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1830 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1831 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1832 | |
| 1833 | path = btrfs_alloc_path(); |
| 1834 | if (!path) |
| 1835 | return -ENOMEM; |
| 1836 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1837 | while (1) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1838 | cache = NULL; |
| 1839 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 1840 | for (n = rb_first(&root->fs_info->block_group_cache_tree); |
| 1841 | n; n = rb_next(n)) { |
| 1842 | entry = rb_entry(n, struct btrfs_block_group_cache, |
| 1843 | cache_node); |
| 1844 | if (entry->dirty) { |
| 1845 | cache = entry; |
| 1846 | break; |
| 1847 | } |
| 1848 | } |
| 1849 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 1850 | |
| 1851 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1852 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1853 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1854 | cache->dirty = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1855 | last += cache->key.offset; |
| 1856 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1857 | err = write_one_cache_group(trans, root, |
| 1858 | path, cache); |
| 1859 | /* |
| 1860 | * if we fail to write the cache group, we want |
| 1861 | * to keep it marked dirty in hopes that a later |
| 1862 | * write will work |
| 1863 | */ |
| 1864 | if (err) { |
| 1865 | werr = err; |
| 1866 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | btrfs_free_path(path); |
| 1870 | return werr; |
| 1871 | } |
| 1872 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 1873 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 1874 | { |
| 1875 | struct btrfs_block_group_cache *block_group; |
| 1876 | int readonly = 0; |
| 1877 | |
| 1878 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 1879 | if (!block_group || block_group->ro) |
| 1880 | readonly = 1; |
| 1881 | if (block_group) |
| 1882 | put_block_group(block_group); |
| 1883 | return readonly; |
| 1884 | } |
| 1885 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1886 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1887 | u64 total_bytes, u64 bytes_used, |
| 1888 | struct btrfs_space_info **space_info) |
| 1889 | { |
| 1890 | struct btrfs_space_info *found; |
| 1891 | |
| 1892 | found = __find_space_info(info, flags); |
| 1893 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1894 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1895 | found->total_bytes += total_bytes; |
| 1896 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1897 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1898 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1899 | *space_info = found; |
| 1900 | return 0; |
| 1901 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1902 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1903 | if (!found) |
| 1904 | return -ENOMEM; |
| 1905 | |
| 1906 | list_add(&found->list, &info->space_info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1907 | INIT_LIST_HEAD(&found->block_groups); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1908 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1909 | spin_lock_init(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1910 | found->flags = flags; |
| 1911 | found->total_bytes = total_bytes; |
| 1912 | found->bytes_used = bytes_used; |
| 1913 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 1914 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1915 | found->bytes_readonly = 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame^] | 1916 | found->bytes_delalloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1917 | found->full = 0; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 1918 | found->force_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1919 | *space_info = found; |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1923 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1924 | { |
| 1925 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1926 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1927 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1928 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1929 | if (extra_flags) { |
| 1930 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1931 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1932 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1933 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1934 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1935 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1936 | } |
| 1937 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1938 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 1939 | static void set_block_group_readonly(struct btrfs_block_group_cache *cache) |
| 1940 | { |
| 1941 | spin_lock(&cache->space_info->lock); |
| 1942 | spin_lock(&cache->lock); |
| 1943 | if (!cache->ro) { |
| 1944 | cache->space_info->bytes_readonly += cache->key.offset - |
| 1945 | btrfs_block_group_used(&cache->item); |
| 1946 | cache->ro = 1; |
| 1947 | } |
| 1948 | spin_unlock(&cache->lock); |
| 1949 | spin_unlock(&cache->space_info->lock); |
| 1950 | } |
| 1951 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1952 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1953 | { |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1954 | u64 num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1955 | |
| 1956 | if (num_devices == 1) |
| 1957 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 1958 | if (num_devices < 4) |
| 1959 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 1960 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1961 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 1962 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1963 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1964 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1965 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1966 | |
| 1967 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1968 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1969 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 1970 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 1971 | |
| 1972 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 1973 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 1974 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 1975 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 1976 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 1977 | return flags; |
| 1978 | } |
| 1979 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame^] | 1980 | static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data) |
| 1981 | { |
| 1982 | struct btrfs_fs_info *info = root->fs_info; |
| 1983 | u64 alloc_profile; |
| 1984 | |
| 1985 | if (data) { |
| 1986 | alloc_profile = info->avail_data_alloc_bits & |
| 1987 | info->data_alloc_profile; |
| 1988 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
| 1989 | } else if (root == root->fs_info->chunk_root) { |
| 1990 | alloc_profile = info->avail_system_alloc_bits & |
| 1991 | info->system_alloc_profile; |
| 1992 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
| 1993 | } else { |
| 1994 | alloc_profile = info->avail_metadata_alloc_bits & |
| 1995 | info->metadata_alloc_profile; |
| 1996 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
| 1997 | } |
| 1998 | |
| 1999 | return btrfs_reduce_alloc_profile(root, data); |
| 2000 | } |
| 2001 | |
| 2002 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 2003 | { |
| 2004 | u64 alloc_target; |
| 2005 | |
| 2006 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2007 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
| 2008 | alloc_target); |
| 2009 | } |
| 2010 | |
| 2011 | /* |
| 2012 | * for now this just makes sure we have at least 5% of our metadata space free |
| 2013 | * for use. |
| 2014 | */ |
| 2015 | int btrfs_check_metadata_free_space(struct btrfs_root *root) |
| 2016 | { |
| 2017 | struct btrfs_fs_info *info = root->fs_info; |
| 2018 | struct btrfs_space_info *meta_sinfo; |
| 2019 | u64 alloc_target, thresh; |
| 2020 | |
| 2021 | /* get the space info for where the metadata will live */ |
| 2022 | alloc_target = btrfs_get_alloc_profile(root, 0); |
| 2023 | meta_sinfo = __find_space_info(info, alloc_target); |
| 2024 | |
| 2025 | /* |
| 2026 | * if the metadata area isn't maxed out then there is no sense in |
| 2027 | * checking how much is used, since we can always allocate a new chunk |
| 2028 | */ |
| 2029 | if (!meta_sinfo->full) |
| 2030 | return 0; |
| 2031 | |
| 2032 | spin_lock(&meta_sinfo->lock); |
| 2033 | thresh = meta_sinfo->total_bytes * 95; |
| 2034 | |
| 2035 | do_div(thresh, 100); |
| 2036 | |
| 2037 | if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved + |
| 2038 | meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) { |
| 2039 | spin_unlock(&meta_sinfo->lock); |
| 2040 | return -ENOSPC; |
| 2041 | } |
| 2042 | spin_unlock(&meta_sinfo->lock); |
| 2043 | |
| 2044 | return 0; |
| 2045 | } |
| 2046 | |
| 2047 | /* |
| 2048 | * This will check the space that the inode allocates from to make sure we have |
| 2049 | * enough space for bytes. |
| 2050 | */ |
| 2051 | int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode, |
| 2052 | u64 bytes) |
| 2053 | { |
| 2054 | struct btrfs_space_info *data_sinfo; |
| 2055 | int ret = 0; |
| 2056 | |
| 2057 | /* make sure bytes are sectorsize aligned */ |
| 2058 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2059 | |
| 2060 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2061 | again: |
| 2062 | /* make sure we have enough space to handle the data first */ |
| 2063 | spin_lock(&data_sinfo->lock); |
| 2064 | if (data_sinfo->total_bytes - data_sinfo->bytes_used - |
| 2065 | data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved - |
| 2066 | data_sinfo->bytes_pinned - data_sinfo->bytes_readonly - |
| 2067 | data_sinfo->bytes_may_use < bytes) { |
| 2068 | /* |
| 2069 | * if we don't have enough free bytes in this space then we need |
| 2070 | * to alloc a new chunk. |
| 2071 | */ |
| 2072 | if (!data_sinfo->full) { |
| 2073 | u64 alloc_target; |
| 2074 | struct btrfs_trans_handle *trans; |
| 2075 | |
| 2076 | data_sinfo->force_alloc = 1; |
| 2077 | spin_unlock(&data_sinfo->lock); |
| 2078 | |
| 2079 | alloc_target = btrfs_get_alloc_profile(root, 1); |
| 2080 | trans = btrfs_start_transaction(root, 1); |
| 2081 | if (!trans) |
| 2082 | return -ENOMEM; |
| 2083 | |
| 2084 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2085 | bytes + 2 * 1024 * 1024, |
| 2086 | alloc_target, 0); |
| 2087 | btrfs_end_transaction(trans, root); |
| 2088 | if (ret) |
| 2089 | return ret; |
| 2090 | goto again; |
| 2091 | } |
| 2092 | spin_unlock(&data_sinfo->lock); |
| 2093 | printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes" |
| 2094 | ", %llu bytes_used, %llu bytes_reserved, " |
| 2095 | "%llu bytes_pinned, %llu bytes_readonly, %llu may use" |
| 2096 | "%llu total\n", bytes, data_sinfo->bytes_delalloc, |
| 2097 | data_sinfo->bytes_used, data_sinfo->bytes_reserved, |
| 2098 | data_sinfo->bytes_pinned, data_sinfo->bytes_readonly, |
| 2099 | data_sinfo->bytes_may_use, data_sinfo->total_bytes); |
| 2100 | return -ENOSPC; |
| 2101 | } |
| 2102 | data_sinfo->bytes_may_use += bytes; |
| 2103 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 2104 | spin_unlock(&data_sinfo->lock); |
| 2105 | |
| 2106 | return btrfs_check_metadata_free_space(root); |
| 2107 | } |
| 2108 | |
| 2109 | /* |
| 2110 | * if there was an error for whatever reason after calling |
| 2111 | * btrfs_check_data_free_space, call this so we can cleanup the counters. |
| 2112 | */ |
| 2113 | void btrfs_free_reserved_data_space(struct btrfs_root *root, |
| 2114 | struct inode *inode, u64 bytes) |
| 2115 | { |
| 2116 | struct btrfs_space_info *data_sinfo; |
| 2117 | |
| 2118 | /* make sure bytes are sectorsize aligned */ |
| 2119 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 2120 | |
| 2121 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2122 | spin_lock(&data_sinfo->lock); |
| 2123 | data_sinfo->bytes_may_use -= bytes; |
| 2124 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 2125 | spin_unlock(&data_sinfo->lock); |
| 2126 | } |
| 2127 | |
| 2128 | /* called when we are adding a delalloc extent to the inode's io_tree */ |
| 2129 | void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode, |
| 2130 | u64 bytes) |
| 2131 | { |
| 2132 | struct btrfs_space_info *data_sinfo; |
| 2133 | |
| 2134 | /* get the space info for where this inode will be storing its data */ |
| 2135 | data_sinfo = BTRFS_I(inode)->space_info; |
| 2136 | |
| 2137 | /* make sure we have enough space to handle the data first */ |
| 2138 | spin_lock(&data_sinfo->lock); |
| 2139 | data_sinfo->bytes_delalloc += bytes; |
| 2140 | |
| 2141 | /* |
| 2142 | * we are adding a delalloc extent without calling |
| 2143 | * btrfs_check_data_free_space first. This happens on a weird |
| 2144 | * writepage condition, but shouldn't hurt our accounting |
| 2145 | */ |
| 2146 | if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) { |
| 2147 | data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes; |
| 2148 | BTRFS_I(inode)->reserved_bytes = 0; |
| 2149 | } else { |
| 2150 | data_sinfo->bytes_may_use -= bytes; |
| 2151 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 2152 | } |
| 2153 | |
| 2154 | spin_unlock(&data_sinfo->lock); |
| 2155 | } |
| 2156 | |
| 2157 | /* called when we are clearing an delalloc extent from the inode's io_tree */ |
| 2158 | void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode, |
| 2159 | u64 bytes) |
| 2160 | { |
| 2161 | struct btrfs_space_info *info; |
| 2162 | |
| 2163 | info = BTRFS_I(inode)->space_info; |
| 2164 | |
| 2165 | spin_lock(&info->lock); |
| 2166 | info->bytes_delalloc -= bytes; |
| 2167 | spin_unlock(&info->lock); |
| 2168 | } |
| 2169 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2170 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 2171 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2172 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2173 | { |
| 2174 | struct btrfs_space_info *space_info; |
| 2175 | u64 thresh; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2176 | int ret = 0; |
| 2177 | |
| 2178 | mutex_lock(&extent_root->fs_info->chunk_mutex); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2179 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2180 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2181 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2182 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2183 | if (!space_info) { |
| 2184 | ret = update_space_info(extent_root->fs_info, flags, |
| 2185 | 0, 0, &space_info); |
| 2186 | BUG_ON(ret); |
| 2187 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2188 | BUG_ON(!space_info); |
| 2189 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2190 | spin_lock(&space_info->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2191 | if (space_info->force_alloc) { |
| 2192 | force = 1; |
| 2193 | space_info->force_alloc = 0; |
| 2194 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2195 | if (space_info->full) { |
| 2196 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2197 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2198 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2199 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2200 | thresh = space_info->total_bytes - space_info->bytes_readonly; |
| 2201 | thresh = div_factor(thresh, 6); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 2202 | if (!force && |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2203 | (space_info->bytes_used + space_info->bytes_pinned + |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2204 | space_info->bytes_reserved + alloc_bytes) < thresh) { |
| 2205 | spin_unlock(&space_info->lock); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2206 | goto out; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2207 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2208 | spin_unlock(&space_info->lock); |
| 2209 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2210 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2211 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2212 | space_info->full = 1; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 2213 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2214 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2215 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2216 | } |
| 2217 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2218 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 2219 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2220 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2221 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2222 | { |
| 2223 | struct btrfs_block_group_cache *cache; |
| 2224 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2225 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2226 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2227 | u64 byte_in_group; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 2228 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2229 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2230 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2231 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2232 | return -1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2233 | byte_in_group = bytenr - cache->key.objectid; |
| 2234 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2235 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2236 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2237 | spin_lock(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2238 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2239 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2240 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2241 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2242 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2243 | cache->space_info->bytes_used += num_bytes; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 2244 | if (cache->ro) |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2245 | cache->space_info->bytes_readonly -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2246 | btrfs_set_block_group_used(&cache->item, old_val); |
| 2247 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2248 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2249 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2250 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2251 | cache->space_info->bytes_used -= num_bytes; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2252 | if (cache->ro) |
| 2253 | cache->space_info->bytes_readonly += num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2254 | btrfs_set_block_group_used(&cache->item, old_val); |
| 2255 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2256 | spin_unlock(&cache->space_info->lock); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2257 | if (mark_free) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2258 | int ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2259 | |
| 2260 | ret = btrfs_discard_extent(root, bytenr, |
| 2261 | num_bytes); |
| 2262 | WARN_ON(ret); |
| 2263 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2264 | ret = btrfs_add_free_space(cache, bytenr, |
| 2265 | num_bytes); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2266 | WARN_ON(ret); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2267 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 2268 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2269 | put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2270 | total -= num_bytes; |
| 2271 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2272 | } |
| 2273 | return 0; |
| 2274 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2275 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2276 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 2277 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2278 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2279 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2280 | |
| 2281 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 2282 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2283 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2284 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2285 | bytenr = cache->key.objectid; |
| 2286 | put_block_group(cache); |
| 2287 | |
| 2288 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2289 | } |
| 2290 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2291 | int btrfs_update_pinned_extents(struct btrfs_root *root, |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2292 | u64 bytenr, u64 num, int pin) |
| 2293 | { |
| 2294 | u64 len; |
| 2295 | struct btrfs_block_group_cache *cache; |
| 2296 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2297 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2298 | WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2299 | if (pin) { |
| 2300 | set_extent_dirty(&fs_info->pinned_extents, |
| 2301 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2302 | } else { |
| 2303 | clear_extent_dirty(&fs_info->pinned_extents, |
| 2304 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 2305 | } |
| 2306 | while (num > 0) { |
| 2307 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2308 | BUG_ON(!cache); |
| 2309 | len = min(num, cache->key.offset - |
| 2310 | (bytenr - cache->key.objectid)); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2311 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2312 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2313 | spin_lock(&cache->lock); |
| 2314 | cache->pinned += len; |
| 2315 | cache->space_info->bytes_pinned += len; |
| 2316 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2317 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2318 | fs_info->total_pinned += len; |
| 2319 | } else { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2320 | spin_lock(&cache->space_info->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2321 | spin_lock(&cache->lock); |
| 2322 | cache->pinned -= len; |
| 2323 | cache->space_info->bytes_pinned -= len; |
| 2324 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2325 | spin_unlock(&cache->space_info->lock); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2326 | fs_info->total_pinned -= len; |
Josef Bacik | 07103a3 | 2008-11-19 15:17:55 -0500 | [diff] [blame] | 2327 | if (cache->cached) |
| 2328 | btrfs_add_free_space(cache, bytenr, len); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2329 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2330 | put_block_group(cache); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 2331 | bytenr += len; |
| 2332 | num -= len; |
| 2333 | } |
| 2334 | return 0; |
| 2335 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2336 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2337 | static int update_reserved_extents(struct btrfs_root *root, |
| 2338 | u64 bytenr, u64 num, int reserve) |
| 2339 | { |
| 2340 | u64 len; |
| 2341 | struct btrfs_block_group_cache *cache; |
| 2342 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2343 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2344 | while (num > 0) { |
| 2345 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 2346 | BUG_ON(!cache); |
| 2347 | len = min(num, cache->key.offset - |
| 2348 | (bytenr - cache->key.objectid)); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2349 | |
| 2350 | spin_lock(&cache->space_info->lock); |
| 2351 | spin_lock(&cache->lock); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2352 | if (reserve) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2353 | cache->reserved += len; |
| 2354 | cache->space_info->bytes_reserved += len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2355 | } else { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2356 | cache->reserved -= len; |
| 2357 | cache->space_info->bytes_reserved -= len; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2358 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2359 | spin_unlock(&cache->lock); |
| 2360 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2361 | put_block_group(cache); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2362 | bytenr += len; |
| 2363 | num -= len; |
| 2364 | } |
| 2365 | return 0; |
| 2366 | } |
| 2367 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2368 | 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] | 2369 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2370 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2371 | u64 start; |
| 2372 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2373 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2374 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2375 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2376 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2377 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2378 | ret = find_first_extent_bit(pinned_extents, last, |
| 2379 | &start, &end, EXTENT_DIRTY); |
| 2380 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2381 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2382 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 2383 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2384 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2385 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2386 | return 0; |
| 2387 | } |
| 2388 | |
| 2389 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 2390 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2391 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2392 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2393 | u64 start; |
| 2394 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2395 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2396 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2397 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2398 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2399 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 2400 | EXTENT_DIRTY); |
| 2401 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2402 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2403 | |
| 2404 | ret = btrfs_discard_extent(root, start, end + 1 - start); |
| 2405 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2406 | btrfs_update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2407 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2408 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2409 | if (need_resched()) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2410 | mutex_unlock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2411 | cond_resched(); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2412 | mutex_lock(&root->fs_info->pinned_mutex); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2413 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2414 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2415 | mutex_unlock(&root->fs_info->pinned_mutex); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 2416 | return ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2417 | } |
| 2418 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2419 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2420 | struct btrfs_root *extent_root, int all) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2421 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2422 | u64 start; |
| 2423 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2424 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2425 | u64 search = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2426 | struct btrfs_fs_info *info = extent_root->fs_info; |
| 2427 | struct btrfs_path *path; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2428 | struct pending_extent_op *extent_op, *tmp; |
| 2429 | struct list_head insert_list, update_list; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2430 | int ret; |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2431 | int num_inserts = 0, max_inserts, restart = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2432 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2433 | path = btrfs_alloc_path(); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2434 | INIT_LIST_HEAD(&insert_list); |
| 2435 | INIT_LIST_HEAD(&update_list); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2436 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2437 | max_inserts = extent_root->leafsize / |
| 2438 | (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) + |
| 2439 | sizeof(struct btrfs_extent_ref) + |
| 2440 | sizeof(struct btrfs_extent_item)); |
| 2441 | again: |
| 2442 | mutex_lock(&info->extent_ins_mutex); |
| 2443 | while (1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2444 | ret = find_first_extent_bit(&info->extent_ins, search, &start, |
| 2445 | &end, EXTENT_WRITEBACK); |
| 2446 | if (ret) { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2447 | if (restart && !num_inserts && |
Yan Zheng | 5a7be51 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 2448 | list_empty(&update_list)) { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2449 | restart = 0; |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame] | 2450 | search = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2451 | continue; |
| 2452 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2453 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS); |
| 2457 | if (!ret) { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2458 | if (all) |
| 2459 | restart = 1; |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2460 | search = end + 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2461 | if (need_resched()) { |
| 2462 | mutex_unlock(&info->extent_ins_mutex); |
| 2463 | cond_resched(); |
| 2464 | mutex_lock(&info->extent_ins_mutex); |
| 2465 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2466 | continue; |
| 2467 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2468 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2469 | ret = get_state_private(&info->extent_ins, start, &priv); |
| 2470 | BUG_ON(ret); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2471 | extent_op = (struct pending_extent_op *)(unsigned long) priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2472 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2473 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2474 | num_inserts++; |
| 2475 | list_add_tail(&extent_op->list, &insert_list); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2476 | search = end + 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2477 | if (num_inserts == max_inserts) { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2478 | restart = 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2479 | break; |
| 2480 | } |
| 2481 | } else if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 2482 | list_add_tail(&extent_op->list, &update_list); |
| 2483 | search = end + 1; |
| 2484 | } else { |
| 2485 | BUG(); |
| 2486 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2487 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2488 | |
| 2489 | /* |
Liu Hui | b4eec2c | 2008-11-18 11:30:10 -0500 | [diff] [blame] | 2490 | * process the update list, clear the writeback bit for it, and if |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2491 | * somebody marked this thing for deletion then just unlock it and be |
| 2492 | * done, the free_extents will handle it |
| 2493 | */ |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2494 | list_for_each_entry_safe(extent_op, tmp, &update_list, list) { |
| 2495 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, |
| 2496 | extent_op->bytenr + extent_op->num_bytes - 1, |
| 2497 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2498 | if (extent_op->del) { |
| 2499 | list_del_init(&extent_op->list); |
| 2500 | unlock_extent(&info->extent_ins, extent_op->bytenr, |
| 2501 | extent_op->bytenr + extent_op->num_bytes |
| 2502 | - 1, GFP_NOFS); |
| 2503 | kfree(extent_op); |
| 2504 | } |
| 2505 | } |
| 2506 | mutex_unlock(&info->extent_ins_mutex); |
| 2507 | |
| 2508 | /* |
| 2509 | * still have things left on the update list, go ahead an update |
| 2510 | * everything |
| 2511 | */ |
| 2512 | if (!list_empty(&update_list)) { |
| 2513 | ret = update_backrefs(trans, extent_root, path, &update_list); |
| 2514 | BUG_ON(ret); |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2515 | |
| 2516 | /* we may have COW'ed new blocks, so lets start over */ |
| 2517 | if (all) |
| 2518 | restart = 1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | /* |
| 2522 | * if no inserts need to be done, but we skipped some extents and we |
| 2523 | * need to make sure everything is cleaned then reset everything and |
| 2524 | * go back to the beginning |
| 2525 | */ |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2526 | if (!num_inserts && restart) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2527 | search = 0; |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2528 | restart = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2529 | INIT_LIST_HEAD(&update_list); |
| 2530 | INIT_LIST_HEAD(&insert_list); |
| 2531 | goto again; |
| 2532 | } else if (!num_inserts) { |
| 2533 | goto out; |
| 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * process the insert extents list. Again if we are deleting this |
| 2538 | * extent, then just unlock it, pin down the bytes if need be, and be |
| 2539 | * done with it. Saves us from having to actually insert the extent |
| 2540 | * into the tree and then subsequently come along and delete it |
| 2541 | */ |
| 2542 | mutex_lock(&info->extent_ins_mutex); |
| 2543 | list_for_each_entry_safe(extent_op, tmp, &insert_list, list) { |
| 2544 | clear_extent_bits(&info->extent_ins, extent_op->bytenr, |
| 2545 | extent_op->bytenr + extent_op->num_bytes - 1, |
| 2546 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2547 | if (extent_op->del) { |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 2548 | u64 used; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2549 | list_del_init(&extent_op->list); |
| 2550 | unlock_extent(&info->extent_ins, extent_op->bytenr, |
| 2551 | extent_op->bytenr + extent_op->num_bytes |
| 2552 | - 1, GFP_NOFS); |
| 2553 | |
| 2554 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 2555 | ret = pin_down_bytes(trans, extent_root, |
| 2556 | extent_op->bytenr, |
| 2557 | extent_op->num_bytes, 0); |
| 2558 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 2559 | |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 2560 | spin_lock(&info->delalloc_lock); |
| 2561 | used = btrfs_super_bytes_used(&info->super_copy); |
| 2562 | btrfs_set_super_bytes_used(&info->super_copy, |
| 2563 | used - extent_op->num_bytes); |
| 2564 | used = btrfs_root_used(&extent_root->root_item); |
| 2565 | btrfs_set_root_used(&extent_root->root_item, |
| 2566 | used - extent_op->num_bytes); |
| 2567 | spin_unlock(&info->delalloc_lock); |
| 2568 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2569 | ret = update_block_group(trans, extent_root, |
| 2570 | extent_op->bytenr, |
| 2571 | extent_op->num_bytes, |
| 2572 | 0, ret > 0); |
| 2573 | BUG_ON(ret); |
| 2574 | kfree(extent_op); |
| 2575 | num_inserts--; |
| 2576 | } |
| 2577 | } |
| 2578 | mutex_unlock(&info->extent_ins_mutex); |
| 2579 | |
| 2580 | ret = insert_extents(trans, extent_root, path, &insert_list, |
| 2581 | num_inserts); |
| 2582 | BUG_ON(ret); |
| 2583 | |
| 2584 | /* |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2585 | * if restart is set for whatever reason we need to go back and start |
| 2586 | * searching through the pending list again. |
| 2587 | * |
| 2588 | * We just inserted some extents, which could have resulted in new |
| 2589 | * blocks being allocated, which would result in new blocks needing |
| 2590 | * updates, so if all is set we _must_ restart to get the updated |
| 2591 | * blocks. |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2592 | */ |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2593 | if (restart || all) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2594 | INIT_LIST_HEAD(&insert_list); |
| 2595 | INIT_LIST_HEAD(&update_list); |
| 2596 | search = 0; |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2597 | restart = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2598 | num_inserts = 0; |
| 2599 | goto again; |
| 2600 | } |
| 2601 | out: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2602 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2603 | return 0; |
| 2604 | } |
| 2605 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2606 | static int pin_down_bytes(struct btrfs_trans_handle *trans, |
| 2607 | struct btrfs_root *root, |
| 2608 | u64 bytenr, u64 num_bytes, int is_data) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2609 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2610 | int err = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2611 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2612 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2613 | if (is_data) |
| 2614 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2615 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2616 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
| 2617 | if (!buf) |
| 2618 | goto pinit; |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2619 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2620 | /* we can reuse a block if it hasn't been written |
| 2621 | * and it is from this transaction. We can't |
| 2622 | * reuse anything from the tree log root because |
| 2623 | * it has tiny sub-transactions. |
| 2624 | */ |
| 2625 | if (btrfs_buffer_uptodate(buf, 0) && |
| 2626 | btrfs_try_tree_lock(buf)) { |
| 2627 | u64 header_owner = btrfs_header_owner(buf); |
| 2628 | u64 header_transid = btrfs_header_generation(buf); |
| 2629 | if (header_owner != BTRFS_TREE_LOG_OBJECTID && |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 2630 | header_owner != BTRFS_TREE_RELOC_OBJECTID && |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2631 | header_transid == trans->transid && |
| 2632 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 2633 | clean_tree_block(NULL, root, buf); |
| 2634 | btrfs_tree_unlock(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2635 | free_extent_buffer(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2636 | return 1; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2637 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2638 | btrfs_tree_unlock(buf); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2639 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2640 | free_extent_buffer(buf); |
| 2641 | pinit: |
| 2642 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 2643 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2644 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2645 | return 0; |
| 2646 | } |
| 2647 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2648 | /* |
| 2649 | * remove an extent from the root, returns 0 on success |
| 2650 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2651 | static int __free_extent(struct btrfs_trans_handle *trans, |
| 2652 | struct btrfs_root *root, |
| 2653 | u64 bytenr, u64 num_bytes, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2654 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2655 | u64 owner_objectid, int pin, int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2656 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2657 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 2658 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 2659 | struct btrfs_fs_info *info = root->fs_info; |
| 2660 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2661 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2662 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2663 | int extent_slot = 0; |
| 2664 | int found_extent = 0; |
| 2665 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2666 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2667 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 2668 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2669 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2670 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2671 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2672 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2673 | if (!path) |
| 2674 | return -ENOMEM; |
| 2675 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2676 | path->reada = 1; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2677 | ret = lookup_extent_backref(trans, extent_root, path, |
| 2678 | bytenr, parent, root_objectid, |
| 2679 | ref_generation, owner_objectid, 1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2680 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2681 | struct btrfs_key found_key; |
| 2682 | extent_slot = path->slots[0]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2683 | while (extent_slot > 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2684 | extent_slot--; |
| 2685 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2686 | extent_slot); |
| 2687 | if (found_key.objectid != bytenr) |
| 2688 | break; |
| 2689 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2690 | found_key.offset == num_bytes) { |
| 2691 | found_extent = 1; |
| 2692 | break; |
| 2693 | } |
| 2694 | if (path->slots[0] - extent_slot > 5) |
| 2695 | break; |
| 2696 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2697 | if (!found_extent) { |
| 2698 | ret = remove_extent_backref(trans, extent_root, path); |
| 2699 | BUG_ON(ret); |
| 2700 | btrfs_release_path(extent_root, path); |
| 2701 | ret = btrfs_search_slot(trans, extent_root, |
| 2702 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2703 | if (ret) { |
| 2704 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2705 | ", was looking for %llu\n", ret, |
| 2706 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2707 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2708 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2709 | BUG_ON(ret); |
| 2710 | extent_slot = path->slots[0]; |
| 2711 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2712 | } else { |
| 2713 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 2714 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2715 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
| 2716 | "root %llu gen %llu owner %llu\n", |
| 2717 | (unsigned long long)bytenr, |
| 2718 | (unsigned long long)root_objectid, |
| 2719 | (unsigned long long)ref_generation, |
| 2720 | (unsigned long long)owner_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2721 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2722 | |
| 2723 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2724 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 2725 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2726 | refs = btrfs_extent_refs(leaf, ei); |
| 2727 | BUG_ON(refs == 0); |
| 2728 | refs -= 1; |
| 2729 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2730 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2731 | btrfs_mark_buffer_dirty(leaf); |
| 2732 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2733 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2734 | struct btrfs_extent_ref *ref; |
| 2735 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 2736 | struct btrfs_extent_ref); |
| 2737 | BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2738 | /* if the back ref and the extent are next to each other |
| 2739 | * they get deleted below in one shot |
| 2740 | */ |
| 2741 | path->slots[0] = extent_slot; |
| 2742 | num_to_del = 2; |
| 2743 | } else if (found_extent) { |
| 2744 | /* otherwise delete the extent back ref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2745 | ret = remove_extent_backref(trans, extent_root, path); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2746 | BUG_ON(ret); |
| 2747 | /* if refs are 0, we need to setup the path for deletion */ |
| 2748 | if (refs == 0) { |
| 2749 | btrfs_release_path(extent_root, path); |
| 2750 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 2751 | -1, 1); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2752 | BUG_ON(ret); |
| 2753 | } |
| 2754 | } |
| 2755 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 2756 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2757 | u64 super_used; |
| 2758 | u64 root_used; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2759 | |
| 2760 | if (pin) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2761 | mutex_lock(&root->fs_info->pinned_mutex); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2762 | ret = pin_down_bytes(trans, root, bytenr, num_bytes, |
| 2763 | owner_objectid >= BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2764 | mutex_unlock(&root->fs_info->pinned_mutex); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 2765 | if (ret > 0) |
| 2766 | mark_free = 1; |
| 2767 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 2768 | } |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2769 | /* block accounting for super block */ |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 2770 | spin_lock(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2771 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 2772 | btrfs_set_super_bytes_used(&info->super_copy, |
| 2773 | super_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 2774 | |
| 2775 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2776 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2777 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2778 | root_used - num_bytes); |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 2779 | spin_unlock(&info->delalloc_lock); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 2780 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 2781 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2782 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2783 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 2784 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2785 | if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 2786 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 2787 | BUG_ON(ret); |
| 2788 | } |
| 2789 | |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 2790 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
| 2791 | mark_free); |
| 2792 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2793 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2794 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 2795 | finish_current_insert(trans, extent_root, 0); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2796 | return ret; |
| 2797 | } |
| 2798 | |
| 2799 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2800 | * find all the blocks marked as pending in the radix tree and remove |
| 2801 | * them from the extent map |
| 2802 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2803 | static int del_pending_extents(struct btrfs_trans_handle *trans, |
| 2804 | struct btrfs_root *extent_root, int all) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2805 | { |
| 2806 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2807 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2808 | u64 start; |
| 2809 | u64 end; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2810 | u64 priv; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2811 | u64 search = 0; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2812 | int nr = 0, skipped = 0; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2813 | struct extent_io_tree *pending_del; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2814 | struct extent_io_tree *extent_ins; |
| 2815 | struct pending_extent_op *extent_op; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2816 | struct btrfs_fs_info *info = extent_root->fs_info; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2817 | struct list_head delete_list; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 2818 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2819 | INIT_LIST_HEAD(&delete_list); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2820 | extent_ins = &extent_root->fs_info->extent_ins; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2821 | pending_del = &extent_root->fs_info->pending_del; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2822 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2823 | again: |
| 2824 | mutex_lock(&info->extent_ins_mutex); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2825 | while (1) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2826 | ret = find_first_extent_bit(pending_del, search, &start, &end, |
| 2827 | EXTENT_WRITEBACK); |
| 2828 | if (ret) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2829 | if (all && skipped && !nr) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2830 | search = 0; |
Yan Zheng | 5a7be51 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 2831 | skipped = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2832 | continue; |
| 2833 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2834 | mutex_unlock(&info->extent_ins_mutex); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2835 | break; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | ret = try_lock_extent(extent_ins, start, end, GFP_NOFS); |
| 2839 | if (!ret) { |
| 2840 | search = end+1; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2841 | skipped = 1; |
| 2842 | |
| 2843 | if (need_resched()) { |
| 2844 | mutex_unlock(&info->extent_ins_mutex); |
| 2845 | cond_resched(); |
| 2846 | mutex_lock(&info->extent_ins_mutex); |
| 2847 | } |
| 2848 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2849 | continue; |
| 2850 | } |
| 2851 | BUG_ON(ret < 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2852 | |
| 2853 | ret = get_state_private(pending_del, start, &priv); |
| 2854 | BUG_ON(ret); |
| 2855 | extent_op = (struct pending_extent_op *)(unsigned long)priv; |
| 2856 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2857 | clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK, |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2858 | GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2859 | if (!test_range_bit(extent_ins, start, end, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2860 | EXTENT_WRITEBACK, 0)) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2861 | list_add_tail(&extent_op->list, &delete_list); |
| 2862 | nr++; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2863 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2864 | kfree(extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2865 | |
| 2866 | ret = get_state_private(&info->extent_ins, start, |
| 2867 | &priv); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2868 | BUG_ON(ret); |
| 2869 | extent_op = (struct pending_extent_op *) |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2870 | (unsigned long)priv; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2871 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2872 | clear_extent_bits(&info->extent_ins, start, end, |
| 2873 | EXTENT_WRITEBACK, GFP_NOFS); |
| 2874 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2875 | if (extent_op->type == PENDING_BACKREF_UPDATE) { |
| 2876 | list_add_tail(&extent_op->list, &delete_list); |
| 2877 | search = end + 1; |
| 2878 | nr++; |
| 2879 | continue; |
| 2880 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2881 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2882 | mutex_lock(&extent_root->fs_info->pinned_mutex); |
| 2883 | ret = pin_down_bytes(trans, extent_root, start, |
| 2884 | end + 1 - start, 0); |
| 2885 | mutex_unlock(&extent_root->fs_info->pinned_mutex); |
| 2886 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2887 | ret = update_block_group(trans, extent_root, start, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2888 | end + 1 - start, 0, ret > 0); |
| 2889 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2890 | unlock_extent(extent_ins, start, end, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2891 | BUG_ON(ret); |
| 2892 | kfree(extent_op); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2893 | } |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 2894 | if (ret) |
| 2895 | err = ret; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 2896 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2897 | search = end + 1; |
| 2898 | |
| 2899 | if (need_resched()) { |
| 2900 | mutex_unlock(&info->extent_ins_mutex); |
| 2901 | cond_resched(); |
| 2902 | mutex_lock(&info->extent_ins_mutex); |
| 2903 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2904 | } |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2905 | |
| 2906 | if (nr) { |
| 2907 | ret = free_extents(trans, extent_root, &delete_list); |
| 2908 | BUG_ON(ret); |
| 2909 | } |
| 2910 | |
| 2911 | if (all && skipped) { |
| 2912 | INIT_LIST_HEAD(&delete_list); |
| 2913 | search = 0; |
| 2914 | nr = 0; |
| 2915 | goto again; |
| 2916 | } |
| 2917 | |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2918 | if (!err) |
| 2919 | finish_current_insert(trans, extent_root, 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2920 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | /* |
| 2924 | * remove an extent from the root, returns 0 on success |
| 2925 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2926 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2927 | struct btrfs_root *root, |
| 2928 | u64 bytenr, u64 num_bytes, u64 parent, |
| 2929 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 2930 | u64 owner_objectid, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2931 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 2932 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 2933 | int pending_ret; |
| 2934 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2935 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2936 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2937 | if (root == extent_root) { |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2938 | struct pending_extent_op *extent_op = NULL; |
| 2939 | |
| 2940 | mutex_lock(&root->fs_info->extent_ins_mutex); |
| 2941 | if (test_range_bit(&root->fs_info->extent_ins, bytenr, |
| 2942 | bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) { |
| 2943 | u64 priv; |
| 2944 | ret = get_state_private(&root->fs_info->extent_ins, |
| 2945 | bytenr, &priv); |
| 2946 | BUG_ON(ret); |
| 2947 | extent_op = (struct pending_extent_op *) |
| 2948 | (unsigned long)priv; |
| 2949 | |
| 2950 | extent_op->del = 1; |
| 2951 | if (extent_op->type == PENDING_EXTENT_INSERT) { |
| 2952 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
| 2953 | return 0; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | if (extent_op) { |
| 2958 | ref_generation = extent_op->orig_generation; |
| 2959 | parent = extent_op->orig_parent; |
| 2960 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2961 | |
| 2962 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2963 | BUG_ON(!extent_op); |
| 2964 | |
| 2965 | extent_op->type = PENDING_EXTENT_DELETE; |
| 2966 | extent_op->bytenr = bytenr; |
| 2967 | extent_op->num_bytes = num_bytes; |
| 2968 | extent_op->parent = parent; |
| 2969 | extent_op->orig_parent = parent; |
| 2970 | extent_op->generation = ref_generation; |
| 2971 | extent_op->orig_generation = ref_generation; |
| 2972 | extent_op->level = (int)owner_objectid; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 2973 | INIT_LIST_HEAD(&extent_op->list); |
| 2974 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2975 | |
| 2976 | set_extent_bits(&root->fs_info->pending_del, |
| 2977 | bytenr, bytenr + num_bytes - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2978 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2979 | set_state_private(&root->fs_info->pending_del, |
| 2980 | bytenr, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2981 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2982 | return 0; |
| 2983 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2984 | /* if metadata always pin */ |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2985 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 2986 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan Zheng | 7237f18 | 2009-01-21 12:54:03 -0500 | [diff] [blame] | 2987 | mutex_lock(&root->fs_info->pinned_mutex); |
| 2988 | btrfs_update_pinned_extents(root, bytenr, num_bytes, 1); |
| 2989 | mutex_unlock(&root->fs_info->pinned_mutex); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2990 | update_reserved_extents(root, bytenr, num_bytes, 0); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2991 | return 0; |
| 2992 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2993 | pin = 1; |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 2994 | } |
Chris Mason | 4bef084 | 2008-09-08 11:18:08 -0400 | [diff] [blame] | 2995 | |
| 2996 | /* if data pin when any transaction has committed this */ |
| 2997 | if (ref_generation != trans->transid) |
| 2998 | pin = 1; |
| 2999 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3000 | ret = __free_extent(trans, root, bytenr, num_bytes, parent, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3001 | root_objectid, ref_generation, |
| 3002 | owner_objectid, pin, pin == 0); |
Chris Mason | ee6e650 | 2008-07-17 12:54:40 -0400 | [diff] [blame] | 3003 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 3004 | finish_current_insert(trans, root->fs_info->extent_root, 0); |
| 3005 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3006 | return ret ? ret : pending_ret; |
| 3007 | } |
| 3008 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3009 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3010 | struct btrfs_root *root, |
| 3011 | u64 bytenr, u64 num_bytes, u64 parent, |
| 3012 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3013 | u64 owner_objectid, int pin) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3014 | { |
| 3015 | int ret; |
| 3016 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3017 | ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent, |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3018 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3019 | owner_objectid, pin); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3020 | return ret; |
| 3021 | } |
| 3022 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 3023 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 3024 | { |
| 3025 | u64 mask = ((u64)root->stripesize - 1); |
| 3026 | u64 ret = (val + mask) & ~mask; |
| 3027 | return ret; |
| 3028 | } |
| 3029 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3030 | /* |
| 3031 | * walks the btree of allocated extents and find a hole of a given size. |
| 3032 | * The key ins is changed to record the hole: |
| 3033 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 3034 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3035 | * ins->offset == number of blocks |
| 3036 | * Any available blocks before search_start are skipped. |
| 3037 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3038 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3039 | struct btrfs_root *orig_root, |
| 3040 | u64 num_bytes, u64 empty_size, |
| 3041 | u64 search_start, u64 search_end, |
| 3042 | u64 hint_byte, struct btrfs_key *ins, |
| 3043 | u64 exclude_start, u64 exclude_nr, |
| 3044 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3045 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3046 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3047 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3048 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3049 | u64 *last_ptr = NULL; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3050 | u64 last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3051 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3052 | int chunk_alloc_done = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3053 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3054 | int allowed_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3055 | struct list_head *head = NULL, *cur = NULL; |
| 3056 | int loop = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3057 | int extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3058 | struct btrfs_space_info *space_info; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3059 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3060 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 3061 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3062 | ins->objectid = 0; |
| 3063 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 3064 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3065 | if (orig_root->ref_cows || empty_size) |
| 3066 | allowed_chunk_alloc = 1; |
| 3067 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3068 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 3069 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 3070 | if (!btrfs_test_opt(root, SSD)) |
| 3071 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3072 | } |
| 3073 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3074 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3075 | last_ptr = &root->fs_info->last_data_alloc; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3076 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3077 | if (last_ptr) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3078 | if (*last_ptr) { |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3079 | hint_byte = *last_ptr; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3080 | last_wanted = *last_ptr; |
| 3081 | } else |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3082 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3083 | } else { |
| 3084 | empty_cluster = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3085 | } |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3086 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 3087 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3088 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3089 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3090 | last_wanted = 0; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3091 | empty_size += empty_cluster; |
| 3092 | } |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3093 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3094 | total_needed += empty_size; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3095 | block_group = btrfs_lookup_block_group(root->fs_info, search_start); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 3096 | if (!block_group) |
| 3097 | block_group = btrfs_lookup_first_block_group(root->fs_info, |
| 3098 | search_start); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3099 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3100 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3101 | down_read(&space_info->groups_sem); |
| 3102 | while (1) { |
| 3103 | struct btrfs_free_space *free_space; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3104 | /* |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3105 | * the only way this happens if our hint points to a block |
| 3106 | * group thats not of the proper type, while looping this |
| 3107 | * should never happen |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3108 | */ |
Chris Mason | 8a1413a2 | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 3109 | if (empty_size) |
| 3110 | extra_loop = 1; |
| 3111 | |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3112 | if (!block_group) |
| 3113 | goto new_group_no_lock; |
| 3114 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3115 | if (unlikely(!block_group->cached)) { |
| 3116 | mutex_lock(&block_group->cache_mutex); |
| 3117 | ret = cache_block_group(root, block_group); |
| 3118 | mutex_unlock(&block_group->cache_mutex); |
| 3119 | if (ret) |
| 3120 | break; |
| 3121 | } |
| 3122 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3123 | mutex_lock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3124 | if (unlikely(!block_group_bits(block_group, data))) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3125 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3126 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3127 | if (unlikely(block_group->ro)) |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3128 | goto new_group; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3129 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3130 | free_space = btrfs_find_free_space(block_group, search_start, |
| 3131 | total_needed); |
| 3132 | if (free_space) { |
| 3133 | u64 start = block_group->key.objectid; |
| 3134 | u64 end = block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3135 | block_group->key.offset; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3136 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3137 | search_start = stripe_align(root, free_space->offset); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3138 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3139 | /* move on to the next group */ |
| 3140 | if (search_start + num_bytes >= search_end) |
| 3141 | goto new_group; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3142 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3143 | /* move on to the next group */ |
| 3144 | if (search_start + num_bytes > end) |
| 3145 | goto new_group; |
| 3146 | |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3147 | if (last_wanted && search_start != last_wanted) { |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 3148 | total_needed += empty_cluster; |
Chris Mason | 8a1413a2 | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 3149 | empty_size += empty_cluster; |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3150 | last_wanted = 0; |
Chris Mason | 3b7885b | 2008-11-06 21:48:27 -0500 | [diff] [blame] | 3151 | /* |
| 3152 | * if search_start is still in this block group |
| 3153 | * then we just re-search this block group |
| 3154 | */ |
| 3155 | if (search_start >= start && |
| 3156 | search_start < end) { |
| 3157 | mutex_unlock(&block_group->alloc_mutex); |
| 3158 | continue; |
| 3159 | } |
| 3160 | |
| 3161 | /* else we go to the next block group */ |
| 3162 | goto new_group; |
| 3163 | } |
| 3164 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3165 | if (exclude_nr > 0 && |
| 3166 | (search_start + num_bytes > exclude_start && |
| 3167 | search_start < exclude_start + exclude_nr)) { |
| 3168 | search_start = exclude_start + exclude_nr; |
| 3169 | /* |
| 3170 | * if search_start is still in this block group |
| 3171 | * then we just re-search this block group |
| 3172 | */ |
| 3173 | if (search_start >= start && |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3174 | search_start < end) { |
| 3175 | mutex_unlock(&block_group->alloc_mutex); |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3176 | last_wanted = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3177 | continue; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3178 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3179 | |
| 3180 | /* else we go to the next block group */ |
| 3181 | goto new_group; |
| 3182 | } |
| 3183 | |
| 3184 | ins->objectid = search_start; |
| 3185 | ins->offset = num_bytes; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3186 | |
| 3187 | btrfs_remove_free_space_lock(block_group, search_start, |
| 3188 | num_bytes); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3189 | /* we are all good, lets return */ |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3190 | mutex_unlock(&block_group->alloc_mutex); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3191 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3192 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3193 | new_group: |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3194 | mutex_unlock(&block_group->alloc_mutex); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3195 | put_block_group(block_group); |
| 3196 | block_group = NULL; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3197 | new_group_no_lock: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3198 | /* don't try to compare new allocations against the |
| 3199 | * last allocation any more |
| 3200 | */ |
Chris Mason | 4366211 | 2008-11-07 09:06:11 -0500 | [diff] [blame] | 3201 | last_wanted = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3202 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3203 | /* |
| 3204 | * Here's how this works. |
| 3205 | * loop == 0: we were searching a block group via a hint |
| 3206 | * and didn't find anything, so we start at |
| 3207 | * the head of the block groups and keep searching |
| 3208 | * loop == 1: we're searching through all of the block groups |
| 3209 | * if we hit the head again we have searched |
| 3210 | * all of the block groups for this space and we |
| 3211 | * need to try and allocate, if we cant error out. |
| 3212 | * loop == 2: we allocated more space and are looping through |
| 3213 | * all of the block groups again. |
| 3214 | */ |
| 3215 | if (loop == 0) { |
| 3216 | head = &space_info->block_groups; |
| 3217 | cur = head->next; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3218 | loop++; |
| 3219 | } else if (loop == 1 && cur == head) { |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3220 | int keep_going; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3221 | |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3222 | /* at this point we give up on the empty_size |
| 3223 | * allocations and just try to allocate the min |
| 3224 | * space. |
| 3225 | * |
| 3226 | * The extra_loop field was set if an empty_size |
| 3227 | * allocation was attempted above, and if this |
| 3228 | * is try we need to try the loop again without |
| 3229 | * the additional empty_size. |
| 3230 | */ |
Chris Mason | 5b7c3fc | 2008-11-10 07:26:33 -0500 | [diff] [blame] | 3231 | total_needed -= empty_size; |
| 3232 | empty_size = 0; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3233 | keep_going = extra_loop; |
| 3234 | loop++; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 3235 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3236 | if (allowed_chunk_alloc && !chunk_alloc_done) { |
| 3237 | up_read(&space_info->groups_sem); |
| 3238 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 3239 | 2 * 1024 * 1024, data, 1); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3240 | down_read(&space_info->groups_sem); |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 3241 | if (ret < 0) |
| 3242 | goto loop_check; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3243 | head = &space_info->block_groups; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3244 | /* |
| 3245 | * we've allocated a new chunk, keep |
| 3246 | * trying |
| 3247 | */ |
| 3248 | keep_going = 1; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3249 | chunk_alloc_done = 1; |
| 3250 | } else if (!allowed_chunk_alloc) { |
| 3251 | space_info->force_alloc = 1; |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3252 | } |
Chris Mason | 2ed6d66 | 2008-11-13 09:59:33 -0500 | [diff] [blame] | 3253 | loop_check: |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 3254 | if (keep_going) { |
| 3255 | cur = head->next; |
| 3256 | extra_loop = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3257 | } else { |
| 3258 | break; |
| 3259 | } |
| 3260 | } else if (cur == head) { |
| 3261 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3262 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3263 | |
| 3264 | block_group = list_entry(cur, struct btrfs_block_group_cache, |
| 3265 | list); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3266 | atomic_inc(&block_group->count); |
| 3267 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3268 | search_start = block_group->key.objectid; |
| 3269 | cur = cur->next; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 3270 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3271 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3272 | /* we found what we needed */ |
| 3273 | if (ins->objectid) { |
| 3274 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3275 | trans->block_group = block_group->key.objectid; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3276 | |
| 3277 | if (last_ptr) |
| 3278 | *last_ptr = ins->objectid + ins->offset; |
| 3279 | ret = 0; |
| 3280 | } else if (!ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3281 | printk(KERN_ERR "btrfs searching for %llu bytes, " |
| 3282 | "num_bytes %llu, loop %d, allowed_alloc %d\n", |
| 3283 | (unsigned long long)total_needed, |
| 3284 | (unsigned long long)num_bytes, |
Josef Bacik | 4ce4cb5 | 2008-11-17 21:12:00 -0500 | [diff] [blame] | 3285 | loop, allowed_chunk_alloc); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3286 | ret = -ENOSPC; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 3287 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3288 | if (block_group) |
| 3289 | put_block_group(block_group); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3290 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3291 | up_read(&space_info->groups_sem); |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 3292 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3293 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3294 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3295 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes) |
| 3296 | { |
| 3297 | struct btrfs_block_group_cache *cache; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3298 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3299 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 3300 | (unsigned long long)(info->total_bytes - info->bytes_used - |
| 3301 | info->bytes_pinned - info->bytes_reserved), |
| 3302 | (info->full) ? "" : "not "); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame^] | 3303 | printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu," |
| 3304 | " may_use=%llu, used=%llu\n", info->total_bytes, |
| 3305 | info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use, |
| 3306 | info->bytes_used); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3307 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3308 | down_read(&info->groups_sem); |
Qinghuang Feng | c6e3087 | 2009-01-21 10:59:08 -0500 | [diff] [blame] | 3309 | list_for_each_entry(cache, &info->block_groups, list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3310 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3311 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 3312 | "%llu pinned %llu reserved\n", |
| 3313 | (unsigned long long)cache->key.objectid, |
| 3314 | (unsigned long long)cache->key.offset, |
| 3315 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 3316 | (unsigned long long)cache->pinned, |
| 3317 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3318 | btrfs_dump_free_space(cache, bytes); |
| 3319 | spin_unlock(&cache->lock); |
| 3320 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3321 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3322 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3323 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3324 | static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3325 | struct btrfs_root *root, |
| 3326 | u64 num_bytes, u64 min_alloc_size, |
| 3327 | u64 empty_size, u64 hint_byte, |
| 3328 | u64 search_end, struct btrfs_key *ins, |
| 3329 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3330 | { |
| 3331 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 3332 | u64 search_start = 0; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 3333 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3334 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame^] | 3335 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3336 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3337 | /* |
| 3338 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 3339 | * is not called recursively on allocations |
| 3340 | */ |
| 3341 | if (empty_size || root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3342 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3343 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3344 | 2 * 1024 * 1024, |
| 3345 | BTRFS_BLOCK_GROUP_METADATA | |
| 3346 | (info->metadata_alloc_profile & |
| 3347 | info->avail_metadata_alloc_bits), 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3348 | } |
| 3349 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3350 | num_bytes + 2 * 1024 * 1024, data, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3351 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3352 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3353 | WARN_ON(num_bytes < root->sectorsize); |
| 3354 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 3355 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3356 | trans->alloc_exclude_start, |
| 3357 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 3358 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3359 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 3360 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3361 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3362 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3363 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3364 | num_bytes, data, 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 3365 | goto again; |
| 3366 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3367 | if (ret) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3368 | struct btrfs_space_info *sinfo; |
| 3369 | |
| 3370 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3371 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 3372 | "wanted %llu\n", (unsigned long long)data, |
| 3373 | (unsigned long long)num_bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3374 | dump_space_info(sinfo, num_bytes); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3375 | BUG(); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3376 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3377 | |
| 3378 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3379 | } |
| 3380 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3381 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 3382 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3383 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3384 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3385 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3386 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 3387 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3388 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 3389 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3390 | return -ENOSPC; |
| 3391 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3392 | |
| 3393 | ret = btrfs_discard_extent(root, start, len); |
| 3394 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3395 | btrfs_add_free_space(cache, start, len); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3396 | put_block_group(cache); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 3397 | update_reserved_extents(root, start, len, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 3398 | |
| 3399 | return ret; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3400 | } |
| 3401 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3402 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 3403 | struct btrfs_root *root, |
| 3404 | u64 num_bytes, u64 min_alloc_size, |
| 3405 | u64 empty_size, u64 hint_byte, |
| 3406 | u64 search_end, struct btrfs_key *ins, |
| 3407 | u64 data) |
| 3408 | { |
| 3409 | int ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3410 | ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size, |
| 3411 | empty_size, hint_byte, search_end, ins, |
| 3412 | data); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3413 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3414 | return ret; |
| 3415 | } |
| 3416 | |
| 3417 | static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3418 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3419 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3420 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3421 | { |
| 3422 | int ret; |
| 3423 | int pending_ret; |
| 3424 | u64 super_used; |
| 3425 | u64 root_used; |
| 3426 | u64 num_bytes = ins->offset; |
| 3427 | u32 sizes[2]; |
| 3428 | struct btrfs_fs_info *info = root->fs_info; |
| 3429 | struct btrfs_root *extent_root = info->extent_root; |
| 3430 | struct btrfs_extent_item *extent_item; |
| 3431 | struct btrfs_extent_ref *ref; |
| 3432 | struct btrfs_path *path; |
| 3433 | struct btrfs_key keys[2]; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 3434 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3435 | if (parent == 0) |
| 3436 | parent = ins->objectid; |
| 3437 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3438 | /* block accounting for super block */ |
Chris Mason | 75eff68 | 2008-12-15 15:54:40 -0500 | [diff] [blame] | 3439 | spin_lock(&info->delalloc_lock); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3440 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 3441 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3442 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3443 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3444 | root_used = btrfs_root_used(&root->root_item); |
| 3445 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Yan Zheng | 34bf63c | 2008-12-19 10:58:46 -0500 | [diff] [blame] | 3446 | spin_unlock(&info->delalloc_lock); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 3447 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3448 | if (root == extent_root) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3449 | struct pending_extent_op *extent_op; |
| 3450 | |
| 3451 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 3452 | BUG_ON(!extent_op); |
| 3453 | |
| 3454 | extent_op->type = PENDING_EXTENT_INSERT; |
| 3455 | extent_op->bytenr = ins->objectid; |
| 3456 | extent_op->num_bytes = ins->offset; |
| 3457 | extent_op->parent = parent; |
| 3458 | extent_op->orig_parent = 0; |
| 3459 | extent_op->generation = ref_generation; |
| 3460 | extent_op->orig_generation = 0; |
| 3461 | extent_op->level = (int)owner; |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 3462 | INIT_LIST_HEAD(&extent_op->list); |
| 3463 | extent_op->del = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3464 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3465 | mutex_lock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 3466 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 3467 | ins->objectid + ins->offset - 1, |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3468 | EXTENT_WRITEBACK, GFP_NOFS); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3469 | set_state_private(&root->fs_info->extent_ins, |
| 3470 | ins->objectid, (unsigned long)extent_op); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3471 | mutex_unlock(&root->fs_info->extent_ins_mutex); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3472 | goto update_block; |
| 3473 | } |
| 3474 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3475 | memcpy(&keys[0], ins, sizeof(*ins)); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3476 | keys[1].objectid = ins->objectid; |
| 3477 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3478 | keys[1].offset = parent; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3479 | sizes[0] = sizeof(*extent_item); |
| 3480 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3481 | |
| 3482 | path = btrfs_alloc_path(); |
| 3483 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3484 | |
| 3485 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 3486 | sizes, 2); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 3487 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3488 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3489 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3490 | struct btrfs_extent_item); |
| 3491 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); |
| 3492 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 3493 | struct btrfs_extent_ref); |
| 3494 | |
| 3495 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 3496 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 3497 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3498 | btrfs_set_ref_num_refs(path->nodes[0], ref, 1); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 3499 | |
| 3500 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 3501 | |
| 3502 | trans->alloc_exclude_start = 0; |
| 3503 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3504 | btrfs_free_path(path); |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 3505 | finish_current_insert(trans, extent_root, 0); |
| 3506 | pending_ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 3507 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3508 | if (ret) |
| 3509 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3510 | if (pending_ret) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3511 | ret = pending_ret; |
| 3512 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 3513 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 3514 | |
| 3515 | update_block: |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3516 | ret = update_block_group(trans, root, ins->objectid, |
| 3517 | ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 3518 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3519 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 3520 | "%llu\n", (unsigned long long)ins->objectid, |
| 3521 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 3522 | BUG(); |
| 3523 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3524 | out: |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3525 | return ret; |
| 3526 | } |
| 3527 | |
| 3528 | int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3529 | struct btrfs_root *root, u64 parent, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3530 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3531 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3532 | { |
| 3533 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 3534 | |
| 3535 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) |
| 3536 | return 0; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3537 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 3538 | ref_generation, owner, ins); |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3539 | update_reserved_extents(root, ins->objectid, ins->offset, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3540 | return ret; |
| 3541 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3542 | |
| 3543 | /* |
| 3544 | * this is used by the tree logging recovery code. It records that |
| 3545 | * an extent has been allocated and makes sure to clear the free |
| 3546 | * space cache bits as well |
| 3547 | */ |
| 3548 | int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3549 | struct btrfs_root *root, u64 parent, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3550 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3551 | u64 owner, struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3552 | { |
| 3553 | int ret; |
| 3554 | struct btrfs_block_group_cache *block_group; |
| 3555 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3556 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3557 | mutex_lock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3558 | cache_block_group(root, block_group); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3559 | mutex_unlock(&block_group->cache_mutex); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3560 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 3561 | ret = btrfs_remove_free_space(block_group, ins->objectid, |
| 3562 | ins->offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3563 | BUG_ON(ret); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3564 | put_block_group(block_group); |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3565 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid, |
| 3566 | ref_generation, owner, ins); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3567 | return ret; |
| 3568 | } |
| 3569 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3570 | /* |
| 3571 | * finds a free extent and does all the dirty work required for allocation |
| 3572 | * returns the key for the extent through ins, and a tree buffer for |
| 3573 | * the first block of the extent through buf. |
| 3574 | * |
| 3575 | * returns 0 if everything worked, non-zero otherwise. |
| 3576 | */ |
| 3577 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
| 3578 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3579 | u64 num_bytes, u64 parent, u64 min_alloc_size, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3580 | u64 root_objectid, u64 ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3581 | u64 owner_objectid, u64 empty_size, u64 hint_byte, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3582 | u64 search_end, struct btrfs_key *ins, u64 data) |
| 3583 | { |
| 3584 | int ret; |
| 3585 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3586 | ret = __btrfs_reserve_extent(trans, root, num_bytes, |
| 3587 | min_alloc_size, empty_size, hint_byte, |
| 3588 | search_end, ins, data); |
| 3589 | BUG_ON(ret); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3590 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3591 | ret = __btrfs_alloc_reserved_extent(trans, root, parent, |
| 3592 | root_objectid, ref_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3593 | owner_objectid, ins); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3594 | BUG_ON(ret); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 3595 | |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3596 | } else { |
| 3597 | update_reserved_extents(root, ins->objectid, ins->offset, 1); |
Chris Mason | d00aff0 | 2008-09-11 15:54:42 -0400 | [diff] [blame] | 3598 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3599 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3600 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3601 | |
| 3602 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 3603 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3604 | u64 bytenr, u32 blocksize, |
| 3605 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3606 | { |
| 3607 | struct extent_buffer *buf; |
| 3608 | |
| 3609 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 3610 | if (!buf) |
| 3611 | return ERR_PTR(-ENOMEM); |
| 3612 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3613 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3614 | btrfs_tree_lock(buf); |
| 3615 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3616 | |
| 3617 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3618 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3619 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3620 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 3621 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3622 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 3623 | } else { |
| 3624 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 3625 | buf->start + buf->len - 1, GFP_NOFS); |
| 3626 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3627 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 3628 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 3629 | return buf; |
| 3630 | } |
| 3631 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3632 | /* |
| 3633 | * helper function to allocate a block for a given tree |
| 3634 | * returns the tree buffer or NULL. |
| 3635 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3636 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3637 | struct btrfs_root *root, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3638 | u32 blocksize, u64 parent, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3639 | u64 root_objectid, |
| 3640 | u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3641 | int level, |
| 3642 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3643 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3644 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 3645 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3646 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3647 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3648 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3649 | ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3650 | root_objectid, ref_generation, level, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3651 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3652 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3653 | BUG_ON(ret > 0); |
| 3654 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3655 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 3656 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 3657 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 3658 | blocksize, level); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 3659 | return buf; |
| 3660 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 3661 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3662 | int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 3663 | struct btrfs_root *root, struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3664 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3665 | u64 leaf_owner; |
| 3666 | u64 leaf_generation; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3667 | struct refsort *sorted; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3668 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3669 | struct btrfs_file_extent_item *fi; |
| 3670 | int i; |
| 3671 | int nritems; |
| 3672 | int ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3673 | int refi = 0; |
| 3674 | int slot; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3675 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3676 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 3677 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3678 | leaf_owner = btrfs_header_owner(leaf); |
| 3679 | leaf_generation = btrfs_header_generation(leaf); |
| 3680 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3681 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); |
| 3682 | /* we do this loop twice. The first time we build a list |
| 3683 | * of the extents we have a reference on, then we sort the list |
| 3684 | * by bytenr. The second time around we actually do the |
| 3685 | * extent freeing. |
| 3686 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3687 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3688 | u64 disk_bytenr; |
Chris Mason | e34a5b4 | 2008-07-22 12:08:37 -0400 | [diff] [blame] | 3689 | cond_resched(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3690 | |
| 3691 | btrfs_item_key_to_cpu(leaf, &key, i); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3692 | |
| 3693 | /* only extents have references, skip everything else */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3694 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3695 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3696 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3697 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3698 | |
| 3699 | /* inline extents live in the btree, they don't have refs */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3700 | if (btrfs_file_extent_type(leaf, fi) == |
| 3701 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 3702 | continue; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3703 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3704 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3705 | |
| 3706 | /* holes don't have refs */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3707 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 3708 | continue; |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 3709 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3710 | sorted[refi].bytenr = disk_bytenr; |
| 3711 | sorted[refi].slot = i; |
| 3712 | refi++; |
| 3713 | } |
| 3714 | |
| 3715 | if (refi == 0) |
| 3716 | goto out; |
| 3717 | |
| 3718 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 3719 | |
| 3720 | for (i = 0; i < refi; i++) { |
| 3721 | u64 disk_bytenr; |
| 3722 | |
| 3723 | disk_bytenr = sorted[i].bytenr; |
| 3724 | slot = sorted[i].slot; |
| 3725 | |
| 3726 | cond_resched(); |
| 3727 | |
| 3728 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 3729 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 3730 | continue; |
| 3731 | |
| 3732 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 3733 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3734 | ret = __btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3735 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3736 | leaf->start, leaf_owner, leaf_generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3737 | key.objectid, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3738 | BUG_ON(ret); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3739 | |
| 3740 | atomic_inc(&root->fs_info->throttle_gen); |
| 3741 | wake_up(&root->fs_info->transaction_throttle); |
| 3742 | cond_resched(); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3743 | } |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3744 | out: |
| 3745 | kfree(sorted); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 3746 | return 0; |
| 3747 | } |
| 3748 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3749 | static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3750 | struct btrfs_root *root, |
| 3751 | struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3752 | { |
| 3753 | int i; |
| 3754 | int ret; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3755 | struct btrfs_extent_info *info; |
| 3756 | struct refsort *sorted; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3757 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3758 | if (ref->nritems == 0) |
| 3759 | return 0; |
| 3760 | |
| 3761 | sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3762 | for (i = 0; i < ref->nritems; i++) { |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3763 | sorted[i].bytenr = ref->extents[i].bytenr; |
| 3764 | sorted[i].slot = i; |
| 3765 | } |
| 3766 | sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL); |
| 3767 | |
| 3768 | /* |
| 3769 | * the items in the ref were sorted when the ref was inserted |
| 3770 | * into the ref cache, so this is already in order |
| 3771 | */ |
| 3772 | for (i = 0; i < ref->nritems; i++) { |
| 3773 | info = ref->extents + sorted[i].slot; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3774 | ret = __btrfs_free_extent(trans, root, info->bytenr, |
| 3775 | info->num_bytes, ref->bytenr, |
| 3776 | ref->owner, ref->generation, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 3777 | info->objectid, 0); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 3778 | |
| 3779 | atomic_inc(&root->fs_info->throttle_gen); |
| 3780 | wake_up(&root->fs_info->transaction_throttle); |
| 3781 | cond_resched(); |
| 3782 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3783 | BUG_ON(ret); |
| 3784 | info++; |
| 3785 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3786 | |
Chris Mason | 806638b | 2009-02-05 09:08:14 -0500 | [diff] [blame] | 3787 | kfree(sorted); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 3788 | return 0; |
| 3789 | } |
| 3790 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3791 | static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, |
| 3792 | u64 len, u32 *refs) |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3793 | { |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3794 | int ret; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3795 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3796 | ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3797 | BUG_ON(ret); |
| 3798 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3799 | #if 0 /* some debugging code in case we see problems here */ |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3800 | /* if the refs count is one, it won't get increased again. But |
| 3801 | * if the ref count is > 1, someone may be decreasing it at |
| 3802 | * the same time we are. |
| 3803 | */ |
| 3804 | if (*refs != 1) { |
| 3805 | struct extent_buffer *eb = NULL; |
| 3806 | eb = btrfs_find_create_tree_block(root, start, len); |
| 3807 | if (eb) |
| 3808 | btrfs_tree_lock(eb); |
| 3809 | |
| 3810 | mutex_lock(&root->fs_info->alloc_mutex); |
| 3811 | ret = lookup_extent_ref(NULL, root, start, len, refs); |
| 3812 | BUG_ON(ret); |
| 3813 | mutex_unlock(&root->fs_info->alloc_mutex); |
| 3814 | |
| 3815 | if (eb) { |
| 3816 | btrfs_tree_unlock(eb); |
| 3817 | free_extent_buffer(eb); |
| 3818 | } |
| 3819 | if (*refs == 1) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3820 | printk(KERN_ERR "btrfs block %llu went down to one " |
| 3821 | "during drop_snap\n", (unsigned long long)start); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | } |
| 3825 | #endif |
| 3826 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 3827 | cond_resched(); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 3828 | return ret; |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | /* |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 3832 | * this is used while deleting old snapshots, and it drops the refs |
| 3833 | * on a whole subtree starting from a level 1 node. |
| 3834 | * |
| 3835 | * The idea is to sort all the leaf pointers, and then drop the |
| 3836 | * ref on all the leaves in order. Most of the time the leaves |
| 3837 | * will have ref cache entries, so no leaf IOs will be required to |
| 3838 | * find the extents they have references on. |
| 3839 | * |
| 3840 | * For each leaf, any references it has are also dropped in order |
| 3841 | * |
| 3842 | * This ends up dropping the references in something close to optimal |
| 3843 | * order for reading and modifying the extent allocation tree. |
| 3844 | */ |
| 3845 | static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans, |
| 3846 | struct btrfs_root *root, |
| 3847 | struct btrfs_path *path) |
| 3848 | { |
| 3849 | u64 bytenr; |
| 3850 | u64 root_owner; |
| 3851 | u64 root_gen; |
| 3852 | struct extent_buffer *eb = path->nodes[1]; |
| 3853 | struct extent_buffer *leaf; |
| 3854 | struct btrfs_leaf_ref *ref; |
| 3855 | struct refsort *sorted = NULL; |
| 3856 | int nritems = btrfs_header_nritems(eb); |
| 3857 | int ret; |
| 3858 | int i; |
| 3859 | int refi = 0; |
| 3860 | int slot = path->slots[1]; |
| 3861 | u32 blocksize = btrfs_level_size(root, 0); |
| 3862 | u32 refs; |
| 3863 | |
| 3864 | if (nritems == 0) |
| 3865 | goto out; |
| 3866 | |
| 3867 | root_owner = btrfs_header_owner(eb); |
| 3868 | root_gen = btrfs_header_generation(eb); |
| 3869 | sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS); |
| 3870 | |
| 3871 | /* |
| 3872 | * step one, sort all the leaf pointers so we don't scribble |
| 3873 | * randomly into the extent allocation tree |
| 3874 | */ |
| 3875 | for (i = slot; i < nritems; i++) { |
| 3876 | sorted[refi].bytenr = btrfs_node_blockptr(eb, i); |
| 3877 | sorted[refi].slot = i; |
| 3878 | refi++; |
| 3879 | } |
| 3880 | |
| 3881 | /* |
| 3882 | * nritems won't be zero, but if we're picking up drop_snapshot |
| 3883 | * after a crash, slot might be > 0, so double check things |
| 3884 | * just in case. |
| 3885 | */ |
| 3886 | if (refi == 0) |
| 3887 | goto out; |
| 3888 | |
| 3889 | sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL); |
| 3890 | |
| 3891 | /* |
| 3892 | * the first loop frees everything the leaves point to |
| 3893 | */ |
| 3894 | for (i = 0; i < refi; i++) { |
| 3895 | u64 ptr_gen; |
| 3896 | |
| 3897 | bytenr = sorted[i].bytenr; |
| 3898 | |
| 3899 | /* |
| 3900 | * check the reference count on this leaf. If it is > 1 |
| 3901 | * we just decrement it below and don't update any |
| 3902 | * of the refs the leaf points to. |
| 3903 | */ |
| 3904 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
| 3905 | BUG_ON(ret); |
| 3906 | if (refs != 1) |
| 3907 | continue; |
| 3908 | |
| 3909 | ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot); |
| 3910 | |
| 3911 | /* |
| 3912 | * the leaf only had one reference, which means the |
| 3913 | * only thing pointing to this leaf is the snapshot |
| 3914 | * we're deleting. It isn't possible for the reference |
| 3915 | * count to increase again later |
| 3916 | * |
| 3917 | * The reference cache is checked for the leaf, |
| 3918 | * and if found we'll be able to drop any refs held by |
| 3919 | * the leaf without needing to read it in. |
| 3920 | */ |
| 3921 | ref = btrfs_lookup_leaf_ref(root, bytenr); |
| 3922 | if (ref && ref->generation != ptr_gen) { |
| 3923 | btrfs_free_leaf_ref(root, ref); |
| 3924 | ref = NULL; |
| 3925 | } |
| 3926 | if (ref) { |
| 3927 | ret = cache_drop_leaf_ref(trans, root, ref); |
| 3928 | BUG_ON(ret); |
| 3929 | btrfs_remove_leaf_ref(root, ref); |
| 3930 | btrfs_free_leaf_ref(root, ref); |
| 3931 | } else { |
| 3932 | /* |
| 3933 | * the leaf wasn't in the reference cache, so |
| 3934 | * we have to read it. |
| 3935 | */ |
| 3936 | leaf = read_tree_block(root, bytenr, blocksize, |
| 3937 | ptr_gen); |
| 3938 | ret = btrfs_drop_leaf_ref(trans, root, leaf); |
| 3939 | BUG_ON(ret); |
| 3940 | free_extent_buffer(leaf); |
| 3941 | } |
| 3942 | atomic_inc(&root->fs_info->throttle_gen); |
| 3943 | wake_up(&root->fs_info->transaction_throttle); |
| 3944 | cond_resched(); |
| 3945 | } |
| 3946 | |
| 3947 | /* |
| 3948 | * run through the loop again to free the refs on the leaves. |
| 3949 | * This is faster than doing it in the loop above because |
| 3950 | * the leaves are likely to be clustered together. We end up |
| 3951 | * working in nice chunks on the extent allocation tree. |
| 3952 | */ |
| 3953 | for (i = 0; i < refi; i++) { |
| 3954 | bytenr = sorted[i].bytenr; |
| 3955 | ret = __btrfs_free_extent(trans, root, bytenr, |
| 3956 | blocksize, eb->start, |
| 3957 | root_owner, root_gen, 0, 1); |
| 3958 | BUG_ON(ret); |
| 3959 | |
| 3960 | atomic_inc(&root->fs_info->throttle_gen); |
| 3961 | wake_up(&root->fs_info->transaction_throttle); |
| 3962 | cond_resched(); |
| 3963 | } |
| 3964 | out: |
| 3965 | kfree(sorted); |
| 3966 | |
| 3967 | /* |
| 3968 | * update the path to show we've processed the entire level 1 |
| 3969 | * node. This will get saved into the root's drop_snapshot_progress |
| 3970 | * field so these drops are not repeated again if this transaction |
| 3971 | * commits. |
| 3972 | */ |
| 3973 | path->slots[1] = nritems; |
| 3974 | return 0; |
| 3975 | } |
| 3976 | |
| 3977 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 3978 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 3979 | * counts as it goes. |
| 3980 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3981 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3982 | struct btrfs_root *root, |
| 3983 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3984 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3985 | u64 root_owner; |
| 3986 | u64 root_gen; |
| 3987 | u64 bytenr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 3988 | u64 ptr_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3989 | struct extent_buffer *next; |
| 3990 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3991 | struct extent_buffer *parent; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3992 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 3993 | int ret; |
| 3994 | u32 refs; |
| 3995 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 3996 | WARN_ON(*level < 0); |
| 3997 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3998 | ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3999 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4000 | BUG_ON(ret); |
| 4001 | if (refs > 1) |
| 4002 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4003 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4004 | /* |
| 4005 | * walk down to the last node level and free all the leaves |
| 4006 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4007 | while (*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4008 | WARN_ON(*level < 0); |
| 4009 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4010 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4011 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4012 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4013 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 4014 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4015 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4016 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4017 | break; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4018 | |
| 4019 | /* the new code goes down to level 1 and does all the |
| 4020 | * leaves pointed to that node in bulk. So, this check |
| 4021 | * for level 0 will always be false. |
| 4022 | * |
| 4023 | * But, the disk format allows the drop_snapshot_progress |
| 4024 | * field in the root to leave things in a state where |
| 4025 | * a leaf will need cleaning up here. If someone crashes |
| 4026 | * with the old code and then boots with the new code, |
| 4027 | * we might find a leaf here. |
| 4028 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4029 | if (*level == 0) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 4030 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4031 | BUG_ON(ret); |
| 4032 | break; |
| 4033 | } |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4034 | |
| 4035 | /* |
| 4036 | * once we get to level one, process the whole node |
| 4037 | * at once, including everything below it. |
| 4038 | */ |
| 4039 | if (*level == 1) { |
| 4040 | ret = drop_level_one_refs(trans, root, path); |
| 4041 | BUG_ON(ret); |
| 4042 | break; |
| 4043 | } |
| 4044 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4045 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 4046 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4047 | blocksize = btrfs_level_size(root, *level - 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4048 | |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4049 | ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4050 | BUG_ON(ret); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4051 | |
| 4052 | /* |
| 4053 | * if there is more than one reference, we don't need |
| 4054 | * to read that node to drop any references it has. We |
| 4055 | * just drop the ref we hold on that node and move on to the |
| 4056 | * next slot in this level. |
| 4057 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4058 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4059 | parent = path->nodes[*level]; |
| 4060 | root_owner = btrfs_header_owner(parent); |
| 4061 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4062 | path->slots[*level]++; |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4063 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4064 | ret = __btrfs_free_extent(trans, root, bytenr, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4065 | blocksize, parent->start, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4066 | root_owner, root_gen, |
| 4067 | *level - 1, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4068 | BUG_ON(ret); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 4069 | |
| 4070 | atomic_inc(&root->fs_info->throttle_gen); |
| 4071 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 4072 | cond_resched(); |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 4073 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4074 | continue; |
| 4075 | } |
Chris Mason | 333db94 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4076 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4077 | /* |
| 4078 | * we need to keep freeing things in the next level down. |
| 4079 | * read the block and loop around to process it |
| 4080 | */ |
| 4081 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4082 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 4083 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4084 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4085 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4086 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4087 | path->slots[*level] = 0; |
Chris Mason | 2dd3e67 | 2008-08-04 08:20:15 -0400 | [diff] [blame] | 4088 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4089 | } |
| 4090 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4091 | WARN_ON(*level < 0); |
| 4092 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4093 | |
| 4094 | if (path->nodes[*level] == root->node) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4095 | parent = path->nodes[*level]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4096 | bytenr = path->nodes[*level]->start; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4097 | } else { |
| 4098 | parent = path->nodes[*level + 1]; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4099 | bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4100 | } |
| 4101 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4102 | blocksize = btrfs_level_size(root, *level); |
| 4103 | root_owner = btrfs_header_owner(parent); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4104 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4105 | |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4106 | /* |
| 4107 | * cleanup and free the reference on the last node |
| 4108 | * we processed |
| 4109 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 4110 | ret = __btrfs_free_extent(trans, root, bytenr, blocksize, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4111 | parent->start, root_owner, root_gen, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4112 | *level, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4113 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4114 | path->nodes[*level] = NULL; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4115 | |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4116 | *level += 1; |
| 4117 | BUG_ON(ret); |
Chris Mason | f87f057 | 2008-08-01 11:27:23 -0400 | [diff] [blame] | 4118 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4119 | cond_resched(); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4120 | return 0; |
| 4121 | } |
| 4122 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4123 | /* |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4124 | * helper function for drop_subtree, this function is similar to |
| 4125 | * walk_down_tree. The main difference is that it checks reference |
| 4126 | * counts while tree blocks are locked. |
| 4127 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4128 | static noinline int walk_down_subtree(struct btrfs_trans_handle *trans, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4129 | struct btrfs_root *root, |
| 4130 | struct btrfs_path *path, int *level) |
| 4131 | { |
| 4132 | struct extent_buffer *next; |
| 4133 | struct extent_buffer *cur; |
| 4134 | struct extent_buffer *parent; |
| 4135 | u64 bytenr; |
| 4136 | u64 ptr_gen; |
| 4137 | u32 blocksize; |
| 4138 | u32 refs; |
| 4139 | int ret; |
| 4140 | |
| 4141 | cur = path->nodes[*level]; |
| 4142 | ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len, |
| 4143 | &refs); |
| 4144 | BUG_ON(ret); |
| 4145 | if (refs > 1) |
| 4146 | goto out; |
| 4147 | |
| 4148 | while (*level >= 0) { |
| 4149 | cur = path->nodes[*level]; |
| 4150 | if (*level == 0) { |
| 4151 | ret = btrfs_drop_leaf_ref(trans, root, cur); |
| 4152 | BUG_ON(ret); |
| 4153 | clean_tree_block(trans, root, cur); |
| 4154 | break; |
| 4155 | } |
| 4156 | if (path->slots[*level] >= btrfs_header_nritems(cur)) { |
| 4157 | clean_tree_block(trans, root, cur); |
| 4158 | break; |
| 4159 | } |
| 4160 | |
| 4161 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 4162 | blocksize = btrfs_level_size(root, *level - 1); |
| 4163 | ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); |
| 4164 | |
| 4165 | next = read_tree_block(root, bytenr, blocksize, ptr_gen); |
| 4166 | btrfs_tree_lock(next); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4167 | btrfs_set_lock_blocking(next); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4168 | |
| 4169 | ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize, |
| 4170 | &refs); |
| 4171 | BUG_ON(ret); |
| 4172 | if (refs > 1) { |
| 4173 | parent = path->nodes[*level]; |
| 4174 | ret = btrfs_free_extent(trans, root, bytenr, |
| 4175 | blocksize, parent->start, |
| 4176 | btrfs_header_owner(parent), |
| 4177 | btrfs_header_generation(parent), |
| 4178 | *level - 1, 1); |
| 4179 | BUG_ON(ret); |
| 4180 | path->slots[*level]++; |
| 4181 | btrfs_tree_unlock(next); |
| 4182 | free_extent_buffer(next); |
| 4183 | continue; |
| 4184 | } |
| 4185 | |
| 4186 | *level = btrfs_header_level(next); |
| 4187 | path->nodes[*level] = next; |
| 4188 | path->slots[*level] = 0; |
| 4189 | path->locks[*level] = 1; |
| 4190 | cond_resched(); |
| 4191 | } |
| 4192 | out: |
| 4193 | parent = path->nodes[*level + 1]; |
| 4194 | bytenr = path->nodes[*level]->start; |
| 4195 | blocksize = path->nodes[*level]->len; |
| 4196 | |
| 4197 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, |
| 4198 | parent->start, btrfs_header_owner(parent), |
| 4199 | btrfs_header_generation(parent), *level, 1); |
| 4200 | BUG_ON(ret); |
| 4201 | |
| 4202 | if (path->locks[*level]) { |
| 4203 | btrfs_tree_unlock(path->nodes[*level]); |
| 4204 | path->locks[*level] = 0; |
| 4205 | } |
| 4206 | free_extent_buffer(path->nodes[*level]); |
| 4207 | path->nodes[*level] = NULL; |
| 4208 | *level += 1; |
| 4209 | cond_resched(); |
| 4210 | return 0; |
| 4211 | } |
| 4212 | |
| 4213 | /* |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4214 | * helper for dropping snapshots. This walks back up the tree in the path |
| 4215 | * to find the first node higher up where we haven't yet gone through |
| 4216 | * all the slots |
| 4217 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4218 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4219 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4220 | struct btrfs_path *path, |
| 4221 | int *level, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4222 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4223 | u64 root_owner; |
| 4224 | u64 root_gen; |
| 4225 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4226 | int i; |
| 4227 | int slot; |
| 4228 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4229 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4230 | for (i = *level; i < max_level && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4231 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4232 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 4233 | struct extent_buffer *node; |
| 4234 | struct btrfs_disk_key disk_key; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4235 | |
| 4236 | /* |
| 4237 | * there is more work to do in this level. |
| 4238 | * Update the drop_progress marker to reflect |
| 4239 | * the work we've done so far, and then bump |
| 4240 | * the slot number |
| 4241 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4242 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4243 | path->slots[i]++; |
| 4244 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4245 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4246 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4247 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4248 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4249 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4250 | return 0; |
| 4251 | } else { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4252 | struct extent_buffer *parent; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 4253 | |
| 4254 | /* |
| 4255 | * this whole node is done, free our reference |
| 4256 | * on it and go up one level |
| 4257 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4258 | if (path->nodes[*level] == root->node) |
| 4259 | parent = path->nodes[*level]; |
| 4260 | else |
| 4261 | parent = path->nodes[*level + 1]; |
| 4262 | |
| 4263 | root_owner = btrfs_header_owner(parent); |
| 4264 | root_gen = btrfs_header_generation(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4265 | |
| 4266 | clean_tree_block(trans, root, path->nodes[*level]); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 4267 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4268 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4269 | path->nodes[*level]->len, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4270 | parent->start, root_owner, |
| 4271 | root_gen, *level, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 4272 | BUG_ON(ret); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4273 | if (path->locks[*level]) { |
| 4274 | btrfs_tree_unlock(path->nodes[*level]); |
| 4275 | path->locks[*level] = 0; |
| 4276 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4277 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 4278 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4279 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4280 | } |
| 4281 | } |
| 4282 | return 1; |
| 4283 | } |
| 4284 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4285 | /* |
| 4286 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 4287 | * the tree freeing any blocks that have a ref count of zero after being |
| 4288 | * decremented. |
| 4289 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 4290 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4291 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4292 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 4293 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4294 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4295 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4296 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4297 | int i; |
| 4298 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4299 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4300 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4301 | WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4302 | path = btrfs_alloc_path(); |
| 4303 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4304 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4305 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4306 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4307 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 4308 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 4309 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4310 | path->slots[level] = 0; |
| 4311 | } else { |
| 4312 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4313 | struct btrfs_disk_key found_key; |
| 4314 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 4315 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4316 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 4317 | level = root_item->drop_level; |
| 4318 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4319 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 4320 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4321 | ret = wret; |
| 4322 | goto out; |
| 4323 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4324 | node = path->nodes[level]; |
| 4325 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 4326 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 4327 | sizeof(found_key))); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 4328 | /* |
| 4329 | * unlock our path, this is safe because only this |
| 4330 | * function is allowed to delete this snapshot |
| 4331 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4332 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
| 4333 | if (path->nodes[i] && path->locks[i]) { |
| 4334 | path->locks[i] = 0; |
| 4335 | btrfs_tree_unlock(path->nodes[i]); |
| 4336 | } |
| 4337 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4338 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4339 | while (1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4340 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4341 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4342 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4343 | if (wret < 0) |
| 4344 | ret = wret; |
| 4345 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4346 | wret = walk_up_tree(trans, root, path, &level, |
| 4347 | BTRFS_MAX_LEVEL); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4348 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4349 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4350 | if (wret < 0) |
| 4351 | ret = wret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 4352 | if (trans->transaction->in_commit) { |
| 4353 | ret = -EAGAIN; |
| 4354 | break; |
| 4355 | } |
Chris Mason | 18e35e0a | 2008-08-01 13:11:41 -0400 | [diff] [blame] | 4356 | atomic_inc(&root->fs_info->throttle_gen); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 4357 | wake_up(&root->fs_info->transaction_throttle); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4358 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 4359 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4360 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4361 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 4362 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 4363 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4364 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 4365 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4366 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 4367 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 4368 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4369 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4370 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 4371 | struct btrfs_root *root, |
| 4372 | struct extent_buffer *node, |
| 4373 | struct extent_buffer *parent) |
| 4374 | { |
| 4375 | struct btrfs_path *path; |
| 4376 | int level; |
| 4377 | int parent_level; |
| 4378 | int ret = 0; |
| 4379 | int wret; |
| 4380 | |
| 4381 | path = btrfs_alloc_path(); |
| 4382 | BUG_ON(!path); |
| 4383 | |
| 4384 | BUG_ON(!btrfs_tree_locked(parent)); |
| 4385 | parent_level = btrfs_header_level(parent); |
| 4386 | extent_buffer_get(parent); |
| 4387 | path->nodes[parent_level] = parent; |
| 4388 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 4389 | |
| 4390 | BUG_ON(!btrfs_tree_locked(node)); |
| 4391 | level = btrfs_header_level(node); |
| 4392 | extent_buffer_get(node); |
| 4393 | path->nodes[level] = node; |
| 4394 | path->slots[level] = 0; |
| 4395 | |
| 4396 | while (1) { |
| 4397 | wret = walk_down_subtree(trans, root, path, &level); |
| 4398 | if (wret < 0) |
| 4399 | ret = wret; |
| 4400 | if (wret != 0) |
| 4401 | break; |
| 4402 | |
| 4403 | wret = walk_up_tree(trans, root, path, &level, parent_level); |
| 4404 | if (wret < 0) |
| 4405 | ret = wret; |
| 4406 | if (wret != 0) |
| 4407 | break; |
| 4408 | } |
| 4409 | |
| 4410 | btrfs_free_path(path); |
| 4411 | return ret; |
| 4412 | } |
| 4413 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 4414 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 4415 | unsigned long nr) |
| 4416 | { |
| 4417 | return min(last, start + nr - 1); |
| 4418 | } |
| 4419 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4420 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4421 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4422 | { |
| 4423 | u64 page_start; |
| 4424 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4425 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4426 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4427 | unsigned long i; |
| 4428 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4429 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4430 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 4431 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4432 | unsigned int total_read = 0; |
| 4433 | unsigned int total_dirty = 0; |
| 4434 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4435 | |
| 4436 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4437 | |
| 4438 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4439 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4440 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 4441 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4442 | /* make sure the dirty trick played by the caller work */ |
| 4443 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 4444 | first_index, last_index); |
| 4445 | if (ret) |
| 4446 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 4447 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4448 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4449 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4450 | for (i = first_index ; i <= last_index; i++) { |
| 4451 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 4452 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4453 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 4454 | } |
| 4455 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 4456 | again: |
| 4457 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4458 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4459 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4460 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4461 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4462 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4463 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4464 | if (!PageUptodate(page)) { |
| 4465 | btrfs_readpage(NULL, page); |
| 4466 | lock_page(page); |
| 4467 | if (!PageUptodate(page)) { |
| 4468 | unlock_page(page); |
| 4469 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4470 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4471 | goto out_unlock; |
| 4472 | } |
| 4473 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4474 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 4475 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4476 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 4477 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4478 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4479 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 4480 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 4481 | if (ordered) { |
| 4482 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 4483 | unlock_page(page); |
| 4484 | page_cache_release(page); |
| 4485 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 4486 | btrfs_put_ordered_extent(ordered); |
| 4487 | goto again; |
| 4488 | } |
| 4489 | set_page_extent_mapped(page); |
| 4490 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4491 | if (i == first_index) |
| 4492 | set_extent_bits(io_tree, page_start, page_end, |
| 4493 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 4494 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4495 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4496 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4497 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4498 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 4499 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4500 | unlock_page(page); |
| 4501 | page_cache_release(page); |
| 4502 | } |
| 4503 | |
| 4504 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 4505 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4506 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4507 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 4508 | return ret; |
| 4509 | } |
| 4510 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4511 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4512 | struct btrfs_key *extent_key, |
| 4513 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4514 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4515 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4516 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 4517 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4518 | u64 start = extent_key->objectid - offset; |
| 4519 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4520 | |
| 4521 | em = alloc_extent_map(GFP_NOFS); |
| 4522 | BUG_ON(!em || IS_ERR(em)); |
| 4523 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4524 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4525 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4526 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4527 | em->block_start = extent_key->objectid; |
| 4528 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 4529 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 4530 | |
| 4531 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4532 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4533 | while (1) { |
| 4534 | int ret; |
| 4535 | spin_lock(&em_tree->lock); |
| 4536 | ret = add_extent_mapping(em_tree, em); |
| 4537 | spin_unlock(&em_tree->lock); |
| 4538 | if (ret != -EEXIST) { |
| 4539 | free_extent_map(em); |
| 4540 | break; |
| 4541 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4542 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4543 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4544 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4545 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 4546 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | struct btrfs_ref_path { |
| 4550 | u64 extent_start; |
| 4551 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 4552 | u64 root_objectid; |
| 4553 | u64 root_generation; |
| 4554 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4555 | u32 num_refs; |
| 4556 | int lowest_level; |
| 4557 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4558 | int shared_level; |
| 4559 | |
| 4560 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 4561 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4562 | }; |
| 4563 | |
| 4564 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4565 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4566 | u64 disk_bytenr; |
| 4567 | u64 disk_num_bytes; |
| 4568 | u64 offset; |
| 4569 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4570 | u8 compression; |
| 4571 | u8 encryption; |
| 4572 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4573 | }; |
| 4574 | |
| 4575 | static int is_cowonly_root(u64 root_objectid) |
| 4576 | { |
| 4577 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 4578 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 4579 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 4580 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 4581 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 4582 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4583 | return 1; |
| 4584 | return 0; |
| 4585 | } |
| 4586 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4587 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4588 | struct btrfs_root *extent_root, |
| 4589 | struct btrfs_ref_path *ref_path, |
| 4590 | int first_time) |
| 4591 | { |
| 4592 | struct extent_buffer *leaf; |
| 4593 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 4594 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4595 | struct btrfs_key key; |
| 4596 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4597 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4598 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4599 | int level; |
| 4600 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4601 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4602 | path = btrfs_alloc_path(); |
| 4603 | if (!path) |
| 4604 | return -ENOMEM; |
| 4605 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4606 | if (first_time) { |
| 4607 | ref_path->lowest_level = -1; |
| 4608 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4609 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4610 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4611 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4612 | walk_down: |
| 4613 | level = ref_path->current_level - 1; |
| 4614 | while (level >= -1) { |
| 4615 | u64 parent; |
| 4616 | if (level < ref_path->lowest_level) |
| 4617 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4618 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4619 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4620 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4621 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4622 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4623 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4624 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4625 | parent = ref_path->nodes[level + 1]; |
| 4626 | ref_path->nodes[level + 1] = 0; |
| 4627 | ref_path->current_level = level; |
| 4628 | BUG_ON(parent == 0); |
| 4629 | |
| 4630 | key.objectid = bytenr; |
| 4631 | key.offset = parent + 1; |
| 4632 | key.type = BTRFS_EXTENT_REF_KEY; |
| 4633 | |
| 4634 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4635 | if (ret < 0) |
| 4636 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4637 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4638 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4639 | leaf = path->nodes[0]; |
| 4640 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4641 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4642 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4643 | if (ret < 0) |
| 4644 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4645 | if (ret > 0) |
| 4646 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 4647 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4648 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4649 | |
| 4650 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4651 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4652 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 4653 | if (level < ref_path->shared_level) |
| 4654 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4655 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4656 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4657 | next: |
| 4658 | level--; |
| 4659 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4660 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4661 | } |
| 4662 | /* reached lowest level */ |
| 4663 | ret = 1; |
| 4664 | goto out; |
| 4665 | walk_up: |
| 4666 | level = ref_path->current_level; |
| 4667 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 4668 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4669 | |
| 4670 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4671 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4672 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4673 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4674 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4675 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4676 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4677 | key.objectid = bytenr; |
| 4678 | key.offset = 0; |
| 4679 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4680 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4681 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 4682 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4683 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4684 | |
| 4685 | leaf = path->nodes[0]; |
| 4686 | nritems = btrfs_header_nritems(leaf); |
| 4687 | if (path->slots[0] >= nritems) { |
| 4688 | ret = btrfs_next_leaf(extent_root, path); |
| 4689 | if (ret < 0) |
| 4690 | goto out; |
| 4691 | if (ret > 0) { |
| 4692 | /* the extent was freed by someone */ |
| 4693 | if (ref_path->lowest_level == level) |
| 4694 | goto out; |
| 4695 | btrfs_release_path(extent_root, path); |
| 4696 | goto walk_down; |
| 4697 | } |
| 4698 | leaf = path->nodes[0]; |
| 4699 | } |
| 4700 | |
| 4701 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4702 | if (found_key.objectid != bytenr || |
| 4703 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 4704 | /* the extent was freed by someone */ |
| 4705 | if (ref_path->lowest_level == level) { |
| 4706 | ret = 1; |
| 4707 | goto out; |
| 4708 | } |
| 4709 | btrfs_release_path(extent_root, path); |
| 4710 | goto walk_down; |
| 4711 | } |
| 4712 | found: |
| 4713 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 4714 | struct btrfs_extent_ref); |
| 4715 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 4716 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4717 | if (first_time) { |
| 4718 | level = (int)ref_objectid; |
| 4719 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 4720 | ref_path->lowest_level = level; |
| 4721 | ref_path->current_level = level; |
| 4722 | ref_path->nodes[level] = bytenr; |
| 4723 | } else { |
| 4724 | WARN_ON(ref_objectid != level); |
| 4725 | } |
| 4726 | } else { |
| 4727 | WARN_ON(level != -1); |
| 4728 | } |
| 4729 | first_time = 0; |
| 4730 | |
| 4731 | if (ref_path->lowest_level == level) { |
| 4732 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4733 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 4734 | } |
| 4735 | |
| 4736 | /* |
| 4737 | * the block is tree root or the block isn't in reference |
| 4738 | * counted tree. |
| 4739 | */ |
| 4740 | if (found_key.objectid == found_key.offset || |
| 4741 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 4742 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4743 | ref_path->root_generation = |
| 4744 | btrfs_ref_generation(leaf, ref); |
| 4745 | if (level < 0) { |
| 4746 | /* special reference from the tree log */ |
| 4747 | ref_path->nodes[0] = found_key.offset; |
| 4748 | ref_path->current_level = 0; |
| 4749 | } |
| 4750 | ret = 0; |
| 4751 | goto out; |
| 4752 | } |
| 4753 | |
| 4754 | level++; |
| 4755 | BUG_ON(ref_path->nodes[level] != 0); |
| 4756 | ref_path->nodes[level] = found_key.offset; |
| 4757 | ref_path->current_level = level; |
| 4758 | |
| 4759 | /* |
| 4760 | * the reference was created in the running transaction, |
| 4761 | * no need to continue walking up. |
| 4762 | */ |
| 4763 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 4764 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 4765 | ref_path->root_generation = |
| 4766 | btrfs_ref_generation(leaf, ref); |
| 4767 | ret = 0; |
| 4768 | goto out; |
| 4769 | } |
| 4770 | |
| 4771 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4772 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4773 | } |
| 4774 | /* reached max tree level, but no tree root found. */ |
| 4775 | BUG(); |
| 4776 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4777 | btrfs_free_path(path); |
| 4778 | return ret; |
| 4779 | } |
| 4780 | |
| 4781 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 4782 | struct btrfs_root *extent_root, |
| 4783 | struct btrfs_ref_path *ref_path, |
| 4784 | u64 extent_start) |
| 4785 | { |
| 4786 | memset(ref_path, 0, sizeof(*ref_path)); |
| 4787 | ref_path->extent_start = extent_start; |
| 4788 | |
| 4789 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 4790 | } |
| 4791 | |
| 4792 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 4793 | struct btrfs_root *extent_root, |
| 4794 | struct btrfs_ref_path *ref_path) |
| 4795 | { |
| 4796 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 4797 | } |
| 4798 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4799 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4800 | struct btrfs_key *extent_key, |
| 4801 | u64 offset, int no_fragment, |
| 4802 | struct disk_extent **extents, |
| 4803 | int *nr_extents) |
| 4804 | { |
| 4805 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 4806 | struct btrfs_path *path; |
| 4807 | struct btrfs_file_extent_item *fi; |
| 4808 | struct extent_buffer *leaf; |
| 4809 | struct disk_extent *exts = *extents; |
| 4810 | struct btrfs_key found_key; |
| 4811 | u64 cur_pos; |
| 4812 | u64 last_byte; |
| 4813 | u32 nritems; |
| 4814 | int nr = 0; |
| 4815 | int max = *nr_extents; |
| 4816 | int ret; |
| 4817 | |
| 4818 | WARN_ON(!no_fragment && *extents); |
| 4819 | if (!exts) { |
| 4820 | max = 1; |
| 4821 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4822 | if (!exts) |
| 4823 | return -ENOMEM; |
| 4824 | } |
| 4825 | |
| 4826 | path = btrfs_alloc_path(); |
| 4827 | BUG_ON(!path); |
| 4828 | |
| 4829 | cur_pos = extent_key->objectid - offset; |
| 4830 | last_byte = extent_key->objectid + extent_key->offset; |
| 4831 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 4832 | cur_pos, 0); |
| 4833 | if (ret < 0) |
| 4834 | goto out; |
| 4835 | if (ret > 0) { |
| 4836 | ret = -ENOENT; |
| 4837 | goto out; |
| 4838 | } |
| 4839 | |
| 4840 | while (1) { |
| 4841 | leaf = path->nodes[0]; |
| 4842 | nritems = btrfs_header_nritems(leaf); |
| 4843 | if (path->slots[0] >= nritems) { |
| 4844 | ret = btrfs_next_leaf(root, path); |
| 4845 | if (ret < 0) |
| 4846 | goto out; |
| 4847 | if (ret > 0) |
| 4848 | break; |
| 4849 | leaf = path->nodes[0]; |
| 4850 | } |
| 4851 | |
| 4852 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 4853 | if (found_key.offset != cur_pos || |
| 4854 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 4855 | found_key.objectid != reloc_inode->i_ino) |
| 4856 | break; |
| 4857 | |
| 4858 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4859 | struct btrfs_file_extent_item); |
| 4860 | if (btrfs_file_extent_type(leaf, fi) != |
| 4861 | BTRFS_FILE_EXTENT_REG || |
| 4862 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 4863 | break; |
| 4864 | |
| 4865 | if (nr == max) { |
| 4866 | struct disk_extent *old = exts; |
| 4867 | max *= 2; |
| 4868 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
| 4869 | memcpy(exts, old, sizeof(*exts) * nr); |
| 4870 | if (old != *extents) |
| 4871 | kfree(old); |
| 4872 | } |
| 4873 | |
| 4874 | exts[nr].disk_bytenr = |
| 4875 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 4876 | exts[nr].disk_num_bytes = |
| 4877 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 4878 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 4879 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 4880 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 4881 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 4882 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 4883 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 4884 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4885 | BUG_ON(exts[nr].offset > 0); |
| 4886 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 4887 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4888 | |
| 4889 | cur_pos += exts[nr].num_bytes; |
| 4890 | nr++; |
| 4891 | |
| 4892 | if (cur_pos + offset >= last_byte) |
| 4893 | break; |
| 4894 | |
| 4895 | if (no_fragment) { |
| 4896 | ret = 1; |
| 4897 | goto out; |
| 4898 | } |
| 4899 | path->slots[0]++; |
| 4900 | } |
| 4901 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 4902 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4903 | if (cur_pos + offset < last_byte) { |
| 4904 | ret = -ENOENT; |
| 4905 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 4906 | } |
| 4907 | ret = 0; |
| 4908 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4909 | btrfs_free_path(path); |
| 4910 | if (ret) { |
| 4911 | if (exts != *extents) |
| 4912 | kfree(exts); |
| 4913 | } else { |
| 4914 | *extents = exts; |
| 4915 | *nr_extents = nr; |
| 4916 | } |
| 4917 | return ret; |
| 4918 | } |
| 4919 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4920 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4921 | struct btrfs_root *root, |
| 4922 | struct btrfs_path *path, |
| 4923 | struct btrfs_key *extent_key, |
| 4924 | struct btrfs_key *leaf_key, |
| 4925 | struct btrfs_ref_path *ref_path, |
| 4926 | struct disk_extent *new_extents, |
| 4927 | int nr_extents) |
| 4928 | { |
| 4929 | struct extent_buffer *leaf; |
| 4930 | struct btrfs_file_extent_item *fi; |
| 4931 | struct inode *inode = NULL; |
| 4932 | struct btrfs_key key; |
| 4933 | u64 lock_start = 0; |
| 4934 | u64 lock_end = 0; |
| 4935 | u64 num_bytes; |
| 4936 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4937 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4938 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4939 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4940 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 4941 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4942 | int ret; |
| 4943 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4944 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4945 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4946 | if (key.objectid < ref_path->owner_objectid || |
| 4947 | (key.objectid == ref_path->owner_objectid && |
| 4948 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 4949 | key.objectid = ref_path->owner_objectid; |
| 4950 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4951 | key.offset = 0; |
| 4952 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | while (1) { |
| 4956 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 4957 | if (ret < 0) |
| 4958 | goto out; |
| 4959 | |
| 4960 | leaf = path->nodes[0]; |
| 4961 | nritems = btrfs_header_nritems(leaf); |
| 4962 | next: |
| 4963 | if (extent_locked && ret > 0) { |
| 4964 | /* |
| 4965 | * the file extent item was modified by someone |
| 4966 | * before the extent got locked. |
| 4967 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4968 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 4969 | lock_end, GFP_NOFS); |
| 4970 | extent_locked = 0; |
| 4971 | } |
| 4972 | |
| 4973 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 4974 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4975 | break; |
| 4976 | |
| 4977 | BUG_ON(extent_locked); |
| 4978 | ret = btrfs_next_leaf(root, path); |
| 4979 | if (ret < 0) |
| 4980 | goto out; |
| 4981 | if (ret > 0) |
| 4982 | break; |
| 4983 | leaf = path->nodes[0]; |
| 4984 | nritems = btrfs_header_nritems(leaf); |
| 4985 | } |
| 4986 | |
| 4987 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4988 | |
| 4989 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 4990 | if ((key.objectid > ref_path->owner_objectid) || |
| 4991 | (key.objectid == ref_path->owner_objectid && |
| 4992 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 4993 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 4994 | break; |
| 4995 | } |
| 4996 | |
| 4997 | if (inode && key.objectid != inode->i_ino) { |
| 4998 | BUG_ON(extent_locked); |
| 4999 | btrfs_release_path(root, path); |
| 5000 | mutex_unlock(&inode->i_mutex); |
| 5001 | iput(inode); |
| 5002 | inode = NULL; |
| 5003 | continue; |
| 5004 | } |
| 5005 | |
| 5006 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 5007 | path->slots[0]++; |
| 5008 | ret = 1; |
| 5009 | goto next; |
| 5010 | } |
| 5011 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 5012 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5013 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 5014 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 5015 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5016 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 5017 | extent_key->objectid)) { |
| 5018 | path->slots[0]++; |
| 5019 | ret = 1; |
| 5020 | goto next; |
| 5021 | } |
| 5022 | |
| 5023 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 5024 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 5025 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5026 | if (search_end == (u64)-1) { |
| 5027 | search_end = key.offset - ext_offset + |
| 5028 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 5029 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5030 | |
| 5031 | if (!extent_locked) { |
| 5032 | lock_start = key.offset; |
| 5033 | lock_end = lock_start + num_bytes - 1; |
| 5034 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 5035 | if (lock_start > key.offset || |
| 5036 | lock_end + 1 < key.offset + num_bytes) { |
| 5037 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 5038 | lock_start, lock_end, GFP_NOFS); |
| 5039 | extent_locked = 0; |
| 5040 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | if (!inode) { |
| 5044 | btrfs_release_path(root, path); |
| 5045 | |
| 5046 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 5047 | key.objectid, root); |
| 5048 | if (inode->i_state & I_NEW) { |
| 5049 | BTRFS_I(inode)->root = root; |
| 5050 | BTRFS_I(inode)->location.objectid = |
| 5051 | key.objectid; |
| 5052 | BTRFS_I(inode)->location.type = |
| 5053 | BTRFS_INODE_ITEM_KEY; |
| 5054 | BTRFS_I(inode)->location.offset = 0; |
| 5055 | btrfs_read_locked_inode(inode); |
| 5056 | unlock_new_inode(inode); |
| 5057 | } |
| 5058 | /* |
| 5059 | * some code call btrfs_commit_transaction while |
| 5060 | * holding the i_mutex, so we can't use mutex_lock |
| 5061 | * here. |
| 5062 | */ |
| 5063 | if (is_bad_inode(inode) || |
| 5064 | !mutex_trylock(&inode->i_mutex)) { |
| 5065 | iput(inode); |
| 5066 | inode = NULL; |
| 5067 | key.offset = (u64)-1; |
| 5068 | goto skip; |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | if (!extent_locked) { |
| 5073 | struct btrfs_ordered_extent *ordered; |
| 5074 | |
| 5075 | btrfs_release_path(root, path); |
| 5076 | |
| 5077 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 5078 | lock_end, GFP_NOFS); |
| 5079 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 5080 | lock_end); |
| 5081 | if (ordered && |
| 5082 | ordered->file_offset <= lock_end && |
| 5083 | ordered->file_offset + ordered->len > lock_start) { |
| 5084 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 5085 | lock_start, lock_end, GFP_NOFS); |
| 5086 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 5087 | btrfs_put_ordered_extent(ordered); |
| 5088 | key.offset += num_bytes; |
| 5089 | goto skip; |
| 5090 | } |
| 5091 | if (ordered) |
| 5092 | btrfs_put_ordered_extent(ordered); |
| 5093 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5094 | extent_locked = 1; |
| 5095 | continue; |
| 5096 | } |
| 5097 | |
| 5098 | if (nr_extents == 1) { |
| 5099 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5100 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 5101 | new_extents[0].disk_bytenr); |
| 5102 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 5103 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5104 | btrfs_mark_buffer_dirty(leaf); |
| 5105 | |
| 5106 | btrfs_drop_extent_cache(inode, key.offset, |
| 5107 | key.offset + num_bytes - 1, 0); |
| 5108 | |
| 5109 | ret = btrfs_inc_extent_ref(trans, root, |
| 5110 | new_extents[0].disk_bytenr, |
| 5111 | new_extents[0].disk_num_bytes, |
| 5112 | leaf->start, |
| 5113 | root->root_key.objectid, |
| 5114 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5115 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5116 | BUG_ON(ret); |
| 5117 | |
| 5118 | ret = btrfs_free_extent(trans, root, |
| 5119 | extent_key->objectid, |
| 5120 | extent_key->offset, |
| 5121 | leaf->start, |
| 5122 | btrfs_header_owner(leaf), |
| 5123 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5124 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5125 | BUG_ON(ret); |
| 5126 | |
| 5127 | btrfs_release_path(root, path); |
| 5128 | key.offset += num_bytes; |
| 5129 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5130 | BUG_ON(1); |
| 5131 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5132 | u64 alloc_hint; |
| 5133 | u64 extent_len; |
| 5134 | int i; |
| 5135 | /* |
| 5136 | * drop old extent pointer at first, then insert the |
| 5137 | * new pointers one bye one |
| 5138 | */ |
| 5139 | btrfs_release_path(root, path); |
| 5140 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 5141 | key.offset + num_bytes, |
| 5142 | key.offset, &alloc_hint); |
| 5143 | BUG_ON(ret); |
| 5144 | |
| 5145 | for (i = 0; i < nr_extents; i++) { |
| 5146 | if (ext_offset >= new_extents[i].num_bytes) { |
| 5147 | ext_offset -= new_extents[i].num_bytes; |
| 5148 | continue; |
| 5149 | } |
| 5150 | extent_len = min(new_extents[i].num_bytes - |
| 5151 | ext_offset, num_bytes); |
| 5152 | |
| 5153 | ret = btrfs_insert_empty_item(trans, root, |
| 5154 | path, &key, |
| 5155 | sizeof(*fi)); |
| 5156 | BUG_ON(ret); |
| 5157 | |
| 5158 | leaf = path->nodes[0]; |
| 5159 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 5160 | struct btrfs_file_extent_item); |
| 5161 | btrfs_set_file_extent_generation(leaf, fi, |
| 5162 | trans->transid); |
| 5163 | btrfs_set_file_extent_type(leaf, fi, |
| 5164 | BTRFS_FILE_EXTENT_REG); |
| 5165 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 5166 | new_extents[i].disk_bytenr); |
| 5167 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 5168 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 5169 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 5170 | new_extents[i].ram_bytes); |
| 5171 | |
| 5172 | btrfs_set_file_extent_compression(leaf, fi, |
| 5173 | new_extents[i].compression); |
| 5174 | btrfs_set_file_extent_encryption(leaf, fi, |
| 5175 | new_extents[i].encryption); |
| 5176 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 5177 | new_extents[i].other_encoding); |
| 5178 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5179 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 5180 | extent_len); |
| 5181 | ext_offset += new_extents[i].offset; |
| 5182 | btrfs_set_file_extent_offset(leaf, fi, |
| 5183 | ext_offset); |
| 5184 | btrfs_mark_buffer_dirty(leaf); |
| 5185 | |
| 5186 | btrfs_drop_extent_cache(inode, key.offset, |
| 5187 | key.offset + extent_len - 1, 0); |
| 5188 | |
| 5189 | ret = btrfs_inc_extent_ref(trans, root, |
| 5190 | new_extents[i].disk_bytenr, |
| 5191 | new_extents[i].disk_num_bytes, |
| 5192 | leaf->start, |
| 5193 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5194 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5195 | BUG_ON(ret); |
| 5196 | btrfs_release_path(root, path); |
| 5197 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 5198 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5199 | |
| 5200 | ext_offset = 0; |
| 5201 | num_bytes -= extent_len; |
| 5202 | key.offset += extent_len; |
| 5203 | |
| 5204 | if (num_bytes == 0) |
| 5205 | break; |
| 5206 | } |
| 5207 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5208 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5212 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 5213 | lock_end, GFP_NOFS); |
| 5214 | extent_locked = 0; |
| 5215 | } |
| 5216 | skip: |
| 5217 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 5218 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5219 | break; |
| 5220 | |
| 5221 | cond_resched(); |
| 5222 | } |
| 5223 | ret = 0; |
| 5224 | out: |
| 5225 | btrfs_release_path(root, path); |
| 5226 | if (inode) { |
| 5227 | mutex_unlock(&inode->i_mutex); |
| 5228 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5229 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 5230 | lock_end, GFP_NOFS); |
| 5231 | } |
| 5232 | iput(inode); |
| 5233 | } |
| 5234 | return ret; |
| 5235 | } |
| 5236 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5237 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 5238 | struct btrfs_root *root, |
| 5239 | struct extent_buffer *buf, u64 orig_start) |
| 5240 | { |
| 5241 | int level; |
| 5242 | int ret; |
| 5243 | |
| 5244 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 5245 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 5246 | |
| 5247 | level = btrfs_header_level(buf); |
| 5248 | if (level == 0) { |
| 5249 | struct btrfs_leaf_ref *ref; |
| 5250 | struct btrfs_leaf_ref *orig_ref; |
| 5251 | |
| 5252 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 5253 | if (!orig_ref) |
| 5254 | return -ENOENT; |
| 5255 | |
| 5256 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 5257 | if (!ref) { |
| 5258 | btrfs_free_leaf_ref(root, orig_ref); |
| 5259 | return -ENOMEM; |
| 5260 | } |
| 5261 | |
| 5262 | ref->nritems = orig_ref->nritems; |
| 5263 | memcpy(ref->extents, orig_ref->extents, |
| 5264 | sizeof(ref->extents[0]) * ref->nritems); |
| 5265 | |
| 5266 | btrfs_free_leaf_ref(root, orig_ref); |
| 5267 | |
| 5268 | ref->root_gen = trans->transid; |
| 5269 | ref->bytenr = buf->start; |
| 5270 | ref->owner = btrfs_header_owner(buf); |
| 5271 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 5272 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5273 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 5274 | WARN_ON(ret); |
| 5275 | btrfs_free_leaf_ref(root, ref); |
| 5276 | } |
| 5277 | return 0; |
| 5278 | } |
| 5279 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5280 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5281 | struct extent_buffer *leaf, |
| 5282 | struct btrfs_block_group_cache *group, |
| 5283 | struct btrfs_root *target_root) |
| 5284 | { |
| 5285 | struct btrfs_key key; |
| 5286 | struct inode *inode = NULL; |
| 5287 | struct btrfs_file_extent_item *fi; |
| 5288 | u64 num_bytes; |
| 5289 | u64 skip_objectid = 0; |
| 5290 | u32 nritems; |
| 5291 | u32 i; |
| 5292 | |
| 5293 | nritems = btrfs_header_nritems(leaf); |
| 5294 | for (i = 0; i < nritems; i++) { |
| 5295 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 5296 | if (key.objectid == skip_objectid || |
| 5297 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 5298 | continue; |
| 5299 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 5300 | if (btrfs_file_extent_type(leaf, fi) == |
| 5301 | BTRFS_FILE_EXTENT_INLINE) |
| 5302 | continue; |
| 5303 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 5304 | continue; |
| 5305 | if (!inode || inode->i_ino != key.objectid) { |
| 5306 | iput(inode); |
| 5307 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 5308 | key.objectid, target_root, 1); |
| 5309 | } |
| 5310 | if (!inode) { |
| 5311 | skip_objectid = key.objectid; |
| 5312 | continue; |
| 5313 | } |
| 5314 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 5315 | |
| 5316 | lock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 5317 | key.offset + num_bytes - 1, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5318 | btrfs_drop_extent_cache(inode, key.offset, |
| 5319 | key.offset + num_bytes - 1, 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5320 | unlock_extent(&BTRFS_I(inode)->io_tree, key.offset, |
| 5321 | key.offset + num_bytes - 1, GFP_NOFS); |
| 5322 | cond_resched(); |
| 5323 | } |
| 5324 | iput(inode); |
| 5325 | return 0; |
| 5326 | } |
| 5327 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5328 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5329 | struct btrfs_root *root, |
| 5330 | struct extent_buffer *leaf, |
| 5331 | struct btrfs_block_group_cache *group, |
| 5332 | struct inode *reloc_inode) |
| 5333 | { |
| 5334 | struct btrfs_key key; |
| 5335 | struct btrfs_key extent_key; |
| 5336 | struct btrfs_file_extent_item *fi; |
| 5337 | struct btrfs_leaf_ref *ref; |
| 5338 | struct disk_extent *new_extent; |
| 5339 | u64 bytenr; |
| 5340 | u64 num_bytes; |
| 5341 | u32 nritems; |
| 5342 | u32 i; |
| 5343 | int ext_index; |
| 5344 | int nr_extent; |
| 5345 | int ret; |
| 5346 | |
| 5347 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
| 5348 | BUG_ON(!new_extent); |
| 5349 | |
| 5350 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 5351 | BUG_ON(!ref); |
| 5352 | |
| 5353 | ext_index = -1; |
| 5354 | nritems = btrfs_header_nritems(leaf); |
| 5355 | for (i = 0; i < nritems; i++) { |
| 5356 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 5357 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 5358 | continue; |
| 5359 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 5360 | if (btrfs_file_extent_type(leaf, fi) == |
| 5361 | BTRFS_FILE_EXTENT_INLINE) |
| 5362 | continue; |
| 5363 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 5364 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 5365 | if (bytenr == 0) |
| 5366 | continue; |
| 5367 | |
| 5368 | ext_index++; |
| 5369 | if (bytenr >= group->key.objectid + group->key.offset || |
| 5370 | bytenr + num_bytes <= group->key.objectid) |
| 5371 | continue; |
| 5372 | |
| 5373 | extent_key.objectid = bytenr; |
| 5374 | extent_key.offset = num_bytes; |
| 5375 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5376 | nr_extent = 1; |
| 5377 | ret = get_new_locations(reloc_inode, &extent_key, |
| 5378 | group->key.objectid, 1, |
| 5379 | &new_extent, &nr_extent); |
| 5380 | if (ret > 0) |
| 5381 | continue; |
| 5382 | BUG_ON(ret < 0); |
| 5383 | |
| 5384 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 5385 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 5386 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 5387 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 5388 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5389 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 5390 | new_extent->disk_bytenr); |
| 5391 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 5392 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5393 | btrfs_mark_buffer_dirty(leaf); |
| 5394 | |
| 5395 | ret = btrfs_inc_extent_ref(trans, root, |
| 5396 | new_extent->disk_bytenr, |
| 5397 | new_extent->disk_num_bytes, |
| 5398 | leaf->start, |
| 5399 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5400 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5401 | BUG_ON(ret); |
| 5402 | ret = btrfs_free_extent(trans, root, |
| 5403 | bytenr, num_bytes, leaf->start, |
| 5404 | btrfs_header_owner(leaf), |
| 5405 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 5406 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5407 | BUG_ON(ret); |
| 5408 | cond_resched(); |
| 5409 | } |
| 5410 | kfree(new_extent); |
| 5411 | BUG_ON(ext_index + 1 != ref->nritems); |
| 5412 | btrfs_free_leaf_ref(root, ref); |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5416 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 5417 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5418 | { |
| 5419 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5420 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5421 | |
| 5422 | if (root->reloc_root) { |
| 5423 | reloc_root = root->reloc_root; |
| 5424 | root->reloc_root = NULL; |
| 5425 | list_add(&reloc_root->dead_list, |
| 5426 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5427 | |
| 5428 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 5429 | reloc_root->node->start); |
| 5430 | btrfs_set_root_level(&root->root_item, |
| 5431 | btrfs_header_level(reloc_root->node)); |
| 5432 | memset(&reloc_root->root_item.drop_progress, 0, |
| 5433 | sizeof(struct btrfs_disk_key)); |
| 5434 | reloc_root->root_item.drop_level = 0; |
| 5435 | |
| 5436 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 5437 | &reloc_root->root_key, |
| 5438 | &reloc_root->root_item); |
| 5439 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5440 | } |
| 5441 | return 0; |
| 5442 | } |
| 5443 | |
| 5444 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 5445 | { |
| 5446 | struct btrfs_trans_handle *trans; |
| 5447 | struct btrfs_root *reloc_root; |
| 5448 | struct btrfs_root *prev_root = NULL; |
| 5449 | struct list_head dead_roots; |
| 5450 | int ret; |
| 5451 | unsigned long nr; |
| 5452 | |
| 5453 | INIT_LIST_HEAD(&dead_roots); |
| 5454 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 5455 | |
| 5456 | while (!list_empty(&dead_roots)) { |
| 5457 | reloc_root = list_entry(dead_roots.prev, |
| 5458 | struct btrfs_root, dead_list); |
| 5459 | list_del_init(&reloc_root->dead_list); |
| 5460 | |
| 5461 | BUG_ON(reloc_root->commit_root != NULL); |
| 5462 | while (1) { |
| 5463 | trans = btrfs_join_transaction(root, 1); |
| 5464 | BUG_ON(!trans); |
| 5465 | |
| 5466 | mutex_lock(&root->fs_info->drop_mutex); |
| 5467 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 5468 | if (ret != -EAGAIN) |
| 5469 | break; |
| 5470 | mutex_unlock(&root->fs_info->drop_mutex); |
| 5471 | |
| 5472 | nr = trans->blocks_used; |
| 5473 | ret = btrfs_end_transaction(trans, root); |
| 5474 | BUG_ON(ret); |
| 5475 | btrfs_btree_balance_dirty(root, nr); |
| 5476 | } |
| 5477 | |
| 5478 | free_extent_buffer(reloc_root->node); |
| 5479 | |
| 5480 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 5481 | &reloc_root->root_key); |
| 5482 | BUG_ON(ret); |
| 5483 | mutex_unlock(&root->fs_info->drop_mutex); |
| 5484 | |
| 5485 | nr = trans->blocks_used; |
| 5486 | ret = btrfs_end_transaction(trans, root); |
| 5487 | BUG_ON(ret); |
| 5488 | btrfs_btree_balance_dirty(root, nr); |
| 5489 | |
| 5490 | kfree(prev_root); |
| 5491 | prev_root = reloc_root; |
| 5492 | } |
| 5493 | if (prev_root) { |
| 5494 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 5495 | kfree(prev_root); |
| 5496 | } |
| 5497 | return 0; |
| 5498 | } |
| 5499 | |
| 5500 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 5501 | { |
| 5502 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 5503 | return 0; |
| 5504 | } |
| 5505 | |
| 5506 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 5507 | { |
| 5508 | struct btrfs_root *reloc_root; |
| 5509 | struct btrfs_trans_handle *trans; |
| 5510 | struct btrfs_key location; |
| 5511 | int found; |
| 5512 | int ret; |
| 5513 | |
| 5514 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5515 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 5516 | BUG_ON(ret); |
| 5517 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 5518 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 5519 | |
| 5520 | if (found) { |
| 5521 | trans = btrfs_start_transaction(root, 1); |
| 5522 | BUG_ON(!trans); |
| 5523 | ret = btrfs_commit_transaction(trans, root); |
| 5524 | BUG_ON(ret); |
| 5525 | } |
| 5526 | |
| 5527 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 5528 | location.offset = (u64)-1; |
| 5529 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 5530 | |
| 5531 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 5532 | BUG_ON(!reloc_root); |
| 5533 | btrfs_orphan_cleanup(reloc_root); |
| 5534 | return 0; |
| 5535 | } |
| 5536 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5537 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5538 | struct btrfs_root *root) |
| 5539 | { |
| 5540 | struct btrfs_root *reloc_root; |
| 5541 | struct extent_buffer *eb; |
| 5542 | struct btrfs_root_item *root_item; |
| 5543 | struct btrfs_key root_key; |
| 5544 | int ret; |
| 5545 | |
| 5546 | BUG_ON(!root->ref_cows); |
| 5547 | if (root->reloc_root) |
| 5548 | return 0; |
| 5549 | |
| 5550 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
| 5551 | BUG_ON(!root_item); |
| 5552 | |
| 5553 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 5554 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 5555 | BUG_ON(ret); |
| 5556 | |
| 5557 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 5558 | root_key.offset = root->root_key.objectid; |
| 5559 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5560 | |
| 5561 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 5562 | btrfs_set_root_refs(root_item, 0); |
| 5563 | btrfs_set_root_bytenr(root_item, eb->start); |
| 5564 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5565 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5566 | |
| 5567 | btrfs_tree_unlock(eb); |
| 5568 | free_extent_buffer(eb); |
| 5569 | |
| 5570 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 5571 | &root_key, root_item); |
| 5572 | BUG_ON(ret); |
| 5573 | kfree(root_item); |
| 5574 | |
| 5575 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 5576 | &root_key); |
| 5577 | BUG_ON(!reloc_root); |
| 5578 | reloc_root->last_trans = trans->transid; |
| 5579 | reloc_root->commit_root = NULL; |
| 5580 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 5581 | |
| 5582 | root->reloc_root = reloc_root; |
| 5583 | return 0; |
| 5584 | } |
| 5585 | |
| 5586 | /* |
| 5587 | * Core function of space balance. |
| 5588 | * |
| 5589 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5590 | * counted roots. There is one reloc tree for each subvol, and all |
| 5591 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 5592 | * of the latest committed roots of subvols (root->commit_root). |
| 5593 | * |
| 5594 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 5595 | * COW the block through subvol's reloc tree, then update block pointer |
| 5596 | * in the subvol to point to the new block. Since all reloc trees share |
| 5597 | * same root key objectid, doing special handing for tree blocks owned |
| 5598 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 5599 | * we can use the resulting new block directly when the same block is |
| 5600 | * required to COW again through other reloc trees. By this way, relocated |
| 5601 | * tree blocks are shared between reloc trees, so they are also shared |
| 5602 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5603 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5604 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5605 | struct btrfs_root *root, |
| 5606 | struct btrfs_path *path, |
| 5607 | struct btrfs_key *first_key, |
| 5608 | struct btrfs_ref_path *ref_path, |
| 5609 | struct btrfs_block_group_cache *group, |
| 5610 | struct inode *reloc_inode) |
| 5611 | { |
| 5612 | struct btrfs_root *reloc_root; |
| 5613 | struct extent_buffer *eb = NULL; |
| 5614 | struct btrfs_key *keys; |
| 5615 | u64 *nodes; |
| 5616 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5617 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5618 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5619 | int ret; |
| 5620 | |
| 5621 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 5622 | lowest_level = ref_path->owner_objectid; |
| 5623 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5624 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5625 | path->lowest_level = lowest_level; |
| 5626 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 5627 | BUG_ON(ret < 0); |
| 5628 | path->lowest_level = 0; |
| 5629 | btrfs_release_path(root, path); |
| 5630 | return 0; |
| 5631 | } |
| 5632 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5633 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 5634 | ret = init_reloc_tree(trans, root); |
| 5635 | BUG_ON(ret); |
| 5636 | reloc_root = root->reloc_root; |
| 5637 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5638 | shared_level = ref_path->shared_level; |
| 5639 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5640 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5641 | keys = ref_path->node_keys; |
| 5642 | nodes = ref_path->new_nodes; |
| 5643 | memset(&keys[shared_level + 1], 0, |
| 5644 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 5645 | memset(&nodes[shared_level + 1], 0, |
| 5646 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5647 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5648 | if (nodes[lowest_level] == 0) { |
| 5649 | path->lowest_level = lowest_level; |
| 5650 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5651 | 0, 1); |
| 5652 | BUG_ON(ret); |
| 5653 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 5654 | eb = path->nodes[level]; |
| 5655 | if (!eb || eb == reloc_root->node) |
| 5656 | break; |
| 5657 | nodes[level] = eb->start; |
| 5658 | if (level == 0) |
| 5659 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 5660 | else |
| 5661 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 5662 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5663 | if (nodes[0] && |
| 5664 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5665 | eb = path->nodes[0]; |
| 5666 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 5667 | group, reloc_inode); |
| 5668 | BUG_ON(ret); |
| 5669 | } |
| 5670 | btrfs_release_path(reloc_root, path); |
| 5671 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5672 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5673 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5674 | BUG_ON(ret); |
| 5675 | } |
| 5676 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5677 | /* |
| 5678 | * replace tree blocks in the fs tree with tree blocks in |
| 5679 | * the reloc tree. |
| 5680 | */ |
| 5681 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 5682 | BUG_ON(ret < 0); |
| 5683 | |
| 5684 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5685 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 5686 | 0, 0); |
| 5687 | BUG_ON(ret); |
| 5688 | extent_buffer_get(path->nodes[0]); |
| 5689 | eb = path->nodes[0]; |
| 5690 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5691 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 5692 | BUG_ON(ret); |
| 5693 | free_extent_buffer(eb); |
| 5694 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5695 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5696 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5697 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5698 | return 0; |
| 5699 | } |
| 5700 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5701 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5702 | struct btrfs_root *root, |
| 5703 | struct btrfs_path *path, |
| 5704 | struct btrfs_key *first_key, |
| 5705 | struct btrfs_ref_path *ref_path) |
| 5706 | { |
| 5707 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5708 | |
| 5709 | ret = relocate_one_path(trans, root, path, first_key, |
| 5710 | ref_path, NULL, NULL); |
| 5711 | BUG_ON(ret); |
| 5712 | |
| 5713 | if (root == root->fs_info->extent_root) |
| 5714 | btrfs_extent_post_op(trans, root); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5715 | |
| 5716 | return 0; |
| 5717 | } |
| 5718 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5719 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5720 | struct btrfs_root *extent_root, |
| 5721 | struct btrfs_path *path, |
| 5722 | struct btrfs_key *extent_key) |
| 5723 | { |
| 5724 | int ret; |
| 5725 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5726 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 5727 | if (ret) |
| 5728 | goto out; |
| 5729 | ret = btrfs_del_item(trans, extent_root, path); |
| 5730 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5731 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5732 | return ret; |
| 5733 | } |
| 5734 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5735 | static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5736 | struct btrfs_ref_path *ref_path) |
| 5737 | { |
| 5738 | struct btrfs_key root_key; |
| 5739 | |
| 5740 | root_key.objectid = ref_path->root_objectid; |
| 5741 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 5742 | if (is_cowonly_root(ref_path->root_objectid)) |
| 5743 | root_key.offset = 0; |
| 5744 | else |
| 5745 | root_key.offset = (u64)-1; |
| 5746 | |
| 5747 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 5748 | } |
| 5749 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5750 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5751 | struct btrfs_path *path, |
| 5752 | struct btrfs_key *extent_key, |
| 5753 | struct btrfs_block_group_cache *group, |
| 5754 | struct inode *reloc_inode, int pass) |
| 5755 | { |
| 5756 | struct btrfs_trans_handle *trans; |
| 5757 | struct btrfs_root *found_root; |
| 5758 | struct btrfs_ref_path *ref_path = NULL; |
| 5759 | struct disk_extent *new_extents = NULL; |
| 5760 | int nr_extents = 0; |
| 5761 | int loops; |
| 5762 | int ret; |
| 5763 | int level; |
| 5764 | struct btrfs_key first_key; |
| 5765 | u64 prev_block = 0; |
| 5766 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5767 | |
| 5768 | trans = btrfs_start_transaction(extent_root, 1); |
| 5769 | BUG_ON(!trans); |
| 5770 | |
| 5771 | if (extent_key->objectid == 0) { |
| 5772 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 5773 | goto out; |
| 5774 | } |
| 5775 | |
| 5776 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 5777 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5778 | ret = -ENOMEM; |
| 5779 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5780 | } |
| 5781 | |
| 5782 | for (loops = 0; ; loops++) { |
| 5783 | if (loops == 0) { |
| 5784 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 5785 | extent_key->objectid); |
| 5786 | } else { |
| 5787 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 5788 | } |
| 5789 | if (ret < 0) |
| 5790 | goto out; |
| 5791 | if (ret > 0) |
| 5792 | break; |
| 5793 | |
| 5794 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 5795 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 5796 | continue; |
| 5797 | |
| 5798 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 5799 | BUG_ON(!found_root); |
| 5800 | /* |
| 5801 | * for reference counted tree, only process reference paths |
| 5802 | * rooted at the latest committed root. |
| 5803 | */ |
| 5804 | if (found_root->ref_cows && |
| 5805 | ref_path->root_generation != found_root->root_key.offset) |
| 5806 | continue; |
| 5807 | |
| 5808 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5809 | if (pass == 0) { |
| 5810 | /* |
| 5811 | * copy data extents to new locations |
| 5812 | */ |
| 5813 | u64 group_start = group->key.objectid; |
| 5814 | ret = relocate_data_extent(reloc_inode, |
| 5815 | extent_key, |
| 5816 | group_start); |
| 5817 | if (ret < 0) |
| 5818 | goto out; |
| 5819 | break; |
| 5820 | } |
| 5821 | level = 0; |
| 5822 | } else { |
| 5823 | level = ref_path->owner_objectid; |
| 5824 | } |
| 5825 | |
| 5826 | if (prev_block != ref_path->nodes[level]) { |
| 5827 | struct extent_buffer *eb; |
| 5828 | u64 block_start = ref_path->nodes[level]; |
| 5829 | u64 block_size = btrfs_level_size(found_root, level); |
| 5830 | |
| 5831 | eb = read_tree_block(found_root, block_start, |
| 5832 | block_size, 0); |
| 5833 | btrfs_tree_lock(eb); |
| 5834 | BUG_ON(level != btrfs_header_level(eb)); |
| 5835 | |
| 5836 | if (level == 0) |
| 5837 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 5838 | else |
| 5839 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 5840 | |
| 5841 | btrfs_tree_unlock(eb); |
| 5842 | free_extent_buffer(eb); |
| 5843 | prev_block = block_start; |
| 5844 | } |
| 5845 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 5846 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5847 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 5848 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5849 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 5850 | /* |
| 5851 | * try to update data extent references while |
| 5852 | * keeping metadata shared between snapshots. |
| 5853 | */ |
| 5854 | if (pass == 1) { |
| 5855 | ret = relocate_one_path(trans, found_root, |
| 5856 | path, &first_key, ref_path, |
| 5857 | group, reloc_inode); |
| 5858 | if (ret < 0) |
| 5859 | goto out; |
| 5860 | continue; |
| 5861 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5862 | /* |
| 5863 | * use fallback method to process the remaining |
| 5864 | * references. |
| 5865 | */ |
| 5866 | if (!new_extents) { |
| 5867 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5868 | new_extents = kmalloc(sizeof(*new_extents), |
| 5869 | GFP_NOFS); |
| 5870 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5871 | ret = get_new_locations(reloc_inode, |
| 5872 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5873 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5874 | &new_extents, |
| 5875 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 5876 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5877 | goto out; |
| 5878 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5879 | ret = replace_one_extent(trans, found_root, |
| 5880 | path, extent_key, |
| 5881 | &first_key, ref_path, |
| 5882 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 5883 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5884 | ret = relocate_tree_block(trans, found_root, path, |
| 5885 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5886 | } |
| 5887 | if (ret < 0) |
| 5888 | goto out; |
| 5889 | } |
| 5890 | ret = 0; |
| 5891 | out: |
| 5892 | btrfs_end_transaction(trans, extent_root); |
| 5893 | kfree(new_extents); |
| 5894 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 5895 | return ret; |
| 5896 | } |
| 5897 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5898 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 5899 | { |
| 5900 | u64 num_devices; |
| 5901 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 5902 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 5903 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 5904 | num_devices = root->fs_info->fs_devices->rw_devices; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5905 | if (num_devices == 1) { |
| 5906 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5907 | stripped = flags & ~stripped; |
| 5908 | |
| 5909 | /* turn raid0 into single device chunks */ |
| 5910 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 5911 | return stripped; |
| 5912 | |
| 5913 | /* turn mirroring into duplication */ |
| 5914 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 5915 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5916 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 5917 | return flags; |
| 5918 | } else { |
| 5919 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5920 | if (flags & stripped) |
| 5921 | return flags; |
| 5922 | |
| 5923 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 5924 | stripped = flags & ~stripped; |
| 5925 | |
| 5926 | /* switch duplicated blocks with raid1 */ |
| 5927 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 5928 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 5929 | |
| 5930 | /* turn single device chunks into raid0 */ |
| 5931 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 5932 | } |
| 5933 | return flags; |
| 5934 | } |
| 5935 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 5936 | static int __alloc_chunk_for_shrink(struct btrfs_root *root, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5937 | struct btrfs_block_group_cache *shrink_block_group, |
| 5938 | int force) |
| 5939 | { |
| 5940 | struct btrfs_trans_handle *trans; |
| 5941 | u64 new_alloc_flags; |
| 5942 | u64 calc; |
| 5943 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5944 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5945 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5946 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5947 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5948 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5949 | spin_lock(&shrink_block_group->lock); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5950 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5951 | new_alloc_flags = update_block_group_flags(root, |
| 5952 | shrink_block_group->flags); |
| 5953 | if (new_alloc_flags != shrink_block_group->flags) { |
| 5954 | calc = |
| 5955 | btrfs_block_group_used(&shrink_block_group->item); |
| 5956 | } else { |
| 5957 | calc = shrink_block_group->key.offset; |
| 5958 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5959 | spin_unlock(&shrink_block_group->lock); |
| 5960 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5961 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 5962 | calc + 2 * 1024 * 1024, new_alloc_flags, force); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 5963 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5964 | btrfs_end_transaction(trans, root); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5965 | } else |
| 5966 | spin_unlock(&shrink_block_group->lock); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5967 | return 0; |
| 5968 | } |
| 5969 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5970 | static int __insert_orphan_inode(struct btrfs_trans_handle *trans, |
| 5971 | struct btrfs_root *root, |
| 5972 | u64 objectid, u64 size) |
| 5973 | { |
| 5974 | struct btrfs_path *path; |
| 5975 | struct btrfs_inode_item *item; |
| 5976 | struct extent_buffer *leaf; |
| 5977 | int ret; |
| 5978 | |
| 5979 | path = btrfs_alloc_path(); |
| 5980 | if (!path) |
| 5981 | return -ENOMEM; |
| 5982 | |
| 5983 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 5984 | if (ret) |
| 5985 | goto out; |
| 5986 | |
| 5987 | leaf = path->nodes[0]; |
| 5988 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item); |
| 5989 | memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item)); |
| 5990 | btrfs_set_inode_generation(leaf, item, 1); |
| 5991 | btrfs_set_inode_size(leaf, item, size); |
| 5992 | btrfs_set_inode_mode(leaf, item, S_IFREG | 0600); |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 5993 | btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 5994 | btrfs_mark_buffer_dirty(leaf); |
| 5995 | btrfs_release_path(root, path); |
| 5996 | out: |
| 5997 | btrfs_free_path(path); |
| 5998 | return ret; |
| 5999 | } |
| 6000 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6001 | static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6002 | struct btrfs_block_group_cache *group) |
| 6003 | { |
| 6004 | struct inode *inode = NULL; |
| 6005 | struct btrfs_trans_handle *trans; |
| 6006 | struct btrfs_root *root; |
| 6007 | struct btrfs_key root_key; |
| 6008 | u64 objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 6009 | int err = 0; |
| 6010 | |
| 6011 | root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 6012 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 6013 | root_key.offset = (u64)-1; |
| 6014 | root = btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 6015 | if (IS_ERR(root)) |
| 6016 | return ERR_CAST(root); |
| 6017 | |
| 6018 | trans = btrfs_start_transaction(root, 1); |
| 6019 | BUG_ON(!trans); |
| 6020 | |
| 6021 | err = btrfs_find_free_objectid(trans, root, objectid, &objectid); |
| 6022 | if (err) |
| 6023 | goto out; |
| 6024 | |
| 6025 | err = __insert_orphan_inode(trans, root, objectid, group->key.offset); |
| 6026 | BUG_ON(err); |
| 6027 | |
| 6028 | err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6029 | group->key.offset, 0, group->key.offset, |
| 6030 | 0, 0, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6031 | BUG_ON(err); |
| 6032 | |
| 6033 | inode = btrfs_iget_locked(root->fs_info->sb, objectid, root); |
| 6034 | if (inode->i_state & I_NEW) { |
| 6035 | BTRFS_I(inode)->root = root; |
| 6036 | BTRFS_I(inode)->location.objectid = objectid; |
| 6037 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 6038 | BTRFS_I(inode)->location.offset = 0; |
| 6039 | btrfs_read_locked_inode(inode); |
| 6040 | unlock_new_inode(inode); |
| 6041 | BUG_ON(is_bad_inode(inode)); |
| 6042 | } else { |
| 6043 | BUG_ON(1); |
| 6044 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 6045 | BTRFS_I(inode)->index_cnt = group->key.objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6046 | |
| 6047 | err = btrfs_orphan_add(trans, inode); |
| 6048 | out: |
| 6049 | btrfs_end_transaction(trans, root); |
| 6050 | if (err) { |
| 6051 | if (inode) |
| 6052 | iput(inode); |
| 6053 | inode = ERR_PTR(err); |
| 6054 | } |
| 6055 | return inode; |
| 6056 | } |
| 6057 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 6058 | int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) |
| 6059 | { |
| 6060 | |
| 6061 | struct btrfs_ordered_sum *sums; |
| 6062 | struct btrfs_sector_sum *sector_sum; |
| 6063 | struct btrfs_ordered_extent *ordered; |
| 6064 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 6065 | struct list_head list; |
| 6066 | size_t offset; |
| 6067 | int ret; |
| 6068 | u64 disk_bytenr; |
| 6069 | |
| 6070 | INIT_LIST_HEAD(&list); |
| 6071 | |
| 6072 | ordered = btrfs_lookup_ordered_extent(inode, file_pos); |
| 6073 | BUG_ON(ordered->file_offset != file_pos || ordered->len != len); |
| 6074 | |
| 6075 | disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 6076 | ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 6077 | disk_bytenr + len - 1, &list); |
| 6078 | |
| 6079 | while (!list_empty(&list)) { |
| 6080 | sums = list_entry(list.next, struct btrfs_ordered_sum, list); |
| 6081 | list_del_init(&sums->list); |
| 6082 | |
| 6083 | sector_sum = sums->sums; |
| 6084 | sums->bytenr = ordered->start; |
| 6085 | |
| 6086 | offset = 0; |
| 6087 | while (offset < sums->len) { |
| 6088 | sector_sum->bytenr += ordered->start - disk_bytenr; |
| 6089 | sector_sum++; |
| 6090 | offset += root->sectorsize; |
| 6091 | } |
| 6092 | |
| 6093 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 6094 | } |
| 6095 | btrfs_put_ordered_extent(ordered); |
| 6096 | return 0; |
| 6097 | } |
| 6098 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6099 | int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6100 | { |
| 6101 | struct btrfs_trans_handle *trans; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6102 | struct btrfs_path *path; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6103 | struct btrfs_fs_info *info = root->fs_info; |
| 6104 | struct extent_buffer *leaf; |
| 6105 | struct inode *reloc_inode; |
| 6106 | struct btrfs_block_group_cache *block_group; |
| 6107 | struct btrfs_key key; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6108 | u64 skipped; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6109 | u64 cur_byte; |
| 6110 | u64 total_found; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6111 | u32 nritems; |
| 6112 | int ret; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6113 | int progress; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6114 | int pass = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6115 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6116 | root = root->fs_info->extent_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6117 | |
| 6118 | block_group = btrfs_lookup_block_group(info, group_start); |
| 6119 | BUG_ON(!block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6120 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6121 | printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6122 | (unsigned long long)block_group->key.objectid, |
| 6123 | (unsigned long long)block_group->flags); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6124 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6125 | path = btrfs_alloc_path(); |
| 6126 | BUG_ON(!path); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6127 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6128 | reloc_inode = create_reloc_inode(info, block_group); |
| 6129 | BUG_ON(IS_ERR(reloc_inode)); |
| 6130 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6131 | __alloc_chunk_for_shrink(root, block_group, 1); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6132 | set_block_group_readonly(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6133 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6134 | btrfs_start_delalloc_inodes(info->tree_root); |
| 6135 | btrfs_wait_ordered_extents(info->tree_root, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6136 | again: |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6137 | skipped = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6138 | total_found = 0; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6139 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6140 | key.objectid = block_group->key.objectid; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6141 | key.offset = 0; |
| 6142 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6143 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6144 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6145 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 6146 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6147 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6148 | mutex_lock(&root->fs_info->cleaner_mutex); |
| 6149 | btrfs_clean_old_snapshots(info->tree_root); |
| 6150 | btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1); |
| 6151 | mutex_unlock(&root->fs_info->cleaner_mutex); |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6152 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6153 | while (1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6154 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6155 | if (ret < 0) |
| 6156 | goto out; |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6157 | next: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6158 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6159 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6160 | if (path->slots[0] >= nritems) { |
| 6161 | ret = btrfs_next_leaf(root, path); |
| 6162 | if (ret < 0) |
| 6163 | goto out; |
| 6164 | if (ret == 1) { |
| 6165 | ret = 0; |
| 6166 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6167 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6168 | leaf = path->nodes[0]; |
| 6169 | nritems = btrfs_header_nritems(leaf); |
| 6170 | } |
| 6171 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6172 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6173 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6174 | if (key.objectid >= block_group->key.objectid + |
| 6175 | block_group->key.offset) |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 6176 | break; |
| 6177 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6178 | if (progress && need_resched()) { |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6179 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6180 | cond_resched(); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6181 | progress = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6182 | continue; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 6183 | } |
| 6184 | progress = 1; |
| 6185 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6186 | if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY || |
| 6187 | key.objectid + key.offset <= cur_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6188 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6189 | goto next; |
| 6190 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6191 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6192 | total_found++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6193 | cur_byte = key.objectid + key.offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6194 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6195 | |
| 6196 | __alloc_chunk_for_shrink(root, block_group, 0); |
| 6197 | ret = relocate_one_extent(root, path, &key, block_group, |
| 6198 | reloc_inode, pass); |
| 6199 | BUG_ON(ret < 0); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6200 | if (ret > 0) |
| 6201 | skipped++; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6202 | |
| 6203 | key.objectid = cur_byte; |
| 6204 | key.type = 0; |
| 6205 | key.offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | btrfs_release_path(root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6209 | |
| 6210 | if (pass == 0) { |
| 6211 | btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1); |
| 6212 | invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6213 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6214 | |
| 6215 | if (total_found > 0) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6216 | printk(KERN_INFO "btrfs found %llu extents in pass %d\n", |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6217 | (unsigned long long)total_found, pass); |
| 6218 | pass++; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6219 | if (total_found == skipped && pass > 2) { |
| 6220 | iput(reloc_inode); |
| 6221 | reloc_inode = create_reloc_inode(info, block_group); |
| 6222 | pass = 0; |
| 6223 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6224 | goto again; |
| 6225 | } |
| 6226 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6227 | /* delete reloc_inode */ |
| 6228 | iput(reloc_inode); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6229 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6230 | /* unpin extents in this range */ |
| 6231 | trans = btrfs_start_transaction(info->tree_root, 1); |
| 6232 | btrfs_commit_transaction(trans, info->tree_root); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6233 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6234 | spin_lock(&block_group->lock); |
| 6235 | WARN_ON(block_group->pinned > 0); |
| 6236 | WARN_ON(block_group->reserved > 0); |
| 6237 | WARN_ON(btrfs_block_group_used(&block_group->item) > 0); |
| 6238 | spin_unlock(&block_group->lock); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6239 | put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6240 | ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6241 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6242 | btrfs_free_path(path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6243 | return ret; |
| 6244 | } |
| 6245 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 6246 | static int find_first_block_group(struct btrfs_root *root, |
| 6247 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6248 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6249 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6250 | struct btrfs_key found_key; |
| 6251 | struct extent_buffer *leaf; |
| 6252 | int slot; |
| 6253 | |
| 6254 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 6255 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6256 | goto out; |
| 6257 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6258 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6259 | slot = path->slots[0]; |
| 6260 | leaf = path->nodes[0]; |
| 6261 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6262 | ret = btrfs_next_leaf(root, path); |
| 6263 | if (ret == 0) |
| 6264 | continue; |
| 6265 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6266 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6267 | break; |
| 6268 | } |
| 6269 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6270 | |
| 6271 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6272 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 6273 | ret = 0; |
| 6274 | goto out; |
| 6275 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6276 | path->slots[0]++; |
| 6277 | } |
| 6278 | ret = -ENOENT; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6279 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6280 | return ret; |
| 6281 | } |
| 6282 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6283 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 6284 | { |
| 6285 | struct btrfs_block_group_cache *block_group; |
| 6286 | struct rb_node *n; |
| 6287 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6288 | spin_lock(&info->block_group_cache_lock); |
| 6289 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 6290 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 6291 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6292 | rb_erase(&block_group->cache_node, |
| 6293 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6294 | spin_unlock(&info->block_group_cache_lock); |
| 6295 | |
| 6296 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6297 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6298 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6299 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6300 | |
| 6301 | WARN_ON(atomic_read(&block_group->count) != 1); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6302 | kfree(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6303 | |
| 6304 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6305 | } |
| 6306 | spin_unlock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6307 | return 0; |
| 6308 | } |
| 6309 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6310 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 6311 | { |
| 6312 | struct btrfs_path *path; |
| 6313 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6314 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 6315 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6316 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6317 | struct btrfs_key key; |
| 6318 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6319 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 6320 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 6321 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6322 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6323 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6324 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6325 | path = btrfs_alloc_path(); |
| 6326 | if (!path) |
| 6327 | return -ENOMEM; |
| 6328 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6329 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6330 | ret = find_first_block_group(root, path, &key); |
| 6331 | if (ret > 0) { |
| 6332 | ret = 0; |
| 6333 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6334 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6335 | if (ret != 0) |
| 6336 | goto error; |
| 6337 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6338 | leaf = path->nodes[0]; |
| 6339 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 6340 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6341 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6342 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6343 | break; |
| 6344 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 6345 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6346 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6347 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6348 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 6349 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6350 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6351 | read_extent_buffer(leaf, &cache->item, |
| 6352 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 6353 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6354 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6355 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6356 | key.objectid = found_key.objectid + found_key.offset; |
| 6357 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6358 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 6359 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6360 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 6361 | btrfs_block_group_used(&cache->item), |
| 6362 | &space_info); |
| 6363 | BUG_ON(ret); |
| 6364 | cache->space_info = space_info; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6365 | down_write(&space_info->groups_sem); |
| 6366 | list_add_tail(&cache->list, &space_info->block_groups); |
| 6367 | up_write(&space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6368 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6369 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 6370 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 6371 | |
| 6372 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6373 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
| 6374 | set_block_group_readonly(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6375 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6376 | ret = 0; |
| 6377 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6378 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6379 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6380 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6381 | |
| 6382 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 6383 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6384 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6385 | u64 size) |
| 6386 | { |
| 6387 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6388 | struct btrfs_root *extent_root; |
| 6389 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6390 | |
| 6391 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6392 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 6393 | root->fs_info->last_trans_new_blockgroup = trans->transid; |
| 6394 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 6395 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6396 | if (!cache) |
| 6397 | return -ENOMEM; |
| 6398 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 6399 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6400 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6401 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 6402 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6403 | spin_lock_init(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6404 | mutex_init(&cache->alloc_mutex); |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 6405 | mutex_init(&cache->cache_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6406 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6407 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6408 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6409 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 6410 | cache->flags = type; |
| 6411 | btrfs_set_block_group_flags(&cache->item, type); |
| 6412 | |
| 6413 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 6414 | &cache->space_info); |
| 6415 | BUG_ON(ret); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6416 | down_write(&cache->space_info->groups_sem); |
| 6417 | list_add_tail(&cache->list, &cache->space_info->block_groups); |
| 6418 | up_write(&cache->space_info->groups_sem); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6419 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6420 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 6421 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6422 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6423 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 6424 | sizeof(cache->item)); |
| 6425 | BUG_ON(ret); |
| 6426 | |
Chris Mason | 87ef2bb | 2008-10-30 11:23:27 -0400 | [diff] [blame] | 6427 | finish_current_insert(trans, extent_root, 0); |
| 6428 | ret = del_pending_extents(trans, extent_root, 0); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6429 | BUG_ON(ret); |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 6430 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6431 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6432 | return 0; |
| 6433 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6434 | |
| 6435 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 6436 | struct btrfs_root *root, u64 group_start) |
| 6437 | { |
| 6438 | struct btrfs_path *path; |
| 6439 | struct btrfs_block_group_cache *block_group; |
| 6440 | struct btrfs_key key; |
| 6441 | int ret; |
| 6442 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6443 | root = root->fs_info->extent_root; |
| 6444 | |
| 6445 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 6446 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6447 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6448 | |
| 6449 | memcpy(&key, &block_group->key, sizeof(key)); |
| 6450 | |
| 6451 | path = btrfs_alloc_path(); |
| 6452 | BUG_ON(!path); |
| 6453 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6454 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6455 | rb_erase(&block_group->cache_node, |
| 6456 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 6457 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 6458 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6459 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6460 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6461 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6462 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6463 | spin_lock(&block_group->space_info->lock); |
| 6464 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 6465 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
| 6466 | spin_unlock(&block_group->space_info->lock); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 6467 | block_group->space_info->full = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 6468 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6469 | put_block_group(block_group); |
| 6470 | put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6471 | |
| 6472 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 6473 | if (ret > 0) |
| 6474 | ret = -EIO; |
| 6475 | if (ret < 0) |
| 6476 | goto out; |
| 6477 | |
| 6478 | ret = btrfs_del_item(trans, root, path); |
| 6479 | out: |
| 6480 | btrfs_free_path(path); |
| 6481 | return ret; |
| 6482 | } |