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 | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 323 | static int caching_kthread(void *data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 324 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 325 | struct btrfs_block_group_cache *block_group = data; |
| 326 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 327 | struct btrfs_caching_control *caching_ctl = block_group->caching_ctl; |
| 328 | struct btrfs_root *extent_root = fs_info->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 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 337 | path = btrfs_alloc_path(); |
| 338 | if (!path) |
| 339 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 340 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 341 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 342 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 343 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 344 | * We don't want to deadlock with somebody trying to allocate a new |
| 345 | * extent for the extent root while also trying to search the extent |
| 346 | * root to add free space. So we skip locking and search the commit |
| 347 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 348 | */ |
| 349 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 350 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 351 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 352 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 353 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 354 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 355 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 356 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 357 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 358 | /* need to make sure the commit_root doesn't disappear */ |
| 359 | down_read(&fs_info->extent_commit_sem); |
| 360 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 361 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 362 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 363 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 364 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 365 | leaf = path->nodes[0]; |
| 366 | nritems = btrfs_header_nritems(leaf); |
| 367 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 368 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 369 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 370 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 371 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 372 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 373 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 374 | if (path->slots[0] < nritems) { |
| 375 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 376 | } else { |
| 377 | ret = find_next_key(path, 0, &key); |
| 378 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 379 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 380 | |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 381 | if (need_resched() || |
| 382 | btrfs_next_leaf(extent_root, path)) { |
| 383 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 384 | btrfs_release_path(path); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 385 | up_read(&fs_info->extent_commit_sem); |
| 386 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 387 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 388 | goto again; |
| 389 | } |
| 390 | leaf = path->nodes[0]; |
| 391 | nritems = btrfs_header_nritems(leaf); |
| 392 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 393 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 394 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 395 | if (key.objectid < block_group->key.objectid) { |
| 396 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 397 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 398 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 399 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 400 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 401 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 402 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 403 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 404 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 405 | total_found += add_new_free_space(block_group, |
| 406 | fs_info, last, |
| 407 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 408 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 409 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 410 | if (total_found > (1024 * 1024 * 2)) { |
| 411 | total_found = 0; |
| 412 | wake_up(&caching_ctl->wait); |
| 413 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 414 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 415 | path->slots[0]++; |
| 416 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 417 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 418 | |
| 419 | total_found += add_new_free_space(block_group, fs_info, last, |
| 420 | block_group->key.objectid + |
| 421 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 422 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 423 | |
| 424 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 425 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 426 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 427 | spin_unlock(&block_group->lock); |
| 428 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 429 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 430 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 431 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 432 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 433 | free_excluded_extents(extent_root, block_group); |
| 434 | |
| 435 | mutex_unlock(&caching_ctl->mutex); |
| 436 | wake_up(&caching_ctl->wait); |
| 437 | |
| 438 | put_caching_control(caching_ctl); |
| 439 | atomic_dec(&block_group->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 440 | btrfs_put_block_group(block_group); |
| 441 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 445 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
| 446 | struct btrfs_trans_handle *trans, |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 447 | struct btrfs_root *root, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 448 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 449 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 450 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 451 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 452 | struct task_struct *tsk; |
| 453 | int ret = 0; |
| 454 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 455 | smp_mb(); |
| 456 | if (cache->cached != BTRFS_CACHE_NO) |
| 457 | return 0; |
| 458 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 459 | /* |
| 460 | * We can't do the read from on-disk cache during a commit since we need |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 461 | * to have the normal tree locking. Also if we are currently trying to |
| 462 | * allocate blocks for the tree root we can't do the fast caching since |
| 463 | * we likely hold important locks. |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 464 | */ |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 465 | if (trans && (!trans->transaction->in_commit) && |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 466 | (root && root != root->fs_info->tree_root)) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 467 | spin_lock(&cache->lock); |
| 468 | if (cache->cached != BTRFS_CACHE_NO) { |
| 469 | spin_unlock(&cache->lock); |
| 470 | return 0; |
| 471 | } |
| 472 | cache->cached = BTRFS_CACHE_STARTED; |
| 473 | spin_unlock(&cache->lock); |
| 474 | |
| 475 | ret = load_free_space_cache(fs_info, cache); |
| 476 | |
| 477 | spin_lock(&cache->lock); |
| 478 | if (ret == 1) { |
| 479 | cache->cached = BTRFS_CACHE_FINISHED; |
| 480 | cache->last_byte_to_unpin = (u64)-1; |
| 481 | } else { |
| 482 | cache->cached = BTRFS_CACHE_NO; |
| 483 | } |
| 484 | spin_unlock(&cache->lock); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 485 | if (ret == 1) { |
| 486 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 487 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 488 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if (load_cache_only) |
| 492 | return 0; |
| 493 | |
Miao Xie | fc0e4a3 | 2011-03-24 11:41:21 +0000 | [diff] [blame] | 494 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 495 | BUG_ON(!caching_ctl); |
| 496 | |
| 497 | INIT_LIST_HEAD(&caching_ctl->list); |
| 498 | mutex_init(&caching_ctl->mutex); |
| 499 | init_waitqueue_head(&caching_ctl->wait); |
| 500 | caching_ctl->block_group = cache; |
| 501 | caching_ctl->progress = cache->key.objectid; |
| 502 | /* one for caching kthread, one for caching block group list */ |
| 503 | atomic_set(&caching_ctl->count, 2); |
| 504 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 505 | spin_lock(&cache->lock); |
| 506 | if (cache->cached != BTRFS_CACHE_NO) { |
| 507 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 508 | kfree(caching_ctl); |
| 509 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 510 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 511 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 512 | cache->cached = BTRFS_CACHE_STARTED; |
| 513 | spin_unlock(&cache->lock); |
| 514 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 515 | down_write(&fs_info->extent_commit_sem); |
| 516 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 517 | up_write(&fs_info->extent_commit_sem); |
| 518 | |
| 519 | atomic_inc(&cache->space_info->caching_threads); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 520 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 521 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 522 | tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n", |
| 523 | cache->key.objectid); |
| 524 | if (IS_ERR(tsk)) { |
| 525 | ret = PTR_ERR(tsk); |
| 526 | printk(KERN_ERR "error running thread %d\n", ret); |
| 527 | BUG(); |
| 528 | } |
| 529 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 530 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 533 | /* |
| 534 | * return the block group that starts at or after bytenr |
| 535 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 536 | static struct btrfs_block_group_cache * |
| 537 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 538 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 539 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 540 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 541 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 542 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 543 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 544 | } |
| 545 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 546 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 547 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 548 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 549 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 550 | struct btrfs_fs_info *info, |
| 551 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 552 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 553 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 554 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 555 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 556 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 557 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 558 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 559 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 560 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 561 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 562 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 563 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 564 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 565 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 566 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 567 | BTRFS_BLOCK_GROUP_METADATA; |
| 568 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 569 | rcu_read_lock(); |
| 570 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 571 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 572 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 573 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 574 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 575 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 576 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 577 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 580 | /* |
| 581 | * after adding space to the filesystem, we need to clear the full flags |
| 582 | * on all the space infos. |
| 583 | */ |
| 584 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 585 | { |
| 586 | struct list_head *head = &info->space_info; |
| 587 | struct btrfs_space_info *found; |
| 588 | |
| 589 | rcu_read_lock(); |
| 590 | list_for_each_entry_rcu(found, head, list) |
| 591 | found->full = 0; |
| 592 | rcu_read_unlock(); |
| 593 | } |
| 594 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 595 | static u64 div_factor(u64 num, int factor) |
| 596 | { |
| 597 | if (factor == 10) |
| 598 | return num; |
| 599 | num *= factor; |
| 600 | do_div(num, 10); |
| 601 | return num; |
| 602 | } |
| 603 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 604 | static u64 div_factor_fine(u64 num, int factor) |
| 605 | { |
| 606 | if (factor == 100) |
| 607 | return num; |
| 608 | num *= factor; |
| 609 | do_div(num, 100); |
| 610 | return num; |
| 611 | } |
| 612 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 613 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 614 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 615 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 616 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 617 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 618 | u64 last = max(search_hint, search_start); |
| 619 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 620 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 621 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 622 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 623 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 624 | while (1) { |
| 625 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 626 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 627 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 628 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 629 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 630 | last = cache->key.objectid + cache->key.offset; |
| 631 | used = btrfs_block_group_used(&cache->item); |
| 632 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 633 | if ((full_search || !cache->ro) && |
| 634 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 635 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 636 | div_factor(cache->key.offset, factor)) { |
| 637 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 638 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 639 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 640 | goto found; |
| 641 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 642 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 643 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 644 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 645 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 646 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 647 | if (!wrapped) { |
| 648 | last = search_start; |
| 649 | wrapped = 1; |
| 650 | goto again; |
| 651 | } |
| 652 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 653 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 654 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 655 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 656 | goto again; |
| 657 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 658 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 659 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 660 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 661 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 662 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 663 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 664 | { |
| 665 | int ret; |
| 666 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 667 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 668 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 669 | path = btrfs_alloc_path(); |
| 670 | BUG_ON(!path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 671 | key.objectid = start; |
| 672 | key.offset = len; |
| 673 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 674 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 675 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 676 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 677 | return ret; |
| 678 | } |
| 679 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 680 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 681 | * helper function to lookup reference count and flags of extent. |
| 682 | * |
| 683 | * the head node for delayed ref is used to store the sum of all the |
| 684 | * reference count modifications queued up in the rbtree. the head |
| 685 | * node may also store the extent flags to set. This way you can check |
| 686 | * to see what the reference count and extent flags would be if all of |
| 687 | * the delayed refs are not processed. |
| 688 | */ |
| 689 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 690 | struct btrfs_root *root, u64 bytenr, |
| 691 | u64 num_bytes, u64 *refs, u64 *flags) |
| 692 | { |
| 693 | struct btrfs_delayed_ref_head *head; |
| 694 | struct btrfs_delayed_ref_root *delayed_refs; |
| 695 | struct btrfs_path *path; |
| 696 | struct btrfs_extent_item *ei; |
| 697 | struct extent_buffer *leaf; |
| 698 | struct btrfs_key key; |
| 699 | u32 item_size; |
| 700 | u64 num_refs; |
| 701 | u64 extent_flags; |
| 702 | int ret; |
| 703 | |
| 704 | path = btrfs_alloc_path(); |
| 705 | if (!path) |
| 706 | return -ENOMEM; |
| 707 | |
| 708 | key.objectid = bytenr; |
| 709 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 710 | key.offset = num_bytes; |
| 711 | if (!trans) { |
| 712 | path->skip_locking = 1; |
| 713 | path->search_commit_root = 1; |
| 714 | } |
| 715 | again: |
| 716 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 717 | &key, path, 0, 0); |
| 718 | if (ret < 0) |
| 719 | goto out_free; |
| 720 | |
| 721 | if (ret == 0) { |
| 722 | leaf = path->nodes[0]; |
| 723 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 724 | if (item_size >= sizeof(*ei)) { |
| 725 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 726 | struct btrfs_extent_item); |
| 727 | num_refs = btrfs_extent_refs(leaf, ei); |
| 728 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 729 | } else { |
| 730 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 731 | struct btrfs_extent_item_v0 *ei0; |
| 732 | BUG_ON(item_size != sizeof(*ei0)); |
| 733 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 734 | struct btrfs_extent_item_v0); |
| 735 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 736 | /* FIXME: this isn't correct for data */ |
| 737 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 738 | #else |
| 739 | BUG(); |
| 740 | #endif |
| 741 | } |
| 742 | BUG_ON(num_refs == 0); |
| 743 | } else { |
| 744 | num_refs = 0; |
| 745 | extent_flags = 0; |
| 746 | ret = 0; |
| 747 | } |
| 748 | |
| 749 | if (!trans) |
| 750 | goto out; |
| 751 | |
| 752 | delayed_refs = &trans->transaction->delayed_refs; |
| 753 | spin_lock(&delayed_refs->lock); |
| 754 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 755 | if (head) { |
| 756 | if (!mutex_trylock(&head->mutex)) { |
| 757 | atomic_inc(&head->node.refs); |
| 758 | spin_unlock(&delayed_refs->lock); |
| 759 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 760 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 761 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 762 | /* |
| 763 | * Mutex was contended, block until it's released and try |
| 764 | * again |
| 765 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 766 | mutex_lock(&head->mutex); |
| 767 | mutex_unlock(&head->mutex); |
| 768 | btrfs_put_delayed_ref(&head->node); |
| 769 | goto again; |
| 770 | } |
| 771 | if (head->extent_op && head->extent_op->update_flags) |
| 772 | extent_flags |= head->extent_op->flags_to_set; |
| 773 | else |
| 774 | BUG_ON(num_refs == 0); |
| 775 | |
| 776 | num_refs += head->node.ref_mod; |
| 777 | mutex_unlock(&head->mutex); |
| 778 | } |
| 779 | spin_unlock(&delayed_refs->lock); |
| 780 | out: |
| 781 | WARN_ON(num_refs == 0); |
| 782 | if (refs) |
| 783 | *refs = num_refs; |
| 784 | if (flags) |
| 785 | *flags = extent_flags; |
| 786 | out_free: |
| 787 | btrfs_free_path(path); |
| 788 | return ret; |
| 789 | } |
| 790 | |
| 791 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 792 | * Back reference rules. Back refs have three main goals: |
| 793 | * |
| 794 | * 1) differentiate between all holders of references to an extent so that |
| 795 | * when a reference is dropped we can make sure it was a valid reference |
| 796 | * before freeing the extent. |
| 797 | * |
| 798 | * 2) Provide enough information to quickly find the holders of an extent |
| 799 | * if we notice a given block is corrupted or bad. |
| 800 | * |
| 801 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 802 | * maintenance. This is actually the same as #2, but with a slightly |
| 803 | * different use case. |
| 804 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 805 | * There are two kinds of back refs. The implicit back refs is optimized |
| 806 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 807 | * back refs of this kind provide information about the block's owner tree |
| 808 | * and the pointer's key. These information allow us to find the block by |
| 809 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 810 | * referenced by their owner trees. The location of tree block is recorded |
| 811 | * in the back refs. Actually the full back refs is generic, and can be |
| 812 | * used in all cases the implicit back refs is used. The major shortcoming |
| 813 | * of the full back refs is its overhead. Every time a tree block gets |
| 814 | * COWed, we have to update back refs entry for all pointers in it. |
| 815 | * |
| 816 | * For a newly allocated tree block, we use implicit back refs for |
| 817 | * pointers in it. This means most tree related operations only involve |
| 818 | * implicit back refs. For a tree block created in old transaction, the |
| 819 | * only way to drop a reference to it is COW it. So we can detect the |
| 820 | * event that tree block loses its owner tree's reference and do the |
| 821 | * back refs conversion. |
| 822 | * |
| 823 | * When a tree block is COW'd through a tree, there are four cases: |
| 824 | * |
| 825 | * The reference count of the block is one and the tree is the block's |
| 826 | * owner tree. Nothing to do in this case. |
| 827 | * |
| 828 | * The reference count of the block is one and the tree is not the |
| 829 | * block's owner tree. In this case, full back refs is used for pointers |
| 830 | * in the block. Remove these full back refs, add implicit back refs for |
| 831 | * every pointers in the new block. |
| 832 | * |
| 833 | * The reference count of the block is greater than one and the tree is |
| 834 | * the block's owner tree. In this case, implicit back refs is used for |
| 835 | * pointers in the block. Add full back refs for every pointers in the |
| 836 | * block, increase lower level extents' reference counts. The original |
| 837 | * implicit back refs are entailed to the new block. |
| 838 | * |
| 839 | * The reference count of the block is greater than one and the tree is |
| 840 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 841 | * the new block, increase lower level extents' reference count. |
| 842 | * |
| 843 | * Back Reference Key composing: |
| 844 | * |
| 845 | * The key objectid corresponds to the first byte in the extent, |
| 846 | * The key type is used to differentiate between types of back refs. |
| 847 | * There are different meanings of the key offset for different types |
| 848 | * of back refs. |
| 849 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 850 | * File extents can be referenced by: |
| 851 | * |
| 852 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 853 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 854 | * - different offsets inside a file (bookend extents in file.c) |
| 855 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 856 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 857 | * |
| 858 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 859 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 860 | * - original offset in the file |
| 861 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 862 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 863 | * The key offset for the implicit back refs is hash of the first |
| 864 | * three fields. |
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 extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 867 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 868 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 869 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 870 | * The key offset for the implicit back refs is the first byte of |
| 871 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 872 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 873 | * When a file extent is allocated, The implicit back refs is used. |
| 874 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 875 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 876 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 877 | * |
| 878 | * When a file extent is removed file truncation, we find the |
| 879 | * corresponding implicit back refs and check the following fields: |
| 880 | * |
| 881 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 882 | * |
| 883 | * Btree extents can be referenced by: |
| 884 | * |
| 885 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 886 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 887 | * Both the implicit back refs and the full back refs for tree blocks |
| 888 | * only consist of key. The key offset for the implicit back refs is |
| 889 | * objectid of block's owner tree. The key offset for the full back refs |
| 890 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 891 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 892 | * When implicit back refs is used, information about the lowest key and |
| 893 | * level of the tree block are required. These information are stored in |
| 894 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 895 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 896 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 897 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 898 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 899 | struct btrfs_root *root, |
| 900 | struct btrfs_path *path, |
| 901 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 902 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 903 | struct btrfs_extent_item *item; |
| 904 | struct btrfs_extent_item_v0 *ei0; |
| 905 | struct btrfs_extent_ref_v0 *ref0; |
| 906 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 907 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 908 | struct btrfs_key key; |
| 909 | struct btrfs_key found_key; |
| 910 | u32 new_size = sizeof(*item); |
| 911 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 912 | int ret; |
| 913 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 914 | leaf = path->nodes[0]; |
| 915 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 916 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 917 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 918 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 919 | struct btrfs_extent_item_v0); |
| 920 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 921 | |
| 922 | if (owner == (u64)-1) { |
| 923 | while (1) { |
| 924 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 925 | ret = btrfs_next_leaf(root, path); |
| 926 | if (ret < 0) |
| 927 | return ret; |
| 928 | BUG_ON(ret > 0); |
| 929 | leaf = path->nodes[0]; |
| 930 | } |
| 931 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 932 | path->slots[0]); |
| 933 | BUG_ON(key.objectid != found_key.objectid); |
| 934 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 935 | path->slots[0]++; |
| 936 | continue; |
| 937 | } |
| 938 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 939 | struct btrfs_extent_ref_v0); |
| 940 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 941 | break; |
| 942 | } |
| 943 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 944 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 945 | |
| 946 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 947 | new_size += sizeof(*bi); |
| 948 | |
| 949 | new_size -= sizeof(*ei0); |
| 950 | ret = btrfs_search_slot(trans, root, &key, path, |
| 951 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 952 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 953 | return ret; |
| 954 | BUG_ON(ret); |
| 955 | |
| 956 | ret = btrfs_extend_item(trans, root, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 957 | |
| 958 | leaf = path->nodes[0]; |
| 959 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 960 | btrfs_set_extent_refs(leaf, item, refs); |
| 961 | /* FIXME: get real generation */ |
| 962 | btrfs_set_extent_generation(leaf, item, 0); |
| 963 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 964 | btrfs_set_extent_flags(leaf, item, |
| 965 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 966 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 967 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 968 | /* FIXME: get first key of the block */ |
| 969 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 970 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 971 | } else { |
| 972 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 973 | } |
| 974 | btrfs_mark_buffer_dirty(leaf); |
| 975 | return 0; |
| 976 | } |
| 977 | #endif |
| 978 | |
| 979 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 980 | { |
| 981 | u32 high_crc = ~(u32)0; |
| 982 | u32 low_crc = ~(u32)0; |
| 983 | __le64 lenum; |
| 984 | |
| 985 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 986 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 987 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 988 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 989 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 990 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 991 | |
| 992 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 993 | } |
| 994 | |
| 995 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 996 | struct btrfs_extent_data_ref *ref) |
| 997 | { |
| 998 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 999 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1000 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1001 | } |
| 1002 | |
| 1003 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1004 | struct btrfs_extent_data_ref *ref, |
| 1005 | u64 root_objectid, u64 owner, u64 offset) |
| 1006 | { |
| 1007 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1008 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1009 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1010 | return 0; |
| 1011 | return 1; |
| 1012 | } |
| 1013 | |
| 1014 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1015 | struct btrfs_root *root, |
| 1016 | struct btrfs_path *path, |
| 1017 | u64 bytenr, u64 parent, |
| 1018 | u64 root_objectid, |
| 1019 | u64 owner, u64 offset) |
| 1020 | { |
| 1021 | struct btrfs_key key; |
| 1022 | struct btrfs_extent_data_ref *ref; |
| 1023 | struct extent_buffer *leaf; |
| 1024 | u32 nritems; |
| 1025 | int ret; |
| 1026 | int recow; |
| 1027 | int err = -ENOENT; |
| 1028 | |
| 1029 | key.objectid = bytenr; |
| 1030 | if (parent) { |
| 1031 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1032 | key.offset = parent; |
| 1033 | } else { |
| 1034 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1035 | key.offset = hash_extent_data_ref(root_objectid, |
| 1036 | owner, offset); |
| 1037 | } |
| 1038 | again: |
| 1039 | recow = 0; |
| 1040 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1041 | if (ret < 0) { |
| 1042 | err = ret; |
| 1043 | goto fail; |
| 1044 | } |
| 1045 | |
| 1046 | if (parent) { |
| 1047 | if (!ret) |
| 1048 | return 0; |
| 1049 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1050 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1051 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1052 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1053 | if (ret < 0) { |
| 1054 | err = ret; |
| 1055 | goto fail; |
| 1056 | } |
| 1057 | if (!ret) |
| 1058 | return 0; |
| 1059 | #endif |
| 1060 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1064 | nritems = btrfs_header_nritems(leaf); |
| 1065 | while (1) { |
| 1066 | if (path->slots[0] >= nritems) { |
| 1067 | ret = btrfs_next_leaf(root, path); |
| 1068 | if (ret < 0) |
| 1069 | err = ret; |
| 1070 | if (ret) |
| 1071 | goto fail; |
| 1072 | |
| 1073 | leaf = path->nodes[0]; |
| 1074 | nritems = btrfs_header_nritems(leaf); |
| 1075 | recow = 1; |
| 1076 | } |
| 1077 | |
| 1078 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1079 | if (key.objectid != bytenr || |
| 1080 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1081 | goto fail; |
| 1082 | |
| 1083 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1084 | struct btrfs_extent_data_ref); |
| 1085 | |
| 1086 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1087 | owner, offset)) { |
| 1088 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1089 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1090 | goto again; |
| 1091 | } |
| 1092 | err = 0; |
| 1093 | break; |
| 1094 | } |
| 1095 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1096 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1097 | fail: |
| 1098 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1099 | } |
| 1100 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1101 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1102 | struct btrfs_root *root, |
| 1103 | struct btrfs_path *path, |
| 1104 | u64 bytenr, u64 parent, |
| 1105 | u64 root_objectid, u64 owner, |
| 1106 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1107 | { |
| 1108 | struct btrfs_key key; |
| 1109 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1110 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1111 | u32 num_refs; |
| 1112 | int ret; |
| 1113 | |
| 1114 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1115 | if (parent) { |
| 1116 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1117 | key.offset = parent; |
| 1118 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1119 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1120 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1121 | key.offset = hash_extent_data_ref(root_objectid, |
| 1122 | owner, offset); |
| 1123 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1124 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1125 | |
| 1126 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1127 | if (ret && ret != -EEXIST) |
| 1128 | goto fail; |
| 1129 | |
| 1130 | leaf = path->nodes[0]; |
| 1131 | if (parent) { |
| 1132 | struct btrfs_shared_data_ref *ref; |
| 1133 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1134 | struct btrfs_shared_data_ref); |
| 1135 | if (ret == 0) { |
| 1136 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1137 | } else { |
| 1138 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1139 | num_refs += refs_to_add; |
| 1140 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1141 | } |
| 1142 | } else { |
| 1143 | struct btrfs_extent_data_ref *ref; |
| 1144 | while (ret == -EEXIST) { |
| 1145 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1146 | struct btrfs_extent_data_ref); |
| 1147 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1148 | owner, offset)) |
| 1149 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1150 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1151 | key.offset++; |
| 1152 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1153 | size); |
| 1154 | if (ret && ret != -EEXIST) |
| 1155 | goto fail; |
| 1156 | |
| 1157 | leaf = path->nodes[0]; |
| 1158 | } |
| 1159 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1160 | struct btrfs_extent_data_ref); |
| 1161 | if (ret == 0) { |
| 1162 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1163 | root_objectid); |
| 1164 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1165 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1166 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1167 | } else { |
| 1168 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1169 | num_refs += refs_to_add; |
| 1170 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1171 | } |
| 1172 | } |
| 1173 | btrfs_mark_buffer_dirty(leaf); |
| 1174 | ret = 0; |
| 1175 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1176 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1177 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1178 | } |
| 1179 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1180 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1181 | struct btrfs_root *root, |
| 1182 | struct btrfs_path *path, |
| 1183 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1184 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1185 | struct btrfs_key key; |
| 1186 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1187 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1188 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1189 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1190 | int ret = 0; |
| 1191 | |
| 1192 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1193 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1194 | |
| 1195 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1196 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1197 | struct btrfs_extent_data_ref); |
| 1198 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1199 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1200 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1201 | struct btrfs_shared_data_ref); |
| 1202 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1203 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1204 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1205 | struct btrfs_extent_ref_v0 *ref0; |
| 1206 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1207 | struct btrfs_extent_ref_v0); |
| 1208 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1209 | #endif |
| 1210 | } else { |
| 1211 | BUG(); |
| 1212 | } |
| 1213 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1214 | BUG_ON(num_refs < refs_to_drop); |
| 1215 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1216 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1217 | if (num_refs == 0) { |
| 1218 | ret = btrfs_del_item(trans, root, path); |
| 1219 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1220 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1221 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1222 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1223 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1224 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1225 | else { |
| 1226 | struct btrfs_extent_ref_v0 *ref0; |
| 1227 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1228 | struct btrfs_extent_ref_v0); |
| 1229 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1230 | } |
| 1231 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1232 | btrfs_mark_buffer_dirty(leaf); |
| 1233 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1234 | return ret; |
| 1235 | } |
| 1236 | |
| 1237 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1238 | struct btrfs_path *path, |
| 1239 | struct btrfs_extent_inline_ref *iref) |
| 1240 | { |
| 1241 | struct btrfs_key key; |
| 1242 | struct extent_buffer *leaf; |
| 1243 | struct btrfs_extent_data_ref *ref1; |
| 1244 | struct btrfs_shared_data_ref *ref2; |
| 1245 | u32 num_refs = 0; |
| 1246 | |
| 1247 | leaf = path->nodes[0]; |
| 1248 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1249 | if (iref) { |
| 1250 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1251 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1252 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1253 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1254 | } else { |
| 1255 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1256 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1257 | } |
| 1258 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1259 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1260 | struct btrfs_extent_data_ref); |
| 1261 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1262 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1263 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1264 | struct btrfs_shared_data_ref); |
| 1265 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1266 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1267 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1268 | struct btrfs_extent_ref_v0 *ref0; |
| 1269 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1270 | struct btrfs_extent_ref_v0); |
| 1271 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1272 | #endif |
| 1273 | } else { |
| 1274 | WARN_ON(1); |
| 1275 | } |
| 1276 | return num_refs; |
| 1277 | } |
| 1278 | |
| 1279 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1280 | struct btrfs_root *root, |
| 1281 | struct btrfs_path *path, |
| 1282 | u64 bytenr, u64 parent, |
| 1283 | u64 root_objectid) |
| 1284 | { |
| 1285 | struct btrfs_key key; |
| 1286 | int ret; |
| 1287 | |
| 1288 | key.objectid = bytenr; |
| 1289 | if (parent) { |
| 1290 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1291 | key.offset = parent; |
| 1292 | } else { |
| 1293 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1294 | key.offset = root_objectid; |
| 1295 | } |
| 1296 | |
| 1297 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1298 | if (ret > 0) |
| 1299 | ret = -ENOENT; |
| 1300 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1301 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1302 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1303 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1304 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1305 | if (ret > 0) |
| 1306 | ret = -ENOENT; |
| 1307 | } |
| 1308 | #endif |
| 1309 | return ret; |
| 1310 | } |
| 1311 | |
| 1312 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1313 | struct btrfs_root *root, |
| 1314 | struct btrfs_path *path, |
| 1315 | u64 bytenr, u64 parent, |
| 1316 | u64 root_objectid) |
| 1317 | { |
| 1318 | struct btrfs_key key; |
| 1319 | int ret; |
| 1320 | |
| 1321 | key.objectid = bytenr; |
| 1322 | if (parent) { |
| 1323 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1324 | key.offset = parent; |
| 1325 | } else { |
| 1326 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1327 | key.offset = root_objectid; |
| 1328 | } |
| 1329 | |
| 1330 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1331 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1332 | return ret; |
| 1333 | } |
| 1334 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1335 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1336 | { |
| 1337 | int type; |
| 1338 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1339 | if (parent > 0) |
| 1340 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1341 | else |
| 1342 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1343 | } else { |
| 1344 | if (parent > 0) |
| 1345 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1346 | else |
| 1347 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1348 | } |
| 1349 | return type; |
| 1350 | } |
| 1351 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1352 | static int find_next_key(struct btrfs_path *path, int level, |
| 1353 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1354 | |
| 1355 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1356 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1357 | if (!path->nodes[level]) |
| 1358 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1359 | if (path->slots[level] + 1 >= |
| 1360 | btrfs_header_nritems(path->nodes[level])) |
| 1361 | continue; |
| 1362 | if (level == 0) |
| 1363 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1364 | path->slots[level] + 1); |
| 1365 | else |
| 1366 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1367 | path->slots[level] + 1); |
| 1368 | return 0; |
| 1369 | } |
| 1370 | return 1; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1375 | * to the address of inline back ref, and 0 is returned. |
| 1376 | * |
| 1377 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1378 | * should be inserted, and -ENOENT is returned. |
| 1379 | * |
| 1380 | * if insert is true and there are too many inline back refs, the path |
| 1381 | * points to the extent item, and -EAGAIN is returned. |
| 1382 | * |
| 1383 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1384 | * items in the tree are ordered. |
| 1385 | */ |
| 1386 | static noinline_for_stack |
| 1387 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1388 | struct btrfs_root *root, |
| 1389 | struct btrfs_path *path, |
| 1390 | struct btrfs_extent_inline_ref **ref_ret, |
| 1391 | u64 bytenr, u64 num_bytes, |
| 1392 | u64 parent, u64 root_objectid, |
| 1393 | u64 owner, u64 offset, int insert) |
| 1394 | { |
| 1395 | struct btrfs_key key; |
| 1396 | struct extent_buffer *leaf; |
| 1397 | struct btrfs_extent_item *ei; |
| 1398 | struct btrfs_extent_inline_ref *iref; |
| 1399 | u64 flags; |
| 1400 | u64 item_size; |
| 1401 | unsigned long ptr; |
| 1402 | unsigned long end; |
| 1403 | int extra_size; |
| 1404 | int type; |
| 1405 | int want; |
| 1406 | int ret; |
| 1407 | int err = 0; |
| 1408 | |
| 1409 | key.objectid = bytenr; |
| 1410 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1411 | key.offset = num_bytes; |
| 1412 | |
| 1413 | want = extent_ref_type(parent, owner); |
| 1414 | if (insert) { |
| 1415 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1416 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1417 | } else |
| 1418 | extra_size = -1; |
| 1419 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1420 | if (ret < 0) { |
| 1421 | err = ret; |
| 1422 | goto out; |
| 1423 | } |
| 1424 | BUG_ON(ret); |
| 1425 | |
| 1426 | leaf = path->nodes[0]; |
| 1427 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1428 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1429 | if (item_size < sizeof(*ei)) { |
| 1430 | if (!insert) { |
| 1431 | err = -ENOENT; |
| 1432 | goto out; |
| 1433 | } |
| 1434 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1435 | extra_size); |
| 1436 | if (ret < 0) { |
| 1437 | err = ret; |
| 1438 | goto out; |
| 1439 | } |
| 1440 | leaf = path->nodes[0]; |
| 1441 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1442 | } |
| 1443 | #endif |
| 1444 | BUG_ON(item_size < sizeof(*ei)); |
| 1445 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1446 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1447 | flags = btrfs_extent_flags(leaf, ei); |
| 1448 | |
| 1449 | ptr = (unsigned long)(ei + 1); |
| 1450 | end = (unsigned long)ei + item_size; |
| 1451 | |
| 1452 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1453 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1454 | BUG_ON(ptr > end); |
| 1455 | } else { |
| 1456 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1457 | } |
| 1458 | |
| 1459 | err = -ENOENT; |
| 1460 | while (1) { |
| 1461 | if (ptr >= end) { |
| 1462 | WARN_ON(ptr > end); |
| 1463 | break; |
| 1464 | } |
| 1465 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1466 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1467 | if (want < type) |
| 1468 | break; |
| 1469 | if (want > type) { |
| 1470 | ptr += btrfs_extent_inline_ref_size(type); |
| 1471 | continue; |
| 1472 | } |
| 1473 | |
| 1474 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1475 | struct btrfs_extent_data_ref *dref; |
| 1476 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1477 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1478 | owner, offset)) { |
| 1479 | err = 0; |
| 1480 | break; |
| 1481 | } |
| 1482 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1483 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1484 | break; |
| 1485 | } else { |
| 1486 | u64 ref_offset; |
| 1487 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1488 | if (parent > 0) { |
| 1489 | if (parent == ref_offset) { |
| 1490 | err = 0; |
| 1491 | break; |
| 1492 | } |
| 1493 | if (ref_offset < parent) |
| 1494 | break; |
| 1495 | } else { |
| 1496 | if (root_objectid == ref_offset) { |
| 1497 | err = 0; |
| 1498 | break; |
| 1499 | } |
| 1500 | if (ref_offset < root_objectid) |
| 1501 | break; |
| 1502 | } |
| 1503 | } |
| 1504 | ptr += btrfs_extent_inline_ref_size(type); |
| 1505 | } |
| 1506 | if (err == -ENOENT && insert) { |
| 1507 | if (item_size + extra_size >= |
| 1508 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1509 | err = -EAGAIN; |
| 1510 | goto out; |
| 1511 | } |
| 1512 | /* |
| 1513 | * To add new inline back ref, we have to make sure |
| 1514 | * there is no corresponding back ref item. |
| 1515 | * For simplicity, we just do not add new inline back |
| 1516 | * ref if there is any kind of item for this block |
| 1517 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1518 | if (find_next_key(path, 0, &key) == 0 && |
| 1519 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1520 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1521 | err = -EAGAIN; |
| 1522 | goto out; |
| 1523 | } |
| 1524 | } |
| 1525 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1526 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1527 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1528 | path->keep_locks = 0; |
| 1529 | btrfs_unlock_up_safe(path, 1); |
| 1530 | } |
| 1531 | return err; |
| 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * helper to add new inline back ref |
| 1536 | */ |
| 1537 | static noinline_for_stack |
| 1538 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1539 | struct btrfs_root *root, |
| 1540 | struct btrfs_path *path, |
| 1541 | struct btrfs_extent_inline_ref *iref, |
| 1542 | u64 parent, u64 root_objectid, |
| 1543 | u64 owner, u64 offset, int refs_to_add, |
| 1544 | struct btrfs_delayed_extent_op *extent_op) |
| 1545 | { |
| 1546 | struct extent_buffer *leaf; |
| 1547 | struct btrfs_extent_item *ei; |
| 1548 | unsigned long ptr; |
| 1549 | unsigned long end; |
| 1550 | unsigned long item_offset; |
| 1551 | u64 refs; |
| 1552 | int size; |
| 1553 | int type; |
| 1554 | int ret; |
| 1555 | |
| 1556 | leaf = path->nodes[0]; |
| 1557 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1558 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1559 | |
| 1560 | type = extent_ref_type(parent, owner); |
| 1561 | size = btrfs_extent_inline_ref_size(type); |
| 1562 | |
| 1563 | ret = btrfs_extend_item(trans, root, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1564 | |
| 1565 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1566 | refs = btrfs_extent_refs(leaf, ei); |
| 1567 | refs += refs_to_add; |
| 1568 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1569 | if (extent_op) |
| 1570 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1571 | |
| 1572 | ptr = (unsigned long)ei + item_offset; |
| 1573 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1574 | if (ptr < end - size) |
| 1575 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1576 | end - size - ptr); |
| 1577 | |
| 1578 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1579 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1580 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1581 | struct btrfs_extent_data_ref *dref; |
| 1582 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1583 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1584 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1585 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1586 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1587 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1588 | struct btrfs_shared_data_ref *sref; |
| 1589 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1590 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1591 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1592 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1593 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1594 | } else { |
| 1595 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1596 | } |
| 1597 | btrfs_mark_buffer_dirty(leaf); |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1602 | struct btrfs_root *root, |
| 1603 | struct btrfs_path *path, |
| 1604 | struct btrfs_extent_inline_ref **ref_ret, |
| 1605 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1606 | u64 root_objectid, u64 owner, u64 offset) |
| 1607 | { |
| 1608 | int ret; |
| 1609 | |
| 1610 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1611 | bytenr, num_bytes, parent, |
| 1612 | root_objectid, owner, offset, 0); |
| 1613 | if (ret != -ENOENT) |
| 1614 | return ret; |
| 1615 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1616 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1617 | *ref_ret = NULL; |
| 1618 | |
| 1619 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1620 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1621 | root_objectid); |
| 1622 | } else { |
| 1623 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1624 | root_objectid, owner, offset); |
| 1625 | } |
| 1626 | return ret; |
| 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * helper to update/remove inline back ref |
| 1631 | */ |
| 1632 | static noinline_for_stack |
| 1633 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1634 | struct btrfs_root *root, |
| 1635 | struct btrfs_path *path, |
| 1636 | struct btrfs_extent_inline_ref *iref, |
| 1637 | int refs_to_mod, |
| 1638 | struct btrfs_delayed_extent_op *extent_op) |
| 1639 | { |
| 1640 | struct extent_buffer *leaf; |
| 1641 | struct btrfs_extent_item *ei; |
| 1642 | struct btrfs_extent_data_ref *dref = NULL; |
| 1643 | struct btrfs_shared_data_ref *sref = NULL; |
| 1644 | unsigned long ptr; |
| 1645 | unsigned long end; |
| 1646 | u32 item_size; |
| 1647 | int size; |
| 1648 | int type; |
| 1649 | int ret; |
| 1650 | u64 refs; |
| 1651 | |
| 1652 | leaf = path->nodes[0]; |
| 1653 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1654 | refs = btrfs_extent_refs(leaf, ei); |
| 1655 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1656 | refs += refs_to_mod; |
| 1657 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1658 | if (extent_op) |
| 1659 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1660 | |
| 1661 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1662 | |
| 1663 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1664 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1665 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1666 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1667 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1668 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1669 | } else { |
| 1670 | refs = 1; |
| 1671 | BUG_ON(refs_to_mod != -1); |
| 1672 | } |
| 1673 | |
| 1674 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1675 | refs += refs_to_mod; |
| 1676 | |
| 1677 | if (refs > 0) { |
| 1678 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1679 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1680 | else |
| 1681 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1682 | } else { |
| 1683 | size = btrfs_extent_inline_ref_size(type); |
| 1684 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1685 | ptr = (unsigned long)iref; |
| 1686 | end = (unsigned long)ei + item_size; |
| 1687 | if (ptr + size < end) |
| 1688 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1689 | end - ptr - size); |
| 1690 | item_size -= size; |
| 1691 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1692 | } |
| 1693 | btrfs_mark_buffer_dirty(leaf); |
| 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | static noinline_for_stack |
| 1698 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1699 | struct btrfs_root *root, |
| 1700 | struct btrfs_path *path, |
| 1701 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1702 | u64 root_objectid, u64 owner, |
| 1703 | u64 offset, int refs_to_add, |
| 1704 | struct btrfs_delayed_extent_op *extent_op) |
| 1705 | { |
| 1706 | struct btrfs_extent_inline_ref *iref; |
| 1707 | int ret; |
| 1708 | |
| 1709 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1710 | bytenr, num_bytes, parent, |
| 1711 | root_objectid, owner, offset, 1); |
| 1712 | if (ret == 0) { |
| 1713 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1714 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1715 | refs_to_add, extent_op); |
| 1716 | } else if (ret == -ENOENT) { |
| 1717 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1718 | parent, root_objectid, |
| 1719 | owner, offset, refs_to_add, |
| 1720 | extent_op); |
| 1721 | } |
| 1722 | return ret; |
| 1723 | } |
| 1724 | |
| 1725 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1726 | struct btrfs_root *root, |
| 1727 | struct btrfs_path *path, |
| 1728 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1729 | u64 owner, u64 offset, int refs_to_add) |
| 1730 | { |
| 1731 | int ret; |
| 1732 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1733 | BUG_ON(refs_to_add != 1); |
| 1734 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1735 | parent, root_objectid); |
| 1736 | } else { |
| 1737 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1738 | parent, root_objectid, |
| 1739 | owner, offset, refs_to_add); |
| 1740 | } |
| 1741 | return ret; |
| 1742 | } |
| 1743 | |
| 1744 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1745 | struct btrfs_root *root, |
| 1746 | struct btrfs_path *path, |
| 1747 | struct btrfs_extent_inline_ref *iref, |
| 1748 | int refs_to_drop, int is_data) |
| 1749 | { |
| 1750 | int ret; |
| 1751 | |
| 1752 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1753 | if (iref) { |
| 1754 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1755 | -refs_to_drop, NULL); |
| 1756 | } else if (is_data) { |
| 1757 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1758 | } else { |
| 1759 | ret = btrfs_del_item(trans, root, path); |
| 1760 | } |
| 1761 | return ret; |
| 1762 | } |
| 1763 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1764 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1765 | u64 start, u64 len) |
| 1766 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1767 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1768 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1769 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1770 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1771 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1772 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1773 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1774 | u64 discarded_bytes = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1775 | struct btrfs_multi_bio *multi = NULL; |
| 1776 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1777 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1778 | /* Tell the block device(s) that the sectors can be discarded */ |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1779 | ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, |
| 1780 | bytenr, &num_bytes, &multi, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1781 | if (!ret) { |
| 1782 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1783 | int i; |
| 1784 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1785 | |
| 1786 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
Josef Bacik | a2ea186 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1787 | if (!stripe->dev->can_discard) |
| 1788 | continue; |
| 1789 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1790 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1791 | stripe->physical, |
| 1792 | stripe->length); |
| 1793 | if (!ret) |
| 1794 | discarded_bytes += stripe->length; |
| 1795 | else if (ret != -EOPNOTSUPP) |
| 1796 | break; |
Josef Bacik | a2ea186 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1797 | |
| 1798 | /* |
| 1799 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1800 | * just ignore the return value so we don't screw up |
| 1801 | * people calling discard_extent. |
| 1802 | */ |
| 1803 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1804 | } |
| 1805 | kfree(multi); |
| 1806 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1807 | |
| 1808 | if (actual_bytes) |
| 1809 | *actual_bytes = discarded_bytes; |
| 1810 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1811 | |
| 1812 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1813 | } |
| 1814 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1815 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1816 | struct btrfs_root *root, |
| 1817 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1818 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1819 | { |
| 1820 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1821 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1822 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1823 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1824 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1825 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1826 | parent, root_objectid, (int)owner, |
| 1827 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1828 | } else { |
| 1829 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1830 | parent, root_objectid, owner, offset, |
| 1831 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1832 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1833 | return ret; |
| 1834 | } |
| 1835 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1836 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1837 | struct btrfs_root *root, |
| 1838 | u64 bytenr, u64 num_bytes, |
| 1839 | u64 parent, u64 root_objectid, |
| 1840 | u64 owner, u64 offset, int refs_to_add, |
| 1841 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1842 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1843 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1844 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1845 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1846 | u64 refs; |
| 1847 | int ret; |
| 1848 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1849 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1850 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1851 | if (!path) |
| 1852 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1853 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1854 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1855 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1856 | /* this will setup the path even if it fails to insert the back ref */ |
| 1857 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1858 | path, bytenr, num_bytes, parent, |
| 1859 | root_objectid, owner, offset, |
| 1860 | refs_to_add, extent_op); |
| 1861 | if (ret == 0) |
| 1862 | goto out; |
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 | if (ret != -EAGAIN) { |
| 1865 | err = ret; |
| 1866 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1867 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1868 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1869 | leaf = path->nodes[0]; |
| 1870 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1871 | refs = btrfs_extent_refs(leaf, item); |
| 1872 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1873 | if (extent_op) |
| 1874 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1875 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1876 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1877 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1878 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1879 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1880 | path->leave_spinning = 1; |
| 1881 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1882 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1883 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1884 | path, bytenr, parent, root_objectid, |
| 1885 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1886 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1887 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1888 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1889 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1890 | } |
| 1891 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1892 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1893 | struct btrfs_root *root, |
| 1894 | struct btrfs_delayed_ref_node *node, |
| 1895 | struct btrfs_delayed_extent_op *extent_op, |
| 1896 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1897 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1898 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1899 | struct btrfs_delayed_data_ref *ref; |
| 1900 | struct btrfs_key ins; |
| 1901 | u64 parent = 0; |
| 1902 | u64 ref_root = 0; |
| 1903 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1904 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1905 | ins.objectid = node->bytenr; |
| 1906 | ins.offset = node->num_bytes; |
| 1907 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1908 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1909 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1910 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1911 | parent = ref->parent; |
| 1912 | else |
| 1913 | ref_root = ref->root; |
| 1914 | |
| 1915 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1916 | if (extent_op) { |
| 1917 | BUG_ON(extent_op->update_key); |
| 1918 | flags |= extent_op->flags_to_set; |
| 1919 | } |
| 1920 | ret = alloc_reserved_file_extent(trans, root, |
| 1921 | parent, ref_root, flags, |
| 1922 | ref->objectid, ref->offset, |
| 1923 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1924 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1925 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1926 | node->num_bytes, parent, |
| 1927 | ref_root, ref->objectid, |
| 1928 | ref->offset, node->ref_mod, |
| 1929 | extent_op); |
| 1930 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1931 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1932 | node->num_bytes, parent, |
| 1933 | ref_root, ref->objectid, |
| 1934 | ref->offset, node->ref_mod, |
| 1935 | extent_op); |
| 1936 | } else { |
| 1937 | BUG(); |
| 1938 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1939 | return ret; |
| 1940 | } |
| 1941 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1942 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1943 | struct extent_buffer *leaf, |
| 1944 | struct btrfs_extent_item *ei) |
| 1945 | { |
| 1946 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1947 | if (extent_op->update_flags) { |
| 1948 | flags |= extent_op->flags_to_set; |
| 1949 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1950 | } |
| 1951 | |
| 1952 | if (extent_op->update_key) { |
| 1953 | struct btrfs_tree_block_info *bi; |
| 1954 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1955 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1956 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1961 | struct btrfs_root *root, |
| 1962 | struct btrfs_delayed_ref_node *node, |
| 1963 | struct btrfs_delayed_extent_op *extent_op) |
| 1964 | { |
| 1965 | struct btrfs_key key; |
| 1966 | struct btrfs_path *path; |
| 1967 | struct btrfs_extent_item *ei; |
| 1968 | struct extent_buffer *leaf; |
| 1969 | u32 item_size; |
| 1970 | int ret; |
| 1971 | int err = 0; |
| 1972 | |
| 1973 | path = btrfs_alloc_path(); |
| 1974 | if (!path) |
| 1975 | return -ENOMEM; |
| 1976 | |
| 1977 | key.objectid = node->bytenr; |
| 1978 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1979 | key.offset = node->num_bytes; |
| 1980 | |
| 1981 | path->reada = 1; |
| 1982 | path->leave_spinning = 1; |
| 1983 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1984 | path, 0, 1); |
| 1985 | if (ret < 0) { |
| 1986 | err = ret; |
| 1987 | goto out; |
| 1988 | } |
| 1989 | if (ret > 0) { |
| 1990 | err = -EIO; |
| 1991 | goto out; |
| 1992 | } |
| 1993 | |
| 1994 | leaf = path->nodes[0]; |
| 1995 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1996 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1997 | if (item_size < sizeof(*ei)) { |
| 1998 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 1999 | path, (u64)-1, 0); |
| 2000 | if (ret < 0) { |
| 2001 | err = ret; |
| 2002 | goto out; |
| 2003 | } |
| 2004 | leaf = path->nodes[0]; |
| 2005 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2006 | } |
| 2007 | #endif |
| 2008 | BUG_ON(item_size < sizeof(*ei)); |
| 2009 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2010 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2011 | |
| 2012 | btrfs_mark_buffer_dirty(leaf); |
| 2013 | out: |
| 2014 | btrfs_free_path(path); |
| 2015 | return err; |
| 2016 | } |
| 2017 | |
| 2018 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2019 | struct btrfs_root *root, |
| 2020 | struct btrfs_delayed_ref_node *node, |
| 2021 | struct btrfs_delayed_extent_op *extent_op, |
| 2022 | int insert_reserved) |
| 2023 | { |
| 2024 | int ret = 0; |
| 2025 | struct btrfs_delayed_tree_ref *ref; |
| 2026 | struct btrfs_key ins; |
| 2027 | u64 parent = 0; |
| 2028 | u64 ref_root = 0; |
| 2029 | |
| 2030 | ins.objectid = node->bytenr; |
| 2031 | ins.offset = node->num_bytes; |
| 2032 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2033 | |
| 2034 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 2035 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2036 | parent = ref->parent; |
| 2037 | else |
| 2038 | ref_root = ref->root; |
| 2039 | |
| 2040 | BUG_ON(node->ref_mod != 1); |
| 2041 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 2042 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 2043 | !extent_op->update_key); |
| 2044 | ret = alloc_reserved_tree_block(trans, root, |
| 2045 | parent, ref_root, |
| 2046 | extent_op->flags_to_set, |
| 2047 | &extent_op->key, |
| 2048 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2049 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2050 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2051 | node->num_bytes, parent, ref_root, |
| 2052 | ref->level, 0, 1, extent_op); |
| 2053 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2054 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2055 | node->num_bytes, parent, ref_root, |
| 2056 | ref->level, 0, 1, extent_op); |
| 2057 | } else { |
| 2058 | BUG(); |
| 2059 | } |
| 2060 | return ret; |
| 2061 | } |
| 2062 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2063 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2064 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2065 | struct btrfs_root *root, |
| 2066 | struct btrfs_delayed_ref_node *node, |
| 2067 | struct btrfs_delayed_extent_op *extent_op, |
| 2068 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2069 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2070 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2071 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2072 | struct btrfs_delayed_ref_head *head; |
| 2073 | /* |
| 2074 | * we've hit the end of the chain and we were supposed |
| 2075 | * to insert this extent into the tree. But, it got |
| 2076 | * deleted before we ever needed to insert it, so all |
| 2077 | * we have to do is clean up the accounting |
| 2078 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2079 | BUG_ON(extent_op); |
| 2080 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2081 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2082 | btrfs_pin_extent(root, node->bytenr, |
| 2083 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2084 | if (head->is_data) { |
| 2085 | ret = btrfs_del_csums(trans, root, |
| 2086 | node->bytenr, |
| 2087 | node->num_bytes); |
| 2088 | BUG_ON(ret); |
| 2089 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2090 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2091 | mutex_unlock(&head->mutex); |
| 2092 | return 0; |
| 2093 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2094 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2095 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2096 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2097 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2098 | insert_reserved); |
| 2099 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2100 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2101 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2102 | insert_reserved); |
| 2103 | else |
| 2104 | BUG(); |
| 2105 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2106 | } |
| 2107 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2108 | static noinline struct btrfs_delayed_ref_node * |
| 2109 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2110 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2111 | struct rb_node *node; |
| 2112 | struct btrfs_delayed_ref_node *ref; |
| 2113 | int action = BTRFS_ADD_DELAYED_REF; |
| 2114 | again: |
| 2115 | /* |
| 2116 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2117 | * this prevents ref count from going down to zero when |
| 2118 | * there still are pending delayed ref. |
| 2119 | */ |
| 2120 | node = rb_prev(&head->node.rb_node); |
| 2121 | while (1) { |
| 2122 | if (!node) |
| 2123 | break; |
| 2124 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2125 | rb_node); |
| 2126 | if (ref->bytenr != head->node.bytenr) |
| 2127 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2128 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2129 | return ref; |
| 2130 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2131 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2132 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2133 | action = BTRFS_DROP_DELAYED_REF; |
| 2134 | goto again; |
| 2135 | } |
| 2136 | return NULL; |
| 2137 | } |
| 2138 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2139 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2140 | struct btrfs_root *root, |
| 2141 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2142 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2143 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2144 | struct btrfs_delayed_ref_node *ref; |
| 2145 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2146 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2147 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2148 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2149 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2150 | |
| 2151 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2152 | while (1) { |
| 2153 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2154 | /* pick a new head ref from the cluster list */ |
| 2155 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2156 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2157 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2158 | locked_ref = list_entry(cluster->next, |
| 2159 | struct btrfs_delayed_ref_head, cluster); |
| 2160 | |
| 2161 | /* grab the lock that says we are going to process |
| 2162 | * all the refs for this head */ |
| 2163 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2164 | |
| 2165 | /* |
| 2166 | * we may have dropped the spin lock to get the head |
| 2167 | * mutex lock, and that might have given someone else |
| 2168 | * time to free the head. If that's true, it has been |
| 2169 | * removed from our list and we can move on. |
| 2170 | */ |
| 2171 | if (ret == -EAGAIN) { |
| 2172 | locked_ref = NULL; |
| 2173 | count++; |
| 2174 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | /* |
| 2179 | * record the must insert reserved flag before we |
| 2180 | * drop the spin lock. |
| 2181 | */ |
| 2182 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2183 | locked_ref->must_insert_reserved = 0; |
| 2184 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2185 | extent_op = locked_ref->extent_op; |
| 2186 | locked_ref->extent_op = NULL; |
| 2187 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2188 | /* |
| 2189 | * locked_ref is the head node, so we have to go one |
| 2190 | * node back for any delayed ref updates |
| 2191 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2192 | ref = select_delayed_ref(locked_ref); |
| 2193 | if (!ref) { |
| 2194 | /* All delayed refs have been processed, Go ahead |
| 2195 | * and send the head node to run_one_delayed_ref, |
| 2196 | * so that any accounting fixes can happen |
| 2197 | */ |
| 2198 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2199 | |
| 2200 | if (extent_op && must_insert_reserved) { |
| 2201 | kfree(extent_op); |
| 2202 | extent_op = NULL; |
| 2203 | } |
| 2204 | |
| 2205 | if (extent_op) { |
| 2206 | spin_unlock(&delayed_refs->lock); |
| 2207 | |
| 2208 | ret = run_delayed_extent_op(trans, root, |
| 2209 | ref, extent_op); |
| 2210 | BUG_ON(ret); |
| 2211 | kfree(extent_op); |
| 2212 | |
| 2213 | cond_resched(); |
| 2214 | spin_lock(&delayed_refs->lock); |
| 2215 | continue; |
| 2216 | } |
| 2217 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2218 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2219 | locked_ref = NULL; |
| 2220 | } |
| 2221 | |
| 2222 | ref->in_tree = 0; |
| 2223 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2224 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2225 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2226 | spin_unlock(&delayed_refs->lock); |
| 2227 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2228 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2229 | must_insert_reserved); |
| 2230 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2231 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2232 | btrfs_put_delayed_ref(ref); |
| 2233 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2234 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2235 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2236 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2237 | spin_lock(&delayed_refs->lock); |
| 2238 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2239 | return count; |
| 2240 | } |
| 2241 | |
| 2242 | /* |
| 2243 | * this starts processing the delayed reference count updates and |
| 2244 | * extent insertions we have queued up so far. count can be |
| 2245 | * 0, which means to process everything in the tree at the start |
| 2246 | * of the run (but not newly added entries), or it can be some target |
| 2247 | * number you'd like to process. |
| 2248 | */ |
| 2249 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2250 | struct btrfs_root *root, unsigned long count) |
| 2251 | { |
| 2252 | struct rb_node *node; |
| 2253 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2254 | struct btrfs_delayed_ref_node *ref; |
| 2255 | struct list_head cluster; |
| 2256 | int ret; |
| 2257 | int run_all = count == (unsigned long)-1; |
| 2258 | int run_most = 0; |
| 2259 | |
| 2260 | if (root == root->fs_info->extent_root) |
| 2261 | root = root->fs_info->tree_root; |
| 2262 | |
| 2263 | delayed_refs = &trans->transaction->delayed_refs; |
| 2264 | INIT_LIST_HEAD(&cluster); |
| 2265 | again: |
| 2266 | spin_lock(&delayed_refs->lock); |
| 2267 | if (count == 0) { |
| 2268 | count = delayed_refs->num_entries * 2; |
| 2269 | run_most = 1; |
| 2270 | } |
| 2271 | while (1) { |
| 2272 | if (!(run_all || run_most) && |
| 2273 | delayed_refs->num_heads_ready < 64) |
| 2274 | break; |
| 2275 | |
| 2276 | /* |
| 2277 | * go find something we can process in the rbtree. We start at |
| 2278 | * the beginning of the tree, and then build a cluster |
| 2279 | * of refs to process starting at the first one we are able to |
| 2280 | * lock |
| 2281 | */ |
| 2282 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2283 | delayed_refs->run_delayed_start); |
| 2284 | if (ret) |
| 2285 | break; |
| 2286 | |
| 2287 | ret = run_clustered_refs(trans, root, &cluster); |
| 2288 | BUG_ON(ret < 0); |
| 2289 | |
| 2290 | count -= min_t(unsigned long, ret, count); |
| 2291 | |
| 2292 | if (count == 0) |
| 2293 | break; |
| 2294 | } |
| 2295 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2296 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2297 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2298 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2299 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2300 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2301 | |
| 2302 | while (node) { |
| 2303 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2304 | rb_node); |
| 2305 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2306 | struct btrfs_delayed_ref_head *head; |
| 2307 | |
| 2308 | head = btrfs_delayed_node_to_head(ref); |
| 2309 | atomic_inc(&ref->refs); |
| 2310 | |
| 2311 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2312 | /* |
| 2313 | * Mutex was contended, block until it's |
| 2314 | * released and try again |
| 2315 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2316 | mutex_lock(&head->mutex); |
| 2317 | mutex_unlock(&head->mutex); |
| 2318 | |
| 2319 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2320 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2321 | goto again; |
| 2322 | } |
| 2323 | node = rb_next(node); |
| 2324 | } |
| 2325 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2326 | schedule_timeout(1); |
| 2327 | goto again; |
| 2328 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2329 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2330 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2331 | return 0; |
| 2332 | } |
| 2333 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2334 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2335 | struct btrfs_root *root, |
| 2336 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2337 | int is_data) |
| 2338 | { |
| 2339 | struct btrfs_delayed_extent_op *extent_op; |
| 2340 | int ret; |
| 2341 | |
| 2342 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2343 | if (!extent_op) |
| 2344 | return -ENOMEM; |
| 2345 | |
| 2346 | extent_op->flags_to_set = flags; |
| 2347 | extent_op->update_flags = 1; |
| 2348 | extent_op->update_key = 0; |
| 2349 | extent_op->is_data = is_data ? 1 : 0; |
| 2350 | |
| 2351 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2352 | if (ret) |
| 2353 | kfree(extent_op); |
| 2354 | return ret; |
| 2355 | } |
| 2356 | |
| 2357 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2358 | struct btrfs_root *root, |
| 2359 | struct btrfs_path *path, |
| 2360 | u64 objectid, u64 offset, u64 bytenr) |
| 2361 | { |
| 2362 | struct btrfs_delayed_ref_head *head; |
| 2363 | struct btrfs_delayed_ref_node *ref; |
| 2364 | struct btrfs_delayed_data_ref *data_ref; |
| 2365 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2366 | struct rb_node *node; |
| 2367 | int ret = 0; |
| 2368 | |
| 2369 | ret = -ENOENT; |
| 2370 | delayed_refs = &trans->transaction->delayed_refs; |
| 2371 | spin_lock(&delayed_refs->lock); |
| 2372 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2373 | if (!head) |
| 2374 | goto out; |
| 2375 | |
| 2376 | if (!mutex_trylock(&head->mutex)) { |
| 2377 | atomic_inc(&head->node.refs); |
| 2378 | spin_unlock(&delayed_refs->lock); |
| 2379 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2380 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2381 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2382 | /* |
| 2383 | * Mutex was contended, block until it's released and let |
| 2384 | * caller try again |
| 2385 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2386 | mutex_lock(&head->mutex); |
| 2387 | mutex_unlock(&head->mutex); |
| 2388 | btrfs_put_delayed_ref(&head->node); |
| 2389 | return -EAGAIN; |
| 2390 | } |
| 2391 | |
| 2392 | node = rb_prev(&head->node.rb_node); |
| 2393 | if (!node) |
| 2394 | goto out_unlock; |
| 2395 | |
| 2396 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2397 | |
| 2398 | if (ref->bytenr != bytenr) |
| 2399 | goto out_unlock; |
| 2400 | |
| 2401 | ret = 1; |
| 2402 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2403 | goto out_unlock; |
| 2404 | |
| 2405 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2406 | |
| 2407 | node = rb_prev(node); |
| 2408 | if (node) { |
| 2409 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2410 | if (ref->bytenr == bytenr) |
| 2411 | goto out_unlock; |
| 2412 | } |
| 2413 | |
| 2414 | if (data_ref->root != root->root_key.objectid || |
| 2415 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2416 | goto out_unlock; |
| 2417 | |
| 2418 | ret = 0; |
| 2419 | out_unlock: |
| 2420 | mutex_unlock(&head->mutex); |
| 2421 | out: |
| 2422 | spin_unlock(&delayed_refs->lock); |
| 2423 | return ret; |
| 2424 | } |
| 2425 | |
| 2426 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2427 | struct btrfs_root *root, |
| 2428 | struct btrfs_path *path, |
| 2429 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2430 | { |
| 2431 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2432 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2433 | struct btrfs_extent_data_ref *ref; |
| 2434 | struct btrfs_extent_inline_ref *iref; |
| 2435 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2436 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2437 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2438 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2439 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2440 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2441 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2442 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2443 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2444 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2445 | if (ret < 0) |
| 2446 | goto out; |
| 2447 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2448 | |
| 2449 | ret = -ENOENT; |
| 2450 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2451 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2452 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2453 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2454 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2455 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2456 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2457 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2458 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2459 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2460 | ret = 1; |
| 2461 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2462 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2463 | if (item_size < sizeof(*ei)) { |
| 2464 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2465 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2466 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2467 | #endif |
| 2468 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2469 | |
| 2470 | if (item_size != sizeof(*ei) + |
| 2471 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2472 | goto out; |
| 2473 | |
| 2474 | if (btrfs_extent_generation(leaf, ei) <= |
| 2475 | btrfs_root_last_snapshot(&root->root_item)) |
| 2476 | goto out; |
| 2477 | |
| 2478 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2479 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2480 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2481 | goto out; |
| 2482 | |
| 2483 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2484 | if (btrfs_extent_refs(leaf, ei) != |
| 2485 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2486 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2487 | root->root_key.objectid || |
| 2488 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2489 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2490 | goto out; |
| 2491 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2492 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2493 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2494 | return ret; |
| 2495 | } |
| 2496 | |
| 2497 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2498 | struct btrfs_root *root, |
| 2499 | u64 objectid, u64 offset, u64 bytenr) |
| 2500 | { |
| 2501 | struct btrfs_path *path; |
| 2502 | int ret; |
| 2503 | int ret2; |
| 2504 | |
| 2505 | path = btrfs_alloc_path(); |
| 2506 | if (!path) |
| 2507 | return -ENOENT; |
| 2508 | |
| 2509 | do { |
| 2510 | ret = check_committed_ref(trans, root, path, objectid, |
| 2511 | offset, bytenr); |
| 2512 | if (ret && ret != -ENOENT) |
| 2513 | goto out; |
| 2514 | |
| 2515 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2516 | offset, bytenr); |
| 2517 | } while (ret2 == -EAGAIN); |
| 2518 | |
| 2519 | if (ret2 && ret2 != -ENOENT) { |
| 2520 | ret = ret2; |
| 2521 | goto out; |
| 2522 | } |
| 2523 | |
| 2524 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2525 | ret = 0; |
| 2526 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2527 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2528 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2529 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2530 | return ret; |
| 2531 | } |
| 2532 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2533 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2534 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2535 | struct extent_buffer *buf, |
| 2536 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2537 | { |
| 2538 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2539 | u64 num_bytes; |
| 2540 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2541 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2542 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2543 | struct btrfs_key key; |
| 2544 | struct btrfs_file_extent_item *fi; |
| 2545 | int i; |
| 2546 | int level; |
| 2547 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2548 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2549 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2550 | |
| 2551 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2552 | nritems = btrfs_header_nritems(buf); |
| 2553 | level = btrfs_header_level(buf); |
| 2554 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2555 | if (!root->ref_cows && level == 0) |
| 2556 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2557 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2558 | if (inc) |
| 2559 | process_func = btrfs_inc_extent_ref; |
| 2560 | else |
| 2561 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2562 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2563 | if (full_backref) |
| 2564 | parent = buf->start; |
| 2565 | else |
| 2566 | parent = 0; |
| 2567 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2568 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2569 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2570 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2571 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2572 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2573 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2574 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2575 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2576 | BTRFS_FILE_EXTENT_INLINE) |
| 2577 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2578 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2579 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2580 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2581 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2582 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2583 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2584 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2585 | parent, ref_root, key.objectid, |
| 2586 | key.offset); |
| 2587 | if (ret) |
| 2588 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2589 | } else { |
| 2590 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2591 | num_bytes = btrfs_level_size(root, level - 1); |
| 2592 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2593 | parent, ref_root, level - 1, 0); |
| 2594 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2595 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2596 | } |
| 2597 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2598 | return 0; |
| 2599 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2600 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2601 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2602 | } |
| 2603 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2604 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2605 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2606 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2607 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2608 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2609 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2610 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2611 | struct extent_buffer *buf, int full_backref) |
| 2612 | { |
| 2613 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2614 | } |
| 2615 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2616 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2617 | struct btrfs_root *root, |
| 2618 | struct btrfs_path *path, |
| 2619 | struct btrfs_block_group_cache *cache) |
| 2620 | { |
| 2621 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2622 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2623 | unsigned long bi; |
| 2624 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2625 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2626 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2627 | if (ret < 0) |
| 2628 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2629 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2630 | |
| 2631 | leaf = path->nodes[0]; |
| 2632 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2633 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2634 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2635 | btrfs_release_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2636 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2637 | if (ret) |
| 2638 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2639 | return 0; |
| 2640 | |
| 2641 | } |
| 2642 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2643 | static struct btrfs_block_group_cache * |
| 2644 | next_block_group(struct btrfs_root *root, |
| 2645 | struct btrfs_block_group_cache *cache) |
| 2646 | { |
| 2647 | struct rb_node *node; |
| 2648 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2649 | node = rb_next(&cache->cache_node); |
| 2650 | btrfs_put_block_group(cache); |
| 2651 | if (node) { |
| 2652 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2653 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2654 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2655 | } else |
| 2656 | cache = NULL; |
| 2657 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2658 | return cache; |
| 2659 | } |
| 2660 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2661 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 2662 | struct btrfs_trans_handle *trans, |
| 2663 | struct btrfs_path *path) |
| 2664 | { |
| 2665 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 2666 | struct inode *inode = NULL; |
| 2667 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2668 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2669 | int num_pages = 0; |
| 2670 | int retries = 0; |
| 2671 | int ret = 0; |
| 2672 | |
| 2673 | /* |
| 2674 | * If this block group is smaller than 100 megs don't bother caching the |
| 2675 | * block group. |
| 2676 | */ |
| 2677 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 2678 | spin_lock(&block_group->lock); |
| 2679 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2680 | spin_unlock(&block_group->lock); |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
| 2684 | again: |
| 2685 | inode = lookup_free_space_inode(root, block_group, path); |
| 2686 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 2687 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2688 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2689 | goto out; |
| 2690 | } |
| 2691 | |
| 2692 | if (IS_ERR(inode)) { |
| 2693 | BUG_ON(retries); |
| 2694 | retries++; |
| 2695 | |
| 2696 | if (block_group->ro) |
| 2697 | goto out_free; |
| 2698 | |
| 2699 | ret = create_free_space_inode(root, trans, block_group, path); |
| 2700 | if (ret) |
| 2701 | goto out_free; |
| 2702 | goto again; |
| 2703 | } |
| 2704 | |
| 2705 | /* |
| 2706 | * We want to set the generation to 0, that way if anything goes wrong |
| 2707 | * from here on out we know not to trust this cache when we load up next |
| 2708 | * time. |
| 2709 | */ |
| 2710 | BTRFS_I(inode)->generation = 0; |
| 2711 | ret = btrfs_update_inode(trans, root, inode); |
| 2712 | WARN_ON(ret); |
| 2713 | |
| 2714 | if (i_size_read(inode) > 0) { |
| 2715 | ret = btrfs_truncate_free_space_cache(root, trans, path, |
| 2716 | inode); |
| 2717 | if (ret) |
| 2718 | goto out_put; |
| 2719 | } |
| 2720 | |
| 2721 | spin_lock(&block_group->lock); |
| 2722 | if (block_group->cached != BTRFS_CACHE_FINISHED) { |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2723 | /* We're not cached, don't bother trying to write stuff out */ |
| 2724 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2725 | spin_unlock(&block_group->lock); |
| 2726 | goto out_put; |
| 2727 | } |
| 2728 | spin_unlock(&block_group->lock); |
| 2729 | |
| 2730 | num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024); |
| 2731 | if (!num_pages) |
| 2732 | num_pages = 1; |
| 2733 | |
| 2734 | /* |
| 2735 | * Just to make absolutely sure we have enough space, we're going to |
| 2736 | * preallocate 12 pages worth of space for each block group. In |
| 2737 | * practice we ought to use at most 8, but we need extra space so we can |
| 2738 | * add our header and have a terminator between the extents and the |
| 2739 | * bitmaps. |
| 2740 | */ |
| 2741 | num_pages *= 16; |
| 2742 | num_pages *= PAGE_CACHE_SIZE; |
| 2743 | |
| 2744 | ret = btrfs_check_data_free_space(inode, num_pages); |
| 2745 | if (ret) |
| 2746 | goto out_put; |
| 2747 | |
| 2748 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 2749 | num_pages, num_pages, |
| 2750 | &alloc_hint); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2751 | if (!ret) |
| 2752 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2753 | btrfs_free_reserved_data_space(inode, num_pages); |
| 2754 | out_put: |
| 2755 | iput(inode); |
| 2756 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2757 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2758 | out: |
| 2759 | spin_lock(&block_group->lock); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2760 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2761 | spin_unlock(&block_group->lock); |
| 2762 | |
| 2763 | return ret; |
| 2764 | } |
| 2765 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2766 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2767 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2768 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2769 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2770 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2771 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2772 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2773 | |
| 2774 | path = btrfs_alloc_path(); |
| 2775 | if (!path) |
| 2776 | return -ENOMEM; |
| 2777 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2778 | again: |
| 2779 | while (1) { |
| 2780 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2781 | while (cache) { |
| 2782 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 2783 | break; |
| 2784 | cache = next_block_group(root, cache); |
| 2785 | } |
| 2786 | if (!cache) { |
| 2787 | if (last == 0) |
| 2788 | break; |
| 2789 | last = 0; |
| 2790 | continue; |
| 2791 | } |
| 2792 | err = cache_save_setup(cache, trans, path); |
| 2793 | last = cache->key.objectid + cache->key.offset; |
| 2794 | btrfs_put_block_group(cache); |
| 2795 | } |
| 2796 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2797 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2798 | if (last == 0) { |
| 2799 | err = btrfs_run_delayed_refs(trans, root, |
| 2800 | (unsigned long)-1); |
| 2801 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2802 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2803 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2804 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2805 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2806 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 2807 | btrfs_put_block_group(cache); |
| 2808 | goto again; |
| 2809 | } |
| 2810 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2811 | if (cache->dirty) |
| 2812 | break; |
| 2813 | cache = next_block_group(root, cache); |
| 2814 | } |
| 2815 | if (!cache) { |
| 2816 | if (last == 0) |
| 2817 | break; |
| 2818 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2819 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2820 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2821 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2822 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 2823 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2824 | cache->dirty = 0; |
| 2825 | last = cache->key.objectid + cache->key.offset; |
| 2826 | |
| 2827 | err = write_one_cache_group(trans, root, path, cache); |
| 2828 | BUG_ON(err); |
| 2829 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2830 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2831 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2832 | while (1) { |
| 2833 | /* |
| 2834 | * I don't think this is needed since we're just marking our |
| 2835 | * preallocated extent as written, but just in case it can't |
| 2836 | * hurt. |
| 2837 | */ |
| 2838 | if (last == 0) { |
| 2839 | err = btrfs_run_delayed_refs(trans, root, |
| 2840 | (unsigned long)-1); |
| 2841 | BUG_ON(err); |
| 2842 | } |
| 2843 | |
| 2844 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2845 | while (cache) { |
| 2846 | /* |
| 2847 | * Really this shouldn't happen, but it could if we |
| 2848 | * couldn't write the entire preallocated extent and |
| 2849 | * splitting the extent resulted in a new block. |
| 2850 | */ |
| 2851 | if (cache->dirty) { |
| 2852 | btrfs_put_block_group(cache); |
| 2853 | goto again; |
| 2854 | } |
| 2855 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2856 | break; |
| 2857 | cache = next_block_group(root, cache); |
| 2858 | } |
| 2859 | if (!cache) { |
| 2860 | if (last == 0) |
| 2861 | break; |
| 2862 | last = 0; |
| 2863 | continue; |
| 2864 | } |
| 2865 | |
| 2866 | btrfs_write_out_cache(root, trans, cache, path); |
| 2867 | |
| 2868 | /* |
| 2869 | * If we didn't have an error then the cache state is still |
| 2870 | * NEED_WRITE, so we can set it to WRITTEN. |
| 2871 | */ |
| 2872 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2873 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2874 | last = cache->key.objectid + cache->key.offset; |
| 2875 | btrfs_put_block_group(cache); |
| 2876 | } |
| 2877 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2878 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2879 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2880 | } |
| 2881 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2882 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2883 | { |
| 2884 | struct btrfs_block_group_cache *block_group; |
| 2885 | int readonly = 0; |
| 2886 | |
| 2887 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2888 | if (!block_group || block_group->ro) |
| 2889 | readonly = 1; |
| 2890 | if (block_group) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2891 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2892 | return readonly; |
| 2893 | } |
| 2894 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2895 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2896 | u64 total_bytes, u64 bytes_used, |
| 2897 | struct btrfs_space_info **space_info) |
| 2898 | { |
| 2899 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2900 | int i; |
| 2901 | int factor; |
| 2902 | |
| 2903 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 2904 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2905 | factor = 2; |
| 2906 | else |
| 2907 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2908 | |
| 2909 | found = __find_space_info(info, flags); |
| 2910 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2911 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2912 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2913 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2914 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2915 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2916 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2917 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2918 | *space_info = found; |
| 2919 | return 0; |
| 2920 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2921 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2922 | if (!found) |
| 2923 | return -ENOMEM; |
| 2924 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2925 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 2926 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2927 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2928 | spin_lock_init(&found->lock); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2929 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
| 2930 | BTRFS_BLOCK_GROUP_SYSTEM | |
| 2931 | BTRFS_BLOCK_GROUP_METADATA); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2932 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2933 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2934 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2935 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2936 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2937 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2938 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2939 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2940 | found->full = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 2941 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 2942 | found->chunk_alloc = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2943 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2944 | list_add_rcu(&found->list, &info->space_info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2945 | atomic_set(&found->caching_threads, 0); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2946 | return 0; |
| 2947 | } |
| 2948 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2949 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2950 | { |
| 2951 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2952 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2953 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2954 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2955 | if (extra_flags) { |
| 2956 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2957 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2958 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2959 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2960 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2961 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2962 | } |
| 2963 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2964 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2965 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2966 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2967 | /* |
| 2968 | * we add in the count of missing devices because we want |
| 2969 | * to make sure that any RAID levels on a degraded FS |
| 2970 | * continue to be honored. |
| 2971 | */ |
| 2972 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 2973 | root->fs_info->fs_devices->missing_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2974 | |
| 2975 | if (num_devices == 1) |
| 2976 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2977 | if (num_devices < 4) |
| 2978 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2979 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2980 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 2981 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2982 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2983 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2984 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2985 | |
| 2986 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2987 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2988 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2989 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2990 | |
| 2991 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 2992 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 2993 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 2994 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 2995 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 2996 | return flags; |
| 2997 | } |
| 2998 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2999 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3000 | { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3001 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3002 | flags |= root->fs_info->avail_data_alloc_bits & |
| 3003 | root->fs_info->data_alloc_profile; |
| 3004 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3005 | flags |= root->fs_info->avail_system_alloc_bits & |
| 3006 | root->fs_info->system_alloc_profile; |
| 3007 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3008 | flags |= root->fs_info->avail_metadata_alloc_bits & |
| 3009 | root->fs_info->metadata_alloc_profile; |
| 3010 | return btrfs_reduce_alloc_profile(root, flags); |
| 3011 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3012 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3013 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3014 | { |
| 3015 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3016 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3017 | if (data) |
| 3018 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3019 | else if (root == root->fs_info->chunk_root) |
| 3020 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3021 | else |
| 3022 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3023 | |
| 3024 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 3028 | { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3029 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3030 | BTRFS_BLOCK_GROUP_DATA); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | /* |
| 3034 | * This will check the space that the inode allocates from to make sure we have |
| 3035 | * enough space for bytes. |
| 3036 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3037 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3038 | { |
| 3039 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3040 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | ab6e241 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3041 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3042 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3043 | |
| 3044 | /* make sure bytes are sectorsize aligned */ |
| 3045 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3046 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3047 | if (root == root->fs_info->tree_root || |
| 3048 | BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3049 | alloc_chunk = 0; |
| 3050 | committed = 1; |
| 3051 | } |
| 3052 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3053 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3054 | if (!data_sinfo) |
| 3055 | goto alloc; |
| 3056 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3057 | again: |
| 3058 | /* make sure we have enough space to handle the data first */ |
| 3059 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3060 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3061 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3062 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e241 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3063 | |
| 3064 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3065 | struct btrfs_trans_handle *trans; |
| 3066 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3067 | /* |
| 3068 | * if we don't have enough free bytes in this space then we need |
| 3069 | * to alloc a new chunk. |
| 3070 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3071 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3072 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3073 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3074 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3075 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3076 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3077 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3078 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3079 | if (IS_ERR(trans)) |
| 3080 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3081 | |
| 3082 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3083 | bytes + 2 * 1024 * 1024, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3084 | alloc_target, |
| 3085 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3086 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3087 | if (ret < 0) { |
| 3088 | if (ret != -ENOSPC) |
| 3089 | return ret; |
| 3090 | else |
| 3091 | goto commit_trans; |
| 3092 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3093 | |
| 3094 | if (!data_sinfo) { |
| 3095 | btrfs_set_inode_space_info(root, inode); |
| 3096 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3097 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3098 | goto again; |
| 3099 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3100 | |
| 3101 | /* |
| 3102 | * If we have less pinned bytes than we want to allocate then |
| 3103 | * don't bother committing the transaction, it won't help us. |
| 3104 | */ |
| 3105 | if (data_sinfo->bytes_pinned < bytes) |
| 3106 | committed = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3107 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3108 | |
| 3109 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3110 | commit_trans: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3111 | if (!committed && |
| 3112 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3113 | committed = 1; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3114 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3115 | if (IS_ERR(trans)) |
| 3116 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3117 | ret = btrfs_commit_transaction(trans, root); |
| 3118 | if (ret) |
| 3119 | return ret; |
| 3120 | goto again; |
| 3121 | } |
| 3122 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3123 | return -ENOSPC; |
| 3124 | } |
| 3125 | data_sinfo->bytes_may_use += bytes; |
| 3126 | BTRFS_I(inode)->reserved_bytes += bytes; |
| 3127 | spin_unlock(&data_sinfo->lock); |
| 3128 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3129 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | /* |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3133 | * called when we are clearing an delalloc extent from the |
| 3134 | * inode's io_tree or there was an error for whatever reason |
| 3135 | * after calling btrfs_check_data_free_space |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3136 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3137 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3138 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3139 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3140 | struct btrfs_space_info *data_sinfo; |
| 3141 | |
| 3142 | /* make sure bytes are sectorsize aligned */ |
| 3143 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3144 | |
| 3145 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3146 | spin_lock(&data_sinfo->lock); |
| 3147 | data_sinfo->bytes_may_use -= bytes; |
| 3148 | BTRFS_I(inode)->reserved_bytes -= bytes; |
| 3149 | spin_unlock(&data_sinfo->lock); |
| 3150 | } |
| 3151 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3152 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3153 | { |
| 3154 | struct list_head *head = &info->space_info; |
| 3155 | struct btrfs_space_info *found; |
| 3156 | |
| 3157 | rcu_read_lock(); |
| 3158 | list_for_each_entry_rcu(found, head, list) { |
| 3159 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3160 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3161 | } |
| 3162 | rcu_read_unlock(); |
| 3163 | } |
| 3164 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3165 | static int should_alloc_chunk(struct btrfs_root *root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3166 | struct btrfs_space_info *sinfo, u64 alloc_bytes, |
| 3167 | int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3168 | { |
| 3169 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3170 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3171 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3172 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3173 | if (force == CHUNK_ALLOC_FORCE) |
| 3174 | return 1; |
| 3175 | |
| 3176 | /* |
| 3177 | * in limited mode, we want to have some free space up to |
| 3178 | * about 1% of the FS size. |
| 3179 | */ |
| 3180 | if (force == CHUNK_ALLOC_LIMITED) { |
| 3181 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 3182 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 3183 | div_factor_fine(thresh, 1)); |
| 3184 | |
| 3185 | if (num_bytes - num_allocated < thresh) |
| 3186 | return 1; |
| 3187 | } |
| 3188 | |
| 3189 | /* |
| 3190 | * we have two similar checks here, one based on percentage |
| 3191 | * and once based on a hard number of 256MB. The idea |
| 3192 | * is that if we have a good amount of free |
| 3193 | * room, don't allocate a chunk. A good mount is |
| 3194 | * less than 80% utilized of the chunks we have allocated, |
| 3195 | * or more than 256MB free |
| 3196 | */ |
| 3197 | if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3198 | return 0; |
| 3199 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3200 | if (num_allocated + alloc_bytes < div_factor(num_bytes, 8)) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3201 | return 0; |
| 3202 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3203 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3204 | |
| 3205 | /* 256MB or 5% of the FS */ |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3206 | thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5)); |
| 3207 | |
| 3208 | if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3209 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3210 | return 1; |
| 3211 | } |
| 3212 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3213 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3214 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3215 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3216 | { |
| 3217 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3218 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3219 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3220 | int ret = 0; |
| 3221 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3222 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3223 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3224 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3225 | if (!space_info) { |
| 3226 | ret = update_space_info(extent_root->fs_info, flags, |
| 3227 | 0, 0, &space_info); |
| 3228 | BUG_ON(ret); |
| 3229 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3230 | BUG_ON(!space_info); |
| 3231 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3232 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3233 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3234 | if (space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3235 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3236 | if (space_info->full) { |
| 3237 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3238 | return 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3239 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3240 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3241 | if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3242 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3243 | return 0; |
| 3244 | } else if (space_info->chunk_alloc) { |
| 3245 | wait_for_alloc = 1; |
| 3246 | } else { |
| 3247 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3248 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3249 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3250 | spin_unlock(&space_info->lock); |
| 3251 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3252 | mutex_lock(&fs_info->chunk_mutex); |
| 3253 | |
| 3254 | /* |
| 3255 | * The chunk_mutex is held throughout the entirety of a chunk |
| 3256 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 3257 | * other guy is done and we need to recheck and see if we should |
| 3258 | * allocate. |
| 3259 | */ |
| 3260 | if (wait_for_alloc) { |
| 3261 | mutex_unlock(&fs_info->chunk_mutex); |
| 3262 | wait_for_alloc = 0; |
| 3263 | goto again; |
| 3264 | } |
| 3265 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3266 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3267 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3268 | * allocating mixed chunks instead of individual chunks. |
| 3269 | */ |
| 3270 | if (btrfs_mixed_space_info(space_info)) |
| 3271 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3272 | |
| 3273 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3274 | * if we're doing a data chunk, go ahead and make sure that |
| 3275 | * we keep a reasonable number of metadata chunks allocated in the |
| 3276 | * FS as well. |
| 3277 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3278 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3279 | fs_info->data_chunk_allocations++; |
| 3280 | if (!(fs_info->data_chunk_allocations % |
| 3281 | fs_info->metadata_ratio)) |
| 3282 | force_metadata_allocation(fs_info); |
| 3283 | } |
| 3284 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3285 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3286 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3287 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3288 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3289 | else |
| 3290 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3291 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3292 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3293 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3294 | spin_unlock(&space_info->lock); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3295 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3296 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3297 | } |
| 3298 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3299 | /* |
| 3300 | * shrink metadata reservation for delalloc |
| 3301 | */ |
| 3302 | static int shrink_delalloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3303 | struct btrfs_root *root, u64 to_reclaim, int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3304 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3305 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3306 | struct btrfs_space_info *space_info; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3307 | u64 reserved; |
| 3308 | u64 max_reclaim; |
| 3309 | u64 reclaimed = 0; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3310 | long time_left; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3311 | int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3312 | int loops = 0; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3313 | unsigned long progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3314 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3315 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3316 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3317 | |
| 3318 | smp_mb(); |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3319 | reserved = space_info->bytes_reserved; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3320 | progress = space_info->reservation_progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3321 | |
| 3322 | if (reserved == 0) |
| 3323 | return 0; |
| 3324 | |
| 3325 | max_reclaim = min(reserved, to_reclaim); |
| 3326 | |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3327 | while (loops < 1024) { |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3328 | /* have the flusher threads jump in and do some IO */ |
| 3329 | smp_mb(); |
| 3330 | nr_pages = min_t(unsigned long, nr_pages, |
| 3331 | root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); |
| 3332 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3333 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3334 | spin_lock(&space_info->lock); |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3335 | if (reserved > space_info->bytes_reserved) |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3336 | reclaimed += reserved - space_info->bytes_reserved; |
| 3337 | reserved = space_info->bytes_reserved; |
| 3338 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3339 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3340 | loops++; |
| 3341 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3342 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3343 | break; |
| 3344 | |
| 3345 | if (trans && trans->transaction->blocked) |
| 3346 | return -EAGAIN; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3347 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3348 | time_left = schedule_timeout_interruptible(1); |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3349 | |
| 3350 | /* We were interrupted, exit */ |
| 3351 | if (time_left) |
| 3352 | break; |
| 3353 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3354 | /* we've kicked the IO a few times, if anything has been freed, |
| 3355 | * exit. There is no sense in looping here for a long time |
| 3356 | * when we really need to commit the transaction, or there are |
| 3357 | * just too many writers without enough free space |
| 3358 | */ |
| 3359 | |
| 3360 | if (loops > 3) { |
| 3361 | smp_mb(); |
| 3362 | if (progress != space_info->reservation_progress) |
| 3363 | break; |
| 3364 | } |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3365 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3366 | } |
| 3367 | return reclaimed >= to_reclaim; |
| 3368 | } |
| 3369 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3370 | /* |
| 3371 | * Retries tells us how many times we've called reserve_metadata_bytes. The |
| 3372 | * idea is if this is the first call (retries == 0) then we will add to our |
| 3373 | * reserved count if we can't make the allocation in order to hold our place |
| 3374 | * while we go and try and free up space. That way for retries > 1 we don't try |
| 3375 | * and add space, we just check to see if the amount of unused space is >= the |
| 3376 | * total space, meaning that our reservation is valid. |
| 3377 | * |
| 3378 | * However if we don't intend to retry this reservation, pass -1 as retries so |
| 3379 | * that it short circuits this logic. |
| 3380 | */ |
| 3381 | static int reserve_metadata_bytes(struct btrfs_trans_handle *trans, |
| 3382 | struct btrfs_root *root, |
| 3383 | struct btrfs_block_rsv *block_rsv, |
| 3384 | u64 orig_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3385 | { |
| 3386 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3387 | u64 unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3388 | u64 num_bytes = orig_bytes; |
| 3389 | int retries = 0; |
| 3390 | int ret = 0; |
| 3391 | bool reserved = false; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3392 | bool committed = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3393 | |
| 3394 | again: |
| 3395 | ret = -ENOSPC; |
| 3396 | if (reserved) |
| 3397 | num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3398 | |
| 3399 | spin_lock(&space_info->lock); |
| 3400 | unused = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3401 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3402 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3403 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3404 | /* |
| 3405 | * The idea here is that we've not already over-reserved the block group |
| 3406 | * then we can go ahead and save our reservation first and then start |
| 3407 | * flushing if we need to. Otherwise if we've already overcommitted |
| 3408 | * lets start flushing stuff first and then come back and try to make |
| 3409 | * our reservation. |
| 3410 | */ |
| 3411 | if (unused <= space_info->total_bytes) { |
Arne Jansen | 6f33434 | 2010-11-12 23:17:56 +0000 | [diff] [blame] | 3412 | unused = space_info->total_bytes - unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3413 | if (unused >= num_bytes) { |
| 3414 | if (!reserved) |
| 3415 | space_info->bytes_reserved += orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3416 | ret = 0; |
| 3417 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3418 | /* |
| 3419 | * Ok set num_bytes to orig_bytes since we aren't |
| 3420 | * overocmmitted, this way we only try and reclaim what |
| 3421 | * we need. |
| 3422 | */ |
| 3423 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3424 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3425 | } else { |
| 3426 | /* |
| 3427 | * Ok we're over committed, set num_bytes to the overcommitted |
| 3428 | * amount plus the amount of bytes that we need for this |
| 3429 | * reservation. |
| 3430 | */ |
| 3431 | num_bytes = unused - space_info->total_bytes + |
| 3432 | (orig_bytes * (retries + 1)); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3433 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3434 | |
| 3435 | /* |
| 3436 | * Couldn't make our reservation, save our place so while we're trying |
| 3437 | * to reclaim space we can actually use it instead of somebody else |
| 3438 | * stealing it from us. |
| 3439 | */ |
| 3440 | if (ret && !reserved) { |
| 3441 | space_info->bytes_reserved += orig_bytes; |
| 3442 | reserved = true; |
| 3443 | } |
| 3444 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3445 | spin_unlock(&space_info->lock); |
| 3446 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3447 | if (!ret) |
| 3448 | return 0; |
| 3449 | |
| 3450 | if (!flush) |
| 3451 | goto out; |
| 3452 | |
| 3453 | /* |
| 3454 | * We do synchronous shrinking since we don't actually unreserve |
| 3455 | * metadata until after the IO is completed. |
| 3456 | */ |
| 3457 | ret = shrink_delalloc(trans, root, num_bytes, 1); |
| 3458 | if (ret > 0) |
| 3459 | return 0; |
| 3460 | else if (ret < 0) |
| 3461 | goto out; |
| 3462 | |
| 3463 | /* |
| 3464 | * So if we were overcommitted it's possible that somebody else flushed |
| 3465 | * out enough space and we simply didn't have enough space to reclaim, |
| 3466 | * so go back around and try again. |
| 3467 | */ |
| 3468 | if (retries < 2) { |
| 3469 | retries++; |
| 3470 | goto again; |
| 3471 | } |
| 3472 | |
| 3473 | spin_lock(&space_info->lock); |
| 3474 | /* |
| 3475 | * Not enough space to be reclaimed, don't bother committing the |
| 3476 | * transaction. |
| 3477 | */ |
| 3478 | if (space_info->bytes_pinned < orig_bytes) |
| 3479 | ret = -ENOSPC; |
| 3480 | spin_unlock(&space_info->lock); |
| 3481 | if (ret) |
| 3482 | goto out; |
| 3483 | |
| 3484 | ret = -EAGAIN; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3485 | if (trans || committed) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3486 | goto out; |
| 3487 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3488 | ret = -ENOSPC; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3489 | trans = btrfs_join_transaction(root); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3490 | if (IS_ERR(trans)) |
| 3491 | goto out; |
| 3492 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3493 | if (!ret) { |
| 3494 | trans = NULL; |
| 3495 | committed = true; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3496 | goto again; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3497 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3498 | |
| 3499 | out: |
| 3500 | if (reserved) { |
| 3501 | spin_lock(&space_info->lock); |
| 3502 | space_info->bytes_reserved -= orig_bytes; |
| 3503 | spin_unlock(&space_info->lock); |
| 3504 | } |
| 3505 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3506 | return ret; |
| 3507 | } |
| 3508 | |
| 3509 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3510 | struct btrfs_root *root) |
| 3511 | { |
| 3512 | struct btrfs_block_rsv *block_rsv; |
| 3513 | if (root->ref_cows) |
| 3514 | block_rsv = trans->block_rsv; |
| 3515 | else |
| 3516 | block_rsv = root->block_rsv; |
| 3517 | |
| 3518 | if (!block_rsv) |
| 3519 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3520 | |
| 3521 | return block_rsv; |
| 3522 | } |
| 3523 | |
| 3524 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3525 | u64 num_bytes) |
| 3526 | { |
| 3527 | int ret = -ENOSPC; |
| 3528 | spin_lock(&block_rsv->lock); |
| 3529 | if (block_rsv->reserved >= num_bytes) { |
| 3530 | block_rsv->reserved -= num_bytes; |
| 3531 | if (block_rsv->reserved < block_rsv->size) |
| 3532 | block_rsv->full = 0; |
| 3533 | ret = 0; |
| 3534 | } |
| 3535 | spin_unlock(&block_rsv->lock); |
| 3536 | return ret; |
| 3537 | } |
| 3538 | |
| 3539 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3540 | u64 num_bytes, int update_size) |
| 3541 | { |
| 3542 | spin_lock(&block_rsv->lock); |
| 3543 | block_rsv->reserved += num_bytes; |
| 3544 | if (update_size) |
| 3545 | block_rsv->size += num_bytes; |
| 3546 | else if (block_rsv->reserved >= block_rsv->size) |
| 3547 | block_rsv->full = 1; |
| 3548 | spin_unlock(&block_rsv->lock); |
| 3549 | } |
| 3550 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 3551 | static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3552 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3553 | { |
| 3554 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3555 | |
| 3556 | spin_lock(&block_rsv->lock); |
| 3557 | if (num_bytes == (u64)-1) |
| 3558 | num_bytes = block_rsv->size; |
| 3559 | block_rsv->size -= num_bytes; |
| 3560 | if (block_rsv->reserved >= block_rsv->size) { |
| 3561 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3562 | block_rsv->reserved = block_rsv->size; |
| 3563 | block_rsv->full = 1; |
| 3564 | } else { |
| 3565 | num_bytes = 0; |
| 3566 | } |
| 3567 | spin_unlock(&block_rsv->lock); |
| 3568 | |
| 3569 | if (num_bytes > 0) { |
| 3570 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 3571 | spin_lock(&dest->lock); |
| 3572 | if (!dest->full) { |
| 3573 | u64 bytes_to_add; |
| 3574 | |
| 3575 | bytes_to_add = dest->size - dest->reserved; |
| 3576 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 3577 | dest->reserved += bytes_to_add; |
| 3578 | if (dest->reserved >= dest->size) |
| 3579 | dest->full = 1; |
| 3580 | num_bytes -= bytes_to_add; |
| 3581 | } |
| 3582 | spin_unlock(&dest->lock); |
| 3583 | } |
| 3584 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3585 | spin_lock(&space_info->lock); |
| 3586 | space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3587 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3588 | spin_unlock(&space_info->lock); |
| 3589 | } |
| 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3594 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3595 | { |
| 3596 | int ret; |
| 3597 | |
| 3598 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3599 | if (ret) |
| 3600 | return ret; |
| 3601 | |
| 3602 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3603 | return 0; |
| 3604 | } |
| 3605 | |
| 3606 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3607 | { |
| 3608 | memset(rsv, 0, sizeof(*rsv)); |
| 3609 | spin_lock_init(&rsv->lock); |
| 3610 | atomic_set(&rsv->usage, 1); |
| 3611 | rsv->priority = 6; |
| 3612 | INIT_LIST_HEAD(&rsv->list); |
| 3613 | } |
| 3614 | |
| 3615 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3616 | { |
| 3617 | struct btrfs_block_rsv *block_rsv; |
| 3618 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3619 | |
| 3620 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3621 | if (!block_rsv) |
| 3622 | return NULL; |
| 3623 | |
| 3624 | btrfs_init_block_rsv(block_rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3625 | block_rsv->space_info = __find_space_info(fs_info, |
| 3626 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3627 | return block_rsv; |
| 3628 | } |
| 3629 | |
| 3630 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3631 | struct btrfs_block_rsv *rsv) |
| 3632 | { |
| 3633 | if (rsv && atomic_dec_and_test(&rsv->usage)) { |
| 3634 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3635 | if (!rsv->durable) |
| 3636 | kfree(rsv); |
| 3637 | } |
| 3638 | } |
| 3639 | |
| 3640 | /* |
| 3641 | * make the block_rsv struct be able to capture freed space. |
| 3642 | * the captured space will re-add to the the block_rsv struct |
| 3643 | * after transaction commit |
| 3644 | */ |
| 3645 | void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info, |
| 3646 | struct btrfs_block_rsv *block_rsv) |
| 3647 | { |
| 3648 | block_rsv->durable = 1; |
| 3649 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 3650 | list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list); |
| 3651 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 3652 | } |
| 3653 | |
| 3654 | int btrfs_block_rsv_add(struct btrfs_trans_handle *trans, |
| 3655 | struct btrfs_root *root, |
| 3656 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3657 | u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3658 | { |
| 3659 | int ret; |
| 3660 | |
| 3661 | if (num_bytes == 0) |
| 3662 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3663 | |
| 3664 | ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3665 | if (!ret) { |
| 3666 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3670 | return ret; |
| 3671 | } |
| 3672 | |
| 3673 | int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, |
| 3674 | struct btrfs_root *root, |
| 3675 | struct btrfs_block_rsv *block_rsv, |
| 3676 | u64 min_reserved, int min_factor) |
| 3677 | { |
| 3678 | u64 num_bytes = 0; |
| 3679 | int commit_trans = 0; |
| 3680 | int ret = -ENOSPC; |
| 3681 | |
| 3682 | if (!block_rsv) |
| 3683 | return 0; |
| 3684 | |
| 3685 | spin_lock(&block_rsv->lock); |
| 3686 | if (min_factor > 0) |
| 3687 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3688 | if (min_reserved > num_bytes) |
| 3689 | num_bytes = min_reserved; |
| 3690 | |
| 3691 | if (block_rsv->reserved >= num_bytes) { |
| 3692 | ret = 0; |
| 3693 | } else { |
| 3694 | num_bytes -= block_rsv->reserved; |
| 3695 | if (block_rsv->durable && |
| 3696 | block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes) |
| 3697 | commit_trans = 1; |
| 3698 | } |
| 3699 | spin_unlock(&block_rsv->lock); |
| 3700 | if (!ret) |
| 3701 | return 0; |
| 3702 | |
| 3703 | if (block_rsv->refill_used) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3704 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 3705 | num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3706 | if (!ret) { |
| 3707 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3708 | return 0; |
| 3709 | } |
| 3710 | } |
| 3711 | |
| 3712 | if (commit_trans) { |
| 3713 | if (trans) |
| 3714 | return -EAGAIN; |
| 3715 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3716 | trans = btrfs_join_transaction(root); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3717 | BUG_ON(IS_ERR(trans)); |
| 3718 | ret = btrfs_commit_transaction(trans, root); |
| 3719 | return 0; |
| 3720 | } |
| 3721 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3722 | return -ENOSPC; |
| 3723 | } |
| 3724 | |
| 3725 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3726 | struct btrfs_block_rsv *dst_rsv, |
| 3727 | u64 num_bytes) |
| 3728 | { |
| 3729 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3730 | } |
| 3731 | |
| 3732 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3733 | struct btrfs_block_rsv *block_rsv, |
| 3734 | u64 num_bytes) |
| 3735 | { |
| 3736 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3737 | if (global_rsv->full || global_rsv == block_rsv || |
| 3738 | block_rsv->space_info != global_rsv->space_info) |
| 3739 | global_rsv = NULL; |
| 3740 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3741 | } |
| 3742 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3743 | /* |
| 3744 | * helper to calculate size of global block reservation. |
| 3745 | * the desired value is sum of space used by extent tree, |
| 3746 | * checksum tree and root tree |
| 3747 | */ |
| 3748 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3749 | { |
| 3750 | struct btrfs_space_info *sinfo; |
| 3751 | u64 num_bytes; |
| 3752 | u64 meta_used; |
| 3753 | u64 data_used; |
| 3754 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3755 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3756 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3757 | spin_lock(&sinfo->lock); |
| 3758 | data_used = sinfo->bytes_used; |
| 3759 | spin_unlock(&sinfo->lock); |
| 3760 | |
| 3761 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3762 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3763 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 3764 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3765 | meta_used = sinfo->bytes_used; |
| 3766 | spin_unlock(&sinfo->lock); |
| 3767 | |
| 3768 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3769 | csum_size * 2; |
| 3770 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3771 | |
| 3772 | if (num_bytes * 3 > meta_used) |
| 3773 | num_bytes = div64_u64(meta_used, 3); |
| 3774 | |
| 3775 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3776 | } |
| 3777 | |
| 3778 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3779 | { |
| 3780 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3781 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3782 | u64 num_bytes; |
| 3783 | |
| 3784 | num_bytes = calc_global_metadata_size(fs_info); |
| 3785 | |
| 3786 | spin_lock(&block_rsv->lock); |
| 3787 | spin_lock(&sinfo->lock); |
| 3788 | |
| 3789 | block_rsv->size = num_bytes; |
| 3790 | |
| 3791 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3792 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 3793 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3794 | |
| 3795 | if (sinfo->total_bytes > num_bytes) { |
| 3796 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3797 | block_rsv->reserved += num_bytes; |
| 3798 | sinfo->bytes_reserved += num_bytes; |
| 3799 | } |
| 3800 | |
| 3801 | if (block_rsv->reserved >= block_rsv->size) { |
| 3802 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3803 | sinfo->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3804 | sinfo->reservation_progress++; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3805 | block_rsv->reserved = block_rsv->size; |
| 3806 | block_rsv->full = 1; |
| 3807 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 3808 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3809 | spin_unlock(&sinfo->lock); |
| 3810 | spin_unlock(&block_rsv->lock); |
| 3811 | } |
| 3812 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3813 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3814 | { |
| 3815 | struct btrfs_space_info *space_info; |
| 3816 | |
| 3817 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3818 | fs_info->chunk_block_rsv.space_info = space_info; |
| 3819 | fs_info->chunk_block_rsv.priority = 10; |
| 3820 | |
| 3821 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3822 | fs_info->global_block_rsv.space_info = space_info; |
| 3823 | fs_info->global_block_rsv.priority = 10; |
| 3824 | fs_info->global_block_rsv.refill_used = 1; |
| 3825 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3826 | fs_info->trans_block_rsv.space_info = space_info; |
| 3827 | fs_info->empty_block_rsv.space_info = space_info; |
| 3828 | fs_info->empty_block_rsv.priority = 10; |
| 3829 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3830 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3831 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3832 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3833 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3834 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3835 | |
| 3836 | btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv); |
| 3837 | |
| 3838 | btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv); |
| 3839 | |
| 3840 | update_global_block_rsv(fs_info); |
| 3841 | } |
| 3842 | |
| 3843 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3844 | { |
| 3845 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3846 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3847 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3848 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3849 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3850 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3851 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3852 | } |
| 3853 | |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 3854 | int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3855 | struct btrfs_root *root, |
| 3856 | struct btrfs_block_rsv *rsv) |
| 3857 | { |
| 3858 | struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv; |
| 3859 | u64 num_bytes; |
| 3860 | int ret; |
| 3861 | |
| 3862 | /* |
| 3863 | * Truncate should be freeing data, but give us 2 items just in case it |
| 3864 | * needs to use some space. We may want to be smarter about this in the |
| 3865 | * future. |
| 3866 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 3867 | num_bytes = btrfs_calc_trans_metadata_size(root, 2); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 3868 | |
| 3869 | /* We already have enough bytes, just return */ |
| 3870 | if (rsv->reserved >= num_bytes) |
| 3871 | return 0; |
| 3872 | |
| 3873 | num_bytes -= rsv->reserved; |
| 3874 | |
| 3875 | /* |
| 3876 | * You should have reserved enough space before hand to do this, so this |
| 3877 | * should not fail. |
| 3878 | */ |
| 3879 | ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes); |
| 3880 | BUG_ON(ret); |
| 3881 | |
| 3882 | return 0; |
| 3883 | } |
| 3884 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3885 | int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3886 | struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3887 | int num_items) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3888 | { |
| 3889 | u64 num_bytes; |
| 3890 | int ret; |
| 3891 | |
| 3892 | if (num_items == 0 || root->fs_info->chunk_root == root) |
| 3893 | return 0; |
| 3894 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3895 | num_bytes = btrfs_calc_trans_metadata_size(root, num_items); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3896 | ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3897 | num_bytes); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3898 | if (!ret) { |
| 3899 | trans->bytes_reserved += num_bytes; |
| 3900 | trans->block_rsv = &root->fs_info->trans_block_rsv; |
| 3901 | } |
| 3902 | return ret; |
| 3903 | } |
| 3904 | |
| 3905 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 3906 | struct btrfs_root *root) |
| 3907 | { |
| 3908 | if (!trans->bytes_reserved) |
| 3909 | return; |
| 3910 | |
| 3911 | BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv); |
| 3912 | btrfs_block_rsv_release(root, trans->block_rsv, |
| 3913 | trans->bytes_reserved); |
| 3914 | trans->bytes_reserved = 0; |
| 3915 | } |
| 3916 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3917 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3918 | struct inode *inode) |
| 3919 | { |
| 3920 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3921 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3922 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 3923 | |
| 3924 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 3925 | * We need to hold space in order to delete our orphan item once we've |
| 3926 | * added it, so this takes the reservation so we can release it later |
| 3927 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3928 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 3929 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3930 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3931 | } |
| 3932 | |
| 3933 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 3934 | { |
| 3935 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 3936 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3937 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 3938 | } |
| 3939 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3940 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3941 | struct btrfs_pending_snapshot *pending) |
| 3942 | { |
| 3943 | struct btrfs_root *root = pending->root; |
| 3944 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3945 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 3946 | /* |
| 3947 | * two for root back/forward refs, two for directory entries |
| 3948 | * and one for root of the snapshot. |
| 3949 | */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3950 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3951 | dst_rsv->space_info = src_rsv->space_info; |
| 3952 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3953 | } |
| 3954 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3955 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes) |
| 3956 | { |
| 3957 | return num_bytes >>= 3; |
| 3958 | } |
| 3959 | |
| 3960 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 3961 | { |
| 3962 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3963 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
| 3964 | u64 to_reserve; |
| 3965 | int nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 3966 | int reserved_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3967 | int ret; |
| 3968 | |
| 3969 | if (btrfs_transaction_in_commit(root->fs_info)) |
| 3970 | schedule_timeout(1); |
| 3971 | |
| 3972 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3973 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3974 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 3975 | reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents); |
| 3976 | |
| 3977 | if (nr_extents > reserved_extents) { |
| 3978 | nr_extents -= reserved_extents; |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3979 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3980 | } else { |
| 3981 | nr_extents = 0; |
| 3982 | to_reserve = 0; |
| 3983 | } |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 3984 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3985 | to_reserve += calc_csum_metadata_size(inode, num_bytes); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3986 | ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1); |
| 3987 | if (ret) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3988 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3989 | |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 3990 | atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3991 | atomic_inc(&BTRFS_I(inode)->outstanding_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3992 | |
| 3993 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 3994 | |
| 3995 | if (block_rsv->size > 512 * 1024 * 1024) |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3996 | shrink_delalloc(NULL, root, to_reserve, 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3997 | |
| 3998 | return 0; |
| 3999 | } |
| 4000 | |
| 4001 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 4002 | { |
| 4003 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4004 | u64 to_free; |
| 4005 | int nr_extents; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4006 | int reserved_extents; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4007 | |
| 4008 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
| 4009 | atomic_dec(&BTRFS_I(inode)->outstanding_extents); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 4010 | WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4011 | |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4012 | reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents); |
| 4013 | do { |
| 4014 | int old, new; |
| 4015 | |
| 4016 | nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents); |
| 4017 | if (nr_extents >= reserved_extents) { |
| 4018 | nr_extents = 0; |
| 4019 | break; |
| 4020 | } |
| 4021 | old = reserved_extents; |
| 4022 | nr_extents = reserved_extents - nr_extents; |
| 4023 | new = reserved_extents - nr_extents; |
| 4024 | old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents, |
| 4025 | reserved_extents, new); |
| 4026 | if (likely(old == reserved_extents)) |
| 4027 | break; |
| 4028 | reserved_extents = old; |
| 4029 | } while (1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4030 | |
| 4031 | to_free = calc_csum_metadata_size(inode, num_bytes); |
| 4032 | if (nr_extents > 0) |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4033 | to_free += btrfs_calc_trans_metadata_size(root, nr_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4034 | |
| 4035 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 4036 | to_free); |
| 4037 | } |
| 4038 | |
| 4039 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 4040 | { |
| 4041 | int ret; |
| 4042 | |
| 4043 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 4044 | if (ret) |
| 4045 | return ret; |
| 4046 | |
| 4047 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 4048 | if (ret) { |
| 4049 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4050 | return ret; |
| 4051 | } |
| 4052 | |
| 4053 | return 0; |
| 4054 | } |
| 4055 | |
| 4056 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 4057 | { |
| 4058 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 4059 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4060 | } |
| 4061 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4062 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 4063 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4064 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4065 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4066 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4067 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4068 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4069 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4070 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4071 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 4072 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4073 | /* block accounting for super block */ |
| 4074 | spin_lock(&info->delalloc_lock); |
| 4075 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 4076 | if (alloc) |
| 4077 | old_val += num_bytes; |
| 4078 | else |
| 4079 | old_val -= num_bytes; |
| 4080 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4081 | spin_unlock(&info->delalloc_lock); |
| 4082 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4083 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4084 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4085 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4086 | return -1; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4087 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 4088 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4089 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4090 | factor = 2; |
| 4091 | else |
| 4092 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4093 | /* |
| 4094 | * If this block group has free space cache written out, we |
| 4095 | * need to make sure to load it if we are removing space. This |
| 4096 | * is because we need the unpinning stage to actually add the |
| 4097 | * space back to the block group, otherwise we will leak space. |
| 4098 | */ |
| 4099 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4100 | cache_block_group(cache, trans, NULL, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4101 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4102 | byte_in_group = bytenr - cache->key.objectid; |
| 4103 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4104 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4105 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4106 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4107 | |
| 4108 | if (btrfs_super_cache_generation(&info->super_copy) != 0 && |
| 4109 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 4110 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 4111 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4112 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4113 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4114 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4115 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4116 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4117 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4118 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4119 | cache->space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4120 | cache->space_info->reservation_progress++; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4121 | cache->space_info->bytes_used += num_bytes; |
| 4122 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4123 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4124 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4125 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4126 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4127 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4128 | cache->pinned += num_bytes; |
| 4129 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4130 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4131 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4132 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4133 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4134 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4135 | set_extent_dirty(info->pinned_extents, |
| 4136 | bytenr, bytenr + num_bytes - 1, |
| 4137 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4138 | } |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4139 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4140 | total -= num_bytes; |
| 4141 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4142 | } |
| 4143 | return 0; |
| 4144 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4145 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4146 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 4147 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4148 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4149 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4150 | |
| 4151 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 4152 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4153 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4154 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4155 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4156 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4157 | |
| 4158 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4159 | } |
| 4160 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4161 | static int pin_down_extent(struct btrfs_root *root, |
| 4162 | struct btrfs_block_group_cache *cache, |
| 4163 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4164 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4165 | spin_lock(&cache->space_info->lock); |
| 4166 | spin_lock(&cache->lock); |
| 4167 | cache->pinned += num_bytes; |
| 4168 | cache->space_info->bytes_pinned += num_bytes; |
| 4169 | if (reserved) { |
| 4170 | cache->reserved -= num_bytes; |
| 4171 | cache->space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4172 | cache->space_info->reservation_progress++; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4173 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4174 | spin_unlock(&cache->lock); |
| 4175 | spin_unlock(&cache->space_info->lock); |
| 4176 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4177 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 4178 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4179 | return 0; |
| 4180 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4181 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4182 | /* |
| 4183 | * this function must be called within transaction |
| 4184 | */ |
| 4185 | int btrfs_pin_extent(struct btrfs_root *root, |
| 4186 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4187 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4188 | struct btrfs_block_group_cache *cache; |
| 4189 | |
| 4190 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 4191 | BUG_ON(!cache); |
| 4192 | |
| 4193 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 4194 | |
| 4195 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4196 | return 0; |
| 4197 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4198 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4199 | /* |
| 4200 | * update size of reserved extents. this function may return -EAGAIN |
| 4201 | * if 'reserve' is true or 'sinfo' is false. |
| 4202 | */ |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4203 | int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4204 | u64 num_bytes, int reserve, int sinfo) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4205 | { |
| 4206 | int ret = 0; |
| 4207 | if (sinfo) { |
| 4208 | struct btrfs_space_info *space_info = cache->space_info; |
| 4209 | spin_lock(&space_info->lock); |
| 4210 | spin_lock(&cache->lock); |
| 4211 | if (reserve) { |
| 4212 | if (cache->ro) { |
| 4213 | ret = -EAGAIN; |
| 4214 | } else { |
| 4215 | cache->reserved += num_bytes; |
| 4216 | space_info->bytes_reserved += num_bytes; |
| 4217 | } |
| 4218 | } else { |
| 4219 | if (cache->ro) |
| 4220 | space_info->bytes_readonly += num_bytes; |
| 4221 | cache->reserved -= num_bytes; |
| 4222 | space_info->bytes_reserved -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4223 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4224 | } |
| 4225 | spin_unlock(&cache->lock); |
| 4226 | spin_unlock(&space_info->lock); |
| 4227 | } else { |
| 4228 | spin_lock(&cache->lock); |
| 4229 | if (cache->ro) { |
| 4230 | ret = -EAGAIN; |
| 4231 | } else { |
| 4232 | if (reserve) |
| 4233 | cache->reserved += num_bytes; |
| 4234 | else |
| 4235 | cache->reserved -= num_bytes; |
| 4236 | } |
| 4237 | spin_unlock(&cache->lock); |
| 4238 | } |
| 4239 | return ret; |
| 4240 | } |
| 4241 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4242 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 4243 | struct btrfs_root *root) |
| 4244 | { |
| 4245 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4246 | struct btrfs_caching_control *next; |
| 4247 | struct btrfs_caching_control *caching_ctl; |
| 4248 | struct btrfs_block_group_cache *cache; |
| 4249 | |
| 4250 | down_write(&fs_info->extent_commit_sem); |
| 4251 | |
| 4252 | list_for_each_entry_safe(caching_ctl, next, |
| 4253 | &fs_info->caching_block_groups, list) { |
| 4254 | cache = caching_ctl->block_group; |
| 4255 | if (block_group_cache_done(cache)) { |
| 4256 | cache->last_byte_to_unpin = (u64)-1; |
| 4257 | list_del_init(&caching_ctl->list); |
| 4258 | put_caching_control(caching_ctl); |
| 4259 | } else { |
| 4260 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4261 | } |
| 4262 | } |
| 4263 | |
| 4264 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4265 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4266 | else |
| 4267 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4268 | |
| 4269 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4270 | |
| 4271 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4272 | return 0; |
| 4273 | } |
| 4274 | |
| 4275 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 4276 | { |
| 4277 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4278 | struct btrfs_block_group_cache *cache = NULL; |
| 4279 | u64 len; |
| 4280 | |
| 4281 | while (start <= end) { |
| 4282 | if (!cache || |
| 4283 | start >= cache->key.objectid + cache->key.offset) { |
| 4284 | if (cache) |
| 4285 | btrfs_put_block_group(cache); |
| 4286 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4287 | BUG_ON(!cache); |
| 4288 | } |
| 4289 | |
| 4290 | len = cache->key.objectid + cache->key.offset - start; |
| 4291 | len = min(len, end + 1 - start); |
| 4292 | |
| 4293 | if (start < cache->last_byte_to_unpin) { |
| 4294 | len = min(len, cache->last_byte_to_unpin - start); |
| 4295 | btrfs_add_free_space(cache, start, len); |
| 4296 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4297 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4298 | start += len; |
| 4299 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4300 | spin_lock(&cache->space_info->lock); |
| 4301 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4302 | cache->pinned -= len; |
| 4303 | cache->space_info->bytes_pinned -= len; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4304 | if (cache->ro) { |
| 4305 | cache->space_info->bytes_readonly += len; |
| 4306 | } else if (cache->reserved_pinned > 0) { |
| 4307 | len = min(len, cache->reserved_pinned); |
| 4308 | cache->reserved_pinned -= len; |
| 4309 | cache->space_info->bytes_reserved += len; |
| 4310 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4311 | spin_unlock(&cache->lock); |
| 4312 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4313 | } |
| 4314 | |
| 4315 | if (cache) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4316 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4317 | return 0; |
| 4318 | } |
| 4319 | |
| 4320 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4321 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4322 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4323 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4324 | struct extent_io_tree *unpin; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4325 | struct btrfs_block_rsv *block_rsv; |
| 4326 | struct btrfs_block_rsv *next_rsv; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4327 | u64 start; |
| 4328 | u64 end; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4329 | int idx; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4330 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4331 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4332 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4333 | unpin = &fs_info->freed_extents[1]; |
| 4334 | else |
| 4335 | unpin = &fs_info->freed_extents[0]; |
| 4336 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4337 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4338 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4339 | EXTENT_DIRTY); |
| 4340 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4341 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4342 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 4343 | if (btrfs_test_opt(root, DISCARD)) |
| 4344 | ret = btrfs_discard_extent(root, start, |
| 4345 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4346 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4347 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4348 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4349 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4350 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4351 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4352 | mutex_lock(&fs_info->durable_block_rsv_mutex); |
| 4353 | list_for_each_entry_safe(block_rsv, next_rsv, |
| 4354 | &fs_info->durable_block_rsv_list, list) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4355 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4356 | idx = trans->transid & 0x1; |
| 4357 | if (block_rsv->freed[idx] > 0) { |
| 4358 | block_rsv_add_bytes(block_rsv, |
| 4359 | block_rsv->freed[idx], 0); |
| 4360 | block_rsv->freed[idx] = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 4361 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4362 | if (atomic_read(&block_rsv->usage) == 0) { |
| 4363 | btrfs_block_rsv_release(root, block_rsv, (u64)-1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4364 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4365 | if (block_rsv->freed[0] == 0 && |
| 4366 | block_rsv->freed[1] == 0) { |
| 4367 | list_del_init(&block_rsv->list); |
| 4368 | kfree(block_rsv); |
| 4369 | } |
| 4370 | } else { |
| 4371 | btrfs_block_rsv_release(root, block_rsv, 0); |
| 4372 | } |
| 4373 | } |
| 4374 | mutex_unlock(&fs_info->durable_block_rsv_mutex); |
| 4375 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4376 | return 0; |
| 4377 | } |
| 4378 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4379 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4380 | struct btrfs_root *root, |
| 4381 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4382 | u64 root_objectid, u64 owner_objectid, |
| 4383 | u64 owner_offset, int refs_to_drop, |
| 4384 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4385 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4386 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4387 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4388 | struct btrfs_fs_info *info = root->fs_info; |
| 4389 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4390 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4391 | struct btrfs_extent_item *ei; |
| 4392 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4393 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4394 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4395 | int extent_slot = 0; |
| 4396 | int found_extent = 0; |
| 4397 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4398 | u32 item_size; |
| 4399 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4400 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4401 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4402 | if (!path) |
| 4403 | return -ENOMEM; |
| 4404 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4405 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4406 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4407 | |
| 4408 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4409 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4410 | |
| 4411 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4412 | bytenr, num_bytes, parent, |
| 4413 | root_objectid, owner_objectid, |
| 4414 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4415 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4416 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4417 | while (extent_slot >= 0) { |
| 4418 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4419 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4420 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4421 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4422 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4423 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4424 | found_extent = 1; |
| 4425 | break; |
| 4426 | } |
| 4427 | if (path->slots[0] - extent_slot > 5) |
| 4428 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4429 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4430 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4431 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4432 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4433 | if (found_extent && item_size < sizeof(*ei)) |
| 4434 | found_extent = 0; |
| 4435 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4436 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4437 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4438 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4439 | NULL, refs_to_drop, |
| 4440 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4441 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4442 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4443 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4444 | |
| 4445 | key.objectid = bytenr; |
| 4446 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4447 | key.offset = num_bytes; |
| 4448 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4449 | ret = btrfs_search_slot(trans, extent_root, |
| 4450 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4451 | if (ret) { |
| 4452 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4453 | ", was looking for %llu\n", ret, |
| 4454 | (unsigned long long)bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4455 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4456 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4457 | BUG_ON(ret); |
| 4458 | extent_slot = path->slots[0]; |
| 4459 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4460 | } else { |
| 4461 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4462 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4463 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4464 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4465 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4466 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4467 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4468 | (unsigned long long)owner_objectid, |
| 4469 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4470 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4471 | |
| 4472 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4473 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4474 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4475 | if (item_size < sizeof(*ei)) { |
| 4476 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4477 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4478 | owner_objectid, 0); |
| 4479 | BUG_ON(ret < 0); |
| 4480 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4481 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4482 | path->leave_spinning = 1; |
| 4483 | |
| 4484 | key.objectid = bytenr; |
| 4485 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4486 | key.offset = num_bytes; |
| 4487 | |
| 4488 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4489 | -1, 1); |
| 4490 | if (ret) { |
| 4491 | printk(KERN_ERR "umm, got %d back from search" |
| 4492 | ", was looking for %llu\n", ret, |
| 4493 | (unsigned long long)bytenr); |
| 4494 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4495 | } |
| 4496 | BUG_ON(ret); |
| 4497 | extent_slot = path->slots[0]; |
| 4498 | leaf = path->nodes[0]; |
| 4499 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4500 | } |
| 4501 | #endif |
| 4502 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4503 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4504 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4505 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4506 | struct btrfs_tree_block_info *bi; |
| 4507 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4508 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4509 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4510 | } |
| 4511 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4512 | refs = btrfs_extent_refs(leaf, ei); |
| 4513 | BUG_ON(refs < refs_to_drop); |
| 4514 | refs -= refs_to_drop; |
| 4515 | |
| 4516 | if (refs > 0) { |
| 4517 | if (extent_op) |
| 4518 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4519 | /* |
| 4520 | * In the case of inline back ref, reference count will |
| 4521 | * be updated by remove_extent_backref |
| 4522 | */ |
| 4523 | if (iref) { |
| 4524 | BUG_ON(!found_extent); |
| 4525 | } else { |
| 4526 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4527 | btrfs_mark_buffer_dirty(leaf); |
| 4528 | } |
| 4529 | if (found_extent) { |
| 4530 | ret = remove_extent_backref(trans, extent_root, path, |
| 4531 | iref, refs_to_drop, |
| 4532 | is_data); |
| 4533 | BUG_ON(ret); |
| 4534 | } |
| 4535 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4536 | if (found_extent) { |
| 4537 | BUG_ON(is_data && refs_to_drop != |
| 4538 | extent_data_ref_count(root, path, iref)); |
| 4539 | if (iref) { |
| 4540 | BUG_ON(path->slots[0] != extent_slot); |
| 4541 | } else { |
| 4542 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4543 | path->slots[0] = extent_slot; |
| 4544 | num_to_del = 2; |
| 4545 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4546 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4547 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4548 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4549 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4550 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4551 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4552 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4553 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4554 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4555 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4556 | } else { |
| 4557 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4558 | bytenr >> PAGE_CACHE_SHIFT, |
| 4559 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4560 | } |
| 4561 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4562 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4563 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4564 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4565 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4566 | return ret; |
| 4567 | } |
| 4568 | |
| 4569 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4570 | * 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] | 4571 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4572 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4573 | * removes it from the tree. |
| 4574 | */ |
| 4575 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4576 | struct btrfs_root *root, u64 bytenr) |
| 4577 | { |
| 4578 | struct btrfs_delayed_ref_head *head; |
| 4579 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4580 | struct btrfs_delayed_ref_node *ref; |
| 4581 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4582 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4583 | |
| 4584 | delayed_refs = &trans->transaction->delayed_refs; |
| 4585 | spin_lock(&delayed_refs->lock); |
| 4586 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4587 | if (!head) |
| 4588 | goto out; |
| 4589 | |
| 4590 | node = rb_prev(&head->node.rb_node); |
| 4591 | if (!node) |
| 4592 | goto out; |
| 4593 | |
| 4594 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4595 | |
| 4596 | /* there are still entries for this ref, we can't drop it */ |
| 4597 | if (ref->bytenr == bytenr) |
| 4598 | goto out; |
| 4599 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4600 | if (head->extent_op) { |
| 4601 | if (!head->must_insert_reserved) |
| 4602 | goto out; |
| 4603 | kfree(head->extent_op); |
| 4604 | head->extent_op = NULL; |
| 4605 | } |
| 4606 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4607 | /* |
| 4608 | * waiting for the lock here would deadlock. If someone else has it |
| 4609 | * locked they are already in the process of dropping it anyway |
| 4610 | */ |
| 4611 | if (!mutex_trylock(&head->mutex)) |
| 4612 | goto out; |
| 4613 | |
| 4614 | /* |
| 4615 | * at this point we have a head with no other entries. Go |
| 4616 | * ahead and process it. |
| 4617 | */ |
| 4618 | head->node.in_tree = 0; |
| 4619 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4620 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4621 | delayed_refs->num_entries--; |
| 4622 | |
| 4623 | /* |
| 4624 | * we don't take a ref on the node because we're removing it from the |
| 4625 | * tree, so we just steal the ref the tree was holding. |
| 4626 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4627 | delayed_refs->num_heads--; |
| 4628 | if (list_empty(&head->cluster)) |
| 4629 | delayed_refs->num_heads_ready--; |
| 4630 | |
| 4631 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4632 | spin_unlock(&delayed_refs->lock); |
| 4633 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4634 | BUG_ON(head->extent_op); |
| 4635 | if (head->must_insert_reserved) |
| 4636 | ret = 1; |
| 4637 | |
| 4638 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4639 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4640 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4641 | out: |
| 4642 | spin_unlock(&delayed_refs->lock); |
| 4643 | return 0; |
| 4644 | } |
| 4645 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4646 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4647 | struct btrfs_root *root, |
| 4648 | struct extent_buffer *buf, |
| 4649 | u64 parent, int last_ref) |
| 4650 | { |
| 4651 | struct btrfs_block_rsv *block_rsv; |
| 4652 | struct btrfs_block_group_cache *cache = NULL; |
| 4653 | int ret; |
| 4654 | |
| 4655 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4656 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4657 | parent, root->root_key.objectid, |
| 4658 | btrfs_header_level(buf), |
| 4659 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4660 | BUG_ON(ret); |
| 4661 | } |
| 4662 | |
| 4663 | if (!last_ref) |
| 4664 | return; |
| 4665 | |
| 4666 | block_rsv = get_block_rsv(trans, root); |
| 4667 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | 3bf84a5 | 2010-05-31 09:04:46 +0000 | [diff] [blame] | 4668 | if (block_rsv->space_info != cache->space_info) |
| 4669 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4670 | |
| 4671 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4672 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4673 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4674 | if (!ret) |
| 4675 | goto pin; |
| 4676 | } |
| 4677 | |
| 4678 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4679 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
| 4680 | goto pin; |
| 4681 | } |
| 4682 | |
| 4683 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4684 | |
| 4685 | btrfs_add_free_space(cache, buf->start, buf->len); |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4686 | ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4687 | if (ret == -EAGAIN) { |
| 4688 | /* block group became read-only */ |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 4689 | btrfs_update_reserved_bytes(cache, buf->len, 0, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4690 | goto out; |
| 4691 | } |
| 4692 | |
| 4693 | ret = 1; |
| 4694 | spin_lock(&block_rsv->lock); |
| 4695 | if (block_rsv->reserved < block_rsv->size) { |
| 4696 | block_rsv->reserved += buf->len; |
| 4697 | ret = 0; |
| 4698 | } |
| 4699 | spin_unlock(&block_rsv->lock); |
| 4700 | |
| 4701 | if (ret) { |
| 4702 | spin_lock(&cache->space_info->lock); |
| 4703 | cache->space_info->bytes_reserved -= buf->len; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4704 | cache->space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4705 | spin_unlock(&cache->space_info->lock); |
| 4706 | } |
| 4707 | goto out; |
| 4708 | } |
| 4709 | pin: |
| 4710 | if (block_rsv->durable && !cache->ro) { |
| 4711 | ret = 0; |
| 4712 | spin_lock(&cache->lock); |
| 4713 | if (!cache->ro) { |
| 4714 | cache->reserved_pinned += buf->len; |
| 4715 | ret = 1; |
| 4716 | } |
| 4717 | spin_unlock(&cache->lock); |
| 4718 | |
| 4719 | if (ret) { |
| 4720 | spin_lock(&block_rsv->lock); |
| 4721 | block_rsv->freed[trans->transid & 0x1] += buf->len; |
| 4722 | spin_unlock(&block_rsv->lock); |
| 4723 | } |
| 4724 | } |
| 4725 | out: |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 4726 | /* |
| 4727 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 4728 | * anymore. |
| 4729 | */ |
| 4730 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4731 | btrfs_put_block_group(cache); |
| 4732 | } |
| 4733 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4734 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4735 | struct btrfs_root *root, |
| 4736 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4737 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4738 | { |
| 4739 | int ret; |
| 4740 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4741 | /* |
| 4742 | * tree log blocks never actually go into the extent allocation |
| 4743 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4744 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4745 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4746 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4747 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4748 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4749 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4750 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4751 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4752 | parent, root_objectid, (int)owner, |
| 4753 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4754 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4755 | } else { |
| 4756 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4757 | parent, root_objectid, owner, |
| 4758 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4759 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4760 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4761 | return ret; |
| 4762 | } |
| 4763 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4764 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4765 | { |
| 4766 | u64 mask = ((u64)root->stripesize - 1); |
| 4767 | u64 ret = (val + mask) & ~mask; |
| 4768 | return ret; |
| 4769 | } |
| 4770 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4771 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4772 | * when we wait for progress in the block group caching, its because |
| 4773 | * our allocation attempt failed at least once. So, we must sleep |
| 4774 | * and let some progress happen before we try again. |
| 4775 | * |
| 4776 | * This function will sleep at least once waiting for new free space to |
| 4777 | * show up, and then it will check the block group free space numbers |
| 4778 | * for our min num_bytes. Another option is to have it go ahead |
| 4779 | * and look in the rbtree for a free extent of a given size, but this |
| 4780 | * is a good start. |
| 4781 | */ |
| 4782 | static noinline int |
| 4783 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4784 | u64 num_bytes) |
| 4785 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4786 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4787 | DEFINE_WAIT(wait); |
| 4788 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4789 | caching_ctl = get_caching_control(cache); |
| 4790 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4791 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4792 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4793 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 4794 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4795 | |
| 4796 | put_caching_control(caching_ctl); |
| 4797 | return 0; |
| 4798 | } |
| 4799 | |
| 4800 | static noinline int |
| 4801 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4802 | { |
| 4803 | struct btrfs_caching_control *caching_ctl; |
| 4804 | DEFINE_WAIT(wait); |
| 4805 | |
| 4806 | caching_ctl = get_caching_control(cache); |
| 4807 | if (!caching_ctl) |
| 4808 | return 0; |
| 4809 | |
| 4810 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4811 | |
| 4812 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4813 | return 0; |
| 4814 | } |
| 4815 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4816 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4817 | { |
| 4818 | int index; |
| 4819 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4820 | index = 0; |
| 4821 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4822 | index = 1; |
| 4823 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4824 | index = 2; |
| 4825 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4826 | index = 3; |
| 4827 | else |
| 4828 | index = 4; |
| 4829 | return index; |
| 4830 | } |
| 4831 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4832 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4833 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4834 | LOOP_CACHING_NOWAIT = 1, |
| 4835 | LOOP_CACHING_WAIT = 2, |
| 4836 | LOOP_ALLOC_CHUNK = 3, |
| 4837 | LOOP_NO_EMPTY_SIZE = 4, |
| 4838 | }; |
| 4839 | |
| 4840 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4841 | * walks the btree of allocated extents and find a hole of a given size. |
| 4842 | * The key ins is changed to record the hole: |
| 4843 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4844 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4845 | * ins->offset == number of blocks |
| 4846 | * Any available blocks before search_start are skipped. |
| 4847 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4848 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4849 | struct btrfs_root *orig_root, |
| 4850 | u64 num_bytes, u64 empty_size, |
| 4851 | u64 search_start, u64 search_end, |
| 4852 | u64 hint_byte, struct btrfs_key *ins, |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 4853 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4854 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4855 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4856 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4857 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4858 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4859 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4860 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4861 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4862 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4863 | int last_ptr_loop = 0; |
| 4864 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4865 | int index = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4866 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4867 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4868 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4869 | bool use_cluster = true; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4870 | u64 ideal_cache_percent = 0; |
| 4871 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4872 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4873 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4874 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4875 | ins->objectid = 0; |
| 4876 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4877 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4878 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4879 | if (!space_info) { |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 4880 | printk(KERN_ERR "No space info for %llu\n", data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4881 | return -ENOSPC; |
| 4882 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4883 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4884 | /* |
| 4885 | * If the space info is for both data and metadata it means we have a |
| 4886 | * small filesystem and we can't use the clustering stuff. |
| 4887 | */ |
| 4888 | if (btrfs_mixed_space_info(space_info)) |
| 4889 | use_cluster = false; |
| 4890 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4891 | if (orig_root->ref_cows || empty_size) |
| 4892 | allowed_chunk_alloc = 1; |
| 4893 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4894 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4895 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4896 | if (!btrfs_test_opt(root, SSD)) |
| 4897 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4898 | } |
| 4899 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4900 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
| 4901 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4902 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4903 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4904 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4905 | if (last_ptr) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4906 | spin_lock(&last_ptr->lock); |
| 4907 | if (last_ptr->block_group) |
| 4908 | hint_byte = last_ptr->window_start; |
| 4909 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4910 | } |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4911 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4912 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4913 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4914 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4915 | if (!last_ptr) |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4916 | empty_cluster = 0; |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4917 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4918 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4919 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4920 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4921 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4922 | /* |
| 4923 | * we don't want to use the block group if it doesn't match our |
| 4924 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4925 | * |
| 4926 | * However if we are re-searching with an ideal block group |
| 4927 | * 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] | 4928 | */ |
| 4929 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4930 | (block_group->cached != BTRFS_CACHE_NO || |
| 4931 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4932 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4933 | if (list_empty(&block_group->list) || |
| 4934 | block_group->ro) { |
| 4935 | /* |
| 4936 | * someone is removing this block group, |
| 4937 | * we can't jump into the have_block_group |
| 4938 | * target because our list pointers are not |
| 4939 | * valid |
| 4940 | */ |
| 4941 | btrfs_put_block_group(block_group); |
| 4942 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4943 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4944 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4945 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4946 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4947 | } else if (block_group) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4948 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4949 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4950 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4951 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4952 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4953 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 4954 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4955 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4956 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4957 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4958 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4959 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4960 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4961 | /* |
| 4962 | * this can happen if we end up cycling through all the |
| 4963 | * raid types, but we want to make sure we only allocate |
| 4964 | * for the proper type. |
| 4965 | */ |
| 4966 | if (!block_group_bits(block_group, data)) { |
| 4967 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 4968 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4969 | BTRFS_BLOCK_GROUP_RAID10; |
| 4970 | |
| 4971 | /* |
| 4972 | * if they asked for extra copies and this block group |
| 4973 | * doesn't provide them, bail. This does allow us to |
| 4974 | * fill raid0 from raid1. |
| 4975 | */ |
| 4976 | if ((data & extra) && !(block_group->flags & extra)) |
| 4977 | goto loop; |
| 4978 | } |
| 4979 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4980 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4981 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4982 | u64 free_percent; |
| 4983 | |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4984 | ret = cache_block_group(block_group, trans, |
| 4985 | orig_root, 1); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4986 | if (block_group->cached == BTRFS_CACHE_FINISHED) |
| 4987 | goto have_block_group; |
| 4988 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4989 | free_percent = btrfs_block_group_used(&block_group->item); |
| 4990 | free_percent *= 100; |
| 4991 | free_percent = div64_u64(free_percent, |
| 4992 | block_group->key.offset); |
| 4993 | free_percent = 100 - free_percent; |
| 4994 | if (free_percent > ideal_cache_percent && |
| 4995 | likely(!block_group->ro)) { |
| 4996 | ideal_cache_offset = block_group->key.objectid; |
| 4997 | ideal_cache_percent = free_percent; |
| 4998 | } |
| 4999 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5000 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5001 | * We only want to start kthread caching if we are at |
| 5002 | * the point where we will wait for caching to make |
| 5003 | * progress, or if our ideal search is over and we've |
| 5004 | * found somebody to start caching. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5005 | */ |
| 5006 | if (loop > LOOP_CACHING_NOWAIT || |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5007 | (loop > LOOP_FIND_IDEAL && |
| 5008 | atomic_read(&space_info->caching_threads) < 2)) { |
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++; |
| 5230 | if (!ideal_cache_percent && |
| 5231 | atomic_read(&space_info->caching_threads)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5232 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5233 | |
| 5234 | /* |
| 5235 | * 1 of the following 2 things have happened so far |
| 5236 | * |
| 5237 | * 1) We found an ideal block group for caching that |
| 5238 | * is mostly full and will cache quickly, so we might |
| 5239 | * as well wait for it. |
| 5240 | * |
| 5241 | * 2) We searched for cached only and we didn't find |
| 5242 | * anything, and we didn't start any caching kthreads |
| 5243 | * either, so chances are we will loop through and |
| 5244 | * start a couple caching kthreads, and then come back |
| 5245 | * around and just wait for them. This will be slower |
| 5246 | * because we will have 2 caching kthreads reading at |
| 5247 | * the same time when we could have just started one |
| 5248 | * and waited for it to get far enough to give us an |
| 5249 | * allocation, so go ahead and go to the wait caching |
| 5250 | * loop. |
| 5251 | */ |
| 5252 | loop = LOOP_CACHING_WAIT; |
| 5253 | search_start = ideal_cache_offset; |
| 5254 | ideal_cache_percent = 0; |
| 5255 | goto ideal_cache; |
| 5256 | } else if (loop == LOOP_FIND_IDEAL) { |
| 5257 | /* |
| 5258 | * Didn't find a uncached bg, wait on anything we find |
| 5259 | * next. |
| 5260 | */ |
| 5261 | loop = LOOP_CACHING_WAIT; |
| 5262 | goto search; |
| 5263 | } |
| 5264 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5265 | loop++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5266 | |
| 5267 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5268 | if (allowed_chunk_alloc) { |
| 5269 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 5270 | 2 * 1024 * 1024, data, |
| 5271 | CHUNK_ALLOC_LIMITED); |
| 5272 | allowed_chunk_alloc = 0; |
| 5273 | if (ret == 1) |
| 5274 | done_chunk_alloc = 1; |
| 5275 | } else if (!done_chunk_alloc && |
| 5276 | space_info->force_alloc == |
| 5277 | CHUNK_ALLOC_NO_FORCE) { |
| 5278 | space_info->force_alloc = CHUNK_ALLOC_LIMITED; |
| 5279 | } |
| 5280 | |
| 5281 | /* |
| 5282 | * We didn't allocate a chunk, go ahead and drop the |
| 5283 | * empty size and loop again. |
| 5284 | */ |
| 5285 | if (!done_chunk_alloc) |
| 5286 | loop = LOOP_NO_EMPTY_SIZE; |
| 5287 | } |
| 5288 | |
| 5289 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5290 | empty_size = 0; |
| 5291 | empty_cluster = 0; |
| 5292 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5293 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5294 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5295 | } else if (!ins->objectid) { |
| 5296 | ret = -ENOSPC; |
Josef Bacik | d82a6f1 | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5297 | } else if (ins->objectid) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5298 | ret = 0; |
| 5299 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5300 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5301 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5302 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5303 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5304 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 5305 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5306 | { |
| 5307 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5308 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5309 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5310 | spin_lock(&info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5311 | printk(KERN_INFO "space_info has %llu free, is %sfull\n", |
| 5312 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5313 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5314 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5315 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5316 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 5317 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 5318 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5319 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5320 | (unsigned long long)info->bytes_pinned, |
| 5321 | (unsigned long long)info->bytes_reserved, |
| 5322 | (unsigned long long)info->bytes_may_use, |
| 5323 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5324 | spin_unlock(&info->lock); |
| 5325 | |
| 5326 | if (!dump_block_groups) |
| 5327 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5328 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5329 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5330 | again: |
| 5331 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5332 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5333 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 5334 | "%llu pinned %llu reserved\n", |
| 5335 | (unsigned long long)cache->key.objectid, |
| 5336 | (unsigned long long)cache->key.offset, |
| 5337 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 5338 | (unsigned long long)cache->pinned, |
| 5339 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5340 | btrfs_dump_free_space(cache, bytes); |
| 5341 | spin_unlock(&cache->lock); |
| 5342 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5343 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5344 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5345 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5346 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5347 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5348 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5349 | struct btrfs_root *root, |
| 5350 | u64 num_bytes, u64 min_alloc_size, |
| 5351 | u64 empty_size, u64 hint_byte, |
| 5352 | u64 search_end, struct btrfs_key *ins, |
| 5353 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5354 | { |
| 5355 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5356 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5357 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5358 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5359 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5360 | /* |
| 5361 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5362 | * is not called recursively on allocations |
| 5363 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5364 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5365 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5366 | num_bytes + 2 * 1024 * 1024, data, |
| 5367 | CHUNK_ALLOC_NO_FORCE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5368 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5369 | WARN_ON(num_bytes < root->sectorsize); |
| 5370 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5371 | search_start, search_end, hint_byte, |
| 5372 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5373 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5374 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5375 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5376 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5377 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5378 | do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5379 | num_bytes, data, CHUNK_ALLOC_FORCE); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5380 | goto again; |
| 5381 | } |
Chris Mason | 9143565 | 2011-02-16 13:10:41 -0500 | [diff] [blame] | 5382 | if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5383 | struct btrfs_space_info *sinfo; |
| 5384 | |
| 5385 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5386 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5387 | "wanted %llu\n", (unsigned long long)data, |
| 5388 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5389 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5390 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5391 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5392 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
| 5393 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5394 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5395 | } |
| 5396 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5397 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 5398 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5399 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5400 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5401 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5402 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5403 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5404 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5405 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5406 | return -ENOSPC; |
| 5407 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5408 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5409 | if (btrfs_test_opt(root, DISCARD)) |
| 5410 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5411 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5412 | btrfs_add_free_space(cache, start, len); |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5413 | btrfs_update_reserved_bytes(cache, len, 0, 1); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5414 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5415 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5416 | trace_btrfs_reserved_extent_free(root, start, len); |
| 5417 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5418 | return ret; |
| 5419 | } |
| 5420 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5421 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5422 | struct btrfs_root *root, |
| 5423 | u64 parent, u64 root_objectid, |
| 5424 | u64 flags, u64 owner, u64 offset, |
| 5425 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5426 | { |
| 5427 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5428 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5429 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5430 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5431 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5432 | struct extent_buffer *leaf; |
| 5433 | int type; |
| 5434 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5435 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5436 | if (parent > 0) |
| 5437 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5438 | else |
| 5439 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5440 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5441 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5442 | |
| 5443 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5444 | if (!path) |
| 5445 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5446 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5447 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5448 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5449 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5450 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5451 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5452 | leaf = path->nodes[0]; |
| 5453 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5454 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5455 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5456 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5457 | btrfs_set_extent_flags(leaf, extent_item, |
| 5458 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5459 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5460 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5461 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5462 | if (parent > 0) { |
| 5463 | struct btrfs_shared_data_ref *ref; |
| 5464 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5465 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5466 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5467 | } else { |
| 5468 | struct btrfs_extent_data_ref *ref; |
| 5469 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5470 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5471 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5472 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5473 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5474 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5475 | |
| 5476 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5477 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5478 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5479 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5480 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5481 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5482 | "%llu\n", (unsigned long long)ins->objectid, |
| 5483 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5484 | BUG(); |
| 5485 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5486 | return ret; |
| 5487 | } |
| 5488 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5489 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5490 | struct btrfs_root *root, |
| 5491 | u64 parent, u64 root_objectid, |
| 5492 | u64 flags, struct btrfs_disk_key *key, |
| 5493 | int level, struct btrfs_key *ins) |
| 5494 | { |
| 5495 | int ret; |
| 5496 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5497 | struct btrfs_extent_item *extent_item; |
| 5498 | struct btrfs_tree_block_info *block_info; |
| 5499 | struct btrfs_extent_inline_ref *iref; |
| 5500 | struct btrfs_path *path; |
| 5501 | struct extent_buffer *leaf; |
| 5502 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5503 | |
| 5504 | path = btrfs_alloc_path(); |
| 5505 | BUG_ON(!path); |
| 5506 | |
| 5507 | path->leave_spinning = 1; |
| 5508 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5509 | ins, size); |
| 5510 | BUG_ON(ret); |
| 5511 | |
| 5512 | leaf = path->nodes[0]; |
| 5513 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5514 | struct btrfs_extent_item); |
| 5515 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5516 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5517 | btrfs_set_extent_flags(leaf, extent_item, |
| 5518 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5519 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5520 | |
| 5521 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5522 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5523 | |
| 5524 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5525 | if (parent > 0) { |
| 5526 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5527 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5528 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5529 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5530 | } else { |
| 5531 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5532 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5533 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5534 | } |
| 5535 | |
| 5536 | btrfs_mark_buffer_dirty(leaf); |
| 5537 | btrfs_free_path(path); |
| 5538 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5539 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5540 | if (ret) { |
| 5541 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5542 | "%llu\n", (unsigned long long)ins->objectid, |
| 5543 | (unsigned long long)ins->offset); |
| 5544 | BUG(); |
| 5545 | } |
| 5546 | return ret; |
| 5547 | } |
| 5548 | |
| 5549 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5550 | struct btrfs_root *root, |
| 5551 | u64 root_objectid, u64 owner, |
| 5552 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5553 | { |
| 5554 | int ret; |
Chris Mason | 1c2308f | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5555 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5556 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5557 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5558 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5559 | 0, root_objectid, owner, offset, |
| 5560 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5561 | return ret; |
| 5562 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5563 | |
| 5564 | /* |
| 5565 | * this is used by the tree logging recovery code. It records that |
| 5566 | * an extent has been allocated and makes sure to clear the free |
| 5567 | * space cache bits as well |
| 5568 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5569 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5570 | struct btrfs_root *root, |
| 5571 | u64 root_objectid, u64 owner, u64 offset, |
| 5572 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5573 | { |
| 5574 | int ret; |
| 5575 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5576 | struct btrfs_caching_control *caching_ctl; |
| 5577 | u64 start = ins->objectid; |
| 5578 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5579 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5580 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5581 | cache_block_group(block_group, trans, NULL, 0); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5582 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5583 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5584 | if (!caching_ctl) { |
| 5585 | BUG_ON(!block_group_cache_done(block_group)); |
| 5586 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5587 | BUG_ON(ret); |
| 5588 | } else { |
| 5589 | mutex_lock(&caching_ctl->mutex); |
| 5590 | |
| 5591 | if (start >= caching_ctl->progress) { |
| 5592 | ret = add_excluded_extent(root, start, num_bytes); |
| 5593 | BUG_ON(ret); |
| 5594 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5595 | ret = btrfs_remove_free_space(block_group, |
| 5596 | start, num_bytes); |
| 5597 | BUG_ON(ret); |
| 5598 | } else { |
| 5599 | num_bytes = caching_ctl->progress - start; |
| 5600 | ret = btrfs_remove_free_space(block_group, |
| 5601 | start, num_bytes); |
| 5602 | BUG_ON(ret); |
| 5603 | |
| 5604 | start = caching_ctl->progress; |
| 5605 | num_bytes = ins->objectid + ins->offset - |
| 5606 | caching_ctl->progress; |
| 5607 | ret = add_excluded_extent(root, start, num_bytes); |
| 5608 | BUG_ON(ret); |
| 5609 | } |
| 5610 | |
| 5611 | mutex_unlock(&caching_ctl->mutex); |
| 5612 | put_caching_control(caching_ctl); |
| 5613 | } |
| 5614 | |
Li Dongyang | b4d00d5 | 2011-03-24 10:24:25 +0000 | [diff] [blame] | 5615 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5616 | BUG_ON(ret); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5617 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5618 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5619 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5620 | return ret; |
| 5621 | } |
| 5622 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5623 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5624 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5625 | u64 bytenr, u32 blocksize, |
| 5626 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5627 | { |
| 5628 | struct extent_buffer *buf; |
| 5629 | |
| 5630 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5631 | if (!buf) |
| 5632 | return ERR_PTR(-ENOMEM); |
| 5633 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5634 | btrfs_set_buffer_lockdep_class(buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5635 | btrfs_tree_lock(buf); |
| 5636 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5637 | |
| 5638 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5639 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5640 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5641 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5642 | /* |
| 5643 | * we allow two log transactions at a time, use different |
| 5644 | * EXENT bit to differentiate dirty pages. |
| 5645 | */ |
| 5646 | if (root->log_transid % 2 == 0) |
| 5647 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5648 | buf->start + buf->len - 1, GFP_NOFS); |
| 5649 | else |
| 5650 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5651 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5652 | } else { |
| 5653 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5654 | buf->start + buf->len - 1, GFP_NOFS); |
| 5655 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5656 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5657 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5658 | return buf; |
| 5659 | } |
| 5660 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5661 | static struct btrfs_block_rsv * |
| 5662 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5663 | struct btrfs_root *root, u32 blocksize) |
| 5664 | { |
| 5665 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5666 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5667 | int ret; |
| 5668 | |
| 5669 | block_rsv = get_block_rsv(trans, root); |
| 5670 | |
| 5671 | if (block_rsv->size == 0) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5672 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 5673 | blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5674 | /* |
| 5675 | * If we couldn't reserve metadata bytes try and use some from |
| 5676 | * the global reserve. |
| 5677 | */ |
| 5678 | if (ret && block_rsv != global_rsv) { |
| 5679 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5680 | if (!ret) |
| 5681 | return global_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5682 | return ERR_PTR(ret); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5683 | } else if (ret) { |
| 5684 | return ERR_PTR(ret); |
| 5685 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5686 | return block_rsv; |
| 5687 | } |
| 5688 | |
| 5689 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5690 | if (!ret) |
| 5691 | return block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5692 | if (ret) { |
| 5693 | WARN_ON(1); |
| 5694 | ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize, |
| 5695 | 0); |
| 5696 | if (!ret) { |
| 5697 | spin_lock(&block_rsv->lock); |
| 5698 | block_rsv->size += blocksize; |
| 5699 | spin_unlock(&block_rsv->lock); |
| 5700 | return block_rsv; |
| 5701 | } else if (ret && block_rsv != global_rsv) { |
| 5702 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5703 | if (!ret) |
| 5704 | return global_rsv; |
| 5705 | } |
| 5706 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5707 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5708 | return ERR_PTR(-ENOSPC); |
| 5709 | } |
| 5710 | |
| 5711 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5712 | { |
| 5713 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5714 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5715 | } |
| 5716 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5717 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5718 | * finds a free extent and does all the dirty work required for allocation |
| 5719 | * returns the key for the extent through ins, and a tree buffer for |
| 5720 | * the first block of the extent through buf. |
| 5721 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5722 | * returns the tree buffer or NULL. |
| 5723 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5724 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5725 | struct btrfs_root *root, u32 blocksize, |
| 5726 | u64 parent, u64 root_objectid, |
| 5727 | struct btrfs_disk_key *key, int level, |
| 5728 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5729 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5730 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5731 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5732 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5733 | u64 flags = 0; |
| 5734 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5735 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5736 | |
| 5737 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5738 | if (IS_ERR(block_rsv)) |
| 5739 | return ERR_CAST(block_rsv); |
| 5740 | |
| 5741 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5742 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5743 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5744 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5745 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5746 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5747 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5748 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5749 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5750 | BUG_ON(IS_ERR(buf)); |
| 5751 | |
| 5752 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5753 | if (parent == 0) |
| 5754 | parent = ins.objectid; |
| 5755 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5756 | } else |
| 5757 | BUG_ON(parent > 0); |
| 5758 | |
| 5759 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5760 | struct btrfs_delayed_extent_op *extent_op; |
| 5761 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5762 | BUG_ON(!extent_op); |
| 5763 | if (key) |
| 5764 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5765 | else |
| 5766 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5767 | extent_op->flags_to_set = flags; |
| 5768 | extent_op->update_key = 1; |
| 5769 | extent_op->update_flags = 1; |
| 5770 | extent_op->is_data = 0; |
| 5771 | |
| 5772 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5773 | ins.offset, parent, root_objectid, |
| 5774 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5775 | extent_op); |
| 5776 | BUG_ON(ret); |
| 5777 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5778 | return buf; |
| 5779 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5780 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5781 | struct walk_control { |
| 5782 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5783 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5784 | struct btrfs_key update_progress; |
| 5785 | int stage; |
| 5786 | int level; |
| 5787 | int shared_level; |
| 5788 | int update_ref; |
| 5789 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5790 | int reada_slot; |
| 5791 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5792 | }; |
| 5793 | |
| 5794 | #define DROP_REFERENCE 1 |
| 5795 | #define UPDATE_BACKREF 2 |
| 5796 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5797 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5798 | struct btrfs_root *root, |
| 5799 | struct walk_control *wc, |
| 5800 | struct btrfs_path *path) |
| 5801 | { |
| 5802 | u64 bytenr; |
| 5803 | u64 generation; |
| 5804 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5805 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5806 | u32 nritems; |
| 5807 | u32 blocksize; |
| 5808 | struct btrfs_key key; |
| 5809 | struct extent_buffer *eb; |
| 5810 | int ret; |
| 5811 | int slot; |
| 5812 | int nread = 0; |
| 5813 | |
| 5814 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5815 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5816 | wc->reada_count = max(wc->reada_count, 2); |
| 5817 | } else { |
| 5818 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5819 | wc->reada_count = min_t(int, wc->reada_count, |
| 5820 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5821 | } |
| 5822 | |
| 5823 | eb = path->nodes[wc->level]; |
| 5824 | nritems = btrfs_header_nritems(eb); |
| 5825 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5826 | |
| 5827 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5828 | if (nread >= wc->reada_count) |
| 5829 | break; |
| 5830 | |
| 5831 | cond_resched(); |
| 5832 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5833 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5834 | |
| 5835 | if (slot == path->slots[wc->level]) |
| 5836 | goto reada; |
| 5837 | |
| 5838 | if (wc->stage == UPDATE_BACKREF && |
| 5839 | generation <= root->root_key.offset) |
| 5840 | continue; |
| 5841 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5842 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5843 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5844 | &refs, &flags); |
| 5845 | BUG_ON(ret); |
| 5846 | BUG_ON(refs == 0); |
| 5847 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5848 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5849 | if (refs == 1) |
| 5850 | goto reada; |
| 5851 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5852 | if (wc->level == 1 && |
| 5853 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5854 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5855 | if (!wc->update_ref || |
| 5856 | generation <= root->root_key.offset) |
| 5857 | continue; |
| 5858 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5859 | ret = btrfs_comp_cpu_keys(&key, |
| 5860 | &wc->update_progress); |
| 5861 | if (ret < 0) |
| 5862 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5863 | } else { |
| 5864 | if (wc->level == 1 && |
| 5865 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5866 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5867 | } |
| 5868 | reada: |
| 5869 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5870 | generation); |
| 5871 | if (ret) |
| 5872 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5873 | nread++; |
| 5874 | } |
| 5875 | wc->reada_slot = slot; |
| 5876 | } |
| 5877 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5878 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5879 | * hepler to process tree block while walking down the tree. |
| 5880 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5881 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5882 | * back refs for pointers in the block. |
| 5883 | * |
| 5884 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5885 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5886 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5887 | struct btrfs_root *root, |
| 5888 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5889 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5890 | { |
| 5891 | int level = wc->level; |
| 5892 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5893 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5894 | int ret; |
| 5895 | |
| 5896 | if (wc->stage == UPDATE_BACKREF && |
| 5897 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5898 | return 1; |
| 5899 | |
| 5900 | /* |
| 5901 | * when reference count of tree block is 1, it won't increase |
| 5902 | * again. once full backref flag is set, we never clear it. |
| 5903 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5904 | if (lookup_info && |
| 5905 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5906 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5907 | BUG_ON(!path->locks[level]); |
| 5908 | ret = btrfs_lookup_extent_info(trans, root, |
| 5909 | eb->start, eb->len, |
| 5910 | &wc->refs[level], |
| 5911 | &wc->flags[level]); |
| 5912 | BUG_ON(ret); |
| 5913 | BUG_ON(wc->refs[level] == 0); |
| 5914 | } |
| 5915 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5916 | if (wc->stage == DROP_REFERENCE) { |
| 5917 | if (wc->refs[level] > 1) |
| 5918 | return 1; |
| 5919 | |
| 5920 | if (path->locks[level] && !wc->keep_locks) { |
| 5921 | btrfs_tree_unlock(eb); |
| 5922 | path->locks[level] = 0; |
| 5923 | } |
| 5924 | return 0; |
| 5925 | } |
| 5926 | |
| 5927 | /* wc->stage == UPDATE_BACKREF */ |
| 5928 | if (!(wc->flags[level] & flag)) { |
| 5929 | BUG_ON(!path->locks[level]); |
| 5930 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5931 | BUG_ON(ret); |
| 5932 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5933 | BUG_ON(ret); |
| 5934 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5935 | eb->len, flag, 0); |
| 5936 | BUG_ON(ret); |
| 5937 | wc->flags[level] |= flag; |
| 5938 | } |
| 5939 | |
| 5940 | /* |
| 5941 | * the block is shared by multiple trees, so it's not good to |
| 5942 | * keep the tree lock |
| 5943 | */ |
| 5944 | if (path->locks[level] && level > 0) { |
| 5945 | btrfs_tree_unlock(eb); |
| 5946 | path->locks[level] = 0; |
| 5947 | } |
| 5948 | return 0; |
| 5949 | } |
| 5950 | |
| 5951 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5952 | * hepler to process tree block pointer. |
| 5953 | * |
| 5954 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5955 | * reference count of the block pointed to. if the block |
| 5956 | * is shared and we need update back refs for the subtree |
| 5957 | * rooted at the block, this function changes wc->stage to |
| 5958 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5959 | * need to update back, this function drops the reference |
| 5960 | * to the block. |
| 5961 | * |
| 5962 | * NOTE: return value 1 means we should stop walking down. |
| 5963 | */ |
| 5964 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5965 | struct btrfs_root *root, |
| 5966 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5967 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5968 | { |
| 5969 | u64 bytenr; |
| 5970 | u64 generation; |
| 5971 | u64 parent; |
| 5972 | u32 blocksize; |
| 5973 | struct btrfs_key key; |
| 5974 | struct extent_buffer *next; |
| 5975 | int level = wc->level; |
| 5976 | int reada = 0; |
| 5977 | int ret = 0; |
| 5978 | |
| 5979 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 5980 | path->slots[level]); |
| 5981 | /* |
| 5982 | * if the lower level block was created before the snapshot |
| 5983 | * was created, we know there is no need to update back refs |
| 5984 | * for the subtree |
| 5985 | */ |
| 5986 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5987 | generation <= root->root_key.offset) { |
| 5988 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5989 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5990 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5991 | |
| 5992 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 5993 | blocksize = btrfs_level_size(root, level - 1); |
| 5994 | |
| 5995 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 5996 | if (!next) { |
| 5997 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51 | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 5998 | if (!next) |
| 5999 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6000 | reada = 1; |
| 6001 | } |
| 6002 | btrfs_tree_lock(next); |
| 6003 | btrfs_set_lock_blocking(next); |
| 6004 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6005 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6006 | &wc->refs[level - 1], |
| 6007 | &wc->flags[level - 1]); |
| 6008 | BUG_ON(ret); |
| 6009 | BUG_ON(wc->refs[level - 1] == 0); |
| 6010 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6011 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6012 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6013 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6014 | if (level == 1 && |
| 6015 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6016 | goto skip; |
| 6017 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6018 | if (!wc->update_ref || |
| 6019 | generation <= root->root_key.offset) |
| 6020 | goto skip; |
| 6021 | |
| 6022 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 6023 | path->slots[level]); |
| 6024 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 6025 | if (ret < 0) |
| 6026 | goto skip; |
| 6027 | |
| 6028 | wc->stage = UPDATE_BACKREF; |
| 6029 | wc->shared_level = level - 1; |
| 6030 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6031 | } else { |
| 6032 | if (level == 1 && |
| 6033 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6034 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6035 | } |
| 6036 | |
| 6037 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 6038 | btrfs_tree_unlock(next); |
| 6039 | free_extent_buffer(next); |
| 6040 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6041 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6042 | } |
| 6043 | |
| 6044 | if (!next) { |
| 6045 | if (reada && level == 1) |
| 6046 | reada_walk_down(trans, root, wc, path); |
| 6047 | next = read_tree_block(root, bytenr, blocksize, generation); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 6048 | if (!next) |
| 6049 | return -EIO; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6050 | btrfs_tree_lock(next); |
| 6051 | btrfs_set_lock_blocking(next); |
| 6052 | } |
| 6053 | |
| 6054 | level--; |
| 6055 | BUG_ON(level != btrfs_header_level(next)); |
| 6056 | path->nodes[level] = next; |
| 6057 | path->slots[level] = 0; |
| 6058 | path->locks[level] = 1; |
| 6059 | wc->level = level; |
| 6060 | if (wc->level == 1) |
| 6061 | wc->reada_slot = 0; |
| 6062 | return 0; |
| 6063 | skip: |
| 6064 | wc->refs[level - 1] = 0; |
| 6065 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6066 | if (wc->stage == DROP_REFERENCE) { |
| 6067 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6068 | parent = path->nodes[level]->start; |
| 6069 | } else { |
| 6070 | BUG_ON(root->root_key.objectid != |
| 6071 | btrfs_header_owner(path->nodes[level])); |
| 6072 | parent = 0; |
| 6073 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6074 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6075 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 6076 | root->root_key.objectid, level - 1, 0); |
| 6077 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6078 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6079 | btrfs_tree_unlock(next); |
| 6080 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6081 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6082 | return 1; |
| 6083 | } |
| 6084 | |
| 6085 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6086 | * hepler to process tree block while walking up the tree. |
| 6087 | * |
| 6088 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6089 | * reference count on the block. |
| 6090 | * |
| 6091 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6092 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6093 | * to UPDATE_BACKREF previously while processing the block. |
| 6094 | * |
| 6095 | * NOTE: return value 1 means we should stop walking up. |
| 6096 | */ |
| 6097 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6098 | struct btrfs_root *root, |
| 6099 | struct btrfs_path *path, |
| 6100 | struct walk_control *wc) |
| 6101 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6102 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6103 | int level = wc->level; |
| 6104 | struct extent_buffer *eb = path->nodes[level]; |
| 6105 | u64 parent = 0; |
| 6106 | |
| 6107 | if (wc->stage == UPDATE_BACKREF) { |
| 6108 | BUG_ON(wc->shared_level < level); |
| 6109 | if (level < wc->shared_level) |
| 6110 | goto out; |
| 6111 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6112 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6113 | if (ret > 0) |
| 6114 | wc->update_ref = 0; |
| 6115 | |
| 6116 | wc->stage = DROP_REFERENCE; |
| 6117 | wc->shared_level = -1; |
| 6118 | path->slots[level] = 0; |
| 6119 | |
| 6120 | /* |
| 6121 | * check reference count again if the block isn't locked. |
| 6122 | * we should start walking down the tree again if reference |
| 6123 | * count is one. |
| 6124 | */ |
| 6125 | if (!path->locks[level]) { |
| 6126 | BUG_ON(level == 0); |
| 6127 | btrfs_tree_lock(eb); |
| 6128 | btrfs_set_lock_blocking(eb); |
| 6129 | path->locks[level] = 1; |
| 6130 | |
| 6131 | ret = btrfs_lookup_extent_info(trans, root, |
| 6132 | eb->start, eb->len, |
| 6133 | &wc->refs[level], |
| 6134 | &wc->flags[level]); |
| 6135 | BUG_ON(ret); |
| 6136 | BUG_ON(wc->refs[level] == 0); |
| 6137 | if (wc->refs[level] == 1) { |
| 6138 | btrfs_tree_unlock(eb); |
| 6139 | path->locks[level] = 0; |
| 6140 | return 1; |
| 6141 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6142 | } |
| 6143 | } |
| 6144 | |
| 6145 | /* wc->stage == DROP_REFERENCE */ |
| 6146 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6147 | |
| 6148 | if (wc->refs[level] == 1) { |
| 6149 | if (level == 0) { |
| 6150 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6151 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 6152 | else |
| 6153 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6154 | BUG_ON(ret); |
| 6155 | } |
| 6156 | /* make block locked assertion in clean_tree_block happy */ |
| 6157 | if (!path->locks[level] && |
| 6158 | btrfs_header_generation(eb) == trans->transid) { |
| 6159 | btrfs_tree_lock(eb); |
| 6160 | btrfs_set_lock_blocking(eb); |
| 6161 | path->locks[level] = 1; |
| 6162 | } |
| 6163 | clean_tree_block(trans, root, eb); |
| 6164 | } |
| 6165 | |
| 6166 | if (eb == root->node) { |
| 6167 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6168 | parent = eb->start; |
| 6169 | else |
| 6170 | BUG_ON(root->root_key.objectid != |
| 6171 | btrfs_header_owner(eb)); |
| 6172 | } else { |
| 6173 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6174 | parent = path->nodes[level + 1]->start; |
| 6175 | else |
| 6176 | BUG_ON(root->root_key.objectid != |
| 6177 | btrfs_header_owner(path->nodes[level + 1])); |
| 6178 | } |
| 6179 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6180 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6181 | out: |
| 6182 | wc->refs[level] = 0; |
| 6183 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6184 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6185 | } |
| 6186 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6187 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6188 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6189 | struct btrfs_path *path, |
| 6190 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6191 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6192 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6193 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6194 | int ret; |
| 6195 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6196 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6197 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6198 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6199 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6200 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6201 | if (level == 0) |
| 6202 | break; |
| 6203 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6204 | if (path->slots[level] >= |
| 6205 | btrfs_header_nritems(path->nodes[level])) |
| 6206 | break; |
| 6207 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6208 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6209 | if (ret > 0) { |
| 6210 | path->slots[level]++; |
| 6211 | continue; |
Miao Xie | 90d2c51 | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6212 | } else if (ret < 0) |
| 6213 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6214 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6215 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6216 | return 0; |
| 6217 | } |
| 6218 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6219 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6220 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6221 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6222 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6223 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6224 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6225 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6226 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6227 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6228 | while (level < max_level && path->nodes[level]) { |
| 6229 | wc->level = level; |
| 6230 | if (path->slots[level] + 1 < |
| 6231 | btrfs_header_nritems(path->nodes[level])) { |
| 6232 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6233 | return 0; |
| 6234 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6235 | ret = walk_up_proc(trans, root, path, wc); |
| 6236 | if (ret > 0) |
| 6237 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6238 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6239 | if (path->locks[level]) { |
| 6240 | btrfs_tree_unlock(path->nodes[level]); |
| 6241 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6242 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6243 | free_extent_buffer(path->nodes[level]); |
| 6244 | path->nodes[level] = NULL; |
| 6245 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6246 | } |
| 6247 | } |
| 6248 | return 1; |
| 6249 | } |
| 6250 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6251 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6252 | * drop a subvolume tree. |
| 6253 | * |
| 6254 | * this function traverses the tree freeing any blocks that only |
| 6255 | * referenced by the tree. |
| 6256 | * |
| 6257 | * when a shared tree block is found. this function decreases its |
| 6258 | * reference count by one. if update_ref is true, this function |
| 6259 | * also make sure backrefs for the shared block and all lower level |
| 6260 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6261 | */ |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6262 | int btrfs_drop_snapshot(struct btrfs_root *root, |
| 6263 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6264 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6265 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6266 | struct btrfs_trans_handle *trans; |
| 6267 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6268 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6269 | struct walk_control *wc; |
| 6270 | struct btrfs_key key; |
| 6271 | int err = 0; |
| 6272 | int ret; |
| 6273 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6274 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6275 | path = btrfs_alloc_path(); |
| 6276 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6277 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6278 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
| 6279 | BUG_ON(!wc); |
| 6280 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6281 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6282 | BUG_ON(IS_ERR(trans)); |
| 6283 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6284 | if (block_rsv) |
| 6285 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6286 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6287 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6288 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6289 | path->nodes[level] = btrfs_lock_root_node(root); |
| 6290 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6291 | path->slots[level] = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6292 | path->locks[level] = 1; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6293 | memset(&wc->update_progress, 0, |
| 6294 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6295 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6296 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6297 | memcpy(&wc->update_progress, &key, |
| 6298 | sizeof(wc->update_progress)); |
| 6299 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6300 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6301 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6302 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6303 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6304 | path->lowest_level = 0; |
| 6305 | if (ret < 0) { |
| 6306 | err = ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6307 | goto out; |
| 6308 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6309 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6310 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6311 | /* |
| 6312 | * unlock our path, this is safe because only this |
| 6313 | * function is allowed to delete this snapshot |
| 6314 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6315 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6316 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6317 | level = btrfs_header_level(root->node); |
| 6318 | while (1) { |
| 6319 | btrfs_tree_lock(path->nodes[level]); |
| 6320 | btrfs_set_lock_blocking(path->nodes[level]); |
| 6321 | |
| 6322 | ret = btrfs_lookup_extent_info(trans, root, |
| 6323 | path->nodes[level]->start, |
| 6324 | path->nodes[level]->len, |
| 6325 | &wc->refs[level], |
| 6326 | &wc->flags[level]); |
| 6327 | BUG_ON(ret); |
| 6328 | BUG_ON(wc->refs[level] == 0); |
| 6329 | |
| 6330 | if (level == root_item->drop_level) |
| 6331 | break; |
| 6332 | |
| 6333 | btrfs_tree_unlock(path->nodes[level]); |
| 6334 | WARN_ON(wc->refs[level] != 1); |
| 6335 | level--; |
| 6336 | } |
| 6337 | } |
| 6338 | |
| 6339 | wc->level = level; |
| 6340 | wc->shared_level = -1; |
| 6341 | wc->stage = DROP_REFERENCE; |
| 6342 | wc->update_ref = update_ref; |
| 6343 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6344 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6345 | |
| 6346 | while (1) { |
| 6347 | ret = walk_down_tree(trans, root, path, wc); |
| 6348 | if (ret < 0) { |
| 6349 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6350 | break; |
| 6351 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6352 | |
| 6353 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6354 | if (ret < 0) { |
| 6355 | err = ret; |
| 6356 | break; |
| 6357 | } |
| 6358 | |
| 6359 | if (ret > 0) { |
| 6360 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6361 | break; |
| 6362 | } |
| 6363 | |
| 6364 | if (wc->stage == DROP_REFERENCE) { |
| 6365 | level = wc->level; |
| 6366 | btrfs_node_key(path->nodes[level], |
| 6367 | &root_item->drop_progress, |
| 6368 | path->slots[level]); |
| 6369 | root_item->drop_level = level; |
| 6370 | } |
| 6371 | |
| 6372 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6373 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6374 | ret = btrfs_update_root(trans, tree_root, |
| 6375 | &root->root_key, |
| 6376 | root_item); |
| 6377 | BUG_ON(ret); |
| 6378 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6379 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6380 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6381 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6382 | if (block_rsv) |
| 6383 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6384 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6385 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6386 | btrfs_release_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6387 | BUG_ON(err); |
| 6388 | |
| 6389 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6390 | BUG_ON(ret); |
| 6391 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6392 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6393 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6394 | NULL, NULL); |
| 6395 | BUG_ON(ret < 0); |
| 6396 | if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 6397 | /* if we fail to delete the orphan item this time |
| 6398 | * around, it'll get picked up the next time. |
| 6399 | * |
| 6400 | * The most common failure here is just -ENOENT. |
| 6401 | */ |
| 6402 | btrfs_del_orphan_item(trans, tree_root, |
| 6403 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6404 | } |
| 6405 | } |
| 6406 | |
| 6407 | if (root->in_radix) { |
| 6408 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6409 | } else { |
| 6410 | free_extent_buffer(root->node); |
| 6411 | free_extent_buffer(root->commit_root); |
| 6412 | kfree(root); |
| 6413 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6414 | out: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6415 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6416 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6417 | btrfs_free_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6418 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6419 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6420 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6421 | /* |
| 6422 | * drop subtree rooted at tree block 'node'. |
| 6423 | * |
| 6424 | * NOTE: this function will unlock and release tree block 'node' |
| 6425 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6426 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6427 | struct btrfs_root *root, |
| 6428 | struct extent_buffer *node, |
| 6429 | struct extent_buffer *parent) |
| 6430 | { |
| 6431 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6432 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6433 | int level; |
| 6434 | int parent_level; |
| 6435 | int ret = 0; |
| 6436 | int wret; |
| 6437 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6438 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6439 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6440 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6441 | if (!path) |
| 6442 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6443 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6444 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6445 | if (!wc) { |
| 6446 | btrfs_free_path(path); |
| 6447 | return -ENOMEM; |
| 6448 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6449 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6450 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6451 | parent_level = btrfs_header_level(parent); |
| 6452 | extent_buffer_get(parent); |
| 6453 | path->nodes[parent_level] = parent; |
| 6454 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6455 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6456 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6457 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6458 | path->nodes[level] = node; |
| 6459 | path->slots[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6460 | path->locks[level] = 1; |
| 6461 | |
| 6462 | wc->refs[parent_level] = 1; |
| 6463 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6464 | wc->level = level; |
| 6465 | wc->shared_level = -1; |
| 6466 | wc->stage = DROP_REFERENCE; |
| 6467 | wc->update_ref = 0; |
| 6468 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6469 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6470 | |
| 6471 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6472 | wret = walk_down_tree(trans, root, path, wc); |
| 6473 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6474 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6475 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6476 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6477 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6478 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6479 | if (wret < 0) |
| 6480 | ret = wret; |
| 6481 | if (wret != 0) |
| 6482 | break; |
| 6483 | } |
| 6484 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6485 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6486 | btrfs_free_path(path); |
| 6487 | return ret; |
| 6488 | } |
| 6489 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6490 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 6491 | { |
| 6492 | u64 num_devices; |
| 6493 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 6494 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 6495 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6496 | /* |
| 6497 | * we add in the count of missing devices because we want |
| 6498 | * to make sure that any RAID levels on a degraded FS |
| 6499 | * continue to be honored. |
| 6500 | */ |
| 6501 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 6502 | root->fs_info->fs_devices->missing_devices; |
| 6503 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6504 | if (num_devices == 1) { |
| 6505 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6506 | stripped = flags & ~stripped; |
| 6507 | |
| 6508 | /* turn raid0 into single device chunks */ |
| 6509 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 6510 | return stripped; |
| 6511 | |
| 6512 | /* turn mirroring into duplication */ |
| 6513 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6514 | BTRFS_BLOCK_GROUP_RAID10)) |
| 6515 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 6516 | return flags; |
| 6517 | } else { |
| 6518 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6519 | if (flags & stripped) |
| 6520 | return flags; |
| 6521 | |
| 6522 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6523 | stripped = flags & ~stripped; |
| 6524 | |
| 6525 | /* switch duplicated blocks with raid1 */ |
| 6526 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 6527 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 6528 | |
| 6529 | /* turn single device chunks into raid0 */ |
| 6530 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 6531 | } |
| 6532 | return flags; |
| 6533 | } |
| 6534 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6535 | static int set_block_group_ro(struct btrfs_block_group_cache *cache) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6536 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6537 | struct btrfs_space_info *sinfo = cache->space_info; |
| 6538 | u64 num_bytes; |
| 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 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6544 | spin_lock(&sinfo->lock); |
| 6545 | spin_lock(&cache->lock); |
| 6546 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 6547 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6548 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6549 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
| 6550 | sinfo->bytes_may_use + sinfo->bytes_readonly + |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 6551 | cache->reserved_pinned + num_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6552 | sinfo->bytes_readonly += num_bytes; |
| 6553 | sinfo->bytes_reserved += cache->reserved_pinned; |
| 6554 | cache->reserved_pinned = 0; |
| 6555 | cache->ro = 1; |
| 6556 | ret = 0; |
| 6557 | } |
Chris Mason | 65e5341 | 2010-12-24 06:41:52 -0500 | [diff] [blame] | 6558 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6559 | spin_unlock(&cache->lock); |
| 6560 | spin_unlock(&sinfo->lock); |
| 6561 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6562 | } |
| 6563 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6564 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 6565 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6566 | |
| 6567 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6568 | struct btrfs_trans_handle *trans; |
| 6569 | u64 alloc_flags; |
| 6570 | int ret; |
| 6571 | |
| 6572 | BUG_ON(cache->ro); |
| 6573 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6574 | trans = btrfs_join_transaction(root); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6575 | BUG_ON(IS_ERR(trans)); |
| 6576 | |
| 6577 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 6578 | if (alloc_flags != cache->flags) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6579 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6580 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6581 | |
| 6582 | ret = set_block_group_ro(cache); |
| 6583 | if (!ret) |
| 6584 | goto out; |
| 6585 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6586 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6587 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6588 | if (ret < 0) |
| 6589 | goto out; |
| 6590 | ret = set_block_group_ro(cache); |
| 6591 | out: |
| 6592 | btrfs_end_transaction(trans, root); |
| 6593 | return ret; |
| 6594 | } |
| 6595 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 6596 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 6597 | struct btrfs_root *root, u64 type) |
| 6598 | { |
| 6599 | u64 alloc_flags = get_alloc_profile(root, type); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6600 | return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6601 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 6602 | } |
| 6603 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 6604 | /* |
| 6605 | * helper to account the unused space of all the readonly block group in the |
| 6606 | * list. takes mirrors into account. |
| 6607 | */ |
| 6608 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 6609 | { |
| 6610 | struct btrfs_block_group_cache *block_group; |
| 6611 | u64 free_bytes = 0; |
| 6612 | int factor; |
| 6613 | |
| 6614 | list_for_each_entry(block_group, groups_list, list) { |
| 6615 | spin_lock(&block_group->lock); |
| 6616 | |
| 6617 | if (!block_group->ro) { |
| 6618 | spin_unlock(&block_group->lock); |
| 6619 | continue; |
| 6620 | } |
| 6621 | |
| 6622 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6623 | BTRFS_BLOCK_GROUP_RAID10 | |
| 6624 | BTRFS_BLOCK_GROUP_DUP)) |
| 6625 | factor = 2; |
| 6626 | else |
| 6627 | factor = 1; |
| 6628 | |
| 6629 | free_bytes += (block_group->key.offset - |
| 6630 | btrfs_block_group_used(&block_group->item)) * |
| 6631 | factor; |
| 6632 | |
| 6633 | spin_unlock(&block_group->lock); |
| 6634 | } |
| 6635 | |
| 6636 | return free_bytes; |
| 6637 | } |
| 6638 | |
| 6639 | /* |
| 6640 | * helper to account the unused space of all the readonly block group in the |
| 6641 | * space_info. takes mirrors into account. |
| 6642 | */ |
| 6643 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 6644 | { |
| 6645 | int i; |
| 6646 | u64 free_bytes = 0; |
| 6647 | |
| 6648 | spin_lock(&sinfo->lock); |
| 6649 | |
| 6650 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 6651 | if (!list_empty(&sinfo->block_groups[i])) |
| 6652 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 6653 | &sinfo->block_groups[i]); |
| 6654 | |
| 6655 | spin_unlock(&sinfo->lock); |
| 6656 | |
| 6657 | return free_bytes; |
| 6658 | } |
| 6659 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6660 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 6661 | struct btrfs_block_group_cache *cache) |
| 6662 | { |
| 6663 | struct btrfs_space_info *sinfo = cache->space_info; |
| 6664 | u64 num_bytes; |
| 6665 | |
| 6666 | BUG_ON(!cache->ro); |
| 6667 | |
| 6668 | spin_lock(&sinfo->lock); |
| 6669 | spin_lock(&cache->lock); |
| 6670 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 6671 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 6672 | sinfo->bytes_readonly -= num_bytes; |
| 6673 | cache->ro = 0; |
| 6674 | spin_unlock(&cache->lock); |
| 6675 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6676 | return 0; |
| 6677 | } |
| 6678 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6679 | /* |
| 6680 | * checks to see if its even possible to relocate this block group. |
| 6681 | * |
| 6682 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 6683 | * ok to go ahead and try. |
| 6684 | */ |
| 6685 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6686 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6687 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6688 | struct btrfs_space_info *space_info; |
| 6689 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6690 | struct btrfs_device *device; |
| 6691 | int full = 0; |
| 6692 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6693 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6694 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6695 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6696 | /* odd, couldn't find the block group, leave it alone */ |
| 6697 | if (!block_group) |
| 6698 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6699 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6700 | /* no bytes used, we're good */ |
| 6701 | if (!btrfs_block_group_used(&block_group->item)) |
| 6702 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6703 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6704 | space_info = block_group->space_info; |
| 6705 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6706 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6707 | full = space_info->full; |
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 | /* |
| 6710 | * 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] | 6711 | * relocate it unless we're able to allocate a new chunk below. |
| 6712 | * |
| 6713 | * Otherwise, we need to make sure we have room in the space to handle |
| 6714 | * 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] | 6715 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 6716 | if ((space_info->total_bytes != block_group->key.offset) && |
| 6717 | (space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6718 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 6719 | btrfs_block_group_used(&block_group->item) < |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 6720 | space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6721 | spin_unlock(&space_info->lock); |
| 6722 | goto out; |
| 6723 | } |
| 6724 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6725 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6726 | /* |
| 6727 | * ok we don't have enough space, but maybe we have free space on our |
| 6728 | * devices to allocate new chunks for relocation, so loop through our |
| 6729 | * alloc devices and guess if we have enough space. However, if we |
| 6730 | * were marked as full, then we know there aren't enough chunks, and we |
| 6731 | * can just return. |
| 6732 | */ |
| 6733 | ret = -1; |
| 6734 | if (full) |
| 6735 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6736 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6737 | mutex_lock(&root->fs_info->chunk_mutex); |
| 6738 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
| 6739 | u64 min_free = btrfs_block_group_used(&block_group->item); |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 6740 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6741 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6742 | /* |
| 6743 | * check to make sure we can actually find a chunk with enough |
| 6744 | * space to fit our block group in. |
| 6745 | */ |
| 6746 | if (device->total_bytes > device->bytes_used + min_free) { |
| 6747 | ret = find_free_dev_extent(NULL, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 6748 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6749 | if (!ret) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6750 | break; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6751 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6752 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6753 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6754 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6755 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6756 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6757 | return ret; |
| 6758 | } |
| 6759 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 6760 | static int find_first_block_group(struct btrfs_root *root, |
| 6761 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6762 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6763 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6764 | struct btrfs_key found_key; |
| 6765 | struct extent_buffer *leaf; |
| 6766 | int slot; |
| 6767 | |
| 6768 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 6769 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6770 | goto out; |
| 6771 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6772 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6773 | slot = path->slots[0]; |
| 6774 | leaf = path->nodes[0]; |
| 6775 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6776 | ret = btrfs_next_leaf(root, path); |
| 6777 | if (ret == 0) |
| 6778 | continue; |
| 6779 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6780 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6781 | break; |
| 6782 | } |
| 6783 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6784 | |
| 6785 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6786 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 6787 | ret = 0; |
| 6788 | goto out; |
| 6789 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6790 | path->slots[0]++; |
| 6791 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6792 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6793 | return ret; |
| 6794 | } |
| 6795 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6796 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 6797 | { |
| 6798 | struct btrfs_block_group_cache *block_group; |
| 6799 | u64 last = 0; |
| 6800 | |
| 6801 | while (1) { |
| 6802 | struct inode *inode; |
| 6803 | |
| 6804 | block_group = btrfs_lookup_first_block_group(info, last); |
| 6805 | while (block_group) { |
| 6806 | spin_lock(&block_group->lock); |
| 6807 | if (block_group->iref) |
| 6808 | break; |
| 6809 | spin_unlock(&block_group->lock); |
| 6810 | block_group = next_block_group(info->tree_root, |
| 6811 | block_group); |
| 6812 | } |
| 6813 | if (!block_group) { |
| 6814 | if (last == 0) |
| 6815 | break; |
| 6816 | last = 0; |
| 6817 | continue; |
| 6818 | } |
| 6819 | |
| 6820 | inode = block_group->inode; |
| 6821 | block_group->iref = 0; |
| 6822 | block_group->inode = NULL; |
| 6823 | spin_unlock(&block_group->lock); |
| 6824 | iput(inode); |
| 6825 | last = block_group->key.objectid + block_group->key.offset; |
| 6826 | btrfs_put_block_group(block_group); |
| 6827 | } |
| 6828 | } |
| 6829 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6830 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 6831 | { |
| 6832 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6833 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6834 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6835 | struct rb_node *n; |
| 6836 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6837 | down_write(&info->extent_commit_sem); |
| 6838 | while (!list_empty(&info->caching_block_groups)) { |
| 6839 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 6840 | struct btrfs_caching_control, list); |
| 6841 | list_del(&caching_ctl->list); |
| 6842 | put_caching_control(caching_ctl); |
| 6843 | } |
| 6844 | up_write(&info->extent_commit_sem); |
| 6845 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6846 | spin_lock(&info->block_group_cache_lock); |
| 6847 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 6848 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 6849 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6850 | rb_erase(&block_group->cache_node, |
| 6851 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6852 | spin_unlock(&info->block_group_cache_lock); |
| 6853 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6854 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6855 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6856 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6857 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6858 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6859 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6860 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 6861 | /* |
| 6862 | * We haven't cached this block group, which means we could |
| 6863 | * possibly have excluded extents on this block group. |
| 6864 | */ |
| 6865 | if (block_group->cached == BTRFS_CACHE_NO) |
| 6866 | free_excluded_extents(info->extent_root, block_group); |
| 6867 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6868 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 6869 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6870 | |
| 6871 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6872 | } |
| 6873 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6874 | |
| 6875 | /* now that all the block groups are freed, go through and |
| 6876 | * free all the space_info structs. This is only called during |
| 6877 | * the final stages of unmount, and so we know nobody is |
| 6878 | * using them. We call synchronize_rcu() once before we start, |
| 6879 | * just to be on the safe side. |
| 6880 | */ |
| 6881 | synchronize_rcu(); |
| 6882 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6883 | release_global_block_rsv(info); |
| 6884 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6885 | while(!list_empty(&info->space_info)) { |
| 6886 | space_info = list_entry(info->space_info.next, |
| 6887 | struct btrfs_space_info, |
| 6888 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6889 | if (space_info->bytes_pinned > 0 || |
| 6890 | space_info->bytes_reserved > 0) { |
| 6891 | WARN_ON(1); |
| 6892 | dump_space_info(space_info, 0, 0); |
| 6893 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6894 | list_del(&space_info->list); |
| 6895 | kfree(space_info); |
| 6896 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6897 | return 0; |
| 6898 | } |
| 6899 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6900 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 6901 | struct btrfs_block_group_cache *cache) |
| 6902 | { |
| 6903 | int index = get_block_group_index(cache); |
| 6904 | |
| 6905 | down_write(&space_info->groups_sem); |
| 6906 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 6907 | up_write(&space_info->groups_sem); |
| 6908 | } |
| 6909 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6910 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 6911 | { |
| 6912 | struct btrfs_path *path; |
| 6913 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6914 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 6915 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6916 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6917 | struct btrfs_key key; |
| 6918 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6919 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6920 | int need_clear = 0; |
| 6921 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 6922 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 6923 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6924 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6925 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6926 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6927 | path = btrfs_alloc_path(); |
| 6928 | if (!path) |
| 6929 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 6930 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6931 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6932 | cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); |
| 6933 | if (cache_gen != 0 && |
| 6934 | btrfs_super_generation(&root->fs_info->super_copy) != cache_gen) |
| 6935 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 6936 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 6937 | need_clear = 1; |
Josef Bacik | 8216ef8 | 2010-10-28 16:55:47 -0400 | [diff] [blame] | 6938 | if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen) |
| 6939 | printk(KERN_INFO "btrfs: disk space caching is enabled\n"); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6940 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6941 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6942 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6943 | if (ret > 0) |
| 6944 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6945 | if (ret != 0) |
| 6946 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6947 | leaf = path->nodes[0]; |
| 6948 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 6949 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6950 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6951 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6952 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6953 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 6954 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 6955 | GFP_NOFS); |
| 6956 | if (!cache->free_space_ctl) { |
| 6957 | kfree(cache); |
| 6958 | ret = -ENOMEM; |
| 6959 | goto error; |
| 6960 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 6961 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6962 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6963 | spin_lock_init(&cache->lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6964 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6965 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6966 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6967 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6968 | if (need_clear) |
| 6969 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 6970 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6971 | read_extent_buffer(leaf, &cache->item, |
| 6972 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 6973 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6974 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6975 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6976 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6977 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6978 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6979 | cache->sectorsize = root->sectorsize; |
| 6980 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 6981 | btrfs_init_free_space_ctl(cache); |
| 6982 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6983 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 6984 | * We need to exclude the super stripes now so that the space |
| 6985 | * info has super bytes accounted for, otherwise we'll think |
| 6986 | * we have more space than we actually do. |
| 6987 | */ |
| 6988 | exclude_super_stripes(root, cache); |
| 6989 | |
| 6990 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6991 | * check for two cases, either we are full, and therefore |
| 6992 | * don't need to bother with the caching work since we won't |
| 6993 | * find any space, or we are empty, and we can just add all |
| 6994 | * the space in and be done with it. This saves us _alot_ of |
| 6995 | * time, particularly in the full case. |
| 6996 | */ |
| 6997 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6998 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6999 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7000 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7001 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7002 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7003 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7004 | add_new_free_space(cache, root->fs_info, |
| 7005 | found_key.objectid, |
| 7006 | found_key.objectid + |
| 7007 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7008 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7009 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7010 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7011 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7012 | btrfs_block_group_used(&cache->item), |
| 7013 | &space_info); |
| 7014 | BUG_ON(ret); |
| 7015 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7016 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7017 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7018 | spin_unlock(&cache->space_info->lock); |
| 7019 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7020 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7021 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7022 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7023 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7024 | |
| 7025 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7026 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7027 | set_block_group_ro(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7028 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7029 | |
| 7030 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 7031 | if (!(get_alloc_profile(root, space_info->flags) & |
| 7032 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 7033 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7034 | BTRFS_BLOCK_GROUP_DUP))) |
| 7035 | continue; |
| 7036 | /* |
| 7037 | * avoid allocating from un-mirrored block group if there are |
| 7038 | * mirrored block groups. |
| 7039 | */ |
| 7040 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7041 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7042 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7043 | set_block_group_ro(cache); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7044 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7045 | |
| 7046 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7047 | ret = 0; |
| 7048 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7049 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7050 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7051 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7052 | |
| 7053 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7054 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7055 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7056 | u64 size) |
| 7057 | { |
| 7058 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7059 | struct btrfs_root *extent_root; |
| 7060 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7061 | |
| 7062 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7063 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7064 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7065 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7066 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7067 | if (!cache) |
| 7068 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7069 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7070 | GFP_NOFS); |
| 7071 | if (!cache->free_space_ctl) { |
| 7072 | kfree(cache); |
| 7073 | return -ENOMEM; |
| 7074 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7075 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7076 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7077 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7078 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7079 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7080 | cache->fs_info = root->fs_info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7081 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7082 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7083 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7084 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7085 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7086 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7087 | btrfs_init_free_space_ctl(cache); |
| 7088 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7089 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7090 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7091 | cache->flags = type; |
| 7092 | btrfs_set_block_group_flags(&cache->item, type); |
| 7093 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7094 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7095 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7096 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7097 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7098 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7099 | chunk_offset + size); |
| 7100 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7101 | free_excluded_extents(root, cache); |
| 7102 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7103 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7104 | &cache->space_info); |
| 7105 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7106 | |
| 7107 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7108 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7109 | spin_unlock(&cache->space_info->lock); |
| 7110 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7111 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7112 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7113 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7114 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7115 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7116 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7117 | sizeof(cache->item)); |
| 7118 | BUG_ON(ret); |
| 7119 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7120 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7121 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7122 | return 0; |
| 7123 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7124 | |
| 7125 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7126 | struct btrfs_root *root, u64 group_start) |
| 7127 | { |
| 7128 | struct btrfs_path *path; |
| 7129 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7130 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7131 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7132 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7133 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7134 | int ret; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7135 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7136 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7137 | root = root->fs_info->extent_root; |
| 7138 | |
| 7139 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7140 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7141 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7142 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 7143 | /* |
| 7144 | * Free the reserved super bytes from this block group before |
| 7145 | * remove it. |
| 7146 | */ |
| 7147 | free_excluded_extents(root, block_group); |
| 7148 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7149 | memcpy(&key, &block_group->key, sizeof(key)); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7150 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 7151 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7152 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7153 | factor = 2; |
| 7154 | else |
| 7155 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7156 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7157 | /* make sure this block group isn't part of an allocation cluster */ |
| 7158 | cluster = &root->fs_info->data_alloc_cluster; |
| 7159 | spin_lock(&cluster->refill_lock); |
| 7160 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7161 | spin_unlock(&cluster->refill_lock); |
| 7162 | |
| 7163 | /* |
| 7164 | * make sure this block group isn't part of a metadata |
| 7165 | * allocation cluster |
| 7166 | */ |
| 7167 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7168 | spin_lock(&cluster->refill_lock); |
| 7169 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7170 | spin_unlock(&cluster->refill_lock); |
| 7171 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7172 | path = btrfs_alloc_path(); |
| 7173 | BUG_ON(!path); |
| 7174 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7175 | inode = lookup_free_space_inode(root, block_group, path); |
| 7176 | if (!IS_ERR(inode)) { |
| 7177 | btrfs_orphan_add(trans, inode); |
| 7178 | clear_nlink(inode); |
| 7179 | /* One for the block groups ref */ |
| 7180 | spin_lock(&block_group->lock); |
| 7181 | if (block_group->iref) { |
| 7182 | block_group->iref = 0; |
| 7183 | block_group->inode = NULL; |
| 7184 | spin_unlock(&block_group->lock); |
| 7185 | iput(inode); |
| 7186 | } else { |
| 7187 | spin_unlock(&block_group->lock); |
| 7188 | } |
| 7189 | /* One for our lookup ref */ |
| 7190 | iput(inode); |
| 7191 | } |
| 7192 | |
| 7193 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 7194 | key.offset = block_group->key.objectid; |
| 7195 | key.type = 0; |
| 7196 | |
| 7197 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 7198 | if (ret < 0) |
| 7199 | goto out; |
| 7200 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7201 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7202 | if (ret == 0) { |
| 7203 | ret = btrfs_del_item(trans, tree_root, path); |
| 7204 | if (ret) |
| 7205 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7206 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7207 | } |
| 7208 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7209 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7210 | rb_erase(&block_group->cache_node, |
| 7211 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7212 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7213 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7214 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7215 | /* |
| 7216 | * we must use list_del_init so people can check to see if they |
| 7217 | * are still on the list after taking the semaphore |
| 7218 | */ |
| 7219 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7220 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7221 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7222 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7223 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7224 | |
| 7225 | btrfs_remove_free_space_cache(block_group); |
| 7226 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7227 | spin_lock(&block_group->space_info->lock); |
| 7228 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7229 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7230 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7231 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7232 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7233 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7234 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7235 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7236 | |
Chris Mason | fa9c0d7 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7237 | btrfs_put_block_group(block_group); |
| 7238 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7239 | |
| 7240 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7241 | if (ret > 0) |
| 7242 | ret = -EIO; |
| 7243 | if (ret < 0) |
| 7244 | goto out; |
| 7245 | |
| 7246 | ret = btrfs_del_item(trans, root, path); |
| 7247 | out: |
| 7248 | btrfs_free_path(path); |
| 7249 | return ret; |
| 7250 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7251 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7252 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 7253 | { |
| 7254 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7255 | struct btrfs_super_block *disk_super; |
| 7256 | u64 features; |
| 7257 | u64 flags; |
| 7258 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7259 | int ret; |
| 7260 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7261 | disk_super = &fs_info->super_copy; |
| 7262 | if (!btrfs_super_root(disk_super)) |
| 7263 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7264 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7265 | features = btrfs_super_incompat_flags(disk_super); |
| 7266 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 7267 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7268 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7269 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 7270 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7271 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7272 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7273 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7274 | if (mixed) { |
| 7275 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 7276 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7277 | } else { |
| 7278 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 7279 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7280 | if (ret) |
| 7281 | goto out; |
| 7282 | |
| 7283 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 7284 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7285 | } |
| 7286 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7287 | return ret; |
| 7288 | } |
| 7289 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7290 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 7291 | { |
| 7292 | return unpin_extent_range(root, start, end); |
| 7293 | } |
| 7294 | |
| 7295 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 7296 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7297 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 7298 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7299 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 7300 | |
| 7301 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 7302 | { |
| 7303 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 7304 | struct btrfs_block_group_cache *cache = NULL; |
| 7305 | u64 group_trimmed; |
| 7306 | u64 start; |
| 7307 | u64 end; |
| 7308 | u64 trimmed = 0; |
| 7309 | int ret = 0; |
| 7310 | |
| 7311 | cache = btrfs_lookup_block_group(fs_info, range->start); |
| 7312 | |
| 7313 | while (cache) { |
| 7314 | if (cache->key.objectid >= (range->start + range->len)) { |
| 7315 | btrfs_put_block_group(cache); |
| 7316 | break; |
| 7317 | } |
| 7318 | |
| 7319 | start = max(range->start, cache->key.objectid); |
| 7320 | end = min(range->start + range->len, |
| 7321 | cache->key.objectid + cache->key.offset); |
| 7322 | |
| 7323 | if (end - start >= range->minlen) { |
| 7324 | if (!block_group_cache_done(cache)) { |
| 7325 | ret = cache_block_group(cache, NULL, root, 0); |
| 7326 | if (!ret) |
| 7327 | wait_block_group_cache_done(cache); |
| 7328 | } |
| 7329 | ret = btrfs_trim_block_group(cache, |
| 7330 | &group_trimmed, |
| 7331 | start, |
| 7332 | end, |
| 7333 | range->minlen); |
| 7334 | |
| 7335 | trimmed += group_trimmed; |
| 7336 | if (ret) { |
| 7337 | btrfs_put_block_group(cache); |
| 7338 | break; |
| 7339 | } |
| 7340 | } |
| 7341 | |
| 7342 | cache = next_block_group(fs_info->tree_root, cache); |
| 7343 | } |
| 7344 | |
| 7345 | range->len = trimmed; |
| 7346 | return ret; |
| 7347 | } |