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 | fa9c0d7 | 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 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 97 | static void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 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); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 108 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 109 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 110 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 113 | /* |
| 114 | * this adds the block group to the fs_info rb tree for the block group |
| 115 | * cache |
| 116 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 117 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 118 | struct btrfs_block_group_cache *block_group) |
| 119 | { |
| 120 | struct rb_node **p; |
| 121 | struct rb_node *parent = NULL; |
| 122 | struct btrfs_block_group_cache *cache; |
| 123 | |
| 124 | spin_lock(&info->block_group_cache_lock); |
| 125 | p = &info->block_group_cache_tree.rb_node; |
| 126 | |
| 127 | while (*p) { |
| 128 | parent = *p; |
| 129 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 130 | cache_node); |
| 131 | if (block_group->key.objectid < cache->key.objectid) { |
| 132 | p = &(*p)->rb_left; |
| 133 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 134 | p = &(*p)->rb_right; |
| 135 | } else { |
| 136 | spin_unlock(&info->block_group_cache_lock); |
| 137 | return -EEXIST; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | rb_link_node(&block_group->cache_node, parent, p); |
| 142 | rb_insert_color(&block_group->cache_node, |
| 143 | &info->block_group_cache_tree); |
| 144 | spin_unlock(&info->block_group_cache_lock); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * This will return the block group at or after bytenr if contains is 0, else |
| 151 | * it will return the block group that contains the bytenr |
| 152 | */ |
| 153 | static struct btrfs_block_group_cache * |
| 154 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 155 | int contains) |
| 156 | { |
| 157 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 158 | struct rb_node *n; |
| 159 | u64 end, start; |
| 160 | |
| 161 | spin_lock(&info->block_group_cache_lock); |
| 162 | n = info->block_group_cache_tree.rb_node; |
| 163 | |
| 164 | while (n) { |
| 165 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 166 | cache_node); |
| 167 | end = cache->key.objectid + cache->key.offset - 1; |
| 168 | start = cache->key.objectid; |
| 169 | |
| 170 | if (bytenr < start) { |
| 171 | if (!contains && (!ret || start < ret->key.objectid)) |
| 172 | ret = cache; |
| 173 | n = n->rb_left; |
| 174 | } else if (bytenr > start) { |
| 175 | if (contains && bytenr <= end) { |
| 176 | ret = cache; |
| 177 | break; |
| 178 | } |
| 179 | n = n->rb_right; |
| 180 | } else { |
| 181 | ret = cache; |
| 182 | break; |
| 183 | } |
| 184 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 185 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 186 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 187 | spin_unlock(&info->block_group_cache_lock); |
| 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 192 | static int add_excluded_extent(struct btrfs_root *root, |
| 193 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 194 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 195 | u64 end = start + num_bytes - 1; |
| 196 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 197 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 198 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 199 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 200 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 203 | static void free_excluded_extents(struct btrfs_root *root, |
| 204 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 205 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 206 | u64 start, end; |
| 207 | |
| 208 | start = cache->key.objectid; |
| 209 | end = start + cache->key.offset - 1; |
| 210 | |
| 211 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 212 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 213 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 214 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 215 | } |
| 216 | |
| 217 | static int exclude_super_stripes(struct btrfs_root *root, |
| 218 | struct btrfs_block_group_cache *cache) |
| 219 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 220 | u64 bytenr; |
| 221 | u64 *logical; |
| 222 | int stripe_len; |
| 223 | int i, nr, ret; |
| 224 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 225 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 226 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 227 | cache->bytes_super += stripe_len; |
| 228 | ret = add_excluded_extent(root, cache->key.objectid, |
| 229 | stripe_len); |
| 230 | BUG_ON(ret); |
| 231 | } |
| 232 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 233 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 234 | bytenr = btrfs_sb_offset(i); |
| 235 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 236 | cache->key.objectid, bytenr, |
| 237 | 0, &logical, &nr, &stripe_len); |
| 238 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 239 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 240 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 241 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 242 | ret = add_excluded_extent(root, logical[nr], |
| 243 | stripe_len); |
| 244 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 245 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 246 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 247 | kfree(logical); |
| 248 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 252 | static struct btrfs_caching_control * |
| 253 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 254 | { |
| 255 | struct btrfs_caching_control *ctl; |
| 256 | |
| 257 | spin_lock(&cache->lock); |
| 258 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 259 | spin_unlock(&cache->lock); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 263 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 264 | if (!cache->caching_ctl) { |
| 265 | spin_unlock(&cache->lock); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 269 | ctl = cache->caching_ctl; |
| 270 | atomic_inc(&ctl->count); |
| 271 | spin_unlock(&cache->lock); |
| 272 | return ctl; |
| 273 | } |
| 274 | |
| 275 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 276 | { |
| 277 | if (atomic_dec_and_test(&ctl->count)) |
| 278 | kfree(ctl); |
| 279 | } |
| 280 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 281 | /* |
| 282 | * this is only called by cache_block_group, since we could have freed extents |
| 283 | * we need to check the pinned_extents for any extents that can't be used yet |
| 284 | * since their free space will be released as soon as the transaction commits. |
| 285 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 286 | 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] | 287 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 288 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 289 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 290 | int ret; |
| 291 | |
| 292 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 293 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 294 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 295 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 296 | if (ret) |
| 297 | break; |
| 298 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 299 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 300 | start = extent_end + 1; |
| 301 | } else if (extent_start > start && extent_start < end) { |
| 302 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 303 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 304 | ret = btrfs_add_free_space(block_group, start, |
| 305 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 306 | BUG_ON(ret); |
| 307 | start = extent_end + 1; |
| 308 | } else { |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | if (start < end) { |
| 314 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 315 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 316 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 317 | BUG_ON(ret); |
| 318 | } |
| 319 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 320 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 321 | } |
| 322 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 323 | static noinline void caching_thread(struct btrfs_work *work) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 324 | { |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 325 | struct btrfs_block_group_cache *block_group; |
| 326 | struct btrfs_fs_info *fs_info; |
| 327 | struct btrfs_caching_control *caching_ctl; |
| 328 | struct btrfs_root *extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 329 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 330 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 331 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 332 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 333 | u64 last = 0; |
| 334 | u32 nritems; |
| 335 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 336 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 337 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 338 | block_group = caching_ctl->block_group; |
| 339 | fs_info = block_group->fs_info; |
| 340 | extent_root = fs_info->extent_root; |
| 341 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 342 | path = btrfs_alloc_path(); |
| 343 | if (!path) |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 344 | goto out; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 345 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 346 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 347 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 348 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 349 | * We don't want to deadlock with somebody trying to allocate a new |
| 350 | * extent for the extent root while also trying to search the extent |
| 351 | * root to add free space. So we skip locking and search the commit |
| 352 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 353 | */ |
| 354 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 355 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 356 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 357 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 358 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 359 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 360 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 361 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 362 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 363 | /* need to make sure the commit_root doesn't disappear */ |
| 364 | down_read(&fs_info->extent_commit_sem); |
| 365 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 366 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 367 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 368 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 369 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 370 | leaf = path->nodes[0]; |
| 371 | nritems = btrfs_header_nritems(leaf); |
| 372 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 373 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 374 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 375 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 376 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 377 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 378 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 379 | if (path->slots[0] < nritems) { |
| 380 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 381 | } else { |
| 382 | ret = find_next_key(path, 0, &key); |
| 383 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 384 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 385 | |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 386 | if (need_resched() || |
| 387 | btrfs_next_leaf(extent_root, path)) { |
| 388 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 389 | btrfs_release_path(path); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 390 | up_read(&fs_info->extent_commit_sem); |
| 391 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 392 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 393 | goto again; |
| 394 | } |
| 395 | leaf = path->nodes[0]; |
| 396 | nritems = btrfs_header_nritems(leaf); |
| 397 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 398 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 399 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 400 | if (key.objectid < block_group->key.objectid) { |
| 401 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 402 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 403 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 404 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 405 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 406 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 407 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 408 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 409 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 410 | total_found += add_new_free_space(block_group, |
| 411 | fs_info, last, |
| 412 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 413 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 414 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 415 | if (total_found > (1024 * 1024 * 2)) { |
| 416 | total_found = 0; |
| 417 | wake_up(&caching_ctl->wait); |
| 418 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 419 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 420 | path->slots[0]++; |
| 421 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 422 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 423 | |
| 424 | total_found += add_new_free_space(block_group, fs_info, last, |
| 425 | block_group->key.objectid + |
| 426 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 427 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 428 | |
| 429 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 430 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 431 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 432 | spin_unlock(&block_group->lock); |
| 433 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 434 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 435 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 436 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 437 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 438 | free_excluded_extents(extent_root, block_group); |
| 439 | |
| 440 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 441 | out: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 442 | wake_up(&caching_ctl->wait); |
| 443 | |
| 444 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 445 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 448 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
| 449 | struct btrfs_trans_handle *trans, |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 450 | struct btrfs_root *root, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 451 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 452 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 453 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 454 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 455 | int ret = 0; |
| 456 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 457 | smp_mb(); |
| 458 | if (cache->cached != BTRFS_CACHE_NO) |
| 459 | return 0; |
| 460 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 461 | /* |
| 462 | * 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] | 463 | * to have the normal tree locking. Also if we are currently trying to |
| 464 | * allocate blocks for the tree root we can't do the fast caching since |
| 465 | * we likely hold important locks. |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 466 | */ |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 467 | if (trans && (!trans->transaction->in_commit) && |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 468 | (root && root != root->fs_info->tree_root)) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 469 | spin_lock(&cache->lock); |
| 470 | if (cache->cached != BTRFS_CACHE_NO) { |
| 471 | spin_unlock(&cache->lock); |
| 472 | return 0; |
| 473 | } |
| 474 | cache->cached = BTRFS_CACHE_STARTED; |
| 475 | spin_unlock(&cache->lock); |
| 476 | |
| 477 | ret = load_free_space_cache(fs_info, cache); |
| 478 | |
| 479 | spin_lock(&cache->lock); |
| 480 | if (ret == 1) { |
| 481 | cache->cached = BTRFS_CACHE_FINISHED; |
| 482 | cache->last_byte_to_unpin = (u64)-1; |
| 483 | } else { |
| 484 | cache->cached = BTRFS_CACHE_NO; |
| 485 | } |
| 486 | spin_unlock(&cache->lock); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 487 | if (ret == 1) { |
| 488 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 489 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 490 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | if (load_cache_only) |
| 494 | return 0; |
| 495 | |
Miao Xie | fc0e4a3 | 2011-03-24 11:41:21 +0000 | [diff] [blame] | 496 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 497 | BUG_ON(!caching_ctl); |
| 498 | |
| 499 | INIT_LIST_HEAD(&caching_ctl->list); |
| 500 | mutex_init(&caching_ctl->mutex); |
| 501 | init_waitqueue_head(&caching_ctl->wait); |
| 502 | caching_ctl->block_group = cache; |
| 503 | caching_ctl->progress = cache->key.objectid; |
| 504 | /* one for caching kthread, one for caching block group list */ |
| 505 | atomic_set(&caching_ctl->count, 2); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 506 | caching_ctl->work.func = caching_thread; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 507 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 508 | spin_lock(&cache->lock); |
| 509 | if (cache->cached != BTRFS_CACHE_NO) { |
| 510 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 511 | kfree(caching_ctl); |
| 512 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 513 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 514 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 515 | cache->cached = BTRFS_CACHE_STARTED; |
| 516 | spin_unlock(&cache->lock); |
| 517 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 518 | down_write(&fs_info->extent_commit_sem); |
| 519 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 520 | up_write(&fs_info->extent_commit_sem); |
| 521 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 522 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 523 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 524 | btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 525 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 526 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 529 | /* |
| 530 | * return the block group that starts at or after bytenr |
| 531 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 532 | static struct btrfs_block_group_cache * |
| 533 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 534 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 535 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 536 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 537 | cache = block_group_cache_tree_search(info, bytenr, 0); |
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 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 542 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 543 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 544 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 545 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 546 | struct btrfs_fs_info *info, |
| 547 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 548 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 549 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 550 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 551 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 552 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 553 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 554 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 555 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 556 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 557 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 558 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 559 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 560 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 561 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 562 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 563 | BTRFS_BLOCK_GROUP_METADATA; |
| 564 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 565 | rcu_read_lock(); |
| 566 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 567 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 568 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 569 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 570 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 571 | } |
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 NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 574 | } |
| 575 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 576 | /* |
| 577 | * after adding space to the filesystem, we need to clear the full flags |
| 578 | * on all the space infos. |
| 579 | */ |
| 580 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 581 | { |
| 582 | struct list_head *head = &info->space_info; |
| 583 | struct btrfs_space_info *found; |
| 584 | |
| 585 | rcu_read_lock(); |
| 586 | list_for_each_entry_rcu(found, head, list) |
| 587 | found->full = 0; |
| 588 | rcu_read_unlock(); |
| 589 | } |
| 590 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 591 | static u64 div_factor(u64 num, int factor) |
| 592 | { |
| 593 | if (factor == 10) |
| 594 | return num; |
| 595 | num *= factor; |
| 596 | do_div(num, 10); |
| 597 | return num; |
| 598 | } |
| 599 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 600 | static u64 div_factor_fine(u64 num, int factor) |
| 601 | { |
| 602 | if (factor == 100) |
| 603 | return num; |
| 604 | num *= factor; |
| 605 | do_div(num, 100); |
| 606 | return num; |
| 607 | } |
| 608 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 609 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 610 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 611 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 612 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 613 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 614 | u64 last = max(search_hint, search_start); |
| 615 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 616 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 617 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 618 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 619 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 620 | while (1) { |
| 621 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 622 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 623 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 624 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 625 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 626 | last = cache->key.objectid + cache->key.offset; |
| 627 | used = btrfs_block_group_used(&cache->item); |
| 628 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 629 | if ((full_search || !cache->ro) && |
| 630 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 631 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 632 | div_factor(cache->key.offset, factor)) { |
| 633 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 634 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 635 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 636 | goto found; |
| 637 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 638 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 639 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 640 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 641 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 642 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 643 | if (!wrapped) { |
| 644 | last = search_start; |
| 645 | wrapped = 1; |
| 646 | goto again; |
| 647 | } |
| 648 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 649 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 650 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 651 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 652 | goto again; |
| 653 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 654 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 655 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 656 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 657 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 658 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 659 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 660 | { |
| 661 | int ret; |
| 662 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 663 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 664 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 665 | path = btrfs_alloc_path(); |
| 666 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 667 | key.objectid = start; |
| 668 | key.offset = len; |
| 669 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 670 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 671 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 672 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 673 | return ret; |
| 674 | } |
| 675 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 676 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 677 | * helper function to lookup reference count and flags of extent. |
| 678 | * |
| 679 | * the head node for delayed ref is used to store the sum of all the |
| 680 | * reference count modifications queued up in the rbtree. the head |
| 681 | * node may also store the extent flags to set. This way you can check |
| 682 | * to see what the reference count and extent flags would be if all of |
| 683 | * the delayed refs are not processed. |
| 684 | */ |
| 685 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 686 | struct btrfs_root *root, u64 bytenr, |
| 687 | u64 num_bytes, u64 *refs, u64 *flags) |
| 688 | { |
| 689 | struct btrfs_delayed_ref_head *head; |
| 690 | struct btrfs_delayed_ref_root *delayed_refs; |
| 691 | struct btrfs_path *path; |
| 692 | struct btrfs_extent_item *ei; |
| 693 | struct extent_buffer *leaf; |
| 694 | struct btrfs_key key; |
| 695 | u32 item_size; |
| 696 | u64 num_refs; |
| 697 | u64 extent_flags; |
| 698 | int ret; |
| 699 | |
| 700 | path = btrfs_alloc_path(); |
| 701 | if (!path) |
| 702 | return -ENOMEM; |
| 703 | |
| 704 | key.objectid = bytenr; |
| 705 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 706 | key.offset = num_bytes; |
| 707 | if (!trans) { |
| 708 | path->skip_locking = 1; |
| 709 | path->search_commit_root = 1; |
| 710 | } |
| 711 | again: |
| 712 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 713 | &key, path, 0, 0); |
| 714 | if (ret < 0) |
| 715 | goto out_free; |
| 716 | |
| 717 | if (ret == 0) { |
| 718 | leaf = path->nodes[0]; |
| 719 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 720 | if (item_size >= sizeof(*ei)) { |
| 721 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 722 | struct btrfs_extent_item); |
| 723 | num_refs = btrfs_extent_refs(leaf, ei); |
| 724 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 725 | } else { |
| 726 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 727 | struct btrfs_extent_item_v0 *ei0; |
| 728 | BUG_ON(item_size != sizeof(*ei0)); |
| 729 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 730 | struct btrfs_extent_item_v0); |
| 731 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 732 | /* FIXME: this isn't correct for data */ |
| 733 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 734 | #else |
| 735 | BUG(); |
| 736 | #endif |
| 737 | } |
| 738 | BUG_ON(num_refs == 0); |
| 739 | } else { |
| 740 | num_refs = 0; |
| 741 | extent_flags = 0; |
| 742 | ret = 0; |
| 743 | } |
| 744 | |
| 745 | if (!trans) |
| 746 | goto out; |
| 747 | |
| 748 | delayed_refs = &trans->transaction->delayed_refs; |
| 749 | spin_lock(&delayed_refs->lock); |
| 750 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 751 | if (head) { |
| 752 | if (!mutex_trylock(&head->mutex)) { |
| 753 | atomic_inc(&head->node.refs); |
| 754 | spin_unlock(&delayed_refs->lock); |
| 755 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 756 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 757 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 758 | /* |
| 759 | * Mutex was contended, block until it's released and try |
| 760 | * again |
| 761 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 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 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 940 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 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); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 953 | |
| 954 | leaf = path->nodes[0]; |
| 955 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 956 | btrfs_set_extent_refs(leaf, item, refs); |
| 957 | /* FIXME: get real generation */ |
| 958 | btrfs_set_extent_generation(leaf, item, 0); |
| 959 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 960 | btrfs_set_extent_flags(leaf, item, |
| 961 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 962 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 963 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 964 | /* FIXME: get first key of the block */ |
| 965 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 966 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 967 | } else { |
| 968 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 969 | } |
| 970 | btrfs_mark_buffer_dirty(leaf); |
| 971 | return 0; |
| 972 | } |
| 973 | #endif |
| 974 | |
| 975 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 976 | { |
| 977 | u32 high_crc = ~(u32)0; |
| 978 | u32 low_crc = ~(u32)0; |
| 979 | __le64 lenum; |
| 980 | |
| 981 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 982 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 983 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 984 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 985 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 986 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 987 | |
| 988 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 989 | } |
| 990 | |
| 991 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 992 | struct btrfs_extent_data_ref *ref) |
| 993 | { |
| 994 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 995 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 996 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 997 | } |
| 998 | |
| 999 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1000 | struct btrfs_extent_data_ref *ref, |
| 1001 | u64 root_objectid, u64 owner, u64 offset) |
| 1002 | { |
| 1003 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1004 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1005 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1006 | return 0; |
| 1007 | return 1; |
| 1008 | } |
| 1009 | |
| 1010 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1011 | struct btrfs_root *root, |
| 1012 | struct btrfs_path *path, |
| 1013 | u64 bytenr, u64 parent, |
| 1014 | u64 root_objectid, |
| 1015 | u64 owner, u64 offset) |
| 1016 | { |
| 1017 | struct btrfs_key key; |
| 1018 | struct btrfs_extent_data_ref *ref; |
| 1019 | struct extent_buffer *leaf; |
| 1020 | u32 nritems; |
| 1021 | int ret; |
| 1022 | int recow; |
| 1023 | int err = -ENOENT; |
| 1024 | |
| 1025 | key.objectid = bytenr; |
| 1026 | if (parent) { |
| 1027 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1028 | key.offset = parent; |
| 1029 | } else { |
| 1030 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1031 | key.offset = hash_extent_data_ref(root_objectid, |
| 1032 | owner, offset); |
| 1033 | } |
| 1034 | again: |
| 1035 | recow = 0; |
| 1036 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1037 | if (ret < 0) { |
| 1038 | err = ret; |
| 1039 | goto fail; |
| 1040 | } |
| 1041 | |
| 1042 | if (parent) { |
| 1043 | if (!ret) |
| 1044 | return 0; |
| 1045 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1046 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1047 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1048 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1049 | if (ret < 0) { |
| 1050 | err = ret; |
| 1051 | goto fail; |
| 1052 | } |
| 1053 | if (!ret) |
| 1054 | return 0; |
| 1055 | #endif |
| 1056 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1060 | nritems = btrfs_header_nritems(leaf); |
| 1061 | while (1) { |
| 1062 | if (path->slots[0] >= nritems) { |
| 1063 | ret = btrfs_next_leaf(root, path); |
| 1064 | if (ret < 0) |
| 1065 | err = ret; |
| 1066 | if (ret) |
| 1067 | goto fail; |
| 1068 | |
| 1069 | leaf = path->nodes[0]; |
| 1070 | nritems = btrfs_header_nritems(leaf); |
| 1071 | recow = 1; |
| 1072 | } |
| 1073 | |
| 1074 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1075 | if (key.objectid != bytenr || |
| 1076 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1077 | goto fail; |
| 1078 | |
| 1079 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1080 | struct btrfs_extent_data_ref); |
| 1081 | |
| 1082 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1083 | owner, offset)) { |
| 1084 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1085 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1086 | goto again; |
| 1087 | } |
| 1088 | err = 0; |
| 1089 | break; |
| 1090 | } |
| 1091 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1092 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1093 | fail: |
| 1094 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1095 | } |
| 1096 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1097 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1098 | struct btrfs_root *root, |
| 1099 | struct btrfs_path *path, |
| 1100 | u64 bytenr, u64 parent, |
| 1101 | u64 root_objectid, u64 owner, |
| 1102 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1103 | { |
| 1104 | struct btrfs_key key; |
| 1105 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1106 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1107 | u32 num_refs; |
| 1108 | int ret; |
| 1109 | |
| 1110 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1111 | if (parent) { |
| 1112 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1113 | key.offset = parent; |
| 1114 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1115 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1116 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1117 | key.offset = hash_extent_data_ref(root_objectid, |
| 1118 | owner, offset); |
| 1119 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1120 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1121 | |
| 1122 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1123 | if (ret && ret != -EEXIST) |
| 1124 | goto fail; |
| 1125 | |
| 1126 | leaf = path->nodes[0]; |
| 1127 | if (parent) { |
| 1128 | struct btrfs_shared_data_ref *ref; |
| 1129 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1130 | struct btrfs_shared_data_ref); |
| 1131 | if (ret == 0) { |
| 1132 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1133 | } else { |
| 1134 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1135 | num_refs += refs_to_add; |
| 1136 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1137 | } |
| 1138 | } else { |
| 1139 | struct btrfs_extent_data_ref *ref; |
| 1140 | while (ret == -EEXIST) { |
| 1141 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1142 | struct btrfs_extent_data_ref); |
| 1143 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1144 | owner, offset)) |
| 1145 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1146 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1147 | key.offset++; |
| 1148 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1149 | size); |
| 1150 | if (ret && ret != -EEXIST) |
| 1151 | goto fail; |
| 1152 | |
| 1153 | leaf = path->nodes[0]; |
| 1154 | } |
| 1155 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1156 | struct btrfs_extent_data_ref); |
| 1157 | if (ret == 0) { |
| 1158 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1159 | root_objectid); |
| 1160 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1161 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1162 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1163 | } else { |
| 1164 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1165 | num_refs += refs_to_add; |
| 1166 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1167 | } |
| 1168 | } |
| 1169 | btrfs_mark_buffer_dirty(leaf); |
| 1170 | ret = 0; |
| 1171 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1172 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1173 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1174 | } |
| 1175 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1176 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1177 | struct btrfs_root *root, |
| 1178 | struct btrfs_path *path, |
| 1179 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1180 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1181 | struct btrfs_key key; |
| 1182 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1183 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1184 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1185 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1186 | int ret = 0; |
| 1187 | |
| 1188 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1189 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1190 | |
| 1191 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1192 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1193 | struct btrfs_extent_data_ref); |
| 1194 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1195 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1196 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1197 | struct btrfs_shared_data_ref); |
| 1198 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1199 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1200 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1201 | struct btrfs_extent_ref_v0 *ref0; |
| 1202 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1203 | struct btrfs_extent_ref_v0); |
| 1204 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1205 | #endif |
| 1206 | } else { |
| 1207 | BUG(); |
| 1208 | } |
| 1209 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1210 | BUG_ON(num_refs < refs_to_drop); |
| 1211 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1212 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1213 | if (num_refs == 0) { |
| 1214 | ret = btrfs_del_item(trans, root, path); |
| 1215 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1216 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1217 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1218 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1219 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1220 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1221 | else { |
| 1222 | struct btrfs_extent_ref_v0 *ref0; |
| 1223 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1224 | struct btrfs_extent_ref_v0); |
| 1225 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1226 | } |
| 1227 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1228 | btrfs_mark_buffer_dirty(leaf); |
| 1229 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1230 | return ret; |
| 1231 | } |
| 1232 | |
| 1233 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1234 | struct btrfs_path *path, |
| 1235 | struct btrfs_extent_inline_ref *iref) |
| 1236 | { |
| 1237 | struct btrfs_key key; |
| 1238 | struct extent_buffer *leaf; |
| 1239 | struct btrfs_extent_data_ref *ref1; |
| 1240 | struct btrfs_shared_data_ref *ref2; |
| 1241 | u32 num_refs = 0; |
| 1242 | |
| 1243 | leaf = path->nodes[0]; |
| 1244 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1245 | if (iref) { |
| 1246 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1247 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1248 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1249 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1250 | } else { |
| 1251 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1252 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1253 | } |
| 1254 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1255 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1256 | struct btrfs_extent_data_ref); |
| 1257 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1258 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1259 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1260 | struct btrfs_shared_data_ref); |
| 1261 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1262 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1263 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1264 | struct btrfs_extent_ref_v0 *ref0; |
| 1265 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1266 | struct btrfs_extent_ref_v0); |
| 1267 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1268 | #endif |
| 1269 | } else { |
| 1270 | WARN_ON(1); |
| 1271 | } |
| 1272 | return num_refs; |
| 1273 | } |
| 1274 | |
| 1275 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1276 | struct btrfs_root *root, |
| 1277 | struct btrfs_path *path, |
| 1278 | u64 bytenr, u64 parent, |
| 1279 | u64 root_objectid) |
| 1280 | { |
| 1281 | struct btrfs_key key; |
| 1282 | int ret; |
| 1283 | |
| 1284 | key.objectid = bytenr; |
| 1285 | if (parent) { |
| 1286 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1287 | key.offset = parent; |
| 1288 | } else { |
| 1289 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1290 | key.offset = root_objectid; |
| 1291 | } |
| 1292 | |
| 1293 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1294 | if (ret > 0) |
| 1295 | ret = -ENOENT; |
| 1296 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1297 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1298 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1299 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1300 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1301 | if (ret > 0) |
| 1302 | ret = -ENOENT; |
| 1303 | } |
| 1304 | #endif |
| 1305 | return ret; |
| 1306 | } |
| 1307 | |
| 1308 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1309 | struct btrfs_root *root, |
| 1310 | struct btrfs_path *path, |
| 1311 | u64 bytenr, u64 parent, |
| 1312 | u64 root_objectid) |
| 1313 | { |
| 1314 | struct btrfs_key key; |
| 1315 | int ret; |
| 1316 | |
| 1317 | key.objectid = bytenr; |
| 1318 | if (parent) { |
| 1319 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1320 | key.offset = parent; |
| 1321 | } else { |
| 1322 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1323 | key.offset = root_objectid; |
| 1324 | } |
| 1325 | |
| 1326 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1327 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1328 | return ret; |
| 1329 | } |
| 1330 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1331 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1332 | { |
| 1333 | int type; |
| 1334 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1335 | if (parent > 0) |
| 1336 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1337 | else |
| 1338 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1339 | } else { |
| 1340 | if (parent > 0) |
| 1341 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1342 | else |
| 1343 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1344 | } |
| 1345 | return type; |
| 1346 | } |
| 1347 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1348 | static int find_next_key(struct btrfs_path *path, int level, |
| 1349 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1350 | |
| 1351 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1352 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1353 | if (!path->nodes[level]) |
| 1354 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1355 | if (path->slots[level] + 1 >= |
| 1356 | btrfs_header_nritems(path->nodes[level])) |
| 1357 | continue; |
| 1358 | if (level == 0) |
| 1359 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1360 | path->slots[level] + 1); |
| 1361 | else |
| 1362 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1363 | path->slots[level] + 1); |
| 1364 | return 0; |
| 1365 | } |
| 1366 | return 1; |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1371 | * to the address of inline back ref, and 0 is returned. |
| 1372 | * |
| 1373 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1374 | * should be inserted, and -ENOENT is returned. |
| 1375 | * |
| 1376 | * if insert is true and there are too many inline back refs, the path |
| 1377 | * points to the extent item, and -EAGAIN is returned. |
| 1378 | * |
| 1379 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1380 | * items in the tree are ordered. |
| 1381 | */ |
| 1382 | static noinline_for_stack |
| 1383 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1384 | struct btrfs_root *root, |
| 1385 | struct btrfs_path *path, |
| 1386 | struct btrfs_extent_inline_ref **ref_ret, |
| 1387 | u64 bytenr, u64 num_bytes, |
| 1388 | u64 parent, u64 root_objectid, |
| 1389 | u64 owner, u64 offset, int insert) |
| 1390 | { |
| 1391 | struct btrfs_key key; |
| 1392 | struct extent_buffer *leaf; |
| 1393 | struct btrfs_extent_item *ei; |
| 1394 | struct btrfs_extent_inline_ref *iref; |
| 1395 | u64 flags; |
| 1396 | u64 item_size; |
| 1397 | unsigned long ptr; |
| 1398 | unsigned long end; |
| 1399 | int extra_size; |
| 1400 | int type; |
| 1401 | int want; |
| 1402 | int ret; |
| 1403 | int err = 0; |
| 1404 | |
| 1405 | key.objectid = bytenr; |
| 1406 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1407 | key.offset = num_bytes; |
| 1408 | |
| 1409 | want = extent_ref_type(parent, owner); |
| 1410 | if (insert) { |
| 1411 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1412 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1413 | } else |
| 1414 | extra_size = -1; |
| 1415 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1416 | if (ret < 0) { |
| 1417 | err = ret; |
| 1418 | goto out; |
| 1419 | } |
| 1420 | BUG_ON(ret); |
| 1421 | |
| 1422 | leaf = path->nodes[0]; |
| 1423 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1424 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1425 | if (item_size < sizeof(*ei)) { |
| 1426 | if (!insert) { |
| 1427 | err = -ENOENT; |
| 1428 | goto out; |
| 1429 | } |
| 1430 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1431 | extra_size); |
| 1432 | if (ret < 0) { |
| 1433 | err = ret; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | leaf = path->nodes[0]; |
| 1437 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1438 | } |
| 1439 | #endif |
| 1440 | BUG_ON(item_size < sizeof(*ei)); |
| 1441 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1442 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1443 | flags = btrfs_extent_flags(leaf, ei); |
| 1444 | |
| 1445 | ptr = (unsigned long)(ei + 1); |
| 1446 | end = (unsigned long)ei + item_size; |
| 1447 | |
| 1448 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1449 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1450 | BUG_ON(ptr > end); |
| 1451 | } else { |
| 1452 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1453 | } |
| 1454 | |
| 1455 | err = -ENOENT; |
| 1456 | while (1) { |
| 1457 | if (ptr >= end) { |
| 1458 | WARN_ON(ptr > end); |
| 1459 | break; |
| 1460 | } |
| 1461 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1462 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1463 | if (want < type) |
| 1464 | break; |
| 1465 | if (want > type) { |
| 1466 | ptr += btrfs_extent_inline_ref_size(type); |
| 1467 | continue; |
| 1468 | } |
| 1469 | |
| 1470 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1471 | struct btrfs_extent_data_ref *dref; |
| 1472 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1473 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1474 | owner, offset)) { |
| 1475 | err = 0; |
| 1476 | break; |
| 1477 | } |
| 1478 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1479 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1480 | break; |
| 1481 | } else { |
| 1482 | u64 ref_offset; |
| 1483 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1484 | if (parent > 0) { |
| 1485 | if (parent == ref_offset) { |
| 1486 | err = 0; |
| 1487 | break; |
| 1488 | } |
| 1489 | if (ref_offset < parent) |
| 1490 | break; |
| 1491 | } else { |
| 1492 | if (root_objectid == ref_offset) { |
| 1493 | err = 0; |
| 1494 | break; |
| 1495 | } |
| 1496 | if (ref_offset < root_objectid) |
| 1497 | break; |
| 1498 | } |
| 1499 | } |
| 1500 | ptr += btrfs_extent_inline_ref_size(type); |
| 1501 | } |
| 1502 | if (err == -ENOENT && insert) { |
| 1503 | if (item_size + extra_size >= |
| 1504 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1505 | err = -EAGAIN; |
| 1506 | goto out; |
| 1507 | } |
| 1508 | /* |
| 1509 | * To add new inline back ref, we have to make sure |
| 1510 | * there is no corresponding back ref item. |
| 1511 | * For simplicity, we just do not add new inline back |
| 1512 | * ref if there is any kind of item for this block |
| 1513 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1514 | if (find_next_key(path, 0, &key) == 0 && |
| 1515 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1516 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1517 | err = -EAGAIN; |
| 1518 | goto out; |
| 1519 | } |
| 1520 | } |
| 1521 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1522 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1523 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1524 | path->keep_locks = 0; |
| 1525 | btrfs_unlock_up_safe(path, 1); |
| 1526 | } |
| 1527 | return err; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * helper to add new inline back ref |
| 1532 | */ |
| 1533 | static noinline_for_stack |
| 1534 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1535 | struct btrfs_root *root, |
| 1536 | struct btrfs_path *path, |
| 1537 | struct btrfs_extent_inline_ref *iref, |
| 1538 | u64 parent, u64 root_objectid, |
| 1539 | u64 owner, u64 offset, int refs_to_add, |
| 1540 | struct btrfs_delayed_extent_op *extent_op) |
| 1541 | { |
| 1542 | struct extent_buffer *leaf; |
| 1543 | struct btrfs_extent_item *ei; |
| 1544 | unsigned long ptr; |
| 1545 | unsigned long end; |
| 1546 | unsigned long item_offset; |
| 1547 | u64 refs; |
| 1548 | int size; |
| 1549 | int type; |
| 1550 | int ret; |
| 1551 | |
| 1552 | leaf = path->nodes[0]; |
| 1553 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1554 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1555 | |
| 1556 | type = extent_ref_type(parent, owner); |
| 1557 | size = btrfs_extent_inline_ref_size(type); |
| 1558 | |
| 1559 | ret = btrfs_extend_item(trans, root, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1560 | |
| 1561 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1562 | refs = btrfs_extent_refs(leaf, ei); |
| 1563 | refs += refs_to_add; |
| 1564 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1565 | if (extent_op) |
| 1566 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1567 | |
| 1568 | ptr = (unsigned long)ei + item_offset; |
| 1569 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1570 | if (ptr < end - size) |
| 1571 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1572 | end - size - ptr); |
| 1573 | |
| 1574 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1575 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1576 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1577 | struct btrfs_extent_data_ref *dref; |
| 1578 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1579 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1580 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1581 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1582 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1583 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1584 | struct btrfs_shared_data_ref *sref; |
| 1585 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1586 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1587 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1588 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1589 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1590 | } else { |
| 1591 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1592 | } |
| 1593 | btrfs_mark_buffer_dirty(leaf); |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1598 | struct btrfs_root *root, |
| 1599 | struct btrfs_path *path, |
| 1600 | struct btrfs_extent_inline_ref **ref_ret, |
| 1601 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1602 | u64 root_objectid, u64 owner, u64 offset) |
| 1603 | { |
| 1604 | int ret; |
| 1605 | |
| 1606 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1607 | bytenr, num_bytes, parent, |
| 1608 | root_objectid, owner, offset, 0); |
| 1609 | if (ret != -ENOENT) |
| 1610 | return ret; |
| 1611 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1612 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1613 | *ref_ret = NULL; |
| 1614 | |
| 1615 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1616 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1617 | root_objectid); |
| 1618 | } else { |
| 1619 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1620 | root_objectid, owner, offset); |
| 1621 | } |
| 1622 | return ret; |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | * helper to update/remove inline back ref |
| 1627 | */ |
| 1628 | static noinline_for_stack |
| 1629 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1630 | struct btrfs_root *root, |
| 1631 | struct btrfs_path *path, |
| 1632 | struct btrfs_extent_inline_ref *iref, |
| 1633 | int refs_to_mod, |
| 1634 | struct btrfs_delayed_extent_op *extent_op) |
| 1635 | { |
| 1636 | struct extent_buffer *leaf; |
| 1637 | struct btrfs_extent_item *ei; |
| 1638 | struct btrfs_extent_data_ref *dref = NULL; |
| 1639 | struct btrfs_shared_data_ref *sref = NULL; |
| 1640 | unsigned long ptr; |
| 1641 | unsigned long end; |
| 1642 | u32 item_size; |
| 1643 | int size; |
| 1644 | int type; |
| 1645 | int ret; |
| 1646 | u64 refs; |
| 1647 | |
| 1648 | leaf = path->nodes[0]; |
| 1649 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1650 | refs = btrfs_extent_refs(leaf, ei); |
| 1651 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1652 | refs += refs_to_mod; |
| 1653 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1654 | if (extent_op) |
| 1655 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1656 | |
| 1657 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1658 | |
| 1659 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1660 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1661 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1662 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1663 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1664 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1665 | } else { |
| 1666 | refs = 1; |
| 1667 | BUG_ON(refs_to_mod != -1); |
| 1668 | } |
| 1669 | |
| 1670 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1671 | refs += refs_to_mod; |
| 1672 | |
| 1673 | if (refs > 0) { |
| 1674 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1675 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1676 | else |
| 1677 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1678 | } else { |
| 1679 | size = btrfs_extent_inline_ref_size(type); |
| 1680 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1681 | ptr = (unsigned long)iref; |
| 1682 | end = (unsigned long)ei + item_size; |
| 1683 | if (ptr + size < end) |
| 1684 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1685 | end - ptr - size); |
| 1686 | item_size -= size; |
| 1687 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1688 | } |
| 1689 | btrfs_mark_buffer_dirty(leaf); |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | static noinline_for_stack |
| 1694 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1695 | struct btrfs_root *root, |
| 1696 | struct btrfs_path *path, |
| 1697 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1698 | u64 root_objectid, u64 owner, |
| 1699 | u64 offset, int refs_to_add, |
| 1700 | struct btrfs_delayed_extent_op *extent_op) |
| 1701 | { |
| 1702 | struct btrfs_extent_inline_ref *iref; |
| 1703 | int ret; |
| 1704 | |
| 1705 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1706 | bytenr, num_bytes, parent, |
| 1707 | root_objectid, owner, offset, 1); |
| 1708 | if (ret == 0) { |
| 1709 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1710 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1711 | refs_to_add, extent_op); |
| 1712 | } else if (ret == -ENOENT) { |
| 1713 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1714 | parent, root_objectid, |
| 1715 | owner, offset, refs_to_add, |
| 1716 | extent_op); |
| 1717 | } |
| 1718 | return ret; |
| 1719 | } |
| 1720 | |
| 1721 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1722 | struct btrfs_root *root, |
| 1723 | struct btrfs_path *path, |
| 1724 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1725 | u64 owner, u64 offset, int refs_to_add) |
| 1726 | { |
| 1727 | int ret; |
| 1728 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1729 | BUG_ON(refs_to_add != 1); |
| 1730 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1731 | parent, root_objectid); |
| 1732 | } else { |
| 1733 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1734 | parent, root_objectid, |
| 1735 | owner, offset, refs_to_add); |
| 1736 | } |
| 1737 | return ret; |
| 1738 | } |
| 1739 | |
| 1740 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1741 | struct btrfs_root *root, |
| 1742 | struct btrfs_path *path, |
| 1743 | struct btrfs_extent_inline_ref *iref, |
| 1744 | int refs_to_drop, int is_data) |
| 1745 | { |
| 1746 | int ret; |
| 1747 | |
| 1748 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1749 | if (iref) { |
| 1750 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1751 | -refs_to_drop, NULL); |
| 1752 | } else if (is_data) { |
| 1753 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1754 | } else { |
| 1755 | ret = btrfs_del_item(trans, root, path); |
| 1756 | } |
| 1757 | return ret; |
| 1758 | } |
| 1759 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1760 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1761 | u64 start, u64 len) |
| 1762 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1763 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1764 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1765 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1766 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1767 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1768 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1769 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1770 | u64 discarded_bytes = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1771 | struct btrfs_multi_bio *multi = NULL; |
| 1772 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1773 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1774 | /* Tell the block device(s) that the sectors can be discarded */ |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1775 | ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, |
| 1776 | bytenr, &num_bytes, &multi, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1777 | if (!ret) { |
| 1778 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1779 | int i; |
| 1780 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1781 | |
| 1782 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1783 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1784 | stripe->physical, |
| 1785 | stripe->length); |
| 1786 | if (!ret) |
| 1787 | discarded_bytes += stripe->length; |
| 1788 | else if (ret != -EOPNOTSUPP) |
| 1789 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1790 | } |
| 1791 | kfree(multi); |
| 1792 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1793 | if (discarded_bytes && ret == -EOPNOTSUPP) |
| 1794 | ret = 0; |
| 1795 | |
| 1796 | if (actual_bytes) |
| 1797 | *actual_bytes = discarded_bytes; |
| 1798 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1799 | |
| 1800 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1801 | } |
| 1802 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1803 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1804 | struct btrfs_root *root, |
| 1805 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1806 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1807 | { |
| 1808 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1809 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1810 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1811 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1812 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1813 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1814 | parent, root_objectid, (int)owner, |
| 1815 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1816 | } else { |
| 1817 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1818 | parent, root_objectid, owner, offset, |
| 1819 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1820 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1821 | return ret; |
| 1822 | } |
| 1823 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1824 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1825 | struct btrfs_root *root, |
| 1826 | u64 bytenr, u64 num_bytes, |
| 1827 | u64 parent, u64 root_objectid, |
| 1828 | u64 owner, u64 offset, int refs_to_add, |
| 1829 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1830 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1831 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1832 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1833 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1834 | u64 refs; |
| 1835 | int ret; |
| 1836 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1837 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1838 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1839 | if (!path) |
| 1840 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1841 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1842 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1843 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1844 | /* this will setup the path even if it fails to insert the back ref */ |
| 1845 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1846 | path, bytenr, num_bytes, parent, |
| 1847 | root_objectid, owner, offset, |
| 1848 | refs_to_add, extent_op); |
| 1849 | if (ret == 0) |
| 1850 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1851 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1852 | if (ret != -EAGAIN) { |
| 1853 | err = ret; |
| 1854 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1855 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1856 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1857 | leaf = path->nodes[0]; |
| 1858 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1859 | refs = btrfs_extent_refs(leaf, item); |
| 1860 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1861 | if (extent_op) |
| 1862 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1863 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1864 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1865 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1866 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1867 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1868 | path->leave_spinning = 1; |
| 1869 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1870 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1871 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1872 | path, bytenr, parent, root_objectid, |
| 1873 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1874 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1875 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1876 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1877 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1878 | } |
| 1879 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1880 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1881 | struct btrfs_root *root, |
| 1882 | struct btrfs_delayed_ref_node *node, |
| 1883 | struct btrfs_delayed_extent_op *extent_op, |
| 1884 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1885 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1886 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1887 | struct btrfs_delayed_data_ref *ref; |
| 1888 | struct btrfs_key ins; |
| 1889 | u64 parent = 0; |
| 1890 | u64 ref_root = 0; |
| 1891 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1892 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1893 | ins.objectid = node->bytenr; |
| 1894 | ins.offset = node->num_bytes; |
| 1895 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1896 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1897 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1898 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1899 | parent = ref->parent; |
| 1900 | else |
| 1901 | ref_root = ref->root; |
| 1902 | |
| 1903 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1904 | if (extent_op) { |
| 1905 | BUG_ON(extent_op->update_key); |
| 1906 | flags |= extent_op->flags_to_set; |
| 1907 | } |
| 1908 | ret = alloc_reserved_file_extent(trans, root, |
| 1909 | parent, ref_root, flags, |
| 1910 | ref->objectid, ref->offset, |
| 1911 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1912 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1913 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1914 | node->num_bytes, parent, |
| 1915 | ref_root, ref->objectid, |
| 1916 | ref->offset, node->ref_mod, |
| 1917 | extent_op); |
| 1918 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1919 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1920 | node->num_bytes, parent, |
| 1921 | ref_root, ref->objectid, |
| 1922 | ref->offset, node->ref_mod, |
| 1923 | extent_op); |
| 1924 | } else { |
| 1925 | BUG(); |
| 1926 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1927 | return ret; |
| 1928 | } |
| 1929 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1930 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1931 | struct extent_buffer *leaf, |
| 1932 | struct btrfs_extent_item *ei) |
| 1933 | { |
| 1934 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1935 | if (extent_op->update_flags) { |
| 1936 | flags |= extent_op->flags_to_set; |
| 1937 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1938 | } |
| 1939 | |
| 1940 | if (extent_op->update_key) { |
| 1941 | struct btrfs_tree_block_info *bi; |
| 1942 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1943 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1944 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1949 | struct btrfs_root *root, |
| 1950 | struct btrfs_delayed_ref_node *node, |
| 1951 | struct btrfs_delayed_extent_op *extent_op) |
| 1952 | { |
| 1953 | struct btrfs_key key; |
| 1954 | struct btrfs_path *path; |
| 1955 | struct btrfs_extent_item *ei; |
| 1956 | struct extent_buffer *leaf; |
| 1957 | u32 item_size; |
| 1958 | int ret; |
| 1959 | int err = 0; |
| 1960 | |
| 1961 | path = btrfs_alloc_path(); |
| 1962 | if (!path) |
| 1963 | return -ENOMEM; |
| 1964 | |
| 1965 | key.objectid = node->bytenr; |
| 1966 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1967 | key.offset = node->num_bytes; |
| 1968 | |
| 1969 | path->reada = 1; |
| 1970 | path->leave_spinning = 1; |
| 1971 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1972 | path, 0, 1); |
| 1973 | if (ret < 0) { |
| 1974 | err = ret; |
| 1975 | goto out; |
| 1976 | } |
| 1977 | if (ret > 0) { |
| 1978 | err = -EIO; |
| 1979 | goto out; |
| 1980 | } |
| 1981 | |
| 1982 | leaf = path->nodes[0]; |
| 1983 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1984 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1985 | if (item_size < sizeof(*ei)) { |
| 1986 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1987 | path, (u64)-1, 0); |
| 1988 | if (ret < 0) { |
| 1989 | err = ret; |
| 1990 | goto out; |
| 1991 | } |
| 1992 | leaf = path->nodes[0]; |
| 1993 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1994 | } |
| 1995 | #endif |
| 1996 | BUG_ON(item_size < sizeof(*ei)); |
| 1997 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1998 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1999 | |
| 2000 | btrfs_mark_buffer_dirty(leaf); |
| 2001 | out: |
| 2002 | btrfs_free_path(path); |
| 2003 | return err; |
| 2004 | } |
| 2005 | |
| 2006 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2007 | struct btrfs_root *root, |
| 2008 | struct btrfs_delayed_ref_node *node, |
| 2009 | struct btrfs_delayed_extent_op *extent_op, |
| 2010 | int insert_reserved) |
| 2011 | { |
| 2012 | int ret = 0; |
| 2013 | struct btrfs_delayed_tree_ref *ref; |
| 2014 | struct btrfs_key ins; |
| 2015 | u64 parent = 0; |
| 2016 | u64 ref_root = 0; |
| 2017 | |
| 2018 | ins.objectid = node->bytenr; |
| 2019 | ins.offset = node->num_bytes; |
| 2020 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2021 | |
| 2022 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 2023 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2024 | parent = ref->parent; |
| 2025 | else |
| 2026 | ref_root = ref->root; |
| 2027 | |
| 2028 | BUG_ON(node->ref_mod != 1); |
| 2029 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 2030 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 2031 | !extent_op->update_key); |
| 2032 | ret = alloc_reserved_tree_block(trans, root, |
| 2033 | parent, ref_root, |
| 2034 | extent_op->flags_to_set, |
| 2035 | &extent_op->key, |
| 2036 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2037 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2038 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2039 | node->num_bytes, parent, ref_root, |
| 2040 | ref->level, 0, 1, extent_op); |
| 2041 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2042 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2043 | node->num_bytes, parent, ref_root, |
| 2044 | ref->level, 0, 1, extent_op); |
| 2045 | } else { |
| 2046 | BUG(); |
| 2047 | } |
| 2048 | return ret; |
| 2049 | } |
| 2050 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2051 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2052 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2053 | struct btrfs_root *root, |
| 2054 | struct btrfs_delayed_ref_node *node, |
| 2055 | struct btrfs_delayed_extent_op *extent_op, |
| 2056 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2057 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2058 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2059 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2060 | struct btrfs_delayed_ref_head *head; |
| 2061 | /* |
| 2062 | * we've hit the end of the chain and we were supposed |
| 2063 | * to insert this extent into the tree. But, it got |
| 2064 | * deleted before we ever needed to insert it, so all |
| 2065 | * we have to do is clean up the accounting |
| 2066 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2067 | BUG_ON(extent_op); |
| 2068 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2069 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2070 | btrfs_pin_extent(root, node->bytenr, |
| 2071 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2072 | if (head->is_data) { |
| 2073 | ret = btrfs_del_csums(trans, root, |
| 2074 | node->bytenr, |
| 2075 | node->num_bytes); |
| 2076 | BUG_ON(ret); |
| 2077 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2078 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2079 | mutex_unlock(&head->mutex); |
| 2080 | return 0; |
| 2081 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2082 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2083 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2084 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2085 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2086 | insert_reserved); |
| 2087 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2088 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2089 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2090 | insert_reserved); |
| 2091 | else |
| 2092 | BUG(); |
| 2093 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2094 | } |
| 2095 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2096 | static noinline struct btrfs_delayed_ref_node * |
| 2097 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2098 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2099 | struct rb_node *node; |
| 2100 | struct btrfs_delayed_ref_node *ref; |
| 2101 | int action = BTRFS_ADD_DELAYED_REF; |
| 2102 | again: |
| 2103 | /* |
| 2104 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2105 | * this prevents ref count from going down to zero when |
| 2106 | * there still are pending delayed ref. |
| 2107 | */ |
| 2108 | node = rb_prev(&head->node.rb_node); |
| 2109 | while (1) { |
| 2110 | if (!node) |
| 2111 | break; |
| 2112 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2113 | rb_node); |
| 2114 | if (ref->bytenr != head->node.bytenr) |
| 2115 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2116 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2117 | return ref; |
| 2118 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2119 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2120 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2121 | action = BTRFS_DROP_DELAYED_REF; |
| 2122 | goto again; |
| 2123 | } |
| 2124 | return NULL; |
| 2125 | } |
| 2126 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2127 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2128 | struct btrfs_root *root, |
| 2129 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2130 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2131 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2132 | struct btrfs_delayed_ref_node *ref; |
| 2133 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2134 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2135 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2136 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2137 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2138 | |
| 2139 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2140 | while (1) { |
| 2141 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2142 | /* pick a new head ref from the cluster list */ |
| 2143 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2144 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2145 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2146 | locked_ref = list_entry(cluster->next, |
| 2147 | struct btrfs_delayed_ref_head, cluster); |
| 2148 | |
| 2149 | /* grab the lock that says we are going to process |
| 2150 | * all the refs for this head */ |
| 2151 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2152 | |
| 2153 | /* |
| 2154 | * we may have dropped the spin lock to get the head |
| 2155 | * mutex lock, and that might have given someone else |
| 2156 | * time to free the head. If that's true, it has been |
| 2157 | * removed from our list and we can move on. |
| 2158 | */ |
| 2159 | if (ret == -EAGAIN) { |
| 2160 | locked_ref = NULL; |
| 2161 | count++; |
| 2162 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | /* |
| 2167 | * record the must insert reserved flag before we |
| 2168 | * drop the spin lock. |
| 2169 | */ |
| 2170 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2171 | locked_ref->must_insert_reserved = 0; |
| 2172 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2173 | extent_op = locked_ref->extent_op; |
| 2174 | locked_ref->extent_op = NULL; |
| 2175 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2176 | /* |
| 2177 | * locked_ref is the head node, so we have to go one |
| 2178 | * node back for any delayed ref updates |
| 2179 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2180 | ref = select_delayed_ref(locked_ref); |
| 2181 | if (!ref) { |
| 2182 | /* All delayed refs have been processed, Go ahead |
| 2183 | * and send the head node to run_one_delayed_ref, |
| 2184 | * so that any accounting fixes can happen |
| 2185 | */ |
| 2186 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2187 | |
| 2188 | if (extent_op && must_insert_reserved) { |
| 2189 | kfree(extent_op); |
| 2190 | extent_op = NULL; |
| 2191 | } |
| 2192 | |
| 2193 | if (extent_op) { |
| 2194 | spin_unlock(&delayed_refs->lock); |
| 2195 | |
| 2196 | ret = run_delayed_extent_op(trans, root, |
| 2197 | ref, extent_op); |
| 2198 | BUG_ON(ret); |
| 2199 | kfree(extent_op); |
| 2200 | |
| 2201 | cond_resched(); |
| 2202 | spin_lock(&delayed_refs->lock); |
| 2203 | continue; |
| 2204 | } |
| 2205 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2206 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2207 | locked_ref = NULL; |
| 2208 | } |
| 2209 | |
| 2210 | ref->in_tree = 0; |
| 2211 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2212 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2213 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2214 | spin_unlock(&delayed_refs->lock); |
| 2215 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2216 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2217 | must_insert_reserved); |
| 2218 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2219 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2220 | btrfs_put_delayed_ref(ref); |
| 2221 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2222 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2223 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2224 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2225 | spin_lock(&delayed_refs->lock); |
| 2226 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2227 | return count; |
| 2228 | } |
| 2229 | |
| 2230 | /* |
| 2231 | * this starts processing the delayed reference count updates and |
| 2232 | * extent insertions we have queued up so far. count can be |
| 2233 | * 0, which means to process everything in the tree at the start |
| 2234 | * of the run (but not newly added entries), or it can be some target |
| 2235 | * number you'd like to process. |
| 2236 | */ |
| 2237 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2238 | struct btrfs_root *root, unsigned long count) |
| 2239 | { |
| 2240 | struct rb_node *node; |
| 2241 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2242 | struct btrfs_delayed_ref_node *ref; |
| 2243 | struct list_head cluster; |
| 2244 | int ret; |
| 2245 | int run_all = count == (unsigned long)-1; |
| 2246 | int run_most = 0; |
| 2247 | |
| 2248 | if (root == root->fs_info->extent_root) |
| 2249 | root = root->fs_info->tree_root; |
| 2250 | |
| 2251 | delayed_refs = &trans->transaction->delayed_refs; |
| 2252 | INIT_LIST_HEAD(&cluster); |
| 2253 | again: |
| 2254 | spin_lock(&delayed_refs->lock); |
| 2255 | if (count == 0) { |
| 2256 | count = delayed_refs->num_entries * 2; |
| 2257 | run_most = 1; |
| 2258 | } |
| 2259 | while (1) { |
| 2260 | if (!(run_all || run_most) && |
| 2261 | delayed_refs->num_heads_ready < 64) |
| 2262 | break; |
| 2263 | |
| 2264 | /* |
| 2265 | * go find something we can process in the rbtree. We start at |
| 2266 | * the beginning of the tree, and then build a cluster |
| 2267 | * of refs to process starting at the first one we are able to |
| 2268 | * lock |
| 2269 | */ |
| 2270 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2271 | delayed_refs->run_delayed_start); |
| 2272 | if (ret) |
| 2273 | break; |
| 2274 | |
| 2275 | ret = run_clustered_refs(trans, root, &cluster); |
| 2276 | BUG_ON(ret < 0); |
| 2277 | |
| 2278 | count -= min_t(unsigned long, ret, count); |
| 2279 | |
| 2280 | if (count == 0) |
| 2281 | break; |
| 2282 | } |
| 2283 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2284 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2285 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2286 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2287 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2288 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2289 | |
| 2290 | while (node) { |
| 2291 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2292 | rb_node); |
| 2293 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2294 | struct btrfs_delayed_ref_head *head; |
| 2295 | |
| 2296 | head = btrfs_delayed_node_to_head(ref); |
| 2297 | atomic_inc(&ref->refs); |
| 2298 | |
| 2299 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2300 | /* |
| 2301 | * Mutex was contended, block until it's |
| 2302 | * released and try again |
| 2303 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2304 | mutex_lock(&head->mutex); |
| 2305 | mutex_unlock(&head->mutex); |
| 2306 | |
| 2307 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2308 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2309 | goto again; |
| 2310 | } |
| 2311 | node = rb_next(node); |
| 2312 | } |
| 2313 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2314 | schedule_timeout(1); |
| 2315 | goto again; |
| 2316 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2317 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2318 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2319 | return 0; |
| 2320 | } |
| 2321 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2322 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2323 | struct btrfs_root *root, |
| 2324 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2325 | int is_data) |
| 2326 | { |
| 2327 | struct btrfs_delayed_extent_op *extent_op; |
| 2328 | int ret; |
| 2329 | |
| 2330 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2331 | if (!extent_op) |
| 2332 | return -ENOMEM; |
| 2333 | |
| 2334 | extent_op->flags_to_set = flags; |
| 2335 | extent_op->update_flags = 1; |
| 2336 | extent_op->update_key = 0; |
| 2337 | extent_op->is_data = is_data ? 1 : 0; |
| 2338 | |
| 2339 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2340 | if (ret) |
| 2341 | kfree(extent_op); |
| 2342 | return ret; |
| 2343 | } |
| 2344 | |
| 2345 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2346 | struct btrfs_root *root, |
| 2347 | struct btrfs_path *path, |
| 2348 | u64 objectid, u64 offset, u64 bytenr) |
| 2349 | { |
| 2350 | struct btrfs_delayed_ref_head *head; |
| 2351 | struct btrfs_delayed_ref_node *ref; |
| 2352 | struct btrfs_delayed_data_ref *data_ref; |
| 2353 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2354 | struct rb_node *node; |
| 2355 | int ret = 0; |
| 2356 | |
| 2357 | ret = -ENOENT; |
| 2358 | delayed_refs = &trans->transaction->delayed_refs; |
| 2359 | spin_lock(&delayed_refs->lock); |
| 2360 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2361 | if (!head) |
| 2362 | goto out; |
| 2363 | |
| 2364 | if (!mutex_trylock(&head->mutex)) { |
| 2365 | atomic_inc(&head->node.refs); |
| 2366 | spin_unlock(&delayed_refs->lock); |
| 2367 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2368 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2369 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2370 | /* |
| 2371 | * Mutex was contended, block until it's released and let |
| 2372 | * caller try again |
| 2373 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2374 | mutex_lock(&head->mutex); |
| 2375 | mutex_unlock(&head->mutex); |
| 2376 | btrfs_put_delayed_ref(&head->node); |
| 2377 | return -EAGAIN; |
| 2378 | } |
| 2379 | |
| 2380 | node = rb_prev(&head->node.rb_node); |
| 2381 | if (!node) |
| 2382 | goto out_unlock; |
| 2383 | |
| 2384 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2385 | |
| 2386 | if (ref->bytenr != bytenr) |
| 2387 | goto out_unlock; |
| 2388 | |
| 2389 | ret = 1; |
| 2390 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2391 | goto out_unlock; |
| 2392 | |
| 2393 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2394 | |
| 2395 | node = rb_prev(node); |
| 2396 | if (node) { |
| 2397 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2398 | if (ref->bytenr == bytenr) |
| 2399 | goto out_unlock; |
| 2400 | } |
| 2401 | |
| 2402 | if (data_ref->root != root->root_key.objectid || |
| 2403 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2404 | goto out_unlock; |
| 2405 | |
| 2406 | ret = 0; |
| 2407 | out_unlock: |
| 2408 | mutex_unlock(&head->mutex); |
| 2409 | out: |
| 2410 | spin_unlock(&delayed_refs->lock); |
| 2411 | return ret; |
| 2412 | } |
| 2413 | |
| 2414 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2415 | struct btrfs_root *root, |
| 2416 | struct btrfs_path *path, |
| 2417 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2418 | { |
| 2419 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2420 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2421 | struct btrfs_extent_data_ref *ref; |
| 2422 | struct btrfs_extent_inline_ref *iref; |
| 2423 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2424 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2425 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2426 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2427 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2428 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2429 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2430 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2431 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2432 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2433 | if (ret < 0) |
| 2434 | goto out; |
| 2435 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2436 | |
| 2437 | ret = -ENOENT; |
| 2438 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2439 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2440 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2441 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2442 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2443 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2444 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2445 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2446 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2447 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2448 | ret = 1; |
| 2449 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2450 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2451 | if (item_size < sizeof(*ei)) { |
| 2452 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2453 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2454 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2455 | #endif |
| 2456 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2457 | |
| 2458 | if (item_size != sizeof(*ei) + |
| 2459 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2460 | goto out; |
| 2461 | |
| 2462 | if (btrfs_extent_generation(leaf, ei) <= |
| 2463 | btrfs_root_last_snapshot(&root->root_item)) |
| 2464 | goto out; |
| 2465 | |
| 2466 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2467 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2468 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2469 | goto out; |
| 2470 | |
| 2471 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2472 | if (btrfs_extent_refs(leaf, ei) != |
| 2473 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2474 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2475 | root->root_key.objectid || |
| 2476 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2477 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2478 | goto out; |
| 2479 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2480 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2481 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2482 | return ret; |
| 2483 | } |
| 2484 | |
| 2485 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2486 | struct btrfs_root *root, |
| 2487 | u64 objectid, u64 offset, u64 bytenr) |
| 2488 | { |
| 2489 | struct btrfs_path *path; |
| 2490 | int ret; |
| 2491 | int ret2; |
| 2492 | |
| 2493 | path = btrfs_alloc_path(); |
| 2494 | if (!path) |
| 2495 | return -ENOENT; |
| 2496 | |
| 2497 | do { |
| 2498 | ret = check_committed_ref(trans, root, path, objectid, |
| 2499 | offset, bytenr); |
| 2500 | if (ret && ret != -ENOENT) |
| 2501 | goto out; |
| 2502 | |
| 2503 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2504 | offset, bytenr); |
| 2505 | } while (ret2 == -EAGAIN); |
| 2506 | |
| 2507 | if (ret2 && ret2 != -ENOENT) { |
| 2508 | ret = ret2; |
| 2509 | goto out; |
| 2510 | } |
| 2511 | |
| 2512 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2513 | ret = 0; |
| 2514 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2515 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2516 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2517 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2518 | return ret; |
| 2519 | } |
| 2520 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2521 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2522 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2523 | struct extent_buffer *buf, |
| 2524 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2525 | { |
| 2526 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2527 | u64 num_bytes; |
| 2528 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2529 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2530 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2531 | struct btrfs_key key; |
| 2532 | struct btrfs_file_extent_item *fi; |
| 2533 | int i; |
| 2534 | int level; |
| 2535 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2536 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2537 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2538 | |
| 2539 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2540 | nritems = btrfs_header_nritems(buf); |
| 2541 | level = btrfs_header_level(buf); |
| 2542 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2543 | if (!root->ref_cows && level == 0) |
| 2544 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2545 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2546 | if (inc) |
| 2547 | process_func = btrfs_inc_extent_ref; |
| 2548 | else |
| 2549 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2550 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2551 | if (full_backref) |
| 2552 | parent = buf->start; |
| 2553 | else |
| 2554 | parent = 0; |
| 2555 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2556 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2557 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2558 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2559 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2560 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2561 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2562 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2563 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2564 | BTRFS_FILE_EXTENT_INLINE) |
| 2565 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2566 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2567 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2568 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2569 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2570 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2571 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2572 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2573 | parent, ref_root, key.objectid, |
| 2574 | key.offset); |
| 2575 | if (ret) |
| 2576 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2577 | } else { |
| 2578 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2579 | num_bytes = btrfs_level_size(root, level - 1); |
| 2580 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2581 | parent, ref_root, level - 1, 0); |
| 2582 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2583 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2584 | } |
| 2585 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2586 | return 0; |
| 2587 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2588 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2589 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2590 | } |
| 2591 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2592 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2593 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2594 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2595 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2596 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2597 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2598 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2599 | struct extent_buffer *buf, int full_backref) |
| 2600 | { |
| 2601 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2602 | } |
| 2603 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2604 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2605 | struct btrfs_root *root, |
| 2606 | struct btrfs_path *path, |
| 2607 | struct btrfs_block_group_cache *cache) |
| 2608 | { |
| 2609 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2610 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2611 | unsigned long bi; |
| 2612 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2613 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2614 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2615 | if (ret < 0) |
| 2616 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2617 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2618 | |
| 2619 | leaf = path->nodes[0]; |
| 2620 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2621 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2622 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2623 | btrfs_release_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2624 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2625 | if (ret) |
| 2626 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2627 | return 0; |
| 2628 | |
| 2629 | } |
| 2630 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2631 | static struct btrfs_block_group_cache * |
| 2632 | next_block_group(struct btrfs_root *root, |
| 2633 | struct btrfs_block_group_cache *cache) |
| 2634 | { |
| 2635 | struct rb_node *node; |
| 2636 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2637 | node = rb_next(&cache->cache_node); |
| 2638 | btrfs_put_block_group(cache); |
| 2639 | if (node) { |
| 2640 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2641 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2642 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2643 | } else |
| 2644 | cache = NULL; |
| 2645 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2646 | return cache; |
| 2647 | } |
| 2648 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2649 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 2650 | struct btrfs_trans_handle *trans, |
| 2651 | struct btrfs_path *path) |
| 2652 | { |
| 2653 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 2654 | struct inode *inode = NULL; |
| 2655 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2656 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2657 | int num_pages = 0; |
| 2658 | int retries = 0; |
| 2659 | int ret = 0; |
| 2660 | |
| 2661 | /* |
| 2662 | * If this block group is smaller than 100 megs don't bother caching the |
| 2663 | * block group. |
| 2664 | */ |
| 2665 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 2666 | spin_lock(&block_group->lock); |
| 2667 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2668 | spin_unlock(&block_group->lock); |
| 2669 | return 0; |
| 2670 | } |
| 2671 | |
| 2672 | again: |
| 2673 | inode = lookup_free_space_inode(root, block_group, path); |
| 2674 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 2675 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2676 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2677 | goto out; |
| 2678 | } |
| 2679 | |
| 2680 | if (IS_ERR(inode)) { |
| 2681 | BUG_ON(retries); |
| 2682 | retries++; |
| 2683 | |
| 2684 | if (block_group->ro) |
| 2685 | goto out_free; |
| 2686 | |
| 2687 | ret = create_free_space_inode(root, trans, block_group, path); |
| 2688 | if (ret) |
| 2689 | goto out_free; |
| 2690 | goto again; |
| 2691 | } |
| 2692 | |
| 2693 | /* |
| 2694 | * We want to set the generation to 0, that way if anything goes wrong |
| 2695 | * from here on out we know not to trust this cache when we load up next |
| 2696 | * time. |
| 2697 | */ |
| 2698 | BTRFS_I(inode)->generation = 0; |
| 2699 | ret = btrfs_update_inode(trans, root, inode); |
| 2700 | WARN_ON(ret); |
| 2701 | |
| 2702 | if (i_size_read(inode) > 0) { |
| 2703 | ret = btrfs_truncate_free_space_cache(root, trans, path, |
| 2704 | inode); |
| 2705 | if (ret) |
| 2706 | goto out_put; |
| 2707 | } |
| 2708 | |
| 2709 | spin_lock(&block_group->lock); |
| 2710 | if (block_group->cached != BTRFS_CACHE_FINISHED) { |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2711 | /* We're not cached, don't bother trying to write stuff out */ |
| 2712 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2713 | spin_unlock(&block_group->lock); |
| 2714 | goto out_put; |
| 2715 | } |
| 2716 | spin_unlock(&block_group->lock); |
| 2717 | |
| 2718 | num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024); |
| 2719 | if (!num_pages) |
| 2720 | num_pages = 1; |
| 2721 | |
| 2722 | /* |
| 2723 | * Just to make absolutely sure we have enough space, we're going to |
| 2724 | * preallocate 12 pages worth of space for each block group. In |
| 2725 | * practice we ought to use at most 8, but we need extra space so we can |
| 2726 | * add our header and have a terminator between the extents and the |
| 2727 | * bitmaps. |
| 2728 | */ |
| 2729 | num_pages *= 16; |
| 2730 | num_pages *= PAGE_CACHE_SIZE; |
| 2731 | |
| 2732 | ret = btrfs_check_data_free_space(inode, num_pages); |
| 2733 | if (ret) |
| 2734 | goto out_put; |
| 2735 | |
| 2736 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 2737 | num_pages, num_pages, |
| 2738 | &alloc_hint); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2739 | if (!ret) |
| 2740 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2741 | btrfs_free_reserved_data_space(inode, num_pages); |
| 2742 | out_put: |
| 2743 | iput(inode); |
| 2744 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2745 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2746 | out: |
| 2747 | spin_lock(&block_group->lock); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2748 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2749 | spin_unlock(&block_group->lock); |
| 2750 | |
| 2751 | return ret; |
| 2752 | } |
| 2753 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2754 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2755 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2756 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2757 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2758 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2759 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2760 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2761 | |
| 2762 | path = btrfs_alloc_path(); |
| 2763 | if (!path) |
| 2764 | return -ENOMEM; |
| 2765 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2766 | again: |
| 2767 | while (1) { |
| 2768 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2769 | while (cache) { |
| 2770 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 2771 | break; |
| 2772 | cache = next_block_group(root, cache); |
| 2773 | } |
| 2774 | if (!cache) { |
| 2775 | if (last == 0) |
| 2776 | break; |
| 2777 | last = 0; |
| 2778 | continue; |
| 2779 | } |
| 2780 | err = cache_save_setup(cache, trans, path); |
| 2781 | last = cache->key.objectid + cache->key.offset; |
| 2782 | btrfs_put_block_group(cache); |
| 2783 | } |
| 2784 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2785 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2786 | if (last == 0) { |
| 2787 | err = btrfs_run_delayed_refs(trans, root, |
| 2788 | (unsigned long)-1); |
| 2789 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2790 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2791 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2792 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2793 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2794 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 2795 | btrfs_put_block_group(cache); |
| 2796 | goto again; |
| 2797 | } |
| 2798 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2799 | if (cache->dirty) |
| 2800 | break; |
| 2801 | cache = next_block_group(root, cache); |
| 2802 | } |
| 2803 | if (!cache) { |
| 2804 | if (last == 0) |
| 2805 | break; |
| 2806 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2807 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2808 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2809 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2810 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 2811 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2812 | cache->dirty = 0; |
| 2813 | last = cache->key.objectid + cache->key.offset; |
| 2814 | |
| 2815 | err = write_one_cache_group(trans, root, path, cache); |
| 2816 | BUG_ON(err); |
| 2817 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2818 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2819 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2820 | while (1) { |
| 2821 | /* |
| 2822 | * I don't think this is needed since we're just marking our |
| 2823 | * preallocated extent as written, but just in case it can't |
| 2824 | * hurt. |
| 2825 | */ |
| 2826 | if (last == 0) { |
| 2827 | err = btrfs_run_delayed_refs(trans, root, |
| 2828 | (unsigned long)-1); |
| 2829 | BUG_ON(err); |
| 2830 | } |
| 2831 | |
| 2832 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2833 | while (cache) { |
| 2834 | /* |
| 2835 | * Really this shouldn't happen, but it could if we |
| 2836 | * couldn't write the entire preallocated extent and |
| 2837 | * splitting the extent resulted in a new block. |
| 2838 | */ |
| 2839 | if (cache->dirty) { |
| 2840 | btrfs_put_block_group(cache); |
| 2841 | goto again; |
| 2842 | } |
| 2843 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2844 | break; |
| 2845 | cache = next_block_group(root, cache); |
| 2846 | } |
| 2847 | if (!cache) { |
| 2848 | if (last == 0) |
| 2849 | break; |
| 2850 | last = 0; |
| 2851 | continue; |
| 2852 | } |
| 2853 | |
| 2854 | btrfs_write_out_cache(root, trans, cache, path); |
| 2855 | |
| 2856 | /* |
| 2857 | * If we didn't have an error then the cache state is still |
| 2858 | * NEED_WRITE, so we can set it to WRITTEN. |
| 2859 | */ |
| 2860 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2861 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2862 | last = cache->key.objectid + cache->key.offset; |
| 2863 | btrfs_put_block_group(cache); |
| 2864 | } |
| 2865 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2866 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2867 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2868 | } |
| 2869 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2870 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2871 | { |
| 2872 | struct btrfs_block_group_cache *block_group; |
| 2873 | int readonly = 0; |
| 2874 | |
| 2875 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2876 | if (!block_group || block_group->ro) |
| 2877 | readonly = 1; |
| 2878 | if (block_group) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2879 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2880 | return readonly; |
| 2881 | } |
| 2882 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2883 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2884 | u64 total_bytes, u64 bytes_used, |
| 2885 | struct btrfs_space_info **space_info) |
| 2886 | { |
| 2887 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2888 | int i; |
| 2889 | int factor; |
| 2890 | |
| 2891 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 2892 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2893 | factor = 2; |
| 2894 | else |
| 2895 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2896 | |
| 2897 | found = __find_space_info(info, flags); |
| 2898 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2899 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2900 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2901 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2902 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2903 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2904 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2905 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2906 | *space_info = found; |
| 2907 | return 0; |
| 2908 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2909 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2910 | if (!found) |
| 2911 | return -ENOMEM; |
| 2912 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2913 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 2914 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2915 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2916 | spin_lock_init(&found->lock); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2917 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
| 2918 | BTRFS_BLOCK_GROUP_SYSTEM | |
| 2919 | BTRFS_BLOCK_GROUP_METADATA); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2920 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2921 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2922 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2923 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2924 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2925 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2926 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2927 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2928 | found->full = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 2929 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 2930 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 2931 | found->flush = 0; |
| 2932 | init_waitqueue_head(&found->wait); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2933 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2934 | list_add_rcu(&found->list, &info->space_info); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2935 | return 0; |
| 2936 | } |
| 2937 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2938 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2939 | { |
| 2940 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2941 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2942 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2943 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2944 | if (extra_flags) { |
| 2945 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2946 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2947 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2948 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2949 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2950 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2951 | } |
| 2952 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2953 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2954 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2955 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2956 | /* |
| 2957 | * we add in the count of missing devices because we want |
| 2958 | * to make sure that any RAID levels on a degraded FS |
| 2959 | * continue to be honored. |
| 2960 | */ |
| 2961 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 2962 | root->fs_info->fs_devices->missing_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2963 | |
| 2964 | if (num_devices == 1) |
| 2965 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2966 | if (num_devices < 4) |
| 2967 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2968 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2969 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2970 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2971 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2972 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2973 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2974 | |
| 2975 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2976 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2977 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2978 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2979 | |
| 2980 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2981 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2982 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2983 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2984 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2985 | return flags; |
| 2986 | } |
| 2987 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2988 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 2989 | { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2990 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2991 | flags |= root->fs_info->avail_data_alloc_bits & |
| 2992 | root->fs_info->data_alloc_profile; |
| 2993 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2994 | flags |= root->fs_info->avail_system_alloc_bits & |
| 2995 | root->fs_info->system_alloc_profile; |
| 2996 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2997 | flags |= root->fs_info->avail_metadata_alloc_bits & |
| 2998 | root->fs_info->metadata_alloc_profile; |
| 2999 | return btrfs_reduce_alloc_profile(root, flags); |
| 3000 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3001 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3002 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3003 | { |
| 3004 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3005 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3006 | if (data) |
| 3007 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3008 | else if (root == root->fs_info->chunk_root) |
| 3009 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3010 | else |
| 3011 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3012 | |
| 3013 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 3017 | { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3018 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3019 | BTRFS_BLOCK_GROUP_DATA); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /* |
| 3023 | * This will check the space that the inode allocates from to make sure we have |
| 3024 | * enough space for bytes. |
| 3025 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3026 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3027 | { |
| 3028 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3029 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | ab6e241 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3030 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3031 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3032 | |
| 3033 | /* make sure bytes are sectorsize aligned */ |
| 3034 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3035 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3036 | if (root == root->fs_info->tree_root || |
| 3037 | BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3038 | alloc_chunk = 0; |
| 3039 | committed = 1; |
| 3040 | } |
| 3041 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3042 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3043 | if (!data_sinfo) |
| 3044 | goto alloc; |
| 3045 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3046 | again: |
| 3047 | /* make sure we have enough space to handle the data first */ |
| 3048 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3049 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3050 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3051 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e241 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3052 | |
| 3053 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3054 | struct btrfs_trans_handle *trans; |
| 3055 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3056 | /* |
| 3057 | * if we don't have enough free bytes in this space then we need |
| 3058 | * to alloc a new chunk. |
| 3059 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3060 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3061 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3062 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3063 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3064 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3065 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3066 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3067 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3068 | if (IS_ERR(trans)) |
| 3069 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3070 | |
| 3071 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3072 | bytes + 2 * 1024 * 1024, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3073 | alloc_target, |
| 3074 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3075 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3076 | if (ret < 0) { |
| 3077 | if (ret != -ENOSPC) |
| 3078 | return ret; |
| 3079 | else |
| 3080 | goto commit_trans; |
| 3081 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3082 | |
| 3083 | if (!data_sinfo) { |
| 3084 | btrfs_set_inode_space_info(root, inode); |
| 3085 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3086 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3087 | goto again; |
| 3088 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3089 | |
| 3090 | /* |
| 3091 | * If we have less pinned bytes than we want to allocate then |
| 3092 | * don't bother committing the transaction, it won't help us. |
| 3093 | */ |
| 3094 | if (data_sinfo->bytes_pinned < bytes) |
| 3095 | committed = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3096 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3097 | |
| 3098 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3099 | commit_trans: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3100 | if (!committed && |
| 3101 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3102 | committed = 1; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3103 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3104 | if (IS_ERR(trans)) |
| 3105 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3106 | ret = btrfs_commit_transaction(trans, root); |
| 3107 | if (ret) |
| 3108 | return ret; |
| 3109 | goto again; |
| 3110 | } |
| 3111 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3112 | return -ENOSPC; |
| 3113 | } |
| 3114 | data_sinfo->bytes_may_use += bytes; |
| 3115 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 3116 | spin_unlock(&data_sinfo->lock); |
| 3117 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3118 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | /* |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3122 | * called when we are clearing an delalloc extent from the |
| 3123 | * inode's io_tree or there was an error for whatever reason |
| 3124 | * after calling btrfs_check_data_free_space |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3125 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3126 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3127 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3128 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3129 | struct btrfs_space_info *data_sinfo; |
| 3130 | |
| 3131 | /* make sure bytes are sectorsize aligned */ |
| 3132 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3133 | |
| 3134 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3135 | spin_lock(&data_sinfo->lock); |
| 3136 | data_sinfo->bytes_may_use -= bytes; |
| 3137 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3138 | spin_unlock(&data_sinfo->lock); |
| 3139 | } |
| 3140 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3141 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3142 | { |
| 3143 | struct list_head *head = &info->space_info; |
| 3144 | struct btrfs_space_info *found; |
| 3145 | |
| 3146 | rcu_read_lock(); |
| 3147 | list_for_each_entry_rcu(found, head, list) { |
| 3148 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3149 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3150 | } |
| 3151 | rcu_read_unlock(); |
| 3152 | } |
| 3153 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3154 | static int should_alloc_chunk(struct btrfs_root *root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3155 | struct btrfs_space_info *sinfo, u64 alloc_bytes, |
| 3156 | int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3157 | { |
| 3158 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3159 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3160 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3161 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3162 | if (force == CHUNK_ALLOC_FORCE) |
| 3163 | return 1; |
| 3164 | |
| 3165 | /* |
| 3166 | * in limited mode, we want to have some free space up to |
| 3167 | * about 1% of the FS size. |
| 3168 | */ |
| 3169 | if (force == CHUNK_ALLOC_LIMITED) { |
| 3170 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 3171 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 3172 | div_factor_fine(thresh, 1)); |
| 3173 | |
| 3174 | if (num_bytes - num_allocated < thresh) |
| 3175 | return 1; |
| 3176 | } |
| 3177 | |
| 3178 | /* |
| 3179 | * we have two similar checks here, one based on percentage |
| 3180 | * and once based on a hard number of 256MB. The idea |
| 3181 | * is that if we have a good amount of free |
| 3182 | * room, don't allocate a chunk. A good mount is |
| 3183 | * less than 80% utilized of the chunks we have allocated, |
| 3184 | * or more than 256MB free |
| 3185 | */ |
| 3186 | if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3187 | return 0; |
| 3188 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3189 | if (num_allocated + alloc_bytes < div_factor(num_bytes, 8)) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3190 | return 0; |
| 3191 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3192 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3193 | |
| 3194 | /* 256MB or 5% of the FS */ |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3195 | thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5)); |
| 3196 | |
| 3197 | if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3198 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3199 | return 1; |
| 3200 | } |
| 3201 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3202 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3203 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3204 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3205 | { |
| 3206 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3207 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3208 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3209 | int ret = 0; |
| 3210 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3211 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3212 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3213 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3214 | if (!space_info) { |
| 3215 | ret = update_space_info(extent_root->fs_info, flags, |
| 3216 | 0, 0, &space_info); |
| 3217 | BUG_ON(ret); |
| 3218 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3219 | BUG_ON(!space_info); |
| 3220 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3221 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3222 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3223 | if (space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3224 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3225 | if (space_info->full) { |
| 3226 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3227 | return 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3228 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3229 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3230 | if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3231 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3232 | return 0; |
| 3233 | } else if (space_info->chunk_alloc) { |
| 3234 | wait_for_alloc = 1; |
| 3235 | } else { |
| 3236 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3237 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3238 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3239 | spin_unlock(&space_info->lock); |
| 3240 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3241 | mutex_lock(&fs_info->chunk_mutex); |
| 3242 | |
| 3243 | /* |
| 3244 | * The chunk_mutex is held throughout the entirety of a chunk |
| 3245 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 3246 | * other guy is done and we need to recheck and see if we should |
| 3247 | * allocate. |
| 3248 | */ |
| 3249 | if (wait_for_alloc) { |
| 3250 | mutex_unlock(&fs_info->chunk_mutex); |
| 3251 | wait_for_alloc = 0; |
| 3252 | goto again; |
| 3253 | } |
| 3254 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3255 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3256 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3257 | * allocating mixed chunks instead of individual chunks. |
| 3258 | */ |
| 3259 | if (btrfs_mixed_space_info(space_info)) |
| 3260 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3261 | |
| 3262 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3263 | * if we're doing a data chunk, go ahead and make sure that |
| 3264 | * we keep a reasonable number of metadata chunks allocated in the |
| 3265 | * FS as well. |
| 3266 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3267 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3268 | fs_info->data_chunk_allocations++; |
| 3269 | if (!(fs_info->data_chunk_allocations % |
| 3270 | fs_info->metadata_ratio)) |
| 3271 | force_metadata_allocation(fs_info); |
| 3272 | } |
| 3273 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3274 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3275 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3276 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3277 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3278 | else |
| 3279 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3280 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3281 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3282 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3283 | spin_unlock(&space_info->lock); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3284 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3285 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3286 | } |
| 3287 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3288 | /* |
| 3289 | * shrink metadata reservation for delalloc |
| 3290 | */ |
| 3291 | static int shrink_delalloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3292 | struct btrfs_root *root, u64 to_reclaim, int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3293 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3294 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3295 | struct btrfs_space_info *space_info; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3296 | u64 reserved; |
| 3297 | u64 max_reclaim; |
| 3298 | u64 reclaimed = 0; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3299 | long time_left; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3300 | int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3301 | int loops = 0; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3302 | unsigned long progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3303 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3304 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3305 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3306 | |
| 3307 | smp_mb(); |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3308 | reserved = space_info->bytes_reserved; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3309 | progress = space_info->reservation_progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3310 | |
| 3311 | if (reserved == 0) |
| 3312 | return 0; |
| 3313 | |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3314 | smp_mb(); |
| 3315 | if (root->fs_info->delalloc_bytes == 0) { |
| 3316 | if (trans) |
| 3317 | return 0; |
| 3318 | btrfs_wait_ordered_extents(root, 0, 0); |
| 3319 | return 0; |
| 3320 | } |
| 3321 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3322 | max_reclaim = min(reserved, to_reclaim); |
| 3323 | |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3324 | while (loops < 1024) { |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3325 | /* have the flusher threads jump in and do some IO */ |
| 3326 | smp_mb(); |
| 3327 | nr_pages = min_t(unsigned long, nr_pages, |
| 3328 | root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); |
| 3329 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3330 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3331 | spin_lock(&space_info->lock); |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3332 | if (reserved > space_info->bytes_reserved) |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3333 | reclaimed += reserved - space_info->bytes_reserved; |
| 3334 | reserved = space_info->bytes_reserved; |
| 3335 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3336 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3337 | loops++; |
| 3338 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3339 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3340 | break; |
| 3341 | |
| 3342 | if (trans && trans->transaction->blocked) |
| 3343 | return -EAGAIN; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3344 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3345 | time_left = schedule_timeout_interruptible(1); |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3346 | |
| 3347 | /* We were interrupted, exit */ |
| 3348 | if (time_left) |
| 3349 | break; |
| 3350 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3351 | /* we've kicked the IO a few times, if anything has been freed, |
| 3352 | * exit. There is no sense in looping here for a long time |
| 3353 | * when we really need to commit the transaction, or there are |
| 3354 | * just too many writers without enough free space |
| 3355 | */ |
| 3356 | |
| 3357 | if (loops > 3) { |
| 3358 | smp_mb(); |
| 3359 | if (progress != space_info->reservation_progress) |
| 3360 | break; |
| 3361 | } |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3362 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3363 | } |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3364 | if (reclaimed >= to_reclaim && !trans) |
| 3365 | btrfs_wait_ordered_extents(root, 0, 0); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3366 | return reclaimed >= to_reclaim; |
| 3367 | } |
| 3368 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3369 | /* |
| 3370 | * Retries tells us how many times we've called reserve_metadata_bytes. The |
| 3371 | * idea is if this is the first call (retries == 0) then we will add to our |
| 3372 | * reserved count if we can't make the allocation in order to hold our place |
| 3373 | * while we go and try and free up space. That way for retries > 1 we don't try |
| 3374 | * and add space, we just check to see if the amount of unused space is >= the |
| 3375 | * total space, meaning that our reservation is valid. |
| 3376 | * |
| 3377 | * However if we don't intend to retry this reservation, pass -1 as retries so |
| 3378 | * that it short circuits this logic. |
| 3379 | */ |
| 3380 | static int reserve_metadata_bytes(struct btrfs_trans_handle *trans, |
| 3381 | struct btrfs_root *root, |
| 3382 | struct btrfs_block_rsv *block_rsv, |
| 3383 | u64 orig_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3384 | { |
| 3385 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3386 | u64 unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3387 | u64 num_bytes = orig_bytes; |
| 3388 | int retries = 0; |
| 3389 | int ret = 0; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3390 | bool committed = false; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3391 | bool flushing = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3392 | |
| 3393 | again: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3394 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3395 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3396 | /* |
| 3397 | * We only want to wait if somebody other than us is flushing and we are |
| 3398 | * actually alloed to flush. |
| 3399 | */ |
| 3400 | while (flush && !flushing && space_info->flush) { |
| 3401 | spin_unlock(&space_info->lock); |
| 3402 | /* |
| 3403 | * If we have a trans handle we can't wait because the flusher |
| 3404 | * may have to commit the transaction, which would mean we would |
| 3405 | * deadlock since we are waiting for the flusher to finish, but |
| 3406 | * hold the current transaction open. |
| 3407 | */ |
| 3408 | if (trans) |
| 3409 | return -EAGAIN; |
| 3410 | ret = wait_event_interruptible(space_info->wait, |
| 3411 | !space_info->flush); |
| 3412 | /* Must have been interrupted, return */ |
| 3413 | if (ret) |
| 3414 | return -EINTR; |
| 3415 | |
| 3416 | spin_lock(&space_info->lock); |
| 3417 | } |
| 3418 | |
| 3419 | ret = -ENOSPC; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3420 | unused = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3421 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3422 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3423 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3424 | /* |
| 3425 | * The idea here is that we've not already over-reserved the block group |
| 3426 | * then we can go ahead and save our reservation first and then start |
| 3427 | * flushing if we need to. Otherwise if we've already overcommitted |
| 3428 | * lets start flushing stuff first and then come back and try to make |
| 3429 | * our reservation. |
| 3430 | */ |
| 3431 | if (unused <= space_info->total_bytes) { |
Arne Jansen | 6f33434 | 2010-11-12 23:17:56 +0000 | [diff] [blame] | 3432 | unused = space_info->total_bytes - unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3433 | if (unused >= num_bytes) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3434 | space_info->bytes_reserved += orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3435 | ret = 0; |
| 3436 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3437 | /* |
| 3438 | * Ok set num_bytes to orig_bytes since we aren't |
| 3439 | * overocmmitted, this way we only try and reclaim what |
| 3440 | * we need. |
| 3441 | */ |
| 3442 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3443 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3444 | } else { |
| 3445 | /* |
| 3446 | * Ok we're over committed, set num_bytes to the overcommitted |
| 3447 | * amount plus the amount of bytes that we need for this |
| 3448 | * reservation. |
| 3449 | */ |
| 3450 | num_bytes = unused - space_info->total_bytes + |
| 3451 | (orig_bytes * (retries + 1)); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3452 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3453 | |
| 3454 | /* |
| 3455 | * Couldn't make our reservation, save our place so while we're trying |
| 3456 | * to reclaim space we can actually use it instead of somebody else |
| 3457 | * stealing it from us. |
| 3458 | */ |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3459 | if (ret && flush) { |
| 3460 | flushing = true; |
| 3461 | space_info->flush = 1; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3462 | } |
| 3463 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3464 | spin_unlock(&space_info->lock); |
| 3465 | |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3466 | if (!ret || !flush) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3467 | goto out; |
| 3468 | |
| 3469 | /* |
| 3470 | * We do synchronous shrinking since we don't actually unreserve |
| 3471 | * metadata until after the IO is completed. |
| 3472 | */ |
| 3473 | ret = shrink_delalloc(trans, root, num_bytes, 1); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3474 | if (ret < 0) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3475 | goto out; |
| 3476 | |
Chris Mason | 75c195a | 2011-07-27 15:57:44 -0400 | [diff] [blame] | 3477 | ret = 0; |
| 3478 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3479 | /* |
| 3480 | * So if we were overcommitted it's possible that somebody else flushed |
| 3481 | * out enough space and we simply didn't have enough space to reclaim, |
| 3482 | * so go back around and try again. |
| 3483 | */ |
| 3484 | if (retries < 2) { |
| 3485 | retries++; |
| 3486 | goto again; |
| 3487 | } |
| 3488 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3489 | /* |
| 3490 | * Not enough space to be reclaimed, don't bother committing the |
| 3491 | * transaction. |
| 3492 | */ |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3493 | spin_lock(&space_info->lock); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3494 | if (space_info->bytes_pinned < orig_bytes) |
| 3495 | ret = -ENOSPC; |
| 3496 | spin_unlock(&space_info->lock); |
| 3497 | if (ret) |
| 3498 | goto out; |
| 3499 | |
| 3500 | ret = -EAGAIN; |
Chris Mason | 75c195a | 2011-07-27 15:57:44 -0400 | [diff] [blame] | 3501 | if (trans) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3502 | goto out; |
| 3503 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3504 | ret = -ENOSPC; |
Chris Mason | 75c195a | 2011-07-27 15:57:44 -0400 | [diff] [blame] | 3505 | if (committed) |
| 3506 | goto out; |
| 3507 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3508 | trans = btrfs_join_transaction(root); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3509 | if (IS_ERR(trans)) |
| 3510 | goto out; |
| 3511 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3512 | if (!ret) { |
| 3513 | trans = NULL; |
| 3514 | committed = true; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3515 | goto again; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3516 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3517 | |
| 3518 | out: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3519 | if (flushing) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3520 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3521 | space_info->flush = 0; |
| 3522 | wake_up_all(&space_info->wait); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3523 | spin_unlock(&space_info->lock); |
| 3524 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3525 | return ret; |
| 3526 | } |
| 3527 | |
| 3528 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3529 | struct btrfs_root *root) |
| 3530 | { |
| 3531 | struct btrfs_block_rsv *block_rsv; |
| 3532 | if (root->ref_cows) |
| 3533 | block_rsv = trans->block_rsv; |
| 3534 | else |
| 3535 | block_rsv = root->block_rsv; |
| 3536 | |
| 3537 | if (!block_rsv) |
| 3538 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3539 | |
| 3540 | return block_rsv; |
| 3541 | } |
| 3542 | |
| 3543 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3544 | u64 num_bytes) |
| 3545 | { |
| 3546 | int ret = -ENOSPC; |
| 3547 | spin_lock(&block_rsv->lock); |
| 3548 | if (block_rsv->reserved >= num_bytes) { |
| 3549 | block_rsv->reserved -= num_bytes; |
| 3550 | if (block_rsv->reserved < block_rsv->size) |
| 3551 | block_rsv->full = 0; |
| 3552 | ret = 0; |
| 3553 | } |
| 3554 | spin_unlock(&block_rsv->lock); |
| 3555 | return ret; |
| 3556 | } |
| 3557 | |
| 3558 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3559 | u64 num_bytes, int update_size) |
| 3560 | { |
| 3561 | spin_lock(&block_rsv->lock); |
| 3562 | block_rsv->reserved += num_bytes; |
| 3563 | if (update_size) |
| 3564 | block_rsv->size += num_bytes; |
| 3565 | else if (block_rsv->reserved >= block_rsv->size) |
| 3566 | block_rsv->full = 1; |
| 3567 | spin_unlock(&block_rsv->lock); |
| 3568 | } |
| 3569 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 3570 | static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3571 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3572 | { |
| 3573 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3574 | |
| 3575 | spin_lock(&block_rsv->lock); |
| 3576 | if (num_bytes == (u64)-1) |
| 3577 | num_bytes = block_rsv->size; |
| 3578 | block_rsv->size -= num_bytes; |
| 3579 | if (block_rsv->reserved >= block_rsv->size) { |
| 3580 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3581 | block_rsv->reserved = block_rsv->size; |
| 3582 | block_rsv->full = 1; |
| 3583 | } else { |
| 3584 | num_bytes = 0; |
| 3585 | } |
| 3586 | spin_unlock(&block_rsv->lock); |
| 3587 | |
| 3588 | if (num_bytes > 0) { |
| 3589 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 3590 | spin_lock(&dest->lock); |
| 3591 | if (!dest->full) { |
| 3592 | u64 bytes_to_add; |
| 3593 | |
| 3594 | bytes_to_add = dest->size - dest->reserved; |
| 3595 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 3596 | dest->reserved += bytes_to_add; |
| 3597 | if (dest->reserved >= dest->size) |
| 3598 | dest->full = 1; |
| 3599 | num_bytes -= bytes_to_add; |
| 3600 | } |
| 3601 | spin_unlock(&dest->lock); |
| 3602 | } |
| 3603 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3604 | spin_lock(&space_info->lock); |
| 3605 | space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3606 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3607 | spin_unlock(&space_info->lock); |
| 3608 | } |
| 3609 | } |
| 3610 | } |
| 3611 | |
| 3612 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3613 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3614 | { |
| 3615 | int ret; |
| 3616 | |
| 3617 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3618 | if (ret) |
| 3619 | return ret; |
| 3620 | |
| 3621 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3622 | return 0; |
| 3623 | } |
| 3624 | |
| 3625 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3626 | { |
| 3627 | memset(rsv, 0, sizeof(*rsv)); |
| 3628 | spin_lock_init(&rsv->lock); |
| 3629 | atomic_set(&rsv->usage, 1); |
| 3630 | rsv->priority = 6; |
| 3631 | INIT_LIST_HEAD(&rsv->list); |
| 3632 | } |
| 3633 | |
| 3634 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3635 | { |
| 3636 | struct btrfs_block_rsv *block_rsv; |
| 3637 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3638 | |
| 3639 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3640 | if (!block_rsv) |
| 3641 | return NULL; |
| 3642 | |
| 3643 | btrfs_init_block_rsv(block_rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3644 | block_rsv->space_info = __find_space_info(fs_info, |
| 3645 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3646 | return block_rsv; |
| 3647 | } |
| 3648 | |
| 3649 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3650 | struct btrfs_block_rsv *rsv) |
| 3651 | { |
| 3652 | if (rsv && atomic_dec_and_test(&rsv->usage)) { |
| 3653 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3654 | if (!rsv->durable) |
| 3655 | kfree(rsv); |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | /* |
| 3660 | * make the block_rsv struct be able to capture freed space. |
| 3661 | * the captured space will re-add to the the block_rsv struct |
| 3662 | * after transaction commit |
| 3663 | */ |
| 3664 | void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info, |
| 3665 | struct btrfs_block_rsv *block_rsv) |
| 3666 | { |
| 3667 | block_rsv->durable = 1; |
| 3668 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 3669 | list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list); |
| 3670 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 3671 | } |
| 3672 | |
| 3673 | int btrfs_block_rsv_add(struct btrfs_trans_handle *trans, |
| 3674 | struct btrfs_root *root, |
| 3675 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3676 | u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3677 | { |
| 3678 | int ret; |
| 3679 | |
| 3680 | if (num_bytes == 0) |
| 3681 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3682 | |
| 3683 | ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3684 | if (!ret) { |
| 3685 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3686 | return 0; |
| 3687 | } |
| 3688 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3689 | return ret; |
| 3690 | } |
| 3691 | |
| 3692 | int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, |
| 3693 | struct btrfs_root *root, |
| 3694 | struct btrfs_block_rsv *block_rsv, |
| 3695 | u64 min_reserved, int min_factor) |
| 3696 | { |
| 3697 | u64 num_bytes = 0; |
| 3698 | int commit_trans = 0; |
| 3699 | int ret = -ENOSPC; |
| 3700 | |
| 3701 | if (!block_rsv) |
| 3702 | return 0; |
| 3703 | |
| 3704 | spin_lock(&block_rsv->lock); |
| 3705 | if (min_factor > 0) |
| 3706 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3707 | if (min_reserved > num_bytes) |
| 3708 | num_bytes = min_reserved; |
| 3709 | |
| 3710 | if (block_rsv->reserved >= num_bytes) { |
| 3711 | ret = 0; |
| 3712 | } else { |
| 3713 | num_bytes -= block_rsv->reserved; |
| 3714 | if (block_rsv->durable && |
| 3715 | block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes) |
| 3716 | commit_trans = 1; |
| 3717 | } |
| 3718 | spin_unlock(&block_rsv->lock); |
| 3719 | if (!ret) |
| 3720 | return 0; |
| 3721 | |
| 3722 | if (block_rsv->refill_used) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3723 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 3724 | num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3725 | if (!ret) { |
| 3726 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3727 | return 0; |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | if (commit_trans) { |
| 3732 | if (trans) |
| 3733 | return -EAGAIN; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3734 | trans = btrfs_join_transaction(root); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3735 | BUG_ON(IS_ERR(trans)); |
| 3736 | ret = btrfs_commit_transaction(trans, root); |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3740 | return -ENOSPC; |
| 3741 | } |
| 3742 | |
| 3743 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3744 | struct btrfs_block_rsv *dst_rsv, |
| 3745 | u64 num_bytes) |
| 3746 | { |
| 3747 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3748 | } |
| 3749 | |
| 3750 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3751 | struct btrfs_block_rsv *block_rsv, |
| 3752 | u64 num_bytes) |
| 3753 | { |
| 3754 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3755 | if (global_rsv->full || global_rsv == block_rsv || |
| 3756 | block_rsv->space_info != global_rsv->space_info) |
| 3757 | global_rsv = NULL; |
| 3758 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3759 | } |
| 3760 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3761 | /* |
| 3762 | * helper to calculate size of global block reservation. |
| 3763 | * the desired value is sum of space used by extent tree, |
| 3764 | * checksum tree and root tree |
| 3765 | */ |
| 3766 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3767 | { |
| 3768 | struct btrfs_space_info *sinfo; |
| 3769 | u64 num_bytes; |
| 3770 | u64 meta_used; |
| 3771 | u64 data_used; |
| 3772 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3773 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3774 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3775 | spin_lock(&sinfo->lock); |
| 3776 | data_used = sinfo->bytes_used; |
| 3777 | spin_unlock(&sinfo->lock); |
| 3778 | |
| 3779 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3780 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3781 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 3782 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3783 | meta_used = sinfo->bytes_used; |
| 3784 | spin_unlock(&sinfo->lock); |
| 3785 | |
| 3786 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3787 | csum_size * 2; |
| 3788 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3789 | |
| 3790 | if (num_bytes * 3 > meta_used) |
| 3791 | num_bytes = div64_u64(meta_used, 3); |
| 3792 | |
| 3793 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3794 | } |
| 3795 | |
| 3796 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3797 | { |
| 3798 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3799 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3800 | u64 num_bytes; |
| 3801 | |
| 3802 | num_bytes = calc_global_metadata_size(fs_info); |
| 3803 | |
| 3804 | spin_lock(&block_rsv->lock); |
| 3805 | spin_lock(&sinfo->lock); |
| 3806 | |
| 3807 | block_rsv->size = num_bytes; |
| 3808 | |
| 3809 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3810 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 3811 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3812 | |
| 3813 | if (sinfo->total_bytes > num_bytes) { |
| 3814 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3815 | block_rsv->reserved += num_bytes; |
| 3816 | sinfo->bytes_reserved += num_bytes; |
| 3817 | } |
| 3818 | |
| 3819 | if (block_rsv->reserved >= block_rsv->size) { |
| 3820 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3821 | sinfo->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3822 | sinfo->reservation_progress++; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3823 | block_rsv->reserved = block_rsv->size; |
| 3824 | block_rsv->full = 1; |
| 3825 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 3826 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3827 | spin_unlock(&sinfo->lock); |
| 3828 | spin_unlock(&block_rsv->lock); |
| 3829 | } |
| 3830 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3831 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3832 | { |
| 3833 | struct btrfs_space_info *space_info; |
| 3834 | |
| 3835 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3836 | fs_info->chunk_block_rsv.space_info = space_info; |
| 3837 | fs_info->chunk_block_rsv.priority = 10; |
| 3838 | |
| 3839 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3840 | fs_info->global_block_rsv.space_info = space_info; |
| 3841 | fs_info->global_block_rsv.priority = 10; |
| 3842 | fs_info->global_block_rsv.refill_used = 1; |
| 3843 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3844 | fs_info->trans_block_rsv.space_info = space_info; |
| 3845 | fs_info->empty_block_rsv.space_info = space_info; |
| 3846 | fs_info->empty_block_rsv.priority = 10; |
| 3847 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3848 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3849 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3850 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3851 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3852 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3853 | |
| 3854 | btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv); |
| 3855 | |
| 3856 | btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv); |
| 3857 | |
| 3858 | update_global_block_rsv(fs_info); |
| 3859 | } |
| 3860 | |
| 3861 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3862 | { |
| 3863 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3864 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3865 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3866 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3867 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3868 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3869 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3870 | } |
| 3871 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 3872 | int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3873 | struct btrfs_root *root, |
| 3874 | struct btrfs_block_rsv *rsv) |
| 3875 | { |
| 3876 | struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv; |
| 3877 | u64 num_bytes; |
| 3878 | int ret; |
| 3879 | |
| 3880 | /* |
| 3881 | * Truncate should be freeing data, but give us 2 items just in case it |
| 3882 | * needs to use some space. We may want to be smarter about this in the |
| 3883 | * future. |
| 3884 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 3885 | num_bytes = btrfs_calc_trans_metadata_size(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 3886 | |
| 3887 | /* We already have enough bytes, just return */ |
| 3888 | if (rsv->reserved >= num_bytes) |
| 3889 | return 0; |
| 3890 | |
| 3891 | num_bytes -= rsv->reserved; |
| 3892 | |
| 3893 | /* |
| 3894 | * You should have reserved enough space before hand to do this, so this |
| 3895 | * should not fail. |
| 3896 | */ |
| 3897 | ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes); |
| 3898 | BUG_ON(ret); |
| 3899 | |
| 3900 | return 0; |
| 3901 | } |
| 3902 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3903 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 3904 | struct btrfs_root *root) |
| 3905 | { |
| 3906 | if (!trans->bytes_reserved) |
| 3907 | return; |
| 3908 | |
| 3909 | BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv); |
| 3910 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3911 | trans->bytes_reserved); |
| 3912 | trans->bytes_reserved = 0; |
| 3913 | } |
| 3914 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3915 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3916 | struct inode *inode) |
| 3917 | { |
| 3918 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3919 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3920 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 3921 | |
| 3922 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 3923 | * We need to hold space in order to delete our orphan item once we've |
| 3924 | * added it, so this takes the reservation so we can release it later |
| 3925 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3926 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 3927 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3928 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3929 | } |
| 3930 | |
| 3931 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 3932 | { |
| 3933 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 3934 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3935 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 3936 | } |
| 3937 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3938 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3939 | struct btrfs_pending_snapshot *pending) |
| 3940 | { |
| 3941 | struct btrfs_root *root = pending->root; |
| 3942 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3943 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 3944 | /* |
| 3945 | * two for root back/forward refs, two for directory entries |
| 3946 | * and one for root of the snapshot. |
| 3947 | */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3948 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3949 | dst_rsv->space_info = src_rsv->space_info; |
| 3950 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3951 | } |
| 3952 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 3953 | static unsigned drop_outstanding_extent(struct inode *inode) |
| 3954 | { |
| 3955 | unsigned dropped_extents = 0; |
| 3956 | |
| 3957 | spin_lock(&BTRFS_I(inode)->lock); |
| 3958 | BUG_ON(!BTRFS_I(inode)->outstanding_extents); |
| 3959 | BTRFS_I(inode)->outstanding_extents--; |
| 3960 | |
| 3961 | /* |
| 3962 | * If we have more or the same amount of outsanding extents than we have |
| 3963 | * reserved then we need to leave the reserved extents count alone. |
| 3964 | */ |
| 3965 | if (BTRFS_I(inode)->outstanding_extents >= |
| 3966 | BTRFS_I(inode)->reserved_extents) |
| 3967 | goto out; |
| 3968 | |
| 3969 | dropped_extents = BTRFS_I(inode)->reserved_extents - |
| 3970 | BTRFS_I(inode)->outstanding_extents; |
| 3971 | BTRFS_I(inode)->reserved_extents -= dropped_extents; |
| 3972 | out: |
| 3973 | spin_unlock(&BTRFS_I(inode)->lock); |
| 3974 | return dropped_extents; |
| 3975 | } |
| 3976 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3977 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes) |
| 3978 | { |
| 3979 | return num_bytes >>= 3; |
| 3980 | } |
| 3981 | |
| 3982 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 3983 | { |
| 3984 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3985 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 3986 | u64 to_reserve = 0; |
| 3987 | unsigned nr_extents = 0; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3988 | int ret; |
| 3989 | |
| 3990 | if (btrfs_transaction_in_commit(root->fs_info)) |
| 3991 | schedule_timeout(1); |
| 3992 | |
| 3993 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3994 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 3995 | spin_lock(&BTRFS_I(inode)->lock); |
| 3996 | BTRFS_I(inode)->outstanding_extents++; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 3997 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 3998 | if (BTRFS_I(inode)->outstanding_extents > |
| 3999 | BTRFS_I(inode)->reserved_extents) { |
| 4000 | nr_extents = BTRFS_I(inode)->outstanding_extents - |
| 4001 | BTRFS_I(inode)->reserved_extents; |
| 4002 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 4003 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4004 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4005 | } |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4006 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4007 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4008 | to_reserve += calc_csum_metadata_size(inode, num_bytes); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4009 | ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4010 | if (ret) { |
| 4011 | unsigned dropped; |
| 4012 | /* |
| 4013 | * We don't need the return value since our reservation failed, |
| 4014 | * we just need to clean up our counter. |
| 4015 | */ |
| 4016 | dropped = drop_outstanding_extent(inode); |
| 4017 | WARN_ON(dropped > 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4018 | return ret; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4019 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4020 | |
| 4021 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 4022 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4023 | return 0; |
| 4024 | } |
| 4025 | |
| 4026 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 4027 | { |
| 4028 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4029 | u64 to_free = 0; |
| 4030 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4031 | |
| 4032 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4033 | dropped = drop_outstanding_extent(inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4034 | |
| 4035 | to_free = calc_csum_metadata_size(inode, num_bytes); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4036 | if (dropped > 0) |
| 4037 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4038 | |
| 4039 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 4040 | to_free); |
| 4041 | } |
| 4042 | |
| 4043 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 4044 | { |
| 4045 | int ret; |
| 4046 | |
| 4047 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 4048 | if (ret) |
| 4049 | return ret; |
| 4050 | |
| 4051 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 4052 | if (ret) { |
| 4053 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4054 | return ret; |
| 4055 | } |
| 4056 | |
| 4057 | return 0; |
| 4058 | } |
| 4059 | |
| 4060 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 4061 | { |
| 4062 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 4063 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4064 | } |
| 4065 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4066 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 4067 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4068 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4069 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4070 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4071 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4072 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4073 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4074 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4075 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 4076 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4077 | /* block accounting for super block */ |
| 4078 | spin_lock(&info->delalloc_lock); |
| 4079 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 4080 | if (alloc) |
| 4081 | old_val += num_bytes; |
| 4082 | else |
| 4083 | old_val -= num_bytes; |
| 4084 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4085 | spin_unlock(&info->delalloc_lock); |
| 4086 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4087 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4088 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4089 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4090 | return -1; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4091 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 4092 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4093 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4094 | factor = 2; |
| 4095 | else |
| 4096 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4097 | /* |
| 4098 | * If this block group has free space cache written out, we |
| 4099 | * need to make sure to load it if we are removing space. This |
| 4100 | * is because we need the unpinning stage to actually add the |
| 4101 | * space back to the block group, otherwise we will leak space. |
| 4102 | */ |
| 4103 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4104 | cache_block_group(cache, trans, NULL, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4105 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4106 | byte_in_group = bytenr - cache->key.objectid; |
| 4107 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4108 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4109 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4110 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4111 | |
| 4112 | if (btrfs_super_cache_generation(&info->super_copy) != 0 && |
| 4113 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 4114 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 4115 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4116 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4117 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4118 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4119 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4120 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4121 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4122 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4123 | cache->space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4124 | cache->space_info->reservation_progress++; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4125 | cache->space_info->bytes_used += num_bytes; |
| 4126 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4127 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4128 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4129 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4130 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4131 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4132 | cache->pinned += num_bytes; |
| 4133 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4134 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4135 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4136 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4137 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4138 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4139 | set_extent_dirty(info->pinned_extents, |
| 4140 | bytenr, bytenr + num_bytes - 1, |
| 4141 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4142 | } |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4143 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4144 | total -= num_bytes; |
| 4145 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4146 | } |
| 4147 | return 0; |
| 4148 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4149 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4150 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 4151 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4152 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4153 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4154 | |
| 4155 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 4156 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4157 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4158 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4159 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4160 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4161 | |
| 4162 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4163 | } |
| 4164 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4165 | static int pin_down_extent(struct btrfs_root *root, |
| 4166 | struct btrfs_block_group_cache *cache, |
| 4167 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4168 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4169 | spin_lock(&cache->space_info->lock); |
| 4170 | spin_lock(&cache->lock); |
| 4171 | cache->pinned += num_bytes; |
| 4172 | cache->space_info->bytes_pinned += num_bytes; |
| 4173 | if (reserved) { |
| 4174 | cache->reserved -= num_bytes; |
| 4175 | cache->space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4176 | cache->space_info->reservation_progress++; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4177 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4178 | spin_unlock(&cache->lock); |
| 4179 | spin_unlock(&cache->space_info->lock); |
| 4180 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4181 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 4182 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4183 | return 0; |
| 4184 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4185 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4186 | /* |
| 4187 | * this function must be called within transaction |
| 4188 | */ |
| 4189 | int btrfs_pin_extent(struct btrfs_root *root, |
| 4190 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4191 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4192 | struct btrfs_block_group_cache *cache; |
| 4193 | |
| 4194 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 4195 | BUG_ON(!cache); |
| 4196 | |
| 4197 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 4198 | |
| 4199 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4200 | return 0; |
| 4201 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4202 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4203 | /* |
| 4204 | * update size of reserved extents. this function may return -EAGAIN |
| 4205 | * if 'reserve' is true or 'sinfo' is false. |
| 4206 | */ |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4207 | int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4208 | u64 num_bytes, int reserve, int sinfo) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4209 | { |
| 4210 | int ret = 0; |
| 4211 | if (sinfo) { |
| 4212 | struct btrfs_space_info *space_info = cache->space_info; |
| 4213 | spin_lock(&space_info->lock); |
| 4214 | spin_lock(&cache->lock); |
| 4215 | if (reserve) { |
| 4216 | if (cache->ro) { |
| 4217 | ret = -EAGAIN; |
| 4218 | } else { |
| 4219 | cache->reserved += num_bytes; |
| 4220 | space_info->bytes_reserved += num_bytes; |
| 4221 | } |
| 4222 | } else { |
| 4223 | if (cache->ro) |
| 4224 | space_info->bytes_readonly += num_bytes; |
| 4225 | cache->reserved -= num_bytes; |
| 4226 | space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4227 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4228 | } |
| 4229 | spin_unlock(&cache->lock); |
| 4230 | spin_unlock(&space_info->lock); |
| 4231 | } else { |
| 4232 | spin_lock(&cache->lock); |
| 4233 | if (cache->ro) { |
| 4234 | ret = -EAGAIN; |
| 4235 | } else { |
| 4236 | if (reserve) |
| 4237 | cache->reserved += num_bytes; |
| 4238 | else |
| 4239 | cache->reserved -= num_bytes; |
| 4240 | } |
| 4241 | spin_unlock(&cache->lock); |
| 4242 | } |
| 4243 | return ret; |
| 4244 | } |
| 4245 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4246 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 4247 | struct btrfs_root *root) |
| 4248 | { |
| 4249 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4250 | struct btrfs_caching_control *next; |
| 4251 | struct btrfs_caching_control *caching_ctl; |
| 4252 | struct btrfs_block_group_cache *cache; |
| 4253 | |
| 4254 | down_write(&fs_info->extent_commit_sem); |
| 4255 | |
| 4256 | list_for_each_entry_safe(caching_ctl, next, |
| 4257 | &fs_info->caching_block_groups, list) { |
| 4258 | cache = caching_ctl->block_group; |
| 4259 | if (block_group_cache_done(cache)) { |
| 4260 | cache->last_byte_to_unpin = (u64)-1; |
| 4261 | list_del_init(&caching_ctl->list); |
| 4262 | put_caching_control(caching_ctl); |
| 4263 | } else { |
| 4264 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4265 | } |
| 4266 | } |
| 4267 | |
| 4268 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4269 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4270 | else |
| 4271 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4272 | |
| 4273 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4274 | |
| 4275 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4276 | return 0; |
| 4277 | } |
| 4278 | |
| 4279 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 4280 | { |
| 4281 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4282 | struct btrfs_block_group_cache *cache = NULL; |
| 4283 | u64 len; |
| 4284 | |
| 4285 | while (start <= end) { |
| 4286 | if (!cache || |
| 4287 | start >= cache->key.objectid + cache->key.offset) { |
| 4288 | if (cache) |
| 4289 | btrfs_put_block_group(cache); |
| 4290 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4291 | BUG_ON(!cache); |
| 4292 | } |
| 4293 | |
| 4294 | len = cache->key.objectid + cache->key.offset - start; |
| 4295 | len = min(len, end + 1 - start); |
| 4296 | |
| 4297 | if (start < cache->last_byte_to_unpin) { |
| 4298 | len = min(len, cache->last_byte_to_unpin - start); |
| 4299 | btrfs_add_free_space(cache, start, len); |
| 4300 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4301 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4302 | start += len; |
| 4303 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4304 | spin_lock(&cache->space_info->lock); |
| 4305 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4306 | cache->pinned -= len; |
| 4307 | cache->space_info->bytes_pinned -= len; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4308 | if (cache->ro) { |
| 4309 | cache->space_info->bytes_readonly += len; |
| 4310 | } else if (cache->reserved_pinned > 0) { |
| 4311 | len = min(len, cache->reserved_pinned); |
| 4312 | cache->reserved_pinned -= len; |
| 4313 | cache->space_info->bytes_reserved += len; |
| 4314 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4315 | spin_unlock(&cache->lock); |
| 4316 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4317 | } |
| 4318 | |
| 4319 | if (cache) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4320 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4321 | return 0; |
| 4322 | } |
| 4323 | |
| 4324 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4325 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4326 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4327 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4328 | struct extent_io_tree *unpin; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4329 | struct btrfs_block_rsv *block_rsv; |
| 4330 | struct btrfs_block_rsv *next_rsv; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4331 | u64 start; |
| 4332 | u64 end; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4333 | int idx; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4334 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4335 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4336 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4337 | unpin = &fs_info->freed_extents[1]; |
| 4338 | else |
| 4339 | unpin = &fs_info->freed_extents[0]; |
| 4340 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4341 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4342 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4343 | EXTENT_DIRTY); |
| 4344 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4345 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4346 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 4347 | if (btrfs_test_opt(root, DISCARD)) |
| 4348 | ret = btrfs_discard_extent(root, start, |
| 4349 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4350 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4351 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4352 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4353 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4354 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4355 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4356 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 4357 | list_for_each_entry_safe(block_rsv, next_rsv, |
| 4358 | &fs_info->durable_block_rsv_list, list) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4359 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4360 | idx = trans->transid & 0x1; |
| 4361 | if (block_rsv->freed[idx] > 0) { |
| 4362 | block_rsv_add_bytes(block_rsv, |
| 4363 | block_rsv->freed[idx], 0); |
| 4364 | block_rsv->freed[idx] = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 4365 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4366 | if (atomic_read(&block_rsv->usage) == 0) { |
| 4367 | btrfs_block_rsv_release(root, block_rsv, (u64)-1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4368 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4369 | if (block_rsv->freed[0] == 0 && |
| 4370 | block_rsv->freed[1] == 0) { |
| 4371 | list_del_init(&block_rsv->list); |
| 4372 | kfree(block_rsv); |
| 4373 | } |
| 4374 | } else { |
| 4375 | btrfs_block_rsv_release(root, block_rsv, 0); |
| 4376 | } |
| 4377 | } |
| 4378 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 4379 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4380 | return 0; |
| 4381 | } |
| 4382 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4383 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4384 | struct btrfs_root *root, |
| 4385 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4386 | u64 root_objectid, u64 owner_objectid, |
| 4387 | u64 owner_offset, int refs_to_drop, |
| 4388 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4389 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4390 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4391 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4392 | struct btrfs_fs_info *info = root->fs_info; |
| 4393 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4394 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4395 | struct btrfs_extent_item *ei; |
| 4396 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4397 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4398 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4399 | int extent_slot = 0; |
| 4400 | int found_extent = 0; |
| 4401 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4402 | u32 item_size; |
| 4403 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4404 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4405 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4406 | if (!path) |
| 4407 | return -ENOMEM; |
| 4408 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4409 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4410 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4411 | |
| 4412 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4413 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4414 | |
| 4415 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4416 | bytenr, num_bytes, parent, |
| 4417 | root_objectid, owner_objectid, |
| 4418 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4419 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4420 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4421 | while (extent_slot >= 0) { |
| 4422 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4423 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4424 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4425 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4426 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4427 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4428 | found_extent = 1; |
| 4429 | break; |
| 4430 | } |
| 4431 | if (path->slots[0] - extent_slot > 5) |
| 4432 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4433 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4434 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4435 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4436 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4437 | if (found_extent && item_size < sizeof(*ei)) |
| 4438 | found_extent = 0; |
| 4439 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4440 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4441 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4442 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4443 | NULL, refs_to_drop, |
| 4444 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4445 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4446 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4447 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4448 | |
| 4449 | key.objectid = bytenr; |
| 4450 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4451 | key.offset = num_bytes; |
| 4452 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4453 | ret = btrfs_search_slot(trans, extent_root, |
| 4454 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4455 | if (ret) { |
| 4456 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4457 | ", was looking for %llu\n", ret, |
| 4458 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4459 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4460 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4461 | BUG_ON(ret); |
| 4462 | extent_slot = path->slots[0]; |
| 4463 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4464 | } else { |
| 4465 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4466 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4467 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4468 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4469 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4470 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4471 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4472 | (unsigned long long)owner_objectid, |
| 4473 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4474 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4475 | |
| 4476 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4477 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4478 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4479 | if (item_size < sizeof(*ei)) { |
| 4480 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4481 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4482 | owner_objectid, 0); |
| 4483 | BUG_ON(ret < 0); |
| 4484 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4485 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4486 | path->leave_spinning = 1; |
| 4487 | |
| 4488 | key.objectid = bytenr; |
| 4489 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4490 | key.offset = num_bytes; |
| 4491 | |
| 4492 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4493 | -1, 1); |
| 4494 | if (ret) { |
| 4495 | printk(KERN_ERR "umm, got %d back from search" |
| 4496 | ", was looking for %llu\n", ret, |
| 4497 | (unsigned long long)bytenr); |
| 4498 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4499 | } |
| 4500 | BUG_ON(ret); |
| 4501 | extent_slot = path->slots[0]; |
| 4502 | leaf = path->nodes[0]; |
| 4503 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4504 | } |
| 4505 | #endif |
| 4506 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4507 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4508 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4509 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4510 | struct btrfs_tree_block_info *bi; |
| 4511 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4512 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4513 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4514 | } |
| 4515 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4516 | refs = btrfs_extent_refs(leaf, ei); |
| 4517 | BUG_ON(refs < refs_to_drop); |
| 4518 | refs -= refs_to_drop; |
| 4519 | |
| 4520 | if (refs > 0) { |
| 4521 | if (extent_op) |
| 4522 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4523 | /* |
| 4524 | * In the case of inline back ref, reference count will |
| 4525 | * be updated by remove_extent_backref |
| 4526 | */ |
| 4527 | if (iref) { |
| 4528 | BUG_ON(!found_extent); |
| 4529 | } else { |
| 4530 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4531 | btrfs_mark_buffer_dirty(leaf); |
| 4532 | } |
| 4533 | if (found_extent) { |
| 4534 | ret = remove_extent_backref(trans, extent_root, path, |
| 4535 | iref, refs_to_drop, |
| 4536 | is_data); |
| 4537 | BUG_ON(ret); |
| 4538 | } |
| 4539 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4540 | if (found_extent) { |
| 4541 | BUG_ON(is_data && refs_to_drop != |
| 4542 | extent_data_ref_count(root, path, iref)); |
| 4543 | if (iref) { |
| 4544 | BUG_ON(path->slots[0] != extent_slot); |
| 4545 | } else { |
| 4546 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4547 | path->slots[0] = extent_slot; |
| 4548 | num_to_del = 2; |
| 4549 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4550 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4551 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4552 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4553 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4554 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4555 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4556 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4557 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4558 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4559 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4560 | } else { |
| 4561 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4562 | bytenr >> PAGE_CACHE_SHIFT, |
| 4563 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4564 | } |
| 4565 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4566 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4567 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4568 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4569 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4570 | return ret; |
| 4571 | } |
| 4572 | |
| 4573 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4574 | * 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] | 4575 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4576 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4577 | * removes it from the tree. |
| 4578 | */ |
| 4579 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4580 | struct btrfs_root *root, u64 bytenr) |
| 4581 | { |
| 4582 | struct btrfs_delayed_ref_head *head; |
| 4583 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4584 | struct btrfs_delayed_ref_node *ref; |
| 4585 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4586 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4587 | |
| 4588 | delayed_refs = &trans->transaction->delayed_refs; |
| 4589 | spin_lock(&delayed_refs->lock); |
| 4590 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4591 | if (!head) |
| 4592 | goto out; |
| 4593 | |
| 4594 | node = rb_prev(&head->node.rb_node); |
| 4595 | if (!node) |
| 4596 | goto out; |
| 4597 | |
| 4598 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4599 | |
| 4600 | /* there are still entries for this ref, we can't drop it */ |
| 4601 | if (ref->bytenr == bytenr) |
| 4602 | goto out; |
| 4603 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4604 | if (head->extent_op) { |
| 4605 | if (!head->must_insert_reserved) |
| 4606 | goto out; |
| 4607 | kfree(head->extent_op); |
| 4608 | head->extent_op = NULL; |
| 4609 | } |
| 4610 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4611 | /* |
| 4612 | * waiting for the lock here would deadlock. If someone else has it |
| 4613 | * locked they are already in the process of dropping it anyway |
| 4614 | */ |
| 4615 | if (!mutex_trylock(&head->mutex)) |
| 4616 | goto out; |
| 4617 | |
| 4618 | /* |
| 4619 | * at this point we have a head with no other entries. Go |
| 4620 | * ahead and process it. |
| 4621 | */ |
| 4622 | head->node.in_tree = 0; |
| 4623 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4624 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4625 | delayed_refs->num_entries--; |
| 4626 | |
| 4627 | /* |
| 4628 | * we don't take a ref on the node because we're removing it from the |
| 4629 | * tree, so we just steal the ref the tree was holding. |
| 4630 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4631 | delayed_refs->num_heads--; |
| 4632 | if (list_empty(&head->cluster)) |
| 4633 | delayed_refs->num_heads_ready--; |
| 4634 | |
| 4635 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4636 | spin_unlock(&delayed_refs->lock); |
| 4637 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4638 | BUG_ON(head->extent_op); |
| 4639 | if (head->must_insert_reserved) |
| 4640 | ret = 1; |
| 4641 | |
| 4642 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4643 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4644 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4645 | out: |
| 4646 | spin_unlock(&delayed_refs->lock); |
| 4647 | return 0; |
| 4648 | } |
| 4649 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4650 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4651 | struct btrfs_root *root, |
| 4652 | struct extent_buffer *buf, |
| 4653 | u64 parent, int last_ref) |
| 4654 | { |
| 4655 | struct btrfs_block_rsv *block_rsv; |
| 4656 | struct btrfs_block_group_cache *cache = NULL; |
| 4657 | int ret; |
| 4658 | |
| 4659 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4660 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4661 | parent, root->root_key.objectid, |
| 4662 | btrfs_header_level(buf), |
| 4663 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4664 | BUG_ON(ret); |
| 4665 | } |
| 4666 | |
| 4667 | if (!last_ref) |
| 4668 | return; |
| 4669 | |
| 4670 | block_rsv = get_block_rsv(trans, root); |
| 4671 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | 3bf84a5 | 2010-05-31 09:04:46 +0000 | [diff] [blame] | 4672 | if (block_rsv->space_info != cache->space_info) |
| 4673 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4674 | |
| 4675 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4676 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4677 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4678 | if (!ret) |
| 4679 | goto pin; |
| 4680 | } |
| 4681 | |
| 4682 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4683 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
| 4684 | goto pin; |
| 4685 | } |
| 4686 | |
| 4687 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4688 | |
| 4689 | btrfs_add_free_space(cache, buf->start, buf->len); |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4690 | ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4691 | if (ret == -EAGAIN) { |
| 4692 | /* block group became read-only */ |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4693 | btrfs_update_reserved_bytes(cache, buf->len, 0, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4694 | goto out; |
| 4695 | } |
| 4696 | |
| 4697 | ret = 1; |
| 4698 | spin_lock(&block_rsv->lock); |
| 4699 | if (block_rsv->reserved < block_rsv->size) { |
| 4700 | block_rsv->reserved += buf->len; |
| 4701 | ret = 0; |
| 4702 | } |
| 4703 | spin_unlock(&block_rsv->lock); |
| 4704 | |
| 4705 | if (ret) { |
| 4706 | spin_lock(&cache->space_info->lock); |
| 4707 | cache->space_info->bytes_reserved -= buf->len; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4708 | cache->space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4709 | spin_unlock(&cache->space_info->lock); |
| 4710 | } |
| 4711 | goto out; |
| 4712 | } |
| 4713 | pin: |
| 4714 | if (block_rsv->durable && !cache->ro) { |
| 4715 | ret = 0; |
| 4716 | spin_lock(&cache->lock); |
| 4717 | if (!cache->ro) { |
| 4718 | cache->reserved_pinned += buf->len; |
| 4719 | ret = 1; |
| 4720 | } |
| 4721 | spin_unlock(&cache->lock); |
| 4722 | |
| 4723 | if (ret) { |
| 4724 | spin_lock(&block_rsv->lock); |
| 4725 | block_rsv->freed[trans->transid & 0x1] += buf->len; |
| 4726 | spin_unlock(&block_rsv->lock); |
| 4727 | } |
| 4728 | } |
| 4729 | out: |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 4730 | /* |
| 4731 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 4732 | * anymore. |
| 4733 | */ |
| 4734 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4735 | btrfs_put_block_group(cache); |
| 4736 | } |
| 4737 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4738 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4739 | struct btrfs_root *root, |
| 4740 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4741 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4742 | { |
| 4743 | int ret; |
| 4744 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4745 | /* |
| 4746 | * tree log blocks never actually go into the extent allocation |
| 4747 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4748 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4749 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4750 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4751 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4752 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4753 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4754 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4755 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4756 | parent, root_objectid, (int)owner, |
| 4757 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4758 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4759 | } else { |
| 4760 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4761 | parent, root_objectid, owner, |
| 4762 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4763 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4764 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4765 | return ret; |
| 4766 | } |
| 4767 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4768 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4769 | { |
| 4770 | u64 mask = ((u64)root->stripesize - 1); |
| 4771 | u64 ret = (val + mask) & ~mask; |
| 4772 | return ret; |
| 4773 | } |
| 4774 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4775 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4776 | * when we wait for progress in the block group caching, its because |
| 4777 | * our allocation attempt failed at least once. So, we must sleep |
| 4778 | * and let some progress happen before we try again. |
| 4779 | * |
| 4780 | * This function will sleep at least once waiting for new free space to |
| 4781 | * show up, and then it will check the block group free space numbers |
| 4782 | * for our min num_bytes. Another option is to have it go ahead |
| 4783 | * and look in the rbtree for a free extent of a given size, but this |
| 4784 | * is a good start. |
| 4785 | */ |
| 4786 | static noinline int |
| 4787 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4788 | u64 num_bytes) |
| 4789 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4790 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4791 | DEFINE_WAIT(wait); |
| 4792 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4793 | caching_ctl = get_caching_control(cache); |
| 4794 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4795 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4796 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4797 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 4798 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4799 | |
| 4800 | put_caching_control(caching_ctl); |
| 4801 | return 0; |
| 4802 | } |
| 4803 | |
| 4804 | static noinline int |
| 4805 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4806 | { |
| 4807 | struct btrfs_caching_control *caching_ctl; |
| 4808 | DEFINE_WAIT(wait); |
| 4809 | |
| 4810 | caching_ctl = get_caching_control(cache); |
| 4811 | if (!caching_ctl) |
| 4812 | return 0; |
| 4813 | |
| 4814 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4815 | |
| 4816 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4817 | return 0; |
| 4818 | } |
| 4819 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4820 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4821 | { |
| 4822 | int index; |
| 4823 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4824 | index = 0; |
| 4825 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4826 | index = 1; |
| 4827 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4828 | index = 2; |
| 4829 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4830 | index = 3; |
| 4831 | else |
| 4832 | index = 4; |
| 4833 | return index; |
| 4834 | } |
| 4835 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4836 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4837 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4838 | LOOP_CACHING_NOWAIT = 1, |
| 4839 | LOOP_CACHING_WAIT = 2, |
| 4840 | LOOP_ALLOC_CHUNK = 3, |
| 4841 | LOOP_NO_EMPTY_SIZE = 4, |
| 4842 | }; |
| 4843 | |
| 4844 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4845 | * walks the btree of allocated extents and find a hole of a given size. |
| 4846 | * The key ins is changed to record the hole: |
| 4847 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4848 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4849 | * ins->offset == number of blocks |
| 4850 | * Any available blocks before search_start are skipped. |
| 4851 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4852 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4853 | struct btrfs_root *orig_root, |
| 4854 | u64 num_bytes, u64 empty_size, |
| 4855 | u64 search_start, u64 search_end, |
| 4856 | u64 hint_byte, struct btrfs_key *ins, |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 4857 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4858 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4859 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4860 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4861 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4862 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4863 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4864 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4865 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4866 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4867 | int last_ptr_loop = 0; |
| 4868 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4869 | int index = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4870 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4871 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4872 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4873 | bool use_cluster = true; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4874 | u64 ideal_cache_percent = 0; |
| 4875 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4876 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4877 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4878 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4879 | ins->objectid = 0; |
| 4880 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4881 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4882 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4883 | if (!space_info) { |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 4884 | printk(KERN_ERR "No space info for %llu\n", data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4885 | return -ENOSPC; |
| 4886 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4887 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4888 | /* |
| 4889 | * If the space info is for both data and metadata it means we have a |
| 4890 | * small filesystem and we can't use the clustering stuff. |
| 4891 | */ |
| 4892 | if (btrfs_mixed_space_info(space_info)) |
| 4893 | use_cluster = false; |
| 4894 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4895 | if (orig_root->ref_cows || empty_size) |
| 4896 | allowed_chunk_alloc = 1; |
| 4897 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4898 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4899 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4900 | if (!btrfs_test_opt(root, SSD)) |
| 4901 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4902 | } |
| 4903 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4904 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
| 4905 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4906 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4907 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4908 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4909 | if (last_ptr) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4910 | spin_lock(&last_ptr->lock); |
| 4911 | if (last_ptr->block_group) |
| 4912 | hint_byte = last_ptr->window_start; |
| 4913 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4914 | } |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4915 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4916 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4917 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4918 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4919 | if (!last_ptr) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4920 | empty_cluster = 0; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4921 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4922 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4923 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4924 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4925 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4926 | /* |
| 4927 | * we don't want to use the block group if it doesn't match our |
| 4928 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4929 | * |
| 4930 | * However if we are re-searching with an ideal block group |
| 4931 | * 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] | 4932 | */ |
| 4933 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4934 | (block_group->cached != BTRFS_CACHE_NO || |
| 4935 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4936 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4937 | if (list_empty(&block_group->list) || |
| 4938 | block_group->ro) { |
| 4939 | /* |
| 4940 | * someone is removing this block group, |
| 4941 | * we can't jump into the have_block_group |
| 4942 | * target because our list pointers are not |
| 4943 | * valid |
| 4944 | */ |
| 4945 | btrfs_put_block_group(block_group); |
| 4946 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4947 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4948 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4949 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4950 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4951 | } else if (block_group) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4952 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4953 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4954 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4955 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4956 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4957 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 4958 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4959 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4960 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4961 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4962 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4963 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4964 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4965 | /* |
| 4966 | * this can happen if we end up cycling through all the |
| 4967 | * raid types, but we want to make sure we only allocate |
| 4968 | * for the proper type. |
| 4969 | */ |
| 4970 | if (!block_group_bits(block_group, data)) { |
| 4971 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 4972 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4973 | BTRFS_BLOCK_GROUP_RAID10; |
| 4974 | |
| 4975 | /* |
| 4976 | * if they asked for extra copies and this block group |
| 4977 | * doesn't provide them, bail. This does allow us to |
| 4978 | * fill raid0 from raid1. |
| 4979 | */ |
| 4980 | if ((data & extra) && !(block_group->flags & extra)) |
| 4981 | goto loop; |
| 4982 | } |
| 4983 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4984 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4985 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4986 | u64 free_percent; |
| 4987 | |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4988 | ret = cache_block_group(block_group, trans, |
| 4989 | orig_root, 1); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4990 | if (block_group->cached == BTRFS_CACHE_FINISHED) |
| 4991 | goto have_block_group; |
| 4992 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4993 | free_percent = btrfs_block_group_used(&block_group->item); |
| 4994 | free_percent *= 100; |
| 4995 | free_percent = div64_u64(free_percent, |
| 4996 | block_group->key.offset); |
| 4997 | free_percent = 100 - free_percent; |
| 4998 | if (free_percent > ideal_cache_percent && |
| 4999 | likely(!block_group->ro)) { |
| 5000 | ideal_cache_offset = block_group->key.objectid; |
| 5001 | ideal_cache_percent = free_percent; |
| 5002 | } |
| 5003 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5004 | /* |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5005 | * The caching workers are limited to 2 threads, so we |
| 5006 | * can queue as much work as we care to. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5007 | */ |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5008 | if (loop > LOOP_FIND_IDEAL) { |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5009 | ret = cache_block_group(block_group, trans, |
| 5010 | orig_root, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5011 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5012 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5013 | found_uncached_bg = true; |
| 5014 | |
| 5015 | /* |
| 5016 | * If loop is set for cached only, try the next block |
| 5017 | * group. |
| 5018 | */ |
| 5019 | if (loop == LOOP_FIND_IDEAL) |
| 5020 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5021 | } |
| 5022 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5023 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5024 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5025 | found_uncached_bg = true; |
| 5026 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5027 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5028 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5029 | |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5030 | spin_lock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5031 | if (cached && |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5032 | block_group->free_space_ctl->free_space < |
| 5033 | num_bytes + empty_size) { |
| 5034 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5035 | goto loop; |
| 5036 | } |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5037 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5038 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5039 | /* |
| 5040 | * Ok we want to try and use the cluster allocator, so lets look |
| 5041 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 5042 | * have tried the cluster allocator plenty of times at this |
| 5043 | * point and not have found anything, so we are likely way too |
| 5044 | * fragmented for the clustering stuff to find anything, so lets |
| 5045 | * just skip it and let the allocator find whatever block it can |
| 5046 | * find |
| 5047 | */ |
| 5048 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5049 | /* |
| 5050 | * the refill lock keeps out other |
| 5051 | * people trying to start a new cluster |
| 5052 | */ |
| 5053 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5054 | if (last_ptr->block_group && |
| 5055 | (last_ptr->block_group->ro || |
| 5056 | !block_group_bits(last_ptr->block_group, data))) { |
| 5057 | offset = 0; |
| 5058 | goto refill_cluster; |
| 5059 | } |
| 5060 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5061 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 5062 | num_bytes, search_start); |
| 5063 | if (offset) { |
| 5064 | /* we have a block, we're done */ |
| 5065 | spin_unlock(&last_ptr->refill_lock); |
| 5066 | goto checks; |
| 5067 | } |
| 5068 | |
| 5069 | spin_lock(&last_ptr->lock); |
| 5070 | /* |
| 5071 | * whoops, this cluster doesn't actually point to |
| 5072 | * this block group. Get a ref on the block |
| 5073 | * group is does point to and try again |
| 5074 | */ |
| 5075 | if (!last_ptr_loop && last_ptr->block_group && |
| 5076 | last_ptr->block_group != block_group) { |
| 5077 | |
| 5078 | btrfs_put_block_group(block_group); |
| 5079 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5080 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5081 | spin_unlock(&last_ptr->lock); |
| 5082 | spin_unlock(&last_ptr->refill_lock); |
| 5083 | |
| 5084 | last_ptr_loop = 1; |
| 5085 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5086 | /* |
| 5087 | * we know this block group is properly |
| 5088 | * in the list because |
| 5089 | * btrfs_remove_block_group, drops the |
| 5090 | * cluster before it removes the block |
| 5091 | * group from the list |
| 5092 | */ |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5093 | goto have_block_group; |
| 5094 | } |
| 5095 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5096 | refill_cluster: |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5097 | /* |
| 5098 | * this cluster didn't work out, free it and |
| 5099 | * start over |
| 5100 | */ |
| 5101 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5102 | |
| 5103 | last_ptr_loop = 0; |
| 5104 | |
| 5105 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 5106 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5107 | block_group, last_ptr, |
| 5108 | offset, num_bytes, |
| 5109 | empty_cluster + empty_size); |
| 5110 | if (ret == 0) { |
| 5111 | /* |
| 5112 | * now pull our allocation out of this |
| 5113 | * cluster |
| 5114 | */ |
| 5115 | offset = btrfs_alloc_from_cluster(block_group, |
| 5116 | last_ptr, num_bytes, |
| 5117 | search_start); |
| 5118 | if (offset) { |
| 5119 | /* we found one, proceed */ |
| 5120 | spin_unlock(&last_ptr->refill_lock); |
| 5121 | goto checks; |
| 5122 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5123 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 5124 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5125 | spin_unlock(&last_ptr->refill_lock); |
| 5126 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5127 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5128 | wait_block_group_cache_progress(block_group, |
| 5129 | num_bytes + empty_cluster + empty_size); |
| 5130 | goto have_block_group; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5131 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5132 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5133 | /* |
| 5134 | * at this point we either didn't find a cluster |
| 5135 | * or we weren't able to allocate a block from our |
| 5136 | * cluster. Free the cluster we've been trying |
| 5137 | * to use, and go to the next block group |
| 5138 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5139 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5140 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5141 | goto loop; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5142 | } |
| 5143 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5144 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 5145 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5146 | /* |
| 5147 | * If we didn't find a chunk, and we haven't failed on this |
| 5148 | * block group before, and this block group is in the middle of |
| 5149 | * caching and we are ok with waiting, then go ahead and wait |
| 5150 | * for progress to be made, and set failed_alloc to true. |
| 5151 | * |
| 5152 | * If failed_alloc is true then we've already waited on this |
| 5153 | * block group once and should move on to the next block group. |
| 5154 | */ |
| 5155 | if (!offset && !failed_alloc && !cached && |
| 5156 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5157 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5158 | num_bytes + empty_size); |
| 5159 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5160 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5161 | } else if (!offset) { |
| 5162 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5163 | } |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5164 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5165 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5166 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5167 | if (search_start + num_bytes >= search_end) { |
| 5168 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5169 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5170 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 5171 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5172 | /* move on to the next group */ |
| 5173 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5174 | block_group->key.objectid + block_group->key.offset) { |
| 5175 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5176 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5177 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5178 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5179 | ins->objectid = search_start; |
| 5180 | ins->offset = num_bytes; |
| 5181 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5182 | if (offset < search_start) |
| 5183 | btrfs_add_free_space(block_group, offset, |
| 5184 | search_start - offset); |
| 5185 | BUG_ON(offset > search_start); |
| 5186 | |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5187 | ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5188 | (data & BTRFS_BLOCK_GROUP_DATA)); |
| 5189 | if (ret == -EAGAIN) { |
| 5190 | btrfs_add_free_space(block_group, offset, num_bytes); |
| 5191 | goto loop; |
| 5192 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5193 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5194 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5195 | ins->objectid = search_start; |
| 5196 | ins->offset = num_bytes; |
| 5197 | |
| 5198 | if (offset < search_start) |
| 5199 | btrfs_add_free_space(block_group, offset, |
| 5200 | search_start - offset); |
| 5201 | BUG_ON(offset > search_start); |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5202 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5203 | break; |
| 5204 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5205 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5206 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5207 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5208 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5209 | } |
| 5210 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 5211 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5212 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 5213 | goto search; |
| 5214 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5215 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 5216 | * for them to make caching progress. Also |
| 5217 | * determine the best possible bg to cache |
| 5218 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 5219 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5220 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 5221 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 5222 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 5223 | * again |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5224 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5225 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5226 | index = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5227 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5228 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5229 | loop++; |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5230 | if (!ideal_cache_percent) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5231 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5232 | |
| 5233 | /* |
| 5234 | * 1 of the following 2 things have happened so far |
| 5235 | * |
| 5236 | * 1) We found an ideal block group for caching that |
| 5237 | * is mostly full and will cache quickly, so we might |
| 5238 | * as well wait for it. |
| 5239 | * |
| 5240 | * 2) We searched for cached only and we didn't find |
| 5241 | * anything, and we didn't start any caching kthreads |
| 5242 | * either, so chances are we will loop through and |
| 5243 | * start a couple caching kthreads, and then come back |
| 5244 | * around and just wait for them. This will be slower |
| 5245 | * because we will have 2 caching kthreads reading at |
| 5246 | * the same time when we could have just started one |
| 5247 | * and waited for it to get far enough to give us an |
| 5248 | * allocation, so go ahead and go to the wait caching |
| 5249 | * loop. |
| 5250 | */ |
| 5251 | loop = LOOP_CACHING_WAIT; |
| 5252 | search_start = ideal_cache_offset; |
| 5253 | ideal_cache_percent = 0; |
| 5254 | goto ideal_cache; |
| 5255 | } else if (loop == LOOP_FIND_IDEAL) { |
| 5256 | /* |
| 5257 | * Didn't find a uncached bg, wait on anything we find |
| 5258 | * next. |
| 5259 | */ |
| 5260 | loop = LOOP_CACHING_WAIT; |
| 5261 | goto search; |
| 5262 | } |
| 5263 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5264 | loop++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5265 | |
| 5266 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5267 | if (allowed_chunk_alloc) { |
| 5268 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 5269 | 2 * 1024 * 1024, data, |
| 5270 | CHUNK_ALLOC_LIMITED); |
| 5271 | allowed_chunk_alloc = 0; |
| 5272 | if (ret == 1) |
| 5273 | done_chunk_alloc = 1; |
| 5274 | } else if (!done_chunk_alloc && |
| 5275 | space_info->force_alloc == |
| 5276 | CHUNK_ALLOC_NO_FORCE) { |
| 5277 | space_info->force_alloc = CHUNK_ALLOC_LIMITED; |
| 5278 | } |
| 5279 | |
| 5280 | /* |
| 5281 | * We didn't allocate a chunk, go ahead and drop the |
| 5282 | * empty size and loop again. |
| 5283 | */ |
| 5284 | if (!done_chunk_alloc) |
| 5285 | loop = LOOP_NO_EMPTY_SIZE; |
| 5286 | } |
| 5287 | |
| 5288 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5289 | empty_size = 0; |
| 5290 | empty_cluster = 0; |
| 5291 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5292 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5293 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5294 | } else if (!ins->objectid) { |
| 5295 | ret = -ENOSPC; |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5296 | } else if (ins->objectid) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5297 | ret = 0; |
| 5298 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5299 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5300 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5301 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5302 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5303 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 5304 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5305 | { |
| 5306 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5307 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5308 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5309 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5310 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 5311 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5312 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5313 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5314 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5315 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 5316 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 5317 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5318 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5319 | (unsigned long long)info->bytes_pinned, |
| 5320 | (unsigned long long)info->bytes_reserved, |
| 5321 | (unsigned long long)info->bytes_may_use, |
| 5322 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5323 | spin_unlock(&info->lock); |
| 5324 | |
| 5325 | if (!dump_block_groups) |
| 5326 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5327 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5328 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5329 | again: |
| 5330 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5331 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5332 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 5333 | "%llu pinned %llu reserved\n", |
| 5334 | (unsigned long long)cache->key.objectid, |
| 5335 | (unsigned long long)cache->key.offset, |
| 5336 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 5337 | (unsigned long long)cache->pinned, |
| 5338 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5339 | btrfs_dump_free_space(cache, bytes); |
| 5340 | spin_unlock(&cache->lock); |
| 5341 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5342 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5343 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5344 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5345 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5346 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5347 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5348 | struct btrfs_root *root, |
| 5349 | u64 num_bytes, u64 min_alloc_size, |
| 5350 | u64 empty_size, u64 hint_byte, |
| 5351 | u64 search_end, struct btrfs_key *ins, |
| 5352 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5353 | { |
| 5354 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5355 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5356 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5357 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5358 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5359 | /* |
| 5360 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5361 | * is not called recursively on allocations |
| 5362 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5363 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5364 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5365 | num_bytes + 2 * 1024 * 1024, data, |
| 5366 | CHUNK_ALLOC_NO_FORCE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5367 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5368 | WARN_ON(num_bytes < root->sectorsize); |
| 5369 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5370 | search_start, search_end, hint_byte, |
| 5371 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5372 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5373 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5374 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5375 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5376 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5377 | do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5378 | num_bytes, data, CHUNK_ALLOC_FORCE); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5379 | goto again; |
| 5380 | } |
Chris Mason | 9143565 | 2011-02-16 13:10:41 -0500 | [diff] [blame] | 5381 | if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5382 | struct btrfs_space_info *sinfo; |
| 5383 | |
| 5384 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5385 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5386 | "wanted %llu\n", (unsigned long long)data, |
| 5387 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5388 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5389 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5390 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5391 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
| 5392 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5393 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5394 | } |
| 5395 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5396 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 5397 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5398 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5399 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5400 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5401 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5402 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5403 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5404 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5405 | return -ENOSPC; |
| 5406 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5407 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5408 | if (btrfs_test_opt(root, DISCARD)) |
| 5409 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5410 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5411 | btrfs_add_free_space(cache, start, len); |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5412 | btrfs_update_reserved_bytes(cache, len, 0, 1); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5413 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5414 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5415 | trace_btrfs_reserved_extent_free(root, start, len); |
| 5416 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5417 | return ret; |
| 5418 | } |
| 5419 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5420 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5421 | struct btrfs_root *root, |
| 5422 | u64 parent, u64 root_objectid, |
| 5423 | u64 flags, u64 owner, u64 offset, |
| 5424 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5425 | { |
| 5426 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5427 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5428 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5429 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5430 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5431 | struct extent_buffer *leaf; |
| 5432 | int type; |
| 5433 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5434 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5435 | if (parent > 0) |
| 5436 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5437 | else |
| 5438 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5439 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5440 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5441 | |
| 5442 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5443 | if (!path) |
| 5444 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5445 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5446 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5447 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5448 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5449 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5450 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5451 | leaf = path->nodes[0]; |
| 5452 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5453 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5454 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5455 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5456 | btrfs_set_extent_flags(leaf, extent_item, |
| 5457 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5458 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5459 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5460 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5461 | if (parent > 0) { |
| 5462 | struct btrfs_shared_data_ref *ref; |
| 5463 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5464 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5465 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5466 | } else { |
| 5467 | struct btrfs_extent_data_ref *ref; |
| 5468 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5469 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5470 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5471 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5472 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5473 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5474 | |
| 5475 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5476 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5477 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5478 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5479 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5480 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5481 | "%llu\n", (unsigned long long)ins->objectid, |
| 5482 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5483 | BUG(); |
| 5484 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5485 | return ret; |
| 5486 | } |
| 5487 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5488 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5489 | struct btrfs_root *root, |
| 5490 | u64 parent, u64 root_objectid, |
| 5491 | u64 flags, struct btrfs_disk_key *key, |
| 5492 | int level, struct btrfs_key *ins) |
| 5493 | { |
| 5494 | int ret; |
| 5495 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5496 | struct btrfs_extent_item *extent_item; |
| 5497 | struct btrfs_tree_block_info *block_info; |
| 5498 | struct btrfs_extent_inline_ref *iref; |
| 5499 | struct btrfs_path *path; |
| 5500 | struct extent_buffer *leaf; |
| 5501 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5502 | |
| 5503 | path = btrfs_alloc_path(); |
| 5504 | BUG_ON(!path); |
| 5505 | |
| 5506 | path->leave_spinning = 1; |
| 5507 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5508 | ins, size); |
| 5509 | BUG_ON(ret); |
| 5510 | |
| 5511 | leaf = path->nodes[0]; |
| 5512 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5513 | struct btrfs_extent_item); |
| 5514 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5515 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5516 | btrfs_set_extent_flags(leaf, extent_item, |
| 5517 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5518 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5519 | |
| 5520 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5521 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5522 | |
| 5523 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5524 | if (parent > 0) { |
| 5525 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5526 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5527 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5528 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5529 | } else { |
| 5530 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5531 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5532 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5533 | } |
| 5534 | |
| 5535 | btrfs_mark_buffer_dirty(leaf); |
| 5536 | btrfs_free_path(path); |
| 5537 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5538 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5539 | if (ret) { |
| 5540 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5541 | "%llu\n", (unsigned long long)ins->objectid, |
| 5542 | (unsigned long long)ins->offset); |
| 5543 | BUG(); |
| 5544 | } |
| 5545 | return ret; |
| 5546 | } |
| 5547 | |
| 5548 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5549 | struct btrfs_root *root, |
| 5550 | u64 root_objectid, u64 owner, |
| 5551 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5552 | { |
| 5553 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5554 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5555 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5556 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5557 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5558 | 0, root_objectid, owner, offset, |
| 5559 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5560 | return ret; |
| 5561 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5562 | |
| 5563 | /* |
| 5564 | * this is used by the tree logging recovery code. It records that |
| 5565 | * an extent has been allocated and makes sure to clear the free |
| 5566 | * space cache bits as well |
| 5567 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5568 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5569 | struct btrfs_root *root, |
| 5570 | u64 root_objectid, u64 owner, u64 offset, |
| 5571 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5572 | { |
| 5573 | int ret; |
| 5574 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5575 | struct btrfs_caching_control *caching_ctl; |
| 5576 | u64 start = ins->objectid; |
| 5577 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5578 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5579 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5580 | cache_block_group(block_group, trans, NULL, 0); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5581 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5582 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5583 | if (!caching_ctl) { |
| 5584 | BUG_ON(!block_group_cache_done(block_group)); |
| 5585 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5586 | BUG_ON(ret); |
| 5587 | } else { |
| 5588 | mutex_lock(&caching_ctl->mutex); |
| 5589 | |
| 5590 | if (start >= caching_ctl->progress) { |
| 5591 | ret = add_excluded_extent(root, start, num_bytes); |
| 5592 | BUG_ON(ret); |
| 5593 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5594 | ret = btrfs_remove_free_space(block_group, |
| 5595 | start, num_bytes); |
| 5596 | BUG_ON(ret); |
| 5597 | } else { |
| 5598 | num_bytes = caching_ctl->progress - start; |
| 5599 | ret = btrfs_remove_free_space(block_group, |
| 5600 | start, num_bytes); |
| 5601 | BUG_ON(ret); |
| 5602 | |
| 5603 | start = caching_ctl->progress; |
| 5604 | num_bytes = ins->objectid + ins->offset - |
| 5605 | caching_ctl->progress; |
| 5606 | ret = add_excluded_extent(root, start, num_bytes); |
| 5607 | BUG_ON(ret); |
| 5608 | } |
| 5609 | |
| 5610 | mutex_unlock(&caching_ctl->mutex); |
| 5611 | put_caching_control(caching_ctl); |
| 5612 | } |
| 5613 | |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5614 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5615 | BUG_ON(ret); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5616 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5617 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5618 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5619 | return ret; |
| 5620 | } |
| 5621 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5622 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5623 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5624 | u64 bytenr, u32 blocksize, |
| 5625 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5626 | { |
| 5627 | struct extent_buffer *buf; |
| 5628 | |
| 5629 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5630 | if (!buf) |
| 5631 | return ERR_PTR(-ENOMEM); |
| 5632 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 5633 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5634 | btrfs_tree_lock(buf); |
| 5635 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5636 | |
| 5637 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5638 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5639 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5640 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5641 | /* |
| 5642 | * we allow two log transactions at a time, use different |
| 5643 | * EXENT bit to differentiate dirty pages. |
| 5644 | */ |
| 5645 | if (root->log_transid % 2 == 0) |
| 5646 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5647 | buf->start + buf->len - 1, GFP_NOFS); |
| 5648 | else |
| 5649 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5650 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5651 | } else { |
| 5652 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5653 | buf->start + buf->len - 1, GFP_NOFS); |
| 5654 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5655 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5656 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5657 | return buf; |
| 5658 | } |
| 5659 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5660 | static struct btrfs_block_rsv * |
| 5661 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5662 | struct btrfs_root *root, u32 blocksize) |
| 5663 | { |
| 5664 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5665 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5666 | int ret; |
| 5667 | |
| 5668 | block_rsv = get_block_rsv(trans, root); |
| 5669 | |
| 5670 | if (block_rsv->size == 0) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5671 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 5672 | blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5673 | /* |
| 5674 | * If we couldn't reserve metadata bytes try and use some from |
| 5675 | * the global reserve. |
| 5676 | */ |
| 5677 | if (ret && block_rsv != global_rsv) { |
| 5678 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5679 | if (!ret) |
| 5680 | return global_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5681 | return ERR_PTR(ret); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5682 | } else if (ret) { |
| 5683 | return ERR_PTR(ret); |
| 5684 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5685 | return block_rsv; |
| 5686 | } |
| 5687 | |
| 5688 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5689 | if (!ret) |
| 5690 | return block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5691 | if (ret) { |
| 5692 | WARN_ON(1); |
| 5693 | ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize, |
| 5694 | 0); |
| 5695 | if (!ret) { |
| 5696 | spin_lock(&block_rsv->lock); |
| 5697 | block_rsv->size += blocksize; |
| 5698 | spin_unlock(&block_rsv->lock); |
| 5699 | return block_rsv; |
| 5700 | } else if (ret && block_rsv != global_rsv) { |
| 5701 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5702 | if (!ret) |
| 5703 | return global_rsv; |
| 5704 | } |
| 5705 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5706 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5707 | return ERR_PTR(-ENOSPC); |
| 5708 | } |
| 5709 | |
| 5710 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5711 | { |
| 5712 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5713 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5714 | } |
| 5715 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5716 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5717 | * finds a free extent and does all the dirty work required for allocation |
| 5718 | * returns the key for the extent through ins, and a tree buffer for |
| 5719 | * the first block of the extent through buf. |
| 5720 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5721 | * returns the tree buffer or NULL. |
| 5722 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5723 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5724 | struct btrfs_root *root, u32 blocksize, |
| 5725 | u64 parent, u64 root_objectid, |
| 5726 | struct btrfs_disk_key *key, int level, |
| 5727 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5728 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5729 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5730 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5731 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5732 | u64 flags = 0; |
| 5733 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5734 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5735 | |
| 5736 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5737 | if (IS_ERR(block_rsv)) |
| 5738 | return ERR_CAST(block_rsv); |
| 5739 | |
| 5740 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5741 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5742 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5743 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5744 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5745 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5746 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5747 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5748 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5749 | BUG_ON(IS_ERR(buf)); |
| 5750 | |
| 5751 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5752 | if (parent == 0) |
| 5753 | parent = ins.objectid; |
| 5754 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5755 | } else |
| 5756 | BUG_ON(parent > 0); |
| 5757 | |
| 5758 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5759 | struct btrfs_delayed_extent_op *extent_op; |
| 5760 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5761 | BUG_ON(!extent_op); |
| 5762 | if (key) |
| 5763 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5764 | else |
| 5765 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5766 | extent_op->flags_to_set = flags; |
| 5767 | extent_op->update_key = 1; |
| 5768 | extent_op->update_flags = 1; |
| 5769 | extent_op->is_data = 0; |
| 5770 | |
| 5771 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5772 | ins.offset, parent, root_objectid, |
| 5773 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5774 | extent_op); |
| 5775 | BUG_ON(ret); |
| 5776 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5777 | return buf; |
| 5778 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5779 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5780 | struct walk_control { |
| 5781 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5782 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5783 | struct btrfs_key update_progress; |
| 5784 | int stage; |
| 5785 | int level; |
| 5786 | int shared_level; |
| 5787 | int update_ref; |
| 5788 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5789 | int reada_slot; |
| 5790 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5791 | }; |
| 5792 | |
| 5793 | #define DROP_REFERENCE 1 |
| 5794 | #define UPDATE_BACKREF 2 |
| 5795 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5796 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5797 | struct btrfs_root *root, |
| 5798 | struct walk_control *wc, |
| 5799 | struct btrfs_path *path) |
| 5800 | { |
| 5801 | u64 bytenr; |
| 5802 | u64 generation; |
| 5803 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5804 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5805 | u32 nritems; |
| 5806 | u32 blocksize; |
| 5807 | struct btrfs_key key; |
| 5808 | struct extent_buffer *eb; |
| 5809 | int ret; |
| 5810 | int slot; |
| 5811 | int nread = 0; |
| 5812 | |
| 5813 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5814 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5815 | wc->reada_count = max(wc->reada_count, 2); |
| 5816 | } else { |
| 5817 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5818 | wc->reada_count = min_t(int, wc->reada_count, |
| 5819 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5820 | } |
| 5821 | |
| 5822 | eb = path->nodes[wc->level]; |
| 5823 | nritems = btrfs_header_nritems(eb); |
| 5824 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5825 | |
| 5826 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5827 | if (nread >= wc->reada_count) |
| 5828 | break; |
| 5829 | |
| 5830 | cond_resched(); |
| 5831 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5832 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5833 | |
| 5834 | if (slot == path->slots[wc->level]) |
| 5835 | goto reada; |
| 5836 | |
| 5837 | if (wc->stage == UPDATE_BACKREF && |
| 5838 | generation <= root->root_key.offset) |
| 5839 | continue; |
| 5840 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5841 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5842 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5843 | &refs, &flags); |
| 5844 | BUG_ON(ret); |
| 5845 | BUG_ON(refs == 0); |
| 5846 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5847 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5848 | if (refs == 1) |
| 5849 | goto reada; |
| 5850 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5851 | if (wc->level == 1 && |
| 5852 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5853 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5854 | if (!wc->update_ref || |
| 5855 | generation <= root->root_key.offset) |
| 5856 | continue; |
| 5857 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5858 | ret = btrfs_comp_cpu_keys(&key, |
| 5859 | &wc->update_progress); |
| 5860 | if (ret < 0) |
| 5861 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5862 | } else { |
| 5863 | if (wc->level == 1 && |
| 5864 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5865 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5866 | } |
| 5867 | reada: |
| 5868 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5869 | generation); |
| 5870 | if (ret) |
| 5871 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5872 | nread++; |
| 5873 | } |
| 5874 | wc->reada_slot = slot; |
| 5875 | } |
| 5876 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5877 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5878 | * hepler to process tree block while walking down the tree. |
| 5879 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5880 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5881 | * back refs for pointers in the block. |
| 5882 | * |
| 5883 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5884 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5885 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5886 | struct btrfs_root *root, |
| 5887 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5888 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5889 | { |
| 5890 | int level = wc->level; |
| 5891 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5892 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5893 | int ret; |
| 5894 | |
| 5895 | if (wc->stage == UPDATE_BACKREF && |
| 5896 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5897 | return 1; |
| 5898 | |
| 5899 | /* |
| 5900 | * when reference count of tree block is 1, it won't increase |
| 5901 | * again. once full backref flag is set, we never clear it. |
| 5902 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5903 | if (lookup_info && |
| 5904 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5905 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5906 | BUG_ON(!path->locks[level]); |
| 5907 | ret = btrfs_lookup_extent_info(trans, root, |
| 5908 | eb->start, eb->len, |
| 5909 | &wc->refs[level], |
| 5910 | &wc->flags[level]); |
| 5911 | BUG_ON(ret); |
| 5912 | BUG_ON(wc->refs[level] == 0); |
| 5913 | } |
| 5914 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5915 | if (wc->stage == DROP_REFERENCE) { |
| 5916 | if (wc->refs[level] > 1) |
| 5917 | return 1; |
| 5918 | |
| 5919 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5920 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5921 | path->locks[level] = 0; |
| 5922 | } |
| 5923 | return 0; |
| 5924 | } |
| 5925 | |
| 5926 | /* wc->stage == UPDATE_BACKREF */ |
| 5927 | if (!(wc->flags[level] & flag)) { |
| 5928 | BUG_ON(!path->locks[level]); |
| 5929 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5930 | BUG_ON(ret); |
| 5931 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5932 | BUG_ON(ret); |
| 5933 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5934 | eb->len, flag, 0); |
| 5935 | BUG_ON(ret); |
| 5936 | wc->flags[level] |= flag; |
| 5937 | } |
| 5938 | |
| 5939 | /* |
| 5940 | * the block is shared by multiple trees, so it's not good to |
| 5941 | * keep the tree lock |
| 5942 | */ |
| 5943 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5944 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5945 | path->locks[level] = 0; |
| 5946 | } |
| 5947 | return 0; |
| 5948 | } |
| 5949 | |
| 5950 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5951 | * hepler to process tree block pointer. |
| 5952 | * |
| 5953 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5954 | * reference count of the block pointed to. if the block |
| 5955 | * is shared and we need update back refs for the subtree |
| 5956 | * rooted at the block, this function changes wc->stage to |
| 5957 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5958 | * need to update back, this function drops the reference |
| 5959 | * to the block. |
| 5960 | * |
| 5961 | * NOTE: return value 1 means we should stop walking down. |
| 5962 | */ |
| 5963 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5964 | struct btrfs_root *root, |
| 5965 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5966 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5967 | { |
| 5968 | u64 bytenr; |
| 5969 | u64 generation; |
| 5970 | u64 parent; |
| 5971 | u32 blocksize; |
| 5972 | struct btrfs_key key; |
| 5973 | struct extent_buffer *next; |
| 5974 | int level = wc->level; |
| 5975 | int reada = 0; |
| 5976 | int ret = 0; |
| 5977 | |
| 5978 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5979 | path->slots[level]); |
| 5980 | /* |
| 5981 | * if the lower level block was created before the snapshot |
| 5982 | * was created, we know there is no need to update back refs |
| 5983 | * for the subtree |
| 5984 | */ |
| 5985 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5986 | generation <= root->root_key.offset) { |
| 5987 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5988 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5989 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5990 | |
| 5991 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5992 | blocksize = btrfs_level_size(root, level - 1); |
| 5993 | |
| 5994 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5995 | if (!next) { |
| 5996 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51 | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5997 | if (!next) |
| 5998 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5999 | reada = 1; |
| 6000 | } |
| 6001 | btrfs_tree_lock(next); |
| 6002 | btrfs_set_lock_blocking(next); |
| 6003 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6004 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6005 | &wc->refs[level - 1], |
| 6006 | &wc->flags[level - 1]); |
| 6007 | BUG_ON(ret); |
| 6008 | BUG_ON(wc->refs[level - 1] == 0); |
| 6009 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6010 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6011 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6012 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6013 | if (level == 1 && |
| 6014 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6015 | goto skip; |
| 6016 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6017 | if (!wc->update_ref || |
| 6018 | generation <= root->root_key.offset) |
| 6019 | goto skip; |
| 6020 | |
| 6021 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 6022 | path->slots[level]); |
| 6023 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 6024 | if (ret < 0) |
| 6025 | goto skip; |
| 6026 | |
| 6027 | wc->stage = UPDATE_BACKREF; |
| 6028 | wc->shared_level = level - 1; |
| 6029 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6030 | } else { |
| 6031 | if (level == 1 && |
| 6032 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6033 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6034 | } |
| 6035 | |
| 6036 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 6037 | btrfs_tree_unlock(next); |
| 6038 | free_extent_buffer(next); |
| 6039 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6040 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6041 | } |
| 6042 | |
| 6043 | if (!next) { |
| 6044 | if (reada && level == 1) |
| 6045 | reada_walk_down(trans, root, wc, path); |
| 6046 | next = read_tree_block(root, bytenr, blocksize, generation); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 6047 | if (!next) |
| 6048 | return -EIO; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6049 | btrfs_tree_lock(next); |
| 6050 | btrfs_set_lock_blocking(next); |
| 6051 | } |
| 6052 | |
| 6053 | level--; |
| 6054 | BUG_ON(level != btrfs_header_level(next)); |
| 6055 | path->nodes[level] = next; |
| 6056 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6057 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6058 | wc->level = level; |
| 6059 | if (wc->level == 1) |
| 6060 | wc->reada_slot = 0; |
| 6061 | return 0; |
| 6062 | skip: |
| 6063 | wc->refs[level - 1] = 0; |
| 6064 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6065 | if (wc->stage == DROP_REFERENCE) { |
| 6066 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6067 | parent = path->nodes[level]->start; |
| 6068 | } else { |
| 6069 | BUG_ON(root->root_key.objectid != |
| 6070 | btrfs_header_owner(path->nodes[level])); |
| 6071 | parent = 0; |
| 6072 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6073 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6074 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 6075 | root->root_key.objectid, level - 1, 0); |
| 6076 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6077 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6078 | btrfs_tree_unlock(next); |
| 6079 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6080 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6081 | return 1; |
| 6082 | } |
| 6083 | |
| 6084 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6085 | * hepler to process tree block while walking up the tree. |
| 6086 | * |
| 6087 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6088 | * reference count on the block. |
| 6089 | * |
| 6090 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6091 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6092 | * to UPDATE_BACKREF previously while processing the block. |
| 6093 | * |
| 6094 | * NOTE: return value 1 means we should stop walking up. |
| 6095 | */ |
| 6096 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6097 | struct btrfs_root *root, |
| 6098 | struct btrfs_path *path, |
| 6099 | struct walk_control *wc) |
| 6100 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6101 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6102 | int level = wc->level; |
| 6103 | struct extent_buffer *eb = path->nodes[level]; |
| 6104 | u64 parent = 0; |
| 6105 | |
| 6106 | if (wc->stage == UPDATE_BACKREF) { |
| 6107 | BUG_ON(wc->shared_level < level); |
| 6108 | if (level < wc->shared_level) |
| 6109 | goto out; |
| 6110 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6111 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6112 | if (ret > 0) |
| 6113 | wc->update_ref = 0; |
| 6114 | |
| 6115 | wc->stage = DROP_REFERENCE; |
| 6116 | wc->shared_level = -1; |
| 6117 | path->slots[level] = 0; |
| 6118 | |
| 6119 | /* |
| 6120 | * check reference count again if the block isn't locked. |
| 6121 | * we should start walking down the tree again if reference |
| 6122 | * count is one. |
| 6123 | */ |
| 6124 | if (!path->locks[level]) { |
| 6125 | BUG_ON(level == 0); |
| 6126 | btrfs_tree_lock(eb); |
| 6127 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6128 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6129 | |
| 6130 | ret = btrfs_lookup_extent_info(trans, root, |
| 6131 | eb->start, eb->len, |
| 6132 | &wc->refs[level], |
| 6133 | &wc->flags[level]); |
| 6134 | BUG_ON(ret); |
| 6135 | BUG_ON(wc->refs[level] == 0); |
| 6136 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6137 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6138 | return 1; |
| 6139 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6140 | } |
| 6141 | } |
| 6142 | |
| 6143 | /* wc->stage == DROP_REFERENCE */ |
| 6144 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6145 | |
| 6146 | if (wc->refs[level] == 1) { |
| 6147 | if (level == 0) { |
| 6148 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6149 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 6150 | else |
| 6151 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6152 | BUG_ON(ret); |
| 6153 | } |
| 6154 | /* make block locked assertion in clean_tree_block happy */ |
| 6155 | if (!path->locks[level] && |
| 6156 | btrfs_header_generation(eb) == trans->transid) { |
| 6157 | btrfs_tree_lock(eb); |
| 6158 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6159 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6160 | } |
| 6161 | clean_tree_block(trans, root, eb); |
| 6162 | } |
| 6163 | |
| 6164 | if (eb == root->node) { |
| 6165 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6166 | parent = eb->start; |
| 6167 | else |
| 6168 | BUG_ON(root->root_key.objectid != |
| 6169 | btrfs_header_owner(eb)); |
| 6170 | } else { |
| 6171 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6172 | parent = path->nodes[level + 1]->start; |
| 6173 | else |
| 6174 | BUG_ON(root->root_key.objectid != |
| 6175 | btrfs_header_owner(path->nodes[level + 1])); |
| 6176 | } |
| 6177 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6178 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6179 | out: |
| 6180 | wc->refs[level] = 0; |
| 6181 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6182 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6183 | } |
| 6184 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6185 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6186 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6187 | struct btrfs_path *path, |
| 6188 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6189 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6190 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6191 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6192 | int ret; |
| 6193 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6194 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6195 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6196 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6197 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6198 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6199 | if (level == 0) |
| 6200 | break; |
| 6201 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6202 | if (path->slots[level] >= |
| 6203 | btrfs_header_nritems(path->nodes[level])) |
| 6204 | break; |
| 6205 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6206 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6207 | if (ret > 0) { |
| 6208 | path->slots[level]++; |
| 6209 | continue; |
Miao Xie | 90d2c51 | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6210 | } else if (ret < 0) |
| 6211 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6212 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6213 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6214 | return 0; |
| 6215 | } |
| 6216 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6217 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6218 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6219 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6220 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6221 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6222 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6223 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6224 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6225 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6226 | while (level < max_level && path->nodes[level]) { |
| 6227 | wc->level = level; |
| 6228 | if (path->slots[level] + 1 < |
| 6229 | btrfs_header_nritems(path->nodes[level])) { |
| 6230 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6231 | return 0; |
| 6232 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6233 | ret = walk_up_proc(trans, root, path, wc); |
| 6234 | if (ret > 0) |
| 6235 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6236 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6237 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6238 | btrfs_tree_unlock_rw(path->nodes[level], |
| 6239 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6240 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6241 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6242 | free_extent_buffer(path->nodes[level]); |
| 6243 | path->nodes[level] = NULL; |
| 6244 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6245 | } |
| 6246 | } |
| 6247 | return 1; |
| 6248 | } |
| 6249 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6250 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6251 | * drop a subvolume tree. |
| 6252 | * |
| 6253 | * this function traverses the tree freeing any blocks that only |
| 6254 | * referenced by the tree. |
| 6255 | * |
| 6256 | * when a shared tree block is found. this function decreases its |
| 6257 | * reference count by one. if update_ref is true, this function |
| 6258 | * also make sure backrefs for the shared block and all lower level |
| 6259 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6260 | */ |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6261 | int btrfs_drop_snapshot(struct btrfs_root *root, |
| 6262 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6263 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6264 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6265 | struct btrfs_trans_handle *trans; |
| 6266 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6267 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6268 | struct walk_control *wc; |
| 6269 | struct btrfs_key key; |
| 6270 | int err = 0; |
| 6271 | int ret; |
| 6272 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6273 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6274 | path = btrfs_alloc_path(); |
| 6275 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6276 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6277 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 6278 | BUG_ON(!wc); |
| 6279 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6280 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6281 | BUG_ON(IS_ERR(trans)); |
| 6282 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6283 | if (block_rsv) |
| 6284 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6285 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6286 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6287 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6288 | path->nodes[level] = btrfs_lock_root_node(root); |
| 6289 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6290 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6291 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6292 | memset(&wc->update_progress, 0, |
| 6293 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6294 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6295 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6296 | memcpy(&wc->update_progress, &key, |
| 6297 | sizeof(wc->update_progress)); |
| 6298 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6299 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6300 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6301 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6302 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6303 | path->lowest_level = 0; |
| 6304 | if (ret < 0) { |
| 6305 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6306 | goto out; |
| 6307 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6308 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6309 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6310 | /* |
| 6311 | * unlock our path, this is safe because only this |
| 6312 | * function is allowed to delete this snapshot |
| 6313 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6314 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6315 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6316 | level = btrfs_header_level(root->node); |
| 6317 | while (1) { |
| 6318 | btrfs_tree_lock(path->nodes[level]); |
| 6319 | btrfs_set_lock_blocking(path->nodes[level]); |
| 6320 | |
| 6321 | ret = btrfs_lookup_extent_info(trans, root, |
| 6322 | path->nodes[level]->start, |
| 6323 | path->nodes[level]->len, |
| 6324 | &wc->refs[level], |
| 6325 | &wc->flags[level]); |
| 6326 | BUG_ON(ret); |
| 6327 | BUG_ON(wc->refs[level] == 0); |
| 6328 | |
| 6329 | if (level == root_item->drop_level) |
| 6330 | break; |
| 6331 | |
| 6332 | btrfs_tree_unlock(path->nodes[level]); |
| 6333 | WARN_ON(wc->refs[level] != 1); |
| 6334 | level--; |
| 6335 | } |
| 6336 | } |
| 6337 | |
| 6338 | wc->level = level; |
| 6339 | wc->shared_level = -1; |
| 6340 | wc->stage = DROP_REFERENCE; |
| 6341 | wc->update_ref = update_ref; |
| 6342 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6343 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6344 | |
| 6345 | while (1) { |
| 6346 | ret = walk_down_tree(trans, root, path, wc); |
| 6347 | if (ret < 0) { |
| 6348 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6349 | break; |
| 6350 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6351 | |
| 6352 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6353 | if (ret < 0) { |
| 6354 | err = ret; |
| 6355 | break; |
| 6356 | } |
| 6357 | |
| 6358 | if (ret > 0) { |
| 6359 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6360 | break; |
| 6361 | } |
| 6362 | |
| 6363 | if (wc->stage == DROP_REFERENCE) { |
| 6364 | level = wc->level; |
| 6365 | btrfs_node_key(path->nodes[level], |
| 6366 | &root_item->drop_progress, |
| 6367 | path->slots[level]); |
| 6368 | root_item->drop_level = level; |
| 6369 | } |
| 6370 | |
| 6371 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6372 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6373 | ret = btrfs_update_root(trans, tree_root, |
| 6374 | &root->root_key, |
| 6375 | root_item); |
| 6376 | BUG_ON(ret); |
| 6377 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6378 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6379 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6380 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6381 | if (block_rsv) |
| 6382 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6383 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6384 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6385 | btrfs_release_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6386 | BUG_ON(err); |
| 6387 | |
| 6388 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6389 | BUG_ON(ret); |
| 6390 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6391 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6392 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6393 | NULL, NULL); |
| 6394 | BUG_ON(ret < 0); |
| 6395 | if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 6396 | /* if we fail to delete the orphan item this time |
| 6397 | * around, it'll get picked up the next time. |
| 6398 | * |
| 6399 | * The most common failure here is just -ENOENT. |
| 6400 | */ |
| 6401 | btrfs_del_orphan_item(trans, tree_root, |
| 6402 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6403 | } |
| 6404 | } |
| 6405 | |
| 6406 | if (root->in_radix) { |
| 6407 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6408 | } else { |
| 6409 | free_extent_buffer(root->node); |
| 6410 | free_extent_buffer(root->commit_root); |
| 6411 | kfree(root); |
| 6412 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6413 | out: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6414 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6415 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6416 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6417 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6418 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6419 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6420 | /* |
| 6421 | * drop subtree rooted at tree block 'node'. |
| 6422 | * |
| 6423 | * NOTE: this function will unlock and release tree block 'node' |
| 6424 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6425 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6426 | struct btrfs_root *root, |
| 6427 | struct extent_buffer *node, |
| 6428 | struct extent_buffer *parent) |
| 6429 | { |
| 6430 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6431 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6432 | int level; |
| 6433 | int parent_level; |
| 6434 | int ret = 0; |
| 6435 | int wret; |
| 6436 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6437 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6438 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6439 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6440 | if (!path) |
| 6441 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6442 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6443 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6444 | if (!wc) { |
| 6445 | btrfs_free_path(path); |
| 6446 | return -ENOMEM; |
| 6447 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6448 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6449 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6450 | parent_level = btrfs_header_level(parent); |
| 6451 | extent_buffer_get(parent); |
| 6452 | path->nodes[parent_level] = parent; |
| 6453 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6454 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6455 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6456 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6457 | path->nodes[level] = node; |
| 6458 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6459 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6460 | |
| 6461 | wc->refs[parent_level] = 1; |
| 6462 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6463 | wc->level = level; |
| 6464 | wc->shared_level = -1; |
| 6465 | wc->stage = DROP_REFERENCE; |
| 6466 | wc->update_ref = 0; |
| 6467 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6468 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6469 | |
| 6470 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6471 | wret = walk_down_tree(trans, root, path, wc); |
| 6472 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6473 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6474 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6475 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6476 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6477 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6478 | if (wret < 0) |
| 6479 | ret = wret; |
| 6480 | if (wret != 0) |
| 6481 | break; |
| 6482 | } |
| 6483 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6484 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6485 | btrfs_free_path(path); |
| 6486 | return ret; |
| 6487 | } |
| 6488 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6489 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 6490 | { |
| 6491 | u64 num_devices; |
| 6492 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 6493 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 6494 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6495 | /* |
| 6496 | * we add in the count of missing devices because we want |
| 6497 | * to make sure that any RAID levels on a degraded FS |
| 6498 | * continue to be honored. |
| 6499 | */ |
| 6500 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 6501 | root->fs_info->fs_devices->missing_devices; |
| 6502 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6503 | if (num_devices == 1) { |
| 6504 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6505 | stripped = flags & ~stripped; |
| 6506 | |
| 6507 | /* turn raid0 into single device chunks */ |
| 6508 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 6509 | return stripped; |
| 6510 | |
| 6511 | /* turn mirroring into duplication */ |
| 6512 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6513 | BTRFS_BLOCK_GROUP_RAID10)) |
| 6514 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 6515 | return flags; |
| 6516 | } else { |
| 6517 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6518 | if (flags & stripped) |
| 6519 | return flags; |
| 6520 | |
| 6521 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6522 | stripped = flags & ~stripped; |
| 6523 | |
| 6524 | /* switch duplicated blocks with raid1 */ |
| 6525 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 6526 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 6527 | |
| 6528 | /* turn single device chunks into raid0 */ |
| 6529 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 6530 | } |
| 6531 | return flags; |
| 6532 | } |
| 6533 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6534 | static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6535 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6536 | struct btrfs_space_info *sinfo = cache->space_info; |
| 6537 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6538 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6539 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6540 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6541 | if (cache->ro) |
| 6542 | return 0; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6543 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6544 | /* |
| 6545 | * We need some metadata space and system metadata space for |
| 6546 | * allocating chunks in some corner cases until we force to set |
| 6547 | * it to be readonly. |
| 6548 | */ |
| 6549 | if ((sinfo->flags & |
| 6550 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 6551 | !force) |
| 6552 | min_allocable_bytes = 1 * 1024 * 1024; |
| 6553 | else |
| 6554 | min_allocable_bytes = 0; |
| 6555 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6556 | spin_lock(&sinfo->lock); |
| 6557 | spin_lock(&cache->lock); |
| 6558 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 6559 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6560 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6561 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
| 6562 | sinfo->bytes_may_use + sinfo->bytes_readonly + |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6563 | cache->reserved_pinned + num_bytes + min_allocable_bytes <= |
| 6564 | sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6565 | sinfo->bytes_readonly += num_bytes; |
| 6566 | sinfo->bytes_reserved += cache->reserved_pinned; |
| 6567 | cache->reserved_pinned = 0; |
| 6568 | cache->ro = 1; |
| 6569 | ret = 0; |
| 6570 | } |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 6571 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6572 | spin_unlock(&cache->lock); |
| 6573 | spin_unlock(&sinfo->lock); |
| 6574 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6575 | } |
| 6576 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6577 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 6578 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6579 | |
| 6580 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6581 | struct btrfs_trans_handle *trans; |
| 6582 | u64 alloc_flags; |
| 6583 | int ret; |
| 6584 | |
| 6585 | BUG_ON(cache->ro); |
| 6586 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6587 | trans = btrfs_join_transaction(root); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6588 | BUG_ON(IS_ERR(trans)); |
| 6589 | |
| 6590 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 6591 | if (alloc_flags != cache->flags) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6592 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6593 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6594 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6595 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6596 | if (!ret) |
| 6597 | goto out; |
| 6598 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6599 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6600 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6601 | if (ret < 0) |
| 6602 | goto out; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6603 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6604 | out: |
| 6605 | btrfs_end_transaction(trans, root); |
| 6606 | return ret; |
| 6607 | } |
| 6608 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 6609 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 6610 | struct btrfs_root *root, u64 type) |
| 6611 | { |
| 6612 | u64 alloc_flags = get_alloc_profile(root, type); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6613 | return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6614 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 6615 | } |
| 6616 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 6617 | /* |
| 6618 | * helper to account the unused space of all the readonly block group in the |
| 6619 | * list. takes mirrors into account. |
| 6620 | */ |
| 6621 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 6622 | { |
| 6623 | struct btrfs_block_group_cache *block_group; |
| 6624 | u64 free_bytes = 0; |
| 6625 | int factor; |
| 6626 | |
| 6627 | list_for_each_entry(block_group, groups_list, list) { |
| 6628 | spin_lock(&block_group->lock); |
| 6629 | |
| 6630 | if (!block_group->ro) { |
| 6631 | spin_unlock(&block_group->lock); |
| 6632 | continue; |
| 6633 | } |
| 6634 | |
| 6635 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6636 | BTRFS_BLOCK_GROUP_RAID10 | |
| 6637 | BTRFS_BLOCK_GROUP_DUP)) |
| 6638 | factor = 2; |
| 6639 | else |
| 6640 | factor = 1; |
| 6641 | |
| 6642 | free_bytes += (block_group->key.offset - |
| 6643 | btrfs_block_group_used(&block_group->item)) * |
| 6644 | factor; |
| 6645 | |
| 6646 | spin_unlock(&block_group->lock); |
| 6647 | } |
| 6648 | |
| 6649 | return free_bytes; |
| 6650 | } |
| 6651 | |
| 6652 | /* |
| 6653 | * helper to account the unused space of all the readonly block group in the |
| 6654 | * space_info. takes mirrors into account. |
| 6655 | */ |
| 6656 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 6657 | { |
| 6658 | int i; |
| 6659 | u64 free_bytes = 0; |
| 6660 | |
| 6661 | spin_lock(&sinfo->lock); |
| 6662 | |
| 6663 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 6664 | if (!list_empty(&sinfo->block_groups[i])) |
| 6665 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 6666 | &sinfo->block_groups[i]); |
| 6667 | |
| 6668 | spin_unlock(&sinfo->lock); |
| 6669 | |
| 6670 | return free_bytes; |
| 6671 | } |
| 6672 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6673 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 6674 | struct btrfs_block_group_cache *cache) |
| 6675 | { |
| 6676 | struct btrfs_space_info *sinfo = cache->space_info; |
| 6677 | u64 num_bytes; |
| 6678 | |
| 6679 | BUG_ON(!cache->ro); |
| 6680 | |
| 6681 | spin_lock(&sinfo->lock); |
| 6682 | spin_lock(&cache->lock); |
| 6683 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 6684 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 6685 | sinfo->bytes_readonly -= num_bytes; |
| 6686 | cache->ro = 0; |
| 6687 | spin_unlock(&cache->lock); |
| 6688 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6689 | return 0; |
| 6690 | } |
| 6691 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6692 | /* |
| 6693 | * checks to see if its even possible to relocate this block group. |
| 6694 | * |
| 6695 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 6696 | * ok to go ahead and try. |
| 6697 | */ |
| 6698 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6699 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6700 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6701 | struct btrfs_space_info *space_info; |
| 6702 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6703 | struct btrfs_device *device; |
| 6704 | int full = 0; |
| 6705 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6706 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6707 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6708 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6709 | /* odd, couldn't find the block group, leave it alone */ |
| 6710 | if (!block_group) |
| 6711 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6712 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6713 | /* no bytes used, we're good */ |
| 6714 | if (!btrfs_block_group_used(&block_group->item)) |
| 6715 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6716 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6717 | space_info = block_group->space_info; |
| 6718 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6719 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6720 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6721 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6722 | /* |
| 6723 | * 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] | 6724 | * relocate it unless we're able to allocate a new chunk below. |
| 6725 | * |
| 6726 | * Otherwise, we need to make sure we have room in the space to handle |
| 6727 | * 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] | 6728 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 6729 | if ((space_info->total_bytes != block_group->key.offset) && |
| 6730 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6731 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 6732 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 6733 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6734 | spin_unlock(&space_info->lock); |
| 6735 | goto out; |
| 6736 | } |
| 6737 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6738 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6739 | /* |
| 6740 | * ok we don't have enough space, but maybe we have free space on our |
| 6741 | * devices to allocate new chunks for relocation, so loop through our |
| 6742 | * alloc devices and guess if we have enough space. However, if we |
| 6743 | * were marked as full, then we know there aren't enough chunks, and we |
| 6744 | * can just return. |
| 6745 | */ |
| 6746 | ret = -1; |
| 6747 | if (full) |
| 6748 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6749 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6750 | mutex_lock(&root->fs_info->chunk_mutex); |
| 6751 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 6752 | u64 min_free = btrfs_block_group_used(&block_group->item); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 6753 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6754 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6755 | /* |
| 6756 | * check to make sure we can actually find a chunk with enough |
| 6757 | * space to fit our block group in. |
| 6758 | */ |
| 6759 | if (device->total_bytes > device->bytes_used + min_free) { |
| 6760 | ret = find_free_dev_extent(NULL, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 6761 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6762 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6763 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6764 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6765 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6766 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6767 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6768 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6769 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6770 | return ret; |
| 6771 | } |
| 6772 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 6773 | static int find_first_block_group(struct btrfs_root *root, |
| 6774 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6775 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6776 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6777 | struct btrfs_key found_key; |
| 6778 | struct extent_buffer *leaf; |
| 6779 | int slot; |
| 6780 | |
| 6781 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 6782 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6783 | goto out; |
| 6784 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6785 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6786 | slot = path->slots[0]; |
| 6787 | leaf = path->nodes[0]; |
| 6788 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6789 | ret = btrfs_next_leaf(root, path); |
| 6790 | if (ret == 0) |
| 6791 | continue; |
| 6792 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6793 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6794 | break; |
| 6795 | } |
| 6796 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6797 | |
| 6798 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6799 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 6800 | ret = 0; |
| 6801 | goto out; |
| 6802 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6803 | path->slots[0]++; |
| 6804 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6805 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6806 | return ret; |
| 6807 | } |
| 6808 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6809 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 6810 | { |
| 6811 | struct btrfs_block_group_cache *block_group; |
| 6812 | u64 last = 0; |
| 6813 | |
| 6814 | while (1) { |
| 6815 | struct inode *inode; |
| 6816 | |
| 6817 | block_group = btrfs_lookup_first_block_group(info, last); |
| 6818 | while (block_group) { |
| 6819 | spin_lock(&block_group->lock); |
| 6820 | if (block_group->iref) |
| 6821 | break; |
| 6822 | spin_unlock(&block_group->lock); |
| 6823 | block_group = next_block_group(info->tree_root, |
| 6824 | block_group); |
| 6825 | } |
| 6826 | if (!block_group) { |
| 6827 | if (last == 0) |
| 6828 | break; |
| 6829 | last = 0; |
| 6830 | continue; |
| 6831 | } |
| 6832 | |
| 6833 | inode = block_group->inode; |
| 6834 | block_group->iref = 0; |
| 6835 | block_group->inode = NULL; |
| 6836 | spin_unlock(&block_group->lock); |
| 6837 | iput(inode); |
| 6838 | last = block_group->key.objectid + block_group->key.offset; |
| 6839 | btrfs_put_block_group(block_group); |
| 6840 | } |
| 6841 | } |
| 6842 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6843 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 6844 | { |
| 6845 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6846 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6847 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6848 | struct rb_node *n; |
| 6849 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6850 | down_write(&info->extent_commit_sem); |
| 6851 | while (!list_empty(&info->caching_block_groups)) { |
| 6852 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 6853 | struct btrfs_caching_control, list); |
| 6854 | list_del(&caching_ctl->list); |
| 6855 | put_caching_control(caching_ctl); |
| 6856 | } |
| 6857 | up_write(&info->extent_commit_sem); |
| 6858 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6859 | spin_lock(&info->block_group_cache_lock); |
| 6860 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 6861 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 6862 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6863 | rb_erase(&block_group->cache_node, |
| 6864 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6865 | spin_unlock(&info->block_group_cache_lock); |
| 6866 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6867 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6868 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6869 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6870 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6871 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6872 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6873 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 6874 | /* |
| 6875 | * We haven't cached this block group, which means we could |
| 6876 | * possibly have excluded extents on this block group. |
| 6877 | */ |
| 6878 | if (block_group->cached == BTRFS_CACHE_NO) |
| 6879 | free_excluded_extents(info->extent_root, block_group); |
| 6880 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6881 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 6882 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6883 | |
| 6884 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6885 | } |
| 6886 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6887 | |
| 6888 | /* now that all the block groups are freed, go through and |
| 6889 | * free all the space_info structs. This is only called during |
| 6890 | * the final stages of unmount, and so we know nobody is |
| 6891 | * using them. We call synchronize_rcu() once before we start, |
| 6892 | * just to be on the safe side. |
| 6893 | */ |
| 6894 | synchronize_rcu(); |
| 6895 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6896 | release_global_block_rsv(info); |
| 6897 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6898 | while(!list_empty(&info->space_info)) { |
| 6899 | space_info = list_entry(info->space_info.next, |
| 6900 | struct btrfs_space_info, |
| 6901 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6902 | if (space_info->bytes_pinned > 0 || |
| 6903 | space_info->bytes_reserved > 0) { |
| 6904 | WARN_ON(1); |
| 6905 | dump_space_info(space_info, 0, 0); |
| 6906 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6907 | list_del(&space_info->list); |
| 6908 | kfree(space_info); |
| 6909 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6910 | return 0; |
| 6911 | } |
| 6912 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6913 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 6914 | struct btrfs_block_group_cache *cache) |
| 6915 | { |
| 6916 | int index = get_block_group_index(cache); |
| 6917 | |
| 6918 | down_write(&space_info->groups_sem); |
| 6919 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 6920 | up_write(&space_info->groups_sem); |
| 6921 | } |
| 6922 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6923 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 6924 | { |
| 6925 | struct btrfs_path *path; |
| 6926 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6927 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 6928 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6929 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6930 | struct btrfs_key key; |
| 6931 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6932 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6933 | int need_clear = 0; |
| 6934 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 6935 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 6936 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6937 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6938 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6939 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6940 | path = btrfs_alloc_path(); |
| 6941 | if (!path) |
| 6942 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6943 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6944 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6945 | cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); |
| 6946 | if (cache_gen != 0 && |
| 6947 | btrfs_super_generation(&root->fs_info->super_copy) != cache_gen) |
| 6948 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 6949 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 6950 | need_clear = 1; |
Josef Bacik | 8216ef8 | 2010-10-28 16:55:47 -0400 | [diff] [blame] | 6951 | if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen) |
| 6952 | printk(KERN_INFO "btrfs: disk space caching is enabled\n"); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6953 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6954 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6955 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6956 | if (ret > 0) |
| 6957 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6958 | if (ret != 0) |
| 6959 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6960 | leaf = path->nodes[0]; |
| 6961 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 6962 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6963 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6964 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6965 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6966 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 6967 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 6968 | GFP_NOFS); |
| 6969 | if (!cache->free_space_ctl) { |
| 6970 | kfree(cache); |
| 6971 | ret = -ENOMEM; |
| 6972 | goto error; |
| 6973 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 6974 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6975 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6976 | spin_lock_init(&cache->lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6977 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6978 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6979 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6980 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6981 | if (need_clear) |
| 6982 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 6983 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6984 | read_extent_buffer(leaf, &cache->item, |
| 6985 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 6986 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6987 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6988 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6989 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6990 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6991 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6992 | cache->sectorsize = root->sectorsize; |
| 6993 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 6994 | btrfs_init_free_space_ctl(cache); |
| 6995 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6996 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 6997 | * We need to exclude the super stripes now so that the space |
| 6998 | * info has super bytes accounted for, otherwise we'll think |
| 6999 | * we have more space than we actually do. |
| 7000 | */ |
| 7001 | exclude_super_stripes(root, cache); |
| 7002 | |
| 7003 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7004 | * check for two cases, either we are full, and therefore |
| 7005 | * don't need to bother with the caching work since we won't |
| 7006 | * find any space, or we are empty, and we can just add all |
| 7007 | * the space in and be done with it. This saves us _alot_ of |
| 7008 | * time, particularly in the full case. |
| 7009 | */ |
| 7010 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7011 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7012 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7013 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7014 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7015 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7016 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7017 | add_new_free_space(cache, root->fs_info, |
| 7018 | found_key.objectid, |
| 7019 | found_key.objectid + |
| 7020 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7021 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7022 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7023 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7024 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7025 | btrfs_block_group_used(&cache->item), |
| 7026 | &space_info); |
| 7027 | BUG_ON(ret); |
| 7028 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7029 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7030 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7031 | spin_unlock(&cache->space_info->lock); |
| 7032 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7033 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7034 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7035 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7036 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7037 | |
| 7038 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7039 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7040 | set_block_group_ro(cache, 1); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7041 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7042 | |
| 7043 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 7044 | if (!(get_alloc_profile(root, space_info->flags) & |
| 7045 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 7046 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7047 | BTRFS_BLOCK_GROUP_DUP))) |
| 7048 | continue; |
| 7049 | /* |
| 7050 | * avoid allocating from un-mirrored block group if there are |
| 7051 | * mirrored block groups. |
| 7052 | */ |
| 7053 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7054 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7055 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7056 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7057 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7058 | |
| 7059 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7060 | ret = 0; |
| 7061 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7062 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7063 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7064 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7065 | |
| 7066 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7067 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7068 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7069 | u64 size) |
| 7070 | { |
| 7071 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7072 | struct btrfs_root *extent_root; |
| 7073 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7074 | |
| 7075 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7076 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7077 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7078 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7079 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7080 | if (!cache) |
| 7081 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7082 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7083 | GFP_NOFS); |
| 7084 | if (!cache->free_space_ctl) { |
| 7085 | kfree(cache); |
| 7086 | return -ENOMEM; |
| 7087 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7088 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7089 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7090 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7091 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7092 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7093 | cache->fs_info = root->fs_info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7094 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7095 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7096 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7097 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7098 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7099 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7100 | btrfs_init_free_space_ctl(cache); |
| 7101 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7102 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7103 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7104 | cache->flags = type; |
| 7105 | btrfs_set_block_group_flags(&cache->item, type); |
| 7106 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7107 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7108 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7109 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7110 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7111 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7112 | chunk_offset + size); |
| 7113 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7114 | free_excluded_extents(root, cache); |
| 7115 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7116 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7117 | &cache->space_info); |
| 7118 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7119 | |
| 7120 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7121 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7122 | spin_unlock(&cache->space_info->lock); |
| 7123 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7124 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7125 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7126 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7127 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7128 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7129 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7130 | sizeof(cache->item)); |
| 7131 | BUG_ON(ret); |
| 7132 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7133 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7134 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7135 | return 0; |
| 7136 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7137 | |
| 7138 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7139 | struct btrfs_root *root, u64 group_start) |
| 7140 | { |
| 7141 | struct btrfs_path *path; |
| 7142 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7143 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7144 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7145 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7146 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7147 | int ret; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7148 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7149 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7150 | root = root->fs_info->extent_root; |
| 7151 | |
| 7152 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7153 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7154 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7155 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 7156 | /* |
| 7157 | * Free the reserved super bytes from this block group before |
| 7158 | * remove it. |
| 7159 | */ |
| 7160 | free_excluded_extents(root, block_group); |
| 7161 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7162 | memcpy(&key, &block_group->key, sizeof(key)); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7163 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 7164 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7165 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7166 | factor = 2; |
| 7167 | else |
| 7168 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7169 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7170 | /* make sure this block group isn't part of an allocation cluster */ |
| 7171 | cluster = &root->fs_info->data_alloc_cluster; |
| 7172 | spin_lock(&cluster->refill_lock); |
| 7173 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7174 | spin_unlock(&cluster->refill_lock); |
| 7175 | |
| 7176 | /* |
| 7177 | * make sure this block group isn't part of a metadata |
| 7178 | * allocation cluster |
| 7179 | */ |
| 7180 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7181 | spin_lock(&cluster->refill_lock); |
| 7182 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7183 | spin_unlock(&cluster->refill_lock); |
| 7184 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7185 | path = btrfs_alloc_path(); |
| 7186 | BUG_ON(!path); |
| 7187 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7188 | inode = lookup_free_space_inode(root, block_group, path); |
| 7189 | if (!IS_ERR(inode)) { |
| 7190 | btrfs_orphan_add(trans, inode); |
| 7191 | clear_nlink(inode); |
| 7192 | /* One for the block groups ref */ |
| 7193 | spin_lock(&block_group->lock); |
| 7194 | if (block_group->iref) { |
| 7195 | block_group->iref = 0; |
| 7196 | block_group->inode = NULL; |
| 7197 | spin_unlock(&block_group->lock); |
| 7198 | iput(inode); |
| 7199 | } else { |
| 7200 | spin_unlock(&block_group->lock); |
| 7201 | } |
| 7202 | /* One for our lookup ref */ |
| 7203 | iput(inode); |
| 7204 | } |
| 7205 | |
| 7206 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 7207 | key.offset = block_group->key.objectid; |
| 7208 | key.type = 0; |
| 7209 | |
| 7210 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 7211 | if (ret < 0) |
| 7212 | goto out; |
| 7213 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7214 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7215 | if (ret == 0) { |
| 7216 | ret = btrfs_del_item(trans, tree_root, path); |
| 7217 | if (ret) |
| 7218 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7219 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7220 | } |
| 7221 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7222 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7223 | rb_erase(&block_group->cache_node, |
| 7224 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7225 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7226 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7227 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7228 | /* |
| 7229 | * we must use list_del_init so people can check to see if they |
| 7230 | * are still on the list after taking the semaphore |
| 7231 | */ |
| 7232 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7233 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7234 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7235 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7236 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7237 | |
| 7238 | btrfs_remove_free_space_cache(block_group); |
| 7239 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7240 | spin_lock(&block_group->space_info->lock); |
| 7241 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7242 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7243 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7244 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7245 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7246 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7247 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7248 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7249 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7250 | btrfs_put_block_group(block_group); |
| 7251 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7252 | |
| 7253 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7254 | if (ret > 0) |
| 7255 | ret = -EIO; |
| 7256 | if (ret < 0) |
| 7257 | goto out; |
| 7258 | |
| 7259 | ret = btrfs_del_item(trans, root, path); |
| 7260 | out: |
| 7261 | btrfs_free_path(path); |
| 7262 | return ret; |
| 7263 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7264 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7265 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 7266 | { |
| 7267 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7268 | struct btrfs_super_block *disk_super; |
| 7269 | u64 features; |
| 7270 | u64 flags; |
| 7271 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7272 | int ret; |
| 7273 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7274 | disk_super = &fs_info->super_copy; |
| 7275 | if (!btrfs_super_root(disk_super)) |
| 7276 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7277 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7278 | features = btrfs_super_incompat_flags(disk_super); |
| 7279 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 7280 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7281 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7282 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 7283 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7284 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7285 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7286 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7287 | if (mixed) { |
| 7288 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 7289 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7290 | } else { |
| 7291 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 7292 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7293 | if (ret) |
| 7294 | goto out; |
| 7295 | |
| 7296 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 7297 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7298 | } |
| 7299 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7300 | return ret; |
| 7301 | } |
| 7302 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7303 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 7304 | { |
| 7305 | return unpin_extent_range(root, start, end); |
| 7306 | } |
| 7307 | |
| 7308 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 7309 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7310 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 7311 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7312 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 7313 | |
| 7314 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 7315 | { |
| 7316 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 7317 | struct btrfs_block_group_cache *cache = NULL; |
| 7318 | u64 group_trimmed; |
| 7319 | u64 start; |
| 7320 | u64 end; |
| 7321 | u64 trimmed = 0; |
| 7322 | int ret = 0; |
| 7323 | |
| 7324 | cache = btrfs_lookup_block_group(fs_info, range->start); |
| 7325 | |
| 7326 | while (cache) { |
| 7327 | if (cache->key.objectid >= (range->start + range->len)) { |
| 7328 | btrfs_put_block_group(cache); |
| 7329 | break; |
| 7330 | } |
| 7331 | |
| 7332 | start = max(range->start, cache->key.objectid); |
| 7333 | end = min(range->start + range->len, |
| 7334 | cache->key.objectid + cache->key.offset); |
| 7335 | |
| 7336 | if (end - start >= range->minlen) { |
| 7337 | if (!block_group_cache_done(cache)) { |
| 7338 | ret = cache_block_group(cache, NULL, root, 0); |
| 7339 | if (!ret) |
| 7340 | wait_block_group_cache_done(cache); |
| 7341 | } |
| 7342 | ret = btrfs_trim_block_group(cache, |
| 7343 | &group_trimmed, |
| 7344 | start, |
| 7345 | end, |
| 7346 | range->minlen); |
| 7347 | |
| 7348 | trimmed += group_trimmed; |
| 7349 | if (ret) { |
| 7350 | btrfs_put_block_group(cache); |
| 7351 | break; |
| 7352 | } |
| 7353 | } |
| 7354 | |
| 7355 | cache = next_block_group(fs_info->tree_root, cache); |
| 7356 | } |
| 7357 | |
| 7358 | range->len = trimmed; |
| 7359 | return ret; |
| 7360 | } |