Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 20 | #include <linux/writeback.h> |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 21 | #include <linux/blkdev.h> |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 22 | #include <linux/sort.h> |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 23 | #include <linux/rcupdate.h> |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 26 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 27 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 28 | #include "ctree.h" |
| 29 | #include "disk-io.h" |
| 30 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 31 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 32 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 33 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 34 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 36 | /* control flags for do_chunk_alloc's force field |
| 37 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
| 38 | * if we really need one. |
| 39 | * |
| 40 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
| 41 | * |
| 42 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
| 43 | * if we have very few chunks already allocated. This is |
| 44 | * used as part of the clustering code to help make sure |
| 45 | * we have a good pool of storage to cluster in, without |
| 46 | * filling the FS with empty chunks |
| 47 | * |
| 48 | */ |
| 49 | enum { |
| 50 | CHUNK_ALLOC_NO_FORCE = 0, |
| 51 | CHUNK_ALLOC_FORCE = 1, |
| 52 | CHUNK_ALLOC_LIMITED = 2, |
| 53 | }; |
| 54 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 55 | /* |
| 56 | * Control how reservations are dealt with. |
| 57 | * |
| 58 | * RESERVE_FREE - freeing a reservation. |
| 59 | * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for |
| 60 | * ENOSPC accounting |
| 61 | * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update |
| 62 | * bytes_may_use as the ENOSPC accounting is done elsewhere |
| 63 | */ |
| 64 | enum { |
| 65 | RESERVE_FREE = 0, |
| 66 | RESERVE_ALLOC = 1, |
| 67 | RESERVE_ALLOC_NO_ACCOUNT = 2, |
| 68 | }; |
| 69 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 70 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 71 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 72 | u64 bytenr, u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 73 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 74 | struct btrfs_root *root, |
| 75 | u64 bytenr, u64 num_bytes, u64 parent, |
| 76 | u64 root_objectid, u64 owner_objectid, |
| 77 | u64 owner_offset, int refs_to_drop, |
| 78 | struct btrfs_delayed_extent_op *extra_op); |
| 79 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 80 | struct extent_buffer *leaf, |
| 81 | struct btrfs_extent_item *ei); |
| 82 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 83 | struct btrfs_root *root, |
| 84 | u64 parent, u64 root_objectid, |
| 85 | u64 flags, u64 owner, u64 offset, |
| 86 | struct btrfs_key *ins, int ref_mod); |
| 87 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 88 | struct btrfs_root *root, |
| 89 | u64 parent, u64 root_objectid, |
| 90 | u64 flags, struct btrfs_disk_key *key, |
| 91 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 92 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 93 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 94 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 95 | static int find_next_key(struct btrfs_path *path, int level, |
| 96 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 97 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 98 | int dump_block_groups); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 99 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 100 | u64 num_bytes, int reserve); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 101 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 102 | static noinline int |
| 103 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 104 | { |
| 105 | smp_mb(); |
| 106 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 107 | } |
| 108 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 109 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 110 | { |
| 111 | return (cache->flags & bits) == bits; |
| 112 | } |
| 113 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 114 | static void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 115 | { |
| 116 | atomic_inc(&cache->count); |
| 117 | } |
| 118 | |
| 119 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 120 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 121 | if (atomic_dec_and_test(&cache->count)) { |
| 122 | WARN_ON(cache->pinned > 0); |
| 123 | WARN_ON(cache->reserved > 0); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 124 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 125 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 126 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 129 | /* |
| 130 | * this adds the block group to the fs_info rb tree for the block group |
| 131 | * cache |
| 132 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 133 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 134 | struct btrfs_block_group_cache *block_group) |
| 135 | { |
| 136 | struct rb_node **p; |
| 137 | struct rb_node *parent = NULL; |
| 138 | struct btrfs_block_group_cache *cache; |
| 139 | |
| 140 | spin_lock(&info->block_group_cache_lock); |
| 141 | p = &info->block_group_cache_tree.rb_node; |
| 142 | |
| 143 | while (*p) { |
| 144 | parent = *p; |
| 145 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 146 | cache_node); |
| 147 | if (block_group->key.objectid < cache->key.objectid) { |
| 148 | p = &(*p)->rb_left; |
| 149 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 150 | p = &(*p)->rb_right; |
| 151 | } else { |
| 152 | spin_unlock(&info->block_group_cache_lock); |
| 153 | return -EEXIST; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | rb_link_node(&block_group->cache_node, parent, p); |
| 158 | rb_insert_color(&block_group->cache_node, |
| 159 | &info->block_group_cache_tree); |
| 160 | spin_unlock(&info->block_group_cache_lock); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * This will return the block group at or after bytenr if contains is 0, else |
| 167 | * it will return the block group that contains the bytenr |
| 168 | */ |
| 169 | static struct btrfs_block_group_cache * |
| 170 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 171 | int contains) |
| 172 | { |
| 173 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 174 | struct rb_node *n; |
| 175 | u64 end, start; |
| 176 | |
| 177 | spin_lock(&info->block_group_cache_lock); |
| 178 | n = info->block_group_cache_tree.rb_node; |
| 179 | |
| 180 | while (n) { |
| 181 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 182 | cache_node); |
| 183 | end = cache->key.objectid + cache->key.offset - 1; |
| 184 | start = cache->key.objectid; |
| 185 | |
| 186 | if (bytenr < start) { |
| 187 | if (!contains && (!ret || start < ret->key.objectid)) |
| 188 | ret = cache; |
| 189 | n = n->rb_left; |
| 190 | } else if (bytenr > start) { |
| 191 | if (contains && bytenr <= end) { |
| 192 | ret = cache; |
| 193 | break; |
| 194 | } |
| 195 | n = n->rb_right; |
| 196 | } else { |
| 197 | ret = cache; |
| 198 | break; |
| 199 | } |
| 200 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 201 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 202 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 203 | spin_unlock(&info->block_group_cache_lock); |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 208 | static int add_excluded_extent(struct btrfs_root *root, |
| 209 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 210 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 211 | u64 end = start + num_bytes - 1; |
| 212 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 213 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 214 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 215 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 216 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 219 | static void free_excluded_extents(struct btrfs_root *root, |
| 220 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 221 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 222 | u64 start, end; |
| 223 | |
| 224 | start = cache->key.objectid; |
| 225 | end = start + cache->key.offset - 1; |
| 226 | |
| 227 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 228 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 229 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 230 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 231 | } |
| 232 | |
| 233 | static int exclude_super_stripes(struct btrfs_root *root, |
| 234 | struct btrfs_block_group_cache *cache) |
| 235 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 236 | u64 bytenr; |
| 237 | u64 *logical; |
| 238 | int stripe_len; |
| 239 | int i, nr, ret; |
| 240 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 241 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 242 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 243 | cache->bytes_super += stripe_len; |
| 244 | ret = add_excluded_extent(root, cache->key.objectid, |
| 245 | stripe_len); |
| 246 | BUG_ON(ret); |
| 247 | } |
| 248 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 249 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 250 | bytenr = btrfs_sb_offset(i); |
| 251 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 252 | cache->key.objectid, bytenr, |
| 253 | 0, &logical, &nr, &stripe_len); |
| 254 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 255 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 256 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 257 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 258 | ret = add_excluded_extent(root, logical[nr], |
| 259 | stripe_len); |
| 260 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 261 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 262 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 263 | kfree(logical); |
| 264 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 268 | static struct btrfs_caching_control * |
| 269 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 270 | { |
| 271 | struct btrfs_caching_control *ctl; |
| 272 | |
| 273 | spin_lock(&cache->lock); |
| 274 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 275 | spin_unlock(&cache->lock); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 279 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 280 | if (!cache->caching_ctl) { |
| 281 | spin_unlock(&cache->lock); |
| 282 | return NULL; |
| 283 | } |
| 284 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 285 | ctl = cache->caching_ctl; |
| 286 | atomic_inc(&ctl->count); |
| 287 | spin_unlock(&cache->lock); |
| 288 | return ctl; |
| 289 | } |
| 290 | |
| 291 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 292 | { |
| 293 | if (atomic_dec_and_test(&ctl->count)) |
| 294 | kfree(ctl); |
| 295 | } |
| 296 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 297 | /* |
| 298 | * this is only called by cache_block_group, since we could have freed extents |
| 299 | * we need to check the pinned_extents for any extents that can't be used yet |
| 300 | * since their free space will be released as soon as the transaction commits. |
| 301 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 302 | 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] | 303 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 304 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 305 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 306 | int ret; |
| 307 | |
| 308 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 309 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 310 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 311 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 312 | if (ret) |
| 313 | break; |
| 314 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 315 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 316 | start = extent_end + 1; |
| 317 | } else if (extent_start > start && extent_start < end) { |
| 318 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 319 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 320 | ret = btrfs_add_free_space(block_group, start, |
| 321 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 322 | BUG_ON(ret); |
| 323 | start = extent_end + 1; |
| 324 | } else { |
| 325 | break; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | if (start < end) { |
| 330 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 331 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 332 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 333 | BUG_ON(ret); |
| 334 | } |
| 335 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 336 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 337 | } |
| 338 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 339 | static noinline void caching_thread(struct btrfs_work *work) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 340 | { |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 341 | struct btrfs_block_group_cache *block_group; |
| 342 | struct btrfs_fs_info *fs_info; |
| 343 | struct btrfs_caching_control *caching_ctl; |
| 344 | struct btrfs_root *extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 345 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 346 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 347 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 348 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 349 | u64 last = 0; |
| 350 | u32 nritems; |
| 351 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 352 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 353 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 354 | block_group = caching_ctl->block_group; |
| 355 | fs_info = block_group->fs_info; |
| 356 | extent_root = fs_info->extent_root; |
| 357 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 358 | path = btrfs_alloc_path(); |
| 359 | if (!path) |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 360 | goto out; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 361 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 362 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 363 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 364 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 365 | * We don't want to deadlock with somebody trying to allocate a new |
| 366 | * extent for the extent root while also trying to search the extent |
| 367 | * root to add free space. So we skip locking and search the commit |
| 368 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 369 | */ |
| 370 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 371 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 372 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 373 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 374 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 375 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 376 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 377 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 378 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 379 | /* need to make sure the commit_root doesn't disappear */ |
| 380 | down_read(&fs_info->extent_commit_sem); |
| 381 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 382 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 383 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 384 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 385 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 386 | leaf = path->nodes[0]; |
| 387 | nritems = btrfs_header_nritems(leaf); |
| 388 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 389 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 390 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 391 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 392 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -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 (path->slots[0] < nritems) { |
| 396 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 397 | } else { |
| 398 | ret = find_next_key(path, 0, &key); |
| 399 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 400 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 401 | |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 402 | if (need_resched() || |
| 403 | btrfs_next_leaf(extent_root, path)) { |
| 404 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 405 | btrfs_release_path(path); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 406 | up_read(&fs_info->extent_commit_sem); |
| 407 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 408 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 409 | goto again; |
| 410 | } |
| 411 | leaf = path->nodes[0]; |
| 412 | nritems = btrfs_header_nritems(leaf); |
| 413 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 414 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 415 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 416 | if (key.objectid < block_group->key.objectid) { |
| 417 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 418 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 419 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 420 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 421 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 422 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 423 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 424 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 425 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 426 | total_found += add_new_free_space(block_group, |
| 427 | fs_info, last, |
| 428 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 429 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 430 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 431 | if (total_found > (1024 * 1024 * 2)) { |
| 432 | total_found = 0; |
| 433 | wake_up(&caching_ctl->wait); |
| 434 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 435 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 436 | path->slots[0]++; |
| 437 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 438 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 439 | |
| 440 | total_found += add_new_free_space(block_group, fs_info, last, |
| 441 | block_group->key.objectid + |
| 442 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 443 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 444 | |
| 445 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 446 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 447 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 448 | spin_unlock(&block_group->lock); |
| 449 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 450 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 451 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 452 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 453 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 454 | free_excluded_extents(extent_root, block_group); |
| 455 | |
| 456 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 457 | out: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 458 | wake_up(&caching_ctl->wait); |
| 459 | |
| 460 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 461 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 462 | } |
| 463 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 464 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
| 465 | struct btrfs_trans_handle *trans, |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 466 | struct btrfs_root *root, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 467 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 468 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 469 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 470 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 471 | int ret = 0; |
| 472 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 473 | smp_mb(); |
| 474 | if (cache->cached != BTRFS_CACHE_NO) |
| 475 | return 0; |
| 476 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 477 | /* |
| 478 | * 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] | 479 | * to have the normal tree locking. Also if we are currently trying to |
| 480 | * allocate blocks for the tree root we can't do the fast caching since |
| 481 | * we likely hold important locks. |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 482 | */ |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 483 | if (trans && (!trans->transaction->in_commit) && |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 484 | (root && root != root->fs_info->tree_root)) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 485 | spin_lock(&cache->lock); |
| 486 | if (cache->cached != BTRFS_CACHE_NO) { |
| 487 | spin_unlock(&cache->lock); |
| 488 | return 0; |
| 489 | } |
| 490 | cache->cached = BTRFS_CACHE_STARTED; |
| 491 | spin_unlock(&cache->lock); |
| 492 | |
| 493 | ret = load_free_space_cache(fs_info, cache); |
| 494 | |
| 495 | spin_lock(&cache->lock); |
| 496 | if (ret == 1) { |
| 497 | cache->cached = BTRFS_CACHE_FINISHED; |
| 498 | cache->last_byte_to_unpin = (u64)-1; |
| 499 | } else { |
| 500 | cache->cached = BTRFS_CACHE_NO; |
| 501 | } |
| 502 | spin_unlock(&cache->lock); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 503 | if (ret == 1) { |
| 504 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 505 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 506 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | if (load_cache_only) |
| 510 | return 0; |
| 511 | |
Miao Xie | fc0e4a3 | 2011-03-24 11:41:21 +0000 | [diff] [blame] | 512 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 513 | BUG_ON(!caching_ctl); |
| 514 | |
| 515 | INIT_LIST_HEAD(&caching_ctl->list); |
| 516 | mutex_init(&caching_ctl->mutex); |
| 517 | init_waitqueue_head(&caching_ctl->wait); |
| 518 | caching_ctl->block_group = cache; |
| 519 | caching_ctl->progress = cache->key.objectid; |
| 520 | /* one for caching kthread, one for caching block group list */ |
| 521 | atomic_set(&caching_ctl->count, 2); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 522 | caching_ctl->work.func = caching_thread; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 523 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 524 | spin_lock(&cache->lock); |
| 525 | if (cache->cached != BTRFS_CACHE_NO) { |
| 526 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 527 | kfree(caching_ctl); |
| 528 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 529 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 530 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 531 | cache->cached = BTRFS_CACHE_STARTED; |
| 532 | spin_unlock(&cache->lock); |
| 533 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 534 | down_write(&fs_info->extent_commit_sem); |
| 535 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 536 | up_write(&fs_info->extent_commit_sem); |
| 537 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 538 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 539 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 540 | btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 541 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 542 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 543 | } |
| 544 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 545 | /* |
| 546 | * return the block group that starts at or after bytenr |
| 547 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 548 | static struct btrfs_block_group_cache * |
| 549 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 550 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 551 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 552 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 553 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 554 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 555 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 558 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 559 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 560 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 561 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 562 | struct btrfs_fs_info *info, |
| 563 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 564 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 565 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 566 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 567 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 568 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 569 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 570 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 571 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 572 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 573 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 574 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 575 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 576 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 577 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 578 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 579 | BTRFS_BLOCK_GROUP_METADATA; |
| 580 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 581 | rcu_read_lock(); |
| 582 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 583 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 584 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 585 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 586 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 587 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 588 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 589 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 590 | } |
| 591 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 592 | /* |
| 593 | * after adding space to the filesystem, we need to clear the full flags |
| 594 | * on all the space infos. |
| 595 | */ |
| 596 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 597 | { |
| 598 | struct list_head *head = &info->space_info; |
| 599 | struct btrfs_space_info *found; |
| 600 | |
| 601 | rcu_read_lock(); |
| 602 | list_for_each_entry_rcu(found, head, list) |
| 603 | found->full = 0; |
| 604 | rcu_read_unlock(); |
| 605 | } |
| 606 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 607 | static u64 div_factor(u64 num, int factor) |
| 608 | { |
| 609 | if (factor == 10) |
| 610 | return num; |
| 611 | num *= factor; |
| 612 | do_div(num, 10); |
| 613 | return num; |
| 614 | } |
| 615 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 616 | static u64 div_factor_fine(u64 num, int factor) |
| 617 | { |
| 618 | if (factor == 100) |
| 619 | return num; |
| 620 | num *= factor; |
| 621 | do_div(num, 100); |
| 622 | return num; |
| 623 | } |
| 624 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 625 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 626 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 627 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 628 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 629 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 630 | u64 last = max(search_hint, search_start); |
| 631 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 632 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 633 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 634 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 635 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 636 | while (1) { |
| 637 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 638 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 639 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 640 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 641 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 642 | last = cache->key.objectid + cache->key.offset; |
| 643 | used = btrfs_block_group_used(&cache->item); |
| 644 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 645 | if ((full_search || !cache->ro) && |
| 646 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 647 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 648 | div_factor(cache->key.offset, factor)) { |
| 649 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 650 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 651 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 652 | goto found; |
| 653 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 654 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 655 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 656 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 657 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 658 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 659 | if (!wrapped) { |
| 660 | last = search_start; |
| 661 | wrapped = 1; |
| 662 | goto again; |
| 663 | } |
| 664 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 665 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 666 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 667 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 668 | goto again; |
| 669 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 670 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 671 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 672 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 673 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 674 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 675 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 676 | { |
| 677 | int ret; |
| 678 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 679 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 680 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 681 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 682 | if (!path) |
| 683 | return -ENOMEM; |
| 684 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 685 | key.objectid = start; |
| 686 | key.offset = len; |
| 687 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 688 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 689 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 690 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 691 | return ret; |
| 692 | } |
| 693 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 694 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 695 | * helper function to lookup reference count and flags of extent. |
| 696 | * |
| 697 | * the head node for delayed ref is used to store the sum of all the |
| 698 | * reference count modifications queued up in the rbtree. the head |
| 699 | * node may also store the extent flags to set. This way you can check |
| 700 | * to see what the reference count and extent flags would be if all of |
| 701 | * the delayed refs are not processed. |
| 702 | */ |
| 703 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 704 | struct btrfs_root *root, u64 bytenr, |
| 705 | u64 num_bytes, u64 *refs, u64 *flags) |
| 706 | { |
| 707 | struct btrfs_delayed_ref_head *head; |
| 708 | struct btrfs_delayed_ref_root *delayed_refs; |
| 709 | struct btrfs_path *path; |
| 710 | struct btrfs_extent_item *ei; |
| 711 | struct extent_buffer *leaf; |
| 712 | struct btrfs_key key; |
| 713 | u32 item_size; |
| 714 | u64 num_refs; |
| 715 | u64 extent_flags; |
| 716 | int ret; |
| 717 | |
| 718 | path = btrfs_alloc_path(); |
| 719 | if (!path) |
| 720 | return -ENOMEM; |
| 721 | |
| 722 | key.objectid = bytenr; |
| 723 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 724 | key.offset = num_bytes; |
| 725 | if (!trans) { |
| 726 | path->skip_locking = 1; |
| 727 | path->search_commit_root = 1; |
| 728 | } |
| 729 | again: |
| 730 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 731 | &key, path, 0, 0); |
| 732 | if (ret < 0) |
| 733 | goto out_free; |
| 734 | |
| 735 | if (ret == 0) { |
| 736 | leaf = path->nodes[0]; |
| 737 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 738 | if (item_size >= sizeof(*ei)) { |
| 739 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 740 | struct btrfs_extent_item); |
| 741 | num_refs = btrfs_extent_refs(leaf, ei); |
| 742 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 743 | } else { |
| 744 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 745 | struct btrfs_extent_item_v0 *ei0; |
| 746 | BUG_ON(item_size != sizeof(*ei0)); |
| 747 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 748 | struct btrfs_extent_item_v0); |
| 749 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 750 | /* FIXME: this isn't correct for data */ |
| 751 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 752 | #else |
| 753 | BUG(); |
| 754 | #endif |
| 755 | } |
| 756 | BUG_ON(num_refs == 0); |
| 757 | } else { |
| 758 | num_refs = 0; |
| 759 | extent_flags = 0; |
| 760 | ret = 0; |
| 761 | } |
| 762 | |
| 763 | if (!trans) |
| 764 | goto out; |
| 765 | |
| 766 | delayed_refs = &trans->transaction->delayed_refs; |
| 767 | spin_lock(&delayed_refs->lock); |
| 768 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 769 | if (head) { |
| 770 | if (!mutex_trylock(&head->mutex)) { |
| 771 | atomic_inc(&head->node.refs); |
| 772 | spin_unlock(&delayed_refs->lock); |
| 773 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 774 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 775 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 776 | /* |
| 777 | * Mutex was contended, block until it's released and try |
| 778 | * again |
| 779 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 780 | mutex_lock(&head->mutex); |
| 781 | mutex_unlock(&head->mutex); |
| 782 | btrfs_put_delayed_ref(&head->node); |
| 783 | goto again; |
| 784 | } |
| 785 | if (head->extent_op && head->extent_op->update_flags) |
| 786 | extent_flags |= head->extent_op->flags_to_set; |
| 787 | else |
| 788 | BUG_ON(num_refs == 0); |
| 789 | |
| 790 | num_refs += head->node.ref_mod; |
| 791 | mutex_unlock(&head->mutex); |
| 792 | } |
| 793 | spin_unlock(&delayed_refs->lock); |
| 794 | out: |
| 795 | WARN_ON(num_refs == 0); |
| 796 | if (refs) |
| 797 | *refs = num_refs; |
| 798 | if (flags) |
| 799 | *flags = extent_flags; |
| 800 | out_free: |
| 801 | btrfs_free_path(path); |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 806 | * Back reference rules. Back refs have three main goals: |
| 807 | * |
| 808 | * 1) differentiate between all holders of references to an extent so that |
| 809 | * when a reference is dropped we can make sure it was a valid reference |
| 810 | * before freeing the extent. |
| 811 | * |
| 812 | * 2) Provide enough information to quickly find the holders of an extent |
| 813 | * if we notice a given block is corrupted or bad. |
| 814 | * |
| 815 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 816 | * maintenance. This is actually the same as #2, but with a slightly |
| 817 | * different use case. |
| 818 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 819 | * There are two kinds of back refs. The implicit back refs is optimized |
| 820 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 821 | * back refs of this kind provide information about the block's owner tree |
| 822 | * and the pointer's key. These information allow us to find the block by |
| 823 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 824 | * referenced by their owner trees. The location of tree block is recorded |
| 825 | * in the back refs. Actually the full back refs is generic, and can be |
| 826 | * used in all cases the implicit back refs is used. The major shortcoming |
| 827 | * of the full back refs is its overhead. Every time a tree block gets |
| 828 | * COWed, we have to update back refs entry for all pointers in it. |
| 829 | * |
| 830 | * For a newly allocated tree block, we use implicit back refs for |
| 831 | * pointers in it. This means most tree related operations only involve |
| 832 | * implicit back refs. For a tree block created in old transaction, the |
| 833 | * only way to drop a reference to it is COW it. So we can detect the |
| 834 | * event that tree block loses its owner tree's reference and do the |
| 835 | * back refs conversion. |
| 836 | * |
| 837 | * When a tree block is COW'd through a tree, there are four cases: |
| 838 | * |
| 839 | * The reference count of the block is one and the tree is the block's |
| 840 | * owner tree. Nothing to do in this case. |
| 841 | * |
| 842 | * The reference count of the block is one and the tree is not the |
| 843 | * block's owner tree. In this case, full back refs is used for pointers |
| 844 | * in the block. Remove these full back refs, add implicit back refs for |
| 845 | * every pointers in the new block. |
| 846 | * |
| 847 | * The reference count of the block is greater than one and the tree is |
| 848 | * the block's owner tree. In this case, implicit back refs is used for |
| 849 | * pointers in the block. Add full back refs for every pointers in the |
| 850 | * block, increase lower level extents' reference counts. The original |
| 851 | * implicit back refs are entailed to the new block. |
| 852 | * |
| 853 | * The reference count of the block is greater than one and the tree is |
| 854 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 855 | * the new block, increase lower level extents' reference count. |
| 856 | * |
| 857 | * Back Reference Key composing: |
| 858 | * |
| 859 | * The key objectid corresponds to the first byte in the extent, |
| 860 | * The key type is used to differentiate between types of back refs. |
| 861 | * There are different meanings of the key offset for different types |
| 862 | * of back refs. |
| 863 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 864 | * File extents can be referenced by: |
| 865 | * |
| 866 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 867 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 868 | * - different offsets inside a file (bookend extents in file.c) |
| 869 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 870 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 871 | * |
| 872 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 873 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 874 | * - original offset in the file |
| 875 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 876 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 877 | * The key offset for the implicit back refs is hash of the first |
| 878 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 879 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 880 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 881 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 882 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 883 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 884 | * The key offset for the implicit back refs is the first byte of |
| 885 | * the tree leaf |
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 | * When a file extent is allocated, The implicit back refs is used. |
| 888 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 889 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 890 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 891 | * |
| 892 | * When a file extent is removed file truncation, we find the |
| 893 | * corresponding implicit back refs and check the following fields: |
| 894 | * |
| 895 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 896 | * |
| 897 | * Btree extents can be referenced by: |
| 898 | * |
| 899 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 900 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 901 | * Both the implicit back refs and the full back refs for tree blocks |
| 902 | * only consist of key. The key offset for the implicit back refs is |
| 903 | * objectid of block's owner tree. The key offset for the full back refs |
| 904 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 905 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 906 | * When implicit back refs is used, information about the lowest key and |
| 907 | * level of the tree block are required. These information are stored in |
| 908 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 909 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 910 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 911 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 912 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 913 | struct btrfs_root *root, |
| 914 | struct btrfs_path *path, |
| 915 | u64 owner, u32 extra_size) |
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 | struct btrfs_extent_item *item; |
| 918 | struct btrfs_extent_item_v0 *ei0; |
| 919 | struct btrfs_extent_ref_v0 *ref0; |
| 920 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 921 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 922 | struct btrfs_key key; |
| 923 | struct btrfs_key found_key; |
| 924 | u32 new_size = sizeof(*item); |
| 925 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 926 | int ret; |
| 927 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 928 | leaf = path->nodes[0]; |
| 929 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 930 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 931 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 932 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 933 | struct btrfs_extent_item_v0); |
| 934 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 935 | |
| 936 | if (owner == (u64)-1) { |
| 937 | while (1) { |
| 938 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 939 | ret = btrfs_next_leaf(root, path); |
| 940 | if (ret < 0) |
| 941 | return ret; |
| 942 | BUG_ON(ret > 0); |
| 943 | leaf = path->nodes[0]; |
| 944 | } |
| 945 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 946 | path->slots[0]); |
| 947 | BUG_ON(key.objectid != found_key.objectid); |
| 948 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 949 | path->slots[0]++; |
| 950 | continue; |
| 951 | } |
| 952 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 953 | struct btrfs_extent_ref_v0); |
| 954 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 955 | break; |
| 956 | } |
| 957 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 958 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 959 | |
| 960 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 961 | new_size += sizeof(*bi); |
| 962 | |
| 963 | new_size -= sizeof(*ei0); |
| 964 | ret = btrfs_search_slot(trans, root, &key, path, |
| 965 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 966 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 967 | return ret; |
| 968 | BUG_ON(ret); |
| 969 | |
| 970 | ret = btrfs_extend_item(trans, root, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 971 | |
| 972 | leaf = path->nodes[0]; |
| 973 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 974 | btrfs_set_extent_refs(leaf, item, refs); |
| 975 | /* FIXME: get real generation */ |
| 976 | btrfs_set_extent_generation(leaf, item, 0); |
| 977 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 978 | btrfs_set_extent_flags(leaf, item, |
| 979 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 980 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 981 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 982 | /* FIXME: get first key of the block */ |
| 983 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 984 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 985 | } else { |
| 986 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 987 | } |
| 988 | btrfs_mark_buffer_dirty(leaf); |
| 989 | return 0; |
| 990 | } |
| 991 | #endif |
| 992 | |
| 993 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 994 | { |
| 995 | u32 high_crc = ~(u32)0; |
| 996 | u32 low_crc = ~(u32)0; |
| 997 | __le64 lenum; |
| 998 | |
| 999 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1000 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1001 | lenum = cpu_to_le64(owner); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1002 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1003 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1004 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1005 | |
| 1006 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1007 | } |
| 1008 | |
| 1009 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1010 | struct btrfs_extent_data_ref *ref) |
| 1011 | { |
| 1012 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1013 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1014 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1015 | } |
| 1016 | |
| 1017 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1018 | struct btrfs_extent_data_ref *ref, |
| 1019 | u64 root_objectid, u64 owner, u64 offset) |
| 1020 | { |
| 1021 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1022 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1023 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1024 | return 0; |
| 1025 | return 1; |
| 1026 | } |
| 1027 | |
| 1028 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1029 | struct btrfs_root *root, |
| 1030 | struct btrfs_path *path, |
| 1031 | u64 bytenr, u64 parent, |
| 1032 | u64 root_objectid, |
| 1033 | u64 owner, u64 offset) |
| 1034 | { |
| 1035 | struct btrfs_key key; |
| 1036 | struct btrfs_extent_data_ref *ref; |
| 1037 | struct extent_buffer *leaf; |
| 1038 | u32 nritems; |
| 1039 | int ret; |
| 1040 | int recow; |
| 1041 | int err = -ENOENT; |
| 1042 | |
| 1043 | key.objectid = bytenr; |
| 1044 | if (parent) { |
| 1045 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1046 | key.offset = parent; |
| 1047 | } else { |
| 1048 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1049 | key.offset = hash_extent_data_ref(root_objectid, |
| 1050 | owner, offset); |
| 1051 | } |
| 1052 | again: |
| 1053 | recow = 0; |
| 1054 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1055 | if (ret < 0) { |
| 1056 | err = ret; |
| 1057 | goto fail; |
| 1058 | } |
| 1059 | |
| 1060 | if (parent) { |
| 1061 | if (!ret) |
| 1062 | return 0; |
| 1063 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1064 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1065 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1066 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1067 | if (ret < 0) { |
| 1068 | err = ret; |
| 1069 | goto fail; |
| 1070 | } |
| 1071 | if (!ret) |
| 1072 | return 0; |
| 1073 | #endif |
| 1074 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1078 | nritems = btrfs_header_nritems(leaf); |
| 1079 | while (1) { |
| 1080 | if (path->slots[0] >= nritems) { |
| 1081 | ret = btrfs_next_leaf(root, path); |
| 1082 | if (ret < 0) |
| 1083 | err = ret; |
| 1084 | if (ret) |
| 1085 | goto fail; |
| 1086 | |
| 1087 | leaf = path->nodes[0]; |
| 1088 | nritems = btrfs_header_nritems(leaf); |
| 1089 | recow = 1; |
| 1090 | } |
| 1091 | |
| 1092 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1093 | if (key.objectid != bytenr || |
| 1094 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1095 | goto fail; |
| 1096 | |
| 1097 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1098 | struct btrfs_extent_data_ref); |
| 1099 | |
| 1100 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1101 | owner, offset)) { |
| 1102 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1103 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1104 | goto again; |
| 1105 | } |
| 1106 | err = 0; |
| 1107 | break; |
| 1108 | } |
| 1109 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1110 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1111 | fail: |
| 1112 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1113 | } |
| 1114 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1115 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1116 | struct btrfs_root *root, |
| 1117 | struct btrfs_path *path, |
| 1118 | u64 bytenr, u64 parent, |
| 1119 | u64 root_objectid, u64 owner, |
| 1120 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1121 | { |
| 1122 | struct btrfs_key key; |
| 1123 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1124 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1125 | u32 num_refs; |
| 1126 | int ret; |
| 1127 | |
| 1128 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1129 | if (parent) { |
| 1130 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1131 | key.offset = parent; |
| 1132 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1133 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1134 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1135 | key.offset = hash_extent_data_ref(root_objectid, |
| 1136 | owner, offset); |
| 1137 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1138 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1139 | |
| 1140 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1141 | if (ret && ret != -EEXIST) |
| 1142 | goto fail; |
| 1143 | |
| 1144 | leaf = path->nodes[0]; |
| 1145 | if (parent) { |
| 1146 | struct btrfs_shared_data_ref *ref; |
| 1147 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1148 | struct btrfs_shared_data_ref); |
| 1149 | if (ret == 0) { |
| 1150 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1151 | } else { |
| 1152 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1153 | num_refs += refs_to_add; |
| 1154 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1155 | } |
| 1156 | } else { |
| 1157 | struct btrfs_extent_data_ref *ref; |
| 1158 | while (ret == -EEXIST) { |
| 1159 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1160 | struct btrfs_extent_data_ref); |
| 1161 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1162 | owner, offset)) |
| 1163 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1164 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1165 | key.offset++; |
| 1166 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1167 | size); |
| 1168 | if (ret && ret != -EEXIST) |
| 1169 | goto fail; |
| 1170 | |
| 1171 | leaf = path->nodes[0]; |
| 1172 | } |
| 1173 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1174 | struct btrfs_extent_data_ref); |
| 1175 | if (ret == 0) { |
| 1176 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1177 | root_objectid); |
| 1178 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1179 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1180 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1181 | } else { |
| 1182 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1183 | num_refs += refs_to_add; |
| 1184 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1185 | } |
| 1186 | } |
| 1187 | btrfs_mark_buffer_dirty(leaf); |
| 1188 | ret = 0; |
| 1189 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1190 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1191 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1192 | } |
| 1193 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1194 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1195 | struct btrfs_root *root, |
| 1196 | struct btrfs_path *path, |
| 1197 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1198 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1199 | struct btrfs_key key; |
| 1200 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1201 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1202 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1203 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1204 | int ret = 0; |
| 1205 | |
| 1206 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1207 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1208 | |
| 1209 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1210 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1211 | struct btrfs_extent_data_ref); |
| 1212 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1213 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1214 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1215 | struct btrfs_shared_data_ref); |
| 1216 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1217 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1218 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1219 | struct btrfs_extent_ref_v0 *ref0; |
| 1220 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1221 | struct btrfs_extent_ref_v0); |
| 1222 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1223 | #endif |
| 1224 | } else { |
| 1225 | BUG(); |
| 1226 | } |
| 1227 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1228 | BUG_ON(num_refs < refs_to_drop); |
| 1229 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1230 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1231 | if (num_refs == 0) { |
| 1232 | ret = btrfs_del_item(trans, root, path); |
| 1233 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1234 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1235 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1236 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1237 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1238 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1239 | else { |
| 1240 | struct btrfs_extent_ref_v0 *ref0; |
| 1241 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1242 | struct btrfs_extent_ref_v0); |
| 1243 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1244 | } |
| 1245 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1246 | btrfs_mark_buffer_dirty(leaf); |
| 1247 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1248 | return ret; |
| 1249 | } |
| 1250 | |
| 1251 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1252 | struct btrfs_path *path, |
| 1253 | struct btrfs_extent_inline_ref *iref) |
| 1254 | { |
| 1255 | struct btrfs_key key; |
| 1256 | struct extent_buffer *leaf; |
| 1257 | struct btrfs_extent_data_ref *ref1; |
| 1258 | struct btrfs_shared_data_ref *ref2; |
| 1259 | u32 num_refs = 0; |
| 1260 | |
| 1261 | leaf = path->nodes[0]; |
| 1262 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1263 | if (iref) { |
| 1264 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1265 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1266 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1267 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1268 | } else { |
| 1269 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1270 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1271 | } |
| 1272 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1273 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1274 | struct btrfs_extent_data_ref); |
| 1275 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1276 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1277 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1278 | struct btrfs_shared_data_ref); |
| 1279 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1280 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1281 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1282 | struct btrfs_extent_ref_v0 *ref0; |
| 1283 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1284 | struct btrfs_extent_ref_v0); |
| 1285 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1286 | #endif |
| 1287 | } else { |
| 1288 | WARN_ON(1); |
| 1289 | } |
| 1290 | return num_refs; |
| 1291 | } |
| 1292 | |
| 1293 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1294 | struct btrfs_root *root, |
| 1295 | struct btrfs_path *path, |
| 1296 | u64 bytenr, u64 parent, |
| 1297 | u64 root_objectid) |
| 1298 | { |
| 1299 | struct btrfs_key key; |
| 1300 | int ret; |
| 1301 | |
| 1302 | key.objectid = bytenr; |
| 1303 | if (parent) { |
| 1304 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1305 | key.offset = parent; |
| 1306 | } else { |
| 1307 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1308 | key.offset = root_objectid; |
| 1309 | } |
| 1310 | |
| 1311 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1312 | if (ret > 0) |
| 1313 | ret = -ENOENT; |
| 1314 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1315 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1316 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1317 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1318 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1319 | if (ret > 0) |
| 1320 | ret = -ENOENT; |
| 1321 | } |
| 1322 | #endif |
| 1323 | return ret; |
| 1324 | } |
| 1325 | |
| 1326 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1327 | struct btrfs_root *root, |
| 1328 | struct btrfs_path *path, |
| 1329 | u64 bytenr, u64 parent, |
| 1330 | u64 root_objectid) |
| 1331 | { |
| 1332 | struct btrfs_key key; |
| 1333 | int ret; |
| 1334 | |
| 1335 | key.objectid = bytenr; |
| 1336 | if (parent) { |
| 1337 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1338 | key.offset = parent; |
| 1339 | } else { |
| 1340 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1341 | key.offset = root_objectid; |
| 1342 | } |
| 1343 | |
| 1344 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1345 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1346 | return ret; |
| 1347 | } |
| 1348 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1349 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1350 | { |
| 1351 | int type; |
| 1352 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1353 | if (parent > 0) |
| 1354 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1355 | else |
| 1356 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1357 | } else { |
| 1358 | if (parent > 0) |
| 1359 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1360 | else |
| 1361 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1362 | } |
| 1363 | return type; |
| 1364 | } |
| 1365 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1366 | static int find_next_key(struct btrfs_path *path, int level, |
| 1367 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1368 | |
| 1369 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1370 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1371 | if (!path->nodes[level]) |
| 1372 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1373 | if (path->slots[level] + 1 >= |
| 1374 | btrfs_header_nritems(path->nodes[level])) |
| 1375 | continue; |
| 1376 | if (level == 0) |
| 1377 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1378 | path->slots[level] + 1); |
| 1379 | else |
| 1380 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1381 | path->slots[level] + 1); |
| 1382 | return 0; |
| 1383 | } |
| 1384 | return 1; |
| 1385 | } |
| 1386 | |
| 1387 | /* |
| 1388 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1389 | * to the address of inline back ref, and 0 is returned. |
| 1390 | * |
| 1391 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1392 | * should be inserted, and -ENOENT is returned. |
| 1393 | * |
| 1394 | * if insert is true and there are too many inline back refs, the path |
| 1395 | * points to the extent item, and -EAGAIN is returned. |
| 1396 | * |
| 1397 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1398 | * items in the tree are ordered. |
| 1399 | */ |
| 1400 | static noinline_for_stack |
| 1401 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1402 | struct btrfs_root *root, |
| 1403 | struct btrfs_path *path, |
| 1404 | struct btrfs_extent_inline_ref **ref_ret, |
| 1405 | u64 bytenr, u64 num_bytes, |
| 1406 | u64 parent, u64 root_objectid, |
| 1407 | u64 owner, u64 offset, int insert) |
| 1408 | { |
| 1409 | struct btrfs_key key; |
| 1410 | struct extent_buffer *leaf; |
| 1411 | struct btrfs_extent_item *ei; |
| 1412 | struct btrfs_extent_inline_ref *iref; |
| 1413 | u64 flags; |
| 1414 | u64 item_size; |
| 1415 | unsigned long ptr; |
| 1416 | unsigned long end; |
| 1417 | int extra_size; |
| 1418 | int type; |
| 1419 | int want; |
| 1420 | int ret; |
| 1421 | int err = 0; |
| 1422 | |
| 1423 | key.objectid = bytenr; |
| 1424 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1425 | key.offset = num_bytes; |
| 1426 | |
| 1427 | want = extent_ref_type(parent, owner); |
| 1428 | if (insert) { |
| 1429 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1430 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1431 | } else |
| 1432 | extra_size = -1; |
| 1433 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1434 | if (ret < 0) { |
| 1435 | err = ret; |
| 1436 | goto out; |
| 1437 | } |
| 1438 | BUG_ON(ret); |
| 1439 | |
| 1440 | leaf = path->nodes[0]; |
| 1441 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1442 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1443 | if (item_size < sizeof(*ei)) { |
| 1444 | if (!insert) { |
| 1445 | err = -ENOENT; |
| 1446 | goto out; |
| 1447 | } |
| 1448 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1449 | extra_size); |
| 1450 | if (ret < 0) { |
| 1451 | err = ret; |
| 1452 | goto out; |
| 1453 | } |
| 1454 | leaf = path->nodes[0]; |
| 1455 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1456 | } |
| 1457 | #endif |
| 1458 | BUG_ON(item_size < sizeof(*ei)); |
| 1459 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1460 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1461 | flags = btrfs_extent_flags(leaf, ei); |
| 1462 | |
| 1463 | ptr = (unsigned long)(ei + 1); |
| 1464 | end = (unsigned long)ei + item_size; |
| 1465 | |
| 1466 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1467 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1468 | BUG_ON(ptr > end); |
| 1469 | } else { |
| 1470 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1471 | } |
| 1472 | |
| 1473 | err = -ENOENT; |
| 1474 | while (1) { |
| 1475 | if (ptr >= end) { |
| 1476 | WARN_ON(ptr > end); |
| 1477 | break; |
| 1478 | } |
| 1479 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1480 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1481 | if (want < type) |
| 1482 | break; |
| 1483 | if (want > type) { |
| 1484 | ptr += btrfs_extent_inline_ref_size(type); |
| 1485 | continue; |
| 1486 | } |
| 1487 | |
| 1488 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1489 | struct btrfs_extent_data_ref *dref; |
| 1490 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1491 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1492 | owner, offset)) { |
| 1493 | err = 0; |
| 1494 | break; |
| 1495 | } |
| 1496 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1497 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1498 | break; |
| 1499 | } else { |
| 1500 | u64 ref_offset; |
| 1501 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1502 | if (parent > 0) { |
| 1503 | if (parent == ref_offset) { |
| 1504 | err = 0; |
| 1505 | break; |
| 1506 | } |
| 1507 | if (ref_offset < parent) |
| 1508 | break; |
| 1509 | } else { |
| 1510 | if (root_objectid == ref_offset) { |
| 1511 | err = 0; |
| 1512 | break; |
| 1513 | } |
| 1514 | if (ref_offset < root_objectid) |
| 1515 | break; |
| 1516 | } |
| 1517 | } |
| 1518 | ptr += btrfs_extent_inline_ref_size(type); |
| 1519 | } |
| 1520 | if (err == -ENOENT && insert) { |
| 1521 | if (item_size + extra_size >= |
| 1522 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1523 | err = -EAGAIN; |
| 1524 | goto out; |
| 1525 | } |
| 1526 | /* |
| 1527 | * To add new inline back ref, we have to make sure |
| 1528 | * there is no corresponding back ref item. |
| 1529 | * For simplicity, we just do not add new inline back |
| 1530 | * ref if there is any kind of item for this block |
| 1531 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1532 | if (find_next_key(path, 0, &key) == 0 && |
| 1533 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1534 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1535 | err = -EAGAIN; |
| 1536 | goto out; |
| 1537 | } |
| 1538 | } |
| 1539 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1540 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1541 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1542 | path->keep_locks = 0; |
| 1543 | btrfs_unlock_up_safe(path, 1); |
| 1544 | } |
| 1545 | return err; |
| 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * helper to add new inline back ref |
| 1550 | */ |
| 1551 | static noinline_for_stack |
| 1552 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1553 | struct btrfs_root *root, |
| 1554 | struct btrfs_path *path, |
| 1555 | struct btrfs_extent_inline_ref *iref, |
| 1556 | u64 parent, u64 root_objectid, |
| 1557 | u64 owner, u64 offset, int refs_to_add, |
| 1558 | struct btrfs_delayed_extent_op *extent_op) |
| 1559 | { |
| 1560 | struct extent_buffer *leaf; |
| 1561 | struct btrfs_extent_item *ei; |
| 1562 | unsigned long ptr; |
| 1563 | unsigned long end; |
| 1564 | unsigned long item_offset; |
| 1565 | u64 refs; |
| 1566 | int size; |
| 1567 | int type; |
| 1568 | int ret; |
| 1569 | |
| 1570 | leaf = path->nodes[0]; |
| 1571 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1572 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1573 | |
| 1574 | type = extent_ref_type(parent, owner); |
| 1575 | size = btrfs_extent_inline_ref_size(type); |
| 1576 | |
| 1577 | ret = btrfs_extend_item(trans, root, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1578 | |
| 1579 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1580 | refs = btrfs_extent_refs(leaf, ei); |
| 1581 | refs += refs_to_add; |
| 1582 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1583 | if (extent_op) |
| 1584 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1585 | |
| 1586 | ptr = (unsigned long)ei + item_offset; |
| 1587 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1588 | if (ptr < end - size) |
| 1589 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1590 | end - size - ptr); |
| 1591 | |
| 1592 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1593 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1594 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1595 | struct btrfs_extent_data_ref *dref; |
| 1596 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1597 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1598 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1599 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1600 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1601 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1602 | struct btrfs_shared_data_ref *sref; |
| 1603 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1604 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1605 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1606 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1607 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1608 | } else { |
| 1609 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1610 | } |
| 1611 | btrfs_mark_buffer_dirty(leaf); |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
| 1615 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1616 | struct btrfs_root *root, |
| 1617 | struct btrfs_path *path, |
| 1618 | struct btrfs_extent_inline_ref **ref_ret, |
| 1619 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1620 | u64 root_objectid, u64 owner, u64 offset) |
| 1621 | { |
| 1622 | int ret; |
| 1623 | |
| 1624 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1625 | bytenr, num_bytes, parent, |
| 1626 | root_objectid, owner, offset, 0); |
| 1627 | if (ret != -ENOENT) |
| 1628 | return ret; |
| 1629 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1630 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1631 | *ref_ret = NULL; |
| 1632 | |
| 1633 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1634 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1635 | root_objectid); |
| 1636 | } else { |
| 1637 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1638 | root_objectid, owner, offset); |
| 1639 | } |
| 1640 | return ret; |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * helper to update/remove inline back ref |
| 1645 | */ |
| 1646 | static noinline_for_stack |
| 1647 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1648 | struct btrfs_root *root, |
| 1649 | struct btrfs_path *path, |
| 1650 | struct btrfs_extent_inline_ref *iref, |
| 1651 | int refs_to_mod, |
| 1652 | struct btrfs_delayed_extent_op *extent_op) |
| 1653 | { |
| 1654 | struct extent_buffer *leaf; |
| 1655 | struct btrfs_extent_item *ei; |
| 1656 | struct btrfs_extent_data_ref *dref = NULL; |
| 1657 | struct btrfs_shared_data_ref *sref = NULL; |
| 1658 | unsigned long ptr; |
| 1659 | unsigned long end; |
| 1660 | u32 item_size; |
| 1661 | int size; |
| 1662 | int type; |
| 1663 | int ret; |
| 1664 | u64 refs; |
| 1665 | |
| 1666 | leaf = path->nodes[0]; |
| 1667 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1668 | refs = btrfs_extent_refs(leaf, ei); |
| 1669 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1670 | refs += refs_to_mod; |
| 1671 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1672 | if (extent_op) |
| 1673 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1674 | |
| 1675 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1676 | |
| 1677 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1678 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1679 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1680 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1681 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1682 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1683 | } else { |
| 1684 | refs = 1; |
| 1685 | BUG_ON(refs_to_mod != -1); |
| 1686 | } |
| 1687 | |
| 1688 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1689 | refs += refs_to_mod; |
| 1690 | |
| 1691 | if (refs > 0) { |
| 1692 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1693 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1694 | else |
| 1695 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1696 | } else { |
| 1697 | size = btrfs_extent_inline_ref_size(type); |
| 1698 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1699 | ptr = (unsigned long)iref; |
| 1700 | end = (unsigned long)ei + item_size; |
| 1701 | if (ptr + size < end) |
| 1702 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1703 | end - ptr - size); |
| 1704 | item_size -= size; |
| 1705 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1706 | } |
| 1707 | btrfs_mark_buffer_dirty(leaf); |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static noinline_for_stack |
| 1712 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1713 | struct btrfs_root *root, |
| 1714 | struct btrfs_path *path, |
| 1715 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1716 | u64 root_objectid, u64 owner, |
| 1717 | u64 offset, int refs_to_add, |
| 1718 | struct btrfs_delayed_extent_op *extent_op) |
| 1719 | { |
| 1720 | struct btrfs_extent_inline_ref *iref; |
| 1721 | int ret; |
| 1722 | |
| 1723 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1724 | bytenr, num_bytes, parent, |
| 1725 | root_objectid, owner, offset, 1); |
| 1726 | if (ret == 0) { |
| 1727 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1728 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1729 | refs_to_add, extent_op); |
| 1730 | } else if (ret == -ENOENT) { |
| 1731 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1732 | parent, root_objectid, |
| 1733 | owner, offset, refs_to_add, |
| 1734 | extent_op); |
| 1735 | } |
| 1736 | return ret; |
| 1737 | } |
| 1738 | |
| 1739 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1740 | struct btrfs_root *root, |
| 1741 | struct btrfs_path *path, |
| 1742 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1743 | u64 owner, u64 offset, int refs_to_add) |
| 1744 | { |
| 1745 | int ret; |
| 1746 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1747 | BUG_ON(refs_to_add != 1); |
| 1748 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1749 | parent, root_objectid); |
| 1750 | } else { |
| 1751 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1752 | parent, root_objectid, |
| 1753 | owner, offset, refs_to_add); |
| 1754 | } |
| 1755 | return ret; |
| 1756 | } |
| 1757 | |
| 1758 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1759 | struct btrfs_root *root, |
| 1760 | struct btrfs_path *path, |
| 1761 | struct btrfs_extent_inline_ref *iref, |
| 1762 | int refs_to_drop, int is_data) |
| 1763 | { |
| 1764 | int ret; |
| 1765 | |
| 1766 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1767 | if (iref) { |
| 1768 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1769 | -refs_to_drop, NULL); |
| 1770 | } else if (is_data) { |
| 1771 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1772 | } else { |
| 1773 | ret = btrfs_del_item(trans, root, path); |
| 1774 | } |
| 1775 | return ret; |
| 1776 | } |
| 1777 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1778 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1779 | u64 start, u64 len) |
| 1780 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1781 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1782 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1783 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1784 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1785 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1786 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1787 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1788 | u64 discarded_bytes = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1789 | struct btrfs_multi_bio *multi = NULL; |
| 1790 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1791 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1792 | /* Tell the block device(s) that the sectors can be discarded */ |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1793 | ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, |
| 1794 | bytenr, &num_bytes, &multi, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1795 | if (!ret) { |
| 1796 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1797 | int i; |
| 1798 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1799 | |
| 1800 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1801 | if (!stripe->dev->can_discard) |
| 1802 | continue; |
| 1803 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1804 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1805 | stripe->physical, |
| 1806 | stripe->length); |
| 1807 | if (!ret) |
| 1808 | discarded_bytes += stripe->length; |
| 1809 | else if (ret != -EOPNOTSUPP) |
| 1810 | break; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1811 | |
| 1812 | /* |
| 1813 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1814 | * just ignore the return value so we don't screw up |
| 1815 | * people calling discard_extent. |
| 1816 | */ |
| 1817 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1818 | } |
| 1819 | kfree(multi); |
| 1820 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1821 | |
| 1822 | if (actual_bytes) |
| 1823 | *actual_bytes = discarded_bytes; |
| 1824 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1825 | |
| 1826 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1827 | } |
| 1828 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1829 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1830 | struct btrfs_root *root, |
| 1831 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1832 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1833 | { |
| 1834 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1835 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1836 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1837 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1838 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1839 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1840 | parent, root_objectid, (int)owner, |
| 1841 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1842 | } else { |
| 1843 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1844 | parent, root_objectid, owner, offset, |
| 1845 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1846 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1847 | return ret; |
| 1848 | } |
| 1849 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1850 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1851 | struct btrfs_root *root, |
| 1852 | u64 bytenr, u64 num_bytes, |
| 1853 | u64 parent, u64 root_objectid, |
| 1854 | u64 owner, u64 offset, int refs_to_add, |
| 1855 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1856 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1857 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1858 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1859 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1860 | u64 refs; |
| 1861 | int ret; |
| 1862 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1863 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1864 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1865 | if (!path) |
| 1866 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1867 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1868 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1869 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1870 | /* this will setup the path even if it fails to insert the back ref */ |
| 1871 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1872 | path, bytenr, num_bytes, parent, |
| 1873 | root_objectid, owner, offset, |
| 1874 | refs_to_add, extent_op); |
| 1875 | if (ret == 0) |
| 1876 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1877 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1878 | if (ret != -EAGAIN) { |
| 1879 | err = ret; |
| 1880 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1881 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1882 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1883 | leaf = path->nodes[0]; |
| 1884 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1885 | refs = btrfs_extent_refs(leaf, item); |
| 1886 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1887 | if (extent_op) |
| 1888 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1889 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1890 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1891 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1892 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1893 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1894 | path->leave_spinning = 1; |
| 1895 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1896 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1897 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1898 | path, bytenr, parent, root_objectid, |
| 1899 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1900 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1901 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1902 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1903 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1904 | } |
| 1905 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1906 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1907 | struct btrfs_root *root, |
| 1908 | struct btrfs_delayed_ref_node *node, |
| 1909 | struct btrfs_delayed_extent_op *extent_op, |
| 1910 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1911 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1912 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1913 | struct btrfs_delayed_data_ref *ref; |
| 1914 | struct btrfs_key ins; |
| 1915 | u64 parent = 0; |
| 1916 | u64 ref_root = 0; |
| 1917 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1918 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1919 | ins.objectid = node->bytenr; |
| 1920 | ins.offset = node->num_bytes; |
| 1921 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1922 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1923 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1924 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1925 | parent = ref->parent; |
| 1926 | else |
| 1927 | ref_root = ref->root; |
| 1928 | |
| 1929 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1930 | if (extent_op) { |
| 1931 | BUG_ON(extent_op->update_key); |
| 1932 | flags |= extent_op->flags_to_set; |
| 1933 | } |
| 1934 | ret = alloc_reserved_file_extent(trans, root, |
| 1935 | parent, ref_root, flags, |
| 1936 | ref->objectid, ref->offset, |
| 1937 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1938 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1939 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1940 | node->num_bytes, parent, |
| 1941 | ref_root, ref->objectid, |
| 1942 | ref->offset, node->ref_mod, |
| 1943 | extent_op); |
| 1944 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1945 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1946 | node->num_bytes, parent, |
| 1947 | ref_root, ref->objectid, |
| 1948 | ref->offset, node->ref_mod, |
| 1949 | extent_op); |
| 1950 | } else { |
| 1951 | BUG(); |
| 1952 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1953 | return ret; |
| 1954 | } |
| 1955 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1956 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1957 | struct extent_buffer *leaf, |
| 1958 | struct btrfs_extent_item *ei) |
| 1959 | { |
| 1960 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1961 | if (extent_op->update_flags) { |
| 1962 | flags |= extent_op->flags_to_set; |
| 1963 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1964 | } |
| 1965 | |
| 1966 | if (extent_op->update_key) { |
| 1967 | struct btrfs_tree_block_info *bi; |
| 1968 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1969 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1970 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1975 | struct btrfs_root *root, |
| 1976 | struct btrfs_delayed_ref_node *node, |
| 1977 | struct btrfs_delayed_extent_op *extent_op) |
| 1978 | { |
| 1979 | struct btrfs_key key; |
| 1980 | struct btrfs_path *path; |
| 1981 | struct btrfs_extent_item *ei; |
| 1982 | struct extent_buffer *leaf; |
| 1983 | u32 item_size; |
| 1984 | int ret; |
| 1985 | int err = 0; |
| 1986 | |
| 1987 | path = btrfs_alloc_path(); |
| 1988 | if (!path) |
| 1989 | return -ENOMEM; |
| 1990 | |
| 1991 | key.objectid = node->bytenr; |
| 1992 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1993 | key.offset = node->num_bytes; |
| 1994 | |
| 1995 | path->reada = 1; |
| 1996 | path->leave_spinning = 1; |
| 1997 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 1998 | path, 0, 1); |
| 1999 | if (ret < 0) { |
| 2000 | err = ret; |
| 2001 | goto out; |
| 2002 | } |
| 2003 | if (ret > 0) { |
| 2004 | err = -EIO; |
| 2005 | goto out; |
| 2006 | } |
| 2007 | |
| 2008 | leaf = path->nodes[0]; |
| 2009 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2010 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2011 | if (item_size < sizeof(*ei)) { |
| 2012 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 2013 | path, (u64)-1, 0); |
| 2014 | if (ret < 0) { |
| 2015 | err = ret; |
| 2016 | goto out; |
| 2017 | } |
| 2018 | leaf = path->nodes[0]; |
| 2019 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2020 | } |
| 2021 | #endif |
| 2022 | BUG_ON(item_size < sizeof(*ei)); |
| 2023 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2024 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2025 | |
| 2026 | btrfs_mark_buffer_dirty(leaf); |
| 2027 | out: |
| 2028 | btrfs_free_path(path); |
| 2029 | return err; |
| 2030 | } |
| 2031 | |
| 2032 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2033 | struct btrfs_root *root, |
| 2034 | struct btrfs_delayed_ref_node *node, |
| 2035 | struct btrfs_delayed_extent_op *extent_op, |
| 2036 | int insert_reserved) |
| 2037 | { |
| 2038 | int ret = 0; |
| 2039 | struct btrfs_delayed_tree_ref *ref; |
| 2040 | struct btrfs_key ins; |
| 2041 | u64 parent = 0; |
| 2042 | u64 ref_root = 0; |
| 2043 | |
| 2044 | ins.objectid = node->bytenr; |
| 2045 | ins.offset = node->num_bytes; |
| 2046 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2047 | |
| 2048 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 2049 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2050 | parent = ref->parent; |
| 2051 | else |
| 2052 | ref_root = ref->root; |
| 2053 | |
| 2054 | BUG_ON(node->ref_mod != 1); |
| 2055 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 2056 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 2057 | !extent_op->update_key); |
| 2058 | ret = alloc_reserved_tree_block(trans, root, |
| 2059 | parent, ref_root, |
| 2060 | extent_op->flags_to_set, |
| 2061 | &extent_op->key, |
| 2062 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2063 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2064 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2065 | node->num_bytes, parent, ref_root, |
| 2066 | ref->level, 0, 1, extent_op); |
| 2067 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2068 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2069 | node->num_bytes, parent, ref_root, |
| 2070 | ref->level, 0, 1, extent_op); |
| 2071 | } else { |
| 2072 | BUG(); |
| 2073 | } |
| 2074 | return ret; |
| 2075 | } |
| 2076 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2077 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2078 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2079 | struct btrfs_root *root, |
| 2080 | struct btrfs_delayed_ref_node *node, |
| 2081 | struct btrfs_delayed_extent_op *extent_op, |
| 2082 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2083 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2084 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2085 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2086 | struct btrfs_delayed_ref_head *head; |
| 2087 | /* |
| 2088 | * we've hit the end of the chain and we were supposed |
| 2089 | * to insert this extent into the tree. But, it got |
| 2090 | * deleted before we ever needed to insert it, so all |
| 2091 | * we have to do is clean up the accounting |
| 2092 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2093 | BUG_ON(extent_op); |
| 2094 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2095 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2096 | btrfs_pin_extent(root, node->bytenr, |
| 2097 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2098 | if (head->is_data) { |
| 2099 | ret = btrfs_del_csums(trans, root, |
| 2100 | node->bytenr, |
| 2101 | node->num_bytes); |
| 2102 | BUG_ON(ret); |
| 2103 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2104 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2105 | mutex_unlock(&head->mutex); |
| 2106 | return 0; |
| 2107 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2108 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2109 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2110 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2111 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2112 | insert_reserved); |
| 2113 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2114 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2115 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2116 | insert_reserved); |
| 2117 | else |
| 2118 | BUG(); |
| 2119 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2120 | } |
| 2121 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2122 | static noinline struct btrfs_delayed_ref_node * |
| 2123 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2124 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2125 | struct rb_node *node; |
| 2126 | struct btrfs_delayed_ref_node *ref; |
| 2127 | int action = BTRFS_ADD_DELAYED_REF; |
| 2128 | again: |
| 2129 | /* |
| 2130 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2131 | * this prevents ref count from going down to zero when |
| 2132 | * there still are pending delayed ref. |
| 2133 | */ |
| 2134 | node = rb_prev(&head->node.rb_node); |
| 2135 | while (1) { |
| 2136 | if (!node) |
| 2137 | break; |
| 2138 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2139 | rb_node); |
| 2140 | if (ref->bytenr != head->node.bytenr) |
| 2141 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2142 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2143 | return ref; |
| 2144 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2145 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2146 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2147 | action = BTRFS_DROP_DELAYED_REF; |
| 2148 | goto again; |
| 2149 | } |
| 2150 | return NULL; |
| 2151 | } |
| 2152 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2153 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2154 | struct btrfs_root *root, |
| 2155 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2156 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2157 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2158 | struct btrfs_delayed_ref_node *ref; |
| 2159 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2160 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2161 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2162 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2163 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2164 | |
| 2165 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2166 | while (1) { |
| 2167 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2168 | /* pick a new head ref from the cluster list */ |
| 2169 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2170 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2171 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2172 | locked_ref = list_entry(cluster->next, |
| 2173 | struct btrfs_delayed_ref_head, cluster); |
| 2174 | |
| 2175 | /* grab the lock that says we are going to process |
| 2176 | * all the refs for this head */ |
| 2177 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2178 | |
| 2179 | /* |
| 2180 | * we may have dropped the spin lock to get the head |
| 2181 | * mutex lock, and that might have given someone else |
| 2182 | * time to free the head. If that's true, it has been |
| 2183 | * removed from our list and we can move on. |
| 2184 | */ |
| 2185 | if (ret == -EAGAIN) { |
| 2186 | locked_ref = NULL; |
| 2187 | count++; |
| 2188 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | * record the must insert reserved flag before we |
| 2194 | * drop the spin lock. |
| 2195 | */ |
| 2196 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2197 | locked_ref->must_insert_reserved = 0; |
| 2198 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2199 | extent_op = locked_ref->extent_op; |
| 2200 | locked_ref->extent_op = NULL; |
| 2201 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2202 | /* |
| 2203 | * locked_ref is the head node, so we have to go one |
| 2204 | * node back for any delayed ref updates |
| 2205 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2206 | ref = select_delayed_ref(locked_ref); |
| 2207 | if (!ref) { |
| 2208 | /* All delayed refs have been processed, Go ahead |
| 2209 | * and send the head node to run_one_delayed_ref, |
| 2210 | * so that any accounting fixes can happen |
| 2211 | */ |
| 2212 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2213 | |
| 2214 | if (extent_op && must_insert_reserved) { |
| 2215 | kfree(extent_op); |
| 2216 | extent_op = NULL; |
| 2217 | } |
| 2218 | |
| 2219 | if (extent_op) { |
| 2220 | spin_unlock(&delayed_refs->lock); |
| 2221 | |
| 2222 | ret = run_delayed_extent_op(trans, root, |
| 2223 | ref, extent_op); |
| 2224 | BUG_ON(ret); |
| 2225 | kfree(extent_op); |
| 2226 | |
| 2227 | cond_resched(); |
| 2228 | spin_lock(&delayed_refs->lock); |
| 2229 | continue; |
| 2230 | } |
| 2231 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2232 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2233 | locked_ref = NULL; |
| 2234 | } |
| 2235 | |
| 2236 | ref->in_tree = 0; |
| 2237 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2238 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2239 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2240 | spin_unlock(&delayed_refs->lock); |
| 2241 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2242 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2243 | must_insert_reserved); |
| 2244 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2245 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2246 | btrfs_put_delayed_ref(ref); |
| 2247 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2248 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2249 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2250 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2251 | spin_lock(&delayed_refs->lock); |
| 2252 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2253 | return count; |
| 2254 | } |
| 2255 | |
| 2256 | /* |
| 2257 | * this starts processing the delayed reference count updates and |
| 2258 | * extent insertions we have queued up so far. count can be |
| 2259 | * 0, which means to process everything in the tree at the start |
| 2260 | * of the run (but not newly added entries), or it can be some target |
| 2261 | * number you'd like to process. |
| 2262 | */ |
| 2263 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2264 | struct btrfs_root *root, unsigned long count) |
| 2265 | { |
| 2266 | struct rb_node *node; |
| 2267 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2268 | struct btrfs_delayed_ref_node *ref; |
| 2269 | struct list_head cluster; |
| 2270 | int ret; |
| 2271 | int run_all = count == (unsigned long)-1; |
| 2272 | int run_most = 0; |
| 2273 | |
| 2274 | if (root == root->fs_info->extent_root) |
| 2275 | root = root->fs_info->tree_root; |
| 2276 | |
| 2277 | delayed_refs = &trans->transaction->delayed_refs; |
| 2278 | INIT_LIST_HEAD(&cluster); |
| 2279 | again: |
| 2280 | spin_lock(&delayed_refs->lock); |
| 2281 | if (count == 0) { |
| 2282 | count = delayed_refs->num_entries * 2; |
| 2283 | run_most = 1; |
| 2284 | } |
| 2285 | while (1) { |
| 2286 | if (!(run_all || run_most) && |
| 2287 | delayed_refs->num_heads_ready < 64) |
| 2288 | break; |
| 2289 | |
| 2290 | /* |
| 2291 | * go find something we can process in the rbtree. We start at |
| 2292 | * the beginning of the tree, and then build a cluster |
| 2293 | * of refs to process starting at the first one we are able to |
| 2294 | * lock |
| 2295 | */ |
| 2296 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2297 | delayed_refs->run_delayed_start); |
| 2298 | if (ret) |
| 2299 | break; |
| 2300 | |
| 2301 | ret = run_clustered_refs(trans, root, &cluster); |
| 2302 | BUG_ON(ret < 0); |
| 2303 | |
| 2304 | count -= min_t(unsigned long, ret, count); |
| 2305 | |
| 2306 | if (count == 0) |
| 2307 | break; |
| 2308 | } |
| 2309 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2310 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2311 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2312 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2313 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2314 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2315 | |
| 2316 | while (node) { |
| 2317 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2318 | rb_node); |
| 2319 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2320 | struct btrfs_delayed_ref_head *head; |
| 2321 | |
| 2322 | head = btrfs_delayed_node_to_head(ref); |
| 2323 | atomic_inc(&ref->refs); |
| 2324 | |
| 2325 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2326 | /* |
| 2327 | * Mutex was contended, block until it's |
| 2328 | * released and try again |
| 2329 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2330 | mutex_lock(&head->mutex); |
| 2331 | mutex_unlock(&head->mutex); |
| 2332 | |
| 2333 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2334 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2335 | goto again; |
| 2336 | } |
| 2337 | node = rb_next(node); |
| 2338 | } |
| 2339 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2340 | schedule_timeout(1); |
| 2341 | goto again; |
| 2342 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2343 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2344 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2345 | return 0; |
| 2346 | } |
| 2347 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2348 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2349 | struct btrfs_root *root, |
| 2350 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2351 | int is_data) |
| 2352 | { |
| 2353 | struct btrfs_delayed_extent_op *extent_op; |
| 2354 | int ret; |
| 2355 | |
| 2356 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2357 | if (!extent_op) |
| 2358 | return -ENOMEM; |
| 2359 | |
| 2360 | extent_op->flags_to_set = flags; |
| 2361 | extent_op->update_flags = 1; |
| 2362 | extent_op->update_key = 0; |
| 2363 | extent_op->is_data = is_data ? 1 : 0; |
| 2364 | |
| 2365 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2366 | if (ret) |
| 2367 | kfree(extent_op); |
| 2368 | return ret; |
| 2369 | } |
| 2370 | |
| 2371 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2372 | struct btrfs_root *root, |
| 2373 | struct btrfs_path *path, |
| 2374 | u64 objectid, u64 offset, u64 bytenr) |
| 2375 | { |
| 2376 | struct btrfs_delayed_ref_head *head; |
| 2377 | struct btrfs_delayed_ref_node *ref; |
| 2378 | struct btrfs_delayed_data_ref *data_ref; |
| 2379 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2380 | struct rb_node *node; |
| 2381 | int ret = 0; |
| 2382 | |
| 2383 | ret = -ENOENT; |
| 2384 | delayed_refs = &trans->transaction->delayed_refs; |
| 2385 | spin_lock(&delayed_refs->lock); |
| 2386 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2387 | if (!head) |
| 2388 | goto out; |
| 2389 | |
| 2390 | if (!mutex_trylock(&head->mutex)) { |
| 2391 | atomic_inc(&head->node.refs); |
| 2392 | spin_unlock(&delayed_refs->lock); |
| 2393 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2394 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2395 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2396 | /* |
| 2397 | * Mutex was contended, block until it's released and let |
| 2398 | * caller try again |
| 2399 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2400 | mutex_lock(&head->mutex); |
| 2401 | mutex_unlock(&head->mutex); |
| 2402 | btrfs_put_delayed_ref(&head->node); |
| 2403 | return -EAGAIN; |
| 2404 | } |
| 2405 | |
| 2406 | node = rb_prev(&head->node.rb_node); |
| 2407 | if (!node) |
| 2408 | goto out_unlock; |
| 2409 | |
| 2410 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2411 | |
| 2412 | if (ref->bytenr != bytenr) |
| 2413 | goto out_unlock; |
| 2414 | |
| 2415 | ret = 1; |
| 2416 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2417 | goto out_unlock; |
| 2418 | |
| 2419 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2420 | |
| 2421 | node = rb_prev(node); |
| 2422 | if (node) { |
| 2423 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2424 | if (ref->bytenr == bytenr) |
| 2425 | goto out_unlock; |
| 2426 | } |
| 2427 | |
| 2428 | if (data_ref->root != root->root_key.objectid || |
| 2429 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2430 | goto out_unlock; |
| 2431 | |
| 2432 | ret = 0; |
| 2433 | out_unlock: |
| 2434 | mutex_unlock(&head->mutex); |
| 2435 | out: |
| 2436 | spin_unlock(&delayed_refs->lock); |
| 2437 | return ret; |
| 2438 | } |
| 2439 | |
| 2440 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2441 | struct btrfs_root *root, |
| 2442 | struct btrfs_path *path, |
| 2443 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2444 | { |
| 2445 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2446 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2447 | struct btrfs_extent_data_ref *ref; |
| 2448 | struct btrfs_extent_inline_ref *iref; |
| 2449 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2450 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2451 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2452 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2453 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2454 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2455 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2456 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2457 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2458 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2459 | if (ret < 0) |
| 2460 | goto out; |
| 2461 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2462 | |
| 2463 | ret = -ENOENT; |
| 2464 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2465 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2466 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2467 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2468 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2469 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2470 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2471 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2472 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2473 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2474 | ret = 1; |
| 2475 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2476 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2477 | if (item_size < sizeof(*ei)) { |
| 2478 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2479 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2480 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2481 | #endif |
| 2482 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2483 | |
| 2484 | if (item_size != sizeof(*ei) + |
| 2485 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2486 | goto out; |
| 2487 | |
| 2488 | if (btrfs_extent_generation(leaf, ei) <= |
| 2489 | btrfs_root_last_snapshot(&root->root_item)) |
| 2490 | goto out; |
| 2491 | |
| 2492 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2493 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2494 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2495 | goto out; |
| 2496 | |
| 2497 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2498 | if (btrfs_extent_refs(leaf, ei) != |
| 2499 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2500 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2501 | root->root_key.objectid || |
| 2502 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2503 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2504 | goto out; |
| 2505 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2506 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2507 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2508 | return ret; |
| 2509 | } |
| 2510 | |
| 2511 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2512 | struct btrfs_root *root, |
| 2513 | u64 objectid, u64 offset, u64 bytenr) |
| 2514 | { |
| 2515 | struct btrfs_path *path; |
| 2516 | int ret; |
| 2517 | int ret2; |
| 2518 | |
| 2519 | path = btrfs_alloc_path(); |
| 2520 | if (!path) |
| 2521 | return -ENOENT; |
| 2522 | |
| 2523 | do { |
| 2524 | ret = check_committed_ref(trans, root, path, objectid, |
| 2525 | offset, bytenr); |
| 2526 | if (ret && ret != -ENOENT) |
| 2527 | goto out; |
| 2528 | |
| 2529 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2530 | offset, bytenr); |
| 2531 | } while (ret2 == -EAGAIN); |
| 2532 | |
| 2533 | if (ret2 && ret2 != -ENOENT) { |
| 2534 | ret = ret2; |
| 2535 | goto out; |
| 2536 | } |
| 2537 | |
| 2538 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2539 | ret = 0; |
| 2540 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2541 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2542 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2543 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2544 | return ret; |
| 2545 | } |
| 2546 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2547 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2548 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2549 | struct extent_buffer *buf, |
| 2550 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2551 | { |
| 2552 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2553 | u64 num_bytes; |
| 2554 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2555 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2556 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2557 | struct btrfs_key key; |
| 2558 | struct btrfs_file_extent_item *fi; |
| 2559 | int i; |
| 2560 | int level; |
| 2561 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2562 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2563 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2564 | |
| 2565 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2566 | nritems = btrfs_header_nritems(buf); |
| 2567 | level = btrfs_header_level(buf); |
| 2568 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2569 | if (!root->ref_cows && level == 0) |
| 2570 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2571 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2572 | if (inc) |
| 2573 | process_func = btrfs_inc_extent_ref; |
| 2574 | else |
| 2575 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2576 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2577 | if (full_backref) |
| 2578 | parent = buf->start; |
| 2579 | else |
| 2580 | parent = 0; |
| 2581 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2582 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2583 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2584 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2585 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2586 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2587 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2588 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2589 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2590 | BTRFS_FILE_EXTENT_INLINE) |
| 2591 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2592 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2593 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2594 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2595 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2596 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2597 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2598 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2599 | parent, ref_root, key.objectid, |
| 2600 | key.offset); |
| 2601 | if (ret) |
| 2602 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2603 | } else { |
| 2604 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2605 | num_bytes = btrfs_level_size(root, level - 1); |
| 2606 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2607 | parent, ref_root, level - 1, 0); |
| 2608 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2609 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2610 | } |
| 2611 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2612 | return 0; |
| 2613 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2614 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2615 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2616 | } |
| 2617 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2618 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2619 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2620 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2621 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2622 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2623 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2624 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2625 | struct extent_buffer *buf, int full_backref) |
| 2626 | { |
| 2627 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2628 | } |
| 2629 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2630 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2631 | struct btrfs_root *root, |
| 2632 | struct btrfs_path *path, |
| 2633 | struct btrfs_block_group_cache *cache) |
| 2634 | { |
| 2635 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2636 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2637 | unsigned long bi; |
| 2638 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2639 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2640 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2641 | if (ret < 0) |
| 2642 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2643 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2644 | |
| 2645 | leaf = path->nodes[0]; |
| 2646 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2647 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2648 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2649 | btrfs_release_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2650 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2651 | if (ret) |
| 2652 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2653 | return 0; |
| 2654 | |
| 2655 | } |
| 2656 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2657 | static struct btrfs_block_group_cache * |
| 2658 | next_block_group(struct btrfs_root *root, |
| 2659 | struct btrfs_block_group_cache *cache) |
| 2660 | { |
| 2661 | struct rb_node *node; |
| 2662 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2663 | node = rb_next(&cache->cache_node); |
| 2664 | btrfs_put_block_group(cache); |
| 2665 | if (node) { |
| 2666 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2667 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2668 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2669 | } else |
| 2670 | cache = NULL; |
| 2671 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2672 | return cache; |
| 2673 | } |
| 2674 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2675 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 2676 | struct btrfs_trans_handle *trans, |
| 2677 | struct btrfs_path *path) |
| 2678 | { |
| 2679 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 2680 | struct inode *inode = NULL; |
| 2681 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2682 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2683 | int num_pages = 0; |
| 2684 | int retries = 0; |
| 2685 | int ret = 0; |
| 2686 | |
| 2687 | /* |
| 2688 | * If this block group is smaller than 100 megs don't bother caching the |
| 2689 | * block group. |
| 2690 | */ |
| 2691 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 2692 | spin_lock(&block_group->lock); |
| 2693 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2694 | spin_unlock(&block_group->lock); |
| 2695 | return 0; |
| 2696 | } |
| 2697 | |
| 2698 | again: |
| 2699 | inode = lookup_free_space_inode(root, block_group, path); |
| 2700 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 2701 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2702 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2703 | goto out; |
| 2704 | } |
| 2705 | |
| 2706 | if (IS_ERR(inode)) { |
| 2707 | BUG_ON(retries); |
| 2708 | retries++; |
| 2709 | |
| 2710 | if (block_group->ro) |
| 2711 | goto out_free; |
| 2712 | |
| 2713 | ret = create_free_space_inode(root, trans, block_group, path); |
| 2714 | if (ret) |
| 2715 | goto out_free; |
| 2716 | goto again; |
| 2717 | } |
| 2718 | |
| 2719 | /* |
| 2720 | * We want to set the generation to 0, that way if anything goes wrong |
| 2721 | * from here on out we know not to trust this cache when we load up next |
| 2722 | * time. |
| 2723 | */ |
| 2724 | BTRFS_I(inode)->generation = 0; |
| 2725 | ret = btrfs_update_inode(trans, root, inode); |
| 2726 | WARN_ON(ret); |
| 2727 | |
| 2728 | if (i_size_read(inode) > 0) { |
| 2729 | ret = btrfs_truncate_free_space_cache(root, trans, path, |
| 2730 | inode); |
| 2731 | if (ret) |
| 2732 | goto out_put; |
| 2733 | } |
| 2734 | |
| 2735 | spin_lock(&block_group->lock); |
| 2736 | if (block_group->cached != BTRFS_CACHE_FINISHED) { |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2737 | /* We're not cached, don't bother trying to write stuff out */ |
| 2738 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2739 | spin_unlock(&block_group->lock); |
| 2740 | goto out_put; |
| 2741 | } |
| 2742 | spin_unlock(&block_group->lock); |
| 2743 | |
| 2744 | num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024); |
| 2745 | if (!num_pages) |
| 2746 | num_pages = 1; |
| 2747 | |
| 2748 | /* |
| 2749 | * Just to make absolutely sure we have enough space, we're going to |
| 2750 | * preallocate 12 pages worth of space for each block group. In |
| 2751 | * practice we ought to use at most 8, but we need extra space so we can |
| 2752 | * add our header and have a terminator between the extents and the |
| 2753 | * bitmaps. |
| 2754 | */ |
| 2755 | num_pages *= 16; |
| 2756 | num_pages *= PAGE_CACHE_SIZE; |
| 2757 | |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 2758 | ret = btrfs_delalloc_reserve_space(inode, num_pages); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2759 | if (ret) |
| 2760 | goto out_put; |
| 2761 | |
| 2762 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 2763 | num_pages, num_pages, |
| 2764 | &alloc_hint); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 2765 | if (!ret) { |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2766 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 2767 | btrfs_free_reserved_data_space(inode, num_pages); |
| 2768 | } else { |
| 2769 | btrfs_delalloc_release_space(inode, num_pages); |
| 2770 | } |
| 2771 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2772 | out_put: |
| 2773 | iput(inode); |
| 2774 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2775 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2776 | out: |
| 2777 | spin_lock(&block_group->lock); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2778 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2779 | spin_unlock(&block_group->lock); |
| 2780 | |
| 2781 | return ret; |
| 2782 | } |
| 2783 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2784 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2785 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2786 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2787 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2788 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2789 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2790 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2791 | |
| 2792 | path = btrfs_alloc_path(); |
| 2793 | if (!path) |
| 2794 | return -ENOMEM; |
| 2795 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2796 | again: |
| 2797 | while (1) { |
| 2798 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2799 | while (cache) { |
| 2800 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 2801 | break; |
| 2802 | cache = next_block_group(root, cache); |
| 2803 | } |
| 2804 | if (!cache) { |
| 2805 | if (last == 0) |
| 2806 | break; |
| 2807 | last = 0; |
| 2808 | continue; |
| 2809 | } |
| 2810 | err = cache_save_setup(cache, trans, path); |
| 2811 | last = cache->key.objectid + cache->key.offset; |
| 2812 | btrfs_put_block_group(cache); |
| 2813 | } |
| 2814 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2815 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2816 | if (last == 0) { |
| 2817 | err = btrfs_run_delayed_refs(trans, root, |
| 2818 | (unsigned long)-1); |
| 2819 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2820 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2821 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2822 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2823 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2824 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 2825 | btrfs_put_block_group(cache); |
| 2826 | goto again; |
| 2827 | } |
| 2828 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2829 | if (cache->dirty) |
| 2830 | break; |
| 2831 | cache = next_block_group(root, cache); |
| 2832 | } |
| 2833 | if (!cache) { |
| 2834 | if (last == 0) |
| 2835 | break; |
| 2836 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2837 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2838 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2839 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2840 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 2841 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2842 | cache->dirty = 0; |
| 2843 | last = cache->key.objectid + cache->key.offset; |
| 2844 | |
| 2845 | err = write_one_cache_group(trans, root, path, cache); |
| 2846 | BUG_ON(err); |
| 2847 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2848 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2849 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2850 | while (1) { |
| 2851 | /* |
| 2852 | * I don't think this is needed since we're just marking our |
| 2853 | * preallocated extent as written, but just in case it can't |
| 2854 | * hurt. |
| 2855 | */ |
| 2856 | if (last == 0) { |
| 2857 | err = btrfs_run_delayed_refs(trans, root, |
| 2858 | (unsigned long)-1); |
| 2859 | BUG_ON(err); |
| 2860 | } |
| 2861 | |
| 2862 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2863 | while (cache) { |
| 2864 | /* |
| 2865 | * Really this shouldn't happen, but it could if we |
| 2866 | * couldn't write the entire preallocated extent and |
| 2867 | * splitting the extent resulted in a new block. |
| 2868 | */ |
| 2869 | if (cache->dirty) { |
| 2870 | btrfs_put_block_group(cache); |
| 2871 | goto again; |
| 2872 | } |
| 2873 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2874 | break; |
| 2875 | cache = next_block_group(root, cache); |
| 2876 | } |
| 2877 | if (!cache) { |
| 2878 | if (last == 0) |
| 2879 | break; |
| 2880 | last = 0; |
| 2881 | continue; |
| 2882 | } |
| 2883 | |
| 2884 | btrfs_write_out_cache(root, trans, cache, path); |
| 2885 | |
| 2886 | /* |
| 2887 | * If we didn't have an error then the cache state is still |
| 2888 | * NEED_WRITE, so we can set it to WRITTEN. |
| 2889 | */ |
| 2890 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2891 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2892 | last = cache->key.objectid + cache->key.offset; |
| 2893 | btrfs_put_block_group(cache); |
| 2894 | } |
| 2895 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2896 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2897 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2898 | } |
| 2899 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2900 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2901 | { |
| 2902 | struct btrfs_block_group_cache *block_group; |
| 2903 | int readonly = 0; |
| 2904 | |
| 2905 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2906 | if (!block_group || block_group->ro) |
| 2907 | readonly = 1; |
| 2908 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2909 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2910 | return readonly; |
| 2911 | } |
| 2912 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2913 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2914 | u64 total_bytes, u64 bytes_used, |
| 2915 | struct btrfs_space_info **space_info) |
| 2916 | { |
| 2917 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2918 | int i; |
| 2919 | int factor; |
| 2920 | |
| 2921 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 2922 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2923 | factor = 2; |
| 2924 | else |
| 2925 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2926 | |
| 2927 | found = __find_space_info(info, flags); |
| 2928 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2929 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2930 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2931 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2932 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2933 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2934 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2935 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2936 | *space_info = found; |
| 2937 | return 0; |
| 2938 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2939 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2940 | if (!found) |
| 2941 | return -ENOMEM; |
| 2942 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2943 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 2944 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2945 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2946 | spin_lock_init(&found->lock); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2947 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
| 2948 | BTRFS_BLOCK_GROUP_SYSTEM | |
| 2949 | BTRFS_BLOCK_GROUP_METADATA); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2950 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2951 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2952 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2953 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2954 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2955 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2956 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2957 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2958 | found->full = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 2959 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 2960 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 2961 | found->flush = 0; |
| 2962 | init_waitqueue_head(&found->wait); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2963 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2964 | list_add_rcu(&found->list, &info->space_info); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2965 | return 0; |
| 2966 | } |
| 2967 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2968 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2969 | { |
| 2970 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2971 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2972 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2973 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2974 | if (extra_flags) { |
| 2975 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2976 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2977 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2978 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2979 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2980 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2981 | } |
| 2982 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2983 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2984 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2985 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2986 | /* |
| 2987 | * we add in the count of missing devices because we want |
| 2988 | * to make sure that any RAID levels on a degraded FS |
| 2989 | * continue to be honored. |
| 2990 | */ |
| 2991 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 2992 | root->fs_info->fs_devices->missing_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 2993 | |
| 2994 | if (num_devices == 1) |
| 2995 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 2996 | if (num_devices < 4) |
| 2997 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 2998 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2999 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 3000 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3001 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3002 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3003 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3004 | |
| 3005 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3006 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3007 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3008 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3009 | |
| 3010 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 3011 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 3012 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 3013 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 3014 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 3015 | return flags; |
| 3016 | } |
| 3017 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3018 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3019 | { |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3020 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3021 | flags |= root->fs_info->avail_data_alloc_bits & |
| 3022 | root->fs_info->data_alloc_profile; |
| 3023 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3024 | flags |= root->fs_info->avail_system_alloc_bits & |
| 3025 | root->fs_info->system_alloc_profile; |
| 3026 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3027 | flags |= root->fs_info->avail_metadata_alloc_bits & |
| 3028 | root->fs_info->metadata_alloc_profile; |
| 3029 | return btrfs_reduce_alloc_profile(root, flags); |
| 3030 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3031 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3032 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3033 | { |
| 3034 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3035 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3036 | if (data) |
| 3037 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3038 | else if (root == root->fs_info->chunk_root) |
| 3039 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3040 | else |
| 3041 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3042 | |
| 3043 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 3047 | { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3048 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3049 | BTRFS_BLOCK_GROUP_DATA); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | /* |
| 3053 | * This will check the space that the inode allocates from to make sure we have |
| 3054 | * enough space for bytes. |
| 3055 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3056 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3057 | { |
| 3058 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3059 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3060 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3061 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3062 | |
| 3063 | /* make sure bytes are sectorsize aligned */ |
| 3064 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3065 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3066 | if (root == root->fs_info->tree_root || |
| 3067 | BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3068 | alloc_chunk = 0; |
| 3069 | committed = 1; |
| 3070 | } |
| 3071 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3072 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3073 | if (!data_sinfo) |
| 3074 | goto alloc; |
| 3075 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3076 | again: |
| 3077 | /* make sure we have enough space to handle the data first */ |
| 3078 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3079 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3080 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3081 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3082 | |
| 3083 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3084 | struct btrfs_trans_handle *trans; |
| 3085 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3086 | /* |
| 3087 | * if we don't have enough free bytes in this space then we need |
| 3088 | * to alloc a new chunk. |
| 3089 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3090 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3091 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3092 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3093 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3094 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3095 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3096 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3097 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3098 | if (IS_ERR(trans)) |
| 3099 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3100 | |
| 3101 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3102 | bytes + 2 * 1024 * 1024, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3103 | alloc_target, |
| 3104 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3105 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3106 | if (ret < 0) { |
| 3107 | if (ret != -ENOSPC) |
| 3108 | return ret; |
| 3109 | else |
| 3110 | goto commit_trans; |
| 3111 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3112 | |
| 3113 | if (!data_sinfo) { |
| 3114 | btrfs_set_inode_space_info(root, inode); |
| 3115 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3116 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3117 | goto again; |
| 3118 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3119 | |
| 3120 | /* |
| 3121 | * If we have less pinned bytes than we want to allocate then |
| 3122 | * don't bother committing the transaction, it won't help us. |
| 3123 | */ |
| 3124 | if (data_sinfo->bytes_pinned < bytes) |
| 3125 | committed = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3126 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3127 | |
| 3128 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3129 | commit_trans: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3130 | if (!committed && |
| 3131 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3132 | committed = 1; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3133 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3134 | if (IS_ERR(trans)) |
| 3135 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3136 | ret = btrfs_commit_transaction(trans, root); |
| 3137 | if (ret) |
| 3138 | return ret; |
| 3139 | goto again; |
| 3140 | } |
| 3141 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3142 | return -ENOSPC; |
| 3143 | } |
| 3144 | data_sinfo->bytes_may_use += bytes; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3145 | spin_unlock(&data_sinfo->lock); |
| 3146 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3147 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3151 | * Called if we need to clear a data reservation for this inode. |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3152 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3153 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3154 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3155 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3156 | struct btrfs_space_info *data_sinfo; |
| 3157 | |
| 3158 | /* make sure bytes are sectorsize aligned */ |
| 3159 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3160 | |
| 3161 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3162 | spin_lock(&data_sinfo->lock); |
| 3163 | data_sinfo->bytes_may_use -= bytes; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3164 | spin_unlock(&data_sinfo->lock); |
| 3165 | } |
| 3166 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3167 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3168 | { |
| 3169 | struct list_head *head = &info->space_info; |
| 3170 | struct btrfs_space_info *found; |
| 3171 | |
| 3172 | rcu_read_lock(); |
| 3173 | list_for_each_entry_rcu(found, head, list) { |
| 3174 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3175 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3176 | } |
| 3177 | rcu_read_unlock(); |
| 3178 | } |
| 3179 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3180 | static int should_alloc_chunk(struct btrfs_root *root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3181 | struct btrfs_space_info *sinfo, u64 alloc_bytes, |
| 3182 | int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3183 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3184 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3185 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3186 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3187 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3188 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3189 | if (force == CHUNK_ALLOC_FORCE) |
| 3190 | return 1; |
| 3191 | |
| 3192 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3193 | * We need to take into account the global rsv because for all intents |
| 3194 | * and purposes it's used space. Don't worry about locking the |
| 3195 | * global_rsv, it doesn't change except when the transaction commits. |
| 3196 | */ |
| 3197 | num_allocated += global_rsv->size; |
| 3198 | |
| 3199 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3200 | * in limited mode, we want to have some free space up to |
| 3201 | * about 1% of the FS size. |
| 3202 | */ |
| 3203 | if (force == CHUNK_ALLOC_LIMITED) { |
| 3204 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
| 3205 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 3206 | div_factor_fine(thresh, 1)); |
| 3207 | |
| 3208 | if (num_bytes - num_allocated < thresh) |
| 3209 | return 1; |
| 3210 | } |
| 3211 | |
| 3212 | /* |
| 3213 | * we have two similar checks here, one based on percentage |
| 3214 | * and once based on a hard number of 256MB. The idea |
| 3215 | * is that if we have a good amount of free |
| 3216 | * room, don't allocate a chunk. A good mount is |
| 3217 | * less than 80% utilized of the chunks we have allocated, |
| 3218 | * or more than 256MB free |
| 3219 | */ |
| 3220 | if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3221 | return 0; |
| 3222 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3223 | if (num_allocated + alloc_bytes < div_factor(num_bytes, 8)) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3224 | return 0; |
| 3225 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3226 | thresh = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3227 | |
| 3228 | /* 256MB or 5% of the FS */ |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3229 | thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5)); |
| 3230 | |
| 3231 | if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3232 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3233 | return 1; |
| 3234 | } |
| 3235 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3236 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3237 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3238 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3239 | { |
| 3240 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3241 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3242 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3243 | int ret = 0; |
| 3244 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3245 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3246 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3247 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3248 | if (!space_info) { |
| 3249 | ret = update_space_info(extent_root->fs_info, flags, |
| 3250 | 0, 0, &space_info); |
| 3251 | BUG_ON(ret); |
| 3252 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3253 | BUG_ON(!space_info); |
| 3254 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3255 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3256 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3257 | if (space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3258 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3259 | if (space_info->full) { |
| 3260 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3261 | return 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3262 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3263 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3264 | if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3265 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3266 | return 0; |
| 3267 | } else if (space_info->chunk_alloc) { |
| 3268 | wait_for_alloc = 1; |
| 3269 | } else { |
| 3270 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3271 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3272 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3273 | spin_unlock(&space_info->lock); |
| 3274 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3275 | mutex_lock(&fs_info->chunk_mutex); |
| 3276 | |
| 3277 | /* |
| 3278 | * The chunk_mutex is held throughout the entirety of a chunk |
| 3279 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 3280 | * other guy is done and we need to recheck and see if we should |
| 3281 | * allocate. |
| 3282 | */ |
| 3283 | if (wait_for_alloc) { |
| 3284 | mutex_unlock(&fs_info->chunk_mutex); |
| 3285 | wait_for_alloc = 0; |
| 3286 | goto again; |
| 3287 | } |
| 3288 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3289 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3290 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3291 | * allocating mixed chunks instead of individual chunks. |
| 3292 | */ |
| 3293 | if (btrfs_mixed_space_info(space_info)) |
| 3294 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3295 | |
| 3296 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3297 | * if we're doing a data chunk, go ahead and make sure that |
| 3298 | * we keep a reasonable number of metadata chunks allocated in the |
| 3299 | * FS as well. |
| 3300 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3301 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3302 | fs_info->data_chunk_allocations++; |
| 3303 | if (!(fs_info->data_chunk_allocations % |
| 3304 | fs_info->metadata_ratio)) |
| 3305 | force_metadata_allocation(fs_info); |
| 3306 | } |
| 3307 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3308 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3309 | if (ret < 0 && ret != -ENOSPC) |
| 3310 | goto out; |
| 3311 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3312 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3313 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3314 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3315 | else |
| 3316 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3317 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3318 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3319 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3320 | spin_unlock(&space_info->lock); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3321 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3322 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3323 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3324 | } |
| 3325 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3326 | /* |
| 3327 | * shrink metadata reservation for delalloc |
| 3328 | */ |
| 3329 | static int shrink_delalloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3330 | struct btrfs_root *root, u64 to_reclaim, int sync) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3331 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3332 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3333 | struct btrfs_space_info *space_info; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3334 | u64 reserved; |
| 3335 | u64 max_reclaim; |
| 3336 | u64 reclaimed = 0; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3337 | long time_left; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3338 | int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3339 | int loops = 0; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3340 | unsigned long progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3341 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3342 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3343 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3344 | |
| 3345 | smp_mb(); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3346 | reserved = space_info->bytes_may_use; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3347 | progress = space_info->reservation_progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3348 | |
| 3349 | if (reserved == 0) |
| 3350 | return 0; |
| 3351 | |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3352 | smp_mb(); |
| 3353 | if (root->fs_info->delalloc_bytes == 0) { |
| 3354 | if (trans) |
| 3355 | return 0; |
| 3356 | btrfs_wait_ordered_extents(root, 0, 0); |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3360 | max_reclaim = min(reserved, to_reclaim); |
| 3361 | |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3362 | while (loops < 1024) { |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3363 | /* have the flusher threads jump in and do some IO */ |
| 3364 | smp_mb(); |
| 3365 | nr_pages = min_t(unsigned long, nr_pages, |
| 3366 | root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); |
| 3367 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3368 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3369 | spin_lock(&space_info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3370 | if (reserved > space_info->bytes_may_use) |
| 3371 | reclaimed += reserved - space_info->bytes_may_use; |
| 3372 | reserved = space_info->bytes_may_use; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3373 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3374 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3375 | loops++; |
| 3376 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3377 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3378 | break; |
| 3379 | |
| 3380 | if (trans && trans->transaction->blocked) |
| 3381 | return -EAGAIN; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3382 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3383 | time_left = schedule_timeout_interruptible(1); |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3384 | |
| 3385 | /* We were interrupted, exit */ |
| 3386 | if (time_left) |
| 3387 | break; |
| 3388 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3389 | /* we've kicked the IO a few times, if anything has been freed, |
| 3390 | * exit. There is no sense in looping here for a long time |
| 3391 | * when we really need to commit the transaction, or there are |
| 3392 | * just too many writers without enough free space |
| 3393 | */ |
| 3394 | |
| 3395 | if (loops > 3) { |
| 3396 | smp_mb(); |
| 3397 | if (progress != space_info->reservation_progress) |
| 3398 | break; |
| 3399 | } |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3400 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3401 | } |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3402 | if (reclaimed >= to_reclaim && !trans) |
| 3403 | btrfs_wait_ordered_extents(root, 0, 0); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3404 | return reclaimed >= to_reclaim; |
| 3405 | } |
| 3406 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3407 | /* |
| 3408 | * Retries tells us how many times we've called reserve_metadata_bytes. The |
| 3409 | * idea is if this is the first call (retries == 0) then we will add to our |
| 3410 | * reserved count if we can't make the allocation in order to hold our place |
| 3411 | * while we go and try and free up space. That way for retries > 1 we don't try |
| 3412 | * and add space, we just check to see if the amount of unused space is >= the |
| 3413 | * total space, meaning that our reservation is valid. |
| 3414 | * |
| 3415 | * However if we don't intend to retry this reservation, pass -1 as retries so |
| 3416 | * that it short circuits this logic. |
| 3417 | */ |
| 3418 | static int reserve_metadata_bytes(struct btrfs_trans_handle *trans, |
| 3419 | struct btrfs_root *root, |
| 3420 | struct btrfs_block_rsv *block_rsv, |
| 3421 | u64 orig_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3422 | { |
| 3423 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3424 | u64 unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3425 | u64 num_bytes = orig_bytes; |
| 3426 | int retries = 0; |
| 3427 | int ret = 0; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3428 | bool committed = false; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3429 | bool flushing = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3430 | again: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3431 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3432 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3433 | /* |
| 3434 | * We only want to wait if somebody other than us is flushing and we are |
| 3435 | * actually alloed to flush. |
| 3436 | */ |
| 3437 | while (flush && !flushing && space_info->flush) { |
| 3438 | spin_unlock(&space_info->lock); |
| 3439 | /* |
| 3440 | * If we have a trans handle we can't wait because the flusher |
| 3441 | * may have to commit the transaction, which would mean we would |
| 3442 | * deadlock since we are waiting for the flusher to finish, but |
| 3443 | * hold the current transaction open. |
| 3444 | */ |
| 3445 | if (trans) |
| 3446 | return -EAGAIN; |
| 3447 | ret = wait_event_interruptible(space_info->wait, |
| 3448 | !space_info->flush); |
| 3449 | /* Must have been interrupted, return */ |
| 3450 | if (ret) |
| 3451 | return -EINTR; |
| 3452 | |
| 3453 | spin_lock(&space_info->lock); |
| 3454 | } |
| 3455 | |
| 3456 | ret = -ENOSPC; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3457 | unused = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3458 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3459 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3460 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3461 | /* |
| 3462 | * The idea here is that we've not already over-reserved the block group |
| 3463 | * then we can go ahead and save our reservation first and then start |
| 3464 | * flushing if we need to. Otherwise if we've already overcommitted |
| 3465 | * lets start flushing stuff first and then come back and try to make |
| 3466 | * our reservation. |
| 3467 | */ |
| 3468 | if (unused <= space_info->total_bytes) { |
Arne Jansen | 6f33434 | 2010-11-12 23:17:56 +0000 | [diff] [blame] | 3469 | unused = space_info->total_bytes - unused; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3470 | if (unused >= num_bytes) { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3471 | space_info->bytes_may_use += orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3472 | ret = 0; |
| 3473 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3474 | /* |
| 3475 | * Ok set num_bytes to orig_bytes since we aren't |
| 3476 | * overocmmitted, this way we only try and reclaim what |
| 3477 | * we need. |
| 3478 | */ |
| 3479 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3480 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3481 | } else { |
| 3482 | /* |
| 3483 | * Ok we're over committed, set num_bytes to the overcommitted |
| 3484 | * amount plus the amount of bytes that we need for this |
| 3485 | * reservation. |
| 3486 | */ |
| 3487 | num_bytes = unused - space_info->total_bytes + |
| 3488 | (orig_bytes * (retries + 1)); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3489 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3490 | |
| 3491 | /* |
| 3492 | * Couldn't make our reservation, save our place so while we're trying |
| 3493 | * to reclaim space we can actually use it instead of somebody else |
| 3494 | * stealing it from us. |
| 3495 | */ |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3496 | if (ret && flush) { |
| 3497 | flushing = true; |
| 3498 | space_info->flush = 1; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3499 | } |
| 3500 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3501 | spin_unlock(&space_info->lock); |
| 3502 | |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3503 | if (!ret || !flush) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3504 | goto out; |
| 3505 | |
| 3506 | /* |
| 3507 | * We do synchronous shrinking since we don't actually unreserve |
| 3508 | * metadata until after the IO is completed. |
| 3509 | */ |
| 3510 | ret = shrink_delalloc(trans, root, num_bytes, 1); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3511 | if (ret < 0) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3512 | goto out; |
| 3513 | |
Chris Mason | 75c195a | 2011-07-27 15:57:44 -0400 | [diff] [blame] | 3514 | ret = 0; |
| 3515 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3516 | /* |
| 3517 | * So if we were overcommitted it's possible that somebody else flushed |
| 3518 | * out enough space and we simply didn't have enough space to reclaim, |
| 3519 | * so go back around and try again. |
| 3520 | */ |
| 3521 | if (retries < 2) { |
| 3522 | retries++; |
| 3523 | goto again; |
| 3524 | } |
| 3525 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3526 | /* |
| 3527 | * Not enough space to be reclaimed, don't bother committing the |
| 3528 | * transaction. |
| 3529 | */ |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3530 | spin_lock(&space_info->lock); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3531 | if (space_info->bytes_pinned < orig_bytes) |
| 3532 | ret = -ENOSPC; |
| 3533 | spin_unlock(&space_info->lock); |
| 3534 | if (ret) |
| 3535 | goto out; |
| 3536 | |
| 3537 | ret = -EAGAIN; |
Chris Mason | 75c195a | 2011-07-27 15:57:44 -0400 | [diff] [blame] | 3538 | if (trans) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3539 | goto out; |
| 3540 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3541 | ret = -ENOSPC; |
Chris Mason | 75c195a | 2011-07-27 15:57:44 -0400 | [diff] [blame] | 3542 | if (committed) |
| 3543 | goto out; |
| 3544 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3545 | trans = btrfs_join_transaction(root); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3546 | if (IS_ERR(trans)) |
| 3547 | goto out; |
| 3548 | ret = btrfs_commit_transaction(trans, root); |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3549 | if (!ret) { |
| 3550 | trans = NULL; |
| 3551 | committed = true; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3552 | goto again; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3553 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3554 | |
| 3555 | out: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3556 | if (flushing) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3557 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3558 | space_info->flush = 0; |
| 3559 | wake_up_all(&space_info->wait); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3560 | spin_unlock(&space_info->lock); |
| 3561 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3562 | return ret; |
| 3563 | } |
| 3564 | |
| 3565 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3566 | struct btrfs_root *root) |
| 3567 | { |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame^] | 3568 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3569 | |
| 3570 | if (root->ref_cows || root == root->fs_info->csum_root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3571 | block_rsv = trans->block_rsv; |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame^] | 3572 | |
| 3573 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3574 | block_rsv = root->block_rsv; |
| 3575 | |
| 3576 | if (!block_rsv) |
| 3577 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3578 | |
| 3579 | return block_rsv; |
| 3580 | } |
| 3581 | |
| 3582 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3583 | u64 num_bytes) |
| 3584 | { |
| 3585 | int ret = -ENOSPC; |
| 3586 | spin_lock(&block_rsv->lock); |
| 3587 | if (block_rsv->reserved >= num_bytes) { |
| 3588 | block_rsv->reserved -= num_bytes; |
| 3589 | if (block_rsv->reserved < block_rsv->size) |
| 3590 | block_rsv->full = 0; |
| 3591 | ret = 0; |
| 3592 | } |
| 3593 | spin_unlock(&block_rsv->lock); |
| 3594 | return ret; |
| 3595 | } |
| 3596 | |
| 3597 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3598 | u64 num_bytes, int update_size) |
| 3599 | { |
| 3600 | spin_lock(&block_rsv->lock); |
| 3601 | block_rsv->reserved += num_bytes; |
| 3602 | if (update_size) |
| 3603 | block_rsv->size += num_bytes; |
| 3604 | else if (block_rsv->reserved >= block_rsv->size) |
| 3605 | block_rsv->full = 1; |
| 3606 | spin_unlock(&block_rsv->lock); |
| 3607 | } |
| 3608 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 3609 | static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3610 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3611 | { |
| 3612 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3613 | |
| 3614 | spin_lock(&block_rsv->lock); |
| 3615 | if (num_bytes == (u64)-1) |
| 3616 | num_bytes = block_rsv->size; |
| 3617 | block_rsv->size -= num_bytes; |
| 3618 | if (block_rsv->reserved >= block_rsv->size) { |
| 3619 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3620 | block_rsv->reserved = block_rsv->size; |
| 3621 | block_rsv->full = 1; |
| 3622 | } else { |
| 3623 | num_bytes = 0; |
| 3624 | } |
| 3625 | spin_unlock(&block_rsv->lock); |
| 3626 | |
| 3627 | if (num_bytes > 0) { |
| 3628 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 3629 | spin_lock(&dest->lock); |
| 3630 | if (!dest->full) { |
| 3631 | u64 bytes_to_add; |
| 3632 | |
| 3633 | bytes_to_add = dest->size - dest->reserved; |
| 3634 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 3635 | dest->reserved += bytes_to_add; |
| 3636 | if (dest->reserved >= dest->size) |
| 3637 | dest->full = 1; |
| 3638 | num_bytes -= bytes_to_add; |
| 3639 | } |
| 3640 | spin_unlock(&dest->lock); |
| 3641 | } |
| 3642 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3643 | spin_lock(&space_info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3644 | space_info->bytes_may_use -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3645 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3646 | spin_unlock(&space_info->lock); |
| 3647 | } |
| 3648 | } |
| 3649 | } |
| 3650 | |
| 3651 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3652 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3653 | { |
| 3654 | int ret; |
| 3655 | |
| 3656 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3657 | if (ret) |
| 3658 | return ret; |
| 3659 | |
| 3660 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3661 | return 0; |
| 3662 | } |
| 3663 | |
| 3664 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3665 | { |
| 3666 | memset(rsv, 0, sizeof(*rsv)); |
| 3667 | spin_lock_init(&rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3671 | { |
| 3672 | struct btrfs_block_rsv *block_rsv; |
| 3673 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3674 | |
| 3675 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3676 | if (!block_rsv) |
| 3677 | return NULL; |
| 3678 | |
| 3679 | btrfs_init_block_rsv(block_rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3680 | block_rsv->space_info = __find_space_info(fs_info, |
| 3681 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3682 | return block_rsv; |
| 3683 | } |
| 3684 | |
| 3685 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3686 | struct btrfs_block_rsv *rsv) |
| 3687 | { |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 3688 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3689 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3690 | } |
| 3691 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3692 | int btrfs_block_rsv_add(struct btrfs_trans_handle *trans, |
| 3693 | struct btrfs_root *root, |
| 3694 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3695 | u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3696 | { |
| 3697 | int ret; |
| 3698 | |
| 3699 | if (num_bytes == 0) |
| 3700 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3701 | |
| 3702 | ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3703 | if (!ret) { |
| 3704 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3705 | return 0; |
| 3706 | } |
| 3707 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3708 | return ret; |
| 3709 | } |
| 3710 | |
| 3711 | int btrfs_block_rsv_check(struct btrfs_trans_handle *trans, |
| 3712 | struct btrfs_root *root, |
| 3713 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 482e6dc | 2011-08-19 10:31:56 -0400 | [diff] [blame] | 3714 | u64 min_reserved, int min_factor, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3715 | { |
| 3716 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3717 | int ret = -ENOSPC; |
| 3718 | |
| 3719 | if (!block_rsv) |
| 3720 | return 0; |
| 3721 | |
| 3722 | spin_lock(&block_rsv->lock); |
| 3723 | if (min_factor > 0) |
| 3724 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3725 | if (min_reserved > num_bytes) |
| 3726 | num_bytes = min_reserved; |
| 3727 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 3728 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3729 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 3730 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3731 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3732 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 3733 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3734 | if (!ret) |
| 3735 | return 0; |
| 3736 | |
Josef Bacik | 482e6dc | 2011-08-19 10:31:56 -0400 | [diff] [blame] | 3737 | ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, flush); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 3738 | if (!ret) { |
| 3739 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3740 | return 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3741 | } |
| 3742 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 3743 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3747 | struct btrfs_block_rsv *dst_rsv, |
| 3748 | u64 num_bytes) |
| 3749 | { |
| 3750 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3751 | } |
| 3752 | |
| 3753 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3754 | struct btrfs_block_rsv *block_rsv, |
| 3755 | u64 num_bytes) |
| 3756 | { |
| 3757 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3758 | if (global_rsv->full || global_rsv == block_rsv || |
| 3759 | block_rsv->space_info != global_rsv->space_info) |
| 3760 | global_rsv = NULL; |
| 3761 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3762 | } |
| 3763 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3764 | /* |
| 3765 | * helper to calculate size of global block reservation. |
| 3766 | * the desired value is sum of space used by extent tree, |
| 3767 | * checksum tree and root tree |
| 3768 | */ |
| 3769 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3770 | { |
| 3771 | struct btrfs_space_info *sinfo; |
| 3772 | u64 num_bytes; |
| 3773 | u64 meta_used; |
| 3774 | u64 data_used; |
| 3775 | int csum_size = btrfs_super_csum_size(&fs_info->super_copy); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3776 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3777 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3778 | spin_lock(&sinfo->lock); |
| 3779 | data_used = sinfo->bytes_used; |
| 3780 | spin_unlock(&sinfo->lock); |
| 3781 | |
| 3782 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3783 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3784 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 3785 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3786 | meta_used = sinfo->bytes_used; |
| 3787 | spin_unlock(&sinfo->lock); |
| 3788 | |
| 3789 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3790 | csum_size * 2; |
| 3791 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3792 | |
| 3793 | if (num_bytes * 3 > meta_used) |
| 3794 | num_bytes = div64_u64(meta_used, 3); |
| 3795 | |
| 3796 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3797 | } |
| 3798 | |
| 3799 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3800 | { |
| 3801 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3802 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3803 | u64 num_bytes; |
| 3804 | |
| 3805 | num_bytes = calc_global_metadata_size(fs_info); |
| 3806 | |
| 3807 | spin_lock(&block_rsv->lock); |
| 3808 | spin_lock(&sinfo->lock); |
| 3809 | |
| 3810 | block_rsv->size = num_bytes; |
| 3811 | |
| 3812 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3813 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 3814 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3815 | |
| 3816 | if (sinfo->total_bytes > num_bytes) { |
| 3817 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3818 | block_rsv->reserved += num_bytes; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3819 | sinfo->bytes_may_use += num_bytes; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3820 | } |
| 3821 | |
| 3822 | if (block_rsv->reserved >= block_rsv->size) { |
| 3823 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3824 | sinfo->bytes_may_use -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3825 | sinfo->reservation_progress++; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3826 | block_rsv->reserved = block_rsv->size; |
| 3827 | block_rsv->full = 1; |
| 3828 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 3829 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3830 | spin_unlock(&sinfo->lock); |
| 3831 | spin_unlock(&block_rsv->lock); |
| 3832 | } |
| 3833 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3834 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3835 | { |
| 3836 | struct btrfs_space_info *space_info; |
| 3837 | |
| 3838 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3839 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3840 | |
| 3841 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3842 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3843 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3844 | fs_info->trans_block_rsv.space_info = space_info; |
| 3845 | fs_info->empty_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3846 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3847 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3848 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3849 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3850 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3851 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3852 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3853 | update_global_block_rsv(fs_info); |
| 3854 | } |
| 3855 | |
| 3856 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3857 | { |
| 3858 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3859 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3860 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3861 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3862 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3863 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3864 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3865 | } |
| 3866 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3867 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 3868 | struct btrfs_root *root) |
| 3869 | { |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame^] | 3870 | struct btrfs_block_rsv *block_rsv; |
| 3871 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3872 | if (!trans->bytes_reserved) |
| 3873 | return; |
| 3874 | |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame^] | 3875 | block_rsv = &root->fs_info->trans_block_rsv; |
| 3876 | btrfs_block_rsv_release(root, block_rsv, trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3877 | trans->bytes_reserved = 0; |
| 3878 | } |
| 3879 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3880 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3881 | struct inode *inode) |
| 3882 | { |
| 3883 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3884 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3885 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 3886 | |
| 3887 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 3888 | * We need to hold space in order to delete our orphan item once we've |
| 3889 | * added it, so this takes the reservation so we can release it later |
| 3890 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3891 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 3892 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3893 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3894 | } |
| 3895 | |
| 3896 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 3897 | { |
| 3898 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 3899 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3900 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 3901 | } |
| 3902 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3903 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 3904 | struct btrfs_pending_snapshot *pending) |
| 3905 | { |
| 3906 | struct btrfs_root *root = pending->root; |
| 3907 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 3908 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 3909 | /* |
| 3910 | * two for root back/forward refs, two for directory entries |
| 3911 | * and one for root of the snapshot. |
| 3912 | */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 3913 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3914 | dst_rsv->space_info = src_rsv->space_info; |
| 3915 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3916 | } |
| 3917 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 3918 | /** |
| 3919 | * drop_outstanding_extent - drop an outstanding extent |
| 3920 | * @inode: the inode we're dropping the extent for |
| 3921 | * |
| 3922 | * This is called when we are freeing up an outstanding extent, either called |
| 3923 | * after an error or after an extent is written. This will return the number of |
| 3924 | * reserved extents that need to be freed. This must be called with |
| 3925 | * BTRFS_I(inode)->lock held. |
| 3926 | */ |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 3927 | static unsigned drop_outstanding_extent(struct inode *inode) |
| 3928 | { |
| 3929 | unsigned dropped_extents = 0; |
| 3930 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 3931 | BUG_ON(!BTRFS_I(inode)->outstanding_extents); |
| 3932 | BTRFS_I(inode)->outstanding_extents--; |
| 3933 | |
| 3934 | /* |
| 3935 | * If we have more or the same amount of outsanding extents than we have |
| 3936 | * reserved then we need to leave the reserved extents count alone. |
| 3937 | */ |
| 3938 | if (BTRFS_I(inode)->outstanding_extents >= |
| 3939 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 3940 | return 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 3941 | |
| 3942 | dropped_extents = BTRFS_I(inode)->reserved_extents - |
| 3943 | BTRFS_I(inode)->outstanding_extents; |
| 3944 | BTRFS_I(inode)->reserved_extents -= dropped_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 3945 | return dropped_extents; |
| 3946 | } |
| 3947 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 3948 | /** |
| 3949 | * calc_csum_metadata_size - return the amount of metada space that must be |
| 3950 | * reserved/free'd for the given bytes. |
| 3951 | * @inode: the inode we're manipulating |
| 3952 | * @num_bytes: the number of bytes in question |
| 3953 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 3954 | * |
| 3955 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 3956 | * correct amount of metadata that must either be reserved or freed. We |
| 3957 | * calculate how many checksums we can fit into one leaf and then divide the |
| 3958 | * number of bytes that will need to be checksumed by this value to figure out |
| 3959 | * how many checksums will be required. If we are adding bytes then the number |
| 3960 | * may go up and we will return the number of additional bytes that must be |
| 3961 | * reserved. If it is going down we will return the number of bytes that must |
| 3962 | * be freed. |
| 3963 | * |
| 3964 | * This must be called with BTRFS_I(inode)->lock held. |
| 3965 | */ |
| 3966 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, |
| 3967 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3968 | { |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 3969 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 3970 | u64 csum_size; |
| 3971 | int num_csums_per_leaf; |
| 3972 | int num_csums; |
| 3973 | int old_csums; |
| 3974 | |
| 3975 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && |
| 3976 | BTRFS_I(inode)->csum_bytes == 0) |
| 3977 | return 0; |
| 3978 | |
| 3979 | old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 3980 | if (reserve) |
| 3981 | BTRFS_I(inode)->csum_bytes += num_bytes; |
| 3982 | else |
| 3983 | BTRFS_I(inode)->csum_bytes -= num_bytes; |
| 3984 | csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item); |
| 3985 | num_csums_per_leaf = (int)div64_u64(csum_size, |
| 3986 | sizeof(struct btrfs_csum_item) + |
| 3987 | sizeof(struct btrfs_disk_key)); |
| 3988 | num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 3989 | num_csums = num_csums + num_csums_per_leaf - 1; |
| 3990 | num_csums = num_csums / num_csums_per_leaf; |
| 3991 | |
| 3992 | old_csums = old_csums + num_csums_per_leaf - 1; |
| 3993 | old_csums = old_csums / num_csums_per_leaf; |
| 3994 | |
| 3995 | /* No change, no need to reserve more */ |
| 3996 | if (old_csums == num_csums) |
| 3997 | return 0; |
| 3998 | |
| 3999 | if (reserve) |
| 4000 | return btrfs_calc_trans_metadata_size(root, |
| 4001 | num_csums - old_csums); |
| 4002 | |
| 4003 | return btrfs_calc_trans_metadata_size(root, old_csums - num_csums); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 4007 | { |
| 4008 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4009 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4010 | u64 to_reserve = 0; |
| 4011 | unsigned nr_extents = 0; |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 4012 | int flush = 1; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4013 | int ret; |
| 4014 | |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 4015 | if (btrfs_is_free_space_inode(root, inode)) |
| 4016 | flush = 0; |
| 4017 | |
| 4018 | if (flush && btrfs_transaction_in_commit(root->fs_info)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4019 | schedule_timeout(1); |
| 4020 | |
| 4021 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4022 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4023 | spin_lock(&BTRFS_I(inode)->lock); |
| 4024 | BTRFS_I(inode)->outstanding_extents++; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4025 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4026 | if (BTRFS_I(inode)->outstanding_extents > |
| 4027 | BTRFS_I(inode)->reserved_extents) { |
| 4028 | nr_extents = BTRFS_I(inode)->outstanding_extents - |
| 4029 | BTRFS_I(inode)->reserved_extents; |
| 4030 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 4031 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4032 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4033 | } |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4034 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4035 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4036 | |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 4037 | ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, flush); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4038 | if (ret) { |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4039 | u64 to_free = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4040 | unsigned dropped; |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4041 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4042 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4043 | dropped = drop_outstanding_extent(inode); |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4044 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4045 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4046 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
| 4047 | |
| 4048 | /* |
| 4049 | * Somebody could have come in and twiddled with the |
| 4050 | * reservation, so if we have to free more than we would have |
| 4051 | * reserved from this reservation go ahead and release those |
| 4052 | * bytes. |
| 4053 | */ |
| 4054 | to_free -= to_reserve; |
| 4055 | if (to_free) |
| 4056 | btrfs_block_rsv_release(root, block_rsv, to_free); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4057 | return ret; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4058 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4059 | |
| 4060 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 4061 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4062 | return 0; |
| 4063 | } |
| 4064 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4065 | /** |
| 4066 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 4067 | * @inode: the inode to release the reservation for |
| 4068 | * @num_bytes: the number of bytes we're releasing |
| 4069 | * |
| 4070 | * This will release the metadata reservation for an inode. This can be called |
| 4071 | * once we complete IO for a given set of bytes to release their metadata |
| 4072 | * reservations. |
| 4073 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4074 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 4075 | { |
| 4076 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4077 | u64 to_free = 0; |
| 4078 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4079 | |
| 4080 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4081 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4082 | dropped = drop_outstanding_extent(inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4083 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4084 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
| 4085 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4086 | if (dropped > 0) |
| 4087 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4088 | |
| 4089 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 4090 | to_free); |
| 4091 | } |
| 4092 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4093 | /** |
| 4094 | * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc |
| 4095 | * @inode: inode we're writing to |
| 4096 | * @num_bytes: the number of bytes we want to allocate |
| 4097 | * |
| 4098 | * This will do the following things |
| 4099 | * |
| 4100 | * o reserve space in the data space info for num_bytes |
| 4101 | * o reserve space in the metadata space info based on number of outstanding |
| 4102 | * extents and how much csums will be needed |
| 4103 | * o add to the inodes ->delalloc_bytes |
| 4104 | * o add it to the fs_info's delalloc inodes list. |
| 4105 | * |
| 4106 | * This will return 0 for success and -ENOSPC if there is no space left. |
| 4107 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4108 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 4109 | { |
| 4110 | int ret; |
| 4111 | |
| 4112 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 4113 | if (ret) |
| 4114 | return ret; |
| 4115 | |
| 4116 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 4117 | if (ret) { |
| 4118 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4119 | return ret; |
| 4120 | } |
| 4121 | |
| 4122 | return 0; |
| 4123 | } |
| 4124 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4125 | /** |
| 4126 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
| 4127 | * @inode: inode we're releasing space for |
| 4128 | * @num_bytes: the number of bytes we want to free up |
| 4129 | * |
| 4130 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 4131 | * called in the case that we don't need the metadata AND data reservations |
| 4132 | * anymore. So if there is an error or we insert an inline extent. |
| 4133 | * |
| 4134 | * This function will release the metadata space that was not used and will |
| 4135 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 4136 | * list if there are no delalloc bytes left. |
| 4137 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4138 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 4139 | { |
| 4140 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 4141 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4142 | } |
| 4143 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4144 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 4145 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4146 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4147 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4148 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4149 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4150 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4151 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4152 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4153 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 4154 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4155 | /* block accounting for super block */ |
| 4156 | spin_lock(&info->delalloc_lock); |
| 4157 | old_val = btrfs_super_bytes_used(&info->super_copy); |
| 4158 | if (alloc) |
| 4159 | old_val += num_bytes; |
| 4160 | else |
| 4161 | old_val -= num_bytes; |
| 4162 | btrfs_set_super_bytes_used(&info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4163 | spin_unlock(&info->delalloc_lock); |
| 4164 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4165 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4166 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4167 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4168 | return -1; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4169 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 4170 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4171 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4172 | factor = 2; |
| 4173 | else |
| 4174 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4175 | /* |
| 4176 | * If this block group has free space cache written out, we |
| 4177 | * need to make sure to load it if we are removing space. This |
| 4178 | * is because we need the unpinning stage to actually add the |
| 4179 | * space back to the block group, otherwise we will leak space. |
| 4180 | */ |
| 4181 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4182 | cache_block_group(cache, trans, NULL, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4183 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4184 | byte_in_group = bytenr - cache->key.objectid; |
| 4185 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4186 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4187 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4188 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4189 | |
| 4190 | if (btrfs_super_cache_generation(&info->super_copy) != 0 && |
| 4191 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 4192 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 4193 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4194 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4195 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4196 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4197 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4198 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4199 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4200 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4201 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4202 | cache->space_info->bytes_used += num_bytes; |
| 4203 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4204 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4205 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4206 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4207 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4208 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4209 | cache->pinned += num_bytes; |
| 4210 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4211 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4212 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4213 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4214 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4215 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4216 | set_extent_dirty(info->pinned_extents, |
| 4217 | bytenr, bytenr + num_bytes - 1, |
| 4218 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4219 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4220 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4221 | total -= num_bytes; |
| 4222 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4223 | } |
| 4224 | return 0; |
| 4225 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4226 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4227 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 4228 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4229 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4230 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4231 | |
| 4232 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 4233 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4234 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4235 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4236 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4237 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4238 | |
| 4239 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4240 | } |
| 4241 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4242 | static int pin_down_extent(struct btrfs_root *root, |
| 4243 | struct btrfs_block_group_cache *cache, |
| 4244 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4245 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4246 | spin_lock(&cache->space_info->lock); |
| 4247 | spin_lock(&cache->lock); |
| 4248 | cache->pinned += num_bytes; |
| 4249 | cache->space_info->bytes_pinned += num_bytes; |
| 4250 | if (reserved) { |
| 4251 | cache->reserved -= num_bytes; |
| 4252 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4253 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4254 | spin_unlock(&cache->lock); |
| 4255 | spin_unlock(&cache->space_info->lock); |
| 4256 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4257 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 4258 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4259 | return 0; |
| 4260 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4261 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4262 | /* |
| 4263 | * this function must be called within transaction |
| 4264 | */ |
| 4265 | int btrfs_pin_extent(struct btrfs_root *root, |
| 4266 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4267 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4268 | struct btrfs_block_group_cache *cache; |
| 4269 | |
| 4270 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 4271 | BUG_ON(!cache); |
| 4272 | |
| 4273 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 4274 | |
| 4275 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4276 | return 0; |
| 4277 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4278 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4279 | /** |
| 4280 | * btrfs_update_reserved_bytes - update the block_group and space info counters |
| 4281 | * @cache: The cache we are manipulating |
| 4282 | * @num_bytes: The number of bytes in question |
| 4283 | * @reserve: One of the reservation enums |
| 4284 | * |
| 4285 | * This is called by the allocator when it reserves space, or by somebody who is |
| 4286 | * freeing space that was never actually used on disk. For example if you |
| 4287 | * reserve some space for a new leaf in transaction A and before transaction A |
| 4288 | * commits you free that leaf, you call this with reserve set to 0 in order to |
| 4289 | * clear the reservation. |
| 4290 | * |
| 4291 | * Metadata reservations should be called with RESERVE_ALLOC so we do the proper |
| 4292 | * ENOSPC accounting. For data we handle the reservation through clearing the |
| 4293 | * delalloc bits in the io_tree. We have to do this since we could end up |
| 4294 | * allocating less disk space for the amount of data we have reserved in the |
| 4295 | * case of compression. |
| 4296 | * |
| 4297 | * If this is a reservation and the block group has become read only we cannot |
| 4298 | * make the reservation and return -EAGAIN, otherwise this function always |
| 4299 | * succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4300 | */ |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4301 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4302 | u64 num_bytes, int reserve) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4303 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4304 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4305 | int ret = 0; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4306 | spin_lock(&space_info->lock); |
| 4307 | spin_lock(&cache->lock); |
| 4308 | if (reserve != RESERVE_FREE) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4309 | if (cache->ro) { |
| 4310 | ret = -EAGAIN; |
| 4311 | } else { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4312 | cache->reserved += num_bytes; |
| 4313 | space_info->bytes_reserved += num_bytes; |
| 4314 | if (reserve == RESERVE_ALLOC) { |
| 4315 | BUG_ON(space_info->bytes_may_use < num_bytes); |
| 4316 | space_info->bytes_may_use -= num_bytes; |
| 4317 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4318 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4319 | } else { |
| 4320 | if (cache->ro) |
| 4321 | space_info->bytes_readonly += num_bytes; |
| 4322 | cache->reserved -= num_bytes; |
| 4323 | space_info->bytes_reserved -= num_bytes; |
| 4324 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4325 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4326 | spin_unlock(&cache->lock); |
| 4327 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4328 | return ret; |
| 4329 | } |
| 4330 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4331 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 4332 | struct btrfs_root *root) |
| 4333 | { |
| 4334 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4335 | struct btrfs_caching_control *next; |
| 4336 | struct btrfs_caching_control *caching_ctl; |
| 4337 | struct btrfs_block_group_cache *cache; |
| 4338 | |
| 4339 | down_write(&fs_info->extent_commit_sem); |
| 4340 | |
| 4341 | list_for_each_entry_safe(caching_ctl, next, |
| 4342 | &fs_info->caching_block_groups, list) { |
| 4343 | cache = caching_ctl->block_group; |
| 4344 | if (block_group_cache_done(cache)) { |
| 4345 | cache->last_byte_to_unpin = (u64)-1; |
| 4346 | list_del_init(&caching_ctl->list); |
| 4347 | put_caching_control(caching_ctl); |
| 4348 | } else { |
| 4349 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4350 | } |
| 4351 | } |
| 4352 | |
| 4353 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4354 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4355 | else |
| 4356 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4357 | |
| 4358 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4359 | |
| 4360 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4361 | return 0; |
| 4362 | } |
| 4363 | |
| 4364 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 4365 | { |
| 4366 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4367 | struct btrfs_block_group_cache *cache = NULL; |
| 4368 | u64 len; |
| 4369 | |
| 4370 | while (start <= end) { |
| 4371 | if (!cache || |
| 4372 | start >= cache->key.objectid + cache->key.offset) { |
| 4373 | if (cache) |
| 4374 | btrfs_put_block_group(cache); |
| 4375 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4376 | BUG_ON(!cache); |
| 4377 | } |
| 4378 | |
| 4379 | len = cache->key.objectid + cache->key.offset - start; |
| 4380 | len = min(len, end + 1 - start); |
| 4381 | |
| 4382 | if (start < cache->last_byte_to_unpin) { |
| 4383 | len = min(len, cache->last_byte_to_unpin - start); |
| 4384 | btrfs_add_free_space(cache, start, len); |
| 4385 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4386 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4387 | start += len; |
| 4388 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4389 | spin_lock(&cache->space_info->lock); |
| 4390 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4391 | cache->pinned -= len; |
| 4392 | cache->space_info->bytes_pinned -= len; |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4393 | if (cache->ro) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4394 | cache->space_info->bytes_readonly += len; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4395 | spin_unlock(&cache->lock); |
| 4396 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4400 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4401 | return 0; |
| 4402 | } |
| 4403 | |
| 4404 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4405 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4406 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4407 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4408 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4409 | u64 start; |
| 4410 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4411 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4412 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4413 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4414 | unpin = &fs_info->freed_extents[1]; |
| 4415 | else |
| 4416 | unpin = &fs_info->freed_extents[0]; |
| 4417 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4418 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4419 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4420 | EXTENT_DIRTY); |
| 4421 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4422 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4423 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 4424 | if (btrfs_test_opt(root, DISCARD)) |
| 4425 | ret = btrfs_discard_extent(root, start, |
| 4426 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4427 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4428 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4429 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4430 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4431 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4432 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4433 | return 0; |
| 4434 | } |
| 4435 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4436 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4437 | struct btrfs_root *root, |
| 4438 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4439 | u64 root_objectid, u64 owner_objectid, |
| 4440 | u64 owner_offset, int refs_to_drop, |
| 4441 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4442 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4443 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4444 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4445 | struct btrfs_fs_info *info = root->fs_info; |
| 4446 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4447 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4448 | struct btrfs_extent_item *ei; |
| 4449 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4450 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4451 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4452 | int extent_slot = 0; |
| 4453 | int found_extent = 0; |
| 4454 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4455 | u32 item_size; |
| 4456 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4457 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4458 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4459 | if (!path) |
| 4460 | return -ENOMEM; |
| 4461 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4462 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4463 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4464 | |
| 4465 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4466 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4467 | |
| 4468 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4469 | bytenr, num_bytes, parent, |
| 4470 | root_objectid, owner_objectid, |
| 4471 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4472 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4473 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4474 | while (extent_slot >= 0) { |
| 4475 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4476 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4477 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4478 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4479 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4480 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4481 | found_extent = 1; |
| 4482 | break; |
| 4483 | } |
| 4484 | if (path->slots[0] - extent_slot > 5) |
| 4485 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4486 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4487 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4488 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4489 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4490 | if (found_extent && item_size < sizeof(*ei)) |
| 4491 | found_extent = 0; |
| 4492 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4493 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4494 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4495 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4496 | NULL, refs_to_drop, |
| 4497 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4498 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4499 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4500 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4501 | |
| 4502 | key.objectid = bytenr; |
| 4503 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4504 | key.offset = num_bytes; |
| 4505 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4506 | ret = btrfs_search_slot(trans, extent_root, |
| 4507 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4508 | if (ret) { |
| 4509 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4510 | ", was looking for %llu\n", ret, |
| 4511 | (unsigned long long)bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 4512 | if (ret > 0) |
| 4513 | btrfs_print_leaf(extent_root, |
| 4514 | path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4515 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4516 | BUG_ON(ret); |
| 4517 | extent_slot = path->slots[0]; |
| 4518 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4519 | } else { |
| 4520 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4521 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4522 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4523 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4524 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4525 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4526 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4527 | (unsigned long long)owner_objectid, |
| 4528 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4529 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4530 | |
| 4531 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4532 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4533 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4534 | if (item_size < sizeof(*ei)) { |
| 4535 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4536 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4537 | owner_objectid, 0); |
| 4538 | BUG_ON(ret < 0); |
| 4539 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4540 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4541 | path->leave_spinning = 1; |
| 4542 | |
| 4543 | key.objectid = bytenr; |
| 4544 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4545 | key.offset = num_bytes; |
| 4546 | |
| 4547 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4548 | -1, 1); |
| 4549 | if (ret) { |
| 4550 | printk(KERN_ERR "umm, got %d back from search" |
| 4551 | ", was looking for %llu\n", ret, |
| 4552 | (unsigned long long)bytenr); |
| 4553 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4554 | } |
| 4555 | BUG_ON(ret); |
| 4556 | extent_slot = path->slots[0]; |
| 4557 | leaf = path->nodes[0]; |
| 4558 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4559 | } |
| 4560 | #endif |
| 4561 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4562 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4563 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4564 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4565 | struct btrfs_tree_block_info *bi; |
| 4566 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4567 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4568 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4569 | } |
| 4570 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4571 | refs = btrfs_extent_refs(leaf, ei); |
| 4572 | BUG_ON(refs < refs_to_drop); |
| 4573 | refs -= refs_to_drop; |
| 4574 | |
| 4575 | if (refs > 0) { |
| 4576 | if (extent_op) |
| 4577 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4578 | /* |
| 4579 | * In the case of inline back ref, reference count will |
| 4580 | * be updated by remove_extent_backref |
| 4581 | */ |
| 4582 | if (iref) { |
| 4583 | BUG_ON(!found_extent); |
| 4584 | } else { |
| 4585 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4586 | btrfs_mark_buffer_dirty(leaf); |
| 4587 | } |
| 4588 | if (found_extent) { |
| 4589 | ret = remove_extent_backref(trans, extent_root, path, |
| 4590 | iref, refs_to_drop, |
| 4591 | is_data); |
| 4592 | BUG_ON(ret); |
| 4593 | } |
| 4594 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4595 | if (found_extent) { |
| 4596 | BUG_ON(is_data && refs_to_drop != |
| 4597 | extent_data_ref_count(root, path, iref)); |
| 4598 | if (iref) { |
| 4599 | BUG_ON(path->slots[0] != extent_slot); |
| 4600 | } else { |
| 4601 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4602 | path->slots[0] = extent_slot; |
| 4603 | num_to_del = 2; |
| 4604 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4605 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4606 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4607 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4608 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4609 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4610 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4611 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4612 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4613 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4614 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4615 | } else { |
| 4616 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4617 | bytenr >> PAGE_CACHE_SHIFT, |
| 4618 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4619 | } |
| 4620 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4621 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4622 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4623 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4624 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4625 | return ret; |
| 4626 | } |
| 4627 | |
| 4628 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4629 | * 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] | 4630 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4631 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4632 | * removes it from the tree. |
| 4633 | */ |
| 4634 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4635 | struct btrfs_root *root, u64 bytenr) |
| 4636 | { |
| 4637 | struct btrfs_delayed_ref_head *head; |
| 4638 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4639 | struct btrfs_delayed_ref_node *ref; |
| 4640 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4641 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4642 | |
| 4643 | delayed_refs = &trans->transaction->delayed_refs; |
| 4644 | spin_lock(&delayed_refs->lock); |
| 4645 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4646 | if (!head) |
| 4647 | goto out; |
| 4648 | |
| 4649 | node = rb_prev(&head->node.rb_node); |
| 4650 | if (!node) |
| 4651 | goto out; |
| 4652 | |
| 4653 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4654 | |
| 4655 | /* there are still entries for this ref, we can't drop it */ |
| 4656 | if (ref->bytenr == bytenr) |
| 4657 | goto out; |
| 4658 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4659 | if (head->extent_op) { |
| 4660 | if (!head->must_insert_reserved) |
| 4661 | goto out; |
| 4662 | kfree(head->extent_op); |
| 4663 | head->extent_op = NULL; |
| 4664 | } |
| 4665 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4666 | /* |
| 4667 | * waiting for the lock here would deadlock. If someone else has it |
| 4668 | * locked they are already in the process of dropping it anyway |
| 4669 | */ |
| 4670 | if (!mutex_trylock(&head->mutex)) |
| 4671 | goto out; |
| 4672 | |
| 4673 | /* |
| 4674 | * at this point we have a head with no other entries. Go |
| 4675 | * ahead and process it. |
| 4676 | */ |
| 4677 | head->node.in_tree = 0; |
| 4678 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4679 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4680 | delayed_refs->num_entries--; |
| 4681 | |
| 4682 | /* |
| 4683 | * we don't take a ref on the node because we're removing it from the |
| 4684 | * tree, so we just steal the ref the tree was holding. |
| 4685 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4686 | delayed_refs->num_heads--; |
| 4687 | if (list_empty(&head->cluster)) |
| 4688 | delayed_refs->num_heads_ready--; |
| 4689 | |
| 4690 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4691 | spin_unlock(&delayed_refs->lock); |
| 4692 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4693 | BUG_ON(head->extent_op); |
| 4694 | if (head->must_insert_reserved) |
| 4695 | ret = 1; |
| 4696 | |
| 4697 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4698 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4699 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4700 | out: |
| 4701 | spin_unlock(&delayed_refs->lock); |
| 4702 | return 0; |
| 4703 | } |
| 4704 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4705 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4706 | struct btrfs_root *root, |
| 4707 | struct extent_buffer *buf, |
| 4708 | u64 parent, int last_ref) |
| 4709 | { |
| 4710 | struct btrfs_block_rsv *block_rsv; |
| 4711 | struct btrfs_block_group_cache *cache = NULL; |
| 4712 | int ret; |
| 4713 | |
| 4714 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4715 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4716 | parent, root->root_key.objectid, |
| 4717 | btrfs_header_level(buf), |
| 4718 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4719 | BUG_ON(ret); |
| 4720 | } |
| 4721 | |
| 4722 | if (!last_ref) |
| 4723 | return; |
| 4724 | |
| 4725 | block_rsv = get_block_rsv(trans, root); |
| 4726 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | 3bf84a5 | 2010-05-31 09:04:46 +0000 | [diff] [blame] | 4727 | if (block_rsv->space_info != cache->space_info) |
| 4728 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4729 | |
| 4730 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4731 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4732 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4733 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4734 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4738 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4739 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4743 | |
| 4744 | btrfs_add_free_space(cache, buf->start, buf->len); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4745 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4746 | } |
| 4747 | out: |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 4748 | /* |
| 4749 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 4750 | * anymore. |
| 4751 | */ |
| 4752 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4753 | btrfs_put_block_group(cache); |
| 4754 | } |
| 4755 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4756 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4757 | struct btrfs_root *root, |
| 4758 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4759 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4760 | { |
| 4761 | int ret; |
| 4762 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4763 | /* |
| 4764 | * tree log blocks never actually go into the extent allocation |
| 4765 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4766 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4767 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4768 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4769 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4770 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4771 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4772 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4773 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4774 | parent, root_objectid, (int)owner, |
| 4775 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4776 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4777 | } else { |
| 4778 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4779 | parent, root_objectid, owner, |
| 4780 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4781 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4782 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4783 | return ret; |
| 4784 | } |
| 4785 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4786 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4787 | { |
| 4788 | u64 mask = ((u64)root->stripesize - 1); |
| 4789 | u64 ret = (val + mask) & ~mask; |
| 4790 | return ret; |
| 4791 | } |
| 4792 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4793 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4794 | * when we wait for progress in the block group caching, its because |
| 4795 | * our allocation attempt failed at least once. So, we must sleep |
| 4796 | * and let some progress happen before we try again. |
| 4797 | * |
| 4798 | * This function will sleep at least once waiting for new free space to |
| 4799 | * show up, and then it will check the block group free space numbers |
| 4800 | * for our min num_bytes. Another option is to have it go ahead |
| 4801 | * and look in the rbtree for a free extent of a given size, but this |
| 4802 | * is a good start. |
| 4803 | */ |
| 4804 | static noinline int |
| 4805 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4806 | u64 num_bytes) |
| 4807 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4808 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4809 | DEFINE_WAIT(wait); |
| 4810 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4811 | caching_ctl = get_caching_control(cache); |
| 4812 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4813 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4814 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4815 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 4816 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4817 | |
| 4818 | put_caching_control(caching_ctl); |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
| 4822 | static noinline int |
| 4823 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4824 | { |
| 4825 | struct btrfs_caching_control *caching_ctl; |
| 4826 | DEFINE_WAIT(wait); |
| 4827 | |
| 4828 | caching_ctl = get_caching_control(cache); |
| 4829 | if (!caching_ctl) |
| 4830 | return 0; |
| 4831 | |
| 4832 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4833 | |
| 4834 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4835 | return 0; |
| 4836 | } |
| 4837 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4838 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4839 | { |
| 4840 | int index; |
| 4841 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4842 | index = 0; |
| 4843 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4844 | index = 1; |
| 4845 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4846 | index = 2; |
| 4847 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4848 | index = 3; |
| 4849 | else |
| 4850 | index = 4; |
| 4851 | return index; |
| 4852 | } |
| 4853 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4854 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4855 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4856 | LOOP_CACHING_NOWAIT = 1, |
| 4857 | LOOP_CACHING_WAIT = 2, |
| 4858 | LOOP_ALLOC_CHUNK = 3, |
| 4859 | LOOP_NO_EMPTY_SIZE = 4, |
| 4860 | }; |
| 4861 | |
| 4862 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4863 | * walks the btree of allocated extents and find a hole of a given size. |
| 4864 | * The key ins is changed to record the hole: |
| 4865 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4866 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4867 | * ins->offset == number of blocks |
| 4868 | * Any available blocks before search_start are skipped. |
| 4869 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4870 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 4871 | struct btrfs_root *orig_root, |
| 4872 | u64 num_bytes, u64 empty_size, |
| 4873 | u64 search_start, u64 search_end, |
| 4874 | u64 hint_byte, struct btrfs_key *ins, |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 4875 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4876 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4877 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4878 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4879 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4880 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4881 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4882 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4883 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4884 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4885 | int last_ptr_loop = 0; |
| 4886 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4887 | int index = 0; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4888 | int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ? |
| 4889 | RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4890 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 4891 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 4892 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4893 | bool use_cluster = true; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4894 | u64 ideal_cache_percent = 0; |
| 4895 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4896 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4897 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4898 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4899 | ins->objectid = 0; |
| 4900 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 4901 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4902 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4903 | if (!space_info) { |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 4904 | printk(KERN_ERR "No space info for %llu\n", data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 4905 | return -ENOSPC; |
| 4906 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4907 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4908 | /* |
| 4909 | * If the space info is for both data and metadata it means we have a |
| 4910 | * small filesystem and we can't use the clustering stuff. |
| 4911 | */ |
| 4912 | if (btrfs_mixed_space_info(space_info)) |
| 4913 | use_cluster = false; |
| 4914 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 4915 | if (orig_root->ref_cows || empty_size) |
| 4916 | allowed_chunk_alloc = 1; |
| 4917 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4918 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4919 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 4920 | if (!btrfs_test_opt(root, SSD)) |
| 4921 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4922 | } |
| 4923 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 4924 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
| 4925 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4926 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 4927 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4928 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4929 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4930 | spin_lock(&last_ptr->lock); |
| 4931 | if (last_ptr->block_group) |
| 4932 | hint_byte = last_ptr->window_start; |
| 4933 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4934 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4935 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4936 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 4937 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 4938 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4939 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4940 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4941 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4942 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4943 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4944 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 4945 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4946 | /* |
| 4947 | * we don't want to use the block group if it doesn't match our |
| 4948 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4949 | * |
| 4950 | * However if we are re-searching with an ideal block group |
| 4951 | * 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] | 4952 | */ |
| 4953 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4954 | (block_group->cached != BTRFS_CACHE_NO || |
| 4955 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4956 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4957 | if (list_empty(&block_group->list) || |
| 4958 | block_group->ro) { |
| 4959 | /* |
| 4960 | * someone is removing this block group, |
| 4961 | * we can't jump into the have_block_group |
| 4962 | * target because our list pointers are not |
| 4963 | * valid |
| 4964 | */ |
| 4965 | btrfs_put_block_group(block_group); |
| 4966 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4967 | } else { |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4968 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 4969 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4970 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4971 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4972 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4973 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4974 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4975 | search: |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4976 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4977 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 4978 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 4979 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4980 | int cached; |
Chris Mason | 8a1413a2 | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 4981 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 4982 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 4983 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 4984 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 4985 | /* |
| 4986 | * this can happen if we end up cycling through all the |
| 4987 | * raid types, but we want to make sure we only allocate |
| 4988 | * for the proper type. |
| 4989 | */ |
| 4990 | if (!block_group_bits(block_group, data)) { |
| 4991 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 4992 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4993 | BTRFS_BLOCK_GROUP_RAID10; |
| 4994 | |
| 4995 | /* |
| 4996 | * if they asked for extra copies and this block group |
| 4997 | * doesn't provide them, bail. This does allow us to |
| 4998 | * fill raid0 from raid1. |
| 4999 | */ |
| 5000 | if ((data & extra) && !(block_group->flags & extra)) |
| 5001 | goto loop; |
| 5002 | } |
| 5003 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5004 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5005 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5006 | u64 free_percent; |
| 5007 | |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5008 | ret = cache_block_group(block_group, trans, |
| 5009 | orig_root, 1); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 5010 | if (block_group->cached == BTRFS_CACHE_FINISHED) |
| 5011 | goto have_block_group; |
| 5012 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5013 | free_percent = btrfs_block_group_used(&block_group->item); |
| 5014 | free_percent *= 100; |
| 5015 | free_percent = div64_u64(free_percent, |
| 5016 | block_group->key.offset); |
| 5017 | free_percent = 100 - free_percent; |
| 5018 | if (free_percent > ideal_cache_percent && |
| 5019 | likely(!block_group->ro)) { |
| 5020 | ideal_cache_offset = block_group->key.objectid; |
| 5021 | ideal_cache_percent = free_percent; |
| 5022 | } |
| 5023 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5024 | /* |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5025 | * The caching workers are limited to 2 threads, so we |
| 5026 | * can queue as much work as we care to. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5027 | */ |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5028 | if (loop > LOOP_FIND_IDEAL) { |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5029 | ret = cache_block_group(block_group, trans, |
| 5030 | orig_root, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5031 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5032 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5033 | found_uncached_bg = true; |
| 5034 | |
| 5035 | /* |
| 5036 | * If loop is set for cached only, try the next block |
| 5037 | * group. |
| 5038 | */ |
| 5039 | if (loop == LOOP_FIND_IDEAL) |
| 5040 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5041 | } |
| 5042 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5043 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5044 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5045 | found_uncached_bg = true; |
| 5046 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5047 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5048 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5049 | |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5050 | spin_lock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5051 | if (cached && |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5052 | block_group->free_space_ctl->free_space < |
| 5053 | num_bytes + empty_size) { |
| 5054 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5055 | goto loop; |
| 5056 | } |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5057 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5058 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5059 | /* |
| 5060 | * Ok we want to try and use the cluster allocator, so lets look |
| 5061 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 5062 | * have tried the cluster allocator plenty of times at this |
| 5063 | * point and not have found anything, so we are likely way too |
| 5064 | * fragmented for the clustering stuff to find anything, so lets |
| 5065 | * just skip it and let the allocator find whatever block it can |
| 5066 | * find |
| 5067 | */ |
| 5068 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5069 | /* |
| 5070 | * the refill lock keeps out other |
| 5071 | * people trying to start a new cluster |
| 5072 | */ |
| 5073 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5074 | if (last_ptr->block_group && |
| 5075 | (last_ptr->block_group->ro || |
| 5076 | !block_group_bits(last_ptr->block_group, data))) { |
| 5077 | offset = 0; |
| 5078 | goto refill_cluster; |
| 5079 | } |
| 5080 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5081 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 5082 | num_bytes, search_start); |
| 5083 | if (offset) { |
| 5084 | /* we have a block, we're done */ |
| 5085 | spin_unlock(&last_ptr->refill_lock); |
| 5086 | goto checks; |
| 5087 | } |
| 5088 | |
| 5089 | spin_lock(&last_ptr->lock); |
| 5090 | /* |
| 5091 | * whoops, this cluster doesn't actually point to |
| 5092 | * this block group. Get a ref on the block |
| 5093 | * group is does point to and try again |
| 5094 | */ |
| 5095 | if (!last_ptr_loop && last_ptr->block_group && |
liubo | ff1f2b4 | 2011-07-27 09:49:18 +0000 | [diff] [blame] | 5096 | last_ptr->block_group != block_group && |
| 5097 | index <= |
| 5098 | get_block_group_index(last_ptr->block_group)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5099 | |
| 5100 | btrfs_put_block_group(block_group); |
| 5101 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5102 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5103 | spin_unlock(&last_ptr->lock); |
| 5104 | spin_unlock(&last_ptr->refill_lock); |
| 5105 | |
| 5106 | last_ptr_loop = 1; |
| 5107 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5108 | /* |
| 5109 | * we know this block group is properly |
| 5110 | * in the list because |
| 5111 | * btrfs_remove_block_group, drops the |
| 5112 | * cluster before it removes the block |
| 5113 | * group from the list |
| 5114 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5115 | goto have_block_group; |
| 5116 | } |
| 5117 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5118 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5119 | /* |
| 5120 | * this cluster didn't work out, free it and |
| 5121 | * start over |
| 5122 | */ |
| 5123 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5124 | |
| 5125 | last_ptr_loop = 0; |
| 5126 | |
| 5127 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 5128 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5129 | block_group, last_ptr, |
| 5130 | offset, num_bytes, |
| 5131 | empty_cluster + empty_size); |
| 5132 | if (ret == 0) { |
| 5133 | /* |
| 5134 | * now pull our allocation out of this |
| 5135 | * cluster |
| 5136 | */ |
| 5137 | offset = btrfs_alloc_from_cluster(block_group, |
| 5138 | last_ptr, num_bytes, |
| 5139 | search_start); |
| 5140 | if (offset) { |
| 5141 | /* we found one, proceed */ |
| 5142 | spin_unlock(&last_ptr->refill_lock); |
| 5143 | goto checks; |
| 5144 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5145 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 5146 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5147 | spin_unlock(&last_ptr->refill_lock); |
| 5148 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5149 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5150 | wait_block_group_cache_progress(block_group, |
| 5151 | num_bytes + empty_cluster + empty_size); |
| 5152 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5153 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5154 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5155 | /* |
| 5156 | * at this point we either didn't find a cluster |
| 5157 | * or we weren't able to allocate a block from our |
| 5158 | * cluster. Free the cluster we've been trying |
| 5159 | * to use, and go to the next block group |
| 5160 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5161 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5162 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5163 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5164 | } |
| 5165 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5166 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 5167 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5168 | /* |
| 5169 | * If we didn't find a chunk, and we haven't failed on this |
| 5170 | * block group before, and this block group is in the middle of |
| 5171 | * caching and we are ok with waiting, then go ahead and wait |
| 5172 | * for progress to be made, and set failed_alloc to true. |
| 5173 | * |
| 5174 | * If failed_alloc is true then we've already waited on this |
| 5175 | * block group once and should move on to the next block group. |
| 5176 | */ |
| 5177 | if (!offset && !failed_alloc && !cached && |
| 5178 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5179 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5180 | num_bytes + empty_size); |
| 5181 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5182 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5183 | } else if (!offset) { |
| 5184 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5185 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5186 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5187 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5188 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5189 | if (search_start + num_bytes >= search_end) { |
| 5190 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5191 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5192 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 5193 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5194 | /* move on to the next group */ |
| 5195 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5196 | block_group->key.objectid + block_group->key.offset) { |
| 5197 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5198 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5199 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5200 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5201 | ins->objectid = search_start; |
| 5202 | ins->offset = num_bytes; |
| 5203 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5204 | if (offset < search_start) |
| 5205 | btrfs_add_free_space(block_group, offset, |
| 5206 | search_start - offset); |
| 5207 | BUG_ON(offset > search_start); |
| 5208 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5209 | ret = btrfs_update_reserved_bytes(block_group, num_bytes, |
| 5210 | alloc_type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5211 | if (ret == -EAGAIN) { |
| 5212 | btrfs_add_free_space(block_group, offset, num_bytes); |
| 5213 | goto loop; |
| 5214 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5215 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5216 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5217 | ins->objectid = search_start; |
| 5218 | ins->offset = num_bytes; |
| 5219 | |
| 5220 | if (offset < search_start) |
| 5221 | btrfs_add_free_space(block_group, offset, |
| 5222 | search_start - offset); |
| 5223 | BUG_ON(offset > search_start); |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5224 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5225 | break; |
| 5226 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5227 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5228 | failed_alloc = false; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5229 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5230 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5231 | } |
| 5232 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 5233 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5234 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 5235 | goto search; |
| 5236 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5237 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 5238 | * for them to make caching progress. Also |
| 5239 | * determine the best possible bg to cache |
| 5240 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 5241 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5242 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 5243 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 5244 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 5245 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5246 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5247 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5248 | index = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5249 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5250 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5251 | loop++; |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5252 | if (!ideal_cache_percent) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5253 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5254 | |
| 5255 | /* |
| 5256 | * 1 of the following 2 things have happened so far |
| 5257 | * |
| 5258 | * 1) We found an ideal block group for caching that |
| 5259 | * is mostly full and will cache quickly, so we might |
| 5260 | * as well wait for it. |
| 5261 | * |
| 5262 | * 2) We searched for cached only and we didn't find |
| 5263 | * anything, and we didn't start any caching kthreads |
| 5264 | * either, so chances are we will loop through and |
| 5265 | * start a couple caching kthreads, and then come back |
| 5266 | * around and just wait for them. This will be slower |
| 5267 | * because we will have 2 caching kthreads reading at |
| 5268 | * the same time when we could have just started one |
| 5269 | * and waited for it to get far enough to give us an |
| 5270 | * allocation, so go ahead and go to the wait caching |
| 5271 | * loop. |
| 5272 | */ |
| 5273 | loop = LOOP_CACHING_WAIT; |
| 5274 | search_start = ideal_cache_offset; |
| 5275 | ideal_cache_percent = 0; |
| 5276 | goto ideal_cache; |
| 5277 | } else if (loop == LOOP_FIND_IDEAL) { |
| 5278 | /* |
| 5279 | * Didn't find a uncached bg, wait on anything we find |
| 5280 | * next. |
| 5281 | */ |
| 5282 | loop = LOOP_CACHING_WAIT; |
| 5283 | goto search; |
| 5284 | } |
| 5285 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5286 | loop++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5287 | |
| 5288 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5289 | if (allowed_chunk_alloc) { |
| 5290 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 5291 | 2 * 1024 * 1024, data, |
| 5292 | CHUNK_ALLOC_LIMITED); |
| 5293 | allowed_chunk_alloc = 0; |
| 5294 | if (ret == 1) |
| 5295 | done_chunk_alloc = 1; |
| 5296 | } else if (!done_chunk_alloc && |
| 5297 | space_info->force_alloc == |
| 5298 | CHUNK_ALLOC_NO_FORCE) { |
| 5299 | space_info->force_alloc = CHUNK_ALLOC_LIMITED; |
| 5300 | } |
| 5301 | |
| 5302 | /* |
| 5303 | * We didn't allocate a chunk, go ahead and drop the |
| 5304 | * empty size and loop again. |
| 5305 | */ |
| 5306 | if (!done_chunk_alloc) |
| 5307 | loop = LOOP_NO_EMPTY_SIZE; |
| 5308 | } |
| 5309 | |
| 5310 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5311 | empty_size = 0; |
| 5312 | empty_cluster = 0; |
| 5313 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5314 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5315 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5316 | } else if (!ins->objectid) { |
| 5317 | ret = -ENOSPC; |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5318 | } else if (ins->objectid) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5319 | ret = 0; |
| 5320 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5321 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5322 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5323 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5324 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5325 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 5326 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5327 | { |
| 5328 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5329 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5330 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5331 | spin_lock(&info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5332 | printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n", |
| 5333 | (unsigned long long)info->flags, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5334 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5335 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5336 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5337 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5338 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 5339 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 5340 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5341 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5342 | (unsigned long long)info->bytes_pinned, |
| 5343 | (unsigned long long)info->bytes_reserved, |
| 5344 | (unsigned long long)info->bytes_may_use, |
| 5345 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5346 | spin_unlock(&info->lock); |
| 5347 | |
| 5348 | if (!dump_block_groups) |
| 5349 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5350 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5351 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5352 | again: |
| 5353 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5354 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5355 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 5356 | "%llu pinned %llu reserved\n", |
| 5357 | (unsigned long long)cache->key.objectid, |
| 5358 | (unsigned long long)cache->key.offset, |
| 5359 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 5360 | (unsigned long long)cache->pinned, |
| 5361 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5362 | btrfs_dump_free_space(cache, bytes); |
| 5363 | spin_unlock(&cache->lock); |
| 5364 | } |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5365 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5366 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5367 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5368 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5369 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5370 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5371 | struct btrfs_root *root, |
| 5372 | u64 num_bytes, u64 min_alloc_size, |
| 5373 | u64 empty_size, u64 hint_byte, |
| 5374 | u64 search_end, struct btrfs_key *ins, |
| 5375 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5376 | { |
| 5377 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5378 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5379 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5380 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5381 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5382 | /* |
| 5383 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5384 | * is not called recursively on allocations |
| 5385 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5386 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5387 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5388 | num_bytes + 2 * 1024 * 1024, data, |
| 5389 | CHUNK_ALLOC_NO_FORCE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5390 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5391 | WARN_ON(num_bytes < root->sectorsize); |
| 5392 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5393 | search_start, search_end, hint_byte, |
| 5394 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5395 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5396 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5397 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5398 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5399 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5400 | do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5401 | num_bytes, data, CHUNK_ALLOC_FORCE); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5402 | goto again; |
| 5403 | } |
Chris Mason | 9143565 | 2011-02-16 13:10:41 -0500 | [diff] [blame] | 5404 | if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5405 | struct btrfs_space_info *sinfo; |
| 5406 | |
| 5407 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5408 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5409 | "wanted %llu\n", (unsigned long long)data, |
| 5410 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5411 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5412 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5413 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5414 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
| 5415 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5416 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5417 | } |
| 5418 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5419 | int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) |
| 5420 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5421 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5422 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5423 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5424 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5425 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5426 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5427 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5428 | return -ENOSPC; |
| 5429 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5430 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5431 | if (btrfs_test_opt(root, DISCARD)) |
| 5432 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5433 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5434 | btrfs_add_free_space(cache, start, len); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5435 | btrfs_update_reserved_bytes(cache, len, RESERVE_FREE); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5436 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5437 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5438 | trace_btrfs_reserved_extent_free(root, start, len); |
| 5439 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5440 | return ret; |
| 5441 | } |
| 5442 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5443 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5444 | struct btrfs_root *root, |
| 5445 | u64 parent, u64 root_objectid, |
| 5446 | u64 flags, u64 owner, u64 offset, |
| 5447 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5448 | { |
| 5449 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5450 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5451 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5452 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5453 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5454 | struct extent_buffer *leaf; |
| 5455 | int type; |
| 5456 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5457 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5458 | if (parent > 0) |
| 5459 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5460 | else |
| 5461 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5462 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5463 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5464 | |
| 5465 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5466 | if (!path) |
| 5467 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5468 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5469 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5470 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5471 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5472 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5473 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5474 | leaf = path->nodes[0]; |
| 5475 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5476 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5477 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5478 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5479 | btrfs_set_extent_flags(leaf, extent_item, |
| 5480 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5481 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5482 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5483 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5484 | if (parent > 0) { |
| 5485 | struct btrfs_shared_data_ref *ref; |
| 5486 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5487 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5488 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5489 | } else { |
| 5490 | struct btrfs_extent_data_ref *ref; |
| 5491 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5492 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5493 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5494 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5495 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5496 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5497 | |
| 5498 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5499 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5500 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5501 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5502 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5503 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5504 | "%llu\n", (unsigned long long)ins->objectid, |
| 5505 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5506 | BUG(); |
| 5507 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5508 | return ret; |
| 5509 | } |
| 5510 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5511 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5512 | struct btrfs_root *root, |
| 5513 | u64 parent, u64 root_objectid, |
| 5514 | u64 flags, struct btrfs_disk_key *key, |
| 5515 | int level, struct btrfs_key *ins) |
| 5516 | { |
| 5517 | int ret; |
| 5518 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5519 | struct btrfs_extent_item *extent_item; |
| 5520 | struct btrfs_tree_block_info *block_info; |
| 5521 | struct btrfs_extent_inline_ref *iref; |
| 5522 | struct btrfs_path *path; |
| 5523 | struct extent_buffer *leaf; |
| 5524 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5525 | |
| 5526 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5527 | if (!path) |
| 5528 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5529 | |
| 5530 | path->leave_spinning = 1; |
| 5531 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5532 | ins, size); |
| 5533 | BUG_ON(ret); |
| 5534 | |
| 5535 | leaf = path->nodes[0]; |
| 5536 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5537 | struct btrfs_extent_item); |
| 5538 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5539 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5540 | btrfs_set_extent_flags(leaf, extent_item, |
| 5541 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5542 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5543 | |
| 5544 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5545 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5546 | |
| 5547 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5548 | if (parent > 0) { |
| 5549 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5550 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5551 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5552 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5553 | } else { |
| 5554 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5555 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5556 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5557 | } |
| 5558 | |
| 5559 | btrfs_mark_buffer_dirty(leaf); |
| 5560 | btrfs_free_path(path); |
| 5561 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5562 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5563 | if (ret) { |
| 5564 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5565 | "%llu\n", (unsigned long long)ins->objectid, |
| 5566 | (unsigned long long)ins->offset); |
| 5567 | BUG(); |
| 5568 | } |
| 5569 | return ret; |
| 5570 | } |
| 5571 | |
| 5572 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5573 | struct btrfs_root *root, |
| 5574 | u64 root_objectid, u64 owner, |
| 5575 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5576 | { |
| 5577 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5578 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5579 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5580 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5581 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5582 | 0, root_objectid, owner, offset, |
| 5583 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5584 | return ret; |
| 5585 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5586 | |
| 5587 | /* |
| 5588 | * this is used by the tree logging recovery code. It records that |
| 5589 | * an extent has been allocated and makes sure to clear the free |
| 5590 | * space cache bits as well |
| 5591 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5592 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5593 | struct btrfs_root *root, |
| 5594 | u64 root_objectid, u64 owner, u64 offset, |
| 5595 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5596 | { |
| 5597 | int ret; |
| 5598 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5599 | struct btrfs_caching_control *caching_ctl; |
| 5600 | u64 start = ins->objectid; |
| 5601 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5602 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5603 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5604 | cache_block_group(block_group, trans, NULL, 0); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5605 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5606 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5607 | if (!caching_ctl) { |
| 5608 | BUG_ON(!block_group_cache_done(block_group)); |
| 5609 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5610 | BUG_ON(ret); |
| 5611 | } else { |
| 5612 | mutex_lock(&caching_ctl->mutex); |
| 5613 | |
| 5614 | if (start >= caching_ctl->progress) { |
| 5615 | ret = add_excluded_extent(root, start, num_bytes); |
| 5616 | BUG_ON(ret); |
| 5617 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5618 | ret = btrfs_remove_free_space(block_group, |
| 5619 | start, num_bytes); |
| 5620 | BUG_ON(ret); |
| 5621 | } else { |
| 5622 | num_bytes = caching_ctl->progress - start; |
| 5623 | ret = btrfs_remove_free_space(block_group, |
| 5624 | start, num_bytes); |
| 5625 | BUG_ON(ret); |
| 5626 | |
| 5627 | start = caching_ctl->progress; |
| 5628 | num_bytes = ins->objectid + ins->offset - |
| 5629 | caching_ctl->progress; |
| 5630 | ret = add_excluded_extent(root, start, num_bytes); |
| 5631 | BUG_ON(ret); |
| 5632 | } |
| 5633 | |
| 5634 | mutex_unlock(&caching_ctl->mutex); |
| 5635 | put_caching_control(caching_ctl); |
| 5636 | } |
| 5637 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5638 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, |
| 5639 | RESERVE_ALLOC_NO_ACCOUNT); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5640 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5641 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5642 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5643 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5644 | return ret; |
| 5645 | } |
| 5646 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5647 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5648 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5649 | u64 bytenr, u32 blocksize, |
| 5650 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5651 | { |
| 5652 | struct extent_buffer *buf; |
| 5653 | |
| 5654 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5655 | if (!buf) |
| 5656 | return ERR_PTR(-ENOMEM); |
| 5657 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 5658 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5659 | btrfs_tree_lock(buf); |
| 5660 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5661 | |
| 5662 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5663 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5664 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5665 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5666 | /* |
| 5667 | * we allow two log transactions at a time, use different |
| 5668 | * EXENT bit to differentiate dirty pages. |
| 5669 | */ |
| 5670 | if (root->log_transid % 2 == 0) |
| 5671 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5672 | buf->start + buf->len - 1, GFP_NOFS); |
| 5673 | else |
| 5674 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5675 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5676 | } else { |
| 5677 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5678 | buf->start + buf->len - 1, GFP_NOFS); |
| 5679 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5680 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5681 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5682 | return buf; |
| 5683 | } |
| 5684 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5685 | static struct btrfs_block_rsv * |
| 5686 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5687 | struct btrfs_root *root, u32 blocksize) |
| 5688 | { |
| 5689 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5690 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5691 | int ret; |
| 5692 | |
| 5693 | block_rsv = get_block_rsv(trans, root); |
| 5694 | |
| 5695 | if (block_rsv->size == 0) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5696 | ret = reserve_metadata_bytes(trans, root, block_rsv, |
| 5697 | blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5698 | /* |
| 5699 | * If we couldn't reserve metadata bytes try and use some from |
| 5700 | * the global reserve. |
| 5701 | */ |
| 5702 | if (ret && block_rsv != global_rsv) { |
| 5703 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5704 | if (!ret) |
| 5705 | return global_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5706 | return ERR_PTR(ret); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5707 | } else if (ret) { |
| 5708 | return ERR_PTR(ret); |
| 5709 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5710 | return block_rsv; |
| 5711 | } |
| 5712 | |
| 5713 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5714 | if (!ret) |
| 5715 | return block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5716 | if (ret) { |
| 5717 | WARN_ON(1); |
| 5718 | ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize, |
| 5719 | 0); |
| 5720 | if (!ret) { |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5721 | return block_rsv; |
| 5722 | } else if (ret && block_rsv != global_rsv) { |
| 5723 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5724 | if (!ret) |
| 5725 | return global_rsv; |
| 5726 | } |
| 5727 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5728 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5729 | return ERR_PTR(-ENOSPC); |
| 5730 | } |
| 5731 | |
| 5732 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5733 | { |
| 5734 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5735 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5736 | } |
| 5737 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5738 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5739 | * finds a free extent and does all the dirty work required for allocation |
| 5740 | * returns the key for the extent through ins, and a tree buffer for |
| 5741 | * the first block of the extent through buf. |
| 5742 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5743 | * returns the tree buffer or NULL. |
| 5744 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5745 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5746 | struct btrfs_root *root, u32 blocksize, |
| 5747 | u64 parent, u64 root_objectid, |
| 5748 | struct btrfs_disk_key *key, int level, |
| 5749 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5750 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5751 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5752 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5753 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5754 | u64 flags = 0; |
| 5755 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5756 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5757 | |
| 5758 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5759 | if (IS_ERR(block_rsv)) |
| 5760 | return ERR_CAST(block_rsv); |
| 5761 | |
| 5762 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5763 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5764 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5765 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5766 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5767 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5768 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5769 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5770 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5771 | BUG_ON(IS_ERR(buf)); |
| 5772 | |
| 5773 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5774 | if (parent == 0) |
| 5775 | parent = ins.objectid; |
| 5776 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5777 | } else |
| 5778 | BUG_ON(parent > 0); |
| 5779 | |
| 5780 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5781 | struct btrfs_delayed_extent_op *extent_op; |
| 5782 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5783 | BUG_ON(!extent_op); |
| 5784 | if (key) |
| 5785 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5786 | else |
| 5787 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5788 | extent_op->flags_to_set = flags; |
| 5789 | extent_op->update_key = 1; |
| 5790 | extent_op->update_flags = 1; |
| 5791 | extent_op->is_data = 0; |
| 5792 | |
| 5793 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5794 | ins.offset, parent, root_objectid, |
| 5795 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5796 | extent_op); |
| 5797 | BUG_ON(ret); |
| 5798 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5799 | return buf; |
| 5800 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5801 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5802 | struct walk_control { |
| 5803 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5804 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5805 | struct btrfs_key update_progress; |
| 5806 | int stage; |
| 5807 | int level; |
| 5808 | int shared_level; |
| 5809 | int update_ref; |
| 5810 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5811 | int reada_slot; |
| 5812 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5813 | }; |
| 5814 | |
| 5815 | #define DROP_REFERENCE 1 |
| 5816 | #define UPDATE_BACKREF 2 |
| 5817 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5818 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5819 | struct btrfs_root *root, |
| 5820 | struct walk_control *wc, |
| 5821 | struct btrfs_path *path) |
| 5822 | { |
| 5823 | u64 bytenr; |
| 5824 | u64 generation; |
| 5825 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5826 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5827 | u32 nritems; |
| 5828 | u32 blocksize; |
| 5829 | struct btrfs_key key; |
| 5830 | struct extent_buffer *eb; |
| 5831 | int ret; |
| 5832 | int slot; |
| 5833 | int nread = 0; |
| 5834 | |
| 5835 | if (path->slots[wc->level] < wc->reada_slot) { |
| 5836 | wc->reada_count = wc->reada_count * 2 / 3; |
| 5837 | wc->reada_count = max(wc->reada_count, 2); |
| 5838 | } else { |
| 5839 | wc->reada_count = wc->reada_count * 3 / 2; |
| 5840 | wc->reada_count = min_t(int, wc->reada_count, |
| 5841 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 5842 | } |
| 5843 | |
| 5844 | eb = path->nodes[wc->level]; |
| 5845 | nritems = btrfs_header_nritems(eb); |
| 5846 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 5847 | |
| 5848 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 5849 | if (nread >= wc->reada_count) |
| 5850 | break; |
| 5851 | |
| 5852 | cond_resched(); |
| 5853 | bytenr = btrfs_node_blockptr(eb, slot); |
| 5854 | generation = btrfs_node_ptr_generation(eb, slot); |
| 5855 | |
| 5856 | if (slot == path->slots[wc->level]) |
| 5857 | goto reada; |
| 5858 | |
| 5859 | if (wc->stage == UPDATE_BACKREF && |
| 5860 | generation <= root->root_key.offset) |
| 5861 | continue; |
| 5862 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5863 | /* We don't lock the tree block, it's OK to be racy here */ |
| 5864 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 5865 | &refs, &flags); |
| 5866 | BUG_ON(ret); |
| 5867 | BUG_ON(refs == 0); |
| 5868 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5869 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5870 | if (refs == 1) |
| 5871 | goto reada; |
| 5872 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5873 | if (wc->level == 1 && |
| 5874 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5875 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5876 | if (!wc->update_ref || |
| 5877 | generation <= root->root_key.offset) |
| 5878 | continue; |
| 5879 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 5880 | ret = btrfs_comp_cpu_keys(&key, |
| 5881 | &wc->update_progress); |
| 5882 | if (ret < 0) |
| 5883 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5884 | } else { |
| 5885 | if (wc->level == 1 && |
| 5886 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 5887 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5888 | } |
| 5889 | reada: |
| 5890 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 5891 | generation); |
| 5892 | if (ret) |
| 5893 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5894 | nread++; |
| 5895 | } |
| 5896 | wc->reada_slot = slot; |
| 5897 | } |
| 5898 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 5899 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5900 | * hepler to process tree block while walking down the tree. |
| 5901 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5902 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 5903 | * back refs for pointers in the block. |
| 5904 | * |
| 5905 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5906 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5907 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 5908 | struct btrfs_root *root, |
| 5909 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5910 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5911 | { |
| 5912 | int level = wc->level; |
| 5913 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5914 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5915 | int ret; |
| 5916 | |
| 5917 | if (wc->stage == UPDATE_BACKREF && |
| 5918 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 5919 | return 1; |
| 5920 | |
| 5921 | /* |
| 5922 | * when reference count of tree block is 1, it won't increase |
| 5923 | * again. once full backref flag is set, we never clear it. |
| 5924 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5925 | if (lookup_info && |
| 5926 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 5927 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5928 | BUG_ON(!path->locks[level]); |
| 5929 | ret = btrfs_lookup_extent_info(trans, root, |
| 5930 | eb->start, eb->len, |
| 5931 | &wc->refs[level], |
| 5932 | &wc->flags[level]); |
| 5933 | BUG_ON(ret); |
| 5934 | BUG_ON(wc->refs[level] == 0); |
| 5935 | } |
| 5936 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5937 | if (wc->stage == DROP_REFERENCE) { |
| 5938 | if (wc->refs[level] > 1) |
| 5939 | return 1; |
| 5940 | |
| 5941 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5942 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5943 | path->locks[level] = 0; |
| 5944 | } |
| 5945 | return 0; |
| 5946 | } |
| 5947 | |
| 5948 | /* wc->stage == UPDATE_BACKREF */ |
| 5949 | if (!(wc->flags[level] & flag)) { |
| 5950 | BUG_ON(!path->locks[level]); |
| 5951 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 5952 | BUG_ON(ret); |
| 5953 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 5954 | BUG_ON(ret); |
| 5955 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 5956 | eb->len, flag, 0); |
| 5957 | BUG_ON(ret); |
| 5958 | wc->flags[level] |= flag; |
| 5959 | } |
| 5960 | |
| 5961 | /* |
| 5962 | * the block is shared by multiple trees, so it's not good to |
| 5963 | * keep the tree lock |
| 5964 | */ |
| 5965 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5966 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5967 | path->locks[level] = 0; |
| 5968 | } |
| 5969 | return 0; |
| 5970 | } |
| 5971 | |
| 5972 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5973 | * hepler to process tree block pointer. |
| 5974 | * |
| 5975 | * when wc->stage == DROP_REFERENCE, this function checks |
| 5976 | * reference count of the block pointed to. if the block |
| 5977 | * is shared and we need update back refs for the subtree |
| 5978 | * rooted at the block, this function changes wc->stage to |
| 5979 | * UPDATE_BACKREF. if the block is shared and there is no |
| 5980 | * need to update back, this function drops the reference |
| 5981 | * to the block. |
| 5982 | * |
| 5983 | * NOTE: return value 1 means we should stop walking down. |
| 5984 | */ |
| 5985 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 5986 | struct btrfs_root *root, |
| 5987 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5988 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5989 | { |
| 5990 | u64 bytenr; |
| 5991 | u64 generation; |
| 5992 | u64 parent; |
| 5993 | u32 blocksize; |
| 5994 | struct btrfs_key key; |
| 5995 | struct extent_buffer *next; |
| 5996 | int level = wc->level; |
| 5997 | int reada = 0; |
| 5998 | int ret = 0; |
| 5999 | |
| 6000 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 6001 | path->slots[level]); |
| 6002 | /* |
| 6003 | * if the lower level block was created before the snapshot |
| 6004 | * was created, we know there is no need to update back refs |
| 6005 | * for the subtree |
| 6006 | */ |
| 6007 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6008 | generation <= root->root_key.offset) { |
| 6009 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6010 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6011 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6012 | |
| 6013 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 6014 | blocksize = btrfs_level_size(root, level - 1); |
| 6015 | |
| 6016 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 6017 | if (!next) { |
| 6018 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6019 | if (!next) |
| 6020 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6021 | reada = 1; |
| 6022 | } |
| 6023 | btrfs_tree_lock(next); |
| 6024 | btrfs_set_lock_blocking(next); |
| 6025 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6026 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6027 | &wc->refs[level - 1], |
| 6028 | &wc->flags[level - 1]); |
| 6029 | BUG_ON(ret); |
| 6030 | BUG_ON(wc->refs[level - 1] == 0); |
| 6031 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6032 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6033 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6034 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6035 | if (level == 1 && |
| 6036 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6037 | goto skip; |
| 6038 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6039 | if (!wc->update_ref || |
| 6040 | generation <= root->root_key.offset) |
| 6041 | goto skip; |
| 6042 | |
| 6043 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 6044 | path->slots[level]); |
| 6045 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 6046 | if (ret < 0) |
| 6047 | goto skip; |
| 6048 | |
| 6049 | wc->stage = UPDATE_BACKREF; |
| 6050 | wc->shared_level = level - 1; |
| 6051 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6052 | } else { |
| 6053 | if (level == 1 && |
| 6054 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6055 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6056 | } |
| 6057 | |
| 6058 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 6059 | btrfs_tree_unlock(next); |
| 6060 | free_extent_buffer(next); |
| 6061 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6062 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6063 | } |
| 6064 | |
| 6065 | if (!next) { |
| 6066 | if (reada && level == 1) |
| 6067 | reada_walk_down(trans, root, wc, path); |
| 6068 | next = read_tree_block(root, bytenr, blocksize, generation); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 6069 | if (!next) |
| 6070 | return -EIO; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6071 | btrfs_tree_lock(next); |
| 6072 | btrfs_set_lock_blocking(next); |
| 6073 | } |
| 6074 | |
| 6075 | level--; |
| 6076 | BUG_ON(level != btrfs_header_level(next)); |
| 6077 | path->nodes[level] = next; |
| 6078 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6079 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6080 | wc->level = level; |
| 6081 | if (wc->level == 1) |
| 6082 | wc->reada_slot = 0; |
| 6083 | return 0; |
| 6084 | skip: |
| 6085 | wc->refs[level - 1] = 0; |
| 6086 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6087 | if (wc->stage == DROP_REFERENCE) { |
| 6088 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6089 | parent = path->nodes[level]->start; |
| 6090 | } else { |
| 6091 | BUG_ON(root->root_key.objectid != |
| 6092 | btrfs_header_owner(path->nodes[level])); |
| 6093 | parent = 0; |
| 6094 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6095 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6096 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 6097 | root->root_key.objectid, level - 1, 0); |
| 6098 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6099 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6100 | btrfs_tree_unlock(next); |
| 6101 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6102 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6103 | return 1; |
| 6104 | } |
| 6105 | |
| 6106 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6107 | * hepler to process tree block while walking up the tree. |
| 6108 | * |
| 6109 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6110 | * reference count on the block. |
| 6111 | * |
| 6112 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6113 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6114 | * to UPDATE_BACKREF previously while processing the block. |
| 6115 | * |
| 6116 | * NOTE: return value 1 means we should stop walking up. |
| 6117 | */ |
| 6118 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6119 | struct btrfs_root *root, |
| 6120 | struct btrfs_path *path, |
| 6121 | struct walk_control *wc) |
| 6122 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6123 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6124 | int level = wc->level; |
| 6125 | struct extent_buffer *eb = path->nodes[level]; |
| 6126 | u64 parent = 0; |
| 6127 | |
| 6128 | if (wc->stage == UPDATE_BACKREF) { |
| 6129 | BUG_ON(wc->shared_level < level); |
| 6130 | if (level < wc->shared_level) |
| 6131 | goto out; |
| 6132 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6133 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6134 | if (ret > 0) |
| 6135 | wc->update_ref = 0; |
| 6136 | |
| 6137 | wc->stage = DROP_REFERENCE; |
| 6138 | wc->shared_level = -1; |
| 6139 | path->slots[level] = 0; |
| 6140 | |
| 6141 | /* |
| 6142 | * check reference count again if the block isn't locked. |
| 6143 | * we should start walking down the tree again if reference |
| 6144 | * count is one. |
| 6145 | */ |
| 6146 | if (!path->locks[level]) { |
| 6147 | BUG_ON(level == 0); |
| 6148 | btrfs_tree_lock(eb); |
| 6149 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6150 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6151 | |
| 6152 | ret = btrfs_lookup_extent_info(trans, root, |
| 6153 | eb->start, eb->len, |
| 6154 | &wc->refs[level], |
| 6155 | &wc->flags[level]); |
| 6156 | BUG_ON(ret); |
| 6157 | BUG_ON(wc->refs[level] == 0); |
| 6158 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6159 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6160 | return 1; |
| 6161 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6162 | } |
| 6163 | } |
| 6164 | |
| 6165 | /* wc->stage == DROP_REFERENCE */ |
| 6166 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6167 | |
| 6168 | if (wc->refs[level] == 1) { |
| 6169 | if (level == 0) { |
| 6170 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6171 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 6172 | else |
| 6173 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6174 | BUG_ON(ret); |
| 6175 | } |
| 6176 | /* make block locked assertion in clean_tree_block happy */ |
| 6177 | if (!path->locks[level] && |
| 6178 | btrfs_header_generation(eb) == trans->transid) { |
| 6179 | btrfs_tree_lock(eb); |
| 6180 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6181 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6182 | } |
| 6183 | clean_tree_block(trans, root, eb); |
| 6184 | } |
| 6185 | |
| 6186 | if (eb == root->node) { |
| 6187 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6188 | parent = eb->start; |
| 6189 | else |
| 6190 | BUG_ON(root->root_key.objectid != |
| 6191 | btrfs_header_owner(eb)); |
| 6192 | } else { |
| 6193 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6194 | parent = path->nodes[level + 1]->start; |
| 6195 | else |
| 6196 | BUG_ON(root->root_key.objectid != |
| 6197 | btrfs_header_owner(path->nodes[level + 1])); |
| 6198 | } |
| 6199 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6200 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6201 | out: |
| 6202 | wc->refs[level] = 0; |
| 6203 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6204 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6205 | } |
| 6206 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6207 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6208 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6209 | struct btrfs_path *path, |
| 6210 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6211 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6212 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6213 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6214 | int ret; |
| 6215 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6216 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6217 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6218 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6219 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6220 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6221 | if (level == 0) |
| 6222 | break; |
| 6223 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6224 | if (path->slots[level] >= |
| 6225 | btrfs_header_nritems(path->nodes[level])) |
| 6226 | break; |
| 6227 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6228 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6229 | if (ret > 0) { |
| 6230 | path->slots[level]++; |
| 6231 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6232 | } else if (ret < 0) |
| 6233 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6234 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6235 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6236 | return 0; |
| 6237 | } |
| 6238 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6239 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6240 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6241 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6242 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6243 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6244 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6245 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6246 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6247 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6248 | while (level < max_level && path->nodes[level]) { |
| 6249 | wc->level = level; |
| 6250 | if (path->slots[level] + 1 < |
| 6251 | btrfs_header_nritems(path->nodes[level])) { |
| 6252 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6253 | return 0; |
| 6254 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6255 | ret = walk_up_proc(trans, root, path, wc); |
| 6256 | if (ret > 0) |
| 6257 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6258 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6259 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6260 | btrfs_tree_unlock_rw(path->nodes[level], |
| 6261 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6262 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6263 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6264 | free_extent_buffer(path->nodes[level]); |
| 6265 | path->nodes[level] = NULL; |
| 6266 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6267 | } |
| 6268 | } |
| 6269 | return 1; |
| 6270 | } |
| 6271 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6272 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6273 | * drop a subvolume tree. |
| 6274 | * |
| 6275 | * this function traverses the tree freeing any blocks that only |
| 6276 | * referenced by the tree. |
| 6277 | * |
| 6278 | * when a shared tree block is found. this function decreases its |
| 6279 | * reference count by one. if update_ref is true, this function |
| 6280 | * also make sure backrefs for the shared block and all lower level |
| 6281 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6282 | */ |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6283 | void btrfs_drop_snapshot(struct btrfs_root *root, |
| 6284 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6285 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6286 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6287 | struct btrfs_trans_handle *trans; |
| 6288 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6289 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6290 | struct walk_control *wc; |
| 6291 | struct btrfs_key key; |
| 6292 | int err = 0; |
| 6293 | int ret; |
| 6294 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6295 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6296 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6297 | if (!path) { |
| 6298 | err = -ENOMEM; |
| 6299 | goto out; |
| 6300 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6301 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6302 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 6303 | if (!wc) { |
| 6304 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6305 | err = -ENOMEM; |
| 6306 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 6307 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6308 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6309 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6310 | BUG_ON(IS_ERR(trans)); |
| 6311 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6312 | if (block_rsv) |
| 6313 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6314 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6315 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6316 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6317 | path->nodes[level] = btrfs_lock_root_node(root); |
| 6318 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6319 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6320 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6321 | memset(&wc->update_progress, 0, |
| 6322 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6323 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6324 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6325 | memcpy(&wc->update_progress, &key, |
| 6326 | sizeof(wc->update_progress)); |
| 6327 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6328 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6329 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6330 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6331 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6332 | path->lowest_level = 0; |
| 6333 | if (ret < 0) { |
| 6334 | err = ret; |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6335 | goto out_free; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6336 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6337 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6338 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6339 | /* |
| 6340 | * unlock our path, this is safe because only this |
| 6341 | * function is allowed to delete this snapshot |
| 6342 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6343 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6344 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6345 | level = btrfs_header_level(root->node); |
| 6346 | while (1) { |
| 6347 | btrfs_tree_lock(path->nodes[level]); |
| 6348 | btrfs_set_lock_blocking(path->nodes[level]); |
| 6349 | |
| 6350 | ret = btrfs_lookup_extent_info(trans, root, |
| 6351 | path->nodes[level]->start, |
| 6352 | path->nodes[level]->len, |
| 6353 | &wc->refs[level], |
| 6354 | &wc->flags[level]); |
| 6355 | BUG_ON(ret); |
| 6356 | BUG_ON(wc->refs[level] == 0); |
| 6357 | |
| 6358 | if (level == root_item->drop_level) |
| 6359 | break; |
| 6360 | |
| 6361 | btrfs_tree_unlock(path->nodes[level]); |
| 6362 | WARN_ON(wc->refs[level] != 1); |
| 6363 | level--; |
| 6364 | } |
| 6365 | } |
| 6366 | |
| 6367 | wc->level = level; |
| 6368 | wc->shared_level = -1; |
| 6369 | wc->stage = DROP_REFERENCE; |
| 6370 | wc->update_ref = update_ref; |
| 6371 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6372 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6373 | |
| 6374 | while (1) { |
| 6375 | ret = walk_down_tree(trans, root, path, wc); |
| 6376 | if (ret < 0) { |
| 6377 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6378 | break; |
| 6379 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6380 | |
| 6381 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6382 | if (ret < 0) { |
| 6383 | err = ret; |
| 6384 | break; |
| 6385 | } |
| 6386 | |
| 6387 | if (ret > 0) { |
| 6388 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6389 | break; |
| 6390 | } |
| 6391 | |
| 6392 | if (wc->stage == DROP_REFERENCE) { |
| 6393 | level = wc->level; |
| 6394 | btrfs_node_key(path->nodes[level], |
| 6395 | &root_item->drop_progress, |
| 6396 | path->slots[level]); |
| 6397 | root_item->drop_level = level; |
| 6398 | } |
| 6399 | |
| 6400 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6401 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6402 | ret = btrfs_update_root(trans, tree_root, |
| 6403 | &root->root_key, |
| 6404 | root_item); |
| 6405 | BUG_ON(ret); |
| 6406 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6407 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6408 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6409 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6410 | if (block_rsv) |
| 6411 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6412 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6413 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6414 | btrfs_release_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6415 | BUG_ON(err); |
| 6416 | |
| 6417 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6418 | BUG_ON(ret); |
| 6419 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6420 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6421 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6422 | NULL, NULL); |
| 6423 | BUG_ON(ret < 0); |
| 6424 | if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 6425 | /* if we fail to delete the orphan item this time |
| 6426 | * around, it'll get picked up the next time. |
| 6427 | * |
| 6428 | * The most common failure here is just -ENOENT. |
| 6429 | */ |
| 6430 | btrfs_del_orphan_item(trans, tree_root, |
| 6431 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6432 | } |
| 6433 | } |
| 6434 | |
| 6435 | if (root->in_radix) { |
| 6436 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6437 | } else { |
| 6438 | free_extent_buffer(root->node); |
| 6439 | free_extent_buffer(root->commit_root); |
| 6440 | kfree(root); |
| 6441 | } |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6442 | out_free: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6443 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6444 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6445 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6446 | out: |
| 6447 | if (err) |
| 6448 | btrfs_std_error(root->fs_info, err); |
| 6449 | return; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6450 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6451 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6452 | /* |
| 6453 | * drop subtree rooted at tree block 'node'. |
| 6454 | * |
| 6455 | * NOTE: this function will unlock and release tree block 'node' |
| 6456 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6457 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6458 | struct btrfs_root *root, |
| 6459 | struct extent_buffer *node, |
| 6460 | struct extent_buffer *parent) |
| 6461 | { |
| 6462 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6463 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6464 | int level; |
| 6465 | int parent_level; |
| 6466 | int ret = 0; |
| 6467 | int wret; |
| 6468 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6469 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6470 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6471 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6472 | if (!path) |
| 6473 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6474 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6475 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6476 | if (!wc) { |
| 6477 | btrfs_free_path(path); |
| 6478 | return -ENOMEM; |
| 6479 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6480 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6481 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6482 | parent_level = btrfs_header_level(parent); |
| 6483 | extent_buffer_get(parent); |
| 6484 | path->nodes[parent_level] = parent; |
| 6485 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6486 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6487 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6488 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6489 | path->nodes[level] = node; |
| 6490 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6491 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6492 | |
| 6493 | wc->refs[parent_level] = 1; |
| 6494 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6495 | wc->level = level; |
| 6496 | wc->shared_level = -1; |
| 6497 | wc->stage = DROP_REFERENCE; |
| 6498 | wc->update_ref = 0; |
| 6499 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6500 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6501 | |
| 6502 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6503 | wret = walk_down_tree(trans, root, path, wc); |
| 6504 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6505 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6506 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6507 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6508 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6509 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6510 | if (wret < 0) |
| 6511 | ret = wret; |
| 6512 | if (wret != 0) |
| 6513 | break; |
| 6514 | } |
| 6515 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6516 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6517 | btrfs_free_path(path); |
| 6518 | return ret; |
| 6519 | } |
| 6520 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6521 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 6522 | { |
| 6523 | u64 num_devices; |
| 6524 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 6525 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 6526 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6527 | /* |
| 6528 | * we add in the count of missing devices because we want |
| 6529 | * to make sure that any RAID levels on a degraded FS |
| 6530 | * continue to be honored. |
| 6531 | */ |
| 6532 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 6533 | root->fs_info->fs_devices->missing_devices; |
| 6534 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6535 | if (num_devices == 1) { |
| 6536 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6537 | stripped = flags & ~stripped; |
| 6538 | |
| 6539 | /* turn raid0 into single device chunks */ |
| 6540 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 6541 | return stripped; |
| 6542 | |
| 6543 | /* turn mirroring into duplication */ |
| 6544 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6545 | BTRFS_BLOCK_GROUP_RAID10)) |
| 6546 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 6547 | return flags; |
| 6548 | } else { |
| 6549 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6550 | if (flags & stripped) |
| 6551 | return flags; |
| 6552 | |
| 6553 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6554 | stripped = flags & ~stripped; |
| 6555 | |
| 6556 | /* switch duplicated blocks with raid1 */ |
| 6557 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 6558 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 6559 | |
| 6560 | /* turn single device chunks into raid0 */ |
| 6561 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 6562 | } |
| 6563 | return flags; |
| 6564 | } |
| 6565 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6566 | static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6567 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6568 | struct btrfs_space_info *sinfo = cache->space_info; |
| 6569 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6570 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6571 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6572 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6573 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6574 | /* |
| 6575 | * We need some metadata space and system metadata space for |
| 6576 | * allocating chunks in some corner cases until we force to set |
| 6577 | * it to be readonly. |
| 6578 | */ |
| 6579 | if ((sinfo->flags & |
| 6580 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 6581 | !force) |
| 6582 | min_allocable_bytes = 1 * 1024 * 1024; |
| 6583 | else |
| 6584 | min_allocable_bytes = 0; |
| 6585 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6586 | spin_lock(&sinfo->lock); |
| 6587 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 6588 | |
| 6589 | if (cache->ro) { |
| 6590 | ret = 0; |
| 6591 | goto out; |
| 6592 | } |
| 6593 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6594 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 6595 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6596 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6597 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 6598 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
| 6599 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6600 | sinfo->bytes_readonly += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6601 | cache->ro = 1; |
| 6602 | ret = 0; |
| 6603 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 6604 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6605 | spin_unlock(&cache->lock); |
| 6606 | spin_unlock(&sinfo->lock); |
| 6607 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6608 | } |
| 6609 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6610 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 6611 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6612 | |
| 6613 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6614 | struct btrfs_trans_handle *trans; |
| 6615 | u64 alloc_flags; |
| 6616 | int ret; |
| 6617 | |
| 6618 | BUG_ON(cache->ro); |
| 6619 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6620 | trans = btrfs_join_transaction(root); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6621 | BUG_ON(IS_ERR(trans)); |
| 6622 | |
| 6623 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 6624 | if (alloc_flags != cache->flags) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6625 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6626 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6627 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6628 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6629 | if (!ret) |
| 6630 | goto out; |
| 6631 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6632 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6633 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6634 | if (ret < 0) |
| 6635 | goto out; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6636 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6637 | out: |
| 6638 | btrfs_end_transaction(trans, root); |
| 6639 | return ret; |
| 6640 | } |
| 6641 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 6642 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 6643 | struct btrfs_root *root, u64 type) |
| 6644 | { |
| 6645 | u64 alloc_flags = get_alloc_profile(root, type); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6646 | return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6647 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 6648 | } |
| 6649 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 6650 | /* |
| 6651 | * helper to account the unused space of all the readonly block group in the |
| 6652 | * list. takes mirrors into account. |
| 6653 | */ |
| 6654 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 6655 | { |
| 6656 | struct btrfs_block_group_cache *block_group; |
| 6657 | u64 free_bytes = 0; |
| 6658 | int factor; |
| 6659 | |
| 6660 | list_for_each_entry(block_group, groups_list, list) { |
| 6661 | spin_lock(&block_group->lock); |
| 6662 | |
| 6663 | if (!block_group->ro) { |
| 6664 | spin_unlock(&block_group->lock); |
| 6665 | continue; |
| 6666 | } |
| 6667 | |
| 6668 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6669 | BTRFS_BLOCK_GROUP_RAID10 | |
| 6670 | BTRFS_BLOCK_GROUP_DUP)) |
| 6671 | factor = 2; |
| 6672 | else |
| 6673 | factor = 1; |
| 6674 | |
| 6675 | free_bytes += (block_group->key.offset - |
| 6676 | btrfs_block_group_used(&block_group->item)) * |
| 6677 | factor; |
| 6678 | |
| 6679 | spin_unlock(&block_group->lock); |
| 6680 | } |
| 6681 | |
| 6682 | return free_bytes; |
| 6683 | } |
| 6684 | |
| 6685 | /* |
| 6686 | * helper to account the unused space of all the readonly block group in the |
| 6687 | * space_info. takes mirrors into account. |
| 6688 | */ |
| 6689 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 6690 | { |
| 6691 | int i; |
| 6692 | u64 free_bytes = 0; |
| 6693 | |
| 6694 | spin_lock(&sinfo->lock); |
| 6695 | |
| 6696 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 6697 | if (!list_empty(&sinfo->block_groups[i])) |
| 6698 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 6699 | &sinfo->block_groups[i]); |
| 6700 | |
| 6701 | spin_unlock(&sinfo->lock); |
| 6702 | |
| 6703 | return free_bytes; |
| 6704 | } |
| 6705 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6706 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 6707 | struct btrfs_block_group_cache *cache) |
| 6708 | { |
| 6709 | struct btrfs_space_info *sinfo = cache->space_info; |
| 6710 | u64 num_bytes; |
| 6711 | |
| 6712 | BUG_ON(!cache->ro); |
| 6713 | |
| 6714 | spin_lock(&sinfo->lock); |
| 6715 | spin_lock(&cache->lock); |
| 6716 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 6717 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 6718 | sinfo->bytes_readonly -= num_bytes; |
| 6719 | cache->ro = 0; |
| 6720 | spin_unlock(&cache->lock); |
| 6721 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6722 | return 0; |
| 6723 | } |
| 6724 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6725 | /* |
| 6726 | * checks to see if its even possible to relocate this block group. |
| 6727 | * |
| 6728 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 6729 | * ok to go ahead and try. |
| 6730 | */ |
| 6731 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6732 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6733 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6734 | struct btrfs_space_info *space_info; |
| 6735 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6736 | struct btrfs_device *device; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6737 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 6738 | u64 dev_min = 1; |
| 6739 | u64 dev_nr = 0; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6740 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6741 | int full = 0; |
| 6742 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6743 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6744 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6745 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6746 | /* odd, couldn't find the block group, leave it alone */ |
| 6747 | if (!block_group) |
| 6748 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6749 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6750 | min_free = btrfs_block_group_used(&block_group->item); |
| 6751 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6752 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6753 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6754 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6755 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6756 | space_info = block_group->space_info; |
| 6757 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6758 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6759 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6760 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6761 | /* |
| 6762 | * 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] | 6763 | * relocate it unless we're able to allocate a new chunk below. |
| 6764 | * |
| 6765 | * Otherwise, we need to make sure we have room in the space to handle |
| 6766 | * 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] | 6767 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 6768 | if ((space_info->total_bytes != block_group->key.offset) && |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6769 | (space_info->bytes_used + space_info->bytes_reserved + |
| 6770 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 6771 | min_free < space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6772 | spin_unlock(&space_info->lock); |
| 6773 | goto out; |
| 6774 | } |
| 6775 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6776 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6777 | /* |
| 6778 | * ok we don't have enough space, but maybe we have free space on our |
| 6779 | * devices to allocate new chunks for relocation, so loop through our |
| 6780 | * alloc devices and guess if we have enough space. However, if we |
| 6781 | * were marked as full, then we know there aren't enough chunks, and we |
| 6782 | * can just return. |
| 6783 | */ |
| 6784 | ret = -1; |
| 6785 | if (full) |
| 6786 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6787 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6788 | /* |
| 6789 | * index: |
| 6790 | * 0: raid10 |
| 6791 | * 1: raid1 |
| 6792 | * 2: dup |
| 6793 | * 3: raid0 |
| 6794 | * 4: single |
| 6795 | */ |
| 6796 | index = get_block_group_index(block_group); |
| 6797 | if (index == 0) { |
| 6798 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 6799 | /* Divide by 2 */ |
| 6800 | min_free >>= 1; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6801 | } else if (index == 1) { |
| 6802 | dev_min = 2; |
| 6803 | } else if (index == 2) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 6804 | /* Multiply by 2 */ |
| 6805 | min_free <<= 1; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6806 | } else if (index == 3) { |
| 6807 | dev_min = fs_devices->rw_devices; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 6808 | do_div(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6809 | } |
| 6810 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6811 | mutex_lock(&root->fs_info->chunk_mutex); |
| 6812 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 6813 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6814 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6815 | /* |
| 6816 | * check to make sure we can actually find a chunk with enough |
| 6817 | * space to fit our block group in. |
| 6818 | */ |
| 6819 | if (device->total_bytes > device->bytes_used + min_free) { |
| 6820 | ret = find_free_dev_extent(NULL, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 6821 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6822 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6823 | dev_nr++; |
| 6824 | |
| 6825 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6826 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6827 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6828 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6829 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6830 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6831 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6832 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6833 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6834 | return ret; |
| 6835 | } |
| 6836 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 6837 | static int find_first_block_group(struct btrfs_root *root, |
| 6838 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6839 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6840 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6841 | struct btrfs_key found_key; |
| 6842 | struct extent_buffer *leaf; |
| 6843 | int slot; |
| 6844 | |
| 6845 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 6846 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6847 | goto out; |
| 6848 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6849 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6850 | slot = path->slots[0]; |
| 6851 | leaf = path->nodes[0]; |
| 6852 | if (slot >= btrfs_header_nritems(leaf)) { |
| 6853 | ret = btrfs_next_leaf(root, path); |
| 6854 | if (ret == 0) |
| 6855 | continue; |
| 6856 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6857 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6858 | break; |
| 6859 | } |
| 6860 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 6861 | |
| 6862 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6863 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 6864 | ret = 0; |
| 6865 | goto out; |
| 6866 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6867 | path->slots[0]++; |
| 6868 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6869 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6870 | return ret; |
| 6871 | } |
| 6872 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6873 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 6874 | { |
| 6875 | struct btrfs_block_group_cache *block_group; |
| 6876 | u64 last = 0; |
| 6877 | |
| 6878 | while (1) { |
| 6879 | struct inode *inode; |
| 6880 | |
| 6881 | block_group = btrfs_lookup_first_block_group(info, last); |
| 6882 | while (block_group) { |
| 6883 | spin_lock(&block_group->lock); |
| 6884 | if (block_group->iref) |
| 6885 | break; |
| 6886 | spin_unlock(&block_group->lock); |
| 6887 | block_group = next_block_group(info->tree_root, |
| 6888 | block_group); |
| 6889 | } |
| 6890 | if (!block_group) { |
| 6891 | if (last == 0) |
| 6892 | break; |
| 6893 | last = 0; |
| 6894 | continue; |
| 6895 | } |
| 6896 | |
| 6897 | inode = block_group->inode; |
| 6898 | block_group->iref = 0; |
| 6899 | block_group->inode = NULL; |
| 6900 | spin_unlock(&block_group->lock); |
| 6901 | iput(inode); |
| 6902 | last = block_group->key.objectid + block_group->key.offset; |
| 6903 | btrfs_put_block_group(block_group); |
| 6904 | } |
| 6905 | } |
| 6906 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6907 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 6908 | { |
| 6909 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6910 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6911 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6912 | struct rb_node *n; |
| 6913 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6914 | down_write(&info->extent_commit_sem); |
| 6915 | while (!list_empty(&info->caching_block_groups)) { |
| 6916 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 6917 | struct btrfs_caching_control, list); |
| 6918 | list_del(&caching_ctl->list); |
| 6919 | put_caching_control(caching_ctl); |
| 6920 | } |
| 6921 | up_write(&info->extent_commit_sem); |
| 6922 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6923 | spin_lock(&info->block_group_cache_lock); |
| 6924 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 6925 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 6926 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6927 | rb_erase(&block_group->cache_node, |
| 6928 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6929 | spin_unlock(&info->block_group_cache_lock); |
| 6930 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6931 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6932 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6933 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 6934 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6935 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6936 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6937 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 6938 | /* |
| 6939 | * We haven't cached this block group, which means we could |
| 6940 | * possibly have excluded extents on this block group. |
| 6941 | */ |
| 6942 | if (block_group->cached == BTRFS_CACHE_NO) |
| 6943 | free_excluded_extents(info->extent_root, block_group); |
| 6944 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6945 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 6946 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 6947 | |
| 6948 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6949 | } |
| 6950 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6951 | |
| 6952 | /* now that all the block groups are freed, go through and |
| 6953 | * free all the space_info structs. This is only called during |
| 6954 | * the final stages of unmount, and so we know nobody is |
| 6955 | * using them. We call synchronize_rcu() once before we start, |
| 6956 | * just to be on the safe side. |
| 6957 | */ |
| 6958 | synchronize_rcu(); |
| 6959 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6960 | release_global_block_rsv(info); |
| 6961 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6962 | while(!list_empty(&info->space_info)) { |
| 6963 | space_info = list_entry(info->space_info.next, |
| 6964 | struct btrfs_space_info, |
| 6965 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6966 | if (space_info->bytes_pinned > 0 || |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6967 | space_info->bytes_reserved > 0 || |
| 6968 | space_info->bytes_may_use > 0) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6969 | WARN_ON(1); |
| 6970 | dump_space_info(space_info, 0, 0); |
| 6971 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 6972 | list_del(&space_info->list); |
| 6973 | kfree(space_info); |
| 6974 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6975 | return 0; |
| 6976 | } |
| 6977 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6978 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 6979 | struct btrfs_block_group_cache *cache) |
| 6980 | { |
| 6981 | int index = get_block_group_index(cache); |
| 6982 | |
| 6983 | down_write(&space_info->groups_sem); |
| 6984 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 6985 | up_write(&space_info->groups_sem); |
| 6986 | } |
| 6987 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6988 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 6989 | { |
| 6990 | struct btrfs_path *path; |
| 6991 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6992 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 6993 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 6994 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6995 | struct btrfs_key key; |
| 6996 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 6997 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 6998 | int need_clear = 0; |
| 6999 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7000 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7001 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7002 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7003 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7004 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7005 | path = btrfs_alloc_path(); |
| 7006 | if (!path) |
| 7007 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 7008 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7009 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7010 | cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy); |
| 7011 | if (cache_gen != 0 && |
| 7012 | btrfs_super_generation(&root->fs_info->super_copy) != cache_gen) |
| 7013 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 7014 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 7015 | need_clear = 1; |
Josef Bacik | 8216ef8 | 2010-10-28 16:55:47 -0400 | [diff] [blame] | 7016 | if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen) |
| 7017 | printk(KERN_INFO "btrfs: disk space caching is enabled\n"); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7018 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7019 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7020 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7021 | if (ret > 0) |
| 7022 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7023 | if (ret != 0) |
| 7024 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7025 | leaf = path->nodes[0]; |
| 7026 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7027 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7028 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7029 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7030 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7031 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7032 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7033 | GFP_NOFS); |
| 7034 | if (!cache->free_space_ctl) { |
| 7035 | kfree(cache); |
| 7036 | ret = -ENOMEM; |
| 7037 | goto error; |
| 7038 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7039 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7040 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7041 | spin_lock_init(&cache->lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7042 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7043 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7044 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7045 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7046 | if (need_clear) |
| 7047 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 7048 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7049 | read_extent_buffer(leaf, &cache->item, |
| 7050 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7051 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7052 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7053 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7054 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7055 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7056 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7057 | cache->sectorsize = root->sectorsize; |
| 7058 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7059 | btrfs_init_free_space_ctl(cache); |
| 7060 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7061 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7062 | * We need to exclude the super stripes now so that the space |
| 7063 | * info has super bytes accounted for, otherwise we'll think |
| 7064 | * we have more space than we actually do. |
| 7065 | */ |
| 7066 | exclude_super_stripes(root, cache); |
| 7067 | |
| 7068 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7069 | * check for two cases, either we are full, and therefore |
| 7070 | * don't need to bother with the caching work since we won't |
| 7071 | * find any space, or we are empty, and we can just add all |
| 7072 | * the space in and be done with it. This saves us _alot_ of |
| 7073 | * time, particularly in the full case. |
| 7074 | */ |
| 7075 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7076 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7077 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7078 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7079 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7080 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7081 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7082 | add_new_free_space(cache, root->fs_info, |
| 7083 | found_key.objectid, |
| 7084 | found_key.objectid + |
| 7085 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7086 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7087 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7088 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7089 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7090 | btrfs_block_group_used(&cache->item), |
| 7091 | &space_info); |
| 7092 | BUG_ON(ret); |
| 7093 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7094 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7095 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7096 | spin_unlock(&cache->space_info->lock); |
| 7097 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7098 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7099 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7100 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7101 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7102 | |
| 7103 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7104 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7105 | set_block_group_ro(cache, 1); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7106 | } |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7107 | |
| 7108 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 7109 | if (!(get_alloc_profile(root, space_info->flags) & |
| 7110 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 7111 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7112 | BTRFS_BLOCK_GROUP_DUP))) |
| 7113 | continue; |
| 7114 | /* |
| 7115 | * avoid allocating from un-mirrored block group if there are |
| 7116 | * mirrored block groups. |
| 7117 | */ |
| 7118 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7119 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7120 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7121 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7122 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7123 | |
| 7124 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7125 | ret = 0; |
| 7126 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7127 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7128 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7129 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7130 | |
| 7131 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7132 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7133 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7134 | u64 size) |
| 7135 | { |
| 7136 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7137 | struct btrfs_root *extent_root; |
| 7138 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7139 | |
| 7140 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7141 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7142 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7143 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7144 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7145 | if (!cache) |
| 7146 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7147 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7148 | GFP_NOFS); |
| 7149 | if (!cache->free_space_ctl) { |
| 7150 | kfree(cache); |
| 7151 | return -ENOMEM; |
| 7152 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7153 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7154 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7155 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7156 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7157 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7158 | cache->fs_info = root->fs_info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7159 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7160 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7161 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7162 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7163 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7164 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7165 | btrfs_init_free_space_ctl(cache); |
| 7166 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7167 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7168 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7169 | cache->flags = type; |
| 7170 | btrfs_set_block_group_flags(&cache->item, type); |
| 7171 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7172 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7173 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7174 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7175 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7176 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7177 | chunk_offset + size); |
| 7178 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7179 | free_excluded_extents(root, cache); |
| 7180 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7181 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7182 | &cache->space_info); |
| 7183 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7184 | |
| 7185 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7186 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7187 | spin_unlock(&cache->space_info->lock); |
| 7188 | |
Yan, Zheng | b742bb82 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7189 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7190 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7191 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7192 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7193 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7194 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7195 | sizeof(cache->item)); |
| 7196 | BUG_ON(ret); |
| 7197 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7198 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7199 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7200 | return 0; |
| 7201 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7202 | |
| 7203 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7204 | struct btrfs_root *root, u64 group_start) |
| 7205 | { |
| 7206 | struct btrfs_path *path; |
| 7207 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7208 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7209 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7210 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7211 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7212 | int ret; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7213 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7214 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7215 | root = root->fs_info->extent_root; |
| 7216 | |
| 7217 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7218 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7219 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7220 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 7221 | /* |
| 7222 | * Free the reserved super bytes from this block group before |
| 7223 | * remove it. |
| 7224 | */ |
| 7225 | free_excluded_extents(root, block_group); |
| 7226 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7227 | memcpy(&key, &block_group->key, sizeof(key)); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7228 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 7229 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7230 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7231 | factor = 2; |
| 7232 | else |
| 7233 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7234 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7235 | /* make sure this block group isn't part of an allocation cluster */ |
| 7236 | cluster = &root->fs_info->data_alloc_cluster; |
| 7237 | spin_lock(&cluster->refill_lock); |
| 7238 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7239 | spin_unlock(&cluster->refill_lock); |
| 7240 | |
| 7241 | /* |
| 7242 | * make sure this block group isn't part of a metadata |
| 7243 | * allocation cluster |
| 7244 | */ |
| 7245 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7246 | spin_lock(&cluster->refill_lock); |
| 7247 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7248 | spin_unlock(&cluster->refill_lock); |
| 7249 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7250 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7251 | if (!path) { |
| 7252 | ret = -ENOMEM; |
| 7253 | goto out; |
| 7254 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7255 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7256 | inode = lookup_free_space_inode(root, block_group, path); |
| 7257 | if (!IS_ERR(inode)) { |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 7258 | ret = btrfs_orphan_add(trans, inode); |
| 7259 | BUG_ON(ret); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7260 | clear_nlink(inode); |
| 7261 | /* One for the block groups ref */ |
| 7262 | spin_lock(&block_group->lock); |
| 7263 | if (block_group->iref) { |
| 7264 | block_group->iref = 0; |
| 7265 | block_group->inode = NULL; |
| 7266 | spin_unlock(&block_group->lock); |
| 7267 | iput(inode); |
| 7268 | } else { |
| 7269 | spin_unlock(&block_group->lock); |
| 7270 | } |
| 7271 | /* One for our lookup ref */ |
| 7272 | iput(inode); |
| 7273 | } |
| 7274 | |
| 7275 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 7276 | key.offset = block_group->key.objectid; |
| 7277 | key.type = 0; |
| 7278 | |
| 7279 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 7280 | if (ret < 0) |
| 7281 | goto out; |
| 7282 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7283 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7284 | if (ret == 0) { |
| 7285 | ret = btrfs_del_item(trans, tree_root, path); |
| 7286 | if (ret) |
| 7287 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7288 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7289 | } |
| 7290 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7291 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7292 | rb_erase(&block_group->cache_node, |
| 7293 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7294 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7295 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7296 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7297 | /* |
| 7298 | * we must use list_del_init so people can check to see if they |
| 7299 | * are still on the list after taking the semaphore |
| 7300 | */ |
| 7301 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7302 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7303 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7304 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7305 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7306 | |
| 7307 | btrfs_remove_free_space_cache(block_group); |
| 7308 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7309 | spin_lock(&block_group->space_info->lock); |
| 7310 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7311 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7312 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7313 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7314 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7315 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7316 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7317 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7318 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7319 | btrfs_put_block_group(block_group); |
| 7320 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7321 | |
| 7322 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7323 | if (ret > 0) |
| 7324 | ret = -EIO; |
| 7325 | if (ret < 0) |
| 7326 | goto out; |
| 7327 | |
| 7328 | ret = btrfs_del_item(trans, root, path); |
| 7329 | out: |
| 7330 | btrfs_free_path(path); |
| 7331 | return ret; |
| 7332 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7333 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7334 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 7335 | { |
| 7336 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7337 | struct btrfs_super_block *disk_super; |
| 7338 | u64 features; |
| 7339 | u64 flags; |
| 7340 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7341 | int ret; |
| 7342 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7343 | disk_super = &fs_info->super_copy; |
| 7344 | if (!btrfs_super_root(disk_super)) |
| 7345 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7346 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7347 | features = btrfs_super_incompat_flags(disk_super); |
| 7348 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 7349 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7350 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7351 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 7352 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7353 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7354 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7355 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7356 | if (mixed) { |
| 7357 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 7358 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7359 | } else { |
| 7360 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 7361 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7362 | if (ret) |
| 7363 | goto out; |
| 7364 | |
| 7365 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 7366 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7367 | } |
| 7368 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7369 | return ret; |
| 7370 | } |
| 7371 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7372 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 7373 | { |
| 7374 | return unpin_extent_range(root, start, end); |
| 7375 | } |
| 7376 | |
| 7377 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 7378 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7379 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 7380 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7381 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 7382 | |
| 7383 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 7384 | { |
| 7385 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 7386 | struct btrfs_block_group_cache *cache = NULL; |
| 7387 | u64 group_trimmed; |
| 7388 | u64 start; |
| 7389 | u64 end; |
| 7390 | u64 trimmed = 0; |
| 7391 | int ret = 0; |
| 7392 | |
| 7393 | cache = btrfs_lookup_block_group(fs_info, range->start); |
| 7394 | |
| 7395 | while (cache) { |
| 7396 | if (cache->key.objectid >= (range->start + range->len)) { |
| 7397 | btrfs_put_block_group(cache); |
| 7398 | break; |
| 7399 | } |
| 7400 | |
| 7401 | start = max(range->start, cache->key.objectid); |
| 7402 | end = min(range->start + range->len, |
| 7403 | cache->key.objectid + cache->key.offset); |
| 7404 | |
| 7405 | if (end - start >= range->minlen) { |
| 7406 | if (!block_group_cache_done(cache)) { |
| 7407 | ret = cache_block_group(cache, NULL, root, 0); |
| 7408 | if (!ret) |
| 7409 | wait_block_group_cache_done(cache); |
| 7410 | } |
| 7411 | ret = btrfs_trim_block_group(cache, |
| 7412 | &group_trimmed, |
| 7413 | start, |
| 7414 | end, |
| 7415 | range->minlen); |
| 7416 | |
| 7417 | trimmed += group_trimmed; |
| 7418 | if (ret) { |
| 7419 | btrfs_put_block_group(cache); |
| 7420 | break; |
| 7421 | } |
| 7422 | } |
| 7423 | |
| 7424 | cache = next_block_group(fs_info->tree_root, cache); |
| 7425 | } |
| 7426 | |
| 7427 | range->len = trimmed; |
| 7428 | return ret; |
| 7429 | } |