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 | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 23 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 26 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 27 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 28 | #include "ctree.h" |
| 29 | #include "disk-io.h" |
| 30 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 31 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 32 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 33 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 34 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 36 | /* control flags for do_chunk_alloc's force field |
| 37 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
| 38 | * if we really need one. |
| 39 | * |
| 40 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
| 41 | * |
| 42 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
| 43 | * if we have very few chunks already allocated. This is |
| 44 | * used as part of the clustering code to help make sure |
| 45 | * we have a good pool of storage to cluster in, without |
| 46 | * filling the FS with empty chunks |
| 47 | * |
| 48 | */ |
| 49 | enum { |
| 50 | CHUNK_ALLOC_NO_FORCE = 0, |
| 51 | CHUNK_ALLOC_FORCE = 1, |
| 52 | CHUNK_ALLOC_LIMITED = 2, |
| 53 | }; |
| 54 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 55 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 56 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 57 | u64 bytenr, u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 58 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 59 | struct btrfs_root *root, |
| 60 | u64 bytenr, u64 num_bytes, u64 parent, |
| 61 | u64 root_objectid, u64 owner_objectid, |
| 62 | u64 owner_offset, int refs_to_drop, |
| 63 | struct btrfs_delayed_extent_op *extra_op); |
| 64 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 65 | struct extent_buffer *leaf, |
| 66 | struct btrfs_extent_item *ei); |
| 67 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 68 | struct btrfs_root *root, |
| 69 | u64 parent, u64 root_objectid, |
| 70 | u64 flags, u64 owner, u64 offset, |
| 71 | struct btrfs_key *ins, int ref_mod); |
| 72 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 73 | struct btrfs_root *root, |
| 74 | u64 parent, u64 root_objectid, |
| 75 | u64 flags, struct btrfs_disk_key *key, |
| 76 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 77 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 78 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 79 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 80 | static int find_next_key(struct btrfs_path *path, int level, |
| 81 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 82 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 83 | int dump_block_groups); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 84 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 85 | static noinline int |
| 86 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 87 | { |
| 88 | smp_mb(); |
| 89 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 90 | } |
| 91 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 92 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 93 | { |
| 94 | return (cache->flags & bits) == bits; |
| 95 | } |
| 96 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 97 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
| 98 | { |
| 99 | atomic_inc(&cache->count); |
| 100 | } |
| 101 | |
| 102 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 103 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 104 | if (atomic_dec_and_test(&cache->count)) { |
| 105 | WARN_ON(cache->pinned > 0); |
| 106 | WARN_ON(cache->reserved > 0); |
| 107 | WARN_ON(cache->reserved_pinned > 0); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 108 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 109 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 112 | /* |
| 113 | * this adds the block group to the fs_info rb tree for the block group |
| 114 | * cache |
| 115 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 116 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 117 | struct btrfs_block_group_cache *block_group) |
| 118 | { |
| 119 | struct rb_node **p; |
| 120 | struct rb_node *parent = NULL; |
| 121 | struct btrfs_block_group_cache *cache; |
| 122 | |
| 123 | spin_lock(&info->block_group_cache_lock); |
| 124 | p = &info->block_group_cache_tree.rb_node; |
| 125 | |
| 126 | while (*p) { |
| 127 | parent = *p; |
| 128 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 129 | cache_node); |
| 130 | if (block_group->key.objectid < cache->key.objectid) { |
| 131 | p = &(*p)->rb_left; |
| 132 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 133 | p = &(*p)->rb_right; |
| 134 | } else { |
| 135 | spin_unlock(&info->block_group_cache_lock); |
| 136 | return -EEXIST; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | rb_link_node(&block_group->cache_node, parent, p); |
| 141 | rb_insert_color(&block_group->cache_node, |
| 142 | &info->block_group_cache_tree); |
| 143 | spin_unlock(&info->block_group_cache_lock); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * This will return the block group at or after bytenr if contains is 0, else |
| 150 | * it will return the block group that contains the bytenr |
| 151 | */ |
| 152 | static struct btrfs_block_group_cache * |
| 153 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 154 | int contains) |
| 155 | { |
| 156 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 157 | struct rb_node *n; |
| 158 | u64 end, start; |
| 159 | |
| 160 | spin_lock(&info->block_group_cache_lock); |
| 161 | n = info->block_group_cache_tree.rb_node; |
| 162 | |
| 163 | while (n) { |
| 164 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 165 | cache_node); |
| 166 | end = cache->key.objectid + cache->key.offset - 1; |
| 167 | start = cache->key.objectid; |
| 168 | |
| 169 | if (bytenr < start) { |
| 170 | if (!contains && (!ret || start < ret->key.objectid)) |
| 171 | ret = cache; |
| 172 | n = n->rb_left; |
| 173 | } else if (bytenr > start) { |
| 174 | if (contains && bytenr <= end) { |
| 175 | ret = cache; |
| 176 | break; |
| 177 | } |
| 178 | n = n->rb_right; |
| 179 | } else { |
| 180 | ret = cache; |
| 181 | break; |
| 182 | } |
| 183 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 184 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 185 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 186 | spin_unlock(&info->block_group_cache_lock); |
| 187 | |
| 188 | return ret; |
| 189 | } |
| 190 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 191 | static int add_excluded_extent(struct btrfs_root *root, |
| 192 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 193 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 194 | u64 end = start + num_bytes - 1; |
| 195 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 196 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 197 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 198 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 199 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 202 | static void free_excluded_extents(struct btrfs_root *root, |
| 203 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 204 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 205 | u64 start, end; |
| 206 | |
| 207 | start = cache->key.objectid; |
| 208 | end = start + cache->key.offset - 1; |
| 209 | |
| 210 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 211 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 212 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 213 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 214 | } |
| 215 | |
| 216 | static int exclude_super_stripes(struct btrfs_root *root, |
| 217 | struct btrfs_block_group_cache *cache) |
| 218 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 219 | u64 bytenr; |
| 220 | u64 *logical; |
| 221 | int stripe_len; |
| 222 | int i, nr, ret; |
| 223 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 224 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 225 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 226 | cache->bytes_super += stripe_len; |
| 227 | ret = add_excluded_extent(root, cache->key.objectid, |
| 228 | stripe_len); |
| 229 | BUG_ON(ret); |
| 230 | } |
| 231 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 232 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 233 | bytenr = btrfs_sb_offset(i); |
| 234 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 235 | cache->key.objectid, bytenr, |
| 236 | 0, &logical, &nr, &stripe_len); |
| 237 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 238 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 239 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 240 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 241 | ret = add_excluded_extent(root, logical[nr], |
| 242 | stripe_len); |
| 243 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 244 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 245 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 246 | kfree(logical); |
| 247 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 251 | static struct btrfs_caching_control * |
| 252 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 253 | { |
| 254 | struct btrfs_caching_control *ctl; |
| 255 | |
| 256 | spin_lock(&cache->lock); |
| 257 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 258 | spin_unlock(&cache->lock); |
| 259 | return NULL; |
| 260 | } |
| 261 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 262 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 263 | if (!cache->caching_ctl) { |
| 264 | spin_unlock(&cache->lock); |
| 265 | return NULL; |
| 266 | } |
| 267 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 268 | ctl = cache->caching_ctl; |
| 269 | atomic_inc(&ctl->count); |
| 270 | spin_unlock(&cache->lock); |
| 271 | return ctl; |
| 272 | } |
| 273 | |
| 274 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 275 | { |
| 276 | if (atomic_dec_and_test(&ctl->count)) |
| 277 | kfree(ctl); |
| 278 | } |
| 279 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 280 | /* |
| 281 | * this is only called by cache_block_group, since we could have freed extents |
| 282 | * we need to check the pinned_extents for any extents that can't be used yet |
| 283 | * since their free space will be released as soon as the transaction commits. |
| 284 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 285 | static u64 add_new_free_space(struct btrfs_block_group_cache *block_group, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 286 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 287 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 288 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 289 | int ret; |
| 290 | |
| 291 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 292 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 293 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 294 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 295 | if (ret) |
| 296 | break; |
| 297 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 298 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 299 | start = extent_end + 1; |
| 300 | } else if (extent_start > start && extent_start < end) { |
| 301 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 302 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 303 | ret = btrfs_add_free_space(block_group, start, |
| 304 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 305 | BUG_ON(ret); |
| 306 | start = extent_end + 1; |
| 307 | } else { |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | if (start < end) { |
| 313 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 314 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 315 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 316 | BUG_ON(ret); |
| 317 | } |
| 318 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 319 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 320 | } |
| 321 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 322 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 323 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 324 | struct btrfs_block_group_cache *block_group = data; |
| 325 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 326 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 327 | struct btrfs_root *extent_root = fs_info->extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 328 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 329 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 330 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 331 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 332 | u64 last = 0; |
| 333 | u32 nritems; |
| 334 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 335 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 336 | path = btrfs_alloc_path(); |
| 337 | if (!path) |
| 338 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 339 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 340 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 341 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 342 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 343 | * We don't want to deadlock with somebody trying to allocate a new |
| 344 | * extent for the extent root while also trying to search the extent |
| 345 | * root to add free space. So we skip locking and search the commit |
| 346 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 347 | */ |
| 348 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 349 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame^] | 350 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 351 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 352 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 353 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 354 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 355 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 356 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 357 | /* need to make sure the commit_root doesn't disappear */ |
| 358 | down_read(&fs_info->extent_commit_sem); |
| 359 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 360 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 361 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 362 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 363 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 364 | leaf = path->nodes[0]; |
| 365 | nritems = btrfs_header_nritems(leaf); |
| 366 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 367 | while (1) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 368 | smp_mb(); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 369 | if (fs_info->closing > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 370 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 371 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 372 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 373 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 374 | if (path->slots[0] < nritems) { |
| 375 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 376 | } else { |
| 377 | ret = find_next_key(path, 0, &key); |
| 378 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 379 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 380 | |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 381 | if (need_resched() || |
| 382 | btrfs_next_leaf(extent_root, path)) { |
| 383 | caching_ctl->progress = last; |
| 384 | btrfs_release_path(extent_root, path); |
| 385 | up_read(&fs_info->extent_commit_sem); |
| 386 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 387 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 388 | goto again; |
| 389 | } |
| 390 | leaf = path->nodes[0]; |
| 391 | nritems = btrfs_header_nritems(leaf); |
| 392 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 393 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 394 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 395 | if (key.objectid < block_group->key.objectid) { |
| 396 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 397 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 398 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 399 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 400 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 401 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 402 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 403 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 404 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 405 | total_found += add_new_free_space(block_group, |
| 406 | fs_info, last, |
| 407 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 408 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 409 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 410 | if (total_found > (1024 * 1024 * 2)) { |
| 411 | total_found = 0; |
| 412 | wake_up(&caching_ctl->wait); |
| 413 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 414 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 415 | path->slots[0]++; |
| 416 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 417 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 418 | |
| 419 | total_found += add_new_free_space(block_group, fs_info, last, |
| 420 | block_group->key.objectid + |
| 421 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 422 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 423 | |
| 424 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 425 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 426 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 427 | spin_unlock(&block_group->lock); |
| 428 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 429 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 430 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 431 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 432 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 433 | free_excluded_extents(extent_root, block_group); |
| 434 | |
| 435 | mutex_unlock(&caching_ctl->mutex); |
| 436 | wake_up(&caching_ctl->wait); |
| 437 | |
| 438 | put_caching_control(caching_ctl); |
| 439 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 440 | btrfs_put_block_group(block_group); |
| 441 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 445 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
| 446 | struct btrfs_trans_handle *trans, |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 447 | struct btrfs_root *root, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 448 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 449 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 450 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 451 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 452 | struct task_struct *tsk; |
| 453 | int ret = 0; |
| 454 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 455 | smp_mb(); |
| 456 | if (cache->cached != BTRFS_CACHE_NO) |
| 457 | return 0; |
| 458 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 459 | /* |
| 460 | * We can't do the read from on-disk cache during a commit since we need |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 461 | * to have the normal tree locking. Also if we are currently trying to |
| 462 | * allocate blocks for the tree root we can't do the fast caching since |
| 463 | * we likely hold important locks. |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 464 | */ |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 465 | if (trans && (!trans->transaction->in_commit) && |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 466 | (root && root != root->fs_info->tree_root)) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 467 | spin_lock(&cache->lock); |
| 468 | if (cache->cached != BTRFS_CACHE_NO) { |
| 469 | spin_unlock(&cache->lock); |
| 470 | return 0; |
| 471 | } |
| 472 | cache->cached = BTRFS_CACHE_STARTED; |
| 473 | spin_unlock(&cache->lock); |
| 474 | |
| 475 | ret = load_free_space_cache(fs_info, cache); |
| 476 | |
| 477 | spin_lock(&cache->lock); |
| 478 | if (ret == 1) { |
| 479 | cache->cached = BTRFS_CACHE_FINISHED; |
| 480 | cache->last_byte_to_unpin = (u64)-1; |
| 481 | } else { |
| 482 | cache->cached = BTRFS_CACHE_NO; |
| 483 | } |
| 484 | spin_unlock(&cache->lock); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 485 | if (ret == 1) { |
| 486 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 487 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 488 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if (load_cache_only) |
| 492 | return 0; |
| 493 | |
Miao Xie | fc0e4a3 | 2011-03-24 11:41:21 +0000 | [diff] [blame] | 494 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 495 | BUG_ON(!caching_ctl); |
| 496 | |
| 497 | INIT_LIST_HEAD(&caching_ctl->list); |
| 498 | mutex_init(&caching_ctl->mutex); |
| 499 | init_waitqueue_head(&caching_ctl->wait); |
| 500 | caching_ctl->block_group = cache; |
| 501 | caching_ctl->progress = cache->key.objectid; |
| 502 | /* one for caching kthread, one for caching block group list */ |
| 503 | atomic_set(&caching_ctl->count, 2); |
| 504 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 505 | spin_lock(&cache->lock); |
| 506 | if (cache->cached != BTRFS_CACHE_NO) { |
| 507 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 508 | kfree(caching_ctl); |
| 509 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 510 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 511 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 512 | cache->cached = BTRFS_CACHE_STARTED; |
| 513 | spin_unlock(&cache->lock); |
| 514 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 515 | down_write(&fs_info->extent_commit_sem); |
| 516 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 517 | up_write(&fs_info->extent_commit_sem); |
| 518 | |
| 519 | atomic_inc(&cache->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 520 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 521 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 522 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 523 | cache->key.objectid); |
| 524 | if (IS_ERR(tsk)) { |
| 525 | ret = PTR_ERR(tsk); |
| 526 | printk(KERN_ERR "error running thread %d\n", ret); |
| 527 | BUG(); |
| 528 | } |
| 529 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 530 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 533 | /* |
| 534 | * return the block group that starts at or after bytenr |
| 535 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 536 | static struct btrfs_block_group_cache * |
| 537 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 538 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 539 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 540 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 541 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 542 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 543 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 546 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 547 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 548 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 549 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 550 | struct btrfs_fs_info *info, |
| 551 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 552 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 553 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 554 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 555 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 556 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 557 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 558 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 559 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 560 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 561 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 562 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 563 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 564 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 565 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 566 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 567 | BTRFS_BLOCK_GROUP_METADATA; |
| 568 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 569 | rcu_read_lock(); |
| 570 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 571 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 572 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 573 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 574 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 575 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 576 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 577 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 580 | /* |
| 581 | * after adding space to the filesystem, we need to clear the full flags |
| 582 | * on all the space infos. |
| 583 | */ |
| 584 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 585 | { |
| 586 | struct list_head *head = &info->space_info; |
| 587 | struct btrfs_space_info *found; |
| 588 | |
| 589 | rcu_read_lock(); |
| 590 | list_for_each_entry_rcu(found, head, list) |
| 591 | found->full = 0; |
| 592 | rcu_read_unlock(); |
| 593 | } |
| 594 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 595 | static u64 div_factor(u64 num, int factor) |
| 596 | { |
| 597 | if (factor == 10) |
| 598 | return num; |
| 599 | num *= factor; |
| 600 | do_div(num, 10); |
| 601 | return num; |
| 602 | } |
| 603 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 604 | static u64 div_factor_fine(u64 num, int factor) |
| 605 | { |
| 606 | if (factor == 100) |
| 607 | return num; |
| 608 | num *= factor; |
| 609 | do_div(num, 100); |
| 610 | return num; |
| 611 | } |
| 612 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 613 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 614 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 615 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 616 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 617 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 618 | u64 last = max(search_hint, search_start); |
| 619 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 620 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 621 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 622 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 623 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 624 | while (1) { |
| 625 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 626 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 627 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 628 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 629 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 630 | last = cache->key.objectid + cache->key.offset; |
| 631 | used = btrfs_block_group_used(&cache->item); |
| 632 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 633 | if ((full_search || !cache->ro) && |
| 634 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 635 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 636 | div_factor(cache->key.offset, factor)) { |
| 637 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 638 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 639 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 640 | goto found; |
| 641 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 642 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 643 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 644 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 645 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 646 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 647 | if (!wrapped) { |
| 648 | last = search_start; |
| 649 | wrapped = 1; |
| 650 | goto again; |
| 651 | } |
| 652 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 653 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 654 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 655 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 656 | goto again; |
| 657 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 658 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 659 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 660 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 661 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 662 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 663 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 664 | { |
| 665 | int ret; |
| 666 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 667 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 668 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 669 | path = btrfs_alloc_path(); |
| 670 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 671 | key.objectid = start; |
| 672 | key.offset = len; |
| 673 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 674 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 675 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 676 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 677 | return ret; |
| 678 | } |
| 679 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 680 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 681 | * helper function to lookup reference count and flags of extent. |
| 682 | * |
| 683 | * the head node for delayed ref is used to store the sum of all the |
| 684 | * reference count modifications queued up in the rbtree. the head |
| 685 | * node may also store the extent flags to set. This way you can check |
| 686 | * to see what the reference count and extent flags would be if all of |
| 687 | * the delayed refs are not processed. |
| 688 | */ |
| 689 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 690 | struct btrfs_root *root, u64 bytenr, |
| 691 | u64 num_bytes, u64 *refs, u64 *flags) |
| 692 | { |
| 693 | struct btrfs_delayed_ref_head *head; |
| 694 | struct btrfs_delayed_ref_root *delayed_refs; |
| 695 | struct btrfs_path *path; |
| 696 | struct btrfs_extent_item *ei; |
| 697 | struct extent_buffer *leaf; |
| 698 | struct btrfs_key key; |
| 699 | u32 item_size; |
| 700 | u64 num_refs; |
| 701 | u64 extent_flags; |
| 702 | int ret; |
| 703 | |
| 704 | path = btrfs_alloc_path(); |
| 705 | if (!path) |
| 706 | return -ENOMEM; |
| 707 | |
| 708 | key.objectid = bytenr; |
| 709 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 710 | key.offset = num_bytes; |
| 711 | if (!trans) { |
| 712 | path->skip_locking = 1; |
| 713 | path->search_commit_root = 1; |
| 714 | } |
| 715 | again: |
| 716 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 717 | &key, path, 0, 0); |
| 718 | if (ret < 0) |
| 719 | goto out_free; |
| 720 | |
| 721 | if (ret == 0) { |
| 722 | leaf = path->nodes[0]; |
| 723 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 724 | if (item_size >= sizeof(*ei)) { |
| 725 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 726 | struct btrfs_extent_item); |
| 727 | num_refs = btrfs_extent_refs(leaf, ei); |
| 728 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 729 | } else { |
| 730 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 731 | struct btrfs_extent_item_v0 *ei0; |
| 732 | BUG_ON(item_size != sizeof(*ei0)); |
| 733 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 734 | struct btrfs_extent_item_v0); |
| 735 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 736 | /* FIXME: this isn't correct for data */ |
| 737 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 738 | #else |
| 739 | BUG(); |
| 740 | #endif |
| 741 | } |
| 742 | BUG_ON(num_refs == 0); |
| 743 | } else { |
| 744 | num_refs = 0; |
| 745 | extent_flags = 0; |
| 746 | ret = 0; |
| 747 | } |
| 748 | |
| 749 | if (!trans) |
| 750 | goto out; |
| 751 | |
| 752 | delayed_refs = &trans->transaction->delayed_refs; |
| 753 | spin_lock(&delayed_refs->lock); |
| 754 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 755 | if (head) { |
| 756 | if (!mutex_trylock(&head->mutex)) { |
| 757 | atomic_inc(&head->node.refs); |
| 758 | spin_unlock(&delayed_refs->lock); |
| 759 | |
| 760 | btrfs_release_path(root->fs_info->extent_root, path); |
| 761 | |
| 762 | mutex_lock(&head->mutex); |
| 763 | mutex_unlock(&head->mutex); |
| 764 | btrfs_put_delayed_ref(&head->node); |
| 765 | goto again; |
| 766 | } |
| 767 | if (head->extent_op && head->extent_op->update_flags) |
| 768 | extent_flags |= head->extent_op->flags_to_set; |
| 769 | else |
| 770 | BUG_ON(num_refs == 0); |
| 771 | |
| 772 | num_refs += head->node.ref_mod; |
| 773 | mutex_unlock(&head->mutex); |
| 774 | } |
| 775 | spin_unlock(&delayed_refs->lock); |
| 776 | out: |
| 777 | WARN_ON(num_refs == 0); |
| 778 | if (refs) |
| 779 | *refs = num_refs; |
| 780 | if (flags) |
| 781 | *flags = extent_flags; |
| 782 | out_free: |
| 783 | btrfs_free_path(path); |
| 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 788 | * Back reference rules. Back refs have three main goals: |
| 789 | * |
| 790 | * 1) differentiate between all holders of references to an extent so that |
| 791 | * when a reference is dropped we can make sure it was a valid reference |
| 792 | * before freeing the extent. |
| 793 | * |
| 794 | * 2) Provide enough information to quickly find the holders of an extent |
| 795 | * if we notice a given block is corrupted or bad. |
| 796 | * |
| 797 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 798 | * maintenance. This is actually the same as #2, but with a slightly |
| 799 | * different use case. |
| 800 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 801 | * There are two kinds of back refs. The implicit back refs is optimized |
| 802 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 803 | * back refs of this kind provide information about the block's owner tree |
| 804 | * and the pointer's key. These information allow us to find the block by |
| 805 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 806 | * referenced by their owner trees. The location of tree block is recorded |
| 807 | * in the back refs. Actually the full back refs is generic, and can be |
| 808 | * used in all cases the implicit back refs is used. The major shortcoming |
| 809 | * of the full back refs is its overhead. Every time a tree block gets |
| 810 | * COWed, we have to update back refs entry for all pointers in it. |
| 811 | * |
| 812 | * For a newly allocated tree block, we use implicit back refs for |
| 813 | * pointers in it. This means most tree related operations only involve |
| 814 | * implicit back refs. For a tree block created in old transaction, the |
| 815 | * only way to drop a reference to it is COW it. So we can detect the |
| 816 | * event that tree block loses its owner tree's reference and do the |
| 817 | * back refs conversion. |
| 818 | * |
| 819 | * When a tree block is COW'd through a tree, there are four cases: |
| 820 | * |
| 821 | * The reference count of the block is one and the tree is the block's |
| 822 | * owner tree. Nothing to do in this case. |
| 823 | * |
| 824 | * The reference count of the block is one and the tree is not the |
| 825 | * block's owner tree. In this case, full back refs is used for pointers |
| 826 | * in the block. Remove these full back refs, add implicit back refs for |
| 827 | * every pointers in the new block. |
| 828 | * |
| 829 | * The reference count of the block is greater than one and the tree is |
| 830 | * the block's owner tree. In this case, implicit back refs is used for |
| 831 | * pointers in the block. Add full back refs for every pointers in the |
| 832 | * block, increase lower level extents' reference counts. The original |
| 833 | * implicit back refs are entailed to the new block. |
| 834 | * |
| 835 | * The reference count of the block is greater than one and the tree is |
| 836 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 837 | * the new block, increase lower level extents' reference count. |
| 838 | * |
| 839 | * Back Reference Key composing: |
| 840 | * |
| 841 | * The key objectid corresponds to the first byte in the extent, |
| 842 | * The key type is used to differentiate between types of back refs. |
| 843 | * There are different meanings of the key offset for different types |
| 844 | * of back refs. |
| 845 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 846 | * File extents can be referenced by: |
| 847 | * |
| 848 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 849 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 850 | * - different offsets inside a file (bookend extents in file.c) |
| 851 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 852 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 853 | * |
| 854 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 855 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 856 | * - original offset in the file |
| 857 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 858 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 859 | * The key offset for the implicit back refs is hash of the first |
| 860 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 861 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 862 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 863 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 864 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 865 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 866 | * The key offset for the implicit back refs is the first byte of |
| 867 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 868 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 869 | * When a file extent is allocated, The implicit back refs is used. |
| 870 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 871 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 872 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 873 | * |
| 874 | * When a file extent is removed file truncation, we find the |
| 875 | * corresponding implicit back refs and check the following fields: |
| 876 | * |
| 877 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 878 | * |
| 879 | * Btree extents can be referenced by: |
| 880 | * |
| 881 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 882 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 883 | * Both the implicit back refs and the full back refs for tree blocks |
| 884 | * only consist of key. The key offset for the implicit back refs is |
| 885 | * objectid of block's owner tree. The key offset for the full back refs |
| 886 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 887 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 888 | * When implicit back refs is used, information about the lowest key and |
| 889 | * level of the tree block are required. These information are stored in |
| 890 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 891 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 892 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 893 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 894 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 895 | struct btrfs_root *root, |
| 896 | struct btrfs_path *path, |
| 897 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 898 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 899 | struct btrfs_extent_item *item; |
| 900 | struct btrfs_extent_item_v0 *ei0; |
| 901 | struct btrfs_extent_ref_v0 *ref0; |
| 902 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 903 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 904 | struct btrfs_key key; |
| 905 | struct btrfs_key found_key; |
| 906 | u32 new_size = sizeof(*item); |
| 907 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 908 | int ret; |
| 909 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 910 | leaf = path->nodes[0]; |
| 911 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 912 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 913 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 914 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 915 | struct btrfs_extent_item_v0); |
| 916 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 917 | |
| 918 | if (owner == (u64)-1) { |
| 919 | while (1) { |
| 920 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 921 | ret = btrfs_next_leaf(root, path); |
| 922 | if (ret < 0) |
| 923 | return ret; |
| 924 | BUG_ON(ret > 0); |
| 925 | leaf = path->nodes[0]; |
| 926 | } |
| 927 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 928 | path->slots[0]); |
| 929 | BUG_ON(key.objectid != found_key.objectid); |
| 930 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 931 | path->slots[0]++; |
| 932 | continue; |
| 933 | } |
| 934 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 935 | struct btrfs_extent_ref_v0); |
| 936 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 937 | break; |
| 938 | } |
| 939 | } |
| 940 | btrfs_release_path(root, path); |
| 941 | |
| 942 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 943 | new_size += sizeof(*bi); |
| 944 | |
| 945 | new_size -= sizeof(*ei0); |
| 946 | ret = btrfs_search_slot(trans, root, &key, path, |
| 947 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 948 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 949 | return ret; |
| 950 | BUG_ON(ret); |
| 951 | |
| 952 | ret = btrfs_extend_item(trans, root, path, new_size); |
| 953 | BUG_ON(ret); |
| 954 | |
| 955 | leaf = path->nodes[0]; |
| 956 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 957 | btrfs_set_extent_refs(leaf, item, refs); |
| 958 | /* FIXME: get real generation */ |
| 959 | btrfs_set_extent_generation(leaf, item, 0); |
| 960 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 961 | btrfs_set_extent_flags(leaf, item, |
| 962 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 963 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 964 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 965 | /* FIXME: get first key of the block */ |
| 966 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 967 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 968 | } else { |
| 969 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 970 | } |
| 971 | btrfs_mark_buffer_dirty(leaf); |
| 972 | return 0; |
| 973 | } |
| 974 | #endif |
| 975 | |
| 976 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 977 | { |
| 978 | u32 high_crc = ~(u32)0; |
| 979 | u32 low_crc = ~(u32)0; |
| 980 | __le64 lenum; |
| 981 | |
| 982 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 983 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 984 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 985 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 986 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 987 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 988 | |
| 989 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 990 | } |
| 991 | |
| 992 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 993 | struct btrfs_extent_data_ref *ref) |
| 994 | { |
| 995 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 996 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 997 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 998 | } |
| 999 | |
| 1000 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1001 | struct btrfs_extent_data_ref *ref, |
| 1002 | u64 root_objectid, u64 owner, u64 offset) |
| 1003 | { |
| 1004 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1005 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1006 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1007 | return 0; |
| 1008 | return 1; |
| 1009 | } |
| 1010 | |
| 1011 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1012 | struct btrfs_root *root, |
| 1013 | struct btrfs_path *path, |
| 1014 | u64 bytenr, u64 parent, |
| 1015 | u64 root_objectid, |
| 1016 | u64 owner, u64 offset) |
| 1017 | { |
| 1018 | struct btrfs_key key; |
| 1019 | struct btrfs_extent_data_ref *ref; |
| 1020 | struct extent_buffer *leaf; |
| 1021 | u32 nritems; |
| 1022 | int ret; |
| 1023 | int recow; |
| 1024 | int err = -ENOENT; |
| 1025 | |
| 1026 | key.objectid = bytenr; |
| 1027 | if (parent) { |
| 1028 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1029 | key.offset = parent; |
| 1030 | } else { |
| 1031 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1032 | key.offset = hash_extent_data_ref(root_objectid, |
| 1033 | owner, offset); |
| 1034 | } |
| 1035 | again: |
| 1036 | recow = 0; |
| 1037 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1038 | if (ret < 0) { |
| 1039 | err = ret; |
| 1040 | goto fail; |
| 1041 | } |
| 1042 | |
| 1043 | if (parent) { |
| 1044 | if (!ret) |
| 1045 | return 0; |
| 1046 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1047 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1048 | btrfs_release_path(root, path); |
| 1049 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1050 | if (ret < 0) { |
| 1051 | err = ret; |
| 1052 | goto fail; |
| 1053 | } |
| 1054 | if (!ret) |
| 1055 | return 0; |
| 1056 | #endif |
| 1057 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1061 | nritems = btrfs_header_nritems(leaf); |
| 1062 | while (1) { |
| 1063 | if (path->slots[0] >= nritems) { |
| 1064 | ret = btrfs_next_leaf(root, path); |
| 1065 | if (ret < 0) |
| 1066 | err = ret; |
| 1067 | if (ret) |
| 1068 | goto fail; |
| 1069 | |
| 1070 | leaf = path->nodes[0]; |
| 1071 | nritems = btrfs_header_nritems(leaf); |
| 1072 | recow = 1; |
| 1073 | } |
| 1074 | |
| 1075 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1076 | if (key.objectid != bytenr || |
| 1077 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1078 | goto fail; |
| 1079 | |
| 1080 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1081 | struct btrfs_extent_data_ref); |
| 1082 | |
| 1083 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1084 | owner, offset)) { |
| 1085 | if (recow) { |
| 1086 | btrfs_release_path(root, path); |
| 1087 | goto again; |
| 1088 | } |
| 1089 | err = 0; |
| 1090 | break; |
| 1091 | } |
| 1092 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1093 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1094 | fail: |
| 1095 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1096 | } |
| 1097 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1098 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1099 | struct btrfs_root *root, |
| 1100 | struct btrfs_path *path, |
| 1101 | u64 bytenr, u64 parent, |
| 1102 | u64 root_objectid, u64 owner, |
| 1103 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1104 | { |
| 1105 | struct btrfs_key key; |
| 1106 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1107 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1108 | u32 num_refs; |
| 1109 | int ret; |
| 1110 | |
| 1111 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1112 | if (parent) { |
| 1113 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1114 | key.offset = parent; |
| 1115 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1116 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1117 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1118 | key.offset = hash_extent_data_ref(root_objectid, |
| 1119 | owner, offset); |
| 1120 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1121 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1122 | |
| 1123 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1124 | if (ret && ret != -EEXIST) |
| 1125 | goto fail; |
| 1126 | |
| 1127 | leaf = path->nodes[0]; |
| 1128 | if (parent) { |
| 1129 | struct btrfs_shared_data_ref *ref; |
| 1130 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1131 | struct btrfs_shared_data_ref); |
| 1132 | if (ret == 0) { |
| 1133 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1134 | } else { |
| 1135 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1136 | num_refs += refs_to_add; |
| 1137 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1138 | } |
| 1139 | } else { |
| 1140 | struct btrfs_extent_data_ref *ref; |
| 1141 | while (ret == -EEXIST) { |
| 1142 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1143 | struct btrfs_extent_data_ref); |
| 1144 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1145 | owner, offset)) |
| 1146 | break; |
| 1147 | btrfs_release_path(root, path); |
| 1148 | key.offset++; |
| 1149 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1150 | size); |
| 1151 | if (ret && ret != -EEXIST) |
| 1152 | goto fail; |
| 1153 | |
| 1154 | leaf = path->nodes[0]; |
| 1155 | } |
| 1156 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1157 | struct btrfs_extent_data_ref); |
| 1158 | if (ret == 0) { |
| 1159 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1160 | root_objectid); |
| 1161 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1162 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1163 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1164 | } else { |
| 1165 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1166 | num_refs += refs_to_add; |
| 1167 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1168 | } |
| 1169 | } |
| 1170 | btrfs_mark_buffer_dirty(leaf); |
| 1171 | ret = 0; |
| 1172 | fail: |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1173 | btrfs_release_path(root, path); |
| 1174 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1175 | } |
| 1176 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1177 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1178 | struct btrfs_root *root, |
| 1179 | struct btrfs_path *path, |
| 1180 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1181 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1182 | struct btrfs_key key; |
| 1183 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1184 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1185 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1186 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1187 | int ret = 0; |
| 1188 | |
| 1189 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1190 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1191 | |
| 1192 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1193 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1194 | struct btrfs_extent_data_ref); |
| 1195 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1196 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1197 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1198 | struct btrfs_shared_data_ref); |
| 1199 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1200 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1201 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1202 | struct btrfs_extent_ref_v0 *ref0; |
| 1203 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1204 | struct btrfs_extent_ref_v0); |
| 1205 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1206 | #endif |
| 1207 | } else { |
| 1208 | BUG(); |
| 1209 | } |
| 1210 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1211 | BUG_ON(num_refs < refs_to_drop); |
| 1212 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1213 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1214 | if (num_refs == 0) { |
| 1215 | ret = btrfs_del_item(trans, root, path); |
| 1216 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1217 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1218 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1219 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1220 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1221 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1222 | else { |
| 1223 | struct btrfs_extent_ref_v0 *ref0; |
| 1224 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1225 | struct btrfs_extent_ref_v0); |
| 1226 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1227 | } |
| 1228 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1229 | btrfs_mark_buffer_dirty(leaf); |
| 1230 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1231 | return ret; |
| 1232 | } |
| 1233 | |
| 1234 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1235 | struct btrfs_path *path, |
| 1236 | struct btrfs_extent_inline_ref *iref) |
| 1237 | { |
| 1238 | struct btrfs_key key; |
| 1239 | struct extent_buffer *leaf; |
| 1240 | struct btrfs_extent_data_ref *ref1; |
| 1241 | struct btrfs_shared_data_ref *ref2; |
| 1242 | u32 num_refs = 0; |
| 1243 | |
| 1244 | leaf = path->nodes[0]; |
| 1245 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1246 | if (iref) { |
| 1247 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1248 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1249 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1250 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1251 | } else { |
| 1252 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1253 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1254 | } |
| 1255 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1256 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1257 | struct btrfs_extent_data_ref); |
| 1258 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1259 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1260 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1261 | struct btrfs_shared_data_ref); |
| 1262 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1263 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1264 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1265 | struct btrfs_extent_ref_v0 *ref0; |
| 1266 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1267 | struct btrfs_extent_ref_v0); |
| 1268 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1269 | #endif |
| 1270 | } else { |
| 1271 | WARN_ON(1); |
| 1272 | } |
| 1273 | return num_refs; |
| 1274 | } |
| 1275 | |
| 1276 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1277 | struct btrfs_root *root, |
| 1278 | struct btrfs_path *path, |
| 1279 | u64 bytenr, u64 parent, |
| 1280 | u64 root_objectid) |
| 1281 | { |
| 1282 | struct btrfs_key key; |
| 1283 | int ret; |
| 1284 | |
| 1285 | key.objectid = bytenr; |
| 1286 | if (parent) { |
| 1287 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1288 | key.offset = parent; |
| 1289 | } else { |
| 1290 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1291 | key.offset = root_objectid; |
| 1292 | } |
| 1293 | |
| 1294 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1295 | if (ret > 0) |
| 1296 | ret = -ENOENT; |
| 1297 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1298 | if (ret == -ENOENT && parent) { |
| 1299 | btrfs_release_path(root, path); |
| 1300 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1301 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1302 | if (ret > 0) |
| 1303 | ret = -ENOENT; |
| 1304 | } |
| 1305 | #endif |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1310 | struct btrfs_root *root, |
| 1311 | struct btrfs_path *path, |
| 1312 | u64 bytenr, u64 parent, |
| 1313 | u64 root_objectid) |
| 1314 | { |
| 1315 | struct btrfs_key key; |
| 1316 | int ret; |
| 1317 | |
| 1318 | key.objectid = bytenr; |
| 1319 | if (parent) { |
| 1320 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1321 | key.offset = parent; |
| 1322 | } else { |
| 1323 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1324 | key.offset = root_objectid; |
| 1325 | } |
| 1326 | |
| 1327 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1328 | btrfs_release_path(root, path); |
| 1329 | return ret; |
| 1330 | } |
| 1331 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1332 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1333 | { |
| 1334 | int type; |
| 1335 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1336 | if (parent > 0) |
| 1337 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1338 | else |
| 1339 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1340 | } else { |
| 1341 | if (parent > 0) |
| 1342 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1343 | else |
| 1344 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1345 | } |
| 1346 | return type; |
| 1347 | } |
| 1348 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1349 | static int find_next_key(struct btrfs_path *path, int level, |
| 1350 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1351 | |
| 1352 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1353 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1354 | if (!path->nodes[level]) |
| 1355 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1356 | if (path->slots[level] + 1 >= |
| 1357 | btrfs_header_nritems(path->nodes[level])) |
| 1358 | continue; |
| 1359 | if (level == 0) |
| 1360 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1361 | path->slots[level] + 1); |
| 1362 | else |
| 1363 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1364 | path->slots[level] + 1); |
| 1365 | return 0; |
| 1366 | } |
| 1367 | return 1; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
| 1371 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1372 | * to the address of inline back ref, and 0 is returned. |
| 1373 | * |
| 1374 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1375 | * should be inserted, and -ENOENT is returned. |
| 1376 | * |
| 1377 | * if insert is true and there are too many inline back refs, the path |
| 1378 | * points to the extent item, and -EAGAIN is returned. |
| 1379 | * |
| 1380 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1381 | * items in the tree are ordered. |
| 1382 | */ |
| 1383 | static noinline_for_stack |
| 1384 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1385 | struct btrfs_root *root, |
| 1386 | struct btrfs_path *path, |
| 1387 | struct btrfs_extent_inline_ref **ref_ret, |
| 1388 | u64 bytenr, u64 num_bytes, |
| 1389 | u64 parent, u64 root_objectid, |
| 1390 | u64 owner, u64 offset, int insert) |
| 1391 | { |
| 1392 | struct btrfs_key key; |
| 1393 | struct extent_buffer *leaf; |
| 1394 | struct btrfs_extent_item *ei; |
| 1395 | struct btrfs_extent_inline_ref *iref; |
| 1396 | u64 flags; |
| 1397 | u64 item_size; |
| 1398 | unsigned long ptr; |
| 1399 | unsigned long end; |
| 1400 | int extra_size; |
| 1401 | int type; |
| 1402 | int want; |
| 1403 | int ret; |
| 1404 | int err = 0; |
| 1405 | |
| 1406 | key.objectid = bytenr; |
| 1407 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1408 | key.offset = num_bytes; |
| 1409 | |
| 1410 | want = extent_ref_type(parent, owner); |
| 1411 | if (insert) { |
| 1412 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1413 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1414 | } else |
| 1415 | extra_size = -1; |
| 1416 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1417 | if (ret < 0) { |
| 1418 | err = ret; |
| 1419 | goto out; |
| 1420 | } |
| 1421 | BUG_ON(ret); |
| 1422 | |
| 1423 | leaf = path->nodes[0]; |
| 1424 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1425 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1426 | if (item_size < sizeof(*ei)) { |
| 1427 | if (!insert) { |
| 1428 | err = -ENOENT; |
| 1429 | goto out; |
| 1430 | } |
| 1431 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1432 | extra_size); |
| 1433 | if (ret < 0) { |
| 1434 | err = ret; |
| 1435 | goto out; |
| 1436 | } |
| 1437 | leaf = path->nodes[0]; |
| 1438 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1439 | } |
| 1440 | #endif |
| 1441 | BUG_ON(item_size < sizeof(*ei)); |
| 1442 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1443 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1444 | flags = btrfs_extent_flags(leaf, ei); |
| 1445 | |
| 1446 | ptr = (unsigned long)(ei + 1); |
| 1447 | end = (unsigned long)ei + item_size; |
| 1448 | |
| 1449 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1450 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1451 | BUG_ON(ptr > end); |
| 1452 | } else { |
| 1453 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1454 | } |
| 1455 | |
| 1456 | err = -ENOENT; |
| 1457 | while (1) { |
| 1458 | if (ptr >= end) { |
| 1459 | WARN_ON(ptr > end); |
| 1460 | break; |
| 1461 | } |
| 1462 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1463 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1464 | if (want < type) |
| 1465 | break; |
| 1466 | if (want > type) { |
| 1467 | ptr += btrfs_extent_inline_ref_size(type); |
| 1468 | continue; |
| 1469 | } |
| 1470 | |
| 1471 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1472 | struct btrfs_extent_data_ref *dref; |
| 1473 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1474 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1475 | owner, offset)) { |
| 1476 | err = 0; |
| 1477 | break; |
| 1478 | } |
| 1479 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1480 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1481 | break; |
| 1482 | } else { |
| 1483 | u64 ref_offset; |
| 1484 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1485 | if (parent > 0) { |
| 1486 | if (parent == ref_offset) { |
| 1487 | err = 0; |
| 1488 | break; |
| 1489 | } |
| 1490 | if (ref_offset < parent) |
| 1491 | break; |
| 1492 | } else { |
| 1493 | if (root_objectid == ref_offset) { |
| 1494 | err = 0; |
| 1495 | break; |
| 1496 | } |
| 1497 | if (ref_offset < root_objectid) |
| 1498 | break; |
| 1499 | } |
| 1500 | } |
| 1501 | ptr += btrfs_extent_inline_ref_size(type); |
| 1502 | } |
| 1503 | if (err == -ENOENT && insert) { |
| 1504 | if (item_size + extra_size >= |
| 1505 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1506 | err = -EAGAIN; |
| 1507 | goto out; |
| 1508 | } |
| 1509 | /* |
| 1510 | * To add new inline back ref, we have to make sure |
| 1511 | * there is no corresponding back ref item. |
| 1512 | * For simplicity, we just do not add new inline back |
| 1513 | * ref if there is any kind of item for this block |
| 1514 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1515 | if (find_next_key(path, 0, &key) == 0 && |
| 1516 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1517 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1518 | err = -EAGAIN; |
| 1519 | goto out; |
| 1520 | } |
| 1521 | } |
| 1522 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1523 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1524 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1525 | path->keep_locks = 0; |
| 1526 | btrfs_unlock_up_safe(path, 1); |
| 1527 | } |
| 1528 | return err; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * helper to add new inline back ref |
| 1533 | */ |
| 1534 | static noinline_for_stack |
| 1535 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1536 | struct btrfs_root *root, |
| 1537 | struct btrfs_path *path, |
| 1538 | struct btrfs_extent_inline_ref *iref, |
| 1539 | u64 parent, u64 root_objectid, |
| 1540 | u64 owner, u64 offset, int refs_to_add, |
| 1541 | struct btrfs_delayed_extent_op *extent_op) |
| 1542 | { |
| 1543 | struct extent_buffer *leaf; |
| 1544 | struct btrfs_extent_item *ei; |
| 1545 | unsigned long ptr; |
| 1546 | unsigned long end; |
| 1547 | unsigned long item_offset; |
| 1548 | u64 refs; |
| 1549 | int size; |
| 1550 | int type; |
| 1551 | int ret; |
| 1552 | |
| 1553 | leaf = path->nodes[0]; |
| 1554 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1555 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1556 | |
| 1557 | type = extent_ref_type(parent, owner); |
| 1558 | size = btrfs_extent_inline_ref_size(type); |
| 1559 | |
| 1560 | ret = btrfs_extend_item(trans, root, path, size); |
| 1561 | BUG_ON(ret); |
| 1562 | |
| 1563 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1564 | refs = btrfs_extent_refs(leaf, ei); |
| 1565 | refs += refs_to_add; |
| 1566 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1567 | if (extent_op) |
| 1568 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1569 | |
| 1570 | ptr = (unsigned long)ei + item_offset; |
| 1571 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1572 | if (ptr < end - size) |
| 1573 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1574 | end - size - ptr); |
| 1575 | |
| 1576 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1577 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1578 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1579 | struct btrfs_extent_data_ref *dref; |
| 1580 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1581 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1582 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1583 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1584 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1585 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1586 | struct btrfs_shared_data_ref *sref; |
| 1587 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1588 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1589 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1590 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1591 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1592 | } else { |
| 1593 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1594 | } |
| 1595 | btrfs_mark_buffer_dirty(leaf); |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
| 1599 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1600 | struct btrfs_root *root, |
| 1601 | struct btrfs_path *path, |
| 1602 | struct btrfs_extent_inline_ref **ref_ret, |
| 1603 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1604 | u64 root_objectid, u64 owner, u64 offset) |
| 1605 | { |
| 1606 | int ret; |
| 1607 | |
| 1608 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1609 | bytenr, num_bytes, parent, |
| 1610 | root_objectid, owner, offset, 0); |
| 1611 | if (ret != -ENOENT) |
| 1612 | return ret; |
| 1613 | |
| 1614 | btrfs_release_path(root, path); |
| 1615 | *ref_ret = NULL; |
| 1616 | |
| 1617 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1618 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1619 | root_objectid); |
| 1620 | } else { |
| 1621 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1622 | root_objectid, owner, offset); |
| 1623 | } |
| 1624 | return ret; |
| 1625 | } |
| 1626 | |
| 1627 | /* |
| 1628 | * helper to update/remove inline back ref |
| 1629 | */ |
| 1630 | static noinline_for_stack |
| 1631 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1632 | struct btrfs_root *root, |
| 1633 | struct btrfs_path *path, |
| 1634 | struct btrfs_extent_inline_ref *iref, |
| 1635 | int refs_to_mod, |
| 1636 | struct btrfs_delayed_extent_op *extent_op) |
| 1637 | { |
| 1638 | struct extent_buffer *leaf; |
| 1639 | struct btrfs_extent_item *ei; |
| 1640 | struct btrfs_extent_data_ref *dref = NULL; |
| 1641 | struct btrfs_shared_data_ref *sref = NULL; |
| 1642 | unsigned long ptr; |
| 1643 | unsigned long end; |
| 1644 | u32 item_size; |
| 1645 | int size; |
| 1646 | int type; |
| 1647 | int ret; |
| 1648 | u64 refs; |
| 1649 | |
| 1650 | leaf = path->nodes[0]; |
| 1651 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1652 | refs = btrfs_extent_refs(leaf, ei); |
| 1653 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1654 | refs += refs_to_mod; |
| 1655 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1656 | if (extent_op) |
| 1657 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1658 | |
| 1659 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1660 | |
| 1661 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1662 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1663 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1664 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1665 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1666 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1667 | } else { |
| 1668 | refs = 1; |
| 1669 | BUG_ON(refs_to_mod != -1); |
| 1670 | } |
| 1671 | |
| 1672 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1673 | refs += refs_to_mod; |
| 1674 | |
| 1675 | if (refs > 0) { |
| 1676 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1677 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1678 | else |
| 1679 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1680 | } else { |
| 1681 | size = btrfs_extent_inline_ref_size(type); |
| 1682 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1683 | ptr = (unsigned long)iref; |
| 1684 | end = (unsigned long)ei + item_size; |
| 1685 | if (ptr + size < end) |
| 1686 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1687 | end - ptr - size); |
| 1688 | item_size -= size; |
| 1689 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
| 1690 | BUG_ON(ret); |
| 1691 | } |
| 1692 | btrfs_mark_buffer_dirty(leaf); |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
| 1696 | static noinline_for_stack |
| 1697 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1698 | struct btrfs_root *root, |
| 1699 | struct btrfs_path *path, |
| 1700 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1701 | u64 root_objectid, u64 owner, |
| 1702 | u64 offset, int refs_to_add, |
| 1703 | struct btrfs_delayed_extent_op *extent_op) |
| 1704 | { |
| 1705 | struct btrfs_extent_inline_ref *iref; |
| 1706 | int ret; |
| 1707 | |
| 1708 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1709 | bytenr, num_bytes, parent, |
| 1710 | root_objectid, owner, offset, 1); |
| 1711 | if (ret == 0) { |
| 1712 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1713 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1714 | refs_to_add, extent_op); |
| 1715 | } else if (ret == -ENOENT) { |
| 1716 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1717 | parent, root_objectid, |
| 1718 | owner, offset, refs_to_add, |
| 1719 | extent_op); |
| 1720 | } |
| 1721 | return ret; |
| 1722 | } |
| 1723 | |
| 1724 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1725 | struct btrfs_root *root, |
| 1726 | struct btrfs_path *path, |
| 1727 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1728 | u64 owner, u64 offset, int refs_to_add) |
| 1729 | { |
| 1730 | int ret; |
| 1731 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1732 | BUG_ON(refs_to_add != 1); |
| 1733 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1734 | parent, root_objectid); |
| 1735 | } else { |
| 1736 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1737 | parent, root_objectid, |
| 1738 | owner, offset, refs_to_add); |
| 1739 | } |
| 1740 | return ret; |
| 1741 | } |
| 1742 | |
| 1743 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1744 | struct btrfs_root *root, |
| 1745 | struct btrfs_path *path, |
| 1746 | struct btrfs_extent_inline_ref *iref, |
| 1747 | int refs_to_drop, int is_data) |
| 1748 | { |
| 1749 | int ret; |
| 1750 | |
| 1751 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1752 | if (iref) { |
| 1753 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1754 | -refs_to_drop, NULL); |
| 1755 | } else if (is_data) { |
| 1756 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1757 | } else { |
| 1758 | ret = btrfs_del_item(trans, root, path); |
| 1759 | } |
| 1760 | return ret; |
| 1761 | } |
| 1762 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1763 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1764 | u64 start, u64 len) |
| 1765 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1766 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1767 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1768 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1769 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1770 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1771 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1772 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1773 | u64 discarded_bytes = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1774 | struct btrfs_multi_bio *multi = NULL; |
| 1775 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1776 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1777 | /* Tell the block device(s) that the sectors can be discarded */ |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1778 | ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, |
| 1779 | bytenr, &num_bytes, &multi, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1780 | if (!ret) { |
| 1781 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1782 | int i; |
| 1783 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1784 | |
| 1785 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1786 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1787 | stripe->physical, |
| 1788 | stripe->length); |
| 1789 | if (!ret) |
| 1790 | discarded_bytes += stripe->length; |
| 1791 | else if (ret != -EOPNOTSUPP) |
| 1792 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1793 | } |
| 1794 | kfree(multi); |
| 1795 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1796 | if (discarded_bytes && ret == -EOPNOTSUPP) |
| 1797 | ret = 0; |
| 1798 | |
| 1799 | if (actual_bytes) |
| 1800 | *actual_bytes = discarded_bytes; |
| 1801 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1802 | |
| 1803 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1804 | } |
| 1805 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1806 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1807 | struct btrfs_root *root, |
| 1808 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1809 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1810 | { |
| 1811 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1812 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1813 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1814 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1815 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1816 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1817 | parent, root_objectid, (int)owner, |
| 1818 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1819 | } else { |
| 1820 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1821 | parent, root_objectid, owner, offset, |
| 1822 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1823 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1824 | return ret; |
| 1825 | } |
| 1826 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1827 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1828 | struct btrfs_root *root, |
| 1829 | u64 bytenr, u64 num_bytes, |
| 1830 | u64 parent, u64 root_objectid, |
| 1831 | u64 owner, u64 offset, int refs_to_add, |
| 1832 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1833 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1834 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1835 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1836 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1837 | u64 refs; |
| 1838 | int ret; |
| 1839 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1840 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1841 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1842 | if (!path) |
| 1843 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1844 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1845 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1846 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1847 | /* this will setup the path even if it fails to insert the back ref */ |
| 1848 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1849 | path, bytenr, num_bytes, parent, |
| 1850 | root_objectid, owner, offset, |
| 1851 | refs_to_add, extent_op); |
| 1852 | if (ret == 0) |
| 1853 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1854 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1855 | if (ret != -EAGAIN) { |
| 1856 | err = ret; |
| 1857 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1858 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1859 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1860 | leaf = path->nodes[0]; |
| 1861 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1862 | refs = btrfs_extent_refs(leaf, item); |
| 1863 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1864 | if (extent_op) |
| 1865 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1866 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1867 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1868 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1869 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1870 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1871 | path->leave_spinning = 1; |
| 1872 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1873 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1874 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1875 | path, bytenr, parent, root_objectid, |
| 1876 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1877 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1878 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1879 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1880 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1881 | } |
| 1882 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1883 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1884 | struct btrfs_root *root, |
| 1885 | struct btrfs_delayed_ref_node *node, |
| 1886 | struct btrfs_delayed_extent_op *extent_op, |
| 1887 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1888 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1889 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1890 | struct btrfs_delayed_data_ref *ref; |
| 1891 | struct btrfs_key ins; |
| 1892 | u64 parent = 0; |
| 1893 | u64 ref_root = 0; |
| 1894 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1895 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1896 | ins.objectid = node->bytenr; |
| 1897 | ins.offset = node->num_bytes; |
| 1898 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1899 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1900 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1901 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1902 | parent = ref->parent; |
| 1903 | else |
| 1904 | ref_root = ref->root; |
| 1905 | |
| 1906 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1907 | if (extent_op) { |
| 1908 | BUG_ON(extent_op->update_key); |
| 1909 | flags |= extent_op->flags_to_set; |
| 1910 | } |
| 1911 | ret = alloc_reserved_file_extent(trans, root, |
| 1912 | parent, ref_root, flags, |
| 1913 | ref->objectid, ref->offset, |
| 1914 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1915 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1916 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1917 | node->num_bytes, parent, |
| 1918 | ref_root, ref->objectid, |
| 1919 | ref->offset, node->ref_mod, |
| 1920 | extent_op); |
| 1921 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1922 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1923 | node->num_bytes, parent, |
| 1924 | ref_root, ref->objectid, |
| 1925 | ref->offset, node->ref_mod, |
| 1926 | extent_op); |
| 1927 | } else { |
| 1928 | BUG(); |
| 1929 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1930 | return ret; |
| 1931 | } |
| 1932 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1933 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1934 | struct extent_buffer *leaf, |
| 1935 | struct btrfs_extent_item *ei) |
| 1936 | { |
| 1937 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1938 | if (extent_op->update_flags) { |
| 1939 | flags |= extent_op->flags_to_set; |
| 1940 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1941 | } |
| 1942 | |
| 1943 | if (extent_op->update_key) { |
| 1944 | struct btrfs_tree_block_info *bi; |
| 1945 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1946 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1947 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1952 | struct btrfs_root *root, |
| 1953 | struct btrfs_delayed_ref_node *node, |
| 1954 | struct btrfs_delayed_extent_op *extent_op) |
| 1955 | { |
| 1956 | struct btrfs_key key; |
| 1957 | struct btrfs_path *path; |
| 1958 | struct btrfs_extent_item *ei; |
| 1959 | struct extent_buffer *leaf; |
| 1960 | u32 item_size; |
| 1961 | int ret; |
| 1962 | int err = 0; |
| 1963 | |
| 1964 | path = btrfs_alloc_path(); |
| 1965 | if (!path) |
| 1966 | return -ENOMEM; |
| 1967 | |
| 1968 | key.objectid = node->bytenr; |
| 1969 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1970 | key.offset = node->num_bytes; |
| 1971 | |
| 1972 | path->reada = 1; |
| 1973 | path->leave_spinning = 1; |
| 1974 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1975 | path, 0, 1); |
| 1976 | if (ret < 0) { |
| 1977 | err = ret; |
| 1978 | goto out; |
| 1979 | } |
| 1980 | if (ret > 0) { |
| 1981 | err = -EIO; |
| 1982 | goto out; |
| 1983 | } |
| 1984 | |
| 1985 | leaf = path->nodes[0]; |
| 1986 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1987 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1988 | if (item_size < sizeof(*ei)) { |
| 1989 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1990 | path, (u64)-1, 0); |
| 1991 | if (ret < 0) { |
| 1992 | err = ret; |
| 1993 | goto out; |
| 1994 | } |
| 1995 | leaf = path->nodes[0]; |
| 1996 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1997 | } |
| 1998 | #endif |
| 1999 | BUG_ON(item_size < sizeof(*ei)); |
| 2000 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2001 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2002 | |
| 2003 | btrfs_mark_buffer_dirty(leaf); |
| 2004 | out: |
| 2005 | btrfs_free_path(path); |
| 2006 | return err; |
| 2007 | } |
| 2008 | |
| 2009 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2010 | struct btrfs_root *root, |
| 2011 | struct btrfs_delayed_ref_node *node, |
| 2012 | struct btrfs_delayed_extent_op *extent_op, |
| 2013 | int insert_reserved) |
| 2014 | { |
| 2015 | int ret = 0; |
| 2016 | struct btrfs_delayed_tree_ref *ref; |
| 2017 | struct btrfs_key ins; |
| 2018 | u64 parent = 0; |
| 2019 | u64 ref_root = 0; |
| 2020 | |
| 2021 | ins.objectid = node->bytenr; |
| 2022 | ins.offset = node->num_bytes; |
| 2023 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2024 | |
| 2025 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 2026 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2027 | parent = ref->parent; |
| 2028 | else |
| 2029 | ref_root = ref->root; |
| 2030 | |
| 2031 | BUG_ON(node->ref_mod != 1); |
| 2032 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 2033 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 2034 | !extent_op->update_key); |
| 2035 | ret = alloc_reserved_tree_block(trans, root, |
| 2036 | parent, ref_root, |
| 2037 | extent_op->flags_to_set, |
| 2038 | &extent_op->key, |
| 2039 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2040 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2041 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2042 | node->num_bytes, parent, ref_root, |
| 2043 | ref->level, 0, 1, extent_op); |
| 2044 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2045 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2046 | node->num_bytes, parent, ref_root, |
| 2047 | ref->level, 0, 1, extent_op); |
| 2048 | } else { |
| 2049 | BUG(); |
| 2050 | } |
| 2051 | return ret; |
| 2052 | } |
| 2053 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2054 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2055 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2056 | struct btrfs_root *root, |
| 2057 | struct btrfs_delayed_ref_node *node, |
| 2058 | struct btrfs_delayed_extent_op *extent_op, |
| 2059 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2060 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2061 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2062 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2063 | struct btrfs_delayed_ref_head *head; |
| 2064 | /* |
| 2065 | * we've hit the end of the chain and we were supposed |
| 2066 | * to insert this extent into the tree. But, it got |
| 2067 | * deleted before we ever needed to insert it, so all |
| 2068 | * we have to do is clean up the accounting |
| 2069 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2070 | BUG_ON(extent_op); |
| 2071 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2072 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2073 | btrfs_pin_extent(root, node->bytenr, |
| 2074 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2075 | if (head->is_data) { |
| 2076 | ret = btrfs_del_csums(trans, root, |
| 2077 | node->bytenr, |
| 2078 | node->num_bytes); |
| 2079 | BUG_ON(ret); |
| 2080 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2081 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2082 | mutex_unlock(&head->mutex); |
| 2083 | return 0; |
| 2084 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2085 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2086 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2087 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2088 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2089 | insert_reserved); |
| 2090 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2091 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2092 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2093 | insert_reserved); |
| 2094 | else |
| 2095 | BUG(); |
| 2096 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2097 | } |
| 2098 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2099 | static noinline struct btrfs_delayed_ref_node * |
| 2100 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2101 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2102 | struct rb_node *node; |
| 2103 | struct btrfs_delayed_ref_node *ref; |
| 2104 | int action = BTRFS_ADD_DELAYED_REF; |
| 2105 | again: |
| 2106 | /* |
| 2107 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2108 | * this prevents ref count from going down to zero when |
| 2109 | * there still are pending delayed ref. |
| 2110 | */ |
| 2111 | node = rb_prev(&head->node.rb_node); |
| 2112 | while (1) { |
| 2113 | if (!node) |
| 2114 | break; |
| 2115 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2116 | rb_node); |
| 2117 | if (ref->bytenr != head->node.bytenr) |
| 2118 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2119 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2120 | return ref; |
| 2121 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2122 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2123 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2124 | action = BTRFS_DROP_DELAYED_REF; |
| 2125 | goto again; |
| 2126 | } |
| 2127 | return NULL; |
| 2128 | } |
| 2129 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2130 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2131 | struct btrfs_root *root, |
| 2132 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2133 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2134 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2135 | struct btrfs_delayed_ref_node *ref; |
| 2136 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2137 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2138 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2139 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2140 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2141 | |
| 2142 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2143 | while (1) { |
| 2144 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2145 | /* pick a new head ref from the cluster list */ |
| 2146 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2147 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2148 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2149 | locked_ref = list_entry(cluster->next, |
| 2150 | struct btrfs_delayed_ref_head, cluster); |
| 2151 | |
| 2152 | /* grab the lock that says we are going to process |
| 2153 | * all the refs for this head */ |
| 2154 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2155 | |
| 2156 | /* |
| 2157 | * we may have dropped the spin lock to get the head |
| 2158 | * mutex lock, and that might have given someone else |
| 2159 | * time to free the head. If that's true, it has been |
| 2160 | * removed from our list and we can move on. |
| 2161 | */ |
| 2162 | if (ret == -EAGAIN) { |
| 2163 | locked_ref = NULL; |
| 2164 | count++; |
| 2165 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | /* |
| 2170 | * record the must insert reserved flag before we |
| 2171 | * drop the spin lock. |
| 2172 | */ |
| 2173 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2174 | locked_ref->must_insert_reserved = 0; |
| 2175 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2176 | extent_op = locked_ref->extent_op; |
| 2177 | locked_ref->extent_op = NULL; |
| 2178 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2179 | /* |
| 2180 | * locked_ref is the head node, so we have to go one |
| 2181 | * node back for any delayed ref updates |
| 2182 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2183 | ref = select_delayed_ref(locked_ref); |
| 2184 | if (!ref) { |
| 2185 | /* All delayed refs have been processed, Go ahead |
| 2186 | * and send the head node to run_one_delayed_ref, |
| 2187 | * so that any accounting fixes can happen |
| 2188 | */ |
| 2189 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2190 | |
| 2191 | if (extent_op && must_insert_reserved) { |
| 2192 | kfree(extent_op); |
| 2193 | extent_op = NULL; |
| 2194 | } |
| 2195 | |
| 2196 | if (extent_op) { |
| 2197 | spin_unlock(&delayed_refs->lock); |
| 2198 | |
| 2199 | ret = run_delayed_extent_op(trans, root, |
| 2200 | ref, extent_op); |
| 2201 | BUG_ON(ret); |
| 2202 | kfree(extent_op); |
| 2203 | |
| 2204 | cond_resched(); |
| 2205 | spin_lock(&delayed_refs->lock); |
| 2206 | continue; |
| 2207 | } |
| 2208 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2209 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2210 | locked_ref = NULL; |
| 2211 | } |
| 2212 | |
| 2213 | ref->in_tree = 0; |
| 2214 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2215 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2216 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2217 | spin_unlock(&delayed_refs->lock); |
| 2218 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2219 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2220 | must_insert_reserved); |
| 2221 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2222 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2223 | btrfs_put_delayed_ref(ref); |
| 2224 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2225 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2226 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2227 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2228 | spin_lock(&delayed_refs->lock); |
| 2229 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2230 | return count; |
| 2231 | } |
| 2232 | |
| 2233 | /* |
| 2234 | * this starts processing the delayed reference count updates and |
| 2235 | * extent insertions we have queued up so far. count can be |
| 2236 | * 0, which means to process everything in the tree at the start |
| 2237 | * of the run (but not newly added entries), or it can be some target |
| 2238 | * number you'd like to process. |
| 2239 | */ |
| 2240 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2241 | struct btrfs_root *root, unsigned long count) |
| 2242 | { |
| 2243 | struct rb_node *node; |
| 2244 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2245 | struct btrfs_delayed_ref_node *ref; |
| 2246 | struct list_head cluster; |
| 2247 | int ret; |
| 2248 | int run_all = count == (unsigned long)-1; |
| 2249 | int run_most = 0; |
| 2250 | |
| 2251 | if (root == root->fs_info->extent_root) |
| 2252 | root = root->fs_info->tree_root; |
| 2253 | |
| 2254 | delayed_refs = &trans->transaction->delayed_refs; |
| 2255 | INIT_LIST_HEAD(&cluster); |
| 2256 | again: |
| 2257 | spin_lock(&delayed_refs->lock); |
| 2258 | if (count == 0) { |
| 2259 | count = delayed_refs->num_entries * 2; |
| 2260 | run_most = 1; |
| 2261 | } |
| 2262 | while (1) { |
| 2263 | if (!(run_all || run_most) && |
| 2264 | delayed_refs->num_heads_ready < 64) |
| 2265 | break; |
| 2266 | |
| 2267 | /* |
| 2268 | * go find something we can process in the rbtree. We start at |
| 2269 | * the beginning of the tree, and then build a cluster |
| 2270 | * of refs to process starting at the first one we are able to |
| 2271 | * lock |
| 2272 | */ |
| 2273 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2274 | delayed_refs->run_delayed_start); |
| 2275 | if (ret) |
| 2276 | break; |
| 2277 | |
| 2278 | ret = run_clustered_refs(trans, root, &cluster); |
| 2279 | BUG_ON(ret < 0); |
| 2280 | |
| 2281 | count -= min_t(unsigned long, ret, count); |
| 2282 | |
| 2283 | if (count == 0) |
| 2284 | break; |
| 2285 | } |
| 2286 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2287 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2288 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2289 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2290 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2291 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2292 | |
| 2293 | while (node) { |
| 2294 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2295 | rb_node); |
| 2296 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2297 | struct btrfs_delayed_ref_head *head; |
| 2298 | |
| 2299 | head = btrfs_delayed_node_to_head(ref); |
| 2300 | atomic_inc(&ref->refs); |
| 2301 | |
| 2302 | spin_unlock(&delayed_refs->lock); |
| 2303 | mutex_lock(&head->mutex); |
| 2304 | mutex_unlock(&head->mutex); |
| 2305 | |
| 2306 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2307 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2308 | goto again; |
| 2309 | } |
| 2310 | node = rb_next(node); |
| 2311 | } |
| 2312 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2313 | schedule_timeout(1); |
| 2314 | goto again; |
| 2315 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2316 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2317 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2318 | return 0; |
| 2319 | } |
| 2320 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2321 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2322 | struct btrfs_root *root, |
| 2323 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2324 | int is_data) |
| 2325 | { |
| 2326 | struct btrfs_delayed_extent_op *extent_op; |
| 2327 | int ret; |
| 2328 | |
| 2329 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2330 | if (!extent_op) |
| 2331 | return -ENOMEM; |
| 2332 | |
| 2333 | extent_op->flags_to_set = flags; |
| 2334 | extent_op->update_flags = 1; |
| 2335 | extent_op->update_key = 0; |
| 2336 | extent_op->is_data = is_data ? 1 : 0; |
| 2337 | |
| 2338 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2339 | if (ret) |
| 2340 | kfree(extent_op); |
| 2341 | return ret; |
| 2342 | } |
| 2343 | |
| 2344 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2345 | struct btrfs_root *root, |
| 2346 | struct btrfs_path *path, |
| 2347 | u64 objectid, u64 offset, u64 bytenr) |
| 2348 | { |
| 2349 | struct btrfs_delayed_ref_head *head; |
| 2350 | struct btrfs_delayed_ref_node *ref; |
| 2351 | struct btrfs_delayed_data_ref *data_ref; |
| 2352 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2353 | struct rb_node *node; |
| 2354 | int ret = 0; |
| 2355 | |
| 2356 | ret = -ENOENT; |
| 2357 | delayed_refs = &trans->transaction->delayed_refs; |
| 2358 | spin_lock(&delayed_refs->lock); |
| 2359 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2360 | if (!head) |
| 2361 | goto out; |
| 2362 | |
| 2363 | if (!mutex_trylock(&head->mutex)) { |
| 2364 | atomic_inc(&head->node.refs); |
| 2365 | spin_unlock(&delayed_refs->lock); |
| 2366 | |
| 2367 | btrfs_release_path(root->fs_info->extent_root, path); |
| 2368 | |
| 2369 | mutex_lock(&head->mutex); |
| 2370 | mutex_unlock(&head->mutex); |
| 2371 | btrfs_put_delayed_ref(&head->node); |
| 2372 | return -EAGAIN; |
| 2373 | } |
| 2374 | |
| 2375 | node = rb_prev(&head->node.rb_node); |
| 2376 | if (!node) |
| 2377 | goto out_unlock; |
| 2378 | |
| 2379 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2380 | |
| 2381 | if (ref->bytenr != bytenr) |
| 2382 | goto out_unlock; |
| 2383 | |
| 2384 | ret = 1; |
| 2385 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2386 | goto out_unlock; |
| 2387 | |
| 2388 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2389 | |
| 2390 | node = rb_prev(node); |
| 2391 | if (node) { |
| 2392 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2393 | if (ref->bytenr == bytenr) |
| 2394 | goto out_unlock; |
| 2395 | } |
| 2396 | |
| 2397 | if (data_ref->root != root->root_key.objectid || |
| 2398 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2399 | goto out_unlock; |
| 2400 | |
| 2401 | ret = 0; |
| 2402 | out_unlock: |
| 2403 | mutex_unlock(&head->mutex); |
| 2404 | out: |
| 2405 | spin_unlock(&delayed_refs->lock); |
| 2406 | return ret; |
| 2407 | } |
| 2408 | |
| 2409 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2410 | struct btrfs_root *root, |
| 2411 | struct btrfs_path *path, |
| 2412 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2413 | { |
| 2414 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2415 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2416 | struct btrfs_extent_data_ref *ref; |
| 2417 | struct btrfs_extent_inline_ref *iref; |
| 2418 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2419 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2420 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2421 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2422 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2423 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2424 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2425 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2426 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2427 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2428 | if (ret < 0) |
| 2429 | goto out; |
| 2430 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2431 | |
| 2432 | ret = -ENOENT; |
| 2433 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2434 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2435 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2436 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2437 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2438 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2439 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2440 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2441 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2442 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2443 | ret = 1; |
| 2444 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2445 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2446 | if (item_size < sizeof(*ei)) { |
| 2447 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2448 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2449 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2450 | #endif |
| 2451 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2452 | |
| 2453 | if (item_size != sizeof(*ei) + |
| 2454 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2455 | goto out; |
| 2456 | |
| 2457 | if (btrfs_extent_generation(leaf, ei) <= |
| 2458 | btrfs_root_last_snapshot(&root->root_item)) |
| 2459 | goto out; |
| 2460 | |
| 2461 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2462 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2463 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2464 | goto out; |
| 2465 | |
| 2466 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2467 | if (btrfs_extent_refs(leaf, ei) != |
| 2468 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2469 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2470 | root->root_key.objectid || |
| 2471 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2472 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2473 | goto out; |
| 2474 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2475 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2476 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2477 | return ret; |
| 2478 | } |
| 2479 | |
| 2480 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2481 | struct btrfs_root *root, |
| 2482 | u64 objectid, u64 offset, u64 bytenr) |
| 2483 | { |
| 2484 | struct btrfs_path *path; |
| 2485 | int ret; |
| 2486 | int ret2; |
| 2487 | |
| 2488 | path = btrfs_alloc_path(); |
| 2489 | if (!path) |
| 2490 | return -ENOENT; |
| 2491 | |
| 2492 | do { |
| 2493 | ret = check_committed_ref(trans, root, path, objectid, |
| 2494 | offset, bytenr); |
| 2495 | if (ret && ret != -ENOENT) |
| 2496 | goto out; |
| 2497 | |
| 2498 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2499 | offset, bytenr); |
| 2500 | } while (ret2 == -EAGAIN); |
| 2501 | |
| 2502 | if (ret2 && ret2 != -ENOENT) { |
| 2503 | ret = ret2; |
| 2504 | goto out; |
| 2505 | } |
| 2506 | |
| 2507 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2508 | ret = 0; |
| 2509 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2510 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2511 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2512 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2513 | return ret; |
| 2514 | } |
| 2515 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2516 | #if 0 |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2517 | int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2518 | struct extent_buffer *buf, u32 nr_extents) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2519 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2520 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2521 | struct btrfs_file_extent_item *fi; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2522 | u64 root_gen; |
| 2523 | u32 nritems; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2524 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2525 | int level; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2526 | int ret = 0; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2527 | int shared = 0; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2528 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2529 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2530 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2531 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2532 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 2533 | shared = 0; |
| 2534 | root_gen = root->root_key.offset; |
| 2535 | } else { |
| 2536 | shared = 1; |
| 2537 | root_gen = trans->transid - 1; |
| 2538 | } |
| 2539 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2540 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2541 | nritems = btrfs_header_nritems(buf); |
Chris Mason | 4a09675 | 2008-07-21 10:29:44 -0400 | [diff] [blame] | 2542 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2543 | if (level == 0) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2544 | struct btrfs_leaf_ref *ref; |
| 2545 | struct btrfs_extent_info *info; |
| 2546 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2547 | ref = btrfs_alloc_leaf_ref(root, nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2548 | if (!ref) { |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2549 | ret = -ENOMEM; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2550 | goto out; |
| 2551 | } |
| 2552 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2553 | ref->root_gen = root_gen; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2554 | ref->bytenr = buf->start; |
| 2555 | ref->owner = btrfs_header_owner(buf); |
| 2556 | ref->generation = btrfs_header_generation(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2557 | ref->nritems = nr_extents; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2558 | info = ref->extents; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2559 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2560 | for (i = 0; nr_extents > 0 && i < nritems; i++) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2561 | u64 disk_bytenr; |
| 2562 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2563 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 2564 | continue; |
| 2565 | fi = btrfs_item_ptr(buf, i, |
| 2566 | struct btrfs_file_extent_item); |
| 2567 | if (btrfs_file_extent_type(buf, fi) == |
| 2568 | BTRFS_FILE_EXTENT_INLINE) |
| 2569 | continue; |
| 2570 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2571 | if (disk_bytenr == 0) |
| 2572 | continue; |
| 2573 | |
| 2574 | info->bytenr = disk_bytenr; |
| 2575 | info->num_bytes = |
| 2576 | btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2577 | info->objectid = key.objectid; |
| 2578 | info->offset = key.offset; |
| 2579 | info++; |
| 2580 | } |
| 2581 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 2582 | ret = btrfs_add_leaf_ref(root, ref, shared); |
Yan Zheng | 5b84e8d | 2008-10-09 11:46:19 -0400 | [diff] [blame] | 2583 | if (ret == -EEXIST && shared) { |
| 2584 | struct btrfs_leaf_ref *old; |
| 2585 | old = btrfs_lookup_leaf_ref(root, ref->bytenr); |
| 2586 | BUG_ON(!old); |
| 2587 | btrfs_remove_leaf_ref(root, old); |
| 2588 | btrfs_free_leaf_ref(root, old); |
| 2589 | ret = btrfs_add_leaf_ref(root, ref, shared); |
| 2590 | } |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2591 | WARN_ON(ret); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 2592 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 2593 | } |
| 2594 | out: |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2595 | return ret; |
| 2596 | } |
| 2597 | |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2598 | /* when a block goes through cow, we update the reference counts of |
| 2599 | * everything that block points to. The internal pointers of the block |
| 2600 | * can be in just about any order, and it is likely to have clusters of |
| 2601 | * things that are close together and clusters of things that are not. |
| 2602 | * |
| 2603 | * To help reduce the seeks that come with updating all of these reference |
| 2604 | * counts, sort them by byte number before actual updates are done. |
| 2605 | * |
| 2606 | * struct refsort is used to match byte number to slot in the btree block. |
| 2607 | * we sort based on the byte number and then use the slot to actually |
| 2608 | * find the item. |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 2609 | * |
| 2610 | * struct refsort is smaller than strcut btrfs_item and smaller than |
| 2611 | * struct btrfs_key_ptr. Since we're currently limited to the page size |
| 2612 | * for a btree block, there's no way for a kmalloc of refsorts for a |
| 2613 | * single node to be bigger than a page. |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2614 | */ |
| 2615 | struct refsort { |
| 2616 | u64 bytenr; |
| 2617 | u32 slot; |
| 2618 | }; |
| 2619 | |
| 2620 | /* |
| 2621 | * for passing into sort() |
| 2622 | */ |
| 2623 | static int refsort_cmp(const void *a_void, const void *b_void) |
| 2624 | { |
| 2625 | const struct refsort *a = a_void; |
| 2626 | const struct refsort *b = b_void; |
| 2627 | |
| 2628 | if (a->bytenr < b->bytenr) |
| 2629 | return -1; |
| 2630 | if (a->bytenr > b->bytenr) |
| 2631 | return 1; |
| 2632 | return 0; |
| 2633 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2634 | #endif |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2635 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2636 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2637 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2638 | struct extent_buffer *buf, |
| 2639 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2640 | { |
| 2641 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2642 | u64 num_bytes; |
| 2643 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2644 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2645 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2646 | struct btrfs_key key; |
| 2647 | struct btrfs_file_extent_item *fi; |
| 2648 | int i; |
| 2649 | int level; |
| 2650 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2651 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2652 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2653 | |
| 2654 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2655 | nritems = btrfs_header_nritems(buf); |
| 2656 | level = btrfs_header_level(buf); |
| 2657 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2658 | if (!root->ref_cows && level == 0) |
| 2659 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2660 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2661 | if (inc) |
| 2662 | process_func = btrfs_inc_extent_ref; |
| 2663 | else |
| 2664 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2665 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2666 | if (full_backref) |
| 2667 | parent = buf->start; |
| 2668 | else |
| 2669 | parent = 0; |
| 2670 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2671 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2672 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2673 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2674 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2675 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2676 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2677 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2678 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2679 | BTRFS_FILE_EXTENT_INLINE) |
| 2680 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2681 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2682 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2683 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2684 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2685 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2686 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2687 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2688 | parent, ref_root, key.objectid, |
| 2689 | key.offset); |
| 2690 | if (ret) |
| 2691 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2692 | } else { |
| 2693 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2694 | num_bytes = btrfs_level_size(root, level - 1); |
| 2695 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2696 | parent, ref_root, level - 1, 0); |
| 2697 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2698 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2699 | } |
| 2700 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2701 | return 0; |
| 2702 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2703 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2704 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2705 | } |
| 2706 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2707 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2708 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2709 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2710 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2711 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2712 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2713 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2714 | struct extent_buffer *buf, int full_backref) |
| 2715 | { |
| 2716 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2717 | } |
| 2718 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2719 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2720 | struct btrfs_root *root, |
| 2721 | struct btrfs_path *path, |
| 2722 | struct btrfs_block_group_cache *cache) |
| 2723 | { |
| 2724 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2725 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2726 | unsigned long bi; |
| 2727 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2728 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2729 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2730 | if (ret < 0) |
| 2731 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2732 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2733 | |
| 2734 | leaf = path->nodes[0]; |
| 2735 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2736 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2737 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2738 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2739 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2740 | if (ret) |
| 2741 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2742 | return 0; |
| 2743 | |
| 2744 | } |
| 2745 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2746 | static struct btrfs_block_group_cache * |
| 2747 | next_block_group(struct btrfs_root *root, |
| 2748 | struct btrfs_block_group_cache *cache) |
| 2749 | { |
| 2750 | struct rb_node *node; |
| 2751 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2752 | node = rb_next(&cache->cache_node); |
| 2753 | btrfs_put_block_group(cache); |
| 2754 | if (node) { |
| 2755 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2756 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2757 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2758 | } else |
| 2759 | cache = NULL; |
| 2760 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2761 | return cache; |
| 2762 | } |
| 2763 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2764 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 2765 | struct btrfs_trans_handle *trans, |
| 2766 | struct btrfs_path *path) |
| 2767 | { |
| 2768 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 2769 | struct inode *inode = NULL; |
| 2770 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2771 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2772 | int num_pages = 0; |
| 2773 | int retries = 0; |
| 2774 | int ret = 0; |
| 2775 | |
| 2776 | /* |
| 2777 | * If this block group is smaller than 100 megs don't bother caching the |
| 2778 | * block group. |
| 2779 | */ |
| 2780 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 2781 | spin_lock(&block_group->lock); |
| 2782 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2783 | spin_unlock(&block_group->lock); |
| 2784 | return 0; |
| 2785 | } |
| 2786 | |
| 2787 | again: |
| 2788 | inode = lookup_free_space_inode(root, block_group, path); |
| 2789 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 2790 | ret = PTR_ERR(inode); |
| 2791 | btrfs_release_path(root, path); |
| 2792 | goto out; |
| 2793 | } |
| 2794 | |
| 2795 | if (IS_ERR(inode)) { |
| 2796 | BUG_ON(retries); |
| 2797 | retries++; |
| 2798 | |
| 2799 | if (block_group->ro) |
| 2800 | goto out_free; |
| 2801 | |
| 2802 | ret = create_free_space_inode(root, trans, block_group, path); |
| 2803 | if (ret) |
| 2804 | goto out_free; |
| 2805 | goto again; |
| 2806 | } |
| 2807 | |
| 2808 | /* |
| 2809 | * We want to set the generation to 0, that way if anything goes wrong |
| 2810 | * from here on out we know not to trust this cache when we load up next |
| 2811 | * time. |
| 2812 | */ |
| 2813 | BTRFS_I(inode)->generation = 0; |
| 2814 | ret = btrfs_update_inode(trans, root, inode); |
| 2815 | WARN_ON(ret); |
| 2816 | |
| 2817 | if (i_size_read(inode) > 0) { |
| 2818 | ret = btrfs_truncate_free_space_cache(root, trans, path, |
| 2819 | inode); |
| 2820 | if (ret) |
| 2821 | goto out_put; |
| 2822 | } |
| 2823 | |
| 2824 | spin_lock(&block_group->lock); |
| 2825 | if (block_group->cached != BTRFS_CACHE_FINISHED) { |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2826 | /* We're not cached, don't bother trying to write stuff out */ |
| 2827 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2828 | spin_unlock(&block_group->lock); |
| 2829 | goto out_put; |
| 2830 | } |
| 2831 | spin_unlock(&block_group->lock); |
| 2832 | |
| 2833 | num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024); |
| 2834 | if (!num_pages) |
| 2835 | num_pages = 1; |
| 2836 | |
| 2837 | /* |
| 2838 | * Just to make absolutely sure we have enough space, we're going to |
| 2839 | * preallocate 12 pages worth of space for each block group. In |
| 2840 | * practice we ought to use at most 8, but we need extra space so we can |
| 2841 | * add our header and have a terminator between the extents and the |
| 2842 | * bitmaps. |
| 2843 | */ |
| 2844 | num_pages *= 16; |
| 2845 | num_pages *= PAGE_CACHE_SIZE; |
| 2846 | |
| 2847 | ret = btrfs_check_data_free_space(inode, num_pages); |
| 2848 | if (ret) |
| 2849 | goto out_put; |
| 2850 | |
| 2851 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 2852 | num_pages, num_pages, |
| 2853 | &alloc_hint); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2854 | if (!ret) |
| 2855 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2856 | btrfs_free_reserved_data_space(inode, num_pages); |
| 2857 | out_put: |
| 2858 | iput(inode); |
| 2859 | out_free: |
| 2860 | btrfs_release_path(root, path); |
| 2861 | out: |
| 2862 | spin_lock(&block_group->lock); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2863 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2864 | spin_unlock(&block_group->lock); |
| 2865 | |
| 2866 | return ret; |
| 2867 | } |
| 2868 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2869 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2870 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2871 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2872 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2873 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2874 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2875 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2876 | |
| 2877 | path = btrfs_alloc_path(); |
| 2878 | if (!path) |
| 2879 | return -ENOMEM; |
| 2880 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2881 | again: |
| 2882 | while (1) { |
| 2883 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2884 | while (cache) { |
| 2885 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 2886 | break; |
| 2887 | cache = next_block_group(root, cache); |
| 2888 | } |
| 2889 | if (!cache) { |
| 2890 | if (last == 0) |
| 2891 | break; |
| 2892 | last = 0; |
| 2893 | continue; |
| 2894 | } |
| 2895 | err = cache_save_setup(cache, trans, path); |
| 2896 | last = cache->key.objectid + cache->key.offset; |
| 2897 | btrfs_put_block_group(cache); |
| 2898 | } |
| 2899 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2900 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2901 | if (last == 0) { |
| 2902 | err = btrfs_run_delayed_refs(trans, root, |
| 2903 | (unsigned long)-1); |
| 2904 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2905 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2906 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2907 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2908 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2909 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 2910 | btrfs_put_block_group(cache); |
| 2911 | goto again; |
| 2912 | } |
| 2913 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2914 | if (cache->dirty) |
| 2915 | break; |
| 2916 | cache = next_block_group(root, cache); |
| 2917 | } |
| 2918 | if (!cache) { |
| 2919 | if (last == 0) |
| 2920 | break; |
| 2921 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2922 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2923 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2924 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2925 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 2926 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2927 | cache->dirty = 0; |
| 2928 | last = cache->key.objectid + cache->key.offset; |
| 2929 | |
| 2930 | err = write_one_cache_group(trans, root, path, cache); |
| 2931 | BUG_ON(err); |
| 2932 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2933 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2934 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2935 | while (1) { |
| 2936 | /* |
| 2937 | * I don't think this is needed since we're just marking our |
| 2938 | * preallocated extent as written, but just in case it can't |
| 2939 | * hurt. |
| 2940 | */ |
| 2941 | if (last == 0) { |
| 2942 | err = btrfs_run_delayed_refs(trans, root, |
| 2943 | (unsigned long)-1); |
| 2944 | BUG_ON(err); |
| 2945 | } |
| 2946 | |
| 2947 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2948 | while (cache) { |
| 2949 | /* |
| 2950 | * Really this shouldn't happen, but it could if we |
| 2951 | * couldn't write the entire preallocated extent and |
| 2952 | * splitting the extent resulted in a new block. |
| 2953 | */ |
| 2954 | if (cache->dirty) { |
| 2955 | btrfs_put_block_group(cache); |
| 2956 | goto again; |
| 2957 | } |
| 2958 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2959 | break; |
| 2960 | cache = next_block_group(root, cache); |
| 2961 | } |
| 2962 | if (!cache) { |
| 2963 | if (last == 0) |
| 2964 | break; |
| 2965 | last = 0; |
| 2966 | continue; |
| 2967 | } |
| 2968 | |
| 2969 | btrfs_write_out_cache(root, trans, cache, path); |
| 2970 | |
| 2971 | /* |
| 2972 | * If we didn't have an error then the cache state is still |
| 2973 | * NEED_WRITE, so we can set it to WRITTEN. |
| 2974 | */ |
| 2975 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2976 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2977 | last = cache->key.objectid + cache->key.offset; |
| 2978 | btrfs_put_block_group(cache); |
| 2979 | } |
| 2980 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2981 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2982 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2983 | } |
| 2984 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2985 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2986 | { |
| 2987 | struct btrfs_block_group_cache *block_group; |
| 2988 | int readonly = 0; |
| 2989 | |
| 2990 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2991 | if (!block_group || block_group->ro) |
| 2992 | readonly = 1; |
| 2993 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2994 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2995 | return readonly; |
| 2996 | } |
| 2997 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2998 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2999 | u64 total_bytes, u64 bytes_used, |
| 3000 | struct btrfs_space_info **space_info) |
| 3001 | { |
| 3002 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3003 | int i; |
| 3004 | int factor; |
| 3005 | |
| 3006 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3007 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3008 | factor = 2; |
| 3009 | else |
| 3010 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3011 | |
| 3012 | found = __find_space_info(info, flags); |
| 3013 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3014 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3015 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3016 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3017 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3018 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3019 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3020 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3021 | *space_info = found; |
| 3022 | return 0; |
| 3023 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3024 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3025 | if (!found) |
| 3026 | return -ENOMEM; |
| 3027 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3028 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 3029 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3030 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3031 | spin_lock_init(&found->lock); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3032 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
| 3033 | BTRFS_BLOCK_GROUP_SYSTEM | |
| 3034 | BTRFS_BLOCK_GROUP_METADATA); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3035 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3036 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3037 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3038 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3039 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3040 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3041 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3042 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3043 | found->full = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3044 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3045 | found->chunk_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3046 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3047 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 3048 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3049 | return 0; |
| 3050 | } |
| 3051 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3052 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3053 | { |
| 3054 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3055 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 3056 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3057 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3058 | if (extra_flags) { |
| 3059 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3060 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 3061 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3062 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 3063 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3064 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 3065 | } |
| 3066 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3067 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3068 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3069 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 3070 | /* |
| 3071 | * we add in the count of missing devices because we want |
| 3072 | * to make sure that any RAID levels on a degraded FS |
| 3073 | * continue to be honored. |
| 3074 | */ |
| 3075 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 3076 | root->fs_info->fs_devices->missing_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3077 | |
| 3078 | if (num_devices == 1) |
| 3079 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 3080 | if (num_devices < 4) |
| 3081 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 3082 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3083 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 3084 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3085 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3086 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3087 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3088 | |
| 3089 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3090 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3091 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3092 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3093 | |
| 3094 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 3095 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 3096 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 3097 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 3098 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 3099 | return flags; |
| 3100 | } |
| 3101 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3102 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3103 | { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3104 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3105 | flags |= root->fs_info->avail_data_alloc_bits & |
| 3106 | root->fs_info->data_alloc_profile; |
| 3107 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3108 | flags |= root->fs_info->avail_system_alloc_bits & |
| 3109 | root->fs_info->system_alloc_profile; |
| 3110 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3111 | flags |= root->fs_info->avail_metadata_alloc_bits & |
| 3112 | root->fs_info->metadata_alloc_profile; |
| 3113 | return btrfs_reduce_alloc_profile(root, flags); |
| 3114 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3115 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3116 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3117 | { |
| 3118 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3119 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3120 | if (data) |
| 3121 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3122 | else if (root == root->fs_info->chunk_root) |
| 3123 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3124 | else |
| 3125 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3126 | |
| 3127 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 3131 | { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3132 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3133 | BTRFS_BLOCK_GROUP_DATA); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3134 | } |
| 3135 | |
| 3136 | /* |
| 3137 | * This will check the space that the inode allocates from to make sure we have |
| 3138 | * enough space for bytes. |
| 3139 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3140 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3141 | { |
| 3142 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3143 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3144 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3145 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3146 | |
| 3147 | /* make sure bytes are sectorsize aligned */ |
| 3148 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3149 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3150 | if (root == root->fs_info->tree_root) { |
| 3151 | alloc_chunk = 0; |
| 3152 | committed = 1; |
| 3153 | } |
| 3154 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3155 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3156 | if (!data_sinfo) |
| 3157 | goto alloc; |
| 3158 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3159 | again: |
| 3160 | /* make sure we have enough space to handle the data first */ |
| 3161 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3162 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3163 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3164 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3165 | |
| 3166 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3167 | struct btrfs_trans_handle *trans; |
| 3168 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3169 | /* |
| 3170 | * if we don't have enough free bytes in this space then we need |
| 3171 | * to alloc a new chunk. |
| 3172 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3173 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3174 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3175 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3176 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3177 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3178 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3179 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3180 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3181 | if (IS_ERR(trans)) |
| 3182 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3183 | |
| 3184 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3185 | bytes + 2 * 1024 * 1024, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3186 | alloc_target, |
| 3187 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3188 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3189 | if (ret < 0) { |
| 3190 | if (ret != -ENOSPC) |
| 3191 | return ret; |
| 3192 | else |
| 3193 | goto commit_trans; |
| 3194 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3195 | |
| 3196 | if (!data_sinfo) { |
| 3197 | btrfs_set_inode_space_info(root, inode); |
| 3198 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3199 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3200 | goto again; |
| 3201 | } |
| 3202 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3203 | |
| 3204 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3205 | commit_trans: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3206 | if (!committed && |
| 3207 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3208 | committed = 1; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3209 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3210 | if (IS_ERR(trans)) |
| 3211 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3212 | ret = btrfs_commit_transaction(trans, root); |
| 3213 | if (ret) |
| 3214 | return ret; |
| 3215 | goto again; |
| 3216 | } |
| 3217 | |
Chris Mason | 933b585 | 2010-05-26 11:31:00 -0400 | [diff] [blame] | 3218 | #if 0 /* I hope we never need this code again, just in case */ |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3219 | printk(KERN_ERR "no space left, need %llu, %llu bytes_used, " |
| 3220 | "%llu bytes_reserved, " "%llu bytes_pinned, " |
| 3221 | "%llu bytes_readonly, %llu may use %llu total\n", |
| 3222 | (unsigned long long)bytes, |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 3223 | (unsigned long long)data_sinfo->bytes_used, |
| 3224 | (unsigned long long)data_sinfo->bytes_reserved, |
| 3225 | (unsigned long long)data_sinfo->bytes_pinned, |
| 3226 | (unsigned long long)data_sinfo->bytes_readonly, |
| 3227 | (unsigned long long)data_sinfo->bytes_may_use, |
| 3228 | (unsigned long long)data_sinfo->total_bytes); |
Chris Mason | 933b585 | 2010-05-26 11:31:00 -0400 | [diff] [blame] | 3229 | #endif |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3230 | return -ENOSPC; |
| 3231 | } |
| 3232 | data_sinfo->bytes_may_use += bytes; |
| 3233 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 3234 | spin_unlock(&data_sinfo->lock); |
| 3235 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3236 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | /* |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3240 | * called when we are clearing an delalloc extent from the |
| 3241 | * inode's io_tree or there was an error for whatever reason |
| 3242 | * after calling btrfs_check_data_free_space |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3243 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3244 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3245 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3246 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3247 | struct btrfs_space_info *data_sinfo; |
| 3248 | |
| 3249 | /* make sure bytes are sectorsize aligned */ |
| 3250 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3251 | |
| 3252 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3253 | spin_lock(&data_sinfo->lock); |
| 3254 | data_sinfo->bytes_may_use -= bytes; |
| 3255 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3256 | spin_unlock(&data_sinfo->lock); |
| 3257 | } |
| 3258 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3259 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3260 | { |
| 3261 | struct list_head *head = &info->space_info; |
| 3262 | struct btrfs_space_info *found; |
| 3263 | |
| 3264 | rcu_read_lock(); |
| 3265 | list_for_each_entry_rcu(found, head, list) { |
| 3266 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3267 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3268 | } |
| 3269 | rcu_read_unlock(); |
| 3270 | } |
| 3271 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3272 | static int should_alloc_chunk(struct btrfs_root *root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3273 | struct btrfs_space_info *sinfo, u64 alloc_bytes, |
| 3274 | int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3275 | { |
| 3276 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3277 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3278 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3279 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3280 | if (force == CHUNK_ALLOC_FORCE) |
| 3281 | return 1; |
| 3282 | |
| 3283 | /* |
| 3284 | * in limited mode, we want to have some free space up to |
| 3285 | * about 1% of the FS size. |
| 3286 | */ |
| 3287 | if (force == CHUNK_ALLOC_LIMITED) { |
| 3288 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 3289 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 3290 | div_factor_fine(thresh, 1)); |
| 3291 | |
| 3292 | if (num_bytes - num_allocated < thresh) |
| 3293 | return 1; |
| 3294 | } |
| 3295 | |
| 3296 | /* |
| 3297 | * we have two similar checks here, one based on percentage |
| 3298 | * and once based on a hard number of 256MB. The idea |
| 3299 | * is that if we have a good amount of free |
| 3300 | * room, don't allocate a chunk. A good mount is |
| 3301 | * less than 80% utilized of the chunks we have allocated, |
| 3302 | * or more than 256MB free |
| 3303 | */ |
| 3304 | if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3305 | return 0; |
| 3306 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3307 | if (num_allocated + alloc_bytes < div_factor(num_bytes, 8)) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3308 | return 0; |
| 3309 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3310 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3311 | |
| 3312 | /* 256MB or 5% of the FS */ |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3313 | thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5)); |
| 3314 | |
| 3315 | if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3316 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3317 | return 1; |
| 3318 | } |
| 3319 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3320 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3321 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3322 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3323 | { |
| 3324 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3325 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3326 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3327 | int ret = 0; |
| 3328 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3329 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3330 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3331 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3332 | if (!space_info) { |
| 3333 | ret = update_space_info(extent_root->fs_info, flags, |
| 3334 | 0, 0, &space_info); |
| 3335 | BUG_ON(ret); |
| 3336 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3337 | BUG_ON(!space_info); |
| 3338 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3339 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3340 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3341 | if (space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3342 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3343 | if (space_info->full) { |
| 3344 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3345 | return 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3346 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3347 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3348 | if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3349 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3350 | return 0; |
| 3351 | } else if (space_info->chunk_alloc) { |
| 3352 | wait_for_alloc = 1; |
| 3353 | } else { |
| 3354 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3355 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3356 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3357 | spin_unlock(&space_info->lock); |
| 3358 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3359 | mutex_lock(&fs_info->chunk_mutex); |
| 3360 | |
| 3361 | /* |
| 3362 | * The chunk_mutex is held throughout the entirety of a chunk |
| 3363 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 3364 | * other guy is done and we need to recheck and see if we should |
| 3365 | * allocate. |
| 3366 | */ |
| 3367 | if (wait_for_alloc) { |
| 3368 | mutex_unlock(&fs_info->chunk_mutex); |
| 3369 | wait_for_alloc = 0; |
| 3370 | goto again; |
| 3371 | } |
| 3372 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3373 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3374 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3375 | * allocating mixed chunks instead of individual chunks. |
| 3376 | */ |
| 3377 | if (btrfs_mixed_space_info(space_info)) |
| 3378 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3379 | |
| 3380 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3381 | * if we're doing a data chunk, go ahead and make sure that |
| 3382 | * we keep a reasonable number of metadata chunks allocated in the |
| 3383 | * FS as well. |
| 3384 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3385 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3386 | fs_info->data_chunk_allocations++; |
| 3387 | if (!(fs_info->data_chunk_allocations % |
| 3388 | fs_info->metadata_ratio)) |
| 3389 | force_metadata_allocation(fs_info); |
| 3390 | } |
| 3391 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3392 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3393 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3394 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3395 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3396 | else |
| 3397 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3398 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3399 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3400 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3401 | spin_unlock(&space_info->lock); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3402 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3403 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3404 | } |
| 3405 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3406 | /* |
| 3407 | * shrink metadata reservation for delalloc |
| 3408 | */ |
| 3409 | static int shrink_delalloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3410 | struct btrfs_root *root, u64 to_reclaim, int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3411 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3412 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3413 | struct btrfs_space_info *space_info; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3414 | u64 reserved; |
| 3415 | u64 max_reclaim; |
| 3416 | u64 reclaimed = 0; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3417 | long time_left; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3418 | int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3419 | int loops = 0; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3420 | unsigned long progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3421 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3422 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3423 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3424 | |
| 3425 | smp_mb(); |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3426 | reserved = space_info->bytes_reserved; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3427 | progress = space_info->reservation_progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3428 | |
| 3429 | if (reserved == 0) |
| 3430 | return 0; |
| 3431 | |
| 3432 | max_reclaim = min(reserved, to_reclaim); |
| 3433 | |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3434 | while (loops < 1024) { |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3435 | /* have the flusher threads jump in and do some IO */ |
| 3436 | smp_mb(); |
| 3437 | nr_pages = min_t(unsigned long, nr_pages, |
| 3438 | root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); |
| 3439 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3440 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3441 | spin_lock(&space_info->lock); |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3442 | if (reserved > space_info->bytes_reserved) |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3443 | reclaimed += reserved - space_info->bytes_reserved; |
| 3444 | reserved = space_info->bytes_reserved; |
| 3445 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3446 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3447 | loops++; |
| 3448 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3449 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3450 | break; |
| 3451 | |
| 3452 | if (trans && trans->transaction->blocked) |
| 3453 | return -EAGAIN; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3454 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3455 | time_left = schedule_timeout_interruptible(1); |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3456 | |
| 3457 | /* We were interrupted, exit */ |
| 3458 | if (time_left) |
| 3459 | break; |
| 3460 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3461 | /* we've kicked the IO a few times, if anything has been freed, |
| 3462 | * exit. There is no sense in looping here for a long time |
| 3463 | * when we really need to commit the transaction, or there are |
| 3464 | * just too many writers without enough free space |
| 3465 | */ |
| 3466 | |
| 3467 | if (loops > 3) { |
| 3468 | smp_mb(); |
| 3469 | if (progress != space_info->reservation_progress) |
| 3470 | break; |
| 3471 | } |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3472 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3473 | } |
| 3474 | return reclaimed >= to_reclaim; |
| 3475 | } |
| 3476 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3477 | /* |
| 3478 | * Retries tells us how many times we've called reserve_metadata_bytes. The |
| 3479 | * idea is if this is the first call (retries == 0) then we will add to our |
| 3480 | * reserved count if we can't make the allocation in order to hold our place |
| 3481 | * while we go and try and free up space. That way for retries > 1 we don't try |
| 3482 | * and add space, we just check to see if the amount of unused space is >= the |
| 3483 | * total space, meaning that our reservation is valid. |
| 3484 | * |
| 3485 | * However if we don't intend to retry this reservation, pass -1 as retries so |
| 3486 | * that it short circuits this logic. |
| 3487 | */ |
| 3488 | static int reserve_metadata_bytes(struct btrfs_trans_handle *trans, |
| 3489 | struct btrfs_root *root, |
| 3490 | struct btrfs_block_rsv *block_rsv, |
| 3491 | u64 orig_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3492 | { |
| 3493 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3494 | u64 unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3495 | u64 num_bytes = orig_bytes; |
| 3496 | int retries = 0; |
| 3497 | int ret = 0; |
| 3498 | bool reserved = false; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3499 | bool committed = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3500 | |
| 3501 | again: |
| 3502 | ret = -ENOSPC; |
| 3503 | if (reserved) |
| 3504 | num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3505 | |
| 3506 | spin_lock(&space_info->lock); |
| 3507 | unused = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3508 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3509 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3510 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3511 | /* |
| 3512 | * The idea here is that we've not already over-reserved the block group |
| 3513 | * then we can go ahead and save our reservation first and then start |
| 3514 | * flushing if we need to. Otherwise if we've already overcommitted |
| 3515 | * lets start flushing stuff first and then come back and try to make |
| 3516 | * our reservation. |
| 3517 | */ |
| 3518 | if (unused <= space_info->total_bytes) { |
Arne Jansen | 6f33434 | 2010-11-12 23:17:56 +0000 | [diff] [blame] | 3519 | unused = space_info->total_bytes - unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3520 | if (unused >= num_bytes) { |
| 3521 | if (!reserved) |
| 3522 | space_info->bytes_reserved += orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3523 | ret = 0; |
| 3524 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3525 | /* |
| 3526 | * Ok set num_bytes to orig_bytes since we aren't |
| 3527 | * overocmmitted, this way we only try and reclaim what |
| 3528 | * we need. |
| 3529 | */ |
| 3530 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3531 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3532 | } else { |
| 3533 | /* |
| 3534 | * Ok we're over committed, set num_bytes to the overcommitted |
| 3535 | * amount plus the amount of bytes that we need for this |
| 3536 | * reservation. |
| 3537 | */ |
| 3538 | num_bytes = unused - space_info->total_bytes + |
| 3539 | (orig_bytes * (retries + 1)); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3540 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3541 | |
| 3542 | /* |
| 3543 | * Couldn't make our reservation, save our place so while we're trying |
| 3544 | * to reclaim space we can actually use it instead of somebody else |
| 3545 | * stealing it from us. |
| 3546 | */ |
| 3547 | if (ret && !reserved) { |
| 3548 | space_info->bytes_reserved += orig_bytes; |
| 3549 | reserved = true; |
| 3550 | } |
| 3551 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3552 | spin_unlock(&space_info->lock); |
| 3553 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3554 | if (!ret) |
| 3555 | return 0; |
| 3556 | |
| 3557 | if (!flush) |
| 3558 | goto out; |
| 3559 | |
| 3560 | /* |
| 3561 | * We do synchronous shrinking since we don't actually unreserve |
| 3562 | * metadata until after the IO is completed. |
| 3563 | */ |
| 3564 | ret = shrink_delalloc(trans, root, num_bytes, 1); |
| 3565 | if (ret > 0) |
| 3566 | return 0; |
| 3567 | else if (ret < 0) |
| 3568 | goto out; |
| 3569 | |
| 3570 | /* |
| 3571 | * So if we were overcommitted it's possible that somebody else flushed |
| 3572 | * out enough space and we simply didn't have enough space to reclaim, |
| 3573 | * so go back around and try again. |
| 3574 | */ |
| 3575 | if (retries < 2) { |
| 3576 | retries++; |
| 3577 | goto again; |
| 3578 | } |
| 3579 | |
| 3580 | spin_lock(&space_info->lock); |
| 3581 | /* |
| 3582 | * Not enough space to be reclaimed, don't bother committing the |
| 3583 | * transaction. |
| 3584 | */ |
| 3585 | if (space_info->bytes_pinned < orig_bytes) |
| 3586 | ret = -ENOSPC; |
| 3587 | spin_unlock(&space_info->lock); |
| 3588 | if (ret) |
| 3589 | goto out; |
| 3590 | |
| 3591 | ret = -EAGAIN; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3592 | if (trans || committed) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3593 | goto out; |
| 3594 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3595 | ret = -ENOSPC; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3596 | trans = btrfs_join_transaction(root); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3597 | if (IS_ERR(trans)) |
| 3598 | goto out; |
| 3599 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3600 | if (!ret) { |
| 3601 | trans = NULL; |
| 3602 | committed = true; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3603 | goto again; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3604 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3605 | |
| 3606 | out: |
| 3607 | if (reserved) { |
| 3608 | spin_lock(&space_info->lock); |
| 3609 | space_info->bytes_reserved -= orig_bytes; |
| 3610 | spin_unlock(&space_info->lock); |
| 3611 | } |
| 3612 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3613 | return ret; |
| 3614 | } |
| 3615 | |
| 3616 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3617 | struct btrfs_root *root) |
| 3618 | { |
| 3619 | struct btrfs_block_rsv *block_rsv; |
| 3620 | if (root->ref_cows) |
| 3621 | block_rsv = trans->block_rsv; |
| 3622 | else |
| 3623 | block_rsv = root->block_rsv; |
| 3624 | |
| 3625 | if (!block_rsv) |
| 3626 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3627 | |
| 3628 | return block_rsv; |
| 3629 | } |
| 3630 | |
| 3631 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3632 | u64 num_bytes) |
| 3633 | { |
| 3634 | int ret = -ENOSPC; |
| 3635 | spin_lock(&block_rsv->lock); |
| 3636 | if (block_rsv->reserved >= num_bytes) { |
| 3637 | block_rsv->reserved -= num_bytes; |
| 3638 | if (block_rsv->reserved < block_rsv->size) |
| 3639 | block_rsv->full = 0; |
| 3640 | ret = 0; |
| 3641 | } |
| 3642 | spin_unlock(&block_rsv->lock); |
| 3643 | return ret; |
| 3644 | } |
| 3645 | |
| 3646 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3647 | u64 num_bytes, int update_size) |
| 3648 | { |
| 3649 | spin_lock(&block_rsv->lock); |
| 3650 | block_rsv->reserved += num_bytes; |
| 3651 | if (update_size) |
| 3652 | block_rsv->size += num_bytes; |
| 3653 | else if (block_rsv->reserved >= block_rsv->size) |
| 3654 | block_rsv->full = 1; |
| 3655 | spin_unlock(&block_rsv->lock); |
| 3656 | } |
| 3657 | |
| 3658 | void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3659 | struct btrfs_block_rsv *dest, u64 num_bytes) |
| 3660 | { |
| 3661 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3662 | |
| 3663 | spin_lock(&block_rsv->lock); |
| 3664 | if (num_bytes == (u64)-1) |
| 3665 | num_bytes = block_rsv->size; |
| 3666 | block_rsv->size -= num_bytes; |
| 3667 | if (block_rsv->reserved >= block_rsv->size) { |
| 3668 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3669 | block_rsv->reserved = block_rsv->size; |
| 3670 | block_rsv->full = 1; |
| 3671 | } else { |
| 3672 | num_bytes = 0; |
| 3673 | } |
| 3674 | spin_unlock(&block_rsv->lock); |
| 3675 | |
| 3676 | if (num_bytes > 0) { |
| 3677 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 3678 | spin_lock(&dest->lock); |
| 3679 | if (!dest->full) { |
| 3680 | u64 bytes_to_add; |
| 3681 | |
| 3682 | bytes_to_add = dest->size - dest->reserved; |
| 3683 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 3684 | dest->reserved += bytes_to_add; |
| 3685 | if (dest->reserved >= dest->size) |
| 3686 | dest->full = 1; |
| 3687 | num_bytes -= bytes_to_add; |
| 3688 | } |
| 3689 | spin_unlock(&dest->lock); |
| 3690 | } |
| 3691 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3692 | spin_lock(&space_info->lock); |
| 3693 | space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3694 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3695 | spin_unlock(&space_info->lock); |
| 3696 | } |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3701 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3702 | { |
| 3703 | int ret; |
| 3704 | |
| 3705 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3706 | if (ret) |
| 3707 | return ret; |
| 3708 | |
| 3709 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3710 | return 0; |
| 3711 | } |
| 3712 | |
| 3713 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3714 | { |
| 3715 | memset(rsv, 0, sizeof(*rsv)); |
| 3716 | spin_lock_init(&rsv->lock); |
| 3717 | atomic_set(&rsv->usage, 1); |
| 3718 | rsv->priority = 6; |
| 3719 | INIT_LIST_HEAD(&rsv->list); |
| 3720 | } |
| 3721 | |
| 3722 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3723 | { |
| 3724 | struct btrfs_block_rsv *block_rsv; |
| 3725 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3726 | |
| 3727 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3728 | if (!block_rsv) |
| 3729 | return NULL; |
| 3730 | |
| 3731 | btrfs_init_block_rsv(block_rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3732 | block_rsv->space_info = __find_space_info(fs_info, |
| 3733 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3734 | return block_rsv; |
| 3735 | } |
| 3736 | |
| 3737 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3738 | struct btrfs_block_rsv *rsv) |
| 3739 | { |
| 3740 | if (rsv && atomic_dec_and_test(&rsv->usage)) { |
| 3741 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3742 | if (!rsv->durable) |
| 3743 | kfree(rsv); |
| 3744 | } |
| 3745 | } |
| 3746 | |
| 3747 | /* |
| 3748 | * make the block_rsv struct be able to capture freed space. |
| 3749 | * the captured space will re-add to the the block_rsv struct |
| 3750 | * after transaction commit |
| 3751 | */ |
| 3752 | void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info, |
| 3753 | struct btrfs_block_rsv *block_rsv) |
| 3754 | { |
| 3755 | block_rsv->durable = 1; |
| 3756 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 3757 | list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list); |
| 3758 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 3759 | } |
| 3760 | |
| 3761 | int btrfs_block_rsv_add(struct btrfs_trans_handle *trans, |
| 3762 | struct btrfs_root *root, |
| 3763 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3764 | u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3765 | { |
| 3766 | int ret; |
| 3767 | |
| 3768 | if (num_bytes == 0) |
| 3769 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3770 | |
| 3771 | ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3772 | if (!ret) { |
| 3773 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3774 | return 0; |
| 3775 | } |
| 3776 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3777 | return ret; |
| 3778 | } |
| 3779 | |
| 3780 | int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, |
| 3781 | struct btrfs_root *root, |
| 3782 | struct btrfs_block_rsv *block_rsv, |
| 3783 | u64 min_reserved, int min_factor) |
| 3784 | { |
| 3785 | u64 num_bytes = 0; |
| 3786 | int commit_trans = 0; |
| 3787 | int ret = -ENOSPC; |
| 3788 | |
| 3789 | if (!block_rsv) |
| 3790 | return 0; |
| 3791 | |
| 3792 | spin_lock(&block_rsv->lock); |
| 3793 | if (min_factor > 0) |
| 3794 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3795 | if (min_reserved > num_bytes) |
| 3796 | num_bytes = min_reserved; |
| 3797 | |
| 3798 | if (block_rsv->reserved >= num_bytes) { |
| 3799 | ret = 0; |
| 3800 | } else { |
| 3801 | num_bytes -= block_rsv->reserved; |
| 3802 | if (block_rsv->durable && |
| 3803 | block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes) |
| 3804 | commit_trans = 1; |
| 3805 | } |
| 3806 | spin_unlock(&block_rsv->lock); |
| 3807 | if (!ret) |
| 3808 | return 0; |
| 3809 | |
| 3810 | if (block_rsv->refill_used) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3811 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 3812 | num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3813 | if (!ret) { |
| 3814 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3815 | return 0; |
| 3816 | } |
| 3817 | } |
| 3818 | |
| 3819 | if (commit_trans) { |
| 3820 | if (trans) |
| 3821 | return -EAGAIN; |
| 3822 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3823 | trans = btrfs_join_transaction(root); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3824 | BUG_ON(IS_ERR(trans)); |
| 3825 | ret = btrfs_commit_transaction(trans, root); |
| 3826 | return 0; |
| 3827 | } |
| 3828 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3829 | return -ENOSPC; |
| 3830 | } |
| 3831 | |
| 3832 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3833 | struct btrfs_block_rsv *dst_rsv, |
| 3834 | u64 num_bytes) |
| 3835 | { |
| 3836 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3837 | } |
| 3838 | |
| 3839 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3840 | struct btrfs_block_rsv *block_rsv, |
| 3841 | u64 num_bytes) |
| 3842 | { |
| 3843 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3844 | if (global_rsv->full || global_rsv == block_rsv || |
| 3845 | block_rsv->space_info != global_rsv->space_info) |
| 3846 | global_rsv = NULL; |
| 3847 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3848 | } |
| 3849 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3850 | /* |
| 3851 | * helper to calculate size of global block reservation. |
| 3852 | * the desired value is sum of space used by extent tree, |
| 3853 | * checksum tree and root tree |
| 3854 | */ |
| 3855 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3856 | { |
| 3857 | struct btrfs_space_info *sinfo; |
| 3858 | u64 num_bytes; |
| 3859 | u64 meta_used; |
| 3860 | u64 data_used; |
| 3861 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
| 3862 | #if 0 |
| 3863 | /* |
| 3864 | * per tree used space accounting can be inaccuracy, so we |
| 3865 | * can't rely on it. |
| 3866 | */ |
| 3867 | spin_lock(&fs_info->extent_root->accounting_lock); |
| 3868 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item); |
| 3869 | spin_unlock(&fs_info->extent_root->accounting_lock); |
| 3870 | |
| 3871 | spin_lock(&fs_info->csum_root->accounting_lock); |
| 3872 | num_bytes += btrfs_root_used(&fs_info->csum_root->root_item); |
| 3873 | spin_unlock(&fs_info->csum_root->accounting_lock); |
| 3874 | |
| 3875 | spin_lock(&fs_info->tree_root->accounting_lock); |
| 3876 | num_bytes += btrfs_root_used(&fs_info->tree_root->root_item); |
| 3877 | spin_unlock(&fs_info->tree_root->accounting_lock); |
| 3878 | #endif |
| 3879 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3880 | spin_lock(&sinfo->lock); |
| 3881 | data_used = sinfo->bytes_used; |
| 3882 | spin_unlock(&sinfo->lock); |
| 3883 | |
| 3884 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3885 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3886 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 3887 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3888 | meta_used = sinfo->bytes_used; |
| 3889 | spin_unlock(&sinfo->lock); |
| 3890 | |
| 3891 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3892 | csum_size * 2; |
| 3893 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3894 | |
| 3895 | if (num_bytes * 3 > meta_used) |
| 3896 | num_bytes = div64_u64(meta_used, 3); |
| 3897 | |
| 3898 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3899 | } |
| 3900 | |
| 3901 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3902 | { |
| 3903 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3904 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3905 | u64 num_bytes; |
| 3906 | |
| 3907 | num_bytes = calc_global_metadata_size(fs_info); |
| 3908 | |
| 3909 | spin_lock(&block_rsv->lock); |
| 3910 | spin_lock(&sinfo->lock); |
| 3911 | |
| 3912 | block_rsv->size = num_bytes; |
| 3913 | |
| 3914 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3915 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 3916 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3917 | |
| 3918 | if (sinfo->total_bytes > num_bytes) { |
| 3919 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3920 | block_rsv->reserved += num_bytes; |
| 3921 | sinfo->bytes_reserved += num_bytes; |
| 3922 | } |
| 3923 | |
| 3924 | if (block_rsv->reserved >= block_rsv->size) { |
| 3925 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3926 | sinfo->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3927 | sinfo->reservation_progress++; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3928 | block_rsv->reserved = block_rsv->size; |
| 3929 | block_rsv->full = 1; |
| 3930 | } |
| 3931 | #if 0 |
| 3932 | printk(KERN_INFO"global block rsv size %llu reserved %llu\n", |
| 3933 | block_rsv->size, block_rsv->reserved); |
| 3934 | #endif |
| 3935 | spin_unlock(&sinfo->lock); |
| 3936 | spin_unlock(&block_rsv->lock); |
| 3937 | } |
| 3938 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3939 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3940 | { |
| 3941 | struct btrfs_space_info *space_info; |
| 3942 | |
| 3943 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3944 | fs_info->chunk_block_rsv.space_info = space_info; |
| 3945 | fs_info->chunk_block_rsv.priority = 10; |
| 3946 | |
| 3947 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3948 | fs_info->global_block_rsv.space_info = space_info; |
| 3949 | fs_info->global_block_rsv.priority = 10; |
| 3950 | fs_info->global_block_rsv.refill_used = 1; |
| 3951 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3952 | fs_info->trans_block_rsv.space_info = space_info; |
| 3953 | fs_info->empty_block_rsv.space_info = space_info; |
| 3954 | fs_info->empty_block_rsv.priority = 10; |
| 3955 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3956 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3957 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3958 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3959 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3960 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3961 | |
| 3962 | btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv); |
| 3963 | |
| 3964 | btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv); |
| 3965 | |
| 3966 | update_global_block_rsv(fs_info); |
| 3967 | } |
| 3968 | |
| 3969 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3970 | { |
| 3971 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3972 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3973 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3974 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3975 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3976 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3977 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3978 | } |
| 3979 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3980 | static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items) |
| 3981 | { |
| 3982 | return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) * |
| 3983 | 3 * num_items; |
| 3984 | } |
| 3985 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 3986 | int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3987 | struct btrfs_root *root, |
| 3988 | struct btrfs_block_rsv *rsv) |
| 3989 | { |
| 3990 | struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv; |
| 3991 | u64 num_bytes; |
| 3992 | int ret; |
| 3993 | |
| 3994 | /* |
| 3995 | * Truncate should be freeing data, but give us 2 items just in case it |
| 3996 | * needs to use some space. We may want to be smarter about this in the |
| 3997 | * future. |
| 3998 | */ |
| 3999 | num_bytes = calc_trans_metadata_size(root, 2); |
| 4000 | |
| 4001 | /* We already have enough bytes, just return */ |
| 4002 | if (rsv->reserved >= num_bytes) |
| 4003 | return 0; |
| 4004 | |
| 4005 | num_bytes -= rsv->reserved; |
| 4006 | |
| 4007 | /* |
| 4008 | * You should have reserved enough space before hand to do this, so this |
| 4009 | * should not fail. |
| 4010 | */ |
| 4011 | ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes); |
| 4012 | BUG_ON(ret); |
| 4013 | |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4017 | int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4018 | struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4019 | int num_items) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4020 | { |
| 4021 | u64 num_bytes; |
| 4022 | int ret; |
| 4023 | |
| 4024 | if (num_items == 0 || root->fs_info->chunk_root == root) |
| 4025 | return 0; |
| 4026 | |
| 4027 | num_bytes = calc_trans_metadata_size(root, num_items); |
| 4028 | ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4029 | num_bytes); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4030 | if (!ret) { |
| 4031 | trans->bytes_reserved += num_bytes; |
| 4032 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 4033 | } |
| 4034 | return ret; |
| 4035 | } |
| 4036 | |
| 4037 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 4038 | struct btrfs_root *root) |
| 4039 | { |
| 4040 | if (!trans->bytes_reserved) |
| 4041 | return; |
| 4042 | |
| 4043 | BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv); |
| 4044 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 4045 | trans->bytes_reserved); |
| 4046 | trans->bytes_reserved = 0; |
| 4047 | } |
| 4048 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4049 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4050 | struct inode *inode) |
| 4051 | { |
| 4052 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4053 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 4054 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 4055 | |
| 4056 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4057 | * We need to hold space in order to delete our orphan item once we've |
| 4058 | * added it, so this takes the reservation so we can release it later |
| 4059 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4060 | */ |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4061 | u64 num_bytes = calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4062 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4063 | } |
| 4064 | |
| 4065 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 4066 | { |
| 4067 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4068 | u64 num_bytes = calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4069 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 4070 | } |
| 4071 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4072 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4073 | struct btrfs_pending_snapshot *pending) |
| 4074 | { |
| 4075 | struct btrfs_root *root = pending->root; |
| 4076 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 4077 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 4078 | /* |
| 4079 | * two for root back/forward refs, two for directory entries |
| 4080 | * and one for root of the snapshot. |
| 4081 | */ |
| 4082 | u64 num_bytes = calc_trans_metadata_size(root, 5); |
| 4083 | dst_rsv->space_info = src_rsv->space_info; |
| 4084 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4085 | } |
| 4086 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4087 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes) |
| 4088 | { |
| 4089 | return num_bytes >>= 3; |
| 4090 | } |
| 4091 | |
| 4092 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 4093 | { |
| 4094 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4095 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
| 4096 | u64 to_reserve; |
| 4097 | int nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4098 | int reserved_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4099 | int ret; |
| 4100 | |
| 4101 | if (btrfs_transaction_in_commit(root->fs_info)) |
| 4102 | schedule_timeout(1); |
| 4103 | |
| 4104 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4105 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4106 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4107 | reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents); |
| 4108 | |
| 4109 | if (nr_extents > reserved_extents) { |
| 4110 | nr_extents -= reserved_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4111 | to_reserve = calc_trans_metadata_size(root, nr_extents); |
| 4112 | } else { |
| 4113 | nr_extents = 0; |
| 4114 | to_reserve = 0; |
| 4115 | } |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4116 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4117 | to_reserve += calc_csum_metadata_size(inode, num_bytes); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4118 | ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1); |
| 4119 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4120 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4121 | |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4122 | atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4123 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4124 | |
| 4125 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 4126 | |
| 4127 | if (block_rsv->size > 512 * 1024 * 1024) |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4128 | shrink_delalloc(NULL, root, to_reserve, 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4129 | |
| 4130 | return 0; |
| 4131 | } |
| 4132 | |
| 4133 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 4134 | { |
| 4135 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4136 | u64 to_free; |
| 4137 | int nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4138 | int reserved_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4139 | |
| 4140 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
| 4141 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 4142 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4143 | |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4144 | reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents); |
| 4145 | do { |
| 4146 | int old, new; |
| 4147 | |
| 4148 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents); |
| 4149 | if (nr_extents >= reserved_extents) { |
| 4150 | nr_extents = 0; |
| 4151 | break; |
| 4152 | } |
| 4153 | old = reserved_extents; |
| 4154 | nr_extents = reserved_extents - nr_extents; |
| 4155 | new = reserved_extents - nr_extents; |
| 4156 | old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents, |
| 4157 | reserved_extents, new); |
| 4158 | if (likely(old == reserved_extents)) |
| 4159 | break; |
| 4160 | reserved_extents = old; |
| 4161 | } while (1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4162 | |
| 4163 | to_free = calc_csum_metadata_size(inode, num_bytes); |
| 4164 | if (nr_extents > 0) |
| 4165 | to_free += calc_trans_metadata_size(root, nr_extents); |
| 4166 | |
| 4167 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 4168 | to_free); |
| 4169 | } |
| 4170 | |
| 4171 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 4172 | { |
| 4173 | int ret; |
| 4174 | |
| 4175 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 4176 | if (ret) |
| 4177 | return ret; |
| 4178 | |
| 4179 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 4180 | if (ret) { |
| 4181 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4182 | return ret; |
| 4183 | } |
| 4184 | |
| 4185 | return 0; |
| 4186 | } |
| 4187 | |
| 4188 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 4189 | { |
| 4190 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 4191 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4192 | } |
| 4193 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4194 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 4195 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4196 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4197 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4198 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4199 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4200 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4201 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4202 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4203 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 4204 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4205 | /* block accounting for super block */ |
| 4206 | spin_lock(&info->delalloc_lock); |
| 4207 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 4208 | if (alloc) |
| 4209 | old_val += num_bytes; |
| 4210 | else |
| 4211 | old_val -= num_bytes; |
| 4212 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4213 | spin_unlock(&info->delalloc_lock); |
| 4214 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4215 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4216 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4217 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4218 | return -1; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4219 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 4220 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4221 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4222 | factor = 2; |
| 4223 | else |
| 4224 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4225 | /* |
| 4226 | * If this block group has free space cache written out, we |
| 4227 | * need to make sure to load it if we are removing space. This |
| 4228 | * is because we need the unpinning stage to actually add the |
| 4229 | * space back to the block group, otherwise we will leak space. |
| 4230 | */ |
| 4231 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4232 | cache_block_group(cache, trans, NULL, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4233 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4234 | byte_in_group = bytenr - cache->key.objectid; |
| 4235 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4236 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4237 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4238 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4239 | |
| 4240 | if (btrfs_super_cache_generation(&info->super_copy) != 0 && |
| 4241 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 4242 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 4243 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4244 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4245 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4246 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4247 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4248 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4249 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4250 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4251 | cache->space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4252 | cache->space_info->reservation_progress++; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4253 | cache->space_info->bytes_used += num_bytes; |
| 4254 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4255 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4256 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4257 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4258 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4259 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4260 | cache->pinned += num_bytes; |
| 4261 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4262 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4263 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4264 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4265 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4266 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4267 | set_extent_dirty(info->pinned_extents, |
| 4268 | bytenr, bytenr + num_bytes - 1, |
| 4269 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4270 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4271 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4272 | total -= num_bytes; |
| 4273 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4274 | } |
| 4275 | return 0; |
| 4276 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4277 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4278 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 4279 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4280 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4281 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4282 | |
| 4283 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 4284 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4285 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4286 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4287 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4288 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4289 | |
| 4290 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4291 | } |
| 4292 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4293 | static int pin_down_extent(struct btrfs_root *root, |
| 4294 | struct btrfs_block_group_cache *cache, |
| 4295 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4296 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4297 | spin_lock(&cache->space_info->lock); |
| 4298 | spin_lock(&cache->lock); |
| 4299 | cache->pinned += num_bytes; |
| 4300 | cache->space_info->bytes_pinned += num_bytes; |
| 4301 | if (reserved) { |
| 4302 | cache->reserved -= num_bytes; |
| 4303 | cache->space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4304 | cache->space_info->reservation_progress++; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4305 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4306 | spin_unlock(&cache->lock); |
| 4307 | spin_unlock(&cache->space_info->lock); |
| 4308 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4309 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 4310 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4311 | return 0; |
| 4312 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4313 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4314 | /* |
| 4315 | * this function must be called within transaction |
| 4316 | */ |
| 4317 | int btrfs_pin_extent(struct btrfs_root *root, |
| 4318 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4319 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4320 | struct btrfs_block_group_cache *cache; |
| 4321 | |
| 4322 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 4323 | BUG_ON(!cache); |
| 4324 | |
| 4325 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 4326 | |
| 4327 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4328 | return 0; |
| 4329 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4330 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4331 | /* |
| 4332 | * update size of reserved extents. this function may return -EAGAIN |
| 4333 | * if 'reserve' is true or 'sinfo' is false. |
| 4334 | */ |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4335 | int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4336 | u64 num_bytes, int reserve, int sinfo) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4337 | { |
| 4338 | int ret = 0; |
| 4339 | if (sinfo) { |
| 4340 | struct btrfs_space_info *space_info = cache->space_info; |
| 4341 | spin_lock(&space_info->lock); |
| 4342 | spin_lock(&cache->lock); |
| 4343 | if (reserve) { |
| 4344 | if (cache->ro) { |
| 4345 | ret = -EAGAIN; |
| 4346 | } else { |
| 4347 | cache->reserved += num_bytes; |
| 4348 | space_info->bytes_reserved += num_bytes; |
| 4349 | } |
| 4350 | } else { |
| 4351 | if (cache->ro) |
| 4352 | space_info->bytes_readonly += num_bytes; |
| 4353 | cache->reserved -= num_bytes; |
| 4354 | space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4355 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4356 | } |
| 4357 | spin_unlock(&cache->lock); |
| 4358 | spin_unlock(&space_info->lock); |
| 4359 | } else { |
| 4360 | spin_lock(&cache->lock); |
| 4361 | if (cache->ro) { |
| 4362 | ret = -EAGAIN; |
| 4363 | } else { |
| 4364 | if (reserve) |
| 4365 | cache->reserved += num_bytes; |
| 4366 | else |
| 4367 | cache->reserved -= num_bytes; |
| 4368 | } |
| 4369 | spin_unlock(&cache->lock); |
| 4370 | } |
| 4371 | return ret; |
| 4372 | } |
| 4373 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4374 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 4375 | struct btrfs_root *root) |
| 4376 | { |
| 4377 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4378 | struct btrfs_caching_control *next; |
| 4379 | struct btrfs_caching_control *caching_ctl; |
| 4380 | struct btrfs_block_group_cache *cache; |
| 4381 | |
| 4382 | down_write(&fs_info->extent_commit_sem); |
| 4383 | |
| 4384 | list_for_each_entry_safe(caching_ctl, next, |
| 4385 | &fs_info->caching_block_groups, list) { |
| 4386 | cache = caching_ctl->block_group; |
| 4387 | if (block_group_cache_done(cache)) { |
| 4388 | cache->last_byte_to_unpin = (u64)-1; |
| 4389 | list_del_init(&caching_ctl->list); |
| 4390 | put_caching_control(caching_ctl); |
| 4391 | } else { |
| 4392 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4393 | } |
| 4394 | } |
| 4395 | |
| 4396 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4397 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4398 | else |
| 4399 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4400 | |
| 4401 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4402 | |
| 4403 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4404 | return 0; |
| 4405 | } |
| 4406 | |
| 4407 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 4408 | { |
| 4409 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4410 | struct btrfs_block_group_cache *cache = NULL; |
| 4411 | u64 len; |
| 4412 | |
| 4413 | while (start <= end) { |
| 4414 | if (!cache || |
| 4415 | start >= cache->key.objectid + cache->key.offset) { |
| 4416 | if (cache) |
| 4417 | btrfs_put_block_group(cache); |
| 4418 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4419 | BUG_ON(!cache); |
| 4420 | } |
| 4421 | |
| 4422 | len = cache->key.objectid + cache->key.offset - start; |
| 4423 | len = min(len, end + 1 - start); |
| 4424 | |
| 4425 | if (start < cache->last_byte_to_unpin) { |
| 4426 | len = min(len, cache->last_byte_to_unpin - start); |
| 4427 | btrfs_add_free_space(cache, start, len); |
| 4428 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4429 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4430 | start += len; |
| 4431 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4432 | spin_lock(&cache->space_info->lock); |
| 4433 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4434 | cache->pinned -= len; |
| 4435 | cache->space_info->bytes_pinned -= len; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4436 | if (cache->ro) { |
| 4437 | cache->space_info->bytes_readonly += len; |
| 4438 | } else if (cache->reserved_pinned > 0) { |
| 4439 | len = min(len, cache->reserved_pinned); |
| 4440 | cache->reserved_pinned -= len; |
| 4441 | cache->space_info->bytes_reserved += len; |
| 4442 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4443 | spin_unlock(&cache->lock); |
| 4444 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4448 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4449 | return 0; |
| 4450 | } |
| 4451 | |
| 4452 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4453 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4454 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4455 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4456 | struct extent_io_tree *unpin; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4457 | struct btrfs_block_rsv *block_rsv; |
| 4458 | struct btrfs_block_rsv *next_rsv; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4459 | u64 start; |
| 4460 | u64 end; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4461 | int idx; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4462 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4463 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4464 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4465 | unpin = &fs_info->freed_extents[1]; |
| 4466 | else |
| 4467 | unpin = &fs_info->freed_extents[0]; |
| 4468 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4469 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4470 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4471 | EXTENT_DIRTY); |
| 4472 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4473 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4474 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 4475 | if (btrfs_test_opt(root, DISCARD)) |
| 4476 | ret = btrfs_discard_extent(root, start, |
| 4477 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4478 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4479 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4480 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4481 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4482 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4483 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4484 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 4485 | list_for_each_entry_safe(block_rsv, next_rsv, |
| 4486 | &fs_info->durable_block_rsv_list, list) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4487 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4488 | idx = trans->transid & 0x1; |
| 4489 | if (block_rsv->freed[idx] > 0) { |
| 4490 | block_rsv_add_bytes(block_rsv, |
| 4491 | block_rsv->freed[idx], 0); |
| 4492 | block_rsv->freed[idx] = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 4493 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4494 | if (atomic_read(&block_rsv->usage) == 0) { |
| 4495 | btrfs_block_rsv_release(root, block_rsv, (u64)-1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4496 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4497 | if (block_rsv->freed[0] == 0 && |
| 4498 | block_rsv->freed[1] == 0) { |
| 4499 | list_del_init(&block_rsv->list); |
| 4500 | kfree(block_rsv); |
| 4501 | } |
| 4502 | } else { |
| 4503 | btrfs_block_rsv_release(root, block_rsv, 0); |
| 4504 | } |
| 4505 | } |
| 4506 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 4507 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4508 | return 0; |
| 4509 | } |
| 4510 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4511 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4512 | struct btrfs_root *root, |
| 4513 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4514 | u64 root_objectid, u64 owner_objectid, |
| 4515 | u64 owner_offset, int refs_to_drop, |
| 4516 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4517 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4518 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4519 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4520 | struct btrfs_fs_info *info = root->fs_info; |
| 4521 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4522 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4523 | struct btrfs_extent_item *ei; |
| 4524 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4525 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4526 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4527 | int extent_slot = 0; |
| 4528 | int found_extent = 0; |
| 4529 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4530 | u32 item_size; |
| 4531 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4532 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4533 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4534 | if (!path) |
| 4535 | return -ENOMEM; |
| 4536 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4537 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4538 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4539 | |
| 4540 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4541 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4542 | |
| 4543 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4544 | bytenr, num_bytes, parent, |
| 4545 | root_objectid, owner_objectid, |
| 4546 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4547 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4548 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4549 | while (extent_slot >= 0) { |
| 4550 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4551 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4552 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4553 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4554 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4555 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4556 | found_extent = 1; |
| 4557 | break; |
| 4558 | } |
| 4559 | if (path->slots[0] - extent_slot > 5) |
| 4560 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4561 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4562 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4563 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4564 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4565 | if (found_extent && item_size < sizeof(*ei)) |
| 4566 | found_extent = 0; |
| 4567 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4568 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4569 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4570 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4571 | NULL, refs_to_drop, |
| 4572 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4573 | BUG_ON(ret); |
| 4574 | btrfs_release_path(extent_root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4575 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4576 | |
| 4577 | key.objectid = bytenr; |
| 4578 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4579 | key.offset = num_bytes; |
| 4580 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4581 | ret = btrfs_search_slot(trans, extent_root, |
| 4582 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4583 | if (ret) { |
| 4584 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4585 | ", was looking for %llu\n", ret, |
| 4586 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4587 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4588 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4589 | BUG_ON(ret); |
| 4590 | extent_slot = path->slots[0]; |
| 4591 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4592 | } else { |
| 4593 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4594 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4595 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4596 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4597 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4598 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4599 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4600 | (unsigned long long)owner_objectid, |
| 4601 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4602 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4603 | |
| 4604 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4605 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4606 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4607 | if (item_size < sizeof(*ei)) { |
| 4608 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4609 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4610 | owner_objectid, 0); |
| 4611 | BUG_ON(ret < 0); |
| 4612 | |
| 4613 | btrfs_release_path(extent_root, path); |
| 4614 | path->leave_spinning = 1; |
| 4615 | |
| 4616 | key.objectid = bytenr; |
| 4617 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4618 | key.offset = num_bytes; |
| 4619 | |
| 4620 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4621 | -1, 1); |
| 4622 | if (ret) { |
| 4623 | printk(KERN_ERR "umm, got %d back from search" |
| 4624 | ", was looking for %llu\n", ret, |
| 4625 | (unsigned long long)bytenr); |
| 4626 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4627 | } |
| 4628 | BUG_ON(ret); |
| 4629 | extent_slot = path->slots[0]; |
| 4630 | leaf = path->nodes[0]; |
| 4631 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4632 | } |
| 4633 | #endif |
| 4634 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4635 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4636 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4637 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4638 | struct btrfs_tree_block_info *bi; |
| 4639 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4640 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4641 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4642 | } |
| 4643 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4644 | refs = btrfs_extent_refs(leaf, ei); |
| 4645 | BUG_ON(refs < refs_to_drop); |
| 4646 | refs -= refs_to_drop; |
| 4647 | |
| 4648 | if (refs > 0) { |
| 4649 | if (extent_op) |
| 4650 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4651 | /* |
| 4652 | * In the case of inline back ref, reference count will |
| 4653 | * be updated by remove_extent_backref |
| 4654 | */ |
| 4655 | if (iref) { |
| 4656 | BUG_ON(!found_extent); |
| 4657 | } else { |
| 4658 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4659 | btrfs_mark_buffer_dirty(leaf); |
| 4660 | } |
| 4661 | if (found_extent) { |
| 4662 | ret = remove_extent_backref(trans, extent_root, path, |
| 4663 | iref, refs_to_drop, |
| 4664 | is_data); |
| 4665 | BUG_ON(ret); |
| 4666 | } |
| 4667 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4668 | if (found_extent) { |
| 4669 | BUG_ON(is_data && refs_to_drop != |
| 4670 | extent_data_ref_count(root, path, iref)); |
| 4671 | if (iref) { |
| 4672 | BUG_ON(path->slots[0] != extent_slot); |
| 4673 | } else { |
| 4674 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4675 | path->slots[0] = extent_slot; |
| 4676 | num_to_del = 2; |
| 4677 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4678 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4679 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4680 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4681 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4682 | BUG_ON(ret); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4683 | btrfs_release_path(extent_root, path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4684 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4685 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4686 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4687 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4688 | } else { |
| 4689 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4690 | bytenr >> PAGE_CACHE_SHIFT, |
| 4691 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4692 | } |
| 4693 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4694 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4695 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4696 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4697 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4698 | return ret; |
| 4699 | } |
| 4700 | |
| 4701 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4702 | * when we free an block, it is possible (and likely) that we free the last |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4703 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4704 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4705 | * removes it from the tree. |
| 4706 | */ |
| 4707 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4708 | struct btrfs_root *root, u64 bytenr) |
| 4709 | { |
| 4710 | struct btrfs_delayed_ref_head *head; |
| 4711 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4712 | struct btrfs_delayed_ref_node *ref; |
| 4713 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4714 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4715 | |
| 4716 | delayed_refs = &trans->transaction->delayed_refs; |
| 4717 | spin_lock(&delayed_refs->lock); |
| 4718 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4719 | if (!head) |
| 4720 | goto out; |
| 4721 | |
| 4722 | node = rb_prev(&head->node.rb_node); |
| 4723 | if (!node) |
| 4724 | goto out; |
| 4725 | |
| 4726 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4727 | |
| 4728 | /* there are still entries for this ref, we can't drop it */ |
| 4729 | if (ref->bytenr == bytenr) |
| 4730 | goto out; |
| 4731 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4732 | if (head->extent_op) { |
| 4733 | if (!head->must_insert_reserved) |
| 4734 | goto out; |
| 4735 | kfree(head->extent_op); |
| 4736 | head->extent_op = NULL; |
| 4737 | } |
| 4738 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4739 | /* |
| 4740 | * waiting for the lock here would deadlock. If someone else has it |
| 4741 | * locked they are already in the process of dropping it anyway |
| 4742 | */ |
| 4743 | if (!mutex_trylock(&head->mutex)) |
| 4744 | goto out; |
| 4745 | |
| 4746 | /* |
| 4747 | * at this point we have a head with no other entries. Go |
| 4748 | * ahead and process it. |
| 4749 | */ |
| 4750 | head->node.in_tree = 0; |
| 4751 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4752 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4753 | delayed_refs->num_entries--; |
| 4754 | |
| 4755 | /* |
| 4756 | * we don't take a ref on the node because we're removing it from the |
| 4757 | * tree, so we just steal the ref the tree was holding. |
| 4758 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4759 | delayed_refs->num_heads--; |
| 4760 | if (list_empty(&head->cluster)) |
| 4761 | delayed_refs->num_heads_ready--; |
| 4762 | |
| 4763 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4764 | spin_unlock(&delayed_refs->lock); |
| 4765 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4766 | BUG_ON(head->extent_op); |
| 4767 | if (head->must_insert_reserved) |
| 4768 | ret = 1; |
| 4769 | |
| 4770 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4771 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4772 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4773 | out: |
| 4774 | spin_unlock(&delayed_refs->lock); |
| 4775 | return 0; |
| 4776 | } |
| 4777 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4778 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4779 | struct btrfs_root *root, |
| 4780 | struct extent_buffer *buf, |
| 4781 | u64 parent, int last_ref) |
| 4782 | { |
| 4783 | struct btrfs_block_rsv *block_rsv; |
| 4784 | struct btrfs_block_group_cache *cache = NULL; |
| 4785 | int ret; |
| 4786 | |
| 4787 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4788 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4789 | parent, root->root_key.objectid, |
| 4790 | btrfs_header_level(buf), |
| 4791 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4792 | BUG_ON(ret); |
| 4793 | } |
| 4794 | |
| 4795 | if (!last_ref) |
| 4796 | return; |
| 4797 | |
| 4798 | block_rsv = get_block_rsv(trans, root); |
| 4799 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | 3bf84a5 | 2010-05-31 09:04:46 +0000 | [diff] [blame] | 4800 | if (block_rsv->space_info != cache->space_info) |
| 4801 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4802 | |
| 4803 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4804 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4805 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4806 | if (!ret) |
| 4807 | goto pin; |
| 4808 | } |
| 4809 | |
| 4810 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4811 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
| 4812 | goto pin; |
| 4813 | } |
| 4814 | |
| 4815 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4816 | |
| 4817 | btrfs_add_free_space(cache, buf->start, buf->len); |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4818 | ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4819 | if (ret == -EAGAIN) { |
| 4820 | /* block group became read-only */ |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4821 | btrfs_update_reserved_bytes(cache, buf->len, 0, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4822 | goto out; |
| 4823 | } |
| 4824 | |
| 4825 | ret = 1; |
| 4826 | spin_lock(&block_rsv->lock); |
| 4827 | if (block_rsv->reserved < block_rsv->size) { |
| 4828 | block_rsv->reserved += buf->len; |
| 4829 | ret = 0; |
| 4830 | } |
| 4831 | spin_unlock(&block_rsv->lock); |
| 4832 | |
| 4833 | if (ret) { |
| 4834 | spin_lock(&cache->space_info->lock); |
| 4835 | cache->space_info->bytes_reserved -= buf->len; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4836 | cache->space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4837 | spin_unlock(&cache->space_info->lock); |
| 4838 | } |
| 4839 | goto out; |
| 4840 | } |
| 4841 | pin: |
| 4842 | if (block_rsv->durable && !cache->ro) { |
| 4843 | ret = 0; |
| 4844 | spin_lock(&cache->lock); |
| 4845 | if (!cache->ro) { |
| 4846 | cache->reserved_pinned += buf->len; |
| 4847 | ret = 1; |
| 4848 | } |
| 4849 | spin_unlock(&cache->lock); |
| 4850 | |
| 4851 | if (ret) { |
| 4852 | spin_lock(&block_rsv->lock); |
| 4853 | block_rsv->freed[trans->transid & 0x1] += buf->len; |
| 4854 | spin_unlock(&block_rsv->lock); |
| 4855 | } |
| 4856 | } |
| 4857 | out: |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 4858 | /* |
| 4859 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 4860 | * anymore. |
| 4861 | */ |
| 4862 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4863 | btrfs_put_block_group(cache); |
| 4864 | } |
| 4865 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4866 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4867 | struct btrfs_root *root, |
| 4868 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4869 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4870 | { |
| 4871 | int ret; |
| 4872 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4873 | /* |
| 4874 | * tree log blocks never actually go into the extent allocation |
| 4875 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4876 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4877 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4878 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4879 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4880 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4881 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4882 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4883 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4884 | parent, root_objectid, (int)owner, |
| 4885 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4886 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4887 | } else { |
| 4888 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4889 | parent, root_objectid, owner, |
| 4890 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4891 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4892 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4893 | return ret; |
| 4894 | } |
| 4895 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4896 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4897 | { |
| 4898 | u64 mask = ((u64)root->stripesize - 1); |
| 4899 | u64 ret = (val + mask) & ~mask; |
| 4900 | return ret; |
| 4901 | } |
| 4902 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4903 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4904 | * when we wait for progress in the block group caching, its because |
| 4905 | * our allocation attempt failed at least once. So, we must sleep |
| 4906 | * and let some progress happen before we try again. |
| 4907 | * |
| 4908 | * This function will sleep at least once waiting for new free space to |
| 4909 | * show up, and then it will check the block group free space numbers |
| 4910 | * for our min num_bytes. Another option is to have it go ahead |
| 4911 | * and look in the rbtree for a free extent of a given size, but this |
| 4912 | * is a good start. |
| 4913 | */ |
| 4914 | static noinline int |
| 4915 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4916 | u64 num_bytes) |
| 4917 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4918 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4919 | DEFINE_WAIT(wait); |
| 4920 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4921 | caching_ctl = get_caching_control(cache); |
| 4922 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4923 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4924 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4925 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4926 | (cache->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4927 | |
| 4928 | put_caching_control(caching_ctl); |
| 4929 | return 0; |
| 4930 | } |
| 4931 | |
| 4932 | static noinline int |
| 4933 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4934 | { |
| 4935 | struct btrfs_caching_control *caching_ctl; |
| 4936 | DEFINE_WAIT(wait); |
| 4937 | |
| 4938 | caching_ctl = get_caching_control(cache); |
| 4939 | if (!caching_ctl) |
| 4940 | return 0; |
| 4941 | |
| 4942 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4943 | |
| 4944 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4945 | return 0; |
| 4946 | } |
| 4947 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4948 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4949 | { |
| 4950 | int index; |
| 4951 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4952 | index = 0; |
| 4953 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4954 | index = 1; |
| 4955 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4956 | index = 2; |
| 4957 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4958 | index = 3; |
| 4959 | else |
| 4960 | index = 4; |
| 4961 | return index; |
| 4962 | } |
| 4963 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4964 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4965 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4966 | LOOP_CACHING_NOWAIT = 1, |
| 4967 | LOOP_CACHING_WAIT = 2, |
| 4968 | LOOP_ALLOC_CHUNK = 3, |
| 4969 | LOOP_NO_EMPTY_SIZE = 4, |
| 4970 | }; |
| 4971 | |
| 4972 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4973 | * walks the btree of allocated extents and find a hole of a given size. |
| 4974 | * The key ins is changed to record the hole: |
| 4975 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4976 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4977 | * ins->offset == number of blocks |
| 4978 | * Any available blocks before search_start are skipped. |
| 4979 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4980 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4981 | struct btrfs_root *orig_root, |
| 4982 | u64 num_bytes, u64 empty_size, |
| 4983 | u64 search_start, u64 search_end, |
| 4984 | u64 hint_byte, struct btrfs_key *ins, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4985 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4986 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4987 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4988 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4989 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4990 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4991 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4992 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4993 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4994 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4995 | int last_ptr_loop = 0; |
| 4996 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4997 | int index = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4998 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4999 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5000 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5001 | bool use_cluster = true; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5002 | u64 ideal_cache_percent = 0; |
| 5003 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5004 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5005 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 5006 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5007 | ins->objectid = 0; |
| 5008 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 5009 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5010 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 5011 | if (!space_info) { |
| 5012 | printk(KERN_ERR "No space info for %d\n", data); |
| 5013 | return -ENOSPC; |
| 5014 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5015 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5016 | /* |
| 5017 | * If the space info is for both data and metadata it means we have a |
| 5018 | * small filesystem and we can't use the clustering stuff. |
| 5019 | */ |
| 5020 | if (btrfs_mixed_space_info(space_info)) |
| 5021 | use_cluster = false; |
| 5022 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5023 | if (orig_root->ref_cows || empty_size) |
| 5024 | allowed_chunk_alloc = 1; |
| 5025 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5026 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5027 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 5028 | if (!btrfs_test_opt(root, SSD)) |
| 5029 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5030 | } |
| 5031 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5032 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
| 5033 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5034 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 5035 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5036 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5037 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5038 | spin_lock(&last_ptr->lock); |
| 5039 | if (last_ptr->block_group) |
| 5040 | hint_byte = last_ptr->window_start; |
| 5041 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5042 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5043 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5044 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5045 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5046 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5047 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5048 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5049 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5050 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5051 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5052 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 5053 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5054 | /* |
| 5055 | * we don't want to use the block group if it doesn't match our |
| 5056 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5057 | * |
| 5058 | * However if we are re-searching with an ideal block group |
| 5059 | * picked out then we don't care that the block group is cached. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5060 | */ |
| 5061 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5062 | (block_group->cached != BTRFS_CACHE_NO || |
| 5063 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5064 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5065 | if (list_empty(&block_group->list) || |
| 5066 | block_group->ro) { |
| 5067 | /* |
| 5068 | * someone is removing this block group, |
| 5069 | * we can't jump into the have_block_group |
| 5070 | * target because our list pointers are not |
| 5071 | * valid |
| 5072 | */ |
| 5073 | btrfs_put_block_group(block_group); |
| 5074 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5075 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5076 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5077 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5078 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5079 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5080 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5081 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5082 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5083 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5084 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5085 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 5086 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5087 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5088 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 5089 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5090 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5091 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5092 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 5093 | /* |
| 5094 | * this can happen if we end up cycling through all the |
| 5095 | * raid types, but we want to make sure we only allocate |
| 5096 | * for the proper type. |
| 5097 | */ |
| 5098 | if (!block_group_bits(block_group, data)) { |
| 5099 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 5100 | BTRFS_BLOCK_GROUP_RAID1 | |
| 5101 | BTRFS_BLOCK_GROUP_RAID10; |
| 5102 | |
| 5103 | /* |
| 5104 | * if they asked for extra copies and this block group |
| 5105 | * doesn't provide them, bail. This does allow us to |
| 5106 | * fill raid0 from raid1. |
| 5107 | */ |
| 5108 | if ((data & extra) && !(block_group->flags & extra)) |
| 5109 | goto loop; |
| 5110 | } |
| 5111 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5112 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5113 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5114 | u64 free_percent; |
| 5115 | |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5116 | ret = cache_block_group(block_group, trans, |
| 5117 | orig_root, 1); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 5118 | if (block_group->cached == BTRFS_CACHE_FINISHED) |
| 5119 | goto have_block_group; |
| 5120 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5121 | free_percent = btrfs_block_group_used(&block_group->item); |
| 5122 | free_percent *= 100; |
| 5123 | free_percent = div64_u64(free_percent, |
| 5124 | block_group->key.offset); |
| 5125 | free_percent = 100 - free_percent; |
| 5126 | if (free_percent > ideal_cache_percent && |
| 5127 | likely(!block_group->ro)) { |
| 5128 | ideal_cache_offset = block_group->key.objectid; |
| 5129 | ideal_cache_percent = free_percent; |
| 5130 | } |
| 5131 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5132 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5133 | * We only want to start kthread caching if we are at |
| 5134 | * the point where we will wait for caching to make |
| 5135 | * progress, or if our ideal search is over and we've |
| 5136 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5137 | */ |
| 5138 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5139 | (loop > LOOP_FIND_IDEAL && |
| 5140 | atomic_read(&space_info->caching_threads) < 2)) { |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5141 | ret = cache_block_group(block_group, trans, |
| 5142 | orig_root, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5143 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5144 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5145 | found_uncached_bg = true; |
| 5146 | |
| 5147 | /* |
| 5148 | * If loop is set for cached only, try the next block |
| 5149 | * group. |
| 5150 | */ |
| 5151 | if (loop == LOOP_FIND_IDEAL) |
| 5152 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5153 | } |
| 5154 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5155 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5156 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5157 | found_uncached_bg = true; |
| 5158 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5159 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5160 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5161 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5162 | /* |
| 5163 | * Ok we want to try and use the cluster allocator, so lets look |
| 5164 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 5165 | * have tried the cluster allocator plenty of times at this |
| 5166 | * point and not have found anything, so we are likely way too |
| 5167 | * fragmented for the clustering stuff to find anything, so lets |
| 5168 | * just skip it and let the allocator find whatever block it can |
| 5169 | * find |
| 5170 | */ |
| 5171 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5172 | /* |
| 5173 | * the refill lock keeps out other |
| 5174 | * people trying to start a new cluster |
| 5175 | */ |
| 5176 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5177 | if (last_ptr->block_group && |
| 5178 | (last_ptr->block_group->ro || |
| 5179 | !block_group_bits(last_ptr->block_group, data))) { |
| 5180 | offset = 0; |
| 5181 | goto refill_cluster; |
| 5182 | } |
| 5183 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5184 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 5185 | num_bytes, search_start); |
| 5186 | if (offset) { |
| 5187 | /* we have a block, we're done */ |
| 5188 | spin_unlock(&last_ptr->refill_lock); |
| 5189 | goto checks; |
| 5190 | } |
| 5191 | |
| 5192 | spin_lock(&last_ptr->lock); |
| 5193 | /* |
| 5194 | * whoops, this cluster doesn't actually point to |
| 5195 | * this block group. Get a ref on the block |
| 5196 | * group is does point to and try again |
| 5197 | */ |
| 5198 | if (!last_ptr_loop && last_ptr->block_group && |
| 5199 | last_ptr->block_group != block_group) { |
| 5200 | |
| 5201 | btrfs_put_block_group(block_group); |
| 5202 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5203 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5204 | spin_unlock(&last_ptr->lock); |
| 5205 | spin_unlock(&last_ptr->refill_lock); |
| 5206 | |
| 5207 | last_ptr_loop = 1; |
| 5208 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5209 | /* |
| 5210 | * we know this block group is properly |
| 5211 | * in the list because |
| 5212 | * btrfs_remove_block_group, drops the |
| 5213 | * cluster before it removes the block |
| 5214 | * group from the list |
| 5215 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5216 | goto have_block_group; |
| 5217 | } |
| 5218 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5219 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5220 | /* |
| 5221 | * this cluster didn't work out, free it and |
| 5222 | * start over |
| 5223 | */ |
| 5224 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5225 | |
| 5226 | last_ptr_loop = 0; |
| 5227 | |
| 5228 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 5229 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5230 | block_group, last_ptr, |
| 5231 | offset, num_bytes, |
| 5232 | empty_cluster + empty_size); |
| 5233 | if (ret == 0) { |
| 5234 | /* |
| 5235 | * now pull our allocation out of this |
| 5236 | * cluster |
| 5237 | */ |
| 5238 | offset = btrfs_alloc_from_cluster(block_group, |
| 5239 | last_ptr, num_bytes, |
| 5240 | search_start); |
| 5241 | if (offset) { |
| 5242 | /* we found one, proceed */ |
| 5243 | spin_unlock(&last_ptr->refill_lock); |
| 5244 | goto checks; |
| 5245 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5246 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 5247 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5248 | spin_unlock(&last_ptr->refill_lock); |
| 5249 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5250 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5251 | wait_block_group_cache_progress(block_group, |
| 5252 | num_bytes + empty_cluster + empty_size); |
| 5253 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5254 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5255 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5256 | /* |
| 5257 | * at this point we either didn't find a cluster |
| 5258 | * or we weren't able to allocate a block from our |
| 5259 | * cluster. Free the cluster we've been trying |
| 5260 | * to use, and go to the next block group |
| 5261 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5262 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5263 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5264 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5265 | } |
| 5266 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5267 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 5268 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5269 | /* |
| 5270 | * If we didn't find a chunk, and we haven't failed on this |
| 5271 | * block group before, and this block group is in the middle of |
| 5272 | * caching and we are ok with waiting, then go ahead and wait |
| 5273 | * for progress to be made, and set failed_alloc to true. |
| 5274 | * |
| 5275 | * If failed_alloc is true then we've already waited on this |
| 5276 | * block group once and should move on to the next block group. |
| 5277 | */ |
| 5278 | if (!offset && !failed_alloc && !cached && |
| 5279 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5280 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5281 | num_bytes + empty_size); |
| 5282 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5283 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5284 | } else if (!offset) { |
| 5285 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5286 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5287 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5288 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5289 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5290 | if (search_start + num_bytes >= search_end) { |
| 5291 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5292 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5293 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 5294 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5295 | /* move on to the next group */ |
| 5296 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5297 | block_group->key.objectid + block_group->key.offset) { |
| 5298 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5299 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5300 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5301 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5302 | ins->objectid = search_start; |
| 5303 | ins->offset = num_bytes; |
| 5304 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5305 | if (offset < search_start) |
| 5306 | btrfs_add_free_space(block_group, offset, |
| 5307 | search_start - offset); |
| 5308 | BUG_ON(offset > search_start); |
| 5309 | |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5310 | ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5311 | (data & BTRFS_BLOCK_GROUP_DATA)); |
| 5312 | if (ret == -EAGAIN) { |
| 5313 | btrfs_add_free_space(block_group, offset, num_bytes); |
| 5314 | goto loop; |
| 5315 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5316 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5317 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5318 | ins->objectid = search_start; |
| 5319 | ins->offset = num_bytes; |
| 5320 | |
| 5321 | if (offset < search_start) |
| 5322 | btrfs_add_free_space(block_group, offset, |
| 5323 | search_start - offset); |
| 5324 | BUG_ON(offset > search_start); |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5325 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5326 | break; |
| 5327 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5328 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5329 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5330 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5331 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5332 | } |
| 5333 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 5334 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5335 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 5336 | goto search; |
| 5337 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5338 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 5339 | * for them to make caching progress. Also |
| 5340 | * determine the best possible bg to cache |
| 5341 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 5342 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5343 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 5344 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 5345 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 5346 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5347 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5348 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE && |
| 5349 | (found_uncached_bg || empty_size || empty_cluster || |
| 5350 | allowed_chunk_alloc)) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5351 | index = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5352 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5353 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5354 | loop++; |
| 5355 | if (!ideal_cache_percent && |
| 5356 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5357 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5358 | |
| 5359 | /* |
| 5360 | * 1 of the following 2 things have happened so far |
| 5361 | * |
| 5362 | * 1) We found an ideal block group for caching that |
| 5363 | * is mostly full and will cache quickly, so we might |
| 5364 | * as well wait for it. |
| 5365 | * |
| 5366 | * 2) We searched for cached only and we didn't find |
| 5367 | * anything, and we didn't start any caching kthreads |
| 5368 | * either, so chances are we will loop through and |
| 5369 | * start a couple caching kthreads, and then come back |
| 5370 | * around and just wait for them. This will be slower |
| 5371 | * because we will have 2 caching kthreads reading at |
| 5372 | * the same time when we could have just started one |
| 5373 | * and waited for it to get far enough to give us an |
| 5374 | * allocation, so go ahead and go to the wait caching |
| 5375 | * loop. |
| 5376 | */ |
| 5377 | loop = LOOP_CACHING_WAIT; |
| 5378 | search_start = ideal_cache_offset; |
| 5379 | ideal_cache_percent = 0; |
| 5380 | goto ideal_cache; |
| 5381 | } else if (loop == LOOP_FIND_IDEAL) { |
| 5382 | /* |
| 5383 | * Didn't find a uncached bg, wait on anything we find |
| 5384 | * next. |
| 5385 | */ |
| 5386 | loop = LOOP_CACHING_WAIT; |
| 5387 | goto search; |
| 5388 | } |
| 5389 | |
| 5390 | if (loop < LOOP_CACHING_WAIT) { |
| 5391 | loop++; |
| 5392 | goto search; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5393 | } |
| 5394 | |
| 5395 | if (loop == LOOP_ALLOC_CHUNK) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5396 | empty_size = 0; |
| 5397 | empty_cluster = 0; |
| 5398 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5399 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5400 | if (allowed_chunk_alloc) { |
| 5401 | ret = do_chunk_alloc(trans, root, num_bytes + |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5402 | 2 * 1024 * 1024, data, |
| 5403 | CHUNK_ALLOC_LIMITED); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5404 | allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5405 | done_chunk_alloc = 1; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5406 | } else if (!done_chunk_alloc && |
| 5407 | space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) { |
| 5408 | space_info->force_alloc = CHUNK_ALLOC_LIMITED; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5409 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5410 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5411 | if (loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5412 | loop++; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5413 | goto search; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5414 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5415 | ret = -ENOSPC; |
| 5416 | } else if (!ins->objectid) { |
| 5417 | ret = -ENOSPC; |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5418 | } else if (ins->objectid) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5419 | ret = 0; |
| 5420 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5421 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5422 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5423 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5424 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5425 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 5426 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5427 | { |
| 5428 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5429 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5430 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5431 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5432 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 5433 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5434 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5435 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5436 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5437 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 5438 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 5439 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5440 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5441 | (unsigned long long)info->bytes_pinned, |
| 5442 | (unsigned long long)info->bytes_reserved, |
| 5443 | (unsigned long long)info->bytes_may_use, |
| 5444 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5445 | spin_unlock(&info->lock); |
| 5446 | |
| 5447 | if (!dump_block_groups) |
| 5448 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5449 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5450 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5451 | again: |
| 5452 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5453 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5454 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 5455 | "%llu pinned %llu reserved\n", |
| 5456 | (unsigned long long)cache->key.objectid, |
| 5457 | (unsigned long long)cache->key.offset, |
| 5458 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 5459 | (unsigned long long)cache->pinned, |
| 5460 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5461 | btrfs_dump_free_space(cache, bytes); |
| 5462 | spin_unlock(&cache->lock); |
| 5463 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5464 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5465 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5466 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5467 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5468 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5469 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5470 | struct btrfs_root *root, |
| 5471 | u64 num_bytes, u64 min_alloc_size, |
| 5472 | u64 empty_size, u64 hint_byte, |
| 5473 | u64 search_end, struct btrfs_key *ins, |
| 5474 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5475 | { |
| 5476 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5477 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5478 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5479 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5480 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5481 | /* |
| 5482 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5483 | * is not called recursively on allocations |
| 5484 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5485 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5486 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5487 | num_bytes + 2 * 1024 * 1024, data, |
| 5488 | CHUNK_ALLOC_NO_FORCE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5489 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5490 | WARN_ON(num_bytes < root->sectorsize); |
| 5491 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5492 | search_start, search_end, hint_byte, |
| 5493 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5494 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5495 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5496 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5497 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5498 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5499 | do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5500 | num_bytes, data, CHUNK_ALLOC_FORCE); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5501 | goto again; |
| 5502 | } |
Chris Mason | 9143565 | 2011-02-16 13:10:41 -0500 | [diff] [blame] | 5503 | if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5504 | struct btrfs_space_info *sinfo; |
| 5505 | |
| 5506 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5507 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5508 | "wanted %llu\n", (unsigned long long)data, |
| 5509 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5510 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5511 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5512 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5513 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
| 5514 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5515 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5516 | } |
| 5517 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5518 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 5519 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5520 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5521 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5522 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5523 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5524 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5525 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5526 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5527 | return -ENOSPC; |
| 5528 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5529 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5530 | if (btrfs_test_opt(root, DISCARD)) |
| 5531 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5532 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5533 | btrfs_add_free_space(cache, start, len); |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5534 | btrfs_update_reserved_bytes(cache, len, 0, 1); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5535 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5536 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5537 | trace_btrfs_reserved_extent_free(root, start, len); |
| 5538 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5539 | return ret; |
| 5540 | } |
| 5541 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5542 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5543 | struct btrfs_root *root, |
| 5544 | u64 parent, u64 root_objectid, |
| 5545 | u64 flags, u64 owner, u64 offset, |
| 5546 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5547 | { |
| 5548 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5549 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5550 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5551 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5552 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5553 | struct extent_buffer *leaf; |
| 5554 | int type; |
| 5555 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5556 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5557 | if (parent > 0) |
| 5558 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5559 | else |
| 5560 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5561 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5562 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5563 | |
| 5564 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5565 | if (!path) |
| 5566 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5567 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5568 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5569 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5570 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5571 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5572 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5573 | leaf = path->nodes[0]; |
| 5574 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5575 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5576 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5577 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5578 | btrfs_set_extent_flags(leaf, extent_item, |
| 5579 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5580 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5581 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5582 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5583 | if (parent > 0) { |
| 5584 | struct btrfs_shared_data_ref *ref; |
| 5585 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5586 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5587 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5588 | } else { |
| 5589 | struct btrfs_extent_data_ref *ref; |
| 5590 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5591 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5592 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5593 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5594 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5595 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5596 | |
| 5597 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5598 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5599 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5600 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5601 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5602 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5603 | "%llu\n", (unsigned long long)ins->objectid, |
| 5604 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5605 | BUG(); |
| 5606 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5607 | return ret; |
| 5608 | } |
| 5609 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5610 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5611 | struct btrfs_root *root, |
| 5612 | u64 parent, u64 root_objectid, |
| 5613 | u64 flags, struct btrfs_disk_key *key, |
| 5614 | int level, struct btrfs_key *ins) |
| 5615 | { |
| 5616 | int ret; |
| 5617 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5618 | struct btrfs_extent_item *extent_item; |
| 5619 | struct btrfs_tree_block_info *block_info; |
| 5620 | struct btrfs_extent_inline_ref *iref; |
| 5621 | struct btrfs_path *path; |
| 5622 | struct extent_buffer *leaf; |
| 5623 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5624 | |
| 5625 | path = btrfs_alloc_path(); |
| 5626 | BUG_ON(!path); |
| 5627 | |
| 5628 | path->leave_spinning = 1; |
| 5629 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5630 | ins, size); |
| 5631 | BUG_ON(ret); |
| 5632 | |
| 5633 | leaf = path->nodes[0]; |
| 5634 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5635 | struct btrfs_extent_item); |
| 5636 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5637 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5638 | btrfs_set_extent_flags(leaf, extent_item, |
| 5639 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5640 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5641 | |
| 5642 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5643 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5644 | |
| 5645 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5646 | if (parent > 0) { |
| 5647 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5648 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5649 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5650 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5651 | } else { |
| 5652 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5653 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5654 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5655 | } |
| 5656 | |
| 5657 | btrfs_mark_buffer_dirty(leaf); |
| 5658 | btrfs_free_path(path); |
| 5659 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5660 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5661 | if (ret) { |
| 5662 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5663 | "%llu\n", (unsigned long long)ins->objectid, |
| 5664 | (unsigned long long)ins->offset); |
| 5665 | BUG(); |
| 5666 | } |
| 5667 | return ret; |
| 5668 | } |
| 5669 | |
| 5670 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5671 | struct btrfs_root *root, |
| 5672 | u64 root_objectid, u64 owner, |
| 5673 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5674 | { |
| 5675 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5676 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5677 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5678 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5679 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5680 | 0, root_objectid, owner, offset, |
| 5681 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5682 | return ret; |
| 5683 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5684 | |
| 5685 | /* |
| 5686 | * this is used by the tree logging recovery code. It records that |
| 5687 | * an extent has been allocated and makes sure to clear the free |
| 5688 | * space cache bits as well |
| 5689 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5690 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5691 | struct btrfs_root *root, |
| 5692 | u64 root_objectid, u64 owner, u64 offset, |
| 5693 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5694 | { |
| 5695 | int ret; |
| 5696 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5697 | struct btrfs_caching_control *caching_ctl; |
| 5698 | u64 start = ins->objectid; |
| 5699 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5700 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5701 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5702 | cache_block_group(block_group, trans, NULL, 0); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5703 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5704 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5705 | if (!caching_ctl) { |
| 5706 | BUG_ON(!block_group_cache_done(block_group)); |
| 5707 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5708 | BUG_ON(ret); |
| 5709 | } else { |
| 5710 | mutex_lock(&caching_ctl->mutex); |
| 5711 | |
| 5712 | if (start >= caching_ctl->progress) { |
| 5713 | ret = add_excluded_extent(root, start, num_bytes); |
| 5714 | BUG_ON(ret); |
| 5715 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5716 | ret = btrfs_remove_free_space(block_group, |
| 5717 | start, num_bytes); |
| 5718 | BUG_ON(ret); |
| 5719 | } else { |
| 5720 | num_bytes = caching_ctl->progress - start; |
| 5721 | ret = btrfs_remove_free_space(block_group, |
| 5722 | start, num_bytes); |
| 5723 | BUG_ON(ret); |
| 5724 | |
| 5725 | start = caching_ctl->progress; |
| 5726 | num_bytes = ins->objectid + ins->offset - |
| 5727 | caching_ctl->progress; |
| 5728 | ret = add_excluded_extent(root, start, num_bytes); |
| 5729 | BUG_ON(ret); |
| 5730 | } |
| 5731 | |
| 5732 | mutex_unlock(&caching_ctl->mutex); |
| 5733 | put_caching_control(caching_ctl); |
| 5734 | } |
| 5735 | |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5736 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5737 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5738 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5739 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5740 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5741 | return ret; |
| 5742 | } |
| 5743 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5744 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5745 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5746 | u64 bytenr, u32 blocksize, |
| 5747 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5748 | { |
| 5749 | struct extent_buffer *buf; |
| 5750 | |
| 5751 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5752 | if (!buf) |
| 5753 | return ERR_PTR(-ENOMEM); |
| 5754 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5755 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5756 | btrfs_tree_lock(buf); |
| 5757 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5758 | |
| 5759 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5760 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5761 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5762 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5763 | /* |
| 5764 | * we allow two log transactions at a time, use different |
| 5765 | * EXENT bit to differentiate dirty pages. |
| 5766 | */ |
| 5767 | if (root->log_transid % 2 == 0) |
| 5768 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5769 | buf->start + buf->len - 1, GFP_NOFS); |
| 5770 | else |
| 5771 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5772 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5773 | } else { |
| 5774 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5775 | buf->start + buf->len - 1, GFP_NOFS); |
| 5776 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5777 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5778 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5779 | return buf; |
| 5780 | } |
| 5781 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5782 | static struct btrfs_block_rsv * |
| 5783 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5784 | struct btrfs_root *root, u32 blocksize) |
| 5785 | { |
| 5786 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5787 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5788 | int ret; |
| 5789 | |
| 5790 | block_rsv = get_block_rsv(trans, root); |
| 5791 | |
| 5792 | if (block_rsv->size == 0) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5793 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 5794 | blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5795 | /* |
| 5796 | * If we couldn't reserve metadata bytes try and use some from |
| 5797 | * the global reserve. |
| 5798 | */ |
| 5799 | if (ret && block_rsv != global_rsv) { |
| 5800 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5801 | if (!ret) |
| 5802 | return global_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5803 | return ERR_PTR(ret); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5804 | } else if (ret) { |
| 5805 | return ERR_PTR(ret); |
| 5806 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5807 | return block_rsv; |
| 5808 | } |
| 5809 | |
| 5810 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5811 | if (!ret) |
| 5812 | return block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5813 | if (ret) { |
| 5814 | WARN_ON(1); |
| 5815 | ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize, |
| 5816 | 0); |
| 5817 | if (!ret) { |
| 5818 | spin_lock(&block_rsv->lock); |
| 5819 | block_rsv->size += blocksize; |
| 5820 | spin_unlock(&block_rsv->lock); |
| 5821 | return block_rsv; |
| 5822 | } else if (ret && block_rsv != global_rsv) { |
| 5823 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5824 | if (!ret) |
| 5825 | return global_rsv; |
| 5826 | } |
| 5827 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5828 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5829 | return ERR_PTR(-ENOSPC); |
| 5830 | } |
| 5831 | |
| 5832 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5833 | { |
| 5834 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5835 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5836 | } |
| 5837 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5838 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5839 | * finds a free extent and does all the dirty work required for allocation |
| 5840 | * returns the key for the extent through ins, and a tree buffer for |
| 5841 | * the first block of the extent through buf. |
| 5842 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5843 | * returns the tree buffer or NULL. |
| 5844 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5845 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5846 | struct btrfs_root *root, u32 blocksize, |
| 5847 | u64 parent, u64 root_objectid, |
| 5848 | struct btrfs_disk_key *key, int level, |
| 5849 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5850 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5851 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5852 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5853 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5854 | u64 flags = 0; |
| 5855 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5856 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5857 | |
| 5858 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5859 | if (IS_ERR(block_rsv)) |
| 5860 | return ERR_CAST(block_rsv); |
| 5861 | |
| 5862 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5863 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5864 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5865 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5866 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5867 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5868 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5869 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5870 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5871 | BUG_ON(IS_ERR(buf)); |
| 5872 | |
| 5873 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5874 | if (parent == 0) |
| 5875 | parent = ins.objectid; |
| 5876 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5877 | } else |
| 5878 | BUG_ON(parent > 0); |
| 5879 | |
| 5880 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5881 | struct btrfs_delayed_extent_op *extent_op; |
| 5882 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5883 | BUG_ON(!extent_op); |
| 5884 | if (key) |
| 5885 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5886 | else |
| 5887 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5888 | extent_op->flags_to_set = flags; |
| 5889 | extent_op->update_key = 1; |
| 5890 | extent_op->update_flags = 1; |
| 5891 | extent_op->is_data = 0; |
| 5892 | |
| 5893 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5894 | ins.offset, parent, root_objectid, |
| 5895 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5896 | extent_op); |
| 5897 | BUG_ON(ret); |
| 5898 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5899 | return buf; |
| 5900 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5901 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5902 | struct walk_control { |
| 5903 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5904 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5905 | struct btrfs_key update_progress; |
| 5906 | int stage; |
| 5907 | int level; |
| 5908 | int shared_level; |
| 5909 | int update_ref; |
| 5910 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5911 | int reada_slot; |
| 5912 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5913 | }; |
| 5914 | |
| 5915 | #define DROP_REFERENCE 1 |
| 5916 | #define UPDATE_BACKREF 2 |
| 5917 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5918 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5919 | struct btrfs_root *root, |
| 5920 | struct walk_control *wc, |
| 5921 | struct btrfs_path *path) |
| 5922 | { |
| 5923 | u64 bytenr; |
| 5924 | u64 generation; |
| 5925 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5926 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5927 | u32 nritems; |
| 5928 | u32 blocksize; |
| 5929 | struct btrfs_key key; |
| 5930 | struct extent_buffer *eb; |
| 5931 | int ret; |
| 5932 | int slot; |
| 5933 | int nread = 0; |
| 5934 | |
| 5935 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5936 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5937 | wc->reada_count = max(wc->reada_count, 2); |
| 5938 | } else { |
| 5939 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5940 | wc->reada_count = min_t(int, wc->reada_count, |
| 5941 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5942 | } |
| 5943 | |
| 5944 | eb = path->nodes[wc->level]; |
| 5945 | nritems = btrfs_header_nritems(eb); |
| 5946 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5947 | |
| 5948 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5949 | if (nread >= wc->reada_count) |
| 5950 | break; |
| 5951 | |
| 5952 | cond_resched(); |
| 5953 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5954 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5955 | |
| 5956 | if (slot == path->slots[wc->level]) |
| 5957 | goto reada; |
| 5958 | |
| 5959 | if (wc->stage == UPDATE_BACKREF && |
| 5960 | generation <= root->root_key.offset) |
| 5961 | continue; |
| 5962 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5963 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5964 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5965 | &refs, &flags); |
| 5966 | BUG_ON(ret); |
| 5967 | BUG_ON(refs == 0); |
| 5968 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5969 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5970 | if (refs == 1) |
| 5971 | goto reada; |
| 5972 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5973 | if (wc->level == 1 && |
| 5974 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5975 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5976 | if (!wc->update_ref || |
| 5977 | generation <= root->root_key.offset) |
| 5978 | continue; |
| 5979 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5980 | ret = btrfs_comp_cpu_keys(&key, |
| 5981 | &wc->update_progress); |
| 5982 | if (ret < 0) |
| 5983 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5984 | } else { |
| 5985 | if (wc->level == 1 && |
| 5986 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5987 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5988 | } |
| 5989 | reada: |
| 5990 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5991 | generation); |
| 5992 | if (ret) |
| 5993 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5994 | nread++; |
| 5995 | } |
| 5996 | wc->reada_slot = slot; |
| 5997 | } |
| 5998 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5999 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6000 | * hepler to process tree block while walking down the tree. |
| 6001 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6002 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 6003 | * back refs for pointers in the block. |
| 6004 | * |
| 6005 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6006 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6007 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 6008 | struct btrfs_root *root, |
| 6009 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6010 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6011 | { |
| 6012 | int level = wc->level; |
| 6013 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6014 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6015 | int ret; |
| 6016 | |
| 6017 | if (wc->stage == UPDATE_BACKREF && |
| 6018 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 6019 | return 1; |
| 6020 | |
| 6021 | /* |
| 6022 | * when reference count of tree block is 1, it won't increase |
| 6023 | * again. once full backref flag is set, we never clear it. |
| 6024 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6025 | if (lookup_info && |
| 6026 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 6027 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6028 | BUG_ON(!path->locks[level]); |
| 6029 | ret = btrfs_lookup_extent_info(trans, root, |
| 6030 | eb->start, eb->len, |
| 6031 | &wc->refs[level], |
| 6032 | &wc->flags[level]); |
| 6033 | BUG_ON(ret); |
| 6034 | BUG_ON(wc->refs[level] == 0); |
| 6035 | } |
| 6036 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6037 | if (wc->stage == DROP_REFERENCE) { |
| 6038 | if (wc->refs[level] > 1) |
| 6039 | return 1; |
| 6040 | |
| 6041 | if (path->locks[level] && !wc->keep_locks) { |
| 6042 | btrfs_tree_unlock(eb); |
| 6043 | path->locks[level] = 0; |
| 6044 | } |
| 6045 | return 0; |
| 6046 | } |
| 6047 | |
| 6048 | /* wc->stage == UPDATE_BACKREF */ |
| 6049 | if (!(wc->flags[level] & flag)) { |
| 6050 | BUG_ON(!path->locks[level]); |
| 6051 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 6052 | BUG_ON(ret); |
| 6053 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6054 | BUG_ON(ret); |
| 6055 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 6056 | eb->len, flag, 0); |
| 6057 | BUG_ON(ret); |
| 6058 | wc->flags[level] |= flag; |
| 6059 | } |
| 6060 | |
| 6061 | /* |
| 6062 | * the block is shared by multiple trees, so it's not good to |
| 6063 | * keep the tree lock |
| 6064 | */ |
| 6065 | if (path->locks[level] && level > 0) { |
| 6066 | btrfs_tree_unlock(eb); |
| 6067 | path->locks[level] = 0; |
| 6068 | } |
| 6069 | return 0; |
| 6070 | } |
| 6071 | |
| 6072 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6073 | * hepler to process tree block pointer. |
| 6074 | * |
| 6075 | * when wc->stage == DROP_REFERENCE, this function checks |
| 6076 | * reference count of the block pointed to. if the block |
| 6077 | * is shared and we need update back refs for the subtree |
| 6078 | * rooted at the block, this function changes wc->stage to |
| 6079 | * UPDATE_BACKREF. if the block is shared and there is no |
| 6080 | * need to update back, this function drops the reference |
| 6081 | * to the block. |
| 6082 | * |
| 6083 | * NOTE: return value 1 means we should stop walking down. |
| 6084 | */ |
| 6085 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 6086 | struct btrfs_root *root, |
| 6087 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6088 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6089 | { |
| 6090 | u64 bytenr; |
| 6091 | u64 generation; |
| 6092 | u64 parent; |
| 6093 | u32 blocksize; |
| 6094 | struct btrfs_key key; |
| 6095 | struct extent_buffer *next; |
| 6096 | int level = wc->level; |
| 6097 | int reada = 0; |
| 6098 | int ret = 0; |
| 6099 | |
| 6100 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 6101 | path->slots[level]); |
| 6102 | /* |
| 6103 | * if the lower level block was created before the snapshot |
| 6104 | * was created, we know there is no need to update back refs |
| 6105 | * for the subtree |
| 6106 | */ |
| 6107 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6108 | generation <= root->root_key.offset) { |
| 6109 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6110 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6111 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6112 | |
| 6113 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 6114 | blocksize = btrfs_level_size(root, level - 1); |
| 6115 | |
| 6116 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 6117 | if (!next) { |
| 6118 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6119 | if (!next) |
| 6120 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6121 | reada = 1; |
| 6122 | } |
| 6123 | btrfs_tree_lock(next); |
| 6124 | btrfs_set_lock_blocking(next); |
| 6125 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6126 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6127 | &wc->refs[level - 1], |
| 6128 | &wc->flags[level - 1]); |
| 6129 | BUG_ON(ret); |
| 6130 | BUG_ON(wc->refs[level - 1] == 0); |
| 6131 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6132 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6133 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6134 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6135 | if (level == 1 && |
| 6136 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6137 | goto skip; |
| 6138 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6139 | if (!wc->update_ref || |
| 6140 | generation <= root->root_key.offset) |
| 6141 | goto skip; |
| 6142 | |
| 6143 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 6144 | path->slots[level]); |
| 6145 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 6146 | if (ret < 0) |
| 6147 | goto skip; |
| 6148 | |
| 6149 | wc->stage = UPDATE_BACKREF; |
| 6150 | wc->shared_level = level - 1; |
| 6151 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6152 | } else { |
| 6153 | if (level == 1 && |
| 6154 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6155 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6156 | } |
| 6157 | |
| 6158 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 6159 | btrfs_tree_unlock(next); |
| 6160 | free_extent_buffer(next); |
| 6161 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6162 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6163 | } |
| 6164 | |
| 6165 | if (!next) { |
| 6166 | if (reada && level == 1) |
| 6167 | reada_walk_down(trans, root, wc, path); |
| 6168 | next = read_tree_block(root, bytenr, blocksize, generation); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 6169 | if (!next) |
| 6170 | return -EIO; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6171 | btrfs_tree_lock(next); |
| 6172 | btrfs_set_lock_blocking(next); |
| 6173 | } |
| 6174 | |
| 6175 | level--; |
| 6176 | BUG_ON(level != btrfs_header_level(next)); |
| 6177 | path->nodes[level] = next; |
| 6178 | path->slots[level] = 0; |
| 6179 | path->locks[level] = 1; |
| 6180 | wc->level = level; |
| 6181 | if (wc->level == 1) |
| 6182 | wc->reada_slot = 0; |
| 6183 | return 0; |
| 6184 | skip: |
| 6185 | wc->refs[level - 1] = 0; |
| 6186 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6187 | if (wc->stage == DROP_REFERENCE) { |
| 6188 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6189 | parent = path->nodes[level]->start; |
| 6190 | } else { |
| 6191 | BUG_ON(root->root_key.objectid != |
| 6192 | btrfs_header_owner(path->nodes[level])); |
| 6193 | parent = 0; |
| 6194 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6195 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6196 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 6197 | root->root_key.objectid, level - 1, 0); |
| 6198 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6199 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6200 | btrfs_tree_unlock(next); |
| 6201 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6202 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6203 | return 1; |
| 6204 | } |
| 6205 | |
| 6206 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6207 | * hepler to process tree block while walking up the tree. |
| 6208 | * |
| 6209 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6210 | * reference count on the block. |
| 6211 | * |
| 6212 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6213 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6214 | * to UPDATE_BACKREF previously while processing the block. |
| 6215 | * |
| 6216 | * NOTE: return value 1 means we should stop walking up. |
| 6217 | */ |
| 6218 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6219 | struct btrfs_root *root, |
| 6220 | struct btrfs_path *path, |
| 6221 | struct walk_control *wc) |
| 6222 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6223 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6224 | int level = wc->level; |
| 6225 | struct extent_buffer *eb = path->nodes[level]; |
| 6226 | u64 parent = 0; |
| 6227 | |
| 6228 | if (wc->stage == UPDATE_BACKREF) { |
| 6229 | BUG_ON(wc->shared_level < level); |
| 6230 | if (level < wc->shared_level) |
| 6231 | goto out; |
| 6232 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6233 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6234 | if (ret > 0) |
| 6235 | wc->update_ref = 0; |
| 6236 | |
| 6237 | wc->stage = DROP_REFERENCE; |
| 6238 | wc->shared_level = -1; |
| 6239 | path->slots[level] = 0; |
| 6240 | |
| 6241 | /* |
| 6242 | * check reference count again if the block isn't locked. |
| 6243 | * we should start walking down the tree again if reference |
| 6244 | * count is one. |
| 6245 | */ |
| 6246 | if (!path->locks[level]) { |
| 6247 | BUG_ON(level == 0); |
| 6248 | btrfs_tree_lock(eb); |
| 6249 | btrfs_set_lock_blocking(eb); |
| 6250 | path->locks[level] = 1; |
| 6251 | |
| 6252 | ret = btrfs_lookup_extent_info(trans, root, |
| 6253 | eb->start, eb->len, |
| 6254 | &wc->refs[level], |
| 6255 | &wc->flags[level]); |
| 6256 | BUG_ON(ret); |
| 6257 | BUG_ON(wc->refs[level] == 0); |
| 6258 | if (wc->refs[level] == 1) { |
| 6259 | btrfs_tree_unlock(eb); |
| 6260 | path->locks[level] = 0; |
| 6261 | return 1; |
| 6262 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6263 | } |
| 6264 | } |
| 6265 | |
| 6266 | /* wc->stage == DROP_REFERENCE */ |
| 6267 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6268 | |
| 6269 | if (wc->refs[level] == 1) { |
| 6270 | if (level == 0) { |
| 6271 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6272 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 6273 | else |
| 6274 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6275 | BUG_ON(ret); |
| 6276 | } |
| 6277 | /* make block locked assertion in clean_tree_block happy */ |
| 6278 | if (!path->locks[level] && |
| 6279 | btrfs_header_generation(eb) == trans->transid) { |
| 6280 | btrfs_tree_lock(eb); |
| 6281 | btrfs_set_lock_blocking(eb); |
| 6282 | path->locks[level] = 1; |
| 6283 | } |
| 6284 | clean_tree_block(trans, root, eb); |
| 6285 | } |
| 6286 | |
| 6287 | if (eb == root->node) { |
| 6288 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6289 | parent = eb->start; |
| 6290 | else |
| 6291 | BUG_ON(root->root_key.objectid != |
| 6292 | btrfs_header_owner(eb)); |
| 6293 | } else { |
| 6294 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6295 | parent = path->nodes[level + 1]->start; |
| 6296 | else |
| 6297 | BUG_ON(root->root_key.objectid != |
| 6298 | btrfs_header_owner(path->nodes[level + 1])); |
| 6299 | } |
| 6300 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6301 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6302 | out: |
| 6303 | wc->refs[level] = 0; |
| 6304 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6305 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6306 | } |
| 6307 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6308 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6309 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6310 | struct btrfs_path *path, |
| 6311 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6312 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6313 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6314 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6315 | int ret; |
| 6316 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6317 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6318 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6319 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6320 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6321 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6322 | if (level == 0) |
| 6323 | break; |
| 6324 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6325 | if (path->slots[level] >= |
| 6326 | btrfs_header_nritems(path->nodes[level])) |
| 6327 | break; |
| 6328 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6329 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6330 | if (ret > 0) { |
| 6331 | path->slots[level]++; |
| 6332 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6333 | } else if (ret < 0) |
| 6334 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6335 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6336 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6337 | return 0; |
| 6338 | } |
| 6339 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6340 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6341 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6342 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6343 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6344 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6345 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6346 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6347 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6348 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6349 | while (level < max_level && path->nodes[level]) { |
| 6350 | wc->level = level; |
| 6351 | if (path->slots[level] + 1 < |
| 6352 | btrfs_header_nritems(path->nodes[level])) { |
| 6353 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6354 | return 0; |
| 6355 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6356 | ret = walk_up_proc(trans, root, path, wc); |
| 6357 | if (ret > 0) |
| 6358 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6359 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6360 | if (path->locks[level]) { |
| 6361 | btrfs_tree_unlock(path->nodes[level]); |
| 6362 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6363 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6364 | free_extent_buffer(path->nodes[level]); |
| 6365 | path->nodes[level] = NULL; |
| 6366 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6367 | } |
| 6368 | } |
| 6369 | return 1; |
| 6370 | } |
| 6371 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6372 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6373 | * drop a subvolume tree. |
| 6374 | * |
| 6375 | * this function traverses the tree freeing any blocks that only |
| 6376 | * referenced by the tree. |
| 6377 | * |
| 6378 | * when a shared tree block is found. this function decreases its |
| 6379 | * reference count by one. if update_ref is true, this function |
| 6380 | * also make sure backrefs for the shared block and all lower level |
| 6381 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6382 | */ |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6383 | int btrfs_drop_snapshot(struct btrfs_root *root, |
| 6384 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6385 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6386 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6387 | struct btrfs_trans_handle *trans; |
| 6388 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6389 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6390 | struct walk_control *wc; |
| 6391 | struct btrfs_key key; |
| 6392 | int err = 0; |
| 6393 | int ret; |
| 6394 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6395 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6396 | path = btrfs_alloc_path(); |
| 6397 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6398 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6399 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 6400 | BUG_ON(!wc); |
| 6401 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6402 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6403 | BUG_ON(IS_ERR(trans)); |
| 6404 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6405 | if (block_rsv) |
| 6406 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6407 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6408 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6409 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6410 | path->nodes[level] = btrfs_lock_root_node(root); |
| 6411 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6412 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6413 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6414 | memset(&wc->update_progress, 0, |
| 6415 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6416 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6417 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6418 | memcpy(&wc->update_progress, &key, |
| 6419 | sizeof(wc->update_progress)); |
| 6420 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6421 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6422 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6423 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6424 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6425 | path->lowest_level = 0; |
| 6426 | if (ret < 0) { |
| 6427 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6428 | goto out; |
| 6429 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6430 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6431 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6432 | /* |
| 6433 | * unlock our path, this is safe because only this |
| 6434 | * function is allowed to delete this snapshot |
| 6435 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6436 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6437 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6438 | level = btrfs_header_level(root->node); |
| 6439 | while (1) { |
| 6440 | btrfs_tree_lock(path->nodes[level]); |
| 6441 | btrfs_set_lock_blocking(path->nodes[level]); |
| 6442 | |
| 6443 | ret = btrfs_lookup_extent_info(trans, root, |
| 6444 | path->nodes[level]->start, |
| 6445 | path->nodes[level]->len, |
| 6446 | &wc->refs[level], |
| 6447 | &wc->flags[level]); |
| 6448 | BUG_ON(ret); |
| 6449 | BUG_ON(wc->refs[level] == 0); |
| 6450 | |
| 6451 | if (level == root_item->drop_level) |
| 6452 | break; |
| 6453 | |
| 6454 | btrfs_tree_unlock(path->nodes[level]); |
| 6455 | WARN_ON(wc->refs[level] != 1); |
| 6456 | level--; |
| 6457 | } |
| 6458 | } |
| 6459 | |
| 6460 | wc->level = level; |
| 6461 | wc->shared_level = -1; |
| 6462 | wc->stage = DROP_REFERENCE; |
| 6463 | wc->update_ref = update_ref; |
| 6464 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6465 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6466 | |
| 6467 | while (1) { |
| 6468 | ret = walk_down_tree(trans, root, path, wc); |
| 6469 | if (ret < 0) { |
| 6470 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6471 | break; |
| 6472 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6473 | |
| 6474 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6475 | if (ret < 0) { |
| 6476 | err = ret; |
| 6477 | break; |
| 6478 | } |
| 6479 | |
| 6480 | if (ret > 0) { |
| 6481 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6482 | break; |
| 6483 | } |
| 6484 | |
| 6485 | if (wc->stage == DROP_REFERENCE) { |
| 6486 | level = wc->level; |
| 6487 | btrfs_node_key(path->nodes[level], |
| 6488 | &root_item->drop_progress, |
| 6489 | path->slots[level]); |
| 6490 | root_item->drop_level = level; |
| 6491 | } |
| 6492 | |
| 6493 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6494 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6495 | ret = btrfs_update_root(trans, tree_root, |
| 6496 | &root->root_key, |
| 6497 | root_item); |
| 6498 | BUG_ON(ret); |
| 6499 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6500 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6501 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6502 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6503 | if (block_rsv) |
| 6504 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6505 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6506 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6507 | btrfs_release_path(root, path); |
| 6508 | BUG_ON(err); |
| 6509 | |
| 6510 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6511 | BUG_ON(ret); |
| 6512 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6513 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6514 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6515 | NULL, NULL); |
| 6516 | BUG_ON(ret < 0); |
| 6517 | if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 6518 | /* if we fail to delete the orphan item this time |
| 6519 | * around, it'll get picked up the next time. |
| 6520 | * |
| 6521 | * The most common failure here is just -ENOENT. |
| 6522 | */ |
| 6523 | btrfs_del_orphan_item(trans, tree_root, |
| 6524 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6525 | } |
| 6526 | } |
| 6527 | |
| 6528 | if (root->in_radix) { |
| 6529 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6530 | } else { |
| 6531 | free_extent_buffer(root->node); |
| 6532 | free_extent_buffer(root->commit_root); |
| 6533 | kfree(root); |
| 6534 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6535 | out: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6536 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6537 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6538 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6539 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6540 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6541 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6542 | /* |
| 6543 | * drop subtree rooted at tree block 'node'. |
| 6544 | * |
| 6545 | * NOTE: this function will unlock and release tree block 'node' |
| 6546 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6547 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6548 | struct btrfs_root *root, |
| 6549 | struct extent_buffer *node, |
| 6550 | struct extent_buffer *parent) |
| 6551 | { |
| 6552 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6553 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6554 | int level; |
| 6555 | int parent_level; |
| 6556 | int ret = 0; |
| 6557 | int wret; |
| 6558 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6559 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6560 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6561 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6562 | if (!path) |
| 6563 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6564 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6565 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6566 | if (!wc) { |
| 6567 | btrfs_free_path(path); |
| 6568 | return -ENOMEM; |
| 6569 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6570 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6571 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6572 | parent_level = btrfs_header_level(parent); |
| 6573 | extent_buffer_get(parent); |
| 6574 | path->nodes[parent_level] = parent; |
| 6575 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6576 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6577 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6578 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6579 | path->nodes[level] = node; |
| 6580 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6581 | path->locks[level] = 1; |
| 6582 | |
| 6583 | wc->refs[parent_level] = 1; |
| 6584 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6585 | wc->level = level; |
| 6586 | wc->shared_level = -1; |
| 6587 | wc->stage = DROP_REFERENCE; |
| 6588 | wc->update_ref = 0; |
| 6589 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6590 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6591 | |
| 6592 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6593 | wret = walk_down_tree(trans, root, path, wc); |
| 6594 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6595 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6596 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6597 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6598 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6599 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6600 | if (wret < 0) |
| 6601 | ret = wret; |
| 6602 | if (wret != 0) |
| 6603 | break; |
| 6604 | } |
| 6605 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6606 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6607 | btrfs_free_path(path); |
| 6608 | return ret; |
| 6609 | } |
| 6610 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6611 | #if 0 |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6612 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 6613 | unsigned long nr) |
| 6614 | { |
| 6615 | return min(last, start + nr - 1); |
| 6616 | } |
| 6617 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6618 | static noinline int relocate_inode_pages(struct inode *inode, u64 start, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6619 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6620 | { |
| 6621 | u64 page_start; |
| 6622 | u64 page_end; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6623 | unsigned long first_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6624 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6625 | unsigned long i; |
| 6626 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6627 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6628 | struct file_ra_state *ra; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6629 | struct btrfs_ordered_extent *ordered; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6630 | unsigned int total_read = 0; |
| 6631 | unsigned int total_dirty = 0; |
| 6632 | int ret = 0; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6633 | |
| 6634 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Tsutomu Itoh | 5df6708 | 2011-02-01 09:17:35 +0000 | [diff] [blame] | 6635 | if (!ra) |
| 6636 | return -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6637 | |
| 6638 | mutex_lock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6639 | first_index = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6640 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 6641 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6642 | /* make sure the dirty trick played by the caller work */ |
| 6643 | ret = invalidate_inode_pages2_range(inode->i_mapping, |
| 6644 | first_index, last_index); |
| 6645 | if (ret) |
| 6646 | goto out_unlock; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6647 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6648 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6649 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6650 | for (i = first_index ; i <= last_index; i++) { |
| 6651 | if (total_read % ra->ra_pages == 0) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6652 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6653 | calc_ra(i, last_index, ra->ra_pages)); |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame] | 6654 | } |
| 6655 | total_read++; |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6656 | again: |
| 6657 | if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode)) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6658 | BUG_ON(1); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6659 | page = grab_cache_page(inode->i_mapping, i); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6660 | if (!page) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6661 | ret = -ENOMEM; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6662 | goto out_unlock; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6663 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6664 | if (!PageUptodate(page)) { |
| 6665 | btrfs_readpage(NULL, page); |
| 6666 | lock_page(page); |
| 6667 | if (!PageUptodate(page)) { |
| 6668 | unlock_page(page); |
| 6669 | page_cache_release(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6670 | ret = -EIO; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6671 | goto out_unlock; |
| 6672 | } |
| 6673 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6674 | wait_on_page_writeback(page); |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6675 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6676 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 6677 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6678 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6679 | |
Chris Mason | 3eaa288 | 2008-07-24 11:57:52 -0400 | [diff] [blame] | 6680 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
| 6681 | if (ordered) { |
| 6682 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
| 6683 | unlock_page(page); |
| 6684 | page_cache_release(page); |
| 6685 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 6686 | btrfs_put_ordered_extent(ordered); |
| 6687 | goto again; |
| 6688 | } |
| 6689 | set_page_extent_mapped(page); |
| 6690 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6691 | if (i == first_index) |
| 6692 | set_extent_bits(io_tree, page_start, page_end, |
| 6693 | EXTENT_BOUNDARY, GFP_NOFS); |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 6694 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6695 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6696 | set_page_dirty(page); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6697 | total_dirty++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6698 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 6699 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6700 | unlock_page(page); |
| 6701 | page_cache_release(page); |
| 6702 | } |
| 6703 | |
| 6704 | out_unlock: |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6705 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6706 | mutex_unlock(&inode->i_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6707 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty); |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6708 | return ret; |
| 6709 | } |
| 6710 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6711 | static noinline int relocate_data_extent(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6712 | struct btrfs_key *extent_key, |
| 6713 | u64 offset) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6714 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6715 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 6716 | struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree; |
| 6717 | struct extent_map *em; |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6718 | u64 start = extent_key->objectid - offset; |
| 6719 | u64 end = start + extent_key->offset - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6720 | |
| 6721 | em = alloc_extent_map(GFP_NOFS); |
Tsutomu Itoh | c26a920 | 2011-02-14 00:45:29 +0000 | [diff] [blame] | 6722 | BUG_ON(!em); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6723 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6724 | em->start = start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6725 | em->len = extent_key->offset; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6726 | em->block_len = extent_key->offset; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6727 | em->block_start = extent_key->objectid; |
| 6728 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
| 6729 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
| 6730 | |
| 6731 | /* setup extent map to cheat btrfs_readpage */ |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6732 | lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6733 | while (1) { |
| 6734 | int ret; |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6735 | write_lock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6736 | ret = add_extent_mapping(em_tree, em); |
Chris Mason | 890871b | 2009-09-02 16:24:52 -0400 | [diff] [blame] | 6737 | write_unlock(&em_tree->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6738 | if (ret != -EEXIST) { |
| 6739 | free_extent_map(em); |
| 6740 | break; |
| 6741 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6742 | btrfs_drop_extent_cache(reloc_inode, start, end, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6743 | } |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6744 | unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6745 | |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 6746 | return relocate_inode_pages(reloc_inode, start, extent_key->offset); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | struct btrfs_ref_path { |
| 6750 | u64 extent_start; |
| 6751 | u64 nodes[BTRFS_MAX_LEVEL]; |
| 6752 | u64 root_objectid; |
| 6753 | u64 root_generation; |
| 6754 | u64 owner_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6755 | u32 num_refs; |
| 6756 | int lowest_level; |
| 6757 | int current_level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6758 | int shared_level; |
| 6759 | |
| 6760 | struct btrfs_key node_keys[BTRFS_MAX_LEVEL]; |
| 6761 | u64 new_nodes[BTRFS_MAX_LEVEL]; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6762 | }; |
| 6763 | |
| 6764 | struct disk_extent { |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6765 | u64 ram_bytes; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6766 | u64 disk_bytenr; |
| 6767 | u64 disk_num_bytes; |
| 6768 | u64 offset; |
| 6769 | u64 num_bytes; |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 6770 | u8 compression; |
| 6771 | u8 encryption; |
| 6772 | u16 other_encoding; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6773 | }; |
| 6774 | |
| 6775 | static int is_cowonly_root(u64 root_objectid) |
| 6776 | { |
| 6777 | if (root_objectid == BTRFS_ROOT_TREE_OBJECTID || |
| 6778 | root_objectid == BTRFS_EXTENT_TREE_OBJECTID || |
| 6779 | root_objectid == BTRFS_CHUNK_TREE_OBJECTID || |
| 6780 | root_objectid == BTRFS_DEV_TREE_OBJECTID || |
Yan Zheng | 0403e47 | 2008-12-10 20:32:51 -0500 | [diff] [blame] | 6781 | root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 6782 | root_objectid == BTRFS_CSUM_TREE_OBJECTID) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6783 | return 1; |
| 6784 | return 0; |
| 6785 | } |
| 6786 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6787 | static noinline int __next_ref_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6788 | struct btrfs_root *extent_root, |
| 6789 | struct btrfs_ref_path *ref_path, |
| 6790 | int first_time) |
| 6791 | { |
| 6792 | struct extent_buffer *leaf; |
| 6793 | struct btrfs_path *path; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6794 | struct btrfs_extent_ref *ref; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6795 | struct btrfs_key key; |
| 6796 | struct btrfs_key found_key; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6797 | u64 bytenr; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6798 | u32 nritems; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6799 | int level; |
| 6800 | int ret = 1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6801 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6802 | path = btrfs_alloc_path(); |
| 6803 | if (!path) |
| 6804 | return -ENOMEM; |
| 6805 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6806 | if (first_time) { |
| 6807 | ref_path->lowest_level = -1; |
| 6808 | ref_path->current_level = -1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6809 | ref_path->shared_level = -1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6810 | goto walk_up; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6811 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6812 | walk_down: |
| 6813 | level = ref_path->current_level - 1; |
| 6814 | while (level >= -1) { |
| 6815 | u64 parent; |
| 6816 | if (level < ref_path->lowest_level) |
| 6817 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6818 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6819 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6820 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6821 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6822 | bytenr = ref_path->extent_start; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6823 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6824 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6825 | parent = ref_path->nodes[level + 1]; |
| 6826 | ref_path->nodes[level + 1] = 0; |
| 6827 | ref_path->current_level = level; |
| 6828 | BUG_ON(parent == 0); |
| 6829 | |
| 6830 | key.objectid = bytenr; |
| 6831 | key.offset = parent + 1; |
| 6832 | key.type = BTRFS_EXTENT_REF_KEY; |
| 6833 | |
| 6834 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6835 | if (ret < 0) |
| 6836 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6837 | BUG_ON(ret == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6838 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6839 | leaf = path->nodes[0]; |
| 6840 | nritems = btrfs_header_nritems(leaf); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6841 | if (path->slots[0] >= nritems) { |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6842 | ret = btrfs_next_leaf(extent_root, path); |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6843 | if (ret < 0) |
| 6844 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6845 | if (ret > 0) |
| 6846 | goto next; |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 6847 | leaf = path->nodes[0]; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6848 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6849 | |
| 6850 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6851 | if (found_key.objectid == bytenr && |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6852 | found_key.type == BTRFS_EXTENT_REF_KEY) { |
| 6853 | if (level < ref_path->shared_level) |
| 6854 | ref_path->shared_level = level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6855 | goto found; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6856 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6857 | next: |
| 6858 | level--; |
| 6859 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6860 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6861 | } |
| 6862 | /* reached lowest level */ |
| 6863 | ret = 1; |
| 6864 | goto out; |
| 6865 | walk_up: |
| 6866 | level = ref_path->current_level; |
| 6867 | while (level < BTRFS_MAX_LEVEL - 1) { |
| 6868 | u64 ref_objectid; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6869 | |
| 6870 | if (level >= 0) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6871 | bytenr = ref_path->nodes[level]; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6872 | else |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6873 | bytenr = ref_path->extent_start; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6874 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6875 | BUG_ON(bytenr == 0); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6876 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6877 | key.objectid = bytenr; |
| 6878 | key.offset = 0; |
| 6879 | key.type = BTRFS_EXTENT_REF_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6880 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6881 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0); |
| 6882 | if (ret < 0) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6883 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6884 | |
| 6885 | leaf = path->nodes[0]; |
| 6886 | nritems = btrfs_header_nritems(leaf); |
| 6887 | if (path->slots[0] >= nritems) { |
| 6888 | ret = btrfs_next_leaf(extent_root, path); |
| 6889 | if (ret < 0) |
| 6890 | goto out; |
| 6891 | if (ret > 0) { |
| 6892 | /* the extent was freed by someone */ |
| 6893 | if (ref_path->lowest_level == level) |
| 6894 | goto out; |
| 6895 | btrfs_release_path(extent_root, path); |
| 6896 | goto walk_down; |
| 6897 | } |
| 6898 | leaf = path->nodes[0]; |
| 6899 | } |
| 6900 | |
| 6901 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 6902 | if (found_key.objectid != bytenr || |
| 6903 | found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 6904 | /* the extent was freed by someone */ |
| 6905 | if (ref_path->lowest_level == level) { |
| 6906 | ret = 1; |
| 6907 | goto out; |
| 6908 | } |
| 6909 | btrfs_release_path(extent_root, path); |
| 6910 | goto walk_down; |
| 6911 | } |
| 6912 | found: |
| 6913 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 6914 | struct btrfs_extent_ref); |
| 6915 | ref_objectid = btrfs_ref_objectid(leaf, ref); |
| 6916 | if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 6917 | if (first_time) { |
| 6918 | level = (int)ref_objectid; |
| 6919 | BUG_ON(level >= BTRFS_MAX_LEVEL); |
| 6920 | ref_path->lowest_level = level; |
| 6921 | ref_path->current_level = level; |
| 6922 | ref_path->nodes[level] = bytenr; |
| 6923 | } else { |
| 6924 | WARN_ON(ref_objectid != level); |
| 6925 | } |
| 6926 | } else { |
| 6927 | WARN_ON(level != -1); |
| 6928 | } |
| 6929 | first_time = 0; |
| 6930 | |
| 6931 | if (ref_path->lowest_level == level) { |
| 6932 | ref_path->owner_objectid = ref_objectid; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6933 | ref_path->num_refs = btrfs_ref_num_refs(leaf, ref); |
| 6934 | } |
| 6935 | |
| 6936 | /* |
| 6937 | * the block is tree root or the block isn't in reference |
| 6938 | * counted tree. |
| 6939 | */ |
| 6940 | if (found_key.objectid == found_key.offset || |
| 6941 | is_cowonly_root(btrfs_ref_root(leaf, ref))) { |
| 6942 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6943 | ref_path->root_generation = |
| 6944 | btrfs_ref_generation(leaf, ref); |
| 6945 | if (level < 0) { |
| 6946 | /* special reference from the tree log */ |
| 6947 | ref_path->nodes[0] = found_key.offset; |
| 6948 | ref_path->current_level = 0; |
| 6949 | } |
| 6950 | ret = 0; |
| 6951 | goto out; |
| 6952 | } |
| 6953 | |
| 6954 | level++; |
| 6955 | BUG_ON(ref_path->nodes[level] != 0); |
| 6956 | ref_path->nodes[level] = found_key.offset; |
| 6957 | ref_path->current_level = level; |
| 6958 | |
| 6959 | /* |
| 6960 | * the reference was created in the running transaction, |
| 6961 | * no need to continue walking up. |
| 6962 | */ |
| 6963 | if (btrfs_ref_generation(leaf, ref) == trans->transid) { |
| 6964 | ref_path->root_objectid = btrfs_ref_root(leaf, ref); |
| 6965 | ref_path->root_generation = |
| 6966 | btrfs_ref_generation(leaf, ref); |
| 6967 | ret = 0; |
| 6968 | goto out; |
| 6969 | } |
| 6970 | |
| 6971 | btrfs_release_path(extent_root, path); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6972 | cond_resched(); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6973 | } |
| 6974 | /* reached max tree level, but no tree root found. */ |
| 6975 | BUG(); |
| 6976 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6977 | btrfs_free_path(path); |
| 6978 | return ret; |
| 6979 | } |
| 6980 | |
| 6981 | static int btrfs_first_ref_path(struct btrfs_trans_handle *trans, |
| 6982 | struct btrfs_root *extent_root, |
| 6983 | struct btrfs_ref_path *ref_path, |
| 6984 | u64 extent_start) |
| 6985 | { |
| 6986 | memset(ref_path, 0, sizeof(*ref_path)); |
| 6987 | ref_path->extent_start = extent_start; |
| 6988 | |
| 6989 | return __next_ref_path(trans, extent_root, ref_path, 1); |
| 6990 | } |
| 6991 | |
| 6992 | static int btrfs_next_ref_path(struct btrfs_trans_handle *trans, |
| 6993 | struct btrfs_root *extent_root, |
| 6994 | struct btrfs_ref_path *ref_path) |
| 6995 | { |
| 6996 | return __next_ref_path(trans, extent_root, ref_path, 0); |
| 6997 | } |
| 6998 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6999 | static noinline int get_new_locations(struct inode *reloc_inode, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7000 | struct btrfs_key *extent_key, |
| 7001 | u64 offset, int no_fragment, |
| 7002 | struct disk_extent **extents, |
| 7003 | int *nr_extents) |
| 7004 | { |
| 7005 | struct btrfs_root *root = BTRFS_I(reloc_inode)->root; |
| 7006 | struct btrfs_path *path; |
| 7007 | struct btrfs_file_extent_item *fi; |
| 7008 | struct extent_buffer *leaf; |
| 7009 | struct disk_extent *exts = *extents; |
| 7010 | struct btrfs_key found_key; |
| 7011 | u64 cur_pos; |
| 7012 | u64 last_byte; |
| 7013 | u32 nritems; |
| 7014 | int nr = 0; |
| 7015 | int max = *nr_extents; |
| 7016 | int ret; |
| 7017 | |
| 7018 | WARN_ON(!no_fragment && *extents); |
| 7019 | if (!exts) { |
| 7020 | max = 1; |
| 7021 | exts = kmalloc(sizeof(*exts) * max, GFP_NOFS); |
| 7022 | if (!exts) |
| 7023 | return -ENOMEM; |
| 7024 | } |
| 7025 | |
| 7026 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7027 | if (!path) { |
| 7028 | if (exts != *extents) |
| 7029 | kfree(exts); |
| 7030 | return -ENOMEM; |
| 7031 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7032 | |
| 7033 | cur_pos = extent_key->objectid - offset; |
| 7034 | last_byte = extent_key->objectid + extent_key->offset; |
| 7035 | ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino, |
| 7036 | cur_pos, 0); |
| 7037 | if (ret < 0) |
| 7038 | goto out; |
| 7039 | if (ret > 0) { |
| 7040 | ret = -ENOENT; |
| 7041 | goto out; |
| 7042 | } |
| 7043 | |
| 7044 | while (1) { |
| 7045 | leaf = path->nodes[0]; |
| 7046 | nritems = btrfs_header_nritems(leaf); |
| 7047 | if (path->slots[0] >= nritems) { |
| 7048 | ret = btrfs_next_leaf(root, path); |
| 7049 | if (ret < 0) |
| 7050 | goto out; |
| 7051 | if (ret > 0) |
| 7052 | break; |
| 7053 | leaf = path->nodes[0]; |
| 7054 | } |
| 7055 | |
| 7056 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 7057 | if (found_key.offset != cur_pos || |
| 7058 | found_key.type != BTRFS_EXTENT_DATA_KEY || |
| 7059 | found_key.objectid != reloc_inode->i_ino) |
| 7060 | break; |
| 7061 | |
| 7062 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 7063 | struct btrfs_file_extent_item); |
| 7064 | if (btrfs_file_extent_type(leaf, fi) != |
| 7065 | BTRFS_FILE_EXTENT_REG || |
| 7066 | btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 7067 | break; |
| 7068 | |
| 7069 | if (nr == max) { |
| 7070 | struct disk_extent *old = exts; |
| 7071 | max *= 2; |
| 7072 | exts = kzalloc(sizeof(*exts) * max, GFP_NOFS); |
Yoshinori Sano | dac97e5 | 2011-02-15 12:01:42 +0000 | [diff] [blame] | 7073 | if (!exts) { |
| 7074 | ret = -ENOMEM; |
| 7075 | goto out; |
| 7076 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7077 | memcpy(exts, old, sizeof(*exts) * nr); |
| 7078 | if (old != *extents) |
| 7079 | kfree(old); |
| 7080 | } |
| 7081 | |
| 7082 | exts[nr].disk_bytenr = |
| 7083 | btrfs_file_extent_disk_bytenr(leaf, fi); |
| 7084 | exts[nr].disk_num_bytes = |
| 7085 | btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7086 | exts[nr].offset = btrfs_file_extent_offset(leaf, fi); |
| 7087 | exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7088 | exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); |
| 7089 | exts[nr].compression = btrfs_file_extent_compression(leaf, fi); |
| 7090 | exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi); |
| 7091 | exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf, |
| 7092 | fi); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7093 | BUG_ON(exts[nr].offset > 0); |
| 7094 | BUG_ON(exts[nr].compression || exts[nr].encryption); |
| 7095 | BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7096 | |
| 7097 | cur_pos += exts[nr].num_bytes; |
| 7098 | nr++; |
| 7099 | |
| 7100 | if (cur_pos + offset >= last_byte) |
| 7101 | break; |
| 7102 | |
| 7103 | if (no_fragment) { |
| 7104 | ret = 1; |
| 7105 | goto out; |
| 7106 | } |
| 7107 | path->slots[0]++; |
| 7108 | } |
| 7109 | |
Yan Zheng | 1f80e4d | 2008-12-19 10:59:04 -0500 | [diff] [blame] | 7110 | BUG_ON(cur_pos + offset > last_byte); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7111 | if (cur_pos + offset < last_byte) { |
| 7112 | ret = -ENOENT; |
| 7113 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7114 | } |
| 7115 | ret = 0; |
| 7116 | out: |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7117 | btrfs_free_path(path); |
| 7118 | if (ret) { |
| 7119 | if (exts != *extents) |
| 7120 | kfree(exts); |
| 7121 | } else { |
| 7122 | *extents = exts; |
| 7123 | *nr_extents = nr; |
| 7124 | } |
| 7125 | return ret; |
| 7126 | } |
| 7127 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7128 | static noinline int replace_one_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7129 | struct btrfs_root *root, |
| 7130 | struct btrfs_path *path, |
| 7131 | struct btrfs_key *extent_key, |
| 7132 | struct btrfs_key *leaf_key, |
| 7133 | struct btrfs_ref_path *ref_path, |
| 7134 | struct disk_extent *new_extents, |
| 7135 | int nr_extents) |
| 7136 | { |
| 7137 | struct extent_buffer *leaf; |
| 7138 | struct btrfs_file_extent_item *fi; |
| 7139 | struct inode *inode = NULL; |
| 7140 | struct btrfs_key key; |
| 7141 | u64 lock_start = 0; |
| 7142 | u64 lock_end = 0; |
| 7143 | u64 num_bytes; |
| 7144 | u64 ext_offset; |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7145 | u64 search_end = (u64)-1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7146 | u32 nritems; |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7147 | int nr_scaned = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7148 | int extent_locked = 0; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7149 | int extent_type; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7150 | int ret; |
| 7151 | |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7152 | memcpy(&key, leaf_key, sizeof(key)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7153 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7154 | if (key.objectid < ref_path->owner_objectid || |
| 7155 | (key.objectid == ref_path->owner_objectid && |
| 7156 | key.type < BTRFS_EXTENT_DATA_KEY)) { |
| 7157 | key.objectid = ref_path->owner_objectid; |
| 7158 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 7159 | key.offset = 0; |
| 7160 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7161 | } |
| 7162 | |
| 7163 | while (1) { |
| 7164 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 7165 | if (ret < 0) |
| 7166 | goto out; |
| 7167 | |
| 7168 | leaf = path->nodes[0]; |
| 7169 | nritems = btrfs_header_nritems(leaf); |
| 7170 | next: |
| 7171 | if (extent_locked && ret > 0) { |
| 7172 | /* |
| 7173 | * the file extent item was modified by someone |
| 7174 | * before the extent got locked. |
| 7175 | */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7176 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7177 | lock_end, GFP_NOFS); |
| 7178 | extent_locked = 0; |
| 7179 | } |
| 7180 | |
| 7181 | if (path->slots[0] >= nritems) { |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7182 | if (++nr_scaned > 2) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7183 | break; |
| 7184 | |
| 7185 | BUG_ON(extent_locked); |
| 7186 | ret = btrfs_next_leaf(root, path); |
| 7187 | if (ret < 0) |
| 7188 | goto out; |
| 7189 | if (ret > 0) |
| 7190 | break; |
| 7191 | leaf = path->nodes[0]; |
| 7192 | nritems = btrfs_header_nritems(leaf); |
| 7193 | } |
| 7194 | |
| 7195 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 7196 | |
| 7197 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) { |
| 7198 | if ((key.objectid > ref_path->owner_objectid) || |
| 7199 | (key.objectid == ref_path->owner_objectid && |
| 7200 | key.type > BTRFS_EXTENT_DATA_KEY) || |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7201 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7202 | break; |
| 7203 | } |
| 7204 | |
| 7205 | if (inode && key.objectid != inode->i_ino) { |
| 7206 | BUG_ON(extent_locked); |
| 7207 | btrfs_release_path(root, path); |
| 7208 | mutex_unlock(&inode->i_mutex); |
| 7209 | iput(inode); |
| 7210 | inode = NULL; |
| 7211 | continue; |
| 7212 | } |
| 7213 | |
| 7214 | if (key.type != BTRFS_EXTENT_DATA_KEY) { |
| 7215 | path->slots[0]++; |
| 7216 | ret = 1; |
| 7217 | goto next; |
| 7218 | } |
| 7219 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 7220 | struct btrfs_file_extent_item); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7221 | extent_type = btrfs_file_extent_type(leaf, fi); |
| 7222 | if ((extent_type != BTRFS_FILE_EXTENT_REG && |
| 7223 | extent_type != BTRFS_FILE_EXTENT_PREALLOC) || |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7224 | (btrfs_file_extent_disk_bytenr(leaf, fi) != |
| 7225 | extent_key->objectid)) { |
| 7226 | path->slots[0]++; |
| 7227 | ret = 1; |
| 7228 | goto next; |
| 7229 | } |
| 7230 | |
| 7231 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 7232 | ext_offset = btrfs_file_extent_offset(leaf, fi); |
| 7233 | |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7234 | if (search_end == (u64)-1) { |
| 7235 | search_end = key.offset - ext_offset + |
| 7236 | btrfs_file_extent_ram_bytes(leaf, fi); |
| 7237 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7238 | |
| 7239 | if (!extent_locked) { |
| 7240 | lock_start = key.offset; |
| 7241 | lock_end = lock_start + num_bytes - 1; |
| 7242 | } else { |
Yan Zheng | 6643558 | 2008-10-30 14:19:50 -0400 | [diff] [blame] | 7243 | if (lock_start > key.offset || |
| 7244 | lock_end + 1 < key.offset + num_bytes) { |
| 7245 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 7246 | lock_start, lock_end, GFP_NOFS); |
| 7247 | extent_locked = 0; |
| 7248 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7249 | } |
| 7250 | |
| 7251 | if (!inode) { |
| 7252 | btrfs_release_path(root, path); |
| 7253 | |
| 7254 | inode = btrfs_iget_locked(root->fs_info->sb, |
| 7255 | key.objectid, root); |
| 7256 | if (inode->i_state & I_NEW) { |
| 7257 | BTRFS_I(inode)->root = root; |
| 7258 | BTRFS_I(inode)->location.objectid = |
| 7259 | key.objectid; |
| 7260 | BTRFS_I(inode)->location.type = |
| 7261 | BTRFS_INODE_ITEM_KEY; |
| 7262 | BTRFS_I(inode)->location.offset = 0; |
| 7263 | btrfs_read_locked_inode(inode); |
| 7264 | unlock_new_inode(inode); |
| 7265 | } |
| 7266 | /* |
| 7267 | * some code call btrfs_commit_transaction while |
| 7268 | * holding the i_mutex, so we can't use mutex_lock |
| 7269 | * here. |
| 7270 | */ |
| 7271 | if (is_bad_inode(inode) || |
| 7272 | !mutex_trylock(&inode->i_mutex)) { |
| 7273 | iput(inode); |
| 7274 | inode = NULL; |
| 7275 | key.offset = (u64)-1; |
| 7276 | goto skip; |
| 7277 | } |
| 7278 | } |
| 7279 | |
| 7280 | if (!extent_locked) { |
| 7281 | struct btrfs_ordered_extent *ordered; |
| 7282 | |
| 7283 | btrfs_release_path(root, path); |
| 7284 | |
| 7285 | lock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7286 | lock_end, GFP_NOFS); |
| 7287 | ordered = btrfs_lookup_first_ordered_extent(inode, |
| 7288 | lock_end); |
| 7289 | if (ordered && |
| 7290 | ordered->file_offset <= lock_end && |
| 7291 | ordered->file_offset + ordered->len > lock_start) { |
| 7292 | unlock_extent(&BTRFS_I(inode)->io_tree, |
| 7293 | lock_start, lock_end, GFP_NOFS); |
| 7294 | btrfs_start_ordered_extent(inode, ordered, 1); |
| 7295 | btrfs_put_ordered_extent(ordered); |
| 7296 | key.offset += num_bytes; |
| 7297 | goto skip; |
| 7298 | } |
| 7299 | if (ordered) |
| 7300 | btrfs_put_ordered_extent(ordered); |
| 7301 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7302 | extent_locked = 1; |
| 7303 | continue; |
| 7304 | } |
| 7305 | |
| 7306 | if (nr_extents == 1) { |
| 7307 | /* update extent pointer in place */ |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7308 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7309 | new_extents[0].disk_bytenr); |
| 7310 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7311 | new_extents[0].disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7312 | btrfs_mark_buffer_dirty(leaf); |
| 7313 | |
| 7314 | btrfs_drop_extent_cache(inode, key.offset, |
| 7315 | key.offset + num_bytes - 1, 0); |
| 7316 | |
| 7317 | ret = btrfs_inc_extent_ref(trans, root, |
| 7318 | new_extents[0].disk_bytenr, |
| 7319 | new_extents[0].disk_num_bytes, |
| 7320 | leaf->start, |
| 7321 | root->root_key.objectid, |
| 7322 | trans->transid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7323 | key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7324 | BUG_ON(ret); |
| 7325 | |
| 7326 | ret = btrfs_free_extent(trans, root, |
| 7327 | extent_key->objectid, |
| 7328 | extent_key->offset, |
| 7329 | leaf->start, |
| 7330 | btrfs_header_owner(leaf), |
| 7331 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7332 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7333 | BUG_ON(ret); |
| 7334 | |
| 7335 | btrfs_release_path(root, path); |
| 7336 | key.offset += num_bytes; |
| 7337 | } else { |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7338 | BUG_ON(1); |
| 7339 | #if 0 |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7340 | u64 alloc_hint; |
| 7341 | u64 extent_len; |
| 7342 | int i; |
| 7343 | /* |
| 7344 | * drop old extent pointer at first, then insert the |
| 7345 | * new pointers one bye one |
| 7346 | */ |
| 7347 | btrfs_release_path(root, path); |
| 7348 | ret = btrfs_drop_extents(trans, root, inode, key.offset, |
| 7349 | key.offset + num_bytes, |
| 7350 | key.offset, &alloc_hint); |
| 7351 | BUG_ON(ret); |
| 7352 | |
| 7353 | for (i = 0; i < nr_extents; i++) { |
| 7354 | if (ext_offset >= new_extents[i].num_bytes) { |
| 7355 | ext_offset -= new_extents[i].num_bytes; |
| 7356 | continue; |
| 7357 | } |
| 7358 | extent_len = min(new_extents[i].num_bytes - |
| 7359 | ext_offset, num_bytes); |
| 7360 | |
| 7361 | ret = btrfs_insert_empty_item(trans, root, |
| 7362 | path, &key, |
| 7363 | sizeof(*fi)); |
| 7364 | BUG_ON(ret); |
| 7365 | |
| 7366 | leaf = path->nodes[0]; |
| 7367 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 7368 | struct btrfs_file_extent_item); |
| 7369 | btrfs_set_file_extent_generation(leaf, fi, |
| 7370 | trans->transid); |
| 7371 | btrfs_set_file_extent_type(leaf, fi, |
| 7372 | BTRFS_FILE_EXTENT_REG); |
| 7373 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7374 | new_extents[i].disk_bytenr); |
| 7375 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7376 | new_extents[i].disk_num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 7377 | btrfs_set_file_extent_ram_bytes(leaf, fi, |
| 7378 | new_extents[i].ram_bytes); |
| 7379 | |
| 7380 | btrfs_set_file_extent_compression(leaf, fi, |
| 7381 | new_extents[i].compression); |
| 7382 | btrfs_set_file_extent_encryption(leaf, fi, |
| 7383 | new_extents[i].encryption); |
| 7384 | btrfs_set_file_extent_other_encoding(leaf, fi, |
| 7385 | new_extents[i].other_encoding); |
| 7386 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7387 | btrfs_set_file_extent_num_bytes(leaf, fi, |
| 7388 | extent_len); |
| 7389 | ext_offset += new_extents[i].offset; |
| 7390 | btrfs_set_file_extent_offset(leaf, fi, |
| 7391 | ext_offset); |
| 7392 | btrfs_mark_buffer_dirty(leaf); |
| 7393 | |
| 7394 | btrfs_drop_extent_cache(inode, key.offset, |
| 7395 | key.offset + extent_len - 1, 0); |
| 7396 | |
| 7397 | ret = btrfs_inc_extent_ref(trans, root, |
| 7398 | new_extents[i].disk_bytenr, |
| 7399 | new_extents[i].disk_num_bytes, |
| 7400 | leaf->start, |
| 7401 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7402 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7403 | BUG_ON(ret); |
| 7404 | btrfs_release_path(root, path); |
| 7405 | |
Yan Zheng | a76a3cd | 2008-10-09 11:46:29 -0400 | [diff] [blame] | 7406 | inode_add_bytes(inode, extent_len); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7407 | |
| 7408 | ext_offset = 0; |
| 7409 | num_bytes -= extent_len; |
| 7410 | key.offset += extent_len; |
| 7411 | |
| 7412 | if (num_bytes == 0) |
| 7413 | break; |
| 7414 | } |
| 7415 | BUG_ON(i >= nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7416 | #endif |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7417 | } |
| 7418 | |
| 7419 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7420 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7421 | lock_end, GFP_NOFS); |
| 7422 | extent_locked = 0; |
| 7423 | } |
| 7424 | skip: |
| 7425 | if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS && |
Yan Zheng | 86288a1 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7426 | key.offset >= search_end) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7427 | break; |
| 7428 | |
| 7429 | cond_resched(); |
| 7430 | } |
| 7431 | ret = 0; |
| 7432 | out: |
| 7433 | btrfs_release_path(root, path); |
| 7434 | if (inode) { |
| 7435 | mutex_unlock(&inode->i_mutex); |
| 7436 | if (extent_locked) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7437 | unlock_extent(&BTRFS_I(inode)->io_tree, lock_start, |
| 7438 | lock_end, GFP_NOFS); |
| 7439 | } |
| 7440 | iput(inode); |
| 7441 | } |
| 7442 | return ret; |
| 7443 | } |
| 7444 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7445 | int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans, |
| 7446 | struct btrfs_root *root, |
| 7447 | struct extent_buffer *buf, u64 orig_start) |
| 7448 | { |
| 7449 | int level; |
| 7450 | int ret; |
| 7451 | |
| 7452 | BUG_ON(btrfs_header_generation(buf) != trans->transid); |
| 7453 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 7454 | |
| 7455 | level = btrfs_header_level(buf); |
| 7456 | if (level == 0) { |
| 7457 | struct btrfs_leaf_ref *ref; |
| 7458 | struct btrfs_leaf_ref *orig_ref; |
| 7459 | |
| 7460 | orig_ref = btrfs_lookup_leaf_ref(root, orig_start); |
| 7461 | if (!orig_ref) |
| 7462 | return -ENOENT; |
| 7463 | |
| 7464 | ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems); |
| 7465 | if (!ref) { |
| 7466 | btrfs_free_leaf_ref(root, orig_ref); |
| 7467 | return -ENOMEM; |
| 7468 | } |
| 7469 | |
| 7470 | ref->nritems = orig_ref->nritems; |
| 7471 | memcpy(ref->extents, orig_ref->extents, |
| 7472 | sizeof(ref->extents[0]) * ref->nritems); |
| 7473 | |
| 7474 | btrfs_free_leaf_ref(root, orig_ref); |
| 7475 | |
| 7476 | ref->root_gen = trans->transid; |
| 7477 | ref->bytenr = buf->start; |
| 7478 | ref->owner = btrfs_header_owner(buf); |
| 7479 | ref->generation = btrfs_header_generation(buf); |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 7480 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7481 | ret = btrfs_add_leaf_ref(root, ref, 0); |
| 7482 | WARN_ON(ret); |
| 7483 | btrfs_free_leaf_ref(root, ref); |
| 7484 | } |
| 7485 | return 0; |
| 7486 | } |
| 7487 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7488 | static noinline int invalidate_extent_cache(struct btrfs_root *root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7489 | struct extent_buffer *leaf, |
| 7490 | struct btrfs_block_group_cache *group, |
| 7491 | struct btrfs_root *target_root) |
| 7492 | { |
| 7493 | struct btrfs_key key; |
| 7494 | struct inode *inode = NULL; |
| 7495 | struct btrfs_file_extent_item *fi; |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7496 | struct extent_state *cached_state = NULL; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7497 | u64 num_bytes; |
| 7498 | u64 skip_objectid = 0; |
| 7499 | u32 nritems; |
| 7500 | u32 i; |
| 7501 | |
| 7502 | nritems = btrfs_header_nritems(leaf); |
| 7503 | for (i = 0; i < nritems; i++) { |
| 7504 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 7505 | if (key.objectid == skip_objectid || |
| 7506 | key.type != BTRFS_EXTENT_DATA_KEY) |
| 7507 | continue; |
| 7508 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 7509 | if (btrfs_file_extent_type(leaf, fi) == |
| 7510 | BTRFS_FILE_EXTENT_INLINE) |
| 7511 | continue; |
| 7512 | if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0) |
| 7513 | continue; |
| 7514 | if (!inode || inode->i_ino != key.objectid) { |
| 7515 | iput(inode); |
| 7516 | inode = btrfs_ilookup(target_root->fs_info->sb, |
| 7517 | key.objectid, target_root, 1); |
| 7518 | } |
| 7519 | if (!inode) { |
| 7520 | skip_objectid = key.objectid; |
| 7521 | continue; |
| 7522 | } |
| 7523 | num_bytes = btrfs_file_extent_num_bytes(leaf, fi); |
| 7524 | |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7525 | lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset, |
| 7526 | key.offset + num_bytes - 1, 0, &cached_state, |
| 7527 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7528 | btrfs_drop_extent_cache(inode, key.offset, |
| 7529 | key.offset + num_bytes - 1, 1); |
Josef Bacik | 2ac55d4 | 2010-02-03 19:33:23 +0000 | [diff] [blame] | 7530 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset, |
| 7531 | key.offset + num_bytes - 1, &cached_state, |
| 7532 | GFP_NOFS); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7533 | cond_resched(); |
| 7534 | } |
| 7535 | iput(inode); |
| 7536 | return 0; |
| 7537 | } |
| 7538 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7539 | static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7540 | struct btrfs_root *root, |
| 7541 | struct extent_buffer *leaf, |
| 7542 | struct btrfs_block_group_cache *group, |
| 7543 | struct inode *reloc_inode) |
| 7544 | { |
| 7545 | struct btrfs_key key; |
| 7546 | struct btrfs_key extent_key; |
| 7547 | struct btrfs_file_extent_item *fi; |
| 7548 | struct btrfs_leaf_ref *ref; |
| 7549 | struct disk_extent *new_extent; |
| 7550 | u64 bytenr; |
| 7551 | u64 num_bytes; |
| 7552 | u32 nritems; |
| 7553 | u32 i; |
| 7554 | int ext_index; |
| 7555 | int nr_extent; |
| 7556 | int ret; |
| 7557 | |
| 7558 | new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7559 | if (!new_extent) |
| 7560 | return -ENOMEM; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7561 | |
| 7562 | ref = btrfs_lookup_leaf_ref(root, leaf->start); |
| 7563 | BUG_ON(!ref); |
| 7564 | |
| 7565 | ext_index = -1; |
| 7566 | nritems = btrfs_header_nritems(leaf); |
| 7567 | for (i = 0; i < nritems; i++) { |
| 7568 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 7569 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
| 7570 | continue; |
| 7571 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
| 7572 | if (btrfs_file_extent_type(leaf, fi) == |
| 7573 | BTRFS_FILE_EXTENT_INLINE) |
| 7574 | continue; |
| 7575 | bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 7576 | num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi); |
| 7577 | if (bytenr == 0) |
| 7578 | continue; |
| 7579 | |
| 7580 | ext_index++; |
| 7581 | if (bytenr >= group->key.objectid + group->key.offset || |
| 7582 | bytenr + num_bytes <= group->key.objectid) |
| 7583 | continue; |
| 7584 | |
| 7585 | extent_key.objectid = bytenr; |
| 7586 | extent_key.offset = num_bytes; |
| 7587 | extent_key.type = BTRFS_EXTENT_ITEM_KEY; |
| 7588 | nr_extent = 1; |
| 7589 | ret = get_new_locations(reloc_inode, &extent_key, |
| 7590 | group->key.objectid, 1, |
| 7591 | &new_extent, &nr_extent); |
| 7592 | if (ret > 0) |
| 7593 | continue; |
| 7594 | BUG_ON(ret < 0); |
| 7595 | |
| 7596 | BUG_ON(ref->extents[ext_index].bytenr != bytenr); |
| 7597 | BUG_ON(ref->extents[ext_index].num_bytes != num_bytes); |
| 7598 | ref->extents[ext_index].bytenr = new_extent->disk_bytenr; |
| 7599 | ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes; |
| 7600 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7601 | btrfs_set_file_extent_disk_bytenr(leaf, fi, |
| 7602 | new_extent->disk_bytenr); |
| 7603 | btrfs_set_file_extent_disk_num_bytes(leaf, fi, |
| 7604 | new_extent->disk_num_bytes); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7605 | btrfs_mark_buffer_dirty(leaf); |
| 7606 | |
| 7607 | ret = btrfs_inc_extent_ref(trans, root, |
| 7608 | new_extent->disk_bytenr, |
| 7609 | new_extent->disk_num_bytes, |
| 7610 | leaf->start, |
| 7611 | root->root_key.objectid, |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7612 | trans->transid, key.objectid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7613 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7614 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7615 | ret = btrfs_free_extent(trans, root, |
| 7616 | bytenr, num_bytes, leaf->start, |
| 7617 | btrfs_header_owner(leaf), |
| 7618 | btrfs_header_generation(leaf), |
Yan Zheng | 3bb1a1b | 2008-10-09 11:46:24 -0400 | [diff] [blame] | 7619 | key.objectid, 0); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7620 | BUG_ON(ret); |
| 7621 | cond_resched(); |
| 7622 | } |
| 7623 | kfree(new_extent); |
| 7624 | BUG_ON(ext_index + 1 != ref->nritems); |
| 7625 | btrfs_free_leaf_ref(root, ref); |
| 7626 | return 0; |
| 7627 | } |
| 7628 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7629 | int btrfs_free_reloc_root(struct btrfs_trans_handle *trans, |
| 7630 | struct btrfs_root *root) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7631 | { |
| 7632 | struct btrfs_root *reloc_root; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7633 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7634 | |
| 7635 | if (root->reloc_root) { |
| 7636 | reloc_root = root->reloc_root; |
| 7637 | root->reloc_root = NULL; |
| 7638 | list_add(&reloc_root->dead_list, |
| 7639 | &root->fs_info->dead_reloc_roots); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7640 | |
| 7641 | btrfs_set_root_bytenr(&reloc_root->root_item, |
| 7642 | reloc_root->node->start); |
| 7643 | btrfs_set_root_level(&root->root_item, |
| 7644 | btrfs_header_level(reloc_root->node)); |
| 7645 | memset(&reloc_root->root_item.drop_progress, 0, |
| 7646 | sizeof(struct btrfs_disk_key)); |
| 7647 | reloc_root->root_item.drop_level = 0; |
| 7648 | |
| 7649 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
| 7650 | &reloc_root->root_key, |
| 7651 | &reloc_root->root_item); |
| 7652 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7653 | } |
| 7654 | return 0; |
| 7655 | } |
| 7656 | |
| 7657 | int btrfs_drop_dead_reloc_roots(struct btrfs_root *root) |
| 7658 | { |
| 7659 | struct btrfs_trans_handle *trans; |
| 7660 | struct btrfs_root *reloc_root; |
| 7661 | struct btrfs_root *prev_root = NULL; |
| 7662 | struct list_head dead_roots; |
| 7663 | int ret; |
| 7664 | unsigned long nr; |
| 7665 | |
| 7666 | INIT_LIST_HEAD(&dead_roots); |
| 7667 | list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots); |
| 7668 | |
| 7669 | while (!list_empty(&dead_roots)) { |
| 7670 | reloc_root = list_entry(dead_roots.prev, |
| 7671 | struct btrfs_root, dead_list); |
| 7672 | list_del_init(&reloc_root->dead_list); |
| 7673 | |
| 7674 | BUG_ON(reloc_root->commit_root != NULL); |
| 7675 | while (1) { |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 7676 | trans = btrfs_join_transaction(root); |
Tsutomu Itoh | 3612b49 | 2011-01-25 02:51:38 +0000 | [diff] [blame] | 7677 | BUG_ON(IS_ERR(trans)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7678 | |
| 7679 | mutex_lock(&root->fs_info->drop_mutex); |
| 7680 | ret = btrfs_drop_snapshot(trans, reloc_root); |
| 7681 | if (ret != -EAGAIN) |
| 7682 | break; |
| 7683 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7684 | |
| 7685 | nr = trans->blocks_used; |
| 7686 | ret = btrfs_end_transaction(trans, root); |
| 7687 | BUG_ON(ret); |
| 7688 | btrfs_btree_balance_dirty(root, nr); |
| 7689 | } |
| 7690 | |
| 7691 | free_extent_buffer(reloc_root->node); |
| 7692 | |
| 7693 | ret = btrfs_del_root(trans, root->fs_info->tree_root, |
| 7694 | &reloc_root->root_key); |
| 7695 | BUG_ON(ret); |
| 7696 | mutex_unlock(&root->fs_info->drop_mutex); |
| 7697 | |
| 7698 | nr = trans->blocks_used; |
| 7699 | ret = btrfs_end_transaction(trans, root); |
| 7700 | BUG_ON(ret); |
| 7701 | btrfs_btree_balance_dirty(root, nr); |
| 7702 | |
| 7703 | kfree(prev_root); |
| 7704 | prev_root = reloc_root; |
| 7705 | } |
| 7706 | if (prev_root) { |
| 7707 | btrfs_remove_leaf_refs(prev_root, (u64)-1, 0); |
| 7708 | kfree(prev_root); |
| 7709 | } |
| 7710 | return 0; |
| 7711 | } |
| 7712 | |
| 7713 | int btrfs_add_dead_reloc_root(struct btrfs_root *root) |
| 7714 | { |
| 7715 | list_add(&root->dead_list, &root->fs_info->dead_reloc_roots); |
| 7716 | return 0; |
| 7717 | } |
| 7718 | |
| 7719 | int btrfs_cleanup_reloc_trees(struct btrfs_root *root) |
| 7720 | { |
| 7721 | struct btrfs_root *reloc_root; |
| 7722 | struct btrfs_trans_handle *trans; |
| 7723 | struct btrfs_key location; |
| 7724 | int found; |
| 7725 | int ret; |
| 7726 | |
| 7727 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7728 | ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL); |
| 7729 | BUG_ON(ret); |
| 7730 | found = !list_empty(&root->fs_info->dead_reloc_roots); |
| 7731 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
| 7732 | |
| 7733 | if (found) { |
| 7734 | trans = btrfs_start_transaction(root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 7735 | BUG_ON(IS_ERR(trans)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7736 | ret = btrfs_commit_transaction(trans, root); |
| 7737 | BUG_ON(ret); |
| 7738 | } |
| 7739 | |
| 7740 | location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID; |
| 7741 | location.offset = (u64)-1; |
| 7742 | location.type = BTRFS_ROOT_ITEM_KEY; |
| 7743 | |
| 7744 | reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
| 7745 | BUG_ON(!reloc_root); |
Josef Bacik | 66b4ffd | 2011-01-31 16:22:42 -0500 | [diff] [blame] | 7746 | ret = btrfs_orphan_cleanup(reloc_root); |
| 7747 | BUG_ON(ret); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7748 | return 0; |
| 7749 | } |
| 7750 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7751 | static noinline int init_reloc_tree(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7752 | struct btrfs_root *root) |
| 7753 | { |
| 7754 | struct btrfs_root *reloc_root; |
| 7755 | struct extent_buffer *eb; |
| 7756 | struct btrfs_root_item *root_item; |
| 7757 | struct btrfs_key root_key; |
| 7758 | int ret; |
| 7759 | |
| 7760 | BUG_ON(!root->ref_cows); |
| 7761 | if (root->reloc_root) |
| 7762 | return 0; |
| 7763 | |
| 7764 | root_item = kmalloc(sizeof(*root_item), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7765 | if (!root_item) |
| 7766 | return -ENOMEM; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7767 | |
| 7768 | ret = btrfs_copy_root(trans, root, root->commit_root, |
| 7769 | &eb, BTRFS_TREE_RELOC_OBJECTID); |
| 7770 | BUG_ON(ret); |
| 7771 | |
| 7772 | root_key.objectid = BTRFS_TREE_RELOC_OBJECTID; |
| 7773 | root_key.offset = root->root_key.objectid; |
| 7774 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7775 | |
| 7776 | memcpy(root_item, &root->root_item, sizeof(root_item)); |
| 7777 | btrfs_set_root_refs(root_item, 0); |
| 7778 | btrfs_set_root_bytenr(root_item, eb->start); |
| 7779 | btrfs_set_root_level(root_item, btrfs_header_level(eb)); |
Yan Zheng | 84234f3 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7780 | btrfs_set_root_generation(root_item, trans->transid); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7781 | |
| 7782 | btrfs_tree_unlock(eb); |
| 7783 | free_extent_buffer(eb); |
| 7784 | |
| 7785 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, |
| 7786 | &root_key, root_item); |
| 7787 | BUG_ON(ret); |
| 7788 | kfree(root_item); |
| 7789 | |
| 7790 | reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 7791 | &root_key); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 7792 | BUG_ON(IS_ERR(reloc_root)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7793 | reloc_root->last_trans = trans->transid; |
| 7794 | reloc_root->commit_root = NULL; |
| 7795 | reloc_root->ref_tree = &root->fs_info->reloc_ref_tree; |
| 7796 | |
| 7797 | root->reloc_root = reloc_root; |
| 7798 | return 0; |
| 7799 | } |
| 7800 | |
| 7801 | /* |
| 7802 | * Core function of space balance. |
| 7803 | * |
| 7804 | * The idea is using reloc trees to relocate tree blocks in reference |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7805 | * counted roots. There is one reloc tree for each subvol, and all |
| 7806 | * reloc trees share same root key objectid. Reloc trees are snapshots |
| 7807 | * of the latest committed roots of subvols (root->commit_root). |
| 7808 | * |
| 7809 | * To relocate a tree block referenced by a subvol, there are two steps. |
| 7810 | * COW the block through subvol's reloc tree, then update block pointer |
| 7811 | * in the subvol to point to the new block. Since all reloc trees share |
| 7812 | * same root key objectid, doing special handing for tree blocks owned |
| 7813 | * by them is easy. Once a tree block has been COWed in one reloc tree, |
| 7814 | * we can use the resulting new block directly when the same block is |
| 7815 | * required to COW again through other reloc trees. By this way, relocated |
| 7816 | * tree blocks are shared between reloc trees, so they are also shared |
| 7817 | * between subvols. |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7818 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7819 | static noinline int relocate_one_path(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7820 | struct btrfs_root *root, |
| 7821 | struct btrfs_path *path, |
| 7822 | struct btrfs_key *first_key, |
| 7823 | struct btrfs_ref_path *ref_path, |
| 7824 | struct btrfs_block_group_cache *group, |
| 7825 | struct inode *reloc_inode) |
| 7826 | { |
| 7827 | struct btrfs_root *reloc_root; |
| 7828 | struct extent_buffer *eb = NULL; |
| 7829 | struct btrfs_key *keys; |
| 7830 | u64 *nodes; |
| 7831 | int level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7832 | int shared_level; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7833 | int lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7834 | int ret; |
| 7835 | |
| 7836 | if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) |
| 7837 | lowest_level = ref_path->owner_objectid; |
| 7838 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7839 | if (!root->ref_cows) { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7840 | path->lowest_level = lowest_level; |
| 7841 | ret = btrfs_search_slot(trans, root, first_key, path, 0, 1); |
| 7842 | BUG_ON(ret < 0); |
| 7843 | path->lowest_level = 0; |
| 7844 | btrfs_release_path(root, path); |
| 7845 | return 0; |
| 7846 | } |
| 7847 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7848 | mutex_lock(&root->fs_info->tree_reloc_mutex); |
| 7849 | ret = init_reloc_tree(trans, root); |
| 7850 | BUG_ON(ret); |
| 7851 | reloc_root = root->reloc_root; |
| 7852 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7853 | shared_level = ref_path->shared_level; |
| 7854 | ref_path->shared_level = BTRFS_MAX_LEVEL - 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7855 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7856 | keys = ref_path->node_keys; |
| 7857 | nodes = ref_path->new_nodes; |
| 7858 | memset(&keys[shared_level + 1], 0, |
| 7859 | sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
| 7860 | memset(&nodes[shared_level + 1], 0, |
| 7861 | sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7862 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7863 | if (nodes[lowest_level] == 0) { |
| 7864 | path->lowest_level = lowest_level; |
| 7865 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7866 | 0, 1); |
| 7867 | BUG_ON(ret); |
| 7868 | for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) { |
| 7869 | eb = path->nodes[level]; |
| 7870 | if (!eb || eb == reloc_root->node) |
| 7871 | break; |
| 7872 | nodes[level] = eb->start; |
| 7873 | if (level == 0) |
| 7874 | btrfs_item_key_to_cpu(eb, &keys[level], 0); |
| 7875 | else |
| 7876 | btrfs_node_key_to_cpu(eb, &keys[level], 0); |
| 7877 | } |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7878 | if (nodes[0] && |
| 7879 | ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7880 | eb = path->nodes[0]; |
| 7881 | ret = replace_extents_in_leaf(trans, reloc_root, eb, |
| 7882 | group, reloc_inode); |
| 7883 | BUG_ON(ret); |
| 7884 | } |
| 7885 | btrfs_release_path(reloc_root, path); |
| 7886 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7887 | ret = btrfs_merge_path(trans, reloc_root, keys, nodes, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7888 | lowest_level); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7889 | BUG_ON(ret); |
| 7890 | } |
| 7891 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7892 | /* |
| 7893 | * replace tree blocks in the fs tree with tree blocks in |
| 7894 | * the reloc tree. |
| 7895 | */ |
| 7896 | ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level); |
| 7897 | BUG_ON(ret < 0); |
| 7898 | |
| 7899 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7900 | ret = btrfs_search_slot(trans, reloc_root, first_key, path, |
| 7901 | 0, 0); |
| 7902 | BUG_ON(ret); |
| 7903 | extent_buffer_get(path->nodes[0]); |
| 7904 | eb = path->nodes[0]; |
| 7905 | btrfs_release_path(reloc_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7906 | ret = invalidate_extent_cache(reloc_root, eb, group, root); |
| 7907 | BUG_ON(ret); |
| 7908 | free_extent_buffer(eb); |
| 7909 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7910 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7911 | mutex_unlock(&root->fs_info->tree_reloc_mutex); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7912 | path->lowest_level = 0; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7913 | return 0; |
| 7914 | } |
| 7915 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7916 | static noinline int relocate_tree_block(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7917 | struct btrfs_root *root, |
| 7918 | struct btrfs_path *path, |
| 7919 | struct btrfs_key *first_key, |
| 7920 | struct btrfs_ref_path *ref_path) |
| 7921 | { |
| 7922 | int ret; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7923 | |
| 7924 | ret = relocate_one_path(trans, root, path, first_key, |
| 7925 | ref_path, NULL, NULL); |
| 7926 | BUG_ON(ret); |
| 7927 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7928 | return 0; |
| 7929 | } |
| 7930 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7931 | static noinline int del_extent_zero(struct btrfs_trans_handle *trans, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7932 | struct btrfs_root *extent_root, |
| 7933 | struct btrfs_path *path, |
| 7934 | struct btrfs_key *extent_key) |
| 7935 | { |
| 7936 | int ret; |
| 7937 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7938 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); |
| 7939 | if (ret) |
| 7940 | goto out; |
| 7941 | ret = btrfs_del_item(trans, extent_root, path); |
| 7942 | out: |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7943 | btrfs_release_path(extent_root, path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7944 | return ret; |
| 7945 | } |
| 7946 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7947 | 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] | 7948 | struct btrfs_ref_path *ref_path) |
| 7949 | { |
| 7950 | struct btrfs_key root_key; |
| 7951 | |
| 7952 | root_key.objectid = ref_path->root_objectid; |
| 7953 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 7954 | if (is_cowonly_root(ref_path->root_objectid)) |
| 7955 | root_key.offset = 0; |
| 7956 | else |
| 7957 | root_key.offset = (u64)-1; |
| 7958 | |
| 7959 | return btrfs_read_fs_root_no_name(fs_info, &root_key); |
| 7960 | } |
| 7961 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7962 | static noinline int relocate_one_extent(struct btrfs_root *extent_root, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7963 | struct btrfs_path *path, |
| 7964 | struct btrfs_key *extent_key, |
| 7965 | struct btrfs_block_group_cache *group, |
| 7966 | struct inode *reloc_inode, int pass) |
| 7967 | { |
| 7968 | struct btrfs_trans_handle *trans; |
| 7969 | struct btrfs_root *found_root; |
| 7970 | struct btrfs_ref_path *ref_path = NULL; |
| 7971 | struct disk_extent *new_extents = NULL; |
| 7972 | int nr_extents = 0; |
| 7973 | int loops; |
| 7974 | int ret; |
| 7975 | int level; |
| 7976 | struct btrfs_key first_key; |
| 7977 | u64 prev_block = 0; |
| 7978 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7979 | |
| 7980 | trans = btrfs_start_transaction(extent_root, 1); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 7981 | BUG_ON(IS_ERR(trans)); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7982 | |
| 7983 | if (extent_key->objectid == 0) { |
| 7984 | ret = del_extent_zero(trans, extent_root, path, extent_key); |
| 7985 | goto out; |
| 7986 | } |
| 7987 | |
| 7988 | ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS); |
| 7989 | if (!ref_path) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7990 | ret = -ENOMEM; |
| 7991 | goto out; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7992 | } |
| 7993 | |
| 7994 | for (loops = 0; ; loops++) { |
| 7995 | if (loops == 0) { |
| 7996 | ret = btrfs_first_ref_path(trans, extent_root, ref_path, |
| 7997 | extent_key->objectid); |
| 7998 | } else { |
| 7999 | ret = btrfs_next_ref_path(trans, extent_root, ref_path); |
| 8000 | } |
| 8001 | if (ret < 0) |
| 8002 | goto out; |
| 8003 | if (ret > 0) |
| 8004 | break; |
| 8005 | |
| 8006 | if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID || |
| 8007 | ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 8008 | continue; |
| 8009 | |
| 8010 | found_root = read_ref_root(extent_root->fs_info, ref_path); |
| 8011 | BUG_ON(!found_root); |
| 8012 | /* |
| 8013 | * for reference counted tree, only process reference paths |
| 8014 | * rooted at the latest committed root. |
| 8015 | */ |
| 8016 | if (found_root->ref_cows && |
| 8017 | ref_path->root_generation != found_root->root_key.offset) |
| 8018 | continue; |
| 8019 | |
| 8020 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 8021 | if (pass == 0) { |
| 8022 | /* |
| 8023 | * copy data extents to new locations |
| 8024 | */ |
| 8025 | u64 group_start = group->key.objectid; |
| 8026 | ret = relocate_data_extent(reloc_inode, |
| 8027 | extent_key, |
| 8028 | group_start); |
| 8029 | if (ret < 0) |
| 8030 | goto out; |
| 8031 | break; |
| 8032 | } |
| 8033 | level = 0; |
| 8034 | } else { |
| 8035 | level = ref_path->owner_objectid; |
| 8036 | } |
| 8037 | |
| 8038 | if (prev_block != ref_path->nodes[level]) { |
| 8039 | struct extent_buffer *eb; |
| 8040 | u64 block_start = ref_path->nodes[level]; |
| 8041 | u64 block_size = btrfs_level_size(found_root, level); |
| 8042 | |
| 8043 | eb = read_tree_block(found_root, block_start, |
| 8044 | block_size, 0); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 8045 | if (!eb) { |
| 8046 | ret = -EIO; |
| 8047 | goto out; |
| 8048 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8049 | btrfs_tree_lock(eb); |
| 8050 | BUG_ON(level != btrfs_header_level(eb)); |
| 8051 | |
| 8052 | if (level == 0) |
| 8053 | btrfs_item_key_to_cpu(eb, &first_key, 0); |
| 8054 | else |
| 8055 | btrfs_node_key_to_cpu(eb, &first_key, 0); |
| 8056 | |
| 8057 | btrfs_tree_unlock(eb); |
| 8058 | free_extent_buffer(eb); |
| 8059 | prev_block = block_start; |
| 8060 | } |
| 8061 | |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 8062 | mutex_lock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 8063 | btrfs_record_root_in_trans(found_root); |
Yan Zheng | 2456242 | 2009-02-12 14:14:53 -0500 | [diff] [blame] | 8064 | mutex_unlock(&extent_root->fs_info->trans_mutex); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 8065 | if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 8066 | /* |
| 8067 | * try to update data extent references while |
| 8068 | * keeping metadata shared between snapshots. |
| 8069 | */ |
| 8070 | if (pass == 1) { |
| 8071 | ret = relocate_one_path(trans, found_root, |
| 8072 | path, &first_key, ref_path, |
| 8073 | group, reloc_inode); |
| 8074 | if (ret < 0) |
| 8075 | goto out; |
| 8076 | continue; |
| 8077 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8078 | /* |
| 8079 | * use fallback method to process the remaining |
| 8080 | * references. |
| 8081 | */ |
| 8082 | if (!new_extents) { |
| 8083 | u64 group_start = group->key.objectid; |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8084 | new_extents = kmalloc(sizeof(*new_extents), |
| 8085 | GFP_NOFS); |
Tsutomu Itoh | 8d41371 | 2011-04-25 19:43:52 -0400 | [diff] [blame] | 8086 | if (!new_extents) { |
| 8087 | ret = -ENOMEM; |
| 8088 | goto out; |
| 8089 | } |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8090 | nr_extents = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8091 | ret = get_new_locations(reloc_inode, |
| 8092 | extent_key, |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8093 | group_start, 1, |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8094 | &new_extents, |
| 8095 | &nr_extents); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8096 | if (ret) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8097 | goto out; |
| 8098 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8099 | ret = replace_one_extent(trans, found_root, |
| 8100 | path, extent_key, |
| 8101 | &first_key, ref_path, |
| 8102 | new_extents, nr_extents); |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 8103 | } else { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8104 | ret = relocate_tree_block(trans, found_root, path, |
| 8105 | &first_key, ref_path); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8106 | } |
| 8107 | if (ret < 0) |
| 8108 | goto out; |
| 8109 | } |
| 8110 | ret = 0; |
| 8111 | out: |
| 8112 | btrfs_end_transaction(trans, extent_root); |
| 8113 | kfree(new_extents); |
| 8114 | kfree(ref_path); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8115 | return ret; |
| 8116 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8117 | #endif |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8118 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8119 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 8120 | { |
| 8121 | u64 num_devices; |
| 8122 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 8123 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 8124 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 8125 | /* |
| 8126 | * we add in the count of missing devices because we want |
| 8127 | * to make sure that any RAID levels on a degraded FS |
| 8128 | * continue to be honored. |
| 8129 | */ |
| 8130 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 8131 | root->fs_info->fs_devices->missing_devices; |
| 8132 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8133 | if (num_devices == 1) { |
| 8134 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 8135 | stripped = flags & ~stripped; |
| 8136 | |
| 8137 | /* turn raid0 into single device chunks */ |
| 8138 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 8139 | return stripped; |
| 8140 | |
| 8141 | /* turn mirroring into duplication */ |
| 8142 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 8143 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8144 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 8145 | return flags; |
| 8146 | } else { |
| 8147 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8148 | if (flags & stripped) |
| 8149 | return flags; |
| 8150 | |
| 8151 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 8152 | stripped = flags & ~stripped; |
| 8153 | |
| 8154 | /* switch duplicated blocks with raid1 */ |
| 8155 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 8156 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 8157 | |
| 8158 | /* turn single device chunks into raid0 */ |
| 8159 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 8160 | } |
| 8161 | return flags; |
| 8162 | } |
| 8163 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8164 | static int set_block_group_ro(struct btrfs_block_group_cache *cache) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8165 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8166 | struct btrfs_space_info *sinfo = cache->space_info; |
| 8167 | u64 num_bytes; |
| 8168 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8169 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8170 | if (cache->ro) |
| 8171 | return 0; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8172 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8173 | spin_lock(&sinfo->lock); |
| 8174 | spin_lock(&cache->lock); |
| 8175 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 8176 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 8177 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8178 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
| 8179 | sinfo->bytes_may_use + sinfo->bytes_readonly + |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 8180 | cache->reserved_pinned + num_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8181 | sinfo->bytes_readonly += num_bytes; |
| 8182 | sinfo->bytes_reserved += cache->reserved_pinned; |
| 8183 | cache->reserved_pinned = 0; |
| 8184 | cache->ro = 1; |
| 8185 | ret = 0; |
| 8186 | } |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 8187 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8188 | spin_unlock(&cache->lock); |
| 8189 | spin_unlock(&sinfo->lock); |
| 8190 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8191 | } |
| 8192 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8193 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 8194 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8195 | |
| 8196 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8197 | struct btrfs_trans_handle *trans; |
| 8198 | u64 alloc_flags; |
| 8199 | int ret; |
| 8200 | |
| 8201 | BUG_ON(cache->ro); |
| 8202 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 8203 | trans = btrfs_join_transaction(root); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8204 | BUG_ON(IS_ERR(trans)); |
| 8205 | |
| 8206 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 8207 | if (alloc_flags != cache->flags) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 8208 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 8209 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8210 | |
| 8211 | ret = set_block_group_ro(cache); |
| 8212 | if (!ret) |
| 8213 | goto out; |
| 8214 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 8215 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 8216 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8217 | if (ret < 0) |
| 8218 | goto out; |
| 8219 | ret = set_block_group_ro(cache); |
| 8220 | out: |
| 8221 | btrfs_end_transaction(trans, root); |
| 8222 | return ret; |
| 8223 | } |
| 8224 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 8225 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 8226 | struct btrfs_root *root, u64 type) |
| 8227 | { |
| 8228 | u64 alloc_flags = get_alloc_profile(root, type); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 8229 | return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 8230 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 8231 | } |
| 8232 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 8233 | /* |
| 8234 | * helper to account the unused space of all the readonly block group in the |
| 8235 | * list. takes mirrors into account. |
| 8236 | */ |
| 8237 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 8238 | { |
| 8239 | struct btrfs_block_group_cache *block_group; |
| 8240 | u64 free_bytes = 0; |
| 8241 | int factor; |
| 8242 | |
| 8243 | list_for_each_entry(block_group, groups_list, list) { |
| 8244 | spin_lock(&block_group->lock); |
| 8245 | |
| 8246 | if (!block_group->ro) { |
| 8247 | spin_unlock(&block_group->lock); |
| 8248 | continue; |
| 8249 | } |
| 8250 | |
| 8251 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 8252 | BTRFS_BLOCK_GROUP_RAID10 | |
| 8253 | BTRFS_BLOCK_GROUP_DUP)) |
| 8254 | factor = 2; |
| 8255 | else |
| 8256 | factor = 1; |
| 8257 | |
| 8258 | free_bytes += (block_group->key.offset - |
| 8259 | btrfs_block_group_used(&block_group->item)) * |
| 8260 | factor; |
| 8261 | |
| 8262 | spin_unlock(&block_group->lock); |
| 8263 | } |
| 8264 | |
| 8265 | return free_bytes; |
| 8266 | } |
| 8267 | |
| 8268 | /* |
| 8269 | * helper to account the unused space of all the readonly block group in the |
| 8270 | * space_info. takes mirrors into account. |
| 8271 | */ |
| 8272 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 8273 | { |
| 8274 | int i; |
| 8275 | u64 free_bytes = 0; |
| 8276 | |
| 8277 | spin_lock(&sinfo->lock); |
| 8278 | |
| 8279 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 8280 | if (!list_empty(&sinfo->block_groups[i])) |
| 8281 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 8282 | &sinfo->block_groups[i]); |
| 8283 | |
| 8284 | spin_unlock(&sinfo->lock); |
| 8285 | |
| 8286 | return free_bytes; |
| 8287 | } |
| 8288 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8289 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 8290 | struct btrfs_block_group_cache *cache) |
| 8291 | { |
| 8292 | struct btrfs_space_info *sinfo = cache->space_info; |
| 8293 | u64 num_bytes; |
| 8294 | |
| 8295 | BUG_ON(!cache->ro); |
| 8296 | |
| 8297 | spin_lock(&sinfo->lock); |
| 8298 | spin_lock(&cache->lock); |
| 8299 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 8300 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 8301 | sinfo->bytes_readonly -= num_bytes; |
| 8302 | cache->ro = 0; |
| 8303 | spin_unlock(&cache->lock); |
| 8304 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8305 | return 0; |
| 8306 | } |
| 8307 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8308 | /* |
| 8309 | * checks to see if its even possible to relocate this block group. |
| 8310 | * |
| 8311 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 8312 | * ok to go ahead and try. |
| 8313 | */ |
| 8314 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8315 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8316 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8317 | struct btrfs_space_info *space_info; |
| 8318 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 8319 | struct btrfs_device *device; |
| 8320 | int full = 0; |
| 8321 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8322 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8323 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8324 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8325 | /* odd, couldn't find the block group, leave it alone */ |
| 8326 | if (!block_group) |
| 8327 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8328 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8329 | /* no bytes used, we're good */ |
| 8330 | if (!btrfs_block_group_used(&block_group->item)) |
| 8331 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 8332 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8333 | space_info = block_group->space_info; |
| 8334 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 8335 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8336 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8337 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8338 | /* |
| 8339 | * if this is the last block group we have in this space, we can't |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 8340 | * relocate it unless we're able to allocate a new chunk below. |
| 8341 | * |
| 8342 | * Otherwise, we need to make sure we have room in the space to handle |
| 8343 | * all of the extents from this block group. If we can, we're good |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8344 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 8345 | if ((space_info->total_bytes != block_group->key.offset) && |
| 8346 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8347 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 8348 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 8349 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8350 | spin_unlock(&space_info->lock); |
| 8351 | goto out; |
| 8352 | } |
| 8353 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8354 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8355 | /* |
| 8356 | * ok we don't have enough space, but maybe we have free space on our |
| 8357 | * devices to allocate new chunks for relocation, so loop through our |
| 8358 | * alloc devices and guess if we have enough space. However, if we |
| 8359 | * were marked as full, then we know there aren't enough chunks, and we |
| 8360 | * can just return. |
| 8361 | */ |
| 8362 | ret = -1; |
| 8363 | if (full) |
| 8364 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 8365 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8366 | mutex_lock(&root->fs_info->chunk_mutex); |
| 8367 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 8368 | u64 min_free = btrfs_block_group_used(&block_group->item); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 8369 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8370 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8371 | /* |
| 8372 | * check to make sure we can actually find a chunk with enough |
| 8373 | * space to fit our block group in. |
| 8374 | */ |
| 8375 | if (device->total_bytes > device->bytes_used + min_free) { |
| 8376 | ret = find_free_dev_extent(NULL, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 8377 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8378 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8379 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8380 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8381 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8382 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8383 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8384 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8385 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8386 | return ret; |
| 8387 | } |
| 8388 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 8389 | static int find_first_block_group(struct btrfs_root *root, |
| 8390 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8391 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8392 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8393 | struct btrfs_key found_key; |
| 8394 | struct extent_buffer *leaf; |
| 8395 | int slot; |
| 8396 | |
| 8397 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 8398 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8399 | goto out; |
| 8400 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8401 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8402 | slot = path->slots[0]; |
| 8403 | leaf = path->nodes[0]; |
| 8404 | if (slot >= btrfs_header_nritems(leaf)) { |
| 8405 | ret = btrfs_next_leaf(root, path); |
| 8406 | if (ret == 0) |
| 8407 | continue; |
| 8408 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8409 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8410 | break; |
| 8411 | } |
| 8412 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 8413 | |
| 8414 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8415 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 8416 | ret = 0; |
| 8417 | goto out; |
| 8418 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8419 | path->slots[0]++; |
| 8420 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8421 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8422 | return ret; |
| 8423 | } |
| 8424 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8425 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 8426 | { |
| 8427 | struct btrfs_block_group_cache *block_group; |
| 8428 | u64 last = 0; |
| 8429 | |
| 8430 | while (1) { |
| 8431 | struct inode *inode; |
| 8432 | |
| 8433 | block_group = btrfs_lookup_first_block_group(info, last); |
| 8434 | while (block_group) { |
| 8435 | spin_lock(&block_group->lock); |
| 8436 | if (block_group->iref) |
| 8437 | break; |
| 8438 | spin_unlock(&block_group->lock); |
| 8439 | block_group = next_block_group(info->tree_root, |
| 8440 | block_group); |
| 8441 | } |
| 8442 | if (!block_group) { |
| 8443 | if (last == 0) |
| 8444 | break; |
| 8445 | last = 0; |
| 8446 | continue; |
| 8447 | } |
| 8448 | |
| 8449 | inode = block_group->inode; |
| 8450 | block_group->iref = 0; |
| 8451 | block_group->inode = NULL; |
| 8452 | spin_unlock(&block_group->lock); |
| 8453 | iput(inode); |
| 8454 | last = block_group->key.objectid + block_group->key.offset; |
| 8455 | btrfs_put_block_group(block_group); |
| 8456 | } |
| 8457 | } |
| 8458 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8459 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 8460 | { |
| 8461 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8462 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8463 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8464 | struct rb_node *n; |
| 8465 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8466 | down_write(&info->extent_commit_sem); |
| 8467 | while (!list_empty(&info->caching_block_groups)) { |
| 8468 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 8469 | struct btrfs_caching_control, list); |
| 8470 | list_del(&caching_ctl->list); |
| 8471 | put_caching_control(caching_ctl); |
| 8472 | } |
| 8473 | up_write(&info->extent_commit_sem); |
| 8474 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8475 | spin_lock(&info->block_group_cache_lock); |
| 8476 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 8477 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 8478 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8479 | rb_erase(&block_group->cache_node, |
| 8480 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8481 | spin_unlock(&info->block_group_cache_lock); |
| 8482 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8483 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8484 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8485 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8486 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8487 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8488 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8489 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8490 | /* |
| 8491 | * We haven't cached this block group, which means we could |
| 8492 | * possibly have excluded extents on this block group. |
| 8493 | */ |
| 8494 | if (block_group->cached == BTRFS_CACHE_NO) |
| 8495 | free_excluded_extents(info->extent_root, block_group); |
| 8496 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8497 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 8498 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8499 | |
| 8500 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8501 | } |
| 8502 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8503 | |
| 8504 | /* now that all the block groups are freed, go through and |
| 8505 | * free all the space_info structs. This is only called during |
| 8506 | * the final stages of unmount, and so we know nobody is |
| 8507 | * using them. We call synchronize_rcu() once before we start, |
| 8508 | * just to be on the safe side. |
| 8509 | */ |
| 8510 | synchronize_rcu(); |
| 8511 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 8512 | release_global_block_rsv(info); |
| 8513 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8514 | while(!list_empty(&info->space_info)) { |
| 8515 | space_info = list_entry(info->space_info.next, |
| 8516 | struct btrfs_space_info, |
| 8517 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8518 | if (space_info->bytes_pinned > 0 || |
| 8519 | space_info->bytes_reserved > 0) { |
| 8520 | WARN_ON(1); |
| 8521 | dump_space_info(space_info, 0, 0); |
| 8522 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8523 | list_del(&space_info->list); |
| 8524 | kfree(space_info); |
| 8525 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8526 | return 0; |
| 8527 | } |
| 8528 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8529 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 8530 | struct btrfs_block_group_cache *cache) |
| 8531 | { |
| 8532 | int index = get_block_group_index(cache); |
| 8533 | |
| 8534 | down_write(&space_info->groups_sem); |
| 8535 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 8536 | up_write(&space_info->groups_sem); |
| 8537 | } |
| 8538 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8539 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 8540 | { |
| 8541 | struct btrfs_path *path; |
| 8542 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8543 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8544 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8545 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8546 | struct btrfs_key key; |
| 8547 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8548 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8549 | int need_clear = 0; |
| 8550 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8551 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8552 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8553 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8554 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8555 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8556 | path = btrfs_alloc_path(); |
| 8557 | if (!path) |
| 8558 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame^] | 8559 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8560 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8561 | cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); |
| 8562 | if (cache_gen != 0 && |
| 8563 | btrfs_super_generation(&root->fs_info->super_copy) != cache_gen) |
| 8564 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 8565 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 8566 | need_clear = 1; |
Josef Bacik | 8216ef8 | 2010-10-28 16:55:47 -0400 | [diff] [blame] | 8567 | if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen) |
| 8568 | printk(KERN_INFO "btrfs: disk space caching is enabled\n"); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8569 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8570 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8571 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8572 | if (ret > 0) |
| 8573 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8574 | if (ret != 0) |
| 8575 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8576 | leaf = path->nodes[0]; |
| 8577 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8578 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8579 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8580 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8581 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8582 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 8583 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8584 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8585 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 8586 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8587 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8588 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8589 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8590 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8591 | if (need_clear) |
| 8592 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 8593 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8594 | /* |
| 8595 | * we only want to have 32k of ram per block group for keeping |
| 8596 | * track of free space, and if we pass 1/2 of that we want to |
| 8597 | * start converting things over to using bitmaps |
| 8598 | */ |
| 8599 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 8600 | sizeof(struct btrfs_free_space); |
| 8601 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8602 | read_extent_buffer(leaf, &cache->item, |
| 8603 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 8604 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8605 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8606 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8607 | key.objectid = found_key.objectid + found_key.offset; |
| 8608 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8609 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8610 | cache->sectorsize = root->sectorsize; |
| 8611 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8612 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8613 | * We need to exclude the super stripes now so that the space |
| 8614 | * info has super bytes accounted for, otherwise we'll think |
| 8615 | * we have more space than we actually do. |
| 8616 | */ |
| 8617 | exclude_super_stripes(root, cache); |
| 8618 | |
| 8619 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8620 | * check for two cases, either we are full, and therefore |
| 8621 | * don't need to bother with the caching work since we won't |
| 8622 | * find any space, or we are empty, and we can just add all |
| 8623 | * the space in and be done with it. This saves us _alot_ of |
| 8624 | * time, particularly in the full case. |
| 8625 | */ |
| 8626 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8627 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8628 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8629 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8630 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8631 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8632 | cache->cached = BTRFS_CACHE_FINISHED; |
| 8633 | add_new_free_space(cache, root->fs_info, |
| 8634 | found_key.objectid, |
| 8635 | found_key.objectid + |
| 8636 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8637 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8638 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8639 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8640 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 8641 | btrfs_block_group_used(&cache->item), |
| 8642 | &space_info); |
| 8643 | BUG_ON(ret); |
| 8644 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8645 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8646 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8647 | spin_unlock(&cache->space_info->lock); |
| 8648 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8649 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8650 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8651 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8652 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 8653 | |
| 8654 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 8655 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8656 | set_block_group_ro(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8657 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8658 | |
| 8659 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 8660 | if (!(get_alloc_profile(root, space_info->flags) & |
| 8661 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 8662 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8663 | BTRFS_BLOCK_GROUP_DUP))) |
| 8664 | continue; |
| 8665 | /* |
| 8666 | * avoid allocating from un-mirrored block group if there are |
| 8667 | * mirrored block groups. |
| 8668 | */ |
| 8669 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8670 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8671 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8672 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8673 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8674 | |
| 8675 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8676 | ret = 0; |
| 8677 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8678 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8679 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8680 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8681 | |
| 8682 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 8683 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8684 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8685 | u64 size) |
| 8686 | { |
| 8687 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8688 | struct btrfs_root *extent_root; |
| 8689 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8690 | |
| 8691 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8692 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 8693 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8694 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 8695 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8696 | if (!cache) |
| 8697 | return -ENOMEM; |
| 8698 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8699 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8700 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8701 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8702 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8703 | cache->fs_info = root->fs_info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8704 | |
| 8705 | /* |
| 8706 | * we only want to have 32k of ram per block group for keeping track |
| 8707 | * of free space, and if we pass 1/2 of that we want to start |
| 8708 | * converting things over to using bitmaps |
| 8709 | */ |
| 8710 | cache->extents_thresh = ((1024 * 32) / 2) / |
| 8711 | sizeof(struct btrfs_free_space); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8712 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8713 | spin_lock_init(&cache->lock); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 8714 | spin_lock_init(&cache->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8715 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8716 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8717 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8718 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8719 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 8720 | cache->flags = type; |
| 8721 | btrfs_set_block_group_flags(&cache->item, type); |
| 8722 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8723 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8724 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8725 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8726 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8727 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 8728 | chunk_offset + size); |
| 8729 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8730 | free_excluded_extents(root, cache); |
| 8731 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8732 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 8733 | &cache->space_info); |
| 8734 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8735 | |
| 8736 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8737 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8738 | spin_unlock(&cache->space_info->lock); |
| 8739 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8740 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8741 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8742 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8743 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8744 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8745 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 8746 | sizeof(cache->item)); |
| 8747 | BUG_ON(ret); |
| 8748 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 8749 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8750 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8751 | return 0; |
| 8752 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8753 | |
| 8754 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 8755 | struct btrfs_root *root, u64 group_start) |
| 8756 | { |
| 8757 | struct btrfs_path *path; |
| 8758 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8759 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8760 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8761 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8762 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8763 | int ret; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8764 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8765 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8766 | root = root->fs_info->extent_root; |
| 8767 | |
| 8768 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 8769 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8770 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8771 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 8772 | /* |
| 8773 | * Free the reserved super bytes from this block group before |
| 8774 | * remove it. |
| 8775 | */ |
| 8776 | free_excluded_extents(root, block_group); |
| 8777 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8778 | memcpy(&key, &block_group->key, sizeof(key)); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8779 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 8780 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8781 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8782 | factor = 2; |
| 8783 | else |
| 8784 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8785 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8786 | /* make sure this block group isn't part of an allocation cluster */ |
| 8787 | cluster = &root->fs_info->data_alloc_cluster; |
| 8788 | spin_lock(&cluster->refill_lock); |
| 8789 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8790 | spin_unlock(&cluster->refill_lock); |
| 8791 | |
| 8792 | /* |
| 8793 | * make sure this block group isn't part of a metadata |
| 8794 | * allocation cluster |
| 8795 | */ |
| 8796 | cluster = &root->fs_info->meta_alloc_cluster; |
| 8797 | spin_lock(&cluster->refill_lock); |
| 8798 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8799 | spin_unlock(&cluster->refill_lock); |
| 8800 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8801 | path = btrfs_alloc_path(); |
| 8802 | BUG_ON(!path); |
| 8803 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8804 | inode = lookup_free_space_inode(root, block_group, path); |
| 8805 | if (!IS_ERR(inode)) { |
| 8806 | btrfs_orphan_add(trans, inode); |
| 8807 | clear_nlink(inode); |
| 8808 | /* One for the block groups ref */ |
| 8809 | spin_lock(&block_group->lock); |
| 8810 | if (block_group->iref) { |
| 8811 | block_group->iref = 0; |
| 8812 | block_group->inode = NULL; |
| 8813 | spin_unlock(&block_group->lock); |
| 8814 | iput(inode); |
| 8815 | } else { |
| 8816 | spin_unlock(&block_group->lock); |
| 8817 | } |
| 8818 | /* One for our lookup ref */ |
| 8819 | iput(inode); |
| 8820 | } |
| 8821 | |
| 8822 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 8823 | key.offset = block_group->key.objectid; |
| 8824 | key.type = 0; |
| 8825 | |
| 8826 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 8827 | if (ret < 0) |
| 8828 | goto out; |
| 8829 | if (ret > 0) |
| 8830 | btrfs_release_path(tree_root, path); |
| 8831 | if (ret == 0) { |
| 8832 | ret = btrfs_del_item(trans, tree_root, path); |
| 8833 | if (ret) |
| 8834 | goto out; |
| 8835 | btrfs_release_path(tree_root, path); |
| 8836 | } |
| 8837 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8838 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8839 | rb_erase(&block_group->cache_node, |
| 8840 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 8841 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8842 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8843 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8844 | /* |
| 8845 | * we must use list_del_init so people can check to see if they |
| 8846 | * are still on the list after taking the semaphore |
| 8847 | */ |
| 8848 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8849 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8850 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8851 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8852 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8853 | |
| 8854 | btrfs_remove_free_space_cache(block_group); |
| 8855 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8856 | spin_lock(&block_group->space_info->lock); |
| 8857 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 8858 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8859 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8860 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8861 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8862 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8863 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 8864 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8865 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 8866 | btrfs_put_block_group(block_group); |
| 8867 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8868 | |
| 8869 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 8870 | if (ret > 0) |
| 8871 | ret = -EIO; |
| 8872 | if (ret < 0) |
| 8873 | goto out; |
| 8874 | |
| 8875 | ret = btrfs_del_item(trans, root, path); |
| 8876 | out: |
| 8877 | btrfs_free_path(path); |
| 8878 | return ret; |
| 8879 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8880 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8881 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 8882 | { |
| 8883 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8884 | struct btrfs_super_block *disk_super; |
| 8885 | u64 features; |
| 8886 | u64 flags; |
| 8887 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8888 | int ret; |
| 8889 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8890 | disk_super = &fs_info->super_copy; |
| 8891 | if (!btrfs_super_root(disk_super)) |
| 8892 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8893 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8894 | features = btrfs_super_incompat_flags(disk_super); |
| 8895 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 8896 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8897 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8898 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 8899 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8900 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8901 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8902 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 8903 | if (mixed) { |
| 8904 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 8905 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8906 | } else { |
| 8907 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 8908 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8909 | if (ret) |
| 8910 | goto out; |
| 8911 | |
| 8912 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 8913 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 8914 | } |
| 8915 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 8916 | return ret; |
| 8917 | } |
| 8918 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8919 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 8920 | { |
| 8921 | return unpin_extent_range(root, start, end); |
| 8922 | } |
| 8923 | |
| 8924 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 8925 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8926 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 8927 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 8928 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 8929 | |
| 8930 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 8931 | { |
| 8932 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 8933 | struct btrfs_block_group_cache *cache = NULL; |
| 8934 | u64 group_trimmed; |
| 8935 | u64 start; |
| 8936 | u64 end; |
| 8937 | u64 trimmed = 0; |
| 8938 | int ret = 0; |
| 8939 | |
| 8940 | cache = btrfs_lookup_block_group(fs_info, range->start); |
| 8941 | |
| 8942 | while (cache) { |
| 8943 | if (cache->key.objectid >= (range->start + range->len)) { |
| 8944 | btrfs_put_block_group(cache); |
| 8945 | break; |
| 8946 | } |
| 8947 | |
| 8948 | start = max(range->start, cache->key.objectid); |
| 8949 | end = min(range->start + range->len, |
| 8950 | cache->key.objectid + cache->key.offset); |
| 8951 | |
| 8952 | if (end - start >= range->minlen) { |
| 8953 | if (!block_group_cache_done(cache)) { |
| 8954 | ret = cache_block_group(cache, NULL, root, 0); |
| 8955 | if (!ret) |
| 8956 | wait_block_group_cache_done(cache); |
| 8957 | } |
| 8958 | ret = btrfs_trim_block_group(cache, |
| 8959 | &group_trimmed, |
| 8960 | start, |
| 8961 | end, |
| 8962 | range->minlen); |
| 8963 | |
| 8964 | trimmed += group_trimmed; |
| 8965 | if (ret) { |
| 8966 | btrfs_put_block_group(cache); |
| 8967 | break; |
| 8968 | } |
| 8969 | } |
| 8970 | |
| 8971 | cache = next_block_group(fs_info->tree_root, cache); |
| 8972 | } |
| 8973 | |
| 8974 | range->len = trimmed; |
| 8975 | return ret; |
| 8976 | } |