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> |
David Sterba | dff51cd | 2011-06-14 12:52:17 +0200 | [diff] [blame] | 26 | #include <linux/ratelimit.h> |
Chris Mason | 4b4e25f | 2008-11-20 10:22:27 -0500 | [diff] [blame] | 27 | #include "compat.h" |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 28 | #include "hash.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 29 | #include "ctree.h" |
| 30 | #include "disk-io.h" |
| 31 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 32 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 33 | #include "volumes.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 34 | #include "locking.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 35 | #include "free-space-cache.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 36 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 37 | /* control flags for do_chunk_alloc's force field |
| 38 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
| 39 | * if we really need one. |
| 40 | * |
| 41 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
| 42 | * |
| 43 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
| 44 | * if we have very few chunks already allocated. This is |
| 45 | * used as part of the clustering code to help make sure |
| 46 | * we have a good pool of storage to cluster in, without |
| 47 | * filling the FS with empty chunks |
| 48 | * |
| 49 | */ |
| 50 | enum { |
| 51 | CHUNK_ALLOC_NO_FORCE = 0, |
| 52 | CHUNK_ALLOC_FORCE = 1, |
| 53 | CHUNK_ALLOC_LIMITED = 2, |
| 54 | }; |
| 55 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 56 | /* |
| 57 | * Control how reservations are dealt with. |
| 58 | * |
| 59 | * RESERVE_FREE - freeing a reservation. |
| 60 | * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for |
| 61 | * ENOSPC accounting |
| 62 | * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update |
| 63 | * bytes_may_use as the ENOSPC accounting is done elsewhere |
| 64 | */ |
| 65 | enum { |
| 66 | RESERVE_FREE = 0, |
| 67 | RESERVE_ALLOC = 1, |
| 68 | RESERVE_ALLOC_NO_ACCOUNT = 2, |
| 69 | }; |
| 70 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 71 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 72 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 73 | u64 bytenr, u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 74 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 75 | struct btrfs_root *root, |
| 76 | u64 bytenr, u64 num_bytes, u64 parent, |
| 77 | u64 root_objectid, u64 owner_objectid, |
| 78 | u64 owner_offset, int refs_to_drop, |
| 79 | struct btrfs_delayed_extent_op *extra_op); |
| 80 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 81 | struct extent_buffer *leaf, |
| 82 | struct btrfs_extent_item *ei); |
| 83 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 84 | struct btrfs_root *root, |
| 85 | u64 parent, u64 root_objectid, |
| 86 | u64 flags, u64 owner, u64 offset, |
| 87 | struct btrfs_key *ins, int ref_mod); |
| 88 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 89 | struct btrfs_root *root, |
| 90 | u64 parent, u64 root_objectid, |
| 91 | u64 flags, struct btrfs_disk_key *key, |
| 92 | int level, struct btrfs_key *ins); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 93 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 94 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 95 | u64 flags, int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 96 | static int find_next_key(struct btrfs_path *path, int level, |
| 97 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 98 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 99 | int dump_block_groups); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 100 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 101 | u64 num_bytes, int reserve); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 102 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 103 | static noinline int |
| 104 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 105 | { |
| 106 | smp_mb(); |
| 107 | return cache->cached == BTRFS_CACHE_FINISHED; |
| 108 | } |
| 109 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 110 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 111 | { |
| 112 | return (cache->flags & bits) == bits; |
| 113 | } |
| 114 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 115 | static void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 116 | { |
| 117 | atomic_inc(&cache->count); |
| 118 | } |
| 119 | |
| 120 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 121 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 122 | if (atomic_dec_and_test(&cache->count)) { |
| 123 | WARN_ON(cache->pinned > 0); |
| 124 | WARN_ON(cache->reserved > 0); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 125 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 126 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 127 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 130 | /* |
| 131 | * this adds the block group to the fs_info rb tree for the block group |
| 132 | * cache |
| 133 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 134 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 135 | struct btrfs_block_group_cache *block_group) |
| 136 | { |
| 137 | struct rb_node **p; |
| 138 | struct rb_node *parent = NULL; |
| 139 | struct btrfs_block_group_cache *cache; |
| 140 | |
| 141 | spin_lock(&info->block_group_cache_lock); |
| 142 | p = &info->block_group_cache_tree.rb_node; |
| 143 | |
| 144 | while (*p) { |
| 145 | parent = *p; |
| 146 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 147 | cache_node); |
| 148 | if (block_group->key.objectid < cache->key.objectid) { |
| 149 | p = &(*p)->rb_left; |
| 150 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 151 | p = &(*p)->rb_right; |
| 152 | } else { |
| 153 | spin_unlock(&info->block_group_cache_lock); |
| 154 | return -EEXIST; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | rb_link_node(&block_group->cache_node, parent, p); |
| 159 | rb_insert_color(&block_group->cache_node, |
| 160 | &info->block_group_cache_tree); |
| 161 | spin_unlock(&info->block_group_cache_lock); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * This will return the block group at or after bytenr if contains is 0, else |
| 168 | * it will return the block group that contains the bytenr |
| 169 | */ |
| 170 | static struct btrfs_block_group_cache * |
| 171 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 172 | int contains) |
| 173 | { |
| 174 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 175 | struct rb_node *n; |
| 176 | u64 end, start; |
| 177 | |
| 178 | spin_lock(&info->block_group_cache_lock); |
| 179 | n = info->block_group_cache_tree.rb_node; |
| 180 | |
| 181 | while (n) { |
| 182 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 183 | cache_node); |
| 184 | end = cache->key.objectid + cache->key.offset - 1; |
| 185 | start = cache->key.objectid; |
| 186 | |
| 187 | if (bytenr < start) { |
| 188 | if (!contains && (!ret || start < ret->key.objectid)) |
| 189 | ret = cache; |
| 190 | n = n->rb_left; |
| 191 | } else if (bytenr > start) { |
| 192 | if (contains && bytenr <= end) { |
| 193 | ret = cache; |
| 194 | break; |
| 195 | } |
| 196 | n = n->rb_right; |
| 197 | } else { |
| 198 | ret = cache; |
| 199 | break; |
| 200 | } |
| 201 | } |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 202 | if (ret) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 203 | btrfs_get_block_group(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 204 | spin_unlock(&info->block_group_cache_lock); |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 209 | static int add_excluded_extent(struct btrfs_root *root, |
| 210 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 211 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 212 | u64 end = start + num_bytes - 1; |
| 213 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 214 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 215 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 216 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 217 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 220 | static void free_excluded_extents(struct btrfs_root *root, |
| 221 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 222 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 223 | u64 start, end; |
| 224 | |
| 225 | start = cache->key.objectid; |
| 226 | end = start + cache->key.offset - 1; |
| 227 | |
| 228 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 229 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 230 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 231 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 232 | } |
| 233 | |
| 234 | static int exclude_super_stripes(struct btrfs_root *root, |
| 235 | struct btrfs_block_group_cache *cache) |
| 236 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 237 | u64 bytenr; |
| 238 | u64 *logical; |
| 239 | int stripe_len; |
| 240 | int i, nr, ret; |
| 241 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 242 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 243 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 244 | cache->bytes_super += stripe_len; |
| 245 | ret = add_excluded_extent(root, cache->key.objectid, |
| 246 | stripe_len); |
| 247 | BUG_ON(ret); |
| 248 | } |
| 249 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 250 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 251 | bytenr = btrfs_sb_offset(i); |
| 252 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 253 | cache->key.objectid, bytenr, |
| 254 | 0, &logical, &nr, &stripe_len); |
| 255 | BUG_ON(ret); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 256 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 257 | while (nr--) { |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 258 | cache->bytes_super += stripe_len; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 259 | ret = add_excluded_extent(root, logical[nr], |
| 260 | stripe_len); |
| 261 | BUG_ON(ret); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 262 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 263 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 264 | kfree(logical); |
| 265 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 269 | static struct btrfs_caching_control * |
| 270 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 271 | { |
| 272 | struct btrfs_caching_control *ctl; |
| 273 | |
| 274 | spin_lock(&cache->lock); |
| 275 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 276 | spin_unlock(&cache->lock); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 280 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 281 | if (!cache->caching_ctl) { |
| 282 | spin_unlock(&cache->lock); |
| 283 | return NULL; |
| 284 | } |
| 285 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 286 | ctl = cache->caching_ctl; |
| 287 | atomic_inc(&ctl->count); |
| 288 | spin_unlock(&cache->lock); |
| 289 | return ctl; |
| 290 | } |
| 291 | |
| 292 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 293 | { |
| 294 | if (atomic_dec_and_test(&ctl->count)) |
| 295 | kfree(ctl); |
| 296 | } |
| 297 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 298 | /* |
| 299 | * this is only called by cache_block_group, since we could have freed extents |
| 300 | * we need to check the pinned_extents for any extents that can't be used yet |
| 301 | * since their free space will be released as soon as the transaction commits. |
| 302 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 303 | 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] | 304 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 305 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 306 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 307 | int ret; |
| 308 | |
| 309 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 310 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 311 | &extent_start, &extent_end, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 312 | EXTENT_DIRTY | EXTENT_UPTODATE); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 313 | if (ret) |
| 314 | break; |
| 315 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 316 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 317 | start = extent_end + 1; |
| 318 | } else if (extent_start > start && extent_start < end) { |
| 319 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 320 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 321 | ret = btrfs_add_free_space(block_group, start, |
| 322 | size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 323 | BUG_ON(ret); |
| 324 | start = extent_end + 1; |
| 325 | } else { |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | if (start < end) { |
| 331 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 332 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 333 | ret = btrfs_add_free_space(block_group, start, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 334 | BUG_ON(ret); |
| 335 | } |
| 336 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 337 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 338 | } |
| 339 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 340 | static noinline void caching_thread(struct btrfs_work *work) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 341 | { |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 342 | struct btrfs_block_group_cache *block_group; |
| 343 | struct btrfs_fs_info *fs_info; |
| 344 | struct btrfs_caching_control *caching_ctl; |
| 345 | struct btrfs_root *extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 346 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 347 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 348 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 349 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 350 | u64 last = 0; |
| 351 | u32 nritems; |
| 352 | int ret = 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 353 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 354 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 355 | block_group = caching_ctl->block_group; |
| 356 | fs_info = block_group->fs_info; |
| 357 | extent_root = fs_info->extent_root; |
| 358 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 359 | path = btrfs_alloc_path(); |
| 360 | if (!path) |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 361 | goto out; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 362 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 363 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 364 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 365 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 366 | * We don't want to deadlock with somebody trying to allocate a new |
| 367 | * extent for the extent root while also trying to search the extent |
| 368 | * root to add free space. So we skip locking and search the commit |
| 369 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 370 | */ |
| 371 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 372 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 373 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 374 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 375 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 376 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 377 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 378 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 379 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 380 | /* need to make sure the commit_root doesn't disappear */ |
| 381 | down_read(&fs_info->extent_commit_sem); |
| 382 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 383 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 384 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 385 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 386 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 387 | leaf = path->nodes[0]; |
| 388 | nritems = btrfs_header_nritems(leaf); |
| 389 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 390 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 391 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 392 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 393 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 394 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 395 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 396 | if (path->slots[0] < nritems) { |
| 397 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 398 | } else { |
| 399 | ret = find_next_key(path, 0, &key); |
| 400 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 401 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 402 | |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 403 | if (need_resched() || |
| 404 | btrfs_next_leaf(extent_root, path)) { |
| 405 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 406 | btrfs_release_path(path); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 407 | up_read(&fs_info->extent_commit_sem); |
| 408 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 409 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 410 | goto again; |
| 411 | } |
| 412 | leaf = path->nodes[0]; |
| 413 | nritems = btrfs_header_nritems(leaf); |
| 414 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 415 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 416 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 417 | if (key.objectid < block_group->key.objectid) { |
| 418 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 419 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 420 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 421 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 422 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 423 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 424 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 425 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 426 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 427 | total_found += add_new_free_space(block_group, |
| 428 | fs_info, last, |
| 429 | key.objectid); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 430 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 431 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 432 | if (total_found > (1024 * 1024 * 2)) { |
| 433 | total_found = 0; |
| 434 | wake_up(&caching_ctl->wait); |
| 435 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 436 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 437 | path->slots[0]++; |
| 438 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 439 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 440 | |
| 441 | total_found += add_new_free_space(block_group, fs_info, last, |
| 442 | block_group->key.objectid + |
| 443 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 444 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 445 | |
| 446 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 447 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 448 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 449 | spin_unlock(&block_group->lock); |
| 450 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 451 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 452 | btrfs_free_path(path); |
Yan Zheng | 276e680 | 2009-07-30 09:40:40 -0400 | [diff] [blame] | 453 | up_read(&fs_info->extent_commit_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 454 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 455 | free_excluded_extents(extent_root, block_group); |
| 456 | |
| 457 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 458 | out: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 459 | wake_up(&caching_ctl->wait); |
| 460 | |
| 461 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 462 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 463 | } |
| 464 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 465 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
| 466 | struct btrfs_trans_handle *trans, |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 467 | struct btrfs_root *root, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 468 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 469 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 470 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 471 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 472 | int ret = 0; |
| 473 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 474 | smp_mb(); |
| 475 | if (cache->cached != BTRFS_CACHE_NO) |
| 476 | return 0; |
| 477 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 478 | /* |
| 479 | * 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] | 480 | * to have the normal tree locking. Also if we are currently trying to |
| 481 | * allocate blocks for the tree root we can't do the fast caching since |
| 482 | * we likely hold important locks. |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 483 | */ |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 484 | if (trans && (!trans->transaction->in_commit) && |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 485 | (root && root != root->fs_info->tree_root) && |
| 486 | btrfs_test_opt(root, SPACE_CACHE)) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 487 | spin_lock(&cache->lock); |
| 488 | if (cache->cached != BTRFS_CACHE_NO) { |
| 489 | spin_unlock(&cache->lock); |
| 490 | return 0; |
| 491 | } |
| 492 | cache->cached = BTRFS_CACHE_STARTED; |
| 493 | spin_unlock(&cache->lock); |
| 494 | |
| 495 | ret = load_free_space_cache(fs_info, cache); |
| 496 | |
| 497 | spin_lock(&cache->lock); |
| 498 | if (ret == 1) { |
| 499 | cache->cached = BTRFS_CACHE_FINISHED; |
| 500 | cache->last_byte_to_unpin = (u64)-1; |
| 501 | } else { |
| 502 | cache->cached = BTRFS_CACHE_NO; |
| 503 | } |
| 504 | spin_unlock(&cache->lock); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 505 | if (ret == 1) { |
| 506 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 507 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 508 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | if (load_cache_only) |
| 512 | return 0; |
| 513 | |
Miao Xie | fc0e4a3 | 2011-03-24 11:41:21 +0000 | [diff] [blame] | 514 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 515 | BUG_ON(!caching_ctl); |
| 516 | |
| 517 | INIT_LIST_HEAD(&caching_ctl->list); |
| 518 | mutex_init(&caching_ctl->mutex); |
| 519 | init_waitqueue_head(&caching_ctl->wait); |
| 520 | caching_ctl->block_group = cache; |
| 521 | caching_ctl->progress = cache->key.objectid; |
| 522 | /* one for caching kthread, one for caching block group list */ |
| 523 | atomic_set(&caching_ctl->count, 2); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 524 | caching_ctl->work.func = caching_thread; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 525 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 526 | spin_lock(&cache->lock); |
| 527 | if (cache->cached != BTRFS_CACHE_NO) { |
| 528 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 529 | kfree(caching_ctl); |
| 530 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 531 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 532 | cache->caching_ctl = caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 533 | cache->cached = BTRFS_CACHE_STARTED; |
| 534 | spin_unlock(&cache->lock); |
| 535 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 536 | down_write(&fs_info->extent_commit_sem); |
| 537 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
| 538 | up_write(&fs_info->extent_commit_sem); |
| 539 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 540 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 541 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 542 | btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 543 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 544 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 545 | } |
| 546 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 547 | /* |
| 548 | * return the block group that starts at or after bytenr |
| 549 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 550 | static struct btrfs_block_group_cache * |
| 551 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
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 | struct btrfs_block_group_cache *cache; |
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 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 556 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 557 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 558 | } |
| 559 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 560 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 561 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 562 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 563 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 564 | struct btrfs_fs_info *info, |
| 565 | u64 bytenr) |
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 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 568 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 569 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 570 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 571 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 572 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 573 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 574 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 575 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 576 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 577 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 578 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 579 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 580 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
| 581 | BTRFS_BLOCK_GROUP_METADATA; |
| 582 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 583 | rcu_read_lock(); |
| 584 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 585 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 586 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 587 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 588 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 589 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 590 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 591 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 592 | } |
| 593 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 594 | /* |
| 595 | * after adding space to the filesystem, we need to clear the full flags |
| 596 | * on all the space infos. |
| 597 | */ |
| 598 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 599 | { |
| 600 | struct list_head *head = &info->space_info; |
| 601 | struct btrfs_space_info *found; |
| 602 | |
| 603 | rcu_read_lock(); |
| 604 | list_for_each_entry_rcu(found, head, list) |
| 605 | found->full = 0; |
| 606 | rcu_read_unlock(); |
| 607 | } |
| 608 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 609 | static u64 div_factor(u64 num, int factor) |
| 610 | { |
| 611 | if (factor == 10) |
| 612 | return num; |
| 613 | num *= factor; |
| 614 | do_div(num, 10); |
| 615 | return num; |
| 616 | } |
| 617 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 618 | static u64 div_factor_fine(u64 num, int factor) |
| 619 | { |
| 620 | if (factor == 100) |
| 621 | return num; |
| 622 | num *= factor; |
| 623 | do_div(num, 100); |
| 624 | return num; |
| 625 | } |
| 626 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 627 | u64 btrfs_find_block_group(struct btrfs_root *root, |
| 628 | u64 search_start, u64 search_hint, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 629 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 630 | struct btrfs_block_group_cache *cache; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 631 | u64 used; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 632 | u64 last = max(search_hint, search_start); |
| 633 | u64 group_start = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 634 | int full_search = 0; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 635 | int factor = 9; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 636 | int wrapped = 0; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 637 | again: |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 638 | while (1) { |
| 639 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 640 | if (!cache) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 641 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 642 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 643 | spin_lock(&cache->lock); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 644 | last = cache->key.objectid + cache->key.offset; |
| 645 | used = btrfs_block_group_used(&cache->item); |
| 646 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 647 | if ((full_search || !cache->ro) && |
| 648 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 649 | if (used + cache->pinned + cache->reserved < |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 650 | div_factor(cache->key.offset, factor)) { |
| 651 | group_start = cache->key.objectid; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 652 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 653 | btrfs_put_block_group(cache); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 654 | goto found; |
| 655 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 656 | } |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 657 | spin_unlock(&cache->lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 658 | btrfs_put_block_group(cache); |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 659 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 660 | } |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 661 | if (!wrapped) { |
| 662 | last = search_start; |
| 663 | wrapped = 1; |
| 664 | goto again; |
| 665 | } |
| 666 | if (!full_search && factor < 10) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 667 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 668 | full_search = 1; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 669 | factor = 10; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 670 | goto again; |
| 671 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 672 | found: |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 673 | return group_start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 674 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 675 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 676 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 677 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 678 | { |
| 679 | int ret; |
| 680 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 681 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 682 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 683 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 684 | if (!path) |
| 685 | return -ENOMEM; |
| 686 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 687 | key.objectid = start; |
| 688 | key.offset = len; |
| 689 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 690 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 691 | 0, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 692 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 693 | return ret; |
| 694 | } |
| 695 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 696 | /* |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 697 | * helper function to lookup reference count and flags of extent. |
| 698 | * |
| 699 | * the head node for delayed ref is used to store the sum of all the |
| 700 | * reference count modifications queued up in the rbtree. the head |
| 701 | * node may also store the extent flags to set. This way you can check |
| 702 | * to see what the reference count and extent flags would be if all of |
| 703 | * the delayed refs are not processed. |
| 704 | */ |
| 705 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 706 | struct btrfs_root *root, u64 bytenr, |
| 707 | u64 num_bytes, u64 *refs, u64 *flags) |
| 708 | { |
| 709 | struct btrfs_delayed_ref_head *head; |
| 710 | struct btrfs_delayed_ref_root *delayed_refs; |
| 711 | struct btrfs_path *path; |
| 712 | struct btrfs_extent_item *ei; |
| 713 | struct extent_buffer *leaf; |
| 714 | struct btrfs_key key; |
| 715 | u32 item_size; |
| 716 | u64 num_refs; |
| 717 | u64 extent_flags; |
| 718 | int ret; |
| 719 | |
| 720 | path = btrfs_alloc_path(); |
| 721 | if (!path) |
| 722 | return -ENOMEM; |
| 723 | |
| 724 | key.objectid = bytenr; |
| 725 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 726 | key.offset = num_bytes; |
| 727 | if (!trans) { |
| 728 | path->skip_locking = 1; |
| 729 | path->search_commit_root = 1; |
| 730 | } |
| 731 | again: |
| 732 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 733 | &key, path, 0, 0); |
| 734 | if (ret < 0) |
| 735 | goto out_free; |
| 736 | |
| 737 | if (ret == 0) { |
| 738 | leaf = path->nodes[0]; |
| 739 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 740 | if (item_size >= sizeof(*ei)) { |
| 741 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 742 | struct btrfs_extent_item); |
| 743 | num_refs = btrfs_extent_refs(leaf, ei); |
| 744 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 745 | } else { |
| 746 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 747 | struct btrfs_extent_item_v0 *ei0; |
| 748 | BUG_ON(item_size != sizeof(*ei0)); |
| 749 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 750 | struct btrfs_extent_item_v0); |
| 751 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 752 | /* FIXME: this isn't correct for data */ |
| 753 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 754 | #else |
| 755 | BUG(); |
| 756 | #endif |
| 757 | } |
| 758 | BUG_ON(num_refs == 0); |
| 759 | } else { |
| 760 | num_refs = 0; |
| 761 | extent_flags = 0; |
| 762 | ret = 0; |
| 763 | } |
| 764 | |
| 765 | if (!trans) |
| 766 | goto out; |
| 767 | |
| 768 | delayed_refs = &trans->transaction->delayed_refs; |
| 769 | spin_lock(&delayed_refs->lock); |
| 770 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 771 | if (head) { |
| 772 | if (!mutex_trylock(&head->mutex)) { |
| 773 | atomic_inc(&head->node.refs); |
| 774 | spin_unlock(&delayed_refs->lock); |
| 775 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 776 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 777 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 778 | /* |
| 779 | * Mutex was contended, block until it's released and try |
| 780 | * again |
| 781 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 782 | mutex_lock(&head->mutex); |
| 783 | mutex_unlock(&head->mutex); |
| 784 | btrfs_put_delayed_ref(&head->node); |
| 785 | goto again; |
| 786 | } |
| 787 | if (head->extent_op && head->extent_op->update_flags) |
| 788 | extent_flags |= head->extent_op->flags_to_set; |
| 789 | else |
| 790 | BUG_ON(num_refs == 0); |
| 791 | |
| 792 | num_refs += head->node.ref_mod; |
| 793 | mutex_unlock(&head->mutex); |
| 794 | } |
| 795 | spin_unlock(&delayed_refs->lock); |
| 796 | out: |
| 797 | WARN_ON(num_refs == 0); |
| 798 | if (refs) |
| 799 | *refs = num_refs; |
| 800 | if (flags) |
| 801 | *flags = extent_flags; |
| 802 | out_free: |
| 803 | btrfs_free_path(path); |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 808 | * Back reference rules. Back refs have three main goals: |
| 809 | * |
| 810 | * 1) differentiate between all holders of references to an extent so that |
| 811 | * when a reference is dropped we can make sure it was a valid reference |
| 812 | * before freeing the extent. |
| 813 | * |
| 814 | * 2) Provide enough information to quickly find the holders of an extent |
| 815 | * if we notice a given block is corrupted or bad. |
| 816 | * |
| 817 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 818 | * maintenance. This is actually the same as #2, but with a slightly |
| 819 | * different use case. |
| 820 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 821 | * There are two kinds of back refs. The implicit back refs is optimized |
| 822 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 823 | * back refs of this kind provide information about the block's owner tree |
| 824 | * and the pointer's key. These information allow us to find the block by |
| 825 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 826 | * referenced by their owner trees. The location of tree block is recorded |
| 827 | * in the back refs. Actually the full back refs is generic, and can be |
| 828 | * used in all cases the implicit back refs is used. The major shortcoming |
| 829 | * of the full back refs is its overhead. Every time a tree block gets |
| 830 | * COWed, we have to update back refs entry for all pointers in it. |
| 831 | * |
| 832 | * For a newly allocated tree block, we use implicit back refs for |
| 833 | * pointers in it. This means most tree related operations only involve |
| 834 | * implicit back refs. For a tree block created in old transaction, the |
| 835 | * only way to drop a reference to it is COW it. So we can detect the |
| 836 | * event that tree block loses its owner tree's reference and do the |
| 837 | * back refs conversion. |
| 838 | * |
| 839 | * When a tree block is COW'd through a tree, there are four cases: |
| 840 | * |
| 841 | * The reference count of the block is one and the tree is the block's |
| 842 | * owner tree. Nothing to do in this case. |
| 843 | * |
| 844 | * The reference count of the block is one and the tree is not the |
| 845 | * block's owner tree. In this case, full back refs is used for pointers |
| 846 | * in the block. Remove these full back refs, add implicit back refs for |
| 847 | * every pointers in the new block. |
| 848 | * |
| 849 | * The reference count of the block is greater than one and the tree is |
| 850 | * the block's owner tree. In this case, implicit back refs is used for |
| 851 | * pointers in the block. Add full back refs for every pointers in the |
| 852 | * block, increase lower level extents' reference counts. The original |
| 853 | * implicit back refs are entailed to the new block. |
| 854 | * |
| 855 | * The reference count of the block is greater than one and the tree is |
| 856 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 857 | * the new block, increase lower level extents' reference count. |
| 858 | * |
| 859 | * Back Reference Key composing: |
| 860 | * |
| 861 | * The key objectid corresponds to the first byte in the extent, |
| 862 | * The key type is used to differentiate between types of back refs. |
| 863 | * There are different meanings of the key offset for different types |
| 864 | * of back refs. |
| 865 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 866 | * File extents can be referenced by: |
| 867 | * |
| 868 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 869 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 870 | * - different offsets inside a file (bookend extents in file.c) |
| 871 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 872 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 873 | * |
| 874 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 875 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 876 | * - original offset in the file |
| 877 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 878 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 879 | * The key offset for the implicit back refs is hash of the first |
| 880 | * three fields. |
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 | * The extent ref structure for the full back refs has field for: |
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 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 885 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 886 | * The key offset for the implicit back refs is the first byte of |
| 887 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 888 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 889 | * When a file extent is allocated, The implicit back refs is used. |
| 890 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 891 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 892 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 893 | * |
| 894 | * When a file extent is removed file truncation, we find the |
| 895 | * corresponding implicit back refs and check the following fields: |
| 896 | * |
| 897 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 898 | * |
| 899 | * Btree extents can be referenced by: |
| 900 | * |
| 901 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 902 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 903 | * Both the implicit back refs and the full back refs for tree blocks |
| 904 | * only consist of key. The key offset for the implicit back refs is |
| 905 | * objectid of block's owner tree. The key offset for the full back refs |
| 906 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 907 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 908 | * When implicit back refs is used, information about the lowest key and |
| 909 | * level of the tree block are required. These information are stored in |
| 910 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 911 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 912 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 913 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 914 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 915 | struct btrfs_root *root, |
| 916 | struct btrfs_path *path, |
| 917 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 918 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 919 | struct btrfs_extent_item *item; |
| 920 | struct btrfs_extent_item_v0 *ei0; |
| 921 | struct btrfs_extent_ref_v0 *ref0; |
| 922 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 923 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 924 | struct btrfs_key key; |
| 925 | struct btrfs_key found_key; |
| 926 | u32 new_size = sizeof(*item); |
| 927 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 928 | int ret; |
| 929 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 930 | leaf = path->nodes[0]; |
| 931 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 932 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 933 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 934 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 935 | struct btrfs_extent_item_v0); |
| 936 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 937 | |
| 938 | if (owner == (u64)-1) { |
| 939 | while (1) { |
| 940 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 941 | ret = btrfs_next_leaf(root, path); |
| 942 | if (ret < 0) |
| 943 | return ret; |
| 944 | BUG_ON(ret > 0); |
| 945 | leaf = path->nodes[0]; |
| 946 | } |
| 947 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 948 | path->slots[0]); |
| 949 | BUG_ON(key.objectid != found_key.objectid); |
| 950 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 951 | path->slots[0]++; |
| 952 | continue; |
| 953 | } |
| 954 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 955 | struct btrfs_extent_ref_v0); |
| 956 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 957 | break; |
| 958 | } |
| 959 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 960 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 961 | |
| 962 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 963 | new_size += sizeof(*bi); |
| 964 | |
| 965 | new_size -= sizeof(*ei0); |
| 966 | ret = btrfs_search_slot(trans, root, &key, path, |
| 967 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 968 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 969 | return ret; |
| 970 | BUG_ON(ret); |
| 971 | |
| 972 | ret = btrfs_extend_item(trans, root, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 973 | |
| 974 | leaf = path->nodes[0]; |
| 975 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 976 | btrfs_set_extent_refs(leaf, item, refs); |
| 977 | /* FIXME: get real generation */ |
| 978 | btrfs_set_extent_generation(leaf, item, 0); |
| 979 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 980 | btrfs_set_extent_flags(leaf, item, |
| 981 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 982 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 983 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 984 | /* FIXME: get first key of the block */ |
| 985 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 986 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 987 | } else { |
| 988 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 989 | } |
| 990 | btrfs_mark_buffer_dirty(leaf); |
| 991 | return 0; |
| 992 | } |
| 993 | #endif |
| 994 | |
| 995 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 996 | { |
| 997 | u32 high_crc = ~(u32)0; |
| 998 | u32 low_crc = ~(u32)0; |
| 999 | __le64 lenum; |
| 1000 | |
| 1001 | lenum = cpu_to_le64(root_objectid); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1002 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1003 | lenum = cpu_to_le64(owner); |
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 | lenum = cpu_to_le64(offset); |
David Woodhouse | 163e783 | 2009-04-19 13:02:41 +0100 | [diff] [blame] | 1006 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1007 | |
| 1008 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1009 | } |
| 1010 | |
| 1011 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1012 | struct btrfs_extent_data_ref *ref) |
| 1013 | { |
| 1014 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1015 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1016 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1017 | } |
| 1018 | |
| 1019 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1020 | struct btrfs_extent_data_ref *ref, |
| 1021 | u64 root_objectid, u64 owner, u64 offset) |
| 1022 | { |
| 1023 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1024 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1025 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1026 | return 0; |
| 1027 | return 1; |
| 1028 | } |
| 1029 | |
| 1030 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1031 | struct btrfs_root *root, |
| 1032 | struct btrfs_path *path, |
| 1033 | u64 bytenr, u64 parent, |
| 1034 | u64 root_objectid, |
| 1035 | u64 owner, u64 offset) |
| 1036 | { |
| 1037 | struct btrfs_key key; |
| 1038 | struct btrfs_extent_data_ref *ref; |
| 1039 | struct extent_buffer *leaf; |
| 1040 | u32 nritems; |
| 1041 | int ret; |
| 1042 | int recow; |
| 1043 | int err = -ENOENT; |
| 1044 | |
| 1045 | key.objectid = bytenr; |
| 1046 | if (parent) { |
| 1047 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1048 | key.offset = parent; |
| 1049 | } else { |
| 1050 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1051 | key.offset = hash_extent_data_ref(root_objectid, |
| 1052 | owner, offset); |
| 1053 | } |
| 1054 | again: |
| 1055 | recow = 0; |
| 1056 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1057 | if (ret < 0) { |
| 1058 | err = ret; |
| 1059 | goto fail; |
| 1060 | } |
| 1061 | |
| 1062 | if (parent) { |
| 1063 | if (!ret) |
| 1064 | return 0; |
| 1065 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1066 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1067 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1068 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1069 | if (ret < 0) { |
| 1070 | err = ret; |
| 1071 | goto fail; |
| 1072 | } |
| 1073 | if (!ret) |
| 1074 | return 0; |
| 1075 | #endif |
| 1076 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1080 | nritems = btrfs_header_nritems(leaf); |
| 1081 | while (1) { |
| 1082 | if (path->slots[0] >= nritems) { |
| 1083 | ret = btrfs_next_leaf(root, path); |
| 1084 | if (ret < 0) |
| 1085 | err = ret; |
| 1086 | if (ret) |
| 1087 | goto fail; |
| 1088 | |
| 1089 | leaf = path->nodes[0]; |
| 1090 | nritems = btrfs_header_nritems(leaf); |
| 1091 | recow = 1; |
| 1092 | } |
| 1093 | |
| 1094 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1095 | if (key.objectid != bytenr || |
| 1096 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1097 | goto fail; |
| 1098 | |
| 1099 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1100 | struct btrfs_extent_data_ref); |
| 1101 | |
| 1102 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1103 | owner, offset)) { |
| 1104 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1105 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1106 | goto again; |
| 1107 | } |
| 1108 | err = 0; |
| 1109 | break; |
| 1110 | } |
| 1111 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1112 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1113 | fail: |
| 1114 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1115 | } |
| 1116 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1117 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1118 | struct btrfs_root *root, |
| 1119 | struct btrfs_path *path, |
| 1120 | u64 bytenr, u64 parent, |
| 1121 | u64 root_objectid, u64 owner, |
| 1122 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1123 | { |
| 1124 | struct btrfs_key key; |
| 1125 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1126 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1127 | u32 num_refs; |
| 1128 | int ret; |
| 1129 | |
| 1130 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1131 | if (parent) { |
| 1132 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1133 | key.offset = parent; |
| 1134 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1135 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1136 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1137 | key.offset = hash_extent_data_ref(root_objectid, |
| 1138 | owner, offset); |
| 1139 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1140 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1141 | |
| 1142 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1143 | if (ret && ret != -EEXIST) |
| 1144 | goto fail; |
| 1145 | |
| 1146 | leaf = path->nodes[0]; |
| 1147 | if (parent) { |
| 1148 | struct btrfs_shared_data_ref *ref; |
| 1149 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1150 | struct btrfs_shared_data_ref); |
| 1151 | if (ret == 0) { |
| 1152 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1153 | } else { |
| 1154 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1155 | num_refs += refs_to_add; |
| 1156 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1157 | } |
| 1158 | } else { |
| 1159 | struct btrfs_extent_data_ref *ref; |
| 1160 | while (ret == -EEXIST) { |
| 1161 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1162 | struct btrfs_extent_data_ref); |
| 1163 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1164 | owner, offset)) |
| 1165 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1166 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1167 | key.offset++; |
| 1168 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1169 | size); |
| 1170 | if (ret && ret != -EEXIST) |
| 1171 | goto fail; |
| 1172 | |
| 1173 | leaf = path->nodes[0]; |
| 1174 | } |
| 1175 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1176 | struct btrfs_extent_data_ref); |
| 1177 | if (ret == 0) { |
| 1178 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1179 | root_objectid); |
| 1180 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1181 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1182 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1183 | } else { |
| 1184 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1185 | num_refs += refs_to_add; |
| 1186 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1187 | } |
| 1188 | } |
| 1189 | btrfs_mark_buffer_dirty(leaf); |
| 1190 | ret = 0; |
| 1191 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1192 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1193 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1194 | } |
| 1195 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1196 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1197 | struct btrfs_root *root, |
| 1198 | struct btrfs_path *path, |
| 1199 | int refs_to_drop) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1200 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1201 | struct btrfs_key key; |
| 1202 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1203 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1204 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1205 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1206 | int ret = 0; |
| 1207 | |
| 1208 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1209 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1210 | |
| 1211 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1212 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1213 | struct btrfs_extent_data_ref); |
| 1214 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1215 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1216 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1217 | struct btrfs_shared_data_ref); |
| 1218 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1219 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1220 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1221 | struct btrfs_extent_ref_v0 *ref0; |
| 1222 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1223 | struct btrfs_extent_ref_v0); |
| 1224 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1225 | #endif |
| 1226 | } else { |
| 1227 | BUG(); |
| 1228 | } |
| 1229 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1230 | BUG_ON(num_refs < refs_to_drop); |
| 1231 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1232 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1233 | if (num_refs == 0) { |
| 1234 | ret = btrfs_del_item(trans, root, path); |
| 1235 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1236 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1237 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1238 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1239 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1240 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1241 | else { |
| 1242 | struct btrfs_extent_ref_v0 *ref0; |
| 1243 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1244 | struct btrfs_extent_ref_v0); |
| 1245 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1246 | } |
| 1247 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1248 | btrfs_mark_buffer_dirty(leaf); |
| 1249 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1250 | return ret; |
| 1251 | } |
| 1252 | |
| 1253 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1254 | struct btrfs_path *path, |
| 1255 | struct btrfs_extent_inline_ref *iref) |
| 1256 | { |
| 1257 | struct btrfs_key key; |
| 1258 | struct extent_buffer *leaf; |
| 1259 | struct btrfs_extent_data_ref *ref1; |
| 1260 | struct btrfs_shared_data_ref *ref2; |
| 1261 | u32 num_refs = 0; |
| 1262 | |
| 1263 | leaf = path->nodes[0]; |
| 1264 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1265 | if (iref) { |
| 1266 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1267 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1268 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1269 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1270 | } else { |
| 1271 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1272 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1273 | } |
| 1274 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1275 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1276 | struct btrfs_extent_data_ref); |
| 1277 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1278 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1279 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1280 | struct btrfs_shared_data_ref); |
| 1281 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1282 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1283 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1284 | struct btrfs_extent_ref_v0 *ref0; |
| 1285 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1286 | struct btrfs_extent_ref_v0); |
| 1287 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1288 | #endif |
| 1289 | } else { |
| 1290 | WARN_ON(1); |
| 1291 | } |
| 1292 | return num_refs; |
| 1293 | } |
| 1294 | |
| 1295 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1296 | struct btrfs_root *root, |
| 1297 | struct btrfs_path *path, |
| 1298 | u64 bytenr, u64 parent, |
| 1299 | u64 root_objectid) |
| 1300 | { |
| 1301 | struct btrfs_key key; |
| 1302 | int ret; |
| 1303 | |
| 1304 | key.objectid = bytenr; |
| 1305 | if (parent) { |
| 1306 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1307 | key.offset = parent; |
| 1308 | } else { |
| 1309 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1310 | key.offset = root_objectid; |
| 1311 | } |
| 1312 | |
| 1313 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1314 | if (ret > 0) |
| 1315 | ret = -ENOENT; |
| 1316 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1317 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1318 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1319 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1320 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1321 | if (ret > 0) |
| 1322 | ret = -ENOENT; |
| 1323 | } |
| 1324 | #endif |
| 1325 | return ret; |
| 1326 | } |
| 1327 | |
| 1328 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1329 | struct btrfs_root *root, |
| 1330 | struct btrfs_path *path, |
| 1331 | u64 bytenr, u64 parent, |
| 1332 | u64 root_objectid) |
| 1333 | { |
| 1334 | struct btrfs_key key; |
| 1335 | int ret; |
| 1336 | |
| 1337 | key.objectid = bytenr; |
| 1338 | if (parent) { |
| 1339 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1340 | key.offset = parent; |
| 1341 | } else { |
| 1342 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1343 | key.offset = root_objectid; |
| 1344 | } |
| 1345 | |
| 1346 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1347 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1348 | return ret; |
| 1349 | } |
| 1350 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1351 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1352 | { |
| 1353 | int type; |
| 1354 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1355 | if (parent > 0) |
| 1356 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1357 | else |
| 1358 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1359 | } else { |
| 1360 | if (parent > 0) |
| 1361 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1362 | else |
| 1363 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1364 | } |
| 1365 | return type; |
| 1366 | } |
| 1367 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1368 | static int find_next_key(struct btrfs_path *path, int level, |
| 1369 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1370 | |
| 1371 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1372 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1373 | if (!path->nodes[level]) |
| 1374 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1375 | if (path->slots[level] + 1 >= |
| 1376 | btrfs_header_nritems(path->nodes[level])) |
| 1377 | continue; |
| 1378 | if (level == 0) |
| 1379 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1380 | path->slots[level] + 1); |
| 1381 | else |
| 1382 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1383 | path->slots[level] + 1); |
| 1384 | return 0; |
| 1385 | } |
| 1386 | return 1; |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1391 | * to the address of inline back ref, and 0 is returned. |
| 1392 | * |
| 1393 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1394 | * should be inserted, and -ENOENT is returned. |
| 1395 | * |
| 1396 | * if insert is true and there are too many inline back refs, the path |
| 1397 | * points to the extent item, and -EAGAIN is returned. |
| 1398 | * |
| 1399 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1400 | * items in the tree are ordered. |
| 1401 | */ |
| 1402 | static noinline_for_stack |
| 1403 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1404 | struct btrfs_root *root, |
| 1405 | struct btrfs_path *path, |
| 1406 | struct btrfs_extent_inline_ref **ref_ret, |
| 1407 | u64 bytenr, u64 num_bytes, |
| 1408 | u64 parent, u64 root_objectid, |
| 1409 | u64 owner, u64 offset, int insert) |
| 1410 | { |
| 1411 | struct btrfs_key key; |
| 1412 | struct extent_buffer *leaf; |
| 1413 | struct btrfs_extent_item *ei; |
| 1414 | struct btrfs_extent_inline_ref *iref; |
| 1415 | u64 flags; |
| 1416 | u64 item_size; |
| 1417 | unsigned long ptr; |
| 1418 | unsigned long end; |
| 1419 | int extra_size; |
| 1420 | int type; |
| 1421 | int want; |
| 1422 | int ret; |
| 1423 | int err = 0; |
| 1424 | |
| 1425 | key.objectid = bytenr; |
| 1426 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1427 | key.offset = num_bytes; |
| 1428 | |
| 1429 | want = extent_ref_type(parent, owner); |
| 1430 | if (insert) { |
| 1431 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1432 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1433 | } else |
| 1434 | extra_size = -1; |
| 1435 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1436 | if (ret < 0) { |
| 1437 | err = ret; |
| 1438 | goto out; |
| 1439 | } |
| 1440 | BUG_ON(ret); |
| 1441 | |
| 1442 | leaf = path->nodes[0]; |
| 1443 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1444 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1445 | if (item_size < sizeof(*ei)) { |
| 1446 | if (!insert) { |
| 1447 | err = -ENOENT; |
| 1448 | goto out; |
| 1449 | } |
| 1450 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1451 | extra_size); |
| 1452 | if (ret < 0) { |
| 1453 | err = ret; |
| 1454 | goto out; |
| 1455 | } |
| 1456 | leaf = path->nodes[0]; |
| 1457 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1458 | } |
| 1459 | #endif |
| 1460 | BUG_ON(item_size < sizeof(*ei)); |
| 1461 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1462 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1463 | flags = btrfs_extent_flags(leaf, ei); |
| 1464 | |
| 1465 | ptr = (unsigned long)(ei + 1); |
| 1466 | end = (unsigned long)ei + item_size; |
| 1467 | |
| 1468 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1469 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1470 | BUG_ON(ptr > end); |
| 1471 | } else { |
| 1472 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); |
| 1473 | } |
| 1474 | |
| 1475 | err = -ENOENT; |
| 1476 | while (1) { |
| 1477 | if (ptr >= end) { |
| 1478 | WARN_ON(ptr > end); |
| 1479 | break; |
| 1480 | } |
| 1481 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1482 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1483 | if (want < type) |
| 1484 | break; |
| 1485 | if (want > type) { |
| 1486 | ptr += btrfs_extent_inline_ref_size(type); |
| 1487 | continue; |
| 1488 | } |
| 1489 | |
| 1490 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1491 | struct btrfs_extent_data_ref *dref; |
| 1492 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1493 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1494 | owner, offset)) { |
| 1495 | err = 0; |
| 1496 | break; |
| 1497 | } |
| 1498 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1499 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1500 | break; |
| 1501 | } else { |
| 1502 | u64 ref_offset; |
| 1503 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1504 | if (parent > 0) { |
| 1505 | if (parent == ref_offset) { |
| 1506 | err = 0; |
| 1507 | break; |
| 1508 | } |
| 1509 | if (ref_offset < parent) |
| 1510 | break; |
| 1511 | } else { |
| 1512 | if (root_objectid == ref_offset) { |
| 1513 | err = 0; |
| 1514 | break; |
| 1515 | } |
| 1516 | if (ref_offset < root_objectid) |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
| 1520 | ptr += btrfs_extent_inline_ref_size(type); |
| 1521 | } |
| 1522 | if (err == -ENOENT && insert) { |
| 1523 | if (item_size + extra_size >= |
| 1524 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1525 | err = -EAGAIN; |
| 1526 | goto out; |
| 1527 | } |
| 1528 | /* |
| 1529 | * To add new inline back ref, we have to make sure |
| 1530 | * there is no corresponding back ref item. |
| 1531 | * For simplicity, we just do not add new inline back |
| 1532 | * ref if there is any kind of item for this block |
| 1533 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1534 | if (find_next_key(path, 0, &key) == 0 && |
| 1535 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1536 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1537 | err = -EAGAIN; |
| 1538 | goto out; |
| 1539 | } |
| 1540 | } |
| 1541 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1542 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1543 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1544 | path->keep_locks = 0; |
| 1545 | btrfs_unlock_up_safe(path, 1); |
| 1546 | } |
| 1547 | return err; |
| 1548 | } |
| 1549 | |
| 1550 | /* |
| 1551 | * helper to add new inline back ref |
| 1552 | */ |
| 1553 | static noinline_for_stack |
| 1554 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1555 | struct btrfs_root *root, |
| 1556 | struct btrfs_path *path, |
| 1557 | struct btrfs_extent_inline_ref *iref, |
| 1558 | u64 parent, u64 root_objectid, |
| 1559 | u64 owner, u64 offset, int refs_to_add, |
| 1560 | struct btrfs_delayed_extent_op *extent_op) |
| 1561 | { |
| 1562 | struct extent_buffer *leaf; |
| 1563 | struct btrfs_extent_item *ei; |
| 1564 | unsigned long ptr; |
| 1565 | unsigned long end; |
| 1566 | unsigned long item_offset; |
| 1567 | u64 refs; |
| 1568 | int size; |
| 1569 | int type; |
| 1570 | int ret; |
| 1571 | |
| 1572 | leaf = path->nodes[0]; |
| 1573 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1574 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1575 | |
| 1576 | type = extent_ref_type(parent, owner); |
| 1577 | size = btrfs_extent_inline_ref_size(type); |
| 1578 | |
| 1579 | ret = btrfs_extend_item(trans, root, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1580 | |
| 1581 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1582 | refs = btrfs_extent_refs(leaf, ei); |
| 1583 | refs += refs_to_add; |
| 1584 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1585 | if (extent_op) |
| 1586 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1587 | |
| 1588 | ptr = (unsigned long)ei + item_offset; |
| 1589 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1590 | if (ptr < end - size) |
| 1591 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1592 | end - size - ptr); |
| 1593 | |
| 1594 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1595 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1596 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1597 | struct btrfs_extent_data_ref *dref; |
| 1598 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1599 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1600 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1601 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1602 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1603 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1604 | struct btrfs_shared_data_ref *sref; |
| 1605 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1606 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1607 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1608 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1609 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1610 | } else { |
| 1611 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1612 | } |
| 1613 | btrfs_mark_buffer_dirty(leaf); |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1618 | struct btrfs_root *root, |
| 1619 | struct btrfs_path *path, |
| 1620 | struct btrfs_extent_inline_ref **ref_ret, |
| 1621 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1622 | u64 root_objectid, u64 owner, u64 offset) |
| 1623 | { |
| 1624 | int ret; |
| 1625 | |
| 1626 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1627 | bytenr, num_bytes, parent, |
| 1628 | root_objectid, owner, offset, 0); |
| 1629 | if (ret != -ENOENT) |
| 1630 | return ret; |
| 1631 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1632 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1633 | *ref_ret = NULL; |
| 1634 | |
| 1635 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1636 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1637 | root_objectid); |
| 1638 | } else { |
| 1639 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1640 | root_objectid, owner, offset); |
| 1641 | } |
| 1642 | return ret; |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * helper to update/remove inline back ref |
| 1647 | */ |
| 1648 | static noinline_for_stack |
| 1649 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1650 | struct btrfs_root *root, |
| 1651 | struct btrfs_path *path, |
| 1652 | struct btrfs_extent_inline_ref *iref, |
| 1653 | int refs_to_mod, |
| 1654 | struct btrfs_delayed_extent_op *extent_op) |
| 1655 | { |
| 1656 | struct extent_buffer *leaf; |
| 1657 | struct btrfs_extent_item *ei; |
| 1658 | struct btrfs_extent_data_ref *dref = NULL; |
| 1659 | struct btrfs_shared_data_ref *sref = NULL; |
| 1660 | unsigned long ptr; |
| 1661 | unsigned long end; |
| 1662 | u32 item_size; |
| 1663 | int size; |
| 1664 | int type; |
| 1665 | int ret; |
| 1666 | u64 refs; |
| 1667 | |
| 1668 | leaf = path->nodes[0]; |
| 1669 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1670 | refs = btrfs_extent_refs(leaf, ei); |
| 1671 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1672 | refs += refs_to_mod; |
| 1673 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1674 | if (extent_op) |
| 1675 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1676 | |
| 1677 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1678 | |
| 1679 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1680 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1681 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1682 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1683 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1684 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1685 | } else { |
| 1686 | refs = 1; |
| 1687 | BUG_ON(refs_to_mod != -1); |
| 1688 | } |
| 1689 | |
| 1690 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1691 | refs += refs_to_mod; |
| 1692 | |
| 1693 | if (refs > 0) { |
| 1694 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1695 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1696 | else |
| 1697 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1698 | } else { |
| 1699 | size = btrfs_extent_inline_ref_size(type); |
| 1700 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1701 | ptr = (unsigned long)iref; |
| 1702 | end = (unsigned long)ei + item_size; |
| 1703 | if (ptr + size < end) |
| 1704 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1705 | end - ptr - size); |
| 1706 | item_size -= size; |
| 1707 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1708 | } |
| 1709 | btrfs_mark_buffer_dirty(leaf); |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | static noinline_for_stack |
| 1714 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1715 | struct btrfs_root *root, |
| 1716 | struct btrfs_path *path, |
| 1717 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1718 | u64 root_objectid, u64 owner, |
| 1719 | u64 offset, int refs_to_add, |
| 1720 | struct btrfs_delayed_extent_op *extent_op) |
| 1721 | { |
| 1722 | struct btrfs_extent_inline_ref *iref; |
| 1723 | int ret; |
| 1724 | |
| 1725 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1726 | bytenr, num_bytes, parent, |
| 1727 | root_objectid, owner, offset, 1); |
| 1728 | if (ret == 0) { |
| 1729 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
| 1730 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1731 | refs_to_add, extent_op); |
| 1732 | } else if (ret == -ENOENT) { |
| 1733 | ret = setup_inline_extent_backref(trans, root, path, iref, |
| 1734 | parent, root_objectid, |
| 1735 | owner, offset, refs_to_add, |
| 1736 | extent_op); |
| 1737 | } |
| 1738 | return ret; |
| 1739 | } |
| 1740 | |
| 1741 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1742 | struct btrfs_root *root, |
| 1743 | struct btrfs_path *path, |
| 1744 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1745 | u64 owner, u64 offset, int refs_to_add) |
| 1746 | { |
| 1747 | int ret; |
| 1748 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1749 | BUG_ON(refs_to_add != 1); |
| 1750 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1751 | parent, root_objectid); |
| 1752 | } else { |
| 1753 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1754 | parent, root_objectid, |
| 1755 | owner, offset, refs_to_add); |
| 1756 | } |
| 1757 | return ret; |
| 1758 | } |
| 1759 | |
| 1760 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1761 | struct btrfs_root *root, |
| 1762 | struct btrfs_path *path, |
| 1763 | struct btrfs_extent_inline_ref *iref, |
| 1764 | int refs_to_drop, int is_data) |
| 1765 | { |
| 1766 | int ret; |
| 1767 | |
| 1768 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1769 | if (iref) { |
| 1770 | ret = update_inline_extent_backref(trans, root, path, iref, |
| 1771 | -refs_to_drop, NULL); |
| 1772 | } else if (is_data) { |
| 1773 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); |
| 1774 | } else { |
| 1775 | ret = btrfs_del_item(trans, root, path); |
| 1776 | } |
| 1777 | return ret; |
| 1778 | } |
| 1779 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1780 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1781 | u64 start, u64 len) |
| 1782 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1783 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1784 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1785 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1786 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1787 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1788 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1789 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1790 | u64 discarded_bytes = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1791 | struct btrfs_multi_bio *multi = NULL; |
| 1792 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1793 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1794 | /* Tell the block device(s) that the sectors can be discarded */ |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1795 | ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, |
| 1796 | bytenr, &num_bytes, &multi, 0); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1797 | if (!ret) { |
| 1798 | struct btrfs_bio_stripe *stripe = multi->stripes; |
| 1799 | int i; |
| 1800 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1801 | |
| 1802 | for (i = 0; i < multi->num_stripes; i++, stripe++) { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1803 | if (!stripe->dev->can_discard) |
| 1804 | continue; |
| 1805 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1806 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1807 | stripe->physical, |
| 1808 | stripe->length); |
| 1809 | if (!ret) |
| 1810 | discarded_bytes += stripe->length; |
| 1811 | else if (ret != -EOPNOTSUPP) |
| 1812 | break; |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1813 | |
| 1814 | /* |
| 1815 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1816 | * just ignore the return value so we don't screw up |
| 1817 | * people calling discard_extent. |
| 1818 | */ |
| 1819 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1820 | } |
| 1821 | kfree(multi); |
| 1822 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1823 | |
| 1824 | if (actual_bytes) |
| 1825 | *actual_bytes = discarded_bytes; |
| 1826 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1827 | |
| 1828 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1829 | } |
| 1830 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1831 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1832 | struct btrfs_root *root, |
| 1833 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1834 | u64 root_objectid, u64 owner, u64 offset) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1835 | { |
| 1836 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1837 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1838 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1839 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1840 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1841 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 1842 | parent, root_objectid, (int)owner, |
| 1843 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1844 | } else { |
| 1845 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 1846 | parent, root_objectid, owner, offset, |
| 1847 | BTRFS_ADD_DELAYED_REF, NULL); |
| 1848 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1849 | return ret; |
| 1850 | } |
| 1851 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1852 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1853 | struct btrfs_root *root, |
| 1854 | u64 bytenr, u64 num_bytes, |
| 1855 | u64 parent, u64 root_objectid, |
| 1856 | u64 owner, u64 offset, int refs_to_add, |
| 1857 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1858 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1859 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1860 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1861 | struct btrfs_extent_item *item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1862 | u64 refs; |
| 1863 | int ret; |
| 1864 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1865 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1866 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1867 | if (!path) |
| 1868 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1869 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1870 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1871 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1872 | /* this will setup the path even if it fails to insert the back ref */ |
| 1873 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, |
| 1874 | path, bytenr, num_bytes, parent, |
| 1875 | root_objectid, owner, offset, |
| 1876 | refs_to_add, extent_op); |
| 1877 | if (ret == 0) |
| 1878 | goto out; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1879 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1880 | if (ret != -EAGAIN) { |
| 1881 | err = ret; |
| 1882 | goto out; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1883 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1884 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1885 | leaf = path->nodes[0]; |
| 1886 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1887 | refs = btrfs_extent_refs(leaf, item); |
| 1888 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 1889 | if (extent_op) |
| 1890 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1891 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1892 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1893 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1894 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1895 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 1896 | path->leave_spinning = 1; |
| 1897 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1898 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1899 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1900 | path, bytenr, parent, root_objectid, |
| 1901 | owner, offset, refs_to_add); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1902 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1903 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1904 | btrfs_free_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1905 | return err; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1906 | } |
| 1907 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1908 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 1909 | struct btrfs_root *root, |
| 1910 | struct btrfs_delayed_ref_node *node, |
| 1911 | struct btrfs_delayed_extent_op *extent_op, |
| 1912 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1913 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1914 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1915 | struct btrfs_delayed_data_ref *ref; |
| 1916 | struct btrfs_key ins; |
| 1917 | u64 parent = 0; |
| 1918 | u64 ref_root = 0; |
| 1919 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1920 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1921 | ins.objectid = node->bytenr; |
| 1922 | ins.offset = node->num_bytes; |
| 1923 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1924 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1925 | ref = btrfs_delayed_node_to_data_ref(node); |
| 1926 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 1927 | parent = ref->parent; |
| 1928 | else |
| 1929 | ref_root = ref->root; |
| 1930 | |
| 1931 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 1932 | if (extent_op) { |
| 1933 | BUG_ON(extent_op->update_key); |
| 1934 | flags |= extent_op->flags_to_set; |
| 1935 | } |
| 1936 | ret = alloc_reserved_file_extent(trans, root, |
| 1937 | parent, ref_root, flags, |
| 1938 | ref->objectid, ref->offset, |
| 1939 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1940 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 1941 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 1942 | node->num_bytes, parent, |
| 1943 | ref_root, ref->objectid, |
| 1944 | ref->offset, node->ref_mod, |
| 1945 | extent_op); |
| 1946 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 1947 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 1948 | node->num_bytes, parent, |
| 1949 | ref_root, ref->objectid, |
| 1950 | ref->offset, node->ref_mod, |
| 1951 | extent_op); |
| 1952 | } else { |
| 1953 | BUG(); |
| 1954 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1955 | return ret; |
| 1956 | } |
| 1957 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1958 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 1959 | struct extent_buffer *leaf, |
| 1960 | struct btrfs_extent_item *ei) |
| 1961 | { |
| 1962 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 1963 | if (extent_op->update_flags) { |
| 1964 | flags |= extent_op->flags_to_set; |
| 1965 | btrfs_set_extent_flags(leaf, ei, flags); |
| 1966 | } |
| 1967 | |
| 1968 | if (extent_op->update_key) { |
| 1969 | struct btrfs_tree_block_info *bi; |
| 1970 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 1971 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 1972 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 1977 | struct btrfs_root *root, |
| 1978 | struct btrfs_delayed_ref_node *node, |
| 1979 | struct btrfs_delayed_extent_op *extent_op) |
| 1980 | { |
| 1981 | struct btrfs_key key; |
| 1982 | struct btrfs_path *path; |
| 1983 | struct btrfs_extent_item *ei; |
| 1984 | struct extent_buffer *leaf; |
| 1985 | u32 item_size; |
| 1986 | int ret; |
| 1987 | int err = 0; |
| 1988 | |
| 1989 | path = btrfs_alloc_path(); |
| 1990 | if (!path) |
| 1991 | return -ENOMEM; |
| 1992 | |
| 1993 | key.objectid = node->bytenr; |
| 1994 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1995 | key.offset = node->num_bytes; |
| 1996 | |
| 1997 | path->reada = 1; |
| 1998 | path->leave_spinning = 1; |
| 1999 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 2000 | path, 0, 1); |
| 2001 | if (ret < 0) { |
| 2002 | err = ret; |
| 2003 | goto out; |
| 2004 | } |
| 2005 | if (ret > 0) { |
| 2006 | err = -EIO; |
| 2007 | goto out; |
| 2008 | } |
| 2009 | |
| 2010 | leaf = path->nodes[0]; |
| 2011 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2012 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2013 | if (item_size < sizeof(*ei)) { |
| 2014 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 2015 | path, (u64)-1, 0); |
| 2016 | if (ret < 0) { |
| 2017 | err = ret; |
| 2018 | goto out; |
| 2019 | } |
| 2020 | leaf = path->nodes[0]; |
| 2021 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2022 | } |
| 2023 | #endif |
| 2024 | BUG_ON(item_size < sizeof(*ei)); |
| 2025 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2026 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2027 | |
| 2028 | btrfs_mark_buffer_dirty(leaf); |
| 2029 | out: |
| 2030 | btrfs_free_path(path); |
| 2031 | return err; |
| 2032 | } |
| 2033 | |
| 2034 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2035 | struct btrfs_root *root, |
| 2036 | struct btrfs_delayed_ref_node *node, |
| 2037 | struct btrfs_delayed_extent_op *extent_op, |
| 2038 | int insert_reserved) |
| 2039 | { |
| 2040 | int ret = 0; |
| 2041 | struct btrfs_delayed_tree_ref *ref; |
| 2042 | struct btrfs_key ins; |
| 2043 | u64 parent = 0; |
| 2044 | u64 ref_root = 0; |
| 2045 | |
| 2046 | ins.objectid = node->bytenr; |
| 2047 | ins.offset = node->num_bytes; |
| 2048 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2049 | |
| 2050 | ref = btrfs_delayed_node_to_tree_ref(node); |
| 2051 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2052 | parent = ref->parent; |
| 2053 | else |
| 2054 | ref_root = ref->root; |
| 2055 | |
| 2056 | BUG_ON(node->ref_mod != 1); |
| 2057 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
| 2058 | BUG_ON(!extent_op || !extent_op->update_flags || |
| 2059 | !extent_op->update_key); |
| 2060 | ret = alloc_reserved_tree_block(trans, root, |
| 2061 | parent, ref_root, |
| 2062 | extent_op->flags_to_set, |
| 2063 | &extent_op->key, |
| 2064 | ref->level, &ins); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2065 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2066 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2067 | node->num_bytes, parent, ref_root, |
| 2068 | ref->level, 0, 1, extent_op); |
| 2069 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2070 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2071 | node->num_bytes, parent, ref_root, |
| 2072 | ref->level, 0, 1, extent_op); |
| 2073 | } else { |
| 2074 | BUG(); |
| 2075 | } |
| 2076 | return ret; |
| 2077 | } |
| 2078 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2079 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2080 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2081 | struct btrfs_root *root, |
| 2082 | struct btrfs_delayed_ref_node *node, |
| 2083 | struct btrfs_delayed_extent_op *extent_op, |
| 2084 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2085 | { |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2086 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2087 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2088 | struct btrfs_delayed_ref_head *head; |
| 2089 | /* |
| 2090 | * we've hit the end of the chain and we were supposed |
| 2091 | * to insert this extent into the tree. But, it got |
| 2092 | * deleted before we ever needed to insert it, so all |
| 2093 | * we have to do is clean up the accounting |
| 2094 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2095 | BUG_ON(extent_op); |
| 2096 | head = btrfs_delayed_node_to_head(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2097 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2098 | btrfs_pin_extent(root, node->bytenr, |
| 2099 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2100 | if (head->is_data) { |
| 2101 | ret = btrfs_del_csums(trans, root, |
| 2102 | node->bytenr, |
| 2103 | node->num_bytes); |
| 2104 | BUG_ON(ret); |
| 2105 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2106 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2107 | mutex_unlock(&head->mutex); |
| 2108 | return 0; |
| 2109 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2110 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2111 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2112 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2113 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2114 | insert_reserved); |
| 2115 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2116 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2117 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2118 | insert_reserved); |
| 2119 | else |
| 2120 | BUG(); |
| 2121 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2122 | } |
| 2123 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2124 | static noinline struct btrfs_delayed_ref_node * |
| 2125 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2126 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2127 | struct rb_node *node; |
| 2128 | struct btrfs_delayed_ref_node *ref; |
| 2129 | int action = BTRFS_ADD_DELAYED_REF; |
| 2130 | again: |
| 2131 | /* |
| 2132 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2133 | * this prevents ref count from going down to zero when |
| 2134 | * there still are pending delayed ref. |
| 2135 | */ |
| 2136 | node = rb_prev(&head->node.rb_node); |
| 2137 | while (1) { |
| 2138 | if (!node) |
| 2139 | break; |
| 2140 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2141 | rb_node); |
| 2142 | if (ref->bytenr != head->node.bytenr) |
| 2143 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2144 | if (ref->action == action) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2145 | return ref; |
| 2146 | node = rb_prev(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2147 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2148 | if (action == BTRFS_ADD_DELAYED_REF) { |
| 2149 | action = BTRFS_DROP_DELAYED_REF; |
| 2150 | goto again; |
| 2151 | } |
| 2152 | return NULL; |
| 2153 | } |
| 2154 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2155 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
| 2156 | struct btrfs_root *root, |
| 2157 | struct list_head *cluster) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2158 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2159 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2160 | struct btrfs_delayed_ref_node *ref; |
| 2161 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2162 | struct btrfs_delayed_extent_op *extent_op; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2163 | int ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2164 | int count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2165 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2166 | |
| 2167 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2168 | while (1) { |
| 2169 | if (!locked_ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2170 | /* pick a new head ref from the cluster list */ |
| 2171 | if (list_empty(cluster)) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2172 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2173 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2174 | locked_ref = list_entry(cluster->next, |
| 2175 | struct btrfs_delayed_ref_head, cluster); |
| 2176 | |
| 2177 | /* grab the lock that says we are going to process |
| 2178 | * all the refs for this head */ |
| 2179 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
| 2180 | |
| 2181 | /* |
| 2182 | * we may have dropped the spin lock to get the head |
| 2183 | * mutex lock, and that might have given someone else |
| 2184 | * time to free the head. If that's true, it has been |
| 2185 | * removed from our list and we can move on. |
| 2186 | */ |
| 2187 | if (ret == -EAGAIN) { |
| 2188 | locked_ref = NULL; |
| 2189 | count++; |
| 2190 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * record the must insert reserved flag before we |
| 2196 | * drop the spin lock. |
| 2197 | */ |
| 2198 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2199 | locked_ref->must_insert_reserved = 0; |
| 2200 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2201 | extent_op = locked_ref->extent_op; |
| 2202 | locked_ref->extent_op = NULL; |
| 2203 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2204 | /* |
| 2205 | * locked_ref is the head node, so we have to go one |
| 2206 | * node back for any delayed ref updates |
| 2207 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2208 | ref = select_delayed_ref(locked_ref); |
| 2209 | if (!ref) { |
| 2210 | /* All delayed refs have been processed, Go ahead |
| 2211 | * and send the head node to run_one_delayed_ref, |
| 2212 | * so that any accounting fixes can happen |
| 2213 | */ |
| 2214 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2215 | |
| 2216 | if (extent_op && must_insert_reserved) { |
| 2217 | kfree(extent_op); |
| 2218 | extent_op = NULL; |
| 2219 | } |
| 2220 | |
| 2221 | if (extent_op) { |
| 2222 | spin_unlock(&delayed_refs->lock); |
| 2223 | |
| 2224 | ret = run_delayed_extent_op(trans, root, |
| 2225 | ref, extent_op); |
| 2226 | BUG_ON(ret); |
| 2227 | kfree(extent_op); |
| 2228 | |
| 2229 | cond_resched(); |
| 2230 | spin_lock(&delayed_refs->lock); |
| 2231 | continue; |
| 2232 | } |
| 2233 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2234 | list_del_init(&locked_ref->cluster); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2235 | locked_ref = NULL; |
| 2236 | } |
| 2237 | |
| 2238 | ref->in_tree = 0; |
| 2239 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 2240 | delayed_refs->num_entries--; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2241 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2242 | spin_unlock(&delayed_refs->lock); |
| 2243 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2244 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2245 | must_insert_reserved); |
| 2246 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2247 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2248 | btrfs_put_delayed_ref(ref); |
| 2249 | kfree(extent_op); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2250 | count++; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2251 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2252 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2253 | spin_lock(&delayed_refs->lock); |
| 2254 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2255 | return count; |
| 2256 | } |
| 2257 | |
| 2258 | /* |
| 2259 | * this starts processing the delayed reference count updates and |
| 2260 | * extent insertions we have queued up so far. count can be |
| 2261 | * 0, which means to process everything in the tree at the start |
| 2262 | * of the run (but not newly added entries), or it can be some target |
| 2263 | * number you'd like to process. |
| 2264 | */ |
| 2265 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2266 | struct btrfs_root *root, unsigned long count) |
| 2267 | { |
| 2268 | struct rb_node *node; |
| 2269 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2270 | struct btrfs_delayed_ref_node *ref; |
| 2271 | struct list_head cluster; |
| 2272 | int ret; |
| 2273 | int run_all = count == (unsigned long)-1; |
| 2274 | int run_most = 0; |
| 2275 | |
| 2276 | if (root == root->fs_info->extent_root) |
| 2277 | root = root->fs_info->tree_root; |
| 2278 | |
| 2279 | delayed_refs = &trans->transaction->delayed_refs; |
| 2280 | INIT_LIST_HEAD(&cluster); |
| 2281 | again: |
| 2282 | spin_lock(&delayed_refs->lock); |
| 2283 | if (count == 0) { |
| 2284 | count = delayed_refs->num_entries * 2; |
| 2285 | run_most = 1; |
| 2286 | } |
| 2287 | while (1) { |
| 2288 | if (!(run_all || run_most) && |
| 2289 | delayed_refs->num_heads_ready < 64) |
| 2290 | break; |
| 2291 | |
| 2292 | /* |
| 2293 | * go find something we can process in the rbtree. We start at |
| 2294 | * the beginning of the tree, and then build a cluster |
| 2295 | * of refs to process starting at the first one we are able to |
| 2296 | * lock |
| 2297 | */ |
| 2298 | ret = btrfs_find_ref_cluster(trans, &cluster, |
| 2299 | delayed_refs->run_delayed_start); |
| 2300 | if (ret) |
| 2301 | break; |
| 2302 | |
| 2303 | ret = run_clustered_refs(trans, root, &cluster); |
| 2304 | BUG_ON(ret < 0); |
| 2305 | |
| 2306 | count -= min_t(unsigned long, ret, count); |
| 2307 | |
| 2308 | if (count == 0) |
| 2309 | break; |
| 2310 | } |
| 2311 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2312 | if (run_all) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2313 | node = rb_first(&delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2314 | if (!node) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2315 | goto out; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2316 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2317 | |
| 2318 | while (node) { |
| 2319 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2320 | rb_node); |
| 2321 | if (btrfs_delayed_ref_is_head(ref)) { |
| 2322 | struct btrfs_delayed_ref_head *head; |
| 2323 | |
| 2324 | head = btrfs_delayed_node_to_head(ref); |
| 2325 | atomic_inc(&ref->refs); |
| 2326 | |
| 2327 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2328 | /* |
| 2329 | * Mutex was contended, block until it's |
| 2330 | * released and try again |
| 2331 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2332 | mutex_lock(&head->mutex); |
| 2333 | mutex_unlock(&head->mutex); |
| 2334 | |
| 2335 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2336 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2337 | goto again; |
| 2338 | } |
| 2339 | node = rb_next(node); |
| 2340 | } |
| 2341 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2342 | schedule_timeout(1); |
| 2343 | goto again; |
| 2344 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2345 | out: |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2346 | spin_unlock(&delayed_refs->lock); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2347 | return 0; |
| 2348 | } |
| 2349 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2350 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2351 | struct btrfs_root *root, |
| 2352 | u64 bytenr, u64 num_bytes, u64 flags, |
| 2353 | int is_data) |
| 2354 | { |
| 2355 | struct btrfs_delayed_extent_op *extent_op; |
| 2356 | int ret; |
| 2357 | |
| 2358 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 2359 | if (!extent_op) |
| 2360 | return -ENOMEM; |
| 2361 | |
| 2362 | extent_op->flags_to_set = flags; |
| 2363 | extent_op->update_flags = 1; |
| 2364 | extent_op->update_key = 0; |
| 2365 | extent_op->is_data = is_data ? 1 : 0; |
| 2366 | |
| 2367 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); |
| 2368 | if (ret) |
| 2369 | kfree(extent_op); |
| 2370 | return ret; |
| 2371 | } |
| 2372 | |
| 2373 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2374 | struct btrfs_root *root, |
| 2375 | struct btrfs_path *path, |
| 2376 | u64 objectid, u64 offset, u64 bytenr) |
| 2377 | { |
| 2378 | struct btrfs_delayed_ref_head *head; |
| 2379 | struct btrfs_delayed_ref_node *ref; |
| 2380 | struct btrfs_delayed_data_ref *data_ref; |
| 2381 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2382 | struct rb_node *node; |
| 2383 | int ret = 0; |
| 2384 | |
| 2385 | ret = -ENOENT; |
| 2386 | delayed_refs = &trans->transaction->delayed_refs; |
| 2387 | spin_lock(&delayed_refs->lock); |
| 2388 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 2389 | if (!head) |
| 2390 | goto out; |
| 2391 | |
| 2392 | if (!mutex_trylock(&head->mutex)) { |
| 2393 | atomic_inc(&head->node.refs); |
| 2394 | spin_unlock(&delayed_refs->lock); |
| 2395 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2396 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2397 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2398 | /* |
| 2399 | * Mutex was contended, block until it's released and let |
| 2400 | * caller try again |
| 2401 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2402 | mutex_lock(&head->mutex); |
| 2403 | mutex_unlock(&head->mutex); |
| 2404 | btrfs_put_delayed_ref(&head->node); |
| 2405 | return -EAGAIN; |
| 2406 | } |
| 2407 | |
| 2408 | node = rb_prev(&head->node.rb_node); |
| 2409 | if (!node) |
| 2410 | goto out_unlock; |
| 2411 | |
| 2412 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2413 | |
| 2414 | if (ref->bytenr != bytenr) |
| 2415 | goto out_unlock; |
| 2416 | |
| 2417 | ret = 1; |
| 2418 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) |
| 2419 | goto out_unlock; |
| 2420 | |
| 2421 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2422 | |
| 2423 | node = rb_prev(node); |
| 2424 | if (node) { |
| 2425 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2426 | if (ref->bytenr == bytenr) |
| 2427 | goto out_unlock; |
| 2428 | } |
| 2429 | |
| 2430 | if (data_ref->root != root->root_key.objectid || |
| 2431 | data_ref->objectid != objectid || data_ref->offset != offset) |
| 2432 | goto out_unlock; |
| 2433 | |
| 2434 | ret = 0; |
| 2435 | out_unlock: |
| 2436 | mutex_unlock(&head->mutex); |
| 2437 | out: |
| 2438 | spin_unlock(&delayed_refs->lock); |
| 2439 | return ret; |
| 2440 | } |
| 2441 | |
| 2442 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2443 | struct btrfs_root *root, |
| 2444 | struct btrfs_path *path, |
| 2445 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2446 | { |
| 2447 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2448 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2449 | struct btrfs_extent_data_ref *ref; |
| 2450 | struct btrfs_extent_inline_ref *iref; |
| 2451 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2452 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2453 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2454 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2455 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2456 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2457 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2458 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2459 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2460 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2461 | if (ret < 0) |
| 2462 | goto out; |
| 2463 | BUG_ON(ret == 0); |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2464 | |
| 2465 | ret = -ENOENT; |
| 2466 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2467 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2468 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2469 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2470 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2471 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2472 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2473 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2474 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2475 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2476 | ret = 1; |
| 2477 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2478 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2479 | if (item_size < sizeof(*ei)) { |
| 2480 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2481 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2482 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2483 | #endif |
| 2484 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2485 | |
| 2486 | if (item_size != sizeof(*ei) + |
| 2487 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2488 | goto out; |
| 2489 | |
| 2490 | if (btrfs_extent_generation(leaf, ei) <= |
| 2491 | btrfs_root_last_snapshot(&root->root_item)) |
| 2492 | goto out; |
| 2493 | |
| 2494 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 2495 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 2496 | BTRFS_EXTENT_DATA_REF_KEY) |
| 2497 | goto out; |
| 2498 | |
| 2499 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 2500 | if (btrfs_extent_refs(leaf, ei) != |
| 2501 | btrfs_extent_data_ref_count(leaf, ref) || |
| 2502 | btrfs_extent_data_ref_root(leaf, ref) != |
| 2503 | root->root_key.objectid || |
| 2504 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 2505 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 2506 | goto out; |
| 2507 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2508 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2509 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2510 | return ret; |
| 2511 | } |
| 2512 | |
| 2513 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 2514 | struct btrfs_root *root, |
| 2515 | u64 objectid, u64 offset, u64 bytenr) |
| 2516 | { |
| 2517 | struct btrfs_path *path; |
| 2518 | int ret; |
| 2519 | int ret2; |
| 2520 | |
| 2521 | path = btrfs_alloc_path(); |
| 2522 | if (!path) |
| 2523 | return -ENOENT; |
| 2524 | |
| 2525 | do { |
| 2526 | ret = check_committed_ref(trans, root, path, objectid, |
| 2527 | offset, bytenr); |
| 2528 | if (ret && ret != -ENOENT) |
| 2529 | goto out; |
| 2530 | |
| 2531 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 2532 | offset, bytenr); |
| 2533 | } while (ret2 == -EAGAIN); |
| 2534 | |
| 2535 | if (ret2 && ret2 != -ENOENT) { |
| 2536 | ret = ret2; |
| 2537 | goto out; |
| 2538 | } |
| 2539 | |
| 2540 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 2541 | ret = 0; |
| 2542 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2543 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2544 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 2545 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2546 | return ret; |
| 2547 | } |
| 2548 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2549 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2550 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2551 | struct extent_buffer *buf, |
| 2552 | int full_backref, int inc) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2553 | { |
| 2554 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2555 | u64 num_bytes; |
| 2556 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2557 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2558 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2559 | struct btrfs_key key; |
| 2560 | struct btrfs_file_extent_item *fi; |
| 2561 | int i; |
| 2562 | int level; |
| 2563 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2564 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2565 | u64, u64, u64, u64, u64, u64); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2566 | |
| 2567 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2568 | nritems = btrfs_header_nritems(buf); |
| 2569 | level = btrfs_header_level(buf); |
| 2570 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2571 | if (!root->ref_cows && level == 0) |
| 2572 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2573 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2574 | if (inc) |
| 2575 | process_func = btrfs_inc_extent_ref; |
| 2576 | else |
| 2577 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2578 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2579 | if (full_backref) |
| 2580 | parent = buf->start; |
| 2581 | else |
| 2582 | parent = 0; |
| 2583 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2584 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2585 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2586 | btrfs_item_key_to_cpu(buf, &key, i); |
| 2587 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2588 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2589 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2590 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2591 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2592 | BTRFS_FILE_EXTENT_INLINE) |
| 2593 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2594 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 2595 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2596 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2597 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2598 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 2599 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 2600 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2601 | parent, ref_root, key.objectid, |
| 2602 | key.offset); |
| 2603 | if (ret) |
| 2604 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 2605 | } else { |
| 2606 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2607 | num_bytes = btrfs_level_size(root, level - 1); |
| 2608 | ret = process_func(trans, root, bytenr, num_bytes, |
| 2609 | parent, ref_root, level - 1, 0); |
| 2610 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2611 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2612 | } |
| 2613 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2614 | return 0; |
| 2615 | fail: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2616 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2617 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2618 | } |
| 2619 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2620 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2621 | struct extent_buffer *buf, int full_backref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2622 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2623 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
| 2624 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2625 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2626 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2627 | struct extent_buffer *buf, int full_backref) |
| 2628 | { |
| 2629 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2630 | } |
| 2631 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2632 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 2633 | struct btrfs_root *root, |
| 2634 | struct btrfs_path *path, |
| 2635 | struct btrfs_block_group_cache *cache) |
| 2636 | { |
| 2637 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2638 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2639 | unsigned long bi; |
| 2640 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2641 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2642 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2643 | if (ret < 0) |
| 2644 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2645 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2646 | |
| 2647 | leaf = path->nodes[0]; |
| 2648 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 2649 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 2650 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2651 | btrfs_release_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2652 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2653 | if (ret) |
| 2654 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2655 | return 0; |
| 2656 | |
| 2657 | } |
| 2658 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2659 | static struct btrfs_block_group_cache * |
| 2660 | next_block_group(struct btrfs_root *root, |
| 2661 | struct btrfs_block_group_cache *cache) |
| 2662 | { |
| 2663 | struct rb_node *node; |
| 2664 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 2665 | node = rb_next(&cache->cache_node); |
| 2666 | btrfs_put_block_group(cache); |
| 2667 | if (node) { |
| 2668 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 2669 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 2670 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2671 | } else |
| 2672 | cache = NULL; |
| 2673 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 2674 | return cache; |
| 2675 | } |
| 2676 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2677 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 2678 | struct btrfs_trans_handle *trans, |
| 2679 | struct btrfs_path *path) |
| 2680 | { |
| 2681 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 2682 | struct inode *inode = NULL; |
| 2683 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2684 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2685 | int num_pages = 0; |
| 2686 | int retries = 0; |
| 2687 | int ret = 0; |
| 2688 | |
| 2689 | /* |
| 2690 | * If this block group is smaller than 100 megs don't bother caching the |
| 2691 | * block group. |
| 2692 | */ |
| 2693 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 2694 | spin_lock(&block_group->lock); |
| 2695 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2696 | spin_unlock(&block_group->lock); |
| 2697 | return 0; |
| 2698 | } |
| 2699 | |
| 2700 | again: |
| 2701 | inode = lookup_free_space_inode(root, block_group, path); |
| 2702 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 2703 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2704 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2705 | goto out; |
| 2706 | } |
| 2707 | |
| 2708 | if (IS_ERR(inode)) { |
| 2709 | BUG_ON(retries); |
| 2710 | retries++; |
| 2711 | |
| 2712 | if (block_group->ro) |
| 2713 | goto out_free; |
| 2714 | |
| 2715 | ret = create_free_space_inode(root, trans, block_group, path); |
| 2716 | if (ret) |
| 2717 | goto out_free; |
| 2718 | goto again; |
| 2719 | } |
| 2720 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2721 | /* We've already setup this transaction, go ahead and exit */ |
| 2722 | if (block_group->cache_generation == trans->transid && |
| 2723 | i_size_read(inode)) { |
| 2724 | dcs = BTRFS_DC_SETUP; |
| 2725 | goto out_put; |
| 2726 | } |
| 2727 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2728 | /* |
| 2729 | * We want to set the generation to 0, that way if anything goes wrong |
| 2730 | * from here on out we know not to trust this cache when we load up next |
| 2731 | * time. |
| 2732 | */ |
| 2733 | BTRFS_I(inode)->generation = 0; |
| 2734 | ret = btrfs_update_inode(trans, root, inode); |
| 2735 | WARN_ON(ret); |
| 2736 | |
| 2737 | if (i_size_read(inode) > 0) { |
| 2738 | ret = btrfs_truncate_free_space_cache(root, trans, path, |
| 2739 | inode); |
| 2740 | if (ret) |
| 2741 | goto out_put; |
| 2742 | } |
| 2743 | |
| 2744 | spin_lock(&block_group->lock); |
| 2745 | if (block_group->cached != BTRFS_CACHE_FINISHED) { |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2746 | /* We're not cached, don't bother trying to write stuff out */ |
| 2747 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2748 | spin_unlock(&block_group->lock); |
| 2749 | goto out_put; |
| 2750 | } |
| 2751 | spin_unlock(&block_group->lock); |
| 2752 | |
| 2753 | num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024); |
| 2754 | if (!num_pages) |
| 2755 | num_pages = 1; |
| 2756 | |
| 2757 | /* |
| 2758 | * Just to make absolutely sure we have enough space, we're going to |
| 2759 | * preallocate 12 pages worth of space for each block group. In |
| 2760 | * practice we ought to use at most 8, but we need extra space so we can |
| 2761 | * add our header and have a terminator between the extents and the |
| 2762 | * bitmaps. |
| 2763 | */ |
| 2764 | num_pages *= 16; |
| 2765 | num_pages *= PAGE_CACHE_SIZE; |
| 2766 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2767 | ret = btrfs_check_data_free_space(inode, num_pages); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2768 | if (ret) |
| 2769 | goto out_put; |
| 2770 | |
| 2771 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 2772 | num_pages, num_pages, |
| 2773 | &alloc_hint); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2774 | if (!ret) |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2775 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2776 | btrfs_free_reserved_data_space(inode, num_pages); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 2777 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2778 | out_put: |
| 2779 | iput(inode); |
| 2780 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2781 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2782 | out: |
| 2783 | spin_lock(&block_group->lock); |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 2784 | if (!ret) |
| 2785 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 2786 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2787 | spin_unlock(&block_group->lock); |
| 2788 | |
| 2789 | return ret; |
| 2790 | } |
| 2791 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2792 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 2793 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2794 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2795 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2796 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2797 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2798 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2799 | |
| 2800 | path = btrfs_alloc_path(); |
| 2801 | if (!path) |
| 2802 | return -ENOMEM; |
| 2803 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2804 | again: |
| 2805 | while (1) { |
| 2806 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2807 | while (cache) { |
| 2808 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 2809 | break; |
| 2810 | cache = next_block_group(root, cache); |
| 2811 | } |
| 2812 | if (!cache) { |
| 2813 | if (last == 0) |
| 2814 | break; |
| 2815 | last = 0; |
| 2816 | continue; |
| 2817 | } |
| 2818 | err = cache_save_setup(cache, trans, path); |
| 2819 | last = cache->key.objectid + cache->key.offset; |
| 2820 | btrfs_put_block_group(cache); |
| 2821 | } |
| 2822 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2823 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2824 | if (last == 0) { |
| 2825 | err = btrfs_run_delayed_refs(trans, root, |
| 2826 | (unsigned long)-1); |
| 2827 | BUG_ON(err); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2828 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2829 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2830 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2831 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 2832 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 2833 | btrfs_put_block_group(cache); |
| 2834 | goto again; |
| 2835 | } |
| 2836 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2837 | if (cache->dirty) |
| 2838 | break; |
| 2839 | cache = next_block_group(root, cache); |
| 2840 | } |
| 2841 | if (!cache) { |
| 2842 | if (last == 0) |
| 2843 | break; |
| 2844 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2845 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2846 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2847 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2848 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 2849 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2850 | cache->dirty = 0; |
| 2851 | last = cache->key.objectid + cache->key.offset; |
| 2852 | |
| 2853 | err = write_one_cache_group(trans, root, path, cache); |
| 2854 | BUG_ON(err); |
| 2855 | btrfs_put_block_group(cache); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2856 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2857 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 2858 | while (1) { |
| 2859 | /* |
| 2860 | * I don't think this is needed since we're just marking our |
| 2861 | * preallocated extent as written, but just in case it can't |
| 2862 | * hurt. |
| 2863 | */ |
| 2864 | if (last == 0) { |
| 2865 | err = btrfs_run_delayed_refs(trans, root, |
| 2866 | (unsigned long)-1); |
| 2867 | BUG_ON(err); |
| 2868 | } |
| 2869 | |
| 2870 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 2871 | while (cache) { |
| 2872 | /* |
| 2873 | * Really this shouldn't happen, but it could if we |
| 2874 | * couldn't write the entire preallocated extent and |
| 2875 | * splitting the extent resulted in a new block. |
| 2876 | */ |
| 2877 | if (cache->dirty) { |
| 2878 | btrfs_put_block_group(cache); |
| 2879 | goto again; |
| 2880 | } |
| 2881 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2882 | break; |
| 2883 | cache = next_block_group(root, cache); |
| 2884 | } |
| 2885 | if (!cache) { |
| 2886 | if (last == 0) |
| 2887 | break; |
| 2888 | last = 0; |
| 2889 | continue; |
| 2890 | } |
| 2891 | |
| 2892 | btrfs_write_out_cache(root, trans, cache, path); |
| 2893 | |
| 2894 | /* |
| 2895 | * If we didn't have an error then the cache state is still |
| 2896 | * NEED_WRITE, so we can set it to WRITTEN. |
| 2897 | */ |
| 2898 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 2899 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 2900 | last = cache->key.objectid + cache->key.offset; |
| 2901 | btrfs_put_block_group(cache); |
| 2902 | } |
| 2903 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2904 | btrfs_free_path(path); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 2905 | return 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2906 | } |
| 2907 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2908 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 2909 | { |
| 2910 | struct btrfs_block_group_cache *block_group; |
| 2911 | int readonly = 0; |
| 2912 | |
| 2913 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 2914 | if (!block_group || block_group->ro) |
| 2915 | readonly = 1; |
| 2916 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2917 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 2918 | return readonly; |
| 2919 | } |
| 2920 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2921 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 2922 | u64 total_bytes, u64 bytes_used, |
| 2923 | struct btrfs_space_info **space_info) |
| 2924 | { |
| 2925 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2926 | int i; |
| 2927 | int factor; |
| 2928 | |
| 2929 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 2930 | BTRFS_BLOCK_GROUP_RAID10)) |
| 2931 | factor = 2; |
| 2932 | else |
| 2933 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2934 | |
| 2935 | found = __find_space_info(info, flags); |
| 2936 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2937 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2938 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2939 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2940 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2941 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2942 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2943 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2944 | *space_info = found; |
| 2945 | return 0; |
| 2946 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2947 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2948 | if (!found) |
| 2949 | return -ENOMEM; |
| 2950 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2951 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 2952 | INIT_LIST_HEAD(&found->block_groups[i]); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2953 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 2954 | spin_lock_init(&found->lock); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2955 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
| 2956 | BTRFS_BLOCK_GROUP_SYSTEM | |
| 2957 | BTRFS_BLOCK_GROUP_METADATA); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2958 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 2959 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2960 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 2961 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2962 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 2963 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 2964 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2965 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2966 | found->full = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 2967 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 2968 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 2969 | found->flush = 0; |
| 2970 | init_waitqueue_head(&found->wait); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2971 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 2972 | list_add_rcu(&found->list, &info->space_info); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2973 | return 0; |
| 2974 | } |
| 2975 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2976 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 2977 | { |
| 2978 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2979 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 2980 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2981 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2982 | if (extra_flags) { |
| 2983 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 2984 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 2985 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 2986 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 2987 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 2988 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 2989 | } |
| 2990 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 2991 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 2992 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 2993 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 2994 | /* |
| 2995 | * we add in the count of missing devices because we want |
| 2996 | * to make sure that any RAID levels on a degraded FS |
| 2997 | * continue to be honored. |
| 2998 | */ |
| 2999 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 3000 | root->fs_info->fs_devices->missing_devices; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3001 | |
| 3002 | if (num_devices == 1) |
| 3003 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); |
| 3004 | if (num_devices < 4) |
| 3005 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 3006 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3007 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
| 3008 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3009 | BTRFS_BLOCK_GROUP_RAID10))) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3010 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3011 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3012 | |
| 3013 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3014 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3015 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3016 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3017 | |
| 3018 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && |
| 3019 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | |
| 3020 | (flags & BTRFS_BLOCK_GROUP_RAID10) | |
| 3021 | (flags & BTRFS_BLOCK_GROUP_DUP))) |
| 3022 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; |
| 3023 | return flags; |
| 3024 | } |
| 3025 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3026 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3027 | { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3028 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3029 | flags |= root->fs_info->avail_data_alloc_bits & |
| 3030 | root->fs_info->data_alloc_profile; |
| 3031 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3032 | flags |= root->fs_info->avail_system_alloc_bits & |
| 3033 | root->fs_info->system_alloc_profile; |
| 3034 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3035 | flags |= root->fs_info->avail_metadata_alloc_bits & |
| 3036 | root->fs_info->metadata_alloc_profile; |
| 3037 | return btrfs_reduce_alloc_profile(root, flags); |
| 3038 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3039 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3040 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3041 | { |
| 3042 | u64 flags; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3043 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3044 | if (data) |
| 3045 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3046 | else if (root == root->fs_info->chunk_root) |
| 3047 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3048 | else |
| 3049 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3050 | |
| 3051 | return get_alloc_profile(root, flags); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
| 3055 | { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3056 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3057 | BTRFS_BLOCK_GROUP_DATA); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3058 | } |
| 3059 | |
| 3060 | /* |
| 3061 | * This will check the space that the inode allocates from to make sure we have |
| 3062 | * enough space for bytes. |
| 3063 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3064 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3065 | { |
| 3066 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3067 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3068 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3069 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3070 | |
| 3071 | /* make sure bytes are sectorsize aligned */ |
| 3072 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3073 | |
Li Zefan | 82d5902 | 2011-04-20 10:33:24 +0800 | [diff] [blame] | 3074 | if (root == root->fs_info->tree_root || |
| 3075 | BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3076 | alloc_chunk = 0; |
| 3077 | committed = 1; |
| 3078 | } |
| 3079 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3080 | data_sinfo = BTRFS_I(inode)->space_info; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3081 | if (!data_sinfo) |
| 3082 | goto alloc; |
| 3083 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3084 | again: |
| 3085 | /* make sure we have enough space to handle the data first */ |
| 3086 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3087 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3088 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3089 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3090 | |
| 3091 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3092 | struct btrfs_trans_handle *trans; |
| 3093 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3094 | /* |
| 3095 | * if we don't have enough free bytes in this space then we need |
| 3096 | * to alloc a new chunk. |
| 3097 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3098 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3099 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3100 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3101 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3102 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3103 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3104 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3105 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3106 | if (IS_ERR(trans)) |
| 3107 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3108 | |
| 3109 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
| 3110 | bytes + 2 * 1024 * 1024, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3111 | alloc_target, |
| 3112 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3113 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3114 | if (ret < 0) { |
| 3115 | if (ret != -ENOSPC) |
| 3116 | return ret; |
| 3117 | else |
| 3118 | goto commit_trans; |
| 3119 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3120 | |
| 3121 | if (!data_sinfo) { |
| 3122 | btrfs_set_inode_space_info(root, inode); |
| 3123 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3124 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3125 | goto again; |
| 3126 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3127 | |
| 3128 | /* |
| 3129 | * If we have less pinned bytes than we want to allocate then |
| 3130 | * don't bother committing the transaction, it won't help us. |
| 3131 | */ |
| 3132 | if (data_sinfo->bytes_pinned < bytes) |
| 3133 | committed = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3134 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3135 | |
| 3136 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3137 | commit_trans: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3138 | if (!committed && |
| 3139 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3140 | committed = 1; |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3141 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3142 | if (IS_ERR(trans)) |
| 3143 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3144 | ret = btrfs_commit_transaction(trans, root); |
| 3145 | if (ret) |
| 3146 | return ret; |
| 3147 | goto again; |
| 3148 | } |
| 3149 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3150 | return -ENOSPC; |
| 3151 | } |
| 3152 | data_sinfo->bytes_may_use += bytes; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3153 | spin_unlock(&data_sinfo->lock); |
| 3154 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3155 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3159 | * Called if we need to clear a data reservation for this inode. |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3160 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3161 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3162 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3163 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3164 | struct btrfs_space_info *data_sinfo; |
| 3165 | |
| 3166 | /* make sure bytes are sectorsize aligned */ |
| 3167 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
| 3168 | |
| 3169 | data_sinfo = BTRFS_I(inode)->space_info; |
| 3170 | spin_lock(&data_sinfo->lock); |
| 3171 | data_sinfo->bytes_may_use -= bytes; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3172 | spin_unlock(&data_sinfo->lock); |
| 3173 | } |
| 3174 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3175 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3176 | { |
| 3177 | struct list_head *head = &info->space_info; |
| 3178 | struct btrfs_space_info *found; |
| 3179 | |
| 3180 | rcu_read_lock(); |
| 3181 | list_for_each_entry_rcu(found, head, list) { |
| 3182 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3183 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3184 | } |
| 3185 | rcu_read_unlock(); |
| 3186 | } |
| 3187 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3188 | static int should_alloc_chunk(struct btrfs_root *root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3189 | struct btrfs_space_info *sinfo, u64 alloc_bytes, |
| 3190 | int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3191 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3192 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3193 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3194 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3195 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3196 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3197 | if (force == CHUNK_ALLOC_FORCE) |
| 3198 | return 1; |
| 3199 | |
| 3200 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3201 | * We need to take into account the global rsv because for all intents |
| 3202 | * and purposes it's used space. Don't worry about locking the |
| 3203 | * global_rsv, it doesn't change except when the transaction commits. |
| 3204 | */ |
| 3205 | num_allocated += global_rsv->size; |
| 3206 | |
| 3207 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3208 | * in limited mode, we want to have some free space up to |
| 3209 | * about 1% of the FS size. |
| 3210 | */ |
| 3211 | if (force == CHUNK_ALLOC_LIMITED) { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3212 | thresh = btrfs_super_total_bytes(root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3213 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 3214 | div_factor_fine(thresh, 1)); |
| 3215 | |
| 3216 | if (num_bytes - num_allocated < thresh) |
| 3217 | return 1; |
| 3218 | } |
| 3219 | |
| 3220 | /* |
| 3221 | * we have two similar checks here, one based on percentage |
| 3222 | * and once based on a hard number of 256MB. The idea |
| 3223 | * is that if we have a good amount of free |
| 3224 | * room, don't allocate a chunk. A good mount is |
| 3225 | * less than 80% utilized of the chunks we have allocated, |
| 3226 | * or more than 256MB free |
| 3227 | */ |
| 3228 | if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3229 | return 0; |
| 3230 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3231 | if (num_allocated + alloc_bytes < div_factor(num_bytes, 8)) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3232 | return 0; |
| 3233 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3234 | thresh = btrfs_super_total_bytes(root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3235 | |
| 3236 | /* 256MB or 5% of the FS */ |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3237 | thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5)); |
| 3238 | |
| 3239 | if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3240 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3241 | return 1; |
| 3242 | } |
| 3243 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3244 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 3245 | struct btrfs_root *extent_root, u64 alloc_bytes, |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 3246 | u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3247 | { |
| 3248 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3249 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3250 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3251 | int ret = 0; |
| 3252 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3253 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3254 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3255 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3256 | if (!space_info) { |
| 3257 | ret = update_space_info(extent_root->fs_info, flags, |
| 3258 | 0, 0, &space_info); |
| 3259 | BUG_ON(ret); |
| 3260 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3261 | BUG_ON(!space_info); |
| 3262 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3263 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3264 | spin_lock(&space_info->lock); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3265 | if (space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3266 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3267 | if (space_info->full) { |
| 3268 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3269 | return 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3270 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3271 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3272 | if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3273 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3274 | return 0; |
| 3275 | } else if (space_info->chunk_alloc) { |
| 3276 | wait_for_alloc = 1; |
| 3277 | } else { |
| 3278 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3279 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3280 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3281 | spin_unlock(&space_info->lock); |
| 3282 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3283 | mutex_lock(&fs_info->chunk_mutex); |
| 3284 | |
| 3285 | /* |
| 3286 | * The chunk_mutex is held throughout the entirety of a chunk |
| 3287 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 3288 | * other guy is done and we need to recheck and see if we should |
| 3289 | * allocate. |
| 3290 | */ |
| 3291 | if (wait_for_alloc) { |
| 3292 | mutex_unlock(&fs_info->chunk_mutex); |
| 3293 | wait_for_alloc = 0; |
| 3294 | goto again; |
| 3295 | } |
| 3296 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3297 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3298 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3299 | * allocating mixed chunks instead of individual chunks. |
| 3300 | */ |
| 3301 | if (btrfs_mixed_space_info(space_info)) |
| 3302 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3303 | |
| 3304 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3305 | * if we're doing a data chunk, go ahead and make sure that |
| 3306 | * we keep a reasonable number of metadata chunks allocated in the |
| 3307 | * FS as well. |
| 3308 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3309 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3310 | fs_info->data_chunk_allocations++; |
| 3311 | if (!(fs_info->data_chunk_allocations % |
| 3312 | fs_info->metadata_ratio)) |
| 3313 | force_metadata_allocation(fs_info); |
| 3314 | } |
| 3315 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3316 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3317 | if (ret < 0 && ret != -ENOSPC) |
| 3318 | goto out; |
| 3319 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3320 | spin_lock(&space_info->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3321 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3322 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3323 | else |
| 3324 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3325 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3326 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3327 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3328 | spin_unlock(&space_info->lock); |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3329 | out: |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3330 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3331 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3332 | } |
| 3333 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3334 | /* |
| 3335 | * shrink metadata reservation for delalloc |
| 3336 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3337 | static int shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3338 | bool wait_ordered) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3339 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3340 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3341 | struct btrfs_space_info *space_info; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3342 | struct btrfs_trans_handle *trans; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3343 | u64 reserved; |
| 3344 | u64 max_reclaim; |
| 3345 | u64 reclaimed = 0; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3346 | long time_left; |
Josef Bacik | 877da17 | 2011-10-14 14:02:10 -0400 | [diff] [blame] | 3347 | unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3348 | int loops = 0; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3349 | unsigned long progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3350 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3351 | trans = (struct btrfs_trans_handle *)current->journal_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3352 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3353 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3354 | |
| 3355 | smp_mb(); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3356 | reserved = space_info->bytes_may_use; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3357 | progress = space_info->reservation_progress; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3358 | |
| 3359 | if (reserved == 0) |
| 3360 | return 0; |
| 3361 | |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3362 | smp_mb(); |
| 3363 | if (root->fs_info->delalloc_bytes == 0) { |
| 3364 | if (trans) |
| 3365 | return 0; |
| 3366 | btrfs_wait_ordered_extents(root, 0, 0); |
| 3367 | return 0; |
| 3368 | } |
| 3369 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3370 | max_reclaim = min(reserved, to_reclaim); |
Josef Bacik | 877da17 | 2011-10-14 14:02:10 -0400 | [diff] [blame] | 3371 | nr_pages = max_t(unsigned long, nr_pages, |
| 3372 | max_reclaim >> PAGE_CACHE_SHIFT); |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3373 | while (loops < 1024) { |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3374 | /* have the flusher threads jump in and do some IO */ |
| 3375 | smp_mb(); |
| 3376 | nr_pages = min_t(unsigned long, nr_pages, |
| 3377 | root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); |
| 3378 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3379 | |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3380 | spin_lock(&space_info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3381 | if (reserved > space_info->bytes_may_use) |
| 3382 | reclaimed += reserved - space_info->bytes_may_use; |
| 3383 | reserved = space_info->bytes_may_use; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 3384 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3385 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3386 | loops++; |
| 3387 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3388 | if (reserved == 0 || reclaimed >= max_reclaim) |
| 3389 | break; |
| 3390 | |
| 3391 | if (trans && trans->transaction->blocked) |
| 3392 | return -EAGAIN; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3393 | |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3394 | if (wait_ordered && !trans) { |
| 3395 | btrfs_wait_ordered_extents(root, 0, 0); |
| 3396 | } else { |
| 3397 | time_left = schedule_timeout_interruptible(1); |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3398 | |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3399 | /* We were interrupted, exit */ |
| 3400 | if (time_left) |
| 3401 | break; |
| 3402 | } |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 3403 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3404 | /* we've kicked the IO a few times, if anything has been freed, |
| 3405 | * exit. There is no sense in looping here for a long time |
| 3406 | * when we really need to commit the transaction, or there are |
| 3407 | * just too many writers without enough free space |
| 3408 | */ |
| 3409 | |
| 3410 | if (loops > 3) { |
| 3411 | smp_mb(); |
| 3412 | if (progress != space_info->reservation_progress) |
| 3413 | break; |
| 3414 | } |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 3415 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3416 | } |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3417 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3418 | return reclaimed >= to_reclaim; |
| 3419 | } |
| 3420 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3421 | /** |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3422 | * maybe_commit_transaction - possibly commit the transaction if its ok to |
| 3423 | * @root - the root we're allocating for |
| 3424 | * @bytes - the number of bytes we want to reserve |
| 3425 | * @force - force the commit |
| 3426 | * |
| 3427 | * This will check to make sure that committing the transaction will actually |
| 3428 | * get us somewhere and then commit the transaction if it does. Otherwise it |
| 3429 | * will return -ENOSPC. |
| 3430 | */ |
| 3431 | static int may_commit_transaction(struct btrfs_root *root, |
| 3432 | struct btrfs_space_info *space_info, |
| 3433 | u64 bytes, int force) |
| 3434 | { |
| 3435 | struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv; |
| 3436 | struct btrfs_trans_handle *trans; |
| 3437 | |
| 3438 | trans = (struct btrfs_trans_handle *)current->journal_info; |
| 3439 | if (trans) |
| 3440 | return -EAGAIN; |
| 3441 | |
| 3442 | if (force) |
| 3443 | goto commit; |
| 3444 | |
| 3445 | /* See if there is enough pinned space to make this reservation */ |
| 3446 | spin_lock(&space_info->lock); |
| 3447 | if (space_info->bytes_pinned >= bytes) { |
| 3448 | spin_unlock(&space_info->lock); |
| 3449 | goto commit; |
| 3450 | } |
| 3451 | spin_unlock(&space_info->lock); |
| 3452 | |
| 3453 | /* |
| 3454 | * See if there is some space in the delayed insertion reservation for |
| 3455 | * this reservation. |
| 3456 | */ |
| 3457 | if (space_info != delayed_rsv->space_info) |
| 3458 | return -ENOSPC; |
| 3459 | |
| 3460 | spin_lock(&delayed_rsv->lock); |
| 3461 | if (delayed_rsv->size < bytes) { |
| 3462 | spin_unlock(&delayed_rsv->lock); |
| 3463 | return -ENOSPC; |
| 3464 | } |
| 3465 | spin_unlock(&delayed_rsv->lock); |
| 3466 | |
| 3467 | commit: |
| 3468 | trans = btrfs_join_transaction(root); |
| 3469 | if (IS_ERR(trans)) |
| 3470 | return -ENOSPC; |
| 3471 | |
| 3472 | return btrfs_commit_transaction(trans, root); |
| 3473 | } |
| 3474 | |
| 3475 | /** |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3476 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 3477 | * @root - the root we're allocating for |
| 3478 | * @block_rsv - the block_rsv we're allocating for |
| 3479 | * @orig_bytes - the number of bytes we want |
| 3480 | * @flush - wether or not we can flush to make our reservation |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3481 | * |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3482 | * This will reserve orgi_bytes number of bytes from the space info associated |
| 3483 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 3484 | * flush out space to make room. It will do this by flushing delalloc if |
| 3485 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 3486 | * regain reservations will be made and this will fail if there is not enough |
| 3487 | * space already. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3488 | */ |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3489 | static int reserve_metadata_bytes(struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3490 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3491 | u64 orig_bytes, int flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3492 | { |
| 3493 | struct btrfs_space_info *space_info = block_rsv->space_info; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3494 | u64 used; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3495 | u64 num_bytes = orig_bytes; |
| 3496 | int retries = 0; |
| 3497 | int ret = 0; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3498 | bool committed = false; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3499 | bool flushing = false; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3500 | bool wait_ordered = false; |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3501 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3502 | again: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3503 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3504 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3505 | /* |
| 3506 | * We only want to wait if somebody other than us is flushing and we are |
| 3507 | * actually alloed to flush. |
| 3508 | */ |
| 3509 | while (flush && !flushing && space_info->flush) { |
| 3510 | spin_unlock(&space_info->lock); |
| 3511 | /* |
| 3512 | * If we have a trans handle we can't wait because the flusher |
| 3513 | * may have to commit the transaction, which would mean we would |
| 3514 | * deadlock since we are waiting for the flusher to finish, but |
| 3515 | * hold the current transaction open. |
| 3516 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3517 | if (current->journal_info) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3518 | return -EAGAIN; |
| 3519 | ret = wait_event_interruptible(space_info->wait, |
| 3520 | !space_info->flush); |
| 3521 | /* Must have been interrupted, return */ |
| 3522 | if (ret) |
| 3523 | return -EINTR; |
| 3524 | |
| 3525 | spin_lock(&space_info->lock); |
| 3526 | } |
| 3527 | |
| 3528 | ret = -ENOSPC; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3529 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 3530 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 3531 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3532 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3533 | /* |
| 3534 | * The idea here is that we've not already over-reserved the block group |
| 3535 | * then we can go ahead and save our reservation first and then start |
| 3536 | * flushing if we need to. Otherwise if we've already overcommitted |
| 3537 | * lets start flushing stuff first and then come back and try to make |
| 3538 | * our reservation. |
| 3539 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3540 | if (used <= space_info->total_bytes) { |
| 3541 | if (used + orig_bytes <= space_info->total_bytes) { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3542 | space_info->bytes_may_use += orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3543 | ret = 0; |
| 3544 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3545 | /* |
| 3546 | * Ok set num_bytes to orig_bytes since we aren't |
| 3547 | * overocmmitted, this way we only try and reclaim what |
| 3548 | * we need. |
| 3549 | */ |
| 3550 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3551 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3552 | } else { |
| 3553 | /* |
| 3554 | * Ok we're over committed, set num_bytes to the overcommitted |
| 3555 | * amount plus the amount of bytes that we need for this |
| 3556 | * reservation. |
| 3557 | */ |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3558 | wait_ordered = true; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3559 | num_bytes = used - space_info->total_bytes + |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3560 | (orig_bytes * (retries + 1)); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3561 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3562 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3563 | if (ret) { |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3564 | u64 profile = btrfs_get_alloc_profile(root, 0); |
| 3565 | u64 avail; |
| 3566 | |
Josef Bacik | 7e355b8 | 2011-10-18 13:07:31 -0400 | [diff] [blame] | 3567 | /* |
| 3568 | * If we have a lot of space that's pinned, don't bother doing |
| 3569 | * the overcommit dance yet and just commit the transaction. |
| 3570 | */ |
| 3571 | avail = (space_info->total_bytes - space_info->bytes_used) * 8; |
| 3572 | do_div(avail, 10); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3573 | if (space_info->bytes_pinned >= avail && flush && !committed) { |
Josef Bacik | 7e355b8 | 2011-10-18 13:07:31 -0400 | [diff] [blame] | 3574 | space_info->flush = 1; |
| 3575 | flushing = true; |
| 3576 | spin_unlock(&space_info->lock); |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3577 | ret = may_commit_transaction(root, space_info, |
| 3578 | orig_bytes, 1); |
| 3579 | if (ret) |
| 3580 | goto out; |
| 3581 | committed = true; |
| 3582 | goto again; |
Josef Bacik | 7e355b8 | 2011-10-18 13:07:31 -0400 | [diff] [blame] | 3583 | } |
| 3584 | |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3585 | spin_lock(&root->fs_info->free_chunk_lock); |
| 3586 | avail = root->fs_info->free_chunk_space; |
| 3587 | |
| 3588 | /* |
| 3589 | * If we have dup, raid1 or raid10 then only half of the free |
| 3590 | * space is actually useable. |
| 3591 | */ |
| 3592 | if (profile & (BTRFS_BLOCK_GROUP_DUP | |
| 3593 | BTRFS_BLOCK_GROUP_RAID1 | |
| 3594 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3595 | avail >>= 1; |
| 3596 | |
| 3597 | /* |
| 3598 | * If we aren't flushing don't let us overcommit too much, say |
| 3599 | * 1/8th of the space. If we can flush, let it overcommit up to |
| 3600 | * 1/2 of the space. |
| 3601 | */ |
| 3602 | if (flush) |
| 3603 | avail >>= 3; |
| 3604 | else |
| 3605 | avail >>= 1; |
| 3606 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 3607 | |
Josef Bacik | 9a82ca6 | 2011-10-05 16:35:28 -0400 | [diff] [blame] | 3608 | if (used + num_bytes < space_info->total_bytes + avail) { |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3609 | space_info->bytes_may_use += orig_bytes; |
| 3610 | ret = 0; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3611 | } else { |
| 3612 | wait_ordered = true; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 3613 | } |
| 3614 | } |
| 3615 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3616 | /* |
| 3617 | * Couldn't make our reservation, save our place so while we're trying |
| 3618 | * to reclaim space we can actually use it instead of somebody else |
| 3619 | * stealing it from us. |
| 3620 | */ |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3621 | if (ret && flush) { |
| 3622 | flushing = true; |
| 3623 | space_info->flush = 1; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3624 | } |
| 3625 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3626 | spin_unlock(&space_info->lock); |
| 3627 | |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3628 | if (!ret || !flush) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3629 | goto out; |
| 3630 | |
| 3631 | /* |
| 3632 | * We do synchronous shrinking since we don't actually unreserve |
| 3633 | * metadata until after the IO is completed. |
| 3634 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3635 | ret = shrink_delalloc(root, num_bytes, wait_ordered); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3636 | if (ret < 0) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3637 | goto out; |
| 3638 | |
Chris Mason | 75c195a | 2011-07-27 15:57:44 -0400 | [diff] [blame] | 3639 | ret = 0; |
| 3640 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3641 | /* |
| 3642 | * So if we were overcommitted it's possible that somebody else flushed |
| 3643 | * out enough space and we simply didn't have enough space to reclaim, |
| 3644 | * so go back around and try again. |
| 3645 | */ |
| 3646 | if (retries < 2) { |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 3647 | wait_ordered = true; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3648 | retries++; |
| 3649 | goto again; |
| 3650 | } |
| 3651 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3652 | ret = -ENOSPC; |
Chris Mason | 75c195a | 2011-07-27 15:57:44 -0400 | [diff] [blame] | 3653 | if (committed) |
| 3654 | goto out; |
| 3655 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame^] | 3656 | ret = may_commit_transaction(root, space_info, orig_bytes, 0); |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3657 | if (!ret) { |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3658 | committed = true; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3659 | goto again; |
Josef Bacik | 3822793 | 2010-10-26 12:52:53 -0400 | [diff] [blame] | 3660 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3661 | |
| 3662 | out: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3663 | if (flushing) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3664 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3665 | space_info->flush = 0; |
| 3666 | wake_up_all(&space_info->wait); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3667 | spin_unlock(&space_info->lock); |
| 3668 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3669 | return ret; |
| 3670 | } |
| 3671 | |
| 3672 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, |
| 3673 | struct btrfs_root *root) |
| 3674 | { |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 3675 | struct btrfs_block_rsv *block_rsv = NULL; |
| 3676 | |
| 3677 | if (root->ref_cows || root == root->fs_info->csum_root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3678 | block_rsv = trans->block_rsv; |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 3679 | |
| 3680 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3681 | block_rsv = root->block_rsv; |
| 3682 | |
| 3683 | if (!block_rsv) |
| 3684 | block_rsv = &root->fs_info->empty_block_rsv; |
| 3685 | |
| 3686 | return block_rsv; |
| 3687 | } |
| 3688 | |
| 3689 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 3690 | u64 num_bytes) |
| 3691 | { |
| 3692 | int ret = -ENOSPC; |
| 3693 | spin_lock(&block_rsv->lock); |
| 3694 | if (block_rsv->reserved >= num_bytes) { |
| 3695 | block_rsv->reserved -= num_bytes; |
| 3696 | if (block_rsv->reserved < block_rsv->size) |
| 3697 | block_rsv->full = 0; |
| 3698 | ret = 0; |
| 3699 | } |
| 3700 | spin_unlock(&block_rsv->lock); |
| 3701 | return ret; |
| 3702 | } |
| 3703 | |
| 3704 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 3705 | u64 num_bytes, int update_size) |
| 3706 | { |
| 3707 | spin_lock(&block_rsv->lock); |
| 3708 | block_rsv->reserved += num_bytes; |
| 3709 | if (update_size) |
| 3710 | block_rsv->size += num_bytes; |
| 3711 | else if (block_rsv->reserved >= block_rsv->size) |
| 3712 | block_rsv->full = 1; |
| 3713 | spin_unlock(&block_rsv->lock); |
| 3714 | } |
| 3715 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 3716 | static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
| 3717 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3718 | { |
| 3719 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 3720 | |
| 3721 | spin_lock(&block_rsv->lock); |
| 3722 | if (num_bytes == (u64)-1) |
| 3723 | num_bytes = block_rsv->size; |
| 3724 | block_rsv->size -= num_bytes; |
| 3725 | if (block_rsv->reserved >= block_rsv->size) { |
| 3726 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 3727 | block_rsv->reserved = block_rsv->size; |
| 3728 | block_rsv->full = 1; |
| 3729 | } else { |
| 3730 | num_bytes = 0; |
| 3731 | } |
| 3732 | spin_unlock(&block_rsv->lock); |
| 3733 | |
| 3734 | if (num_bytes > 0) { |
| 3735 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 3736 | spin_lock(&dest->lock); |
| 3737 | if (!dest->full) { |
| 3738 | u64 bytes_to_add; |
| 3739 | |
| 3740 | bytes_to_add = dest->size - dest->reserved; |
| 3741 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 3742 | dest->reserved += bytes_to_add; |
| 3743 | if (dest->reserved >= dest->size) |
| 3744 | dest->full = 1; |
| 3745 | num_bytes -= bytes_to_add; |
| 3746 | } |
| 3747 | spin_unlock(&dest->lock); |
| 3748 | } |
| 3749 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3750 | spin_lock(&space_info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3751 | space_info->bytes_may_use -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3752 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3753 | spin_unlock(&space_info->lock); |
| 3754 | } |
| 3755 | } |
| 3756 | } |
| 3757 | |
| 3758 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 3759 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 3760 | { |
| 3761 | int ret; |
| 3762 | |
| 3763 | ret = block_rsv_use_bytes(src, num_bytes); |
| 3764 | if (ret) |
| 3765 | return ret; |
| 3766 | |
| 3767 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 3768 | return 0; |
| 3769 | } |
| 3770 | |
| 3771 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
| 3772 | { |
| 3773 | memset(rsv, 0, sizeof(*rsv)); |
| 3774 | spin_lock_init(&rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) |
| 3778 | { |
| 3779 | struct btrfs_block_rsv *block_rsv; |
| 3780 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3781 | |
| 3782 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 3783 | if (!block_rsv) |
| 3784 | return NULL; |
| 3785 | |
| 3786 | btrfs_init_block_rsv(block_rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3787 | block_rsv->space_info = __find_space_info(fs_info, |
| 3788 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3789 | return block_rsv; |
| 3790 | } |
| 3791 | |
| 3792 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 3793 | struct btrfs_block_rsv *rsv) |
| 3794 | { |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 3795 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 3796 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3797 | } |
| 3798 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3799 | int btrfs_block_rsv_add(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3800 | struct btrfs_block_rsv *block_rsv, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3801 | u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3802 | { |
| 3803 | int ret; |
| 3804 | |
| 3805 | if (num_bytes == 0) |
| 3806 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 3807 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3808 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3809 | if (!ret) { |
| 3810 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 3811 | return 0; |
| 3812 | } |
| 3813 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3814 | return ret; |
| 3815 | } |
| 3816 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 3817 | int btrfs_block_rsv_check(struct btrfs_root *root, |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3818 | struct btrfs_block_rsv *block_rsv, int min_factor) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3819 | { |
| 3820 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3821 | int ret = -ENOSPC; |
| 3822 | |
| 3823 | if (!block_rsv) |
| 3824 | return 0; |
| 3825 | |
| 3826 | spin_lock(&block_rsv->lock); |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3827 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 3828 | if (block_rsv->reserved >= num_bytes) |
| 3829 | ret = 0; |
| 3830 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3831 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3832 | return ret; |
| 3833 | } |
| 3834 | |
| 3835 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
| 3836 | struct btrfs_block_rsv *block_rsv, |
| 3837 | u64 min_reserved) |
| 3838 | { |
| 3839 | u64 num_bytes = 0; |
| 3840 | int ret = -ENOSPC; |
| 3841 | |
| 3842 | if (!block_rsv) |
| 3843 | return 0; |
| 3844 | |
| 3845 | spin_lock(&block_rsv->lock); |
| 3846 | num_bytes = min_reserved; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 3847 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3848 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 3849 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3850 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3851 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 3852 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3853 | if (!ret) |
| 3854 | return 0; |
| 3855 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 3856 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, 1); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 3857 | if (!ret) { |
| 3858 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
| 3859 | return 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3860 | } |
| 3861 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 3862 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3863 | } |
| 3864 | |
| 3865 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 3866 | struct btrfs_block_rsv *dst_rsv, |
| 3867 | u64 num_bytes) |
| 3868 | { |
| 3869 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 3870 | } |
| 3871 | |
| 3872 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 3873 | struct btrfs_block_rsv *block_rsv, |
| 3874 | u64 num_bytes) |
| 3875 | { |
| 3876 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
| 3877 | if (global_rsv->full || global_rsv == block_rsv || |
| 3878 | block_rsv->space_info != global_rsv->space_info) |
| 3879 | global_rsv = NULL; |
| 3880 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); |
| 3881 | } |
| 3882 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3883 | /* |
| 3884 | * helper to calculate size of global block reservation. |
| 3885 | * the desired value is sum of space used by extent tree, |
| 3886 | * checksum tree and root tree |
| 3887 | */ |
| 3888 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 3889 | { |
| 3890 | struct btrfs_space_info *sinfo; |
| 3891 | u64 num_bytes; |
| 3892 | u64 meta_used; |
| 3893 | u64 data_used; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3894 | int csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3895 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3896 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 3897 | spin_lock(&sinfo->lock); |
| 3898 | data_used = sinfo->bytes_used; |
| 3899 | spin_unlock(&sinfo->lock); |
| 3900 | |
| 3901 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 3902 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3903 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 3904 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3905 | meta_used = sinfo->bytes_used; |
| 3906 | spin_unlock(&sinfo->lock); |
| 3907 | |
| 3908 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 3909 | csum_size * 2; |
| 3910 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 3911 | |
| 3912 | if (num_bytes * 3 > meta_used) |
| 3913 | num_bytes = div64_u64(meta_used, 3); |
| 3914 | |
| 3915 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 3916 | } |
| 3917 | |
| 3918 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3919 | { |
| 3920 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 3921 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 3922 | u64 num_bytes; |
| 3923 | |
| 3924 | num_bytes = calc_global_metadata_size(fs_info); |
| 3925 | |
| 3926 | spin_lock(&block_rsv->lock); |
| 3927 | spin_lock(&sinfo->lock); |
| 3928 | |
| 3929 | block_rsv->size = num_bytes; |
| 3930 | |
| 3931 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 3932 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 3933 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3934 | |
| 3935 | if (sinfo->total_bytes > num_bytes) { |
| 3936 | num_bytes = sinfo->total_bytes - num_bytes; |
| 3937 | block_rsv->reserved += num_bytes; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3938 | sinfo->bytes_may_use += num_bytes; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | if (block_rsv->reserved >= block_rsv->size) { |
| 3942 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3943 | sinfo->bytes_may_use -= num_bytes; |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 3944 | sinfo->reservation_progress++; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3945 | block_rsv->reserved = block_rsv->size; |
| 3946 | block_rsv->full = 1; |
| 3947 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 3948 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3949 | spin_unlock(&sinfo->lock); |
| 3950 | spin_unlock(&block_rsv->lock); |
| 3951 | } |
| 3952 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3953 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3954 | { |
| 3955 | struct btrfs_space_info *space_info; |
| 3956 | |
| 3957 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3958 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3959 | |
| 3960 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3961 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3962 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3963 | fs_info->trans_block_rsv.space_info = space_info; |
| 3964 | fs_info->empty_block_rsv.space_info = space_info; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3965 | fs_info->delayed_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3966 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3967 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 3968 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 3969 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 3970 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3971 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3972 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3973 | update_global_block_rsv(fs_info); |
| 3974 | } |
| 3975 | |
| 3976 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 3977 | { |
| 3978 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
| 3979 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 3980 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 3981 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 3982 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 3983 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 3984 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 3985 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
| 3986 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3987 | } |
| 3988 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3989 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 3990 | struct btrfs_root *root) |
| 3991 | { |
| 3992 | if (!trans->bytes_reserved) |
| 3993 | return; |
| 3994 | |
Josef Bacik | b24e03d | 2011-10-14 14:40:17 -0400 | [diff] [blame] | 3995 | btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3996 | trans->bytes_reserved = 0; |
| 3997 | } |
| 3998 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3999 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4000 | struct inode *inode) |
| 4001 | { |
| 4002 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4003 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 4004 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 4005 | |
| 4006 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4007 | * We need to hold space in order to delete our orphan item once we've |
| 4008 | * added it, so this takes the reservation so we can release it later |
| 4009 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4010 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4011 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4012 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4013 | } |
| 4014 | |
| 4015 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 4016 | { |
| 4017 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4018 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4019 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 4020 | } |
| 4021 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4022 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4023 | struct btrfs_pending_snapshot *pending) |
| 4024 | { |
| 4025 | struct btrfs_root *root = pending->root; |
| 4026 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 4027 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; |
| 4028 | /* |
| 4029 | * two for root back/forward refs, two for directory entries |
| 4030 | * and one for root of the snapshot. |
| 4031 | */ |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4032 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4033 | dst_rsv->space_info = src_rsv->space_info; |
| 4034 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4035 | } |
| 4036 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4037 | /** |
| 4038 | * drop_outstanding_extent - drop an outstanding extent |
| 4039 | * @inode: the inode we're dropping the extent for |
| 4040 | * |
| 4041 | * This is called when we are freeing up an outstanding extent, either called |
| 4042 | * after an error or after an extent is written. This will return the number of |
| 4043 | * reserved extents that need to be freed. This must be called with |
| 4044 | * BTRFS_I(inode)->lock held. |
| 4045 | */ |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4046 | static unsigned drop_outstanding_extent(struct inode *inode) |
| 4047 | { |
| 4048 | unsigned dropped_extents = 0; |
| 4049 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4050 | BUG_ON(!BTRFS_I(inode)->outstanding_extents); |
| 4051 | BTRFS_I(inode)->outstanding_extents--; |
| 4052 | |
| 4053 | /* |
| 4054 | * If we have more or the same amount of outsanding extents than we have |
| 4055 | * reserved then we need to leave the reserved extents count alone. |
| 4056 | */ |
| 4057 | if (BTRFS_I(inode)->outstanding_extents >= |
| 4058 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4059 | return 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4060 | |
| 4061 | dropped_extents = BTRFS_I(inode)->reserved_extents - |
| 4062 | BTRFS_I(inode)->outstanding_extents; |
| 4063 | BTRFS_I(inode)->reserved_extents -= dropped_extents; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4064 | return dropped_extents; |
| 4065 | } |
| 4066 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4067 | /** |
| 4068 | * calc_csum_metadata_size - return the amount of metada space that must be |
| 4069 | * reserved/free'd for the given bytes. |
| 4070 | * @inode: the inode we're manipulating |
| 4071 | * @num_bytes: the number of bytes in question |
| 4072 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 4073 | * |
| 4074 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 4075 | * correct amount of metadata that must either be reserved or freed. We |
| 4076 | * calculate how many checksums we can fit into one leaf and then divide the |
| 4077 | * number of bytes that will need to be checksumed by this value to figure out |
| 4078 | * how many checksums will be required. If we are adding bytes then the number |
| 4079 | * may go up and we will return the number of additional bytes that must be |
| 4080 | * reserved. If it is going down we will return the number of bytes that must |
| 4081 | * be freed. |
| 4082 | * |
| 4083 | * This must be called with BTRFS_I(inode)->lock held. |
| 4084 | */ |
| 4085 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, |
| 4086 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4087 | { |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4088 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4089 | u64 csum_size; |
| 4090 | int num_csums_per_leaf; |
| 4091 | int num_csums; |
| 4092 | int old_csums; |
| 4093 | |
| 4094 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && |
| 4095 | BTRFS_I(inode)->csum_bytes == 0) |
| 4096 | return 0; |
| 4097 | |
| 4098 | old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 4099 | if (reserve) |
| 4100 | BTRFS_I(inode)->csum_bytes += num_bytes; |
| 4101 | else |
| 4102 | BTRFS_I(inode)->csum_bytes -= num_bytes; |
| 4103 | csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item); |
| 4104 | num_csums_per_leaf = (int)div64_u64(csum_size, |
| 4105 | sizeof(struct btrfs_csum_item) + |
| 4106 | sizeof(struct btrfs_disk_key)); |
| 4107 | num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 4108 | num_csums = num_csums + num_csums_per_leaf - 1; |
| 4109 | num_csums = num_csums / num_csums_per_leaf; |
| 4110 | |
| 4111 | old_csums = old_csums + num_csums_per_leaf - 1; |
| 4112 | old_csums = old_csums / num_csums_per_leaf; |
| 4113 | |
| 4114 | /* No change, no need to reserve more */ |
| 4115 | if (old_csums == num_csums) |
| 4116 | return 0; |
| 4117 | |
| 4118 | if (reserve) |
| 4119 | return btrfs_calc_trans_metadata_size(root, |
| 4120 | num_csums - old_csums); |
| 4121 | |
| 4122 | return btrfs_calc_trans_metadata_size(root, old_csums - num_csums); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 4126 | { |
| 4127 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4128 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4129 | u64 to_reserve = 0; |
| 4130 | unsigned nr_extents = 0; |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 4131 | int flush = 1; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4132 | int ret; |
| 4133 | |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 4134 | if (btrfs_is_free_space_inode(root, inode)) |
| 4135 | flush = 0; |
| 4136 | |
| 4137 | if (flush && btrfs_transaction_in_commit(root->fs_info)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4138 | schedule_timeout(1); |
| 4139 | |
| 4140 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4141 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4142 | spin_lock(&BTRFS_I(inode)->lock); |
| 4143 | BTRFS_I(inode)->outstanding_extents++; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4144 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4145 | if (BTRFS_I(inode)->outstanding_extents > |
| 4146 | BTRFS_I(inode)->reserved_extents) { |
| 4147 | nr_extents = BTRFS_I(inode)->outstanding_extents - |
| 4148 | BTRFS_I(inode)->reserved_extents; |
| 4149 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 4150 | |
Miao Xie | 16cdcec | 2011-04-22 18:12:22 +0800 | [diff] [blame] | 4151 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4152 | } |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4153 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4154 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 4155 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4156 | ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4157 | if (ret) { |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4158 | u64 to_free = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4159 | unsigned dropped; |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4160 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4161 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4162 | dropped = drop_outstanding_extent(inode); |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4163 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4164 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 7ed49f1 | 2011-08-19 15:45:52 -0400 | [diff] [blame] | 4165 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
| 4166 | |
| 4167 | /* |
| 4168 | * Somebody could have come in and twiddled with the |
| 4169 | * reservation, so if we have to free more than we would have |
| 4170 | * reserved from this reservation go ahead and release those |
| 4171 | * bytes. |
| 4172 | */ |
| 4173 | to_free -= to_reserve; |
| 4174 | if (to_free) |
| 4175 | btrfs_block_rsv_release(root, block_rsv, to_free); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4176 | return ret; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4177 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4178 | |
| 4179 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 4180 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4181 | return 0; |
| 4182 | } |
| 4183 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4184 | /** |
| 4185 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 4186 | * @inode: the inode to release the reservation for |
| 4187 | * @num_bytes: the number of bytes we're releasing |
| 4188 | * |
| 4189 | * This will release the metadata reservation for an inode. This can be called |
| 4190 | * once we complete IO for a given set of bytes to release their metadata |
| 4191 | * reservations. |
| 4192 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4193 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 4194 | { |
| 4195 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4196 | u64 to_free = 0; |
| 4197 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4198 | |
| 4199 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4200 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4201 | dropped = drop_outstanding_extent(inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4202 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4203 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
| 4204 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 4205 | if (dropped > 0) |
| 4206 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4207 | |
| 4208 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 4209 | to_free); |
| 4210 | } |
| 4211 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4212 | /** |
| 4213 | * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc |
| 4214 | * @inode: inode we're writing to |
| 4215 | * @num_bytes: the number of bytes we want to allocate |
| 4216 | * |
| 4217 | * This will do the following things |
| 4218 | * |
| 4219 | * o reserve space in the data space info for num_bytes |
| 4220 | * o reserve space in the metadata space info based on number of outstanding |
| 4221 | * extents and how much csums will be needed |
| 4222 | * o add to the inodes ->delalloc_bytes |
| 4223 | * o add it to the fs_info's delalloc inodes list. |
| 4224 | * |
| 4225 | * This will return 0 for success and -ENOSPC if there is no space left. |
| 4226 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4227 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 4228 | { |
| 4229 | int ret; |
| 4230 | |
| 4231 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 4232 | if (ret) |
| 4233 | return ret; |
| 4234 | |
| 4235 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 4236 | if (ret) { |
| 4237 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4238 | return ret; |
| 4239 | } |
| 4240 | |
| 4241 | return 0; |
| 4242 | } |
| 4243 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 4244 | /** |
| 4245 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
| 4246 | * @inode: inode we're releasing space for |
| 4247 | * @num_bytes: the number of bytes we want to free up |
| 4248 | * |
| 4249 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 4250 | * called in the case that we don't need the metadata AND data reservations |
| 4251 | * anymore. So if there is an error or we insert an inline extent. |
| 4252 | * |
| 4253 | * This function will release the metadata space that was not used and will |
| 4254 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 4255 | * list if there are no delalloc bytes left. |
| 4256 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4257 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 4258 | { |
| 4259 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 4260 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 4261 | } |
| 4262 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4263 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 4264 | struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4265 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4266 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4267 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4268 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4269 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4270 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4271 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4272 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 4273 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4274 | /* block accounting for super block */ |
| 4275 | spin_lock(&info->delalloc_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4276 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4277 | if (alloc) |
| 4278 | old_val += num_bytes; |
| 4279 | else |
| 4280 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4281 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4282 | spin_unlock(&info->delalloc_lock); |
| 4283 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4284 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4285 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4286 | if (!cache) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4287 | return -1; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4288 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 4289 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4290 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4291 | factor = 2; |
| 4292 | else |
| 4293 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 4294 | /* |
| 4295 | * If this block group has free space cache written out, we |
| 4296 | * need to make sure to load it if we are removing space. This |
| 4297 | * is because we need the unpinning stage to actually add the |
| 4298 | * space back to the block group, otherwise we will leak space. |
| 4299 | */ |
| 4300 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 4301 | cache_block_group(cache, trans, NULL, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4302 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4303 | byte_in_group = bytenr - cache->key.objectid; |
| 4304 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4305 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4306 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4307 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4308 | |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 4309 | if (btrfs_test_opt(root, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 4310 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 4311 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 4312 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4313 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4314 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4315 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4316 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4317 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4318 | btrfs_set_block_group_used(&cache->item, old_val); |
| 4319 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4320 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4321 | cache->space_info->bytes_used += num_bytes; |
| 4322 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4323 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4324 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4325 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4326 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4327 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4328 | cache->pinned += num_bytes; |
| 4329 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4330 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4331 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 4332 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4333 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4334 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4335 | set_extent_dirty(info->pinned_extents, |
| 4336 | bytenr, bytenr + num_bytes - 1, |
| 4337 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 4338 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4339 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4340 | total -= num_bytes; |
| 4341 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4342 | } |
| 4343 | return 0; |
| 4344 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4345 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4346 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 4347 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4348 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4349 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4350 | |
| 4351 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 4352 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4353 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4354 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4355 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4356 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 4357 | |
| 4358 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 4359 | } |
| 4360 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4361 | static int pin_down_extent(struct btrfs_root *root, |
| 4362 | struct btrfs_block_group_cache *cache, |
| 4363 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4364 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4365 | spin_lock(&cache->space_info->lock); |
| 4366 | spin_lock(&cache->lock); |
| 4367 | cache->pinned += num_bytes; |
| 4368 | cache->space_info->bytes_pinned += num_bytes; |
| 4369 | if (reserved) { |
| 4370 | cache->reserved -= num_bytes; |
| 4371 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4372 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4373 | spin_unlock(&cache->lock); |
| 4374 | spin_unlock(&cache->space_info->lock); |
| 4375 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4376 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 4377 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 4378 | return 0; |
| 4379 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 4380 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4381 | /* |
| 4382 | * this function must be called within transaction |
| 4383 | */ |
| 4384 | int btrfs_pin_extent(struct btrfs_root *root, |
| 4385 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4386 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4387 | struct btrfs_block_group_cache *cache; |
| 4388 | |
| 4389 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 4390 | BUG_ON(!cache); |
| 4391 | |
| 4392 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 4393 | |
| 4394 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4395 | return 0; |
| 4396 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 4397 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 4398 | /* |
| 4399 | * this function must be called within transaction |
| 4400 | */ |
| 4401 | int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, |
| 4402 | struct btrfs_root *root, |
| 4403 | u64 bytenr, u64 num_bytes) |
| 4404 | { |
| 4405 | struct btrfs_block_group_cache *cache; |
| 4406 | |
| 4407 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 4408 | BUG_ON(!cache); |
| 4409 | |
| 4410 | /* |
| 4411 | * pull in the free space cache (if any) so that our pin |
| 4412 | * removes the free space from the cache. We have load_only set |
| 4413 | * to one because the slow code to read in the free extents does check |
| 4414 | * the pinned extents. |
| 4415 | */ |
| 4416 | cache_block_group(cache, trans, root, 1); |
| 4417 | |
| 4418 | pin_down_extent(root, cache, bytenr, num_bytes, 0); |
| 4419 | |
| 4420 | /* remove us from the free space cache (if we're there at all) */ |
| 4421 | btrfs_remove_free_space(cache, bytenr, num_bytes); |
| 4422 | btrfs_put_block_group(cache); |
| 4423 | return 0; |
| 4424 | } |
| 4425 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4426 | /** |
| 4427 | * btrfs_update_reserved_bytes - update the block_group and space info counters |
| 4428 | * @cache: The cache we are manipulating |
| 4429 | * @num_bytes: The number of bytes in question |
| 4430 | * @reserve: One of the reservation enums |
| 4431 | * |
| 4432 | * This is called by the allocator when it reserves space, or by somebody who is |
| 4433 | * freeing space that was never actually used on disk. For example if you |
| 4434 | * reserve some space for a new leaf in transaction A and before transaction A |
| 4435 | * commits you free that leaf, you call this with reserve set to 0 in order to |
| 4436 | * clear the reservation. |
| 4437 | * |
| 4438 | * Metadata reservations should be called with RESERVE_ALLOC so we do the proper |
| 4439 | * ENOSPC accounting. For data we handle the reservation through clearing the |
| 4440 | * delalloc bits in the io_tree. We have to do this since we could end up |
| 4441 | * allocating less disk space for the amount of data we have reserved in the |
| 4442 | * case of compression. |
| 4443 | * |
| 4444 | * If this is a reservation and the block group has become read only we cannot |
| 4445 | * make the reservation and return -EAGAIN, otherwise this function always |
| 4446 | * succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4447 | */ |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4448 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 4449 | u64 num_bytes, int reserve) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4450 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4451 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4452 | int ret = 0; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4453 | spin_lock(&space_info->lock); |
| 4454 | spin_lock(&cache->lock); |
| 4455 | if (reserve != RESERVE_FREE) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4456 | if (cache->ro) { |
| 4457 | ret = -EAGAIN; |
| 4458 | } else { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4459 | cache->reserved += num_bytes; |
| 4460 | space_info->bytes_reserved += num_bytes; |
| 4461 | if (reserve == RESERVE_ALLOC) { |
| 4462 | BUG_ON(space_info->bytes_may_use < num_bytes); |
| 4463 | space_info->bytes_may_use -= num_bytes; |
| 4464 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4465 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4466 | } else { |
| 4467 | if (cache->ro) |
| 4468 | space_info->bytes_readonly += num_bytes; |
| 4469 | cache->reserved -= num_bytes; |
| 4470 | space_info->bytes_reserved -= num_bytes; |
| 4471 | space_info->reservation_progress++; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4472 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4473 | spin_unlock(&cache->lock); |
| 4474 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4475 | return ret; |
| 4476 | } |
| 4477 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4478 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
| 4479 | struct btrfs_root *root) |
| 4480 | { |
| 4481 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4482 | struct btrfs_caching_control *next; |
| 4483 | struct btrfs_caching_control *caching_ctl; |
| 4484 | struct btrfs_block_group_cache *cache; |
| 4485 | |
| 4486 | down_write(&fs_info->extent_commit_sem); |
| 4487 | |
| 4488 | list_for_each_entry_safe(caching_ctl, next, |
| 4489 | &fs_info->caching_block_groups, list) { |
| 4490 | cache = caching_ctl->block_group; |
| 4491 | if (block_group_cache_done(cache)) { |
| 4492 | cache->last_byte_to_unpin = (u64)-1; |
| 4493 | list_del_init(&caching_ctl->list); |
| 4494 | put_caching_control(caching_ctl); |
| 4495 | } else { |
| 4496 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 4497 | } |
| 4498 | } |
| 4499 | |
| 4500 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4501 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 4502 | else |
| 4503 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 4504 | |
| 4505 | up_write(&fs_info->extent_commit_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4506 | |
| 4507 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4508 | return 0; |
| 4509 | } |
| 4510 | |
| 4511 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 4512 | { |
| 4513 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4514 | struct btrfs_block_group_cache *cache = NULL; |
| 4515 | u64 len; |
| 4516 | |
| 4517 | while (start <= end) { |
| 4518 | if (!cache || |
| 4519 | start >= cache->key.objectid + cache->key.offset) { |
| 4520 | if (cache) |
| 4521 | btrfs_put_block_group(cache); |
| 4522 | cache = btrfs_lookup_block_group(fs_info, start); |
| 4523 | BUG_ON(!cache); |
| 4524 | } |
| 4525 | |
| 4526 | len = cache->key.objectid + cache->key.offset - start; |
| 4527 | len = min(len, end + 1 - start); |
| 4528 | |
| 4529 | if (start < cache->last_byte_to_unpin) { |
| 4530 | len = min(len, cache->last_byte_to_unpin - start); |
| 4531 | btrfs_add_free_space(cache, start, len); |
| 4532 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4533 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4534 | start += len; |
| 4535 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4536 | spin_lock(&cache->space_info->lock); |
| 4537 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4538 | cache->pinned -= len; |
| 4539 | cache->space_info->bytes_pinned -= len; |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4540 | if (cache->ro) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4541 | cache->space_info->bytes_readonly += len; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 4542 | spin_unlock(&cache->lock); |
| 4543 | spin_unlock(&cache->space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4544 | } |
| 4545 | |
| 4546 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 4547 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 4548 | return 0; |
| 4549 | } |
| 4550 | |
| 4551 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4552 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4553 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4554 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 4555 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4556 | u64 start; |
| 4557 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4558 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4559 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4560 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 4561 | unpin = &fs_info->freed_extents[1]; |
| 4562 | else |
| 4563 | unpin = &fs_info->freed_extents[0]; |
| 4564 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4565 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4566 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 4567 | EXTENT_DIRTY); |
| 4568 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4569 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4570 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 4571 | if (btrfs_test_opt(root, DISCARD)) |
| 4572 | ret = btrfs_discard_extent(root, start, |
| 4573 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 4574 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 4575 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4576 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4577 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4578 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4579 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 4580 | return 0; |
| 4581 | } |
| 4582 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4583 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 4584 | struct btrfs_root *root, |
| 4585 | u64 bytenr, u64 num_bytes, u64 parent, |
| 4586 | u64 root_objectid, u64 owner_objectid, |
| 4587 | u64 owner_offset, int refs_to_drop, |
| 4588 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4589 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4590 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4591 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 4592 | struct btrfs_fs_info *info = root->fs_info; |
| 4593 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4594 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4595 | struct btrfs_extent_item *ei; |
| 4596 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4597 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4598 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4599 | int extent_slot = 0; |
| 4600 | int found_extent = 0; |
| 4601 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4602 | u32 item_size; |
| 4603 | u64 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 4604 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4605 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4606 | if (!path) |
| 4607 | return -ENOMEM; |
| 4608 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 4609 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4610 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4611 | |
| 4612 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 4613 | BUG_ON(!is_data && refs_to_drop != 1); |
| 4614 | |
| 4615 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 4616 | bytenr, num_bytes, parent, |
| 4617 | root_objectid, owner_objectid, |
| 4618 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4619 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4620 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4621 | while (extent_slot >= 0) { |
| 4622 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4623 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4624 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4625 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4626 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 4627 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4628 | found_extent = 1; |
| 4629 | break; |
| 4630 | } |
| 4631 | if (path->slots[0] - extent_slot > 5) |
| 4632 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4633 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4634 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4635 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4636 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 4637 | if (found_extent && item_size < sizeof(*ei)) |
| 4638 | found_extent = 0; |
| 4639 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4640 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4641 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4642 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4643 | NULL, refs_to_drop, |
| 4644 | is_data); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4645 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4646 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4647 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4648 | |
| 4649 | key.objectid = bytenr; |
| 4650 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4651 | key.offset = num_bytes; |
| 4652 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4653 | ret = btrfs_search_slot(trans, extent_root, |
| 4654 | &key, path, -1, 1); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4655 | if (ret) { |
| 4656 | printk(KERN_ERR "umm, got %d back from search" |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4657 | ", was looking for %llu\n", ret, |
| 4658 | (unsigned long long)bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 4659 | if (ret > 0) |
| 4660 | btrfs_print_leaf(extent_root, |
| 4661 | path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 4662 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4663 | BUG_ON(ret); |
| 4664 | extent_slot = path->slots[0]; |
| 4665 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4666 | } else { |
| 4667 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4668 | WARN_ON(1); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4669 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4670 | "parent %llu root %llu owner %llu offset %llu\n", |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4671 | (unsigned long long)bytenr, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4672 | (unsigned long long)parent, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4673 | (unsigned long long)root_objectid, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4674 | (unsigned long long)owner_objectid, |
| 4675 | (unsigned long long)owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 4676 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4677 | |
| 4678 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4679 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4680 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 4681 | if (item_size < sizeof(*ei)) { |
| 4682 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 4683 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 4684 | owner_objectid, 0); |
| 4685 | BUG_ON(ret < 0); |
| 4686 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4687 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4688 | path->leave_spinning = 1; |
| 4689 | |
| 4690 | key.objectid = bytenr; |
| 4691 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 4692 | key.offset = num_bytes; |
| 4693 | |
| 4694 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 4695 | -1, 1); |
| 4696 | if (ret) { |
| 4697 | printk(KERN_ERR "umm, got %d back from search" |
| 4698 | ", was looking for %llu\n", ret, |
| 4699 | (unsigned long long)bytenr); |
| 4700 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 4701 | } |
| 4702 | BUG_ON(ret); |
| 4703 | extent_slot = path->slots[0]; |
| 4704 | leaf = path->nodes[0]; |
| 4705 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 4706 | } |
| 4707 | #endif |
| 4708 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4709 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 4710 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4711 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
| 4712 | struct btrfs_tree_block_info *bi; |
| 4713 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 4714 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 4715 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4716 | } |
| 4717 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4718 | refs = btrfs_extent_refs(leaf, ei); |
| 4719 | BUG_ON(refs < refs_to_drop); |
| 4720 | refs -= refs_to_drop; |
| 4721 | |
| 4722 | if (refs > 0) { |
| 4723 | if (extent_op) |
| 4724 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 4725 | /* |
| 4726 | * In the case of inline back ref, reference count will |
| 4727 | * be updated by remove_extent_backref |
| 4728 | */ |
| 4729 | if (iref) { |
| 4730 | BUG_ON(!found_extent); |
| 4731 | } else { |
| 4732 | btrfs_set_extent_refs(leaf, ei, refs); |
| 4733 | btrfs_mark_buffer_dirty(leaf); |
| 4734 | } |
| 4735 | if (found_extent) { |
| 4736 | ret = remove_extent_backref(trans, extent_root, path, |
| 4737 | iref, refs_to_drop, |
| 4738 | is_data); |
| 4739 | BUG_ON(ret); |
| 4740 | } |
| 4741 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4742 | if (found_extent) { |
| 4743 | BUG_ON(is_data && refs_to_drop != |
| 4744 | extent_data_ref_count(root, path, iref)); |
| 4745 | if (iref) { |
| 4746 | BUG_ON(path->slots[0] != extent_slot); |
| 4747 | } else { |
| 4748 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 4749 | path->slots[0] = extent_slot; |
| 4750 | num_to_del = 2; |
| 4751 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 4752 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4753 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 4754 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 4755 | num_to_del); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4756 | BUG_ON(ret); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4757 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 4758 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4759 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4760 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
| 4761 | BUG_ON(ret); |
Chris Mason | d57e62b | 2009-03-31 13:47:50 -0400 | [diff] [blame] | 4762 | } else { |
| 4763 | invalidate_mapping_pages(info->btree_inode->i_mapping, |
| 4764 | bytenr >> PAGE_CACHE_SHIFT, |
| 4765 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4766 | } |
| 4767 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4768 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 4769 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4770 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 4771 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 4772 | return ret; |
| 4773 | } |
| 4774 | |
| 4775 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4776 | * 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] | 4777 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 4778 | * a given extent, and if there are no other delayed refs to be processed, it |
| 4779 | * removes it from the tree. |
| 4780 | */ |
| 4781 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 4782 | struct btrfs_root *root, u64 bytenr) |
| 4783 | { |
| 4784 | struct btrfs_delayed_ref_head *head; |
| 4785 | struct btrfs_delayed_ref_root *delayed_refs; |
| 4786 | struct btrfs_delayed_ref_node *ref; |
| 4787 | struct rb_node *node; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4788 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4789 | |
| 4790 | delayed_refs = &trans->transaction->delayed_refs; |
| 4791 | spin_lock(&delayed_refs->lock); |
| 4792 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 4793 | if (!head) |
| 4794 | goto out; |
| 4795 | |
| 4796 | node = rb_prev(&head->node.rb_node); |
| 4797 | if (!node) |
| 4798 | goto out; |
| 4799 | |
| 4800 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 4801 | |
| 4802 | /* there are still entries for this ref, we can't drop it */ |
| 4803 | if (ref->bytenr == bytenr) |
| 4804 | goto out; |
| 4805 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4806 | if (head->extent_op) { |
| 4807 | if (!head->must_insert_reserved) |
| 4808 | goto out; |
| 4809 | kfree(head->extent_op); |
| 4810 | head->extent_op = NULL; |
| 4811 | } |
| 4812 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4813 | /* |
| 4814 | * waiting for the lock here would deadlock. If someone else has it |
| 4815 | * locked they are already in the process of dropping it anyway |
| 4816 | */ |
| 4817 | if (!mutex_trylock(&head->mutex)) |
| 4818 | goto out; |
| 4819 | |
| 4820 | /* |
| 4821 | * at this point we have a head with no other entries. Go |
| 4822 | * ahead and process it. |
| 4823 | */ |
| 4824 | head->node.in_tree = 0; |
| 4825 | rb_erase(&head->node.rb_node, &delayed_refs->root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4826 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4827 | delayed_refs->num_entries--; |
| 4828 | |
| 4829 | /* |
| 4830 | * we don't take a ref on the node because we're removing it from the |
| 4831 | * tree, so we just steal the ref the tree was holding. |
| 4832 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 4833 | delayed_refs->num_heads--; |
| 4834 | if (list_empty(&head->cluster)) |
| 4835 | delayed_refs->num_heads_ready--; |
| 4836 | |
| 4837 | list_del_init(&head->cluster); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4838 | spin_unlock(&delayed_refs->lock); |
| 4839 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4840 | BUG_ON(head->extent_op); |
| 4841 | if (head->must_insert_reserved) |
| 4842 | ret = 1; |
| 4843 | |
| 4844 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4845 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4846 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4847 | out: |
| 4848 | spin_unlock(&delayed_refs->lock); |
| 4849 | return 0; |
| 4850 | } |
| 4851 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4852 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 4853 | struct btrfs_root *root, |
| 4854 | struct extent_buffer *buf, |
| 4855 | u64 parent, int last_ref) |
| 4856 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4857 | struct btrfs_block_group_cache *cache = NULL; |
| 4858 | int ret; |
| 4859 | |
| 4860 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4861 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, |
| 4862 | parent, root->root_key.objectid, |
| 4863 | btrfs_header_level(buf), |
| 4864 | BTRFS_DROP_DELAYED_REF, NULL); |
| 4865 | BUG_ON(ret); |
| 4866 | } |
| 4867 | |
| 4868 | if (!last_ref) |
| 4869 | return; |
| 4870 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4871 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4872 | |
| 4873 | if (btrfs_header_generation(buf) == trans->transid) { |
| 4874 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 4875 | ret = check_ref_cleanup(trans, root, buf->start); |
| 4876 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4877 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 4881 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 4882 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 4886 | |
| 4887 | btrfs_add_free_space(cache, buf->start, buf->len); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4888 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4889 | } |
| 4890 | out: |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 4891 | /* |
| 4892 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 4893 | * anymore. |
| 4894 | */ |
| 4895 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4896 | btrfs_put_block_group(cache); |
| 4897 | } |
| 4898 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4899 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 4900 | struct btrfs_root *root, |
| 4901 | u64 bytenr, u64 num_bytes, u64 parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4902 | u64 root_objectid, u64 owner, u64 offset) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4903 | { |
| 4904 | int ret; |
| 4905 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4906 | /* |
| 4907 | * tree log blocks never actually go into the extent allocation |
| 4908 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4909 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4910 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 4911 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4912 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4913 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4914 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4915 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 4916 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, |
| 4917 | parent, root_objectid, (int)owner, |
| 4918 | BTRFS_DROP_DELAYED_REF, NULL); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 4919 | BUG_ON(ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4920 | } else { |
| 4921 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, |
| 4922 | parent, root_objectid, owner, |
| 4923 | offset, BTRFS_DROP_DELAYED_REF, NULL); |
| 4924 | BUG_ON(ret); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 4925 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4926 | return ret; |
| 4927 | } |
| 4928 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 4929 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 4930 | { |
| 4931 | u64 mask = ((u64)root->stripesize - 1); |
| 4932 | u64 ret = (val + mask) & ~mask; |
| 4933 | return ret; |
| 4934 | } |
| 4935 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 4936 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4937 | * when we wait for progress in the block group caching, its because |
| 4938 | * our allocation attempt failed at least once. So, we must sleep |
| 4939 | * and let some progress happen before we try again. |
| 4940 | * |
| 4941 | * This function will sleep at least once waiting for new free space to |
| 4942 | * show up, and then it will check the block group free space numbers |
| 4943 | * for our min num_bytes. Another option is to have it go ahead |
| 4944 | * and look in the rbtree for a free extent of a given size, but this |
| 4945 | * is a good start. |
| 4946 | */ |
| 4947 | static noinline int |
| 4948 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 4949 | u64 num_bytes) |
| 4950 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4951 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4952 | DEFINE_WAIT(wait); |
| 4953 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4954 | caching_ctl = get_caching_control(cache); |
| 4955 | if (!caching_ctl) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4956 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4957 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4958 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 4959 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 4960 | |
| 4961 | put_caching_control(caching_ctl); |
| 4962 | return 0; |
| 4963 | } |
| 4964 | |
| 4965 | static noinline int |
| 4966 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 4967 | { |
| 4968 | struct btrfs_caching_control *caching_ctl; |
| 4969 | DEFINE_WAIT(wait); |
| 4970 | |
| 4971 | caching_ctl = get_caching_control(cache); |
| 4972 | if (!caching_ctl) |
| 4973 | return 0; |
| 4974 | |
| 4975 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
| 4976 | |
| 4977 | put_caching_control(caching_ctl); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4978 | return 0; |
| 4979 | } |
| 4980 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 4981 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
| 4982 | { |
| 4983 | int index; |
| 4984 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) |
| 4985 | index = 0; |
| 4986 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) |
| 4987 | index = 1; |
| 4988 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) |
| 4989 | index = 2; |
| 4990 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) |
| 4991 | index = 3; |
| 4992 | else |
| 4993 | index = 4; |
| 4994 | return index; |
| 4995 | } |
| 4996 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4997 | enum btrfs_loop_type { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 4998 | LOOP_FIND_IDEAL = 0, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 4999 | LOOP_CACHING_NOWAIT = 1, |
| 5000 | LOOP_CACHING_WAIT = 2, |
| 5001 | LOOP_ALLOC_CHUNK = 3, |
| 5002 | LOOP_NO_EMPTY_SIZE = 4, |
| 5003 | }; |
| 5004 | |
| 5005 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5006 | * walks the btree of allocated extents and find a hole of a given size. |
| 5007 | * The key ins is changed to record the hole: |
| 5008 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 5009 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5010 | * ins->offset == number of blocks |
| 5011 | * Any available blocks before search_start are skipped. |
| 5012 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5013 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 5014 | struct btrfs_root *orig_root, |
| 5015 | u64 num_bytes, u64 empty_size, |
| 5016 | u64 search_start, u64 search_end, |
| 5017 | u64 hint_byte, struct btrfs_key *ins, |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 5018 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5019 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5020 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5021 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5022 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5023 | struct btrfs_block_group_cache *block_group = NULL; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5024 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5025 | int allowed_chunk_alloc = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5026 | int done_chunk_alloc = 0; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5027 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5028 | int last_ptr_loop = 0; |
| 5029 | int loop = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5030 | int index = 0; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5031 | int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ? |
| 5032 | RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5033 | bool found_uncached_bg = false; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5034 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5035 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5036 | bool use_cluster = true; |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5037 | bool have_caching_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5038 | u64 ideal_cache_percent = 0; |
| 5039 | u64 ideal_cache_offset = 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5040 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5041 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 5042 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5043 | ins->objectid = 0; |
| 5044 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 5045 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5046 | space_info = __find_space_info(root->fs_info, data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 5047 | if (!space_info) { |
Ilya Dryomov | e0f5406 | 2011-06-18 20:26:38 +0000 | [diff] [blame] | 5048 | printk(KERN_ERR "No space info for %llu\n", data); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 5049 | return -ENOSPC; |
| 5050 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5051 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5052 | /* |
| 5053 | * If the space info is for both data and metadata it means we have a |
| 5054 | * small filesystem and we can't use the clustering stuff. |
| 5055 | */ |
| 5056 | if (btrfs_mixed_space_info(space_info)) |
| 5057 | use_cluster = false; |
| 5058 | |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5059 | if (orig_root->ref_cows || empty_size) |
| 5060 | allowed_chunk_alloc = 1; |
| 5061 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5062 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5063 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 5064 | if (!btrfs_test_opt(root, SSD)) |
| 5065 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5066 | } |
| 5067 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 5068 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
| 5069 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5070 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 5071 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5072 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5073 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5074 | spin_lock(&last_ptr->lock); |
| 5075 | if (last_ptr->block_group) |
| 5076 | hint_byte = last_ptr->window_start; |
| 5077 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5078 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5079 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5080 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 5081 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5082 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5083 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5084 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5085 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5086 | if (search_start == hint_byte) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5087 | ideal_cache: |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5088 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 5089 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5090 | /* |
| 5091 | * we don't want to use the block group if it doesn't match our |
| 5092 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5093 | * |
| 5094 | * However if we are re-searching with an ideal block group |
| 5095 | * 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] | 5096 | */ |
| 5097 | if (block_group && block_group_bits(block_group, data) && |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5098 | (block_group->cached != BTRFS_CACHE_NO || |
| 5099 | search_start == ideal_cache_offset)) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5100 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5101 | if (list_empty(&block_group->list) || |
| 5102 | block_group->ro) { |
| 5103 | /* |
| 5104 | * someone is removing this block group, |
| 5105 | * we can't jump into the have_block_group |
| 5106 | * target because our list pointers are not |
| 5107 | * valid |
| 5108 | */ |
| 5109 | btrfs_put_block_group(block_group); |
| 5110 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5111 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5112 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5113 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5114 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5115 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5116 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5117 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5118 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5119 | search: |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5120 | have_caching_bg = false; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5121 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5122 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 5123 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5124 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5125 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 5126 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5127 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5128 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5129 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 5130 | /* |
| 5131 | * this can happen if we end up cycling through all the |
| 5132 | * raid types, but we want to make sure we only allocate |
| 5133 | * for the proper type. |
| 5134 | */ |
| 5135 | if (!block_group_bits(block_group, data)) { |
| 5136 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 5137 | BTRFS_BLOCK_GROUP_RAID1 | |
| 5138 | BTRFS_BLOCK_GROUP_RAID10; |
| 5139 | |
| 5140 | /* |
| 5141 | * if they asked for extra copies and this block group |
| 5142 | * doesn't provide them, bail. This does allow us to |
| 5143 | * fill raid0 from raid1. |
| 5144 | */ |
| 5145 | if ((data & extra) && !(block_group->flags & extra)) |
| 5146 | goto loop; |
| 5147 | } |
| 5148 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5149 | have_block_group: |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5150 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5151 | u64 free_percent; |
| 5152 | |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5153 | ret = cache_block_group(block_group, trans, |
| 5154 | orig_root, 1); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 5155 | if (block_group->cached == BTRFS_CACHE_FINISHED) |
| 5156 | goto have_block_group; |
| 5157 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5158 | free_percent = btrfs_block_group_used(&block_group->item); |
| 5159 | free_percent *= 100; |
| 5160 | free_percent = div64_u64(free_percent, |
| 5161 | block_group->key.offset); |
| 5162 | free_percent = 100 - free_percent; |
| 5163 | if (free_percent > ideal_cache_percent && |
| 5164 | likely(!block_group->ro)) { |
| 5165 | ideal_cache_offset = block_group->key.objectid; |
| 5166 | ideal_cache_percent = free_percent; |
| 5167 | } |
| 5168 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5169 | /* |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5170 | * The caching workers are limited to 2 threads, so we |
| 5171 | * can queue as much work as we care to. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5172 | */ |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5173 | if (loop > LOOP_FIND_IDEAL) { |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5174 | ret = cache_block_group(block_group, trans, |
| 5175 | orig_root, 0); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5176 | BUG_ON(ret); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5177 | } |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5178 | found_uncached_bg = true; |
| 5179 | |
| 5180 | /* |
| 5181 | * If loop is set for cached only, try the next block |
| 5182 | * group. |
| 5183 | */ |
| 5184 | if (loop == LOOP_FIND_IDEAL) |
| 5185 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5186 | } |
| 5187 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5188 | cached = block_group_cache_done(block_group); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5189 | if (unlikely(!cached)) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5190 | found_uncached_bg = true; |
| 5191 | |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 5192 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5193 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5194 | |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5195 | spin_lock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5196 | if (cached && |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5197 | block_group->free_space_ctl->free_space < |
| 5198 | num_bytes + empty_size) { |
| 5199 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5200 | goto loop; |
| 5201 | } |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 5202 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
Josef Bacik | cca1c81 | 2011-05-13 11:07:12 -0400 | [diff] [blame] | 5203 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5204 | /* |
| 5205 | * Ok we want to try and use the cluster allocator, so lets look |
| 5206 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will |
| 5207 | * have tried the cluster allocator plenty of times at this |
| 5208 | * point and not have found anything, so we are likely way too |
| 5209 | * fragmented for the clustering stuff to find anything, so lets |
| 5210 | * just skip it and let the allocator find whatever block it can |
| 5211 | * find |
| 5212 | */ |
| 5213 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5214 | /* |
| 5215 | * the refill lock keeps out other |
| 5216 | * people trying to start a new cluster |
| 5217 | */ |
| 5218 | spin_lock(&last_ptr->refill_lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5219 | if (last_ptr->block_group && |
| 5220 | (last_ptr->block_group->ro || |
| 5221 | !block_group_bits(last_ptr->block_group, data))) { |
| 5222 | offset = 0; |
| 5223 | goto refill_cluster; |
| 5224 | } |
| 5225 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5226 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
| 5227 | num_bytes, search_start); |
| 5228 | if (offset) { |
| 5229 | /* we have a block, we're done */ |
| 5230 | spin_unlock(&last_ptr->refill_lock); |
| 5231 | goto checks; |
| 5232 | } |
| 5233 | |
| 5234 | spin_lock(&last_ptr->lock); |
| 5235 | /* |
| 5236 | * whoops, this cluster doesn't actually point to |
| 5237 | * this block group. Get a ref on the block |
| 5238 | * group is does point to and try again |
| 5239 | */ |
| 5240 | if (!last_ptr_loop && last_ptr->block_group && |
liubo | ff1f2b4 | 2011-07-27 09:49:18 +0000 | [diff] [blame] | 5241 | last_ptr->block_group != block_group && |
| 5242 | index <= |
| 5243 | get_block_group_index(last_ptr->block_group)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5244 | |
| 5245 | btrfs_put_block_group(block_group); |
| 5246 | block_group = last_ptr->block_group; |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 5247 | btrfs_get_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5248 | spin_unlock(&last_ptr->lock); |
| 5249 | spin_unlock(&last_ptr->refill_lock); |
| 5250 | |
| 5251 | last_ptr_loop = 1; |
| 5252 | search_start = block_group->key.objectid; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5253 | /* |
| 5254 | * we know this block group is properly |
| 5255 | * in the list because |
| 5256 | * btrfs_remove_block_group, drops the |
| 5257 | * cluster before it removes the block |
| 5258 | * group from the list |
| 5259 | */ |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5260 | goto have_block_group; |
| 5261 | } |
| 5262 | spin_unlock(&last_ptr->lock); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 5263 | refill_cluster: |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5264 | /* |
| 5265 | * this cluster didn't work out, free it and |
| 5266 | * start over |
| 5267 | */ |
| 5268 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 5269 | |
| 5270 | last_ptr_loop = 0; |
| 5271 | |
| 5272 | /* allocate a cluster in this block group */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 5273 | ret = btrfs_find_space_cluster(trans, root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5274 | block_group, last_ptr, |
| 5275 | offset, num_bytes, |
| 5276 | empty_cluster + empty_size); |
| 5277 | if (ret == 0) { |
| 5278 | /* |
| 5279 | * now pull our allocation out of this |
| 5280 | * cluster |
| 5281 | */ |
| 5282 | offset = btrfs_alloc_from_cluster(block_group, |
| 5283 | last_ptr, num_bytes, |
| 5284 | search_start); |
| 5285 | if (offset) { |
| 5286 | /* we found one, proceed */ |
| 5287 | spin_unlock(&last_ptr->refill_lock); |
| 5288 | goto checks; |
| 5289 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5290 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 5291 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5292 | spin_unlock(&last_ptr->refill_lock); |
| 5293 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5294 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5295 | wait_block_group_cache_progress(block_group, |
| 5296 | num_bytes + empty_cluster + empty_size); |
| 5297 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5298 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5299 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5300 | /* |
| 5301 | * at this point we either didn't find a cluster |
| 5302 | * or we weren't able to allocate a block from our |
| 5303 | * cluster. Free the cluster we've been trying |
| 5304 | * to use, and go to the next block group |
| 5305 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5306 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5307 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5308 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5309 | } |
| 5310 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5311 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
| 5312 | num_bytes, empty_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5313 | /* |
| 5314 | * If we didn't find a chunk, and we haven't failed on this |
| 5315 | * block group before, and this block group is in the middle of |
| 5316 | * caching and we are ok with waiting, then go ahead and wait |
| 5317 | * for progress to be made, and set failed_alloc to true. |
| 5318 | * |
| 5319 | * If failed_alloc is true then we've already waited on this |
| 5320 | * block group once and should move on to the next block group. |
| 5321 | */ |
| 5322 | if (!offset && !failed_alloc && !cached && |
| 5323 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5324 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5325 | num_bytes + empty_size); |
| 5326 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5327 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5328 | } else if (!offset) { |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5329 | if (!cached) |
| 5330 | have_caching_bg = true; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5331 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5332 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5333 | checks: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5334 | search_start = stripe_align(root, offset); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5335 | /* move on to the next group */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5336 | if (search_start + num_bytes >= search_end) { |
| 5337 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5338 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5339 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 5340 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5341 | /* move on to the next group */ |
| 5342 | if (search_start + num_bytes > |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5343 | block_group->key.objectid + block_group->key.offset) { |
| 5344 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5345 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5346 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5347 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5348 | ins->objectid = search_start; |
| 5349 | ins->offset = num_bytes; |
| 5350 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5351 | if (offset < search_start) |
| 5352 | btrfs_add_free_space(block_group, offset, |
| 5353 | search_start - offset); |
| 5354 | BUG_ON(offset > search_start); |
| 5355 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5356 | ret = btrfs_update_reserved_bytes(block_group, num_bytes, |
| 5357 | alloc_type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5358 | if (ret == -EAGAIN) { |
| 5359 | btrfs_add_free_space(block_group, offset, num_bytes); |
| 5360 | goto loop; |
| 5361 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5362 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5363 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5364 | ins->objectid = search_start; |
| 5365 | ins->offset = num_bytes; |
| 5366 | |
| 5367 | if (offset < search_start) |
| 5368 | btrfs_add_free_space(block_group, offset, |
| 5369 | search_start - offset); |
| 5370 | BUG_ON(offset > search_start); |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5371 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5372 | break; |
| 5373 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 5374 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 5375 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5376 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5377 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5378 | } |
| 5379 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 5380 | |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 5381 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
| 5382 | goto search; |
| 5383 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5384 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 5385 | goto search; |
| 5386 | |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5387 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
| 5388 | * for them to make caching progress. Also |
| 5389 | * determine the best possible bg to cache |
| 5390 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 5391 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5392 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 5393 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 5394 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 5395 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5396 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5397 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5398 | index = 0; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5399 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5400 | found_uncached_bg = false; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5401 | loop++; |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 5402 | if (!ideal_cache_percent) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5403 | goto search; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 5404 | |
| 5405 | /* |
| 5406 | * 1 of the following 2 things have happened so far |
| 5407 | * |
| 5408 | * 1) We found an ideal block group for caching that |
| 5409 | * is mostly full and will cache quickly, so we might |
| 5410 | * as well wait for it. |
| 5411 | * |
| 5412 | * 2) We searched for cached only and we didn't find |
| 5413 | * anything, and we didn't start any caching kthreads |
| 5414 | * either, so chances are we will loop through and |
| 5415 | * start a couple caching kthreads, and then come back |
| 5416 | * around and just wait for them. This will be slower |
| 5417 | * because we will have 2 caching kthreads reading at |
| 5418 | * the same time when we could have just started one |
| 5419 | * and waited for it to get far enough to give us an |
| 5420 | * allocation, so go ahead and go to the wait caching |
| 5421 | * loop. |
| 5422 | */ |
| 5423 | loop = LOOP_CACHING_WAIT; |
| 5424 | search_start = ideal_cache_offset; |
| 5425 | ideal_cache_percent = 0; |
| 5426 | goto ideal_cache; |
| 5427 | } else if (loop == LOOP_FIND_IDEAL) { |
| 5428 | /* |
| 5429 | * Didn't find a uncached bg, wait on anything we find |
| 5430 | * next. |
| 5431 | */ |
| 5432 | loop = LOOP_CACHING_WAIT; |
| 5433 | goto search; |
| 5434 | } |
| 5435 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5436 | loop++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5437 | |
| 5438 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5439 | if (allowed_chunk_alloc) { |
| 5440 | ret = do_chunk_alloc(trans, root, num_bytes + |
| 5441 | 2 * 1024 * 1024, data, |
| 5442 | CHUNK_ALLOC_LIMITED); |
| 5443 | allowed_chunk_alloc = 0; |
| 5444 | if (ret == 1) |
| 5445 | done_chunk_alloc = 1; |
| 5446 | } else if (!done_chunk_alloc && |
| 5447 | space_info->force_alloc == |
| 5448 | CHUNK_ALLOC_NO_FORCE) { |
| 5449 | space_info->force_alloc = CHUNK_ALLOC_LIMITED; |
| 5450 | } |
| 5451 | |
| 5452 | /* |
| 5453 | * We didn't allocate a chunk, go ahead and drop the |
| 5454 | * empty size and loop again. |
| 5455 | */ |
| 5456 | if (!done_chunk_alloc) |
| 5457 | loop = LOOP_NO_EMPTY_SIZE; |
| 5458 | } |
| 5459 | |
| 5460 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5461 | empty_size = 0; |
| 5462 | empty_cluster = 0; |
| 5463 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 5464 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 5465 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5466 | } else if (!ins->objectid) { |
| 5467 | ret = -ENOSPC; |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 5468 | } else if (ins->objectid) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 5469 | ret = 0; |
| 5470 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5471 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5472 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5473 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 5474 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5475 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 5476 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5477 | { |
| 5478 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5479 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5480 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5481 | spin_lock(&info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5482 | printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n", |
| 5483 | (unsigned long long)info->flags, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5484 | (unsigned long long)(info->total_bytes - info->bytes_used - |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5485 | info->bytes_pinned - info->bytes_reserved - |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5486 | info->bytes_readonly), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5487 | (info->full) ? "" : "not "); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5488 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
| 5489 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 5490 | (unsigned long long)info->total_bytes, |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5491 | (unsigned long long)info->bytes_used, |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5492 | (unsigned long long)info->bytes_pinned, |
| 5493 | (unsigned long long)info->bytes_reserved, |
| 5494 | (unsigned long long)info->bytes_may_use, |
| 5495 | (unsigned long long)info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5496 | spin_unlock(&info->lock); |
| 5497 | |
| 5498 | if (!dump_block_groups) |
| 5499 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5500 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5501 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5502 | again: |
| 5503 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5504 | spin_lock(&cache->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5505 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
| 5506 | "%llu pinned %llu reserved\n", |
| 5507 | (unsigned long long)cache->key.objectid, |
| 5508 | (unsigned long long)cache->key.offset, |
| 5509 | (unsigned long long)btrfs_block_group_used(&cache->item), |
| 5510 | (unsigned long long)cache->pinned, |
| 5511 | (unsigned long long)cache->reserved); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5512 | btrfs_dump_free_space(cache, bytes); |
| 5513 | spin_unlock(&cache->lock); |
| 5514 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5515 | if (++index < BTRFS_NR_RAID_TYPES) |
| 5516 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5517 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5518 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5519 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5520 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
| 5521 | struct btrfs_root *root, |
| 5522 | u64 num_bytes, u64 min_alloc_size, |
| 5523 | u64 empty_size, u64 hint_byte, |
| 5524 | u64 search_end, struct btrfs_key *ins, |
| 5525 | u64 data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5526 | { |
| 5527 | int ret; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 5528 | u64 search_start = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5529 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 5530 | data = btrfs_get_alloc_profile(root, data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5531 | again: |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5532 | /* |
| 5533 | * the only place that sets empty_size is btrfs_realloc_node, which |
| 5534 | * is not called recursively on allocations |
| 5535 | */ |
Josef Bacik | 83d3c96 | 2009-12-07 21:45:59 +0000 | [diff] [blame] | 5536 | if (empty_size || root->ref_cows) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5537 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5538 | num_bytes + 2 * 1024 * 1024, data, |
| 5539 | CHUNK_ALLOC_NO_FORCE); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5540 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5541 | WARN_ON(num_bytes < root->sectorsize); |
| 5542 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5543 | search_start, search_end, hint_byte, |
| 5544 | ins, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 5545 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5546 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 5547 | num_bytes = num_bytes >> 1; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5548 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5549 | num_bytes = max(num_bytes, min_alloc_size); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 5550 | do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 5551 | num_bytes, data, CHUNK_ALLOC_FORCE); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 5552 | goto again; |
| 5553 | } |
Chris Mason | 9143565 | 2011-02-16 13:10:41 -0500 | [diff] [blame] | 5554 | if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5555 | struct btrfs_space_info *sinfo; |
| 5556 | |
| 5557 | sinfo = __find_space_info(root->fs_info, data); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5558 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
| 5559 | "wanted %llu\n", (unsigned long long)data, |
| 5560 | (unsigned long long)num_bytes); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 5561 | dump_space_info(sinfo, num_bytes, 1); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5562 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5563 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5564 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
| 5565 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5566 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5567 | } |
| 5568 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5569 | static int __btrfs_free_reserved_extent(struct btrfs_root *root, |
| 5570 | u64 start, u64 len, int pin) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5571 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5572 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5573 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5574 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5575 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 5576 | if (!cache) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5577 | printk(KERN_ERR "Unable to find block group for %llu\n", |
| 5578 | (unsigned long long)start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5579 | return -ENOSPC; |
| 5580 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5581 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5582 | if (btrfs_test_opt(root, DISCARD)) |
| 5583 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5584 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5585 | if (pin) |
| 5586 | pin_down_extent(root, cache, start, len, 1); |
| 5587 | else { |
| 5588 | btrfs_add_free_space(cache, start, len); |
| 5589 | btrfs_update_reserved_bytes(cache, len, RESERVE_FREE); |
| 5590 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5591 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5592 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 5593 | trace_btrfs_reserved_extent_free(root, start, len); |
| 5594 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5595 | return ret; |
| 5596 | } |
| 5597 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5598 | int btrfs_free_reserved_extent(struct btrfs_root *root, |
| 5599 | u64 start, u64 len) |
| 5600 | { |
| 5601 | return __btrfs_free_reserved_extent(root, start, len, 0); |
| 5602 | } |
| 5603 | |
| 5604 | int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, |
| 5605 | u64 start, u64 len) |
| 5606 | { |
| 5607 | return __btrfs_free_reserved_extent(root, start, len, 1); |
| 5608 | } |
| 5609 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5610 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5611 | struct btrfs_root *root, |
| 5612 | u64 parent, u64 root_objectid, |
| 5613 | u64 flags, u64 owner, u64 offset, |
| 5614 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5615 | { |
| 5616 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5617 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5618 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5619 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5620 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5621 | struct extent_buffer *leaf; |
| 5622 | int type; |
| 5623 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 5624 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5625 | if (parent > 0) |
| 5626 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 5627 | else |
| 5628 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5629 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5630 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5631 | |
| 5632 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 5633 | if (!path) |
| 5634 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5635 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5636 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5637 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5638 | ins, size); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5639 | BUG_ON(ret); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5640 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5641 | leaf = path->nodes[0]; |
| 5642 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5643 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5644 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 5645 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5646 | btrfs_set_extent_flags(leaf, extent_item, |
| 5647 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5648 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5649 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 5650 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 5651 | if (parent > 0) { |
| 5652 | struct btrfs_shared_data_ref *ref; |
| 5653 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 5654 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5655 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 5656 | } else { |
| 5657 | struct btrfs_extent_data_ref *ref; |
| 5658 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 5659 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 5660 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 5661 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 5662 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 5663 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 5664 | |
| 5665 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5666 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 5667 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5668 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5669 | if (ret) { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5670 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5671 | "%llu\n", (unsigned long long)ins->objectid, |
| 5672 | (unsigned long long)ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 5673 | BUG(); |
| 5674 | } |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5675 | return ret; |
| 5676 | } |
| 5677 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5678 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 5679 | struct btrfs_root *root, |
| 5680 | u64 parent, u64 root_objectid, |
| 5681 | u64 flags, struct btrfs_disk_key *key, |
| 5682 | int level, struct btrfs_key *ins) |
| 5683 | { |
| 5684 | int ret; |
| 5685 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5686 | struct btrfs_extent_item *extent_item; |
| 5687 | struct btrfs_tree_block_info *block_info; |
| 5688 | struct btrfs_extent_inline_ref *iref; |
| 5689 | struct btrfs_path *path; |
| 5690 | struct extent_buffer *leaf; |
| 5691 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); |
| 5692 | |
| 5693 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 5694 | if (!path) |
| 5695 | return -ENOMEM; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5696 | |
| 5697 | path->leave_spinning = 1; |
| 5698 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 5699 | ins, size); |
| 5700 | BUG_ON(ret); |
| 5701 | |
| 5702 | leaf = path->nodes[0]; |
| 5703 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 5704 | struct btrfs_extent_item); |
| 5705 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 5706 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 5707 | btrfs_set_extent_flags(leaf, extent_item, |
| 5708 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
| 5709 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 5710 | |
| 5711 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 5712 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 5713 | |
| 5714 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 5715 | if (parent > 0) { |
| 5716 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 5717 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5718 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 5719 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 5720 | } else { |
| 5721 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 5722 | BTRFS_TREE_BLOCK_REF_KEY); |
| 5723 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 5724 | } |
| 5725 | |
| 5726 | btrfs_mark_buffer_dirty(leaf); |
| 5727 | btrfs_free_path(path); |
| 5728 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5729 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5730 | if (ret) { |
| 5731 | printk(KERN_ERR "btrfs update block group failed for %llu " |
| 5732 | "%llu\n", (unsigned long long)ins->objectid, |
| 5733 | (unsigned long long)ins->offset); |
| 5734 | BUG(); |
| 5735 | } |
| 5736 | return ret; |
| 5737 | } |
| 5738 | |
| 5739 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 5740 | struct btrfs_root *root, |
| 5741 | u64 root_objectid, u64 owner, |
| 5742 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5743 | { |
| 5744 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 5745 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5746 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5747 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5748 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, |
| 5749 | 0, root_objectid, owner, offset, |
| 5750 | BTRFS_ADD_DELAYED_EXTENT, NULL); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 5751 | return ret; |
| 5752 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5753 | |
| 5754 | /* |
| 5755 | * this is used by the tree logging recovery code. It records that |
| 5756 | * an extent has been allocated and makes sure to clear the free |
| 5757 | * space cache bits as well |
| 5758 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5759 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 5760 | struct btrfs_root *root, |
| 5761 | u64 root_objectid, u64 owner, u64 offset, |
| 5762 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5763 | { |
| 5764 | int ret; |
| 5765 | struct btrfs_block_group_cache *block_group; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5766 | struct btrfs_caching_control *caching_ctl; |
| 5767 | u64 start = ins->objectid; |
| 5768 | u64 num_bytes = ins->offset; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5769 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5770 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | b8399de | 2010-12-08 09:15:11 -0500 | [diff] [blame] | 5771 | cache_block_group(block_group, trans, NULL, 0); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5772 | caching_ctl = get_caching_control(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5773 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5774 | if (!caching_ctl) { |
| 5775 | BUG_ON(!block_group_cache_done(block_group)); |
| 5776 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5777 | BUG_ON(ret); |
| 5778 | } else { |
| 5779 | mutex_lock(&caching_ctl->mutex); |
| 5780 | |
| 5781 | if (start >= caching_ctl->progress) { |
| 5782 | ret = add_excluded_extent(root, start, num_bytes); |
| 5783 | BUG_ON(ret); |
| 5784 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5785 | ret = btrfs_remove_free_space(block_group, |
| 5786 | start, num_bytes); |
| 5787 | BUG_ON(ret); |
| 5788 | } else { |
| 5789 | num_bytes = caching_ctl->progress - start; |
| 5790 | ret = btrfs_remove_free_space(block_group, |
| 5791 | start, num_bytes); |
| 5792 | BUG_ON(ret); |
| 5793 | |
| 5794 | start = caching_ctl->progress; |
| 5795 | num_bytes = ins->objectid + ins->offset - |
| 5796 | caching_ctl->progress; |
| 5797 | ret = add_excluded_extent(root, start, num_bytes); |
| 5798 | BUG_ON(ret); |
| 5799 | } |
| 5800 | |
| 5801 | mutex_unlock(&caching_ctl->mutex); |
| 5802 | put_caching_control(caching_ctl); |
| 5803 | } |
| 5804 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5805 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, |
| 5806 | RESERVE_ALLOC_NO_ACCOUNT); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5807 | BUG_ON(ret); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5808 | btrfs_put_block_group(block_group); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5809 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 5810 | 0, owner, offset, ins, 1); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5811 | return ret; |
| 5812 | } |
| 5813 | |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5814 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
| 5815 | struct btrfs_root *root, |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5816 | u64 bytenr, u32 blocksize, |
| 5817 | int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5818 | { |
| 5819 | struct extent_buffer *buf; |
| 5820 | |
| 5821 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 5822 | if (!buf) |
| 5823 | return ERR_PTR(-ENOMEM); |
| 5824 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 5825 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5826 | btrfs_tree_lock(buf); |
| 5827 | clean_tree_block(trans, root, buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5828 | |
| 5829 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5830 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5831 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5832 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 5833 | /* |
| 5834 | * we allow two log transactions at a time, use different |
| 5835 | * EXENT bit to differentiate dirty pages. |
| 5836 | */ |
| 5837 | if (root->log_transid % 2 == 0) |
| 5838 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 5839 | buf->start + buf->len - 1, GFP_NOFS); |
| 5840 | else |
| 5841 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 5842 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 5843 | } else { |
| 5844 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 5845 | buf->start + buf->len - 1, GFP_NOFS); |
| 5846 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5847 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5848 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 5849 | return buf; |
| 5850 | } |
| 5851 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5852 | static struct btrfs_block_rsv * |
| 5853 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 5854 | struct btrfs_root *root, u32 blocksize) |
| 5855 | { |
| 5856 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5857 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5858 | int ret; |
| 5859 | |
| 5860 | block_rsv = get_block_rsv(trans, root); |
| 5861 | |
| 5862 | if (block_rsv->size == 0) { |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5863 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5864 | /* |
| 5865 | * If we couldn't reserve metadata bytes try and use some from |
| 5866 | * the global reserve. |
| 5867 | */ |
| 5868 | if (ret && block_rsv != global_rsv) { |
| 5869 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5870 | if (!ret) |
| 5871 | return global_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5872 | return ERR_PTR(ret); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5873 | } else if (ret) { |
| 5874 | return ERR_PTR(ret); |
| 5875 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5876 | return block_rsv; |
| 5877 | } |
| 5878 | |
| 5879 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 5880 | if (!ret) |
| 5881 | return block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5882 | if (ret) { |
David Sterba | dff51cd | 2011-06-14 12:52:17 +0200 | [diff] [blame] | 5883 | static DEFINE_RATELIMIT_STATE(_rs, |
| 5884 | DEFAULT_RATELIMIT_INTERVAL, |
| 5885 | /*DEFAULT_RATELIMIT_BURST*/ 2); |
| 5886 | if (__ratelimit(&_rs)) { |
| 5887 | printk(KERN_DEBUG "btrfs: block rsv returned %d\n", ret); |
| 5888 | WARN_ON(1); |
| 5889 | } |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 5890 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0); |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5891 | if (!ret) { |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 5892 | return block_rsv; |
| 5893 | } else if (ret && block_rsv != global_rsv) { |
| 5894 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 5895 | if (!ret) |
| 5896 | return global_rsv; |
| 5897 | } |
| 5898 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5899 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5900 | return ERR_PTR(-ENOSPC); |
| 5901 | } |
| 5902 | |
| 5903 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) |
| 5904 | { |
| 5905 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
| 5906 | block_rsv_release_bytes(block_rsv, NULL, 0); |
| 5907 | } |
| 5908 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5909 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5910 | * finds a free extent and does all the dirty work required for allocation |
| 5911 | * returns the key for the extent through ins, and a tree buffer for |
| 5912 | * the first block of the extent through buf. |
| 5913 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5914 | * returns the tree buffer or NULL. |
| 5915 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5916 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5917 | struct btrfs_root *root, u32 blocksize, |
| 5918 | u64 parent, u64 root_objectid, |
| 5919 | struct btrfs_disk_key *key, int level, |
| 5920 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5921 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5922 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5923 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5924 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5925 | u64 flags = 0; |
| 5926 | int ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5927 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5928 | |
| 5929 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 5930 | if (IS_ERR(block_rsv)) |
| 5931 | return ERR_CAST(block_rsv); |
| 5932 | |
| 5933 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, |
| 5934 | empty_size, hint, (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5935 | if (ret) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5936 | unuse_block_rsv(block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5937 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5938 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 5939 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 5940 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 5941 | blocksize, level); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5942 | BUG_ON(IS_ERR(buf)); |
| 5943 | |
| 5944 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 5945 | if (parent == 0) |
| 5946 | parent = ins.objectid; |
| 5947 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 5948 | } else |
| 5949 | BUG_ON(parent > 0); |
| 5950 | |
| 5951 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 5952 | struct btrfs_delayed_extent_op *extent_op; |
| 5953 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); |
| 5954 | BUG_ON(!extent_op); |
| 5955 | if (key) |
| 5956 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 5957 | else |
| 5958 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 5959 | extent_op->flags_to_set = flags; |
| 5960 | extent_op->update_key = 1; |
| 5961 | extent_op->update_flags = 1; |
| 5962 | extent_op->is_data = 0; |
| 5963 | |
| 5964 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, |
| 5965 | ins.offset, parent, root_objectid, |
| 5966 | level, BTRFS_ADD_DELAYED_EXTENT, |
| 5967 | extent_op); |
| 5968 | BUG_ON(ret); |
| 5969 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 5970 | return buf; |
| 5971 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5972 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5973 | struct walk_control { |
| 5974 | u64 refs[BTRFS_MAX_LEVEL]; |
| 5975 | u64 flags[BTRFS_MAX_LEVEL]; |
| 5976 | struct btrfs_key update_progress; |
| 5977 | int stage; |
| 5978 | int level; |
| 5979 | int shared_level; |
| 5980 | int update_ref; |
| 5981 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5982 | int reada_slot; |
| 5983 | int reada_count; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 5984 | }; |
| 5985 | |
| 5986 | #define DROP_REFERENCE 1 |
| 5987 | #define UPDATE_BACKREF 2 |
| 5988 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5989 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 5990 | struct btrfs_root *root, |
| 5991 | struct walk_control *wc, |
| 5992 | struct btrfs_path *path) |
| 5993 | { |
| 5994 | u64 bytenr; |
| 5995 | u64 generation; |
| 5996 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 5997 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 5998 | u32 nritems; |
| 5999 | u32 blocksize; |
| 6000 | struct btrfs_key key; |
| 6001 | struct extent_buffer *eb; |
| 6002 | int ret; |
| 6003 | int slot; |
| 6004 | int nread = 0; |
| 6005 | |
| 6006 | if (path->slots[wc->level] < wc->reada_slot) { |
| 6007 | wc->reada_count = wc->reada_count * 2 / 3; |
| 6008 | wc->reada_count = max(wc->reada_count, 2); |
| 6009 | } else { |
| 6010 | wc->reada_count = wc->reada_count * 3 / 2; |
| 6011 | wc->reada_count = min_t(int, wc->reada_count, |
| 6012 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 6013 | } |
| 6014 | |
| 6015 | eb = path->nodes[wc->level]; |
| 6016 | nritems = btrfs_header_nritems(eb); |
| 6017 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 6018 | |
| 6019 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 6020 | if (nread >= wc->reada_count) |
| 6021 | break; |
| 6022 | |
| 6023 | cond_resched(); |
| 6024 | bytenr = btrfs_node_blockptr(eb, slot); |
| 6025 | generation = btrfs_node_ptr_generation(eb, slot); |
| 6026 | |
| 6027 | if (slot == path->slots[wc->level]) |
| 6028 | goto reada; |
| 6029 | |
| 6030 | if (wc->stage == UPDATE_BACKREF && |
| 6031 | generation <= root->root_key.offset) |
| 6032 | continue; |
| 6033 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6034 | /* We don't lock the tree block, it's OK to be racy here */ |
| 6035 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6036 | &refs, &flags); |
| 6037 | BUG_ON(ret); |
| 6038 | BUG_ON(refs == 0); |
| 6039 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6040 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6041 | if (refs == 1) |
| 6042 | goto reada; |
| 6043 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6044 | if (wc->level == 1 && |
| 6045 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6046 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6047 | if (!wc->update_ref || |
| 6048 | generation <= root->root_key.offset) |
| 6049 | continue; |
| 6050 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 6051 | ret = btrfs_comp_cpu_keys(&key, |
| 6052 | &wc->update_progress); |
| 6053 | if (ret < 0) |
| 6054 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6055 | } else { |
| 6056 | if (wc->level == 1 && |
| 6057 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6058 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6059 | } |
| 6060 | reada: |
| 6061 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 6062 | generation); |
| 6063 | if (ret) |
| 6064 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6065 | nread++; |
| 6066 | } |
| 6067 | wc->reada_slot = slot; |
| 6068 | } |
| 6069 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6070 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6071 | * hepler to process tree block while walking down the tree. |
| 6072 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6073 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 6074 | * back refs for pointers in the block. |
| 6075 | * |
| 6076 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6077 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6078 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 6079 | struct btrfs_root *root, |
| 6080 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6081 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6082 | { |
| 6083 | int level = wc->level; |
| 6084 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6085 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6086 | int ret; |
| 6087 | |
| 6088 | if (wc->stage == UPDATE_BACKREF && |
| 6089 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 6090 | return 1; |
| 6091 | |
| 6092 | /* |
| 6093 | * when reference count of tree block is 1, it won't increase |
| 6094 | * again. once full backref flag is set, we never clear it. |
| 6095 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6096 | if (lookup_info && |
| 6097 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 6098 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6099 | BUG_ON(!path->locks[level]); |
| 6100 | ret = btrfs_lookup_extent_info(trans, root, |
| 6101 | eb->start, eb->len, |
| 6102 | &wc->refs[level], |
| 6103 | &wc->flags[level]); |
| 6104 | BUG_ON(ret); |
| 6105 | BUG_ON(wc->refs[level] == 0); |
| 6106 | } |
| 6107 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6108 | if (wc->stage == DROP_REFERENCE) { |
| 6109 | if (wc->refs[level] > 1) |
| 6110 | return 1; |
| 6111 | |
| 6112 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6113 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6114 | path->locks[level] = 0; |
| 6115 | } |
| 6116 | return 0; |
| 6117 | } |
| 6118 | |
| 6119 | /* wc->stage == UPDATE_BACKREF */ |
| 6120 | if (!(wc->flags[level] & flag)) { |
| 6121 | BUG_ON(!path->locks[level]); |
| 6122 | ret = btrfs_inc_ref(trans, root, eb, 1); |
| 6123 | BUG_ON(ret); |
| 6124 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6125 | BUG_ON(ret); |
| 6126 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
| 6127 | eb->len, flag, 0); |
| 6128 | BUG_ON(ret); |
| 6129 | wc->flags[level] |= flag; |
| 6130 | } |
| 6131 | |
| 6132 | /* |
| 6133 | * the block is shared by multiple trees, so it's not good to |
| 6134 | * keep the tree lock |
| 6135 | */ |
| 6136 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6137 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6138 | path->locks[level] = 0; |
| 6139 | } |
| 6140 | return 0; |
| 6141 | } |
| 6142 | |
| 6143 | /* |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6144 | * hepler to process tree block pointer. |
| 6145 | * |
| 6146 | * when wc->stage == DROP_REFERENCE, this function checks |
| 6147 | * reference count of the block pointed to. if the block |
| 6148 | * is shared and we need update back refs for the subtree |
| 6149 | * rooted at the block, this function changes wc->stage to |
| 6150 | * UPDATE_BACKREF. if the block is shared and there is no |
| 6151 | * need to update back, this function drops the reference |
| 6152 | * to the block. |
| 6153 | * |
| 6154 | * NOTE: return value 1 means we should stop walking down. |
| 6155 | */ |
| 6156 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 6157 | struct btrfs_root *root, |
| 6158 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6159 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6160 | { |
| 6161 | u64 bytenr; |
| 6162 | u64 generation; |
| 6163 | u64 parent; |
| 6164 | u32 blocksize; |
| 6165 | struct btrfs_key key; |
| 6166 | struct extent_buffer *next; |
| 6167 | int level = wc->level; |
| 6168 | int reada = 0; |
| 6169 | int ret = 0; |
| 6170 | |
| 6171 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 6172 | path->slots[level]); |
| 6173 | /* |
| 6174 | * if the lower level block was created before the snapshot |
| 6175 | * was created, we know there is no need to update back refs |
| 6176 | * for the subtree |
| 6177 | */ |
| 6178 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6179 | generation <= root->root_key.offset) { |
| 6180 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6181 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6182 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6183 | |
| 6184 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 6185 | blocksize = btrfs_level_size(root, level - 1); |
| 6186 | |
| 6187 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 6188 | if (!next) { |
| 6189 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6190 | if (!next) |
| 6191 | return -ENOMEM; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6192 | reada = 1; |
| 6193 | } |
| 6194 | btrfs_tree_lock(next); |
| 6195 | btrfs_set_lock_blocking(next); |
| 6196 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6197 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
| 6198 | &wc->refs[level - 1], |
| 6199 | &wc->flags[level - 1]); |
| 6200 | BUG_ON(ret); |
| 6201 | BUG_ON(wc->refs[level - 1] == 0); |
| 6202 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6203 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6204 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6205 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6206 | if (level == 1 && |
| 6207 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6208 | goto skip; |
| 6209 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6210 | if (!wc->update_ref || |
| 6211 | generation <= root->root_key.offset) |
| 6212 | goto skip; |
| 6213 | |
| 6214 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 6215 | path->slots[level]); |
| 6216 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 6217 | if (ret < 0) |
| 6218 | goto skip; |
| 6219 | |
| 6220 | wc->stage = UPDATE_BACKREF; |
| 6221 | wc->shared_level = level - 1; |
| 6222 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6223 | } else { |
| 6224 | if (level == 1 && |
| 6225 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 6226 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6227 | } |
| 6228 | |
| 6229 | if (!btrfs_buffer_uptodate(next, generation)) { |
| 6230 | btrfs_tree_unlock(next); |
| 6231 | free_extent_buffer(next); |
| 6232 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6233 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | if (!next) { |
| 6237 | if (reada && level == 1) |
| 6238 | reada_walk_down(trans, root, wc, path); |
| 6239 | next = read_tree_block(root, bytenr, blocksize, generation); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 6240 | if (!next) |
| 6241 | return -EIO; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6242 | btrfs_tree_lock(next); |
| 6243 | btrfs_set_lock_blocking(next); |
| 6244 | } |
| 6245 | |
| 6246 | level--; |
| 6247 | BUG_ON(level != btrfs_header_level(next)); |
| 6248 | path->nodes[level] = next; |
| 6249 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6250 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6251 | wc->level = level; |
| 6252 | if (wc->level == 1) |
| 6253 | wc->reada_slot = 0; |
| 6254 | return 0; |
| 6255 | skip: |
| 6256 | wc->refs[level - 1] = 0; |
| 6257 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6258 | if (wc->stage == DROP_REFERENCE) { |
| 6259 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 6260 | parent = path->nodes[level]->start; |
| 6261 | } else { |
| 6262 | BUG_ON(root->root_key.objectid != |
| 6263 | btrfs_header_owner(path->nodes[level])); |
| 6264 | parent = 0; |
| 6265 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6266 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6267 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
| 6268 | root->root_key.objectid, level - 1, 0); |
| 6269 | BUG_ON(ret); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6270 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6271 | btrfs_tree_unlock(next); |
| 6272 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6273 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6274 | return 1; |
| 6275 | } |
| 6276 | |
| 6277 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6278 | * hepler to process tree block while walking up the tree. |
| 6279 | * |
| 6280 | * when wc->stage == DROP_REFERENCE, this function drops |
| 6281 | * reference count on the block. |
| 6282 | * |
| 6283 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 6284 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 6285 | * to UPDATE_BACKREF previously while processing the block. |
| 6286 | * |
| 6287 | * NOTE: return value 1 means we should stop walking up. |
| 6288 | */ |
| 6289 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 6290 | struct btrfs_root *root, |
| 6291 | struct btrfs_path *path, |
| 6292 | struct walk_control *wc) |
| 6293 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6294 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6295 | int level = wc->level; |
| 6296 | struct extent_buffer *eb = path->nodes[level]; |
| 6297 | u64 parent = 0; |
| 6298 | |
| 6299 | if (wc->stage == UPDATE_BACKREF) { |
| 6300 | BUG_ON(wc->shared_level < level); |
| 6301 | if (level < wc->shared_level) |
| 6302 | goto out; |
| 6303 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6304 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 6305 | if (ret > 0) |
| 6306 | wc->update_ref = 0; |
| 6307 | |
| 6308 | wc->stage = DROP_REFERENCE; |
| 6309 | wc->shared_level = -1; |
| 6310 | path->slots[level] = 0; |
| 6311 | |
| 6312 | /* |
| 6313 | * check reference count again if the block isn't locked. |
| 6314 | * we should start walking down the tree again if reference |
| 6315 | * count is one. |
| 6316 | */ |
| 6317 | if (!path->locks[level]) { |
| 6318 | BUG_ON(level == 0); |
| 6319 | btrfs_tree_lock(eb); |
| 6320 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6321 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6322 | |
| 6323 | ret = btrfs_lookup_extent_info(trans, root, |
| 6324 | eb->start, eb->len, |
| 6325 | &wc->refs[level], |
| 6326 | &wc->flags[level]); |
| 6327 | BUG_ON(ret); |
| 6328 | BUG_ON(wc->refs[level] == 0); |
| 6329 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6330 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6331 | return 1; |
| 6332 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6333 | } |
| 6334 | } |
| 6335 | |
| 6336 | /* wc->stage == DROP_REFERENCE */ |
| 6337 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 6338 | |
| 6339 | if (wc->refs[level] == 1) { |
| 6340 | if (level == 0) { |
| 6341 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6342 | ret = btrfs_dec_ref(trans, root, eb, 1); |
| 6343 | else |
| 6344 | ret = btrfs_dec_ref(trans, root, eb, 0); |
| 6345 | BUG_ON(ret); |
| 6346 | } |
| 6347 | /* make block locked assertion in clean_tree_block happy */ |
| 6348 | if (!path->locks[level] && |
| 6349 | btrfs_header_generation(eb) == trans->transid) { |
| 6350 | btrfs_tree_lock(eb); |
| 6351 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6352 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6353 | } |
| 6354 | clean_tree_block(trans, root, eb); |
| 6355 | } |
| 6356 | |
| 6357 | if (eb == root->node) { |
| 6358 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6359 | parent = eb->start; |
| 6360 | else |
| 6361 | BUG_ON(root->root_key.objectid != |
| 6362 | btrfs_header_owner(eb)); |
| 6363 | } else { |
| 6364 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 6365 | parent = path->nodes[level + 1]->start; |
| 6366 | else |
| 6367 | BUG_ON(root->root_key.objectid != |
| 6368 | btrfs_header_owner(path->nodes[level + 1])); |
| 6369 | } |
| 6370 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6371 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6372 | out: |
| 6373 | wc->refs[level] = 0; |
| 6374 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6375 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6376 | } |
| 6377 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6378 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 6379 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6380 | struct btrfs_path *path, |
| 6381 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6382 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6383 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6384 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6385 | int ret; |
| 6386 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6387 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6388 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6389 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6390 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6391 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6392 | if (level == 0) |
| 6393 | break; |
| 6394 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 6395 | if (path->slots[level] >= |
| 6396 | btrfs_header_nritems(path->nodes[level])) |
| 6397 | break; |
| 6398 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 6399 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6400 | if (ret > 0) { |
| 6401 | path->slots[level]++; |
| 6402 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 6403 | } else if (ret < 0) |
| 6404 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6405 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6406 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6407 | return 0; |
| 6408 | } |
| 6409 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6410 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6411 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6412 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6413 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6414 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6415 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6416 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6417 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6418 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 6419 | while (level < max_level && path->nodes[level]) { |
| 6420 | wc->level = level; |
| 6421 | if (path->slots[level] + 1 < |
| 6422 | btrfs_header_nritems(path->nodes[level])) { |
| 6423 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6424 | return 0; |
| 6425 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6426 | ret = walk_up_proc(trans, root, path, wc); |
| 6427 | if (ret > 0) |
| 6428 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 6429 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6430 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6431 | btrfs_tree_unlock_rw(path->nodes[level], |
| 6432 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6433 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6434 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6435 | free_extent_buffer(path->nodes[level]); |
| 6436 | path->nodes[level] = NULL; |
| 6437 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6438 | } |
| 6439 | } |
| 6440 | return 1; |
| 6441 | } |
| 6442 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6443 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6444 | * drop a subvolume tree. |
| 6445 | * |
| 6446 | * this function traverses the tree freeing any blocks that only |
| 6447 | * referenced by the tree. |
| 6448 | * |
| 6449 | * when a shared tree block is found. this function decreases its |
| 6450 | * reference count by one. if update_ref is true, this function |
| 6451 | * also make sure backrefs for the shared block and all lower level |
| 6452 | * blocks are properly updated. |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6453 | */ |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6454 | void btrfs_drop_snapshot(struct btrfs_root *root, |
| 6455 | struct btrfs_block_rsv *block_rsv, int update_ref) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6456 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6457 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6458 | struct btrfs_trans_handle *trans; |
| 6459 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6460 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6461 | struct walk_control *wc; |
| 6462 | struct btrfs_key key; |
| 6463 | int err = 0; |
| 6464 | int ret; |
| 6465 | int level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6466 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6467 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6468 | if (!path) { |
| 6469 | err = -ENOMEM; |
| 6470 | goto out; |
| 6471 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6472 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6473 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 6474 | if (!wc) { |
| 6475 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6476 | err = -ENOMEM; |
| 6477 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 6478 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6479 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6480 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6481 | BUG_ON(IS_ERR(trans)); |
| 6482 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6483 | if (block_rsv) |
| 6484 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6485 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6486 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6487 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6488 | path->nodes[level] = btrfs_lock_root_node(root); |
| 6489 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 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 | memset(&wc->update_progress, 0, |
| 6493 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6494 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6495 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6496 | memcpy(&wc->update_progress, &key, |
| 6497 | sizeof(wc->update_progress)); |
| 6498 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6499 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6500 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 6501 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6502 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 6503 | path->lowest_level = 0; |
| 6504 | if (ret < 0) { |
| 6505 | err = ret; |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6506 | goto out_free; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 6507 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6508 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6509 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6510 | /* |
| 6511 | * unlock our path, this is safe because only this |
| 6512 | * function is allowed to delete this snapshot |
| 6513 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6514 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 6515 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6516 | level = btrfs_header_level(root->node); |
| 6517 | while (1) { |
| 6518 | btrfs_tree_lock(path->nodes[level]); |
| 6519 | btrfs_set_lock_blocking(path->nodes[level]); |
| 6520 | |
| 6521 | ret = btrfs_lookup_extent_info(trans, root, |
| 6522 | path->nodes[level]->start, |
| 6523 | path->nodes[level]->len, |
| 6524 | &wc->refs[level], |
| 6525 | &wc->flags[level]); |
| 6526 | BUG_ON(ret); |
| 6527 | BUG_ON(wc->refs[level] == 0); |
| 6528 | |
| 6529 | if (level == root_item->drop_level) |
| 6530 | break; |
| 6531 | |
| 6532 | btrfs_tree_unlock(path->nodes[level]); |
| 6533 | WARN_ON(wc->refs[level] != 1); |
| 6534 | level--; |
| 6535 | } |
| 6536 | } |
| 6537 | |
| 6538 | wc->level = level; |
| 6539 | wc->shared_level = -1; |
| 6540 | wc->stage = DROP_REFERENCE; |
| 6541 | wc->update_ref = update_ref; |
| 6542 | wc->keep_locks = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6543 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6544 | |
| 6545 | while (1) { |
| 6546 | ret = walk_down_tree(trans, root, path, wc); |
| 6547 | if (ret < 0) { |
| 6548 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 6549 | break; |
| 6550 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6551 | |
| 6552 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 6553 | if (ret < 0) { |
| 6554 | err = ret; |
| 6555 | break; |
| 6556 | } |
| 6557 | |
| 6558 | if (ret > 0) { |
| 6559 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 6560 | break; |
| 6561 | } |
| 6562 | |
| 6563 | if (wc->stage == DROP_REFERENCE) { |
| 6564 | level = wc->level; |
| 6565 | btrfs_node_key(path->nodes[level], |
| 6566 | &root_item->drop_progress, |
| 6567 | path->slots[level]); |
| 6568 | root_item->drop_level = level; |
| 6569 | } |
| 6570 | |
| 6571 | BUG_ON(wc->level == 0); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6572 | if (btrfs_should_end_transaction(trans, tree_root)) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6573 | ret = btrfs_update_root(trans, tree_root, |
| 6574 | &root->root_key, |
| 6575 | root_item); |
| 6576 | BUG_ON(ret); |
| 6577 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6578 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 6579 | trans = btrfs_start_transaction(tree_root, 0); |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 6580 | BUG_ON(IS_ERR(trans)); |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6581 | if (block_rsv) |
| 6582 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6583 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6584 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6585 | btrfs_release_path(path); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6586 | BUG_ON(err); |
| 6587 | |
| 6588 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
| 6589 | BUG_ON(ret); |
| 6590 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6591 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
| 6592 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, |
| 6593 | NULL, NULL); |
| 6594 | BUG_ON(ret < 0); |
| 6595 | if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 6596 | /* if we fail to delete the orphan item this time |
| 6597 | * around, it'll get picked up the next time. |
| 6598 | * |
| 6599 | * The most common failure here is just -ENOENT. |
| 6600 | */ |
| 6601 | btrfs_del_orphan_item(trans, tree_root, |
| 6602 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 6603 | } |
| 6604 | } |
| 6605 | |
| 6606 | if (root->in_radix) { |
| 6607 | btrfs_free_fs_root(tree_root->fs_info, root); |
| 6608 | } else { |
| 6609 | free_extent_buffer(root->node); |
| 6610 | free_extent_buffer(root->commit_root); |
| 6611 | kfree(root); |
| 6612 | } |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6613 | out_free: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 6614 | btrfs_end_transaction_throttle(trans, tree_root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6615 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6616 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 6617 | out: |
| 6618 | if (err) |
| 6619 | btrfs_std_error(root->fs_info, err); |
| 6620 | return; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 6621 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 6622 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6623 | /* |
| 6624 | * drop subtree rooted at tree block 'node'. |
| 6625 | * |
| 6626 | * NOTE: this function will unlock and release tree block 'node' |
| 6627 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6628 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 6629 | struct btrfs_root *root, |
| 6630 | struct extent_buffer *node, |
| 6631 | struct extent_buffer *parent) |
| 6632 | { |
| 6633 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6634 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6635 | int level; |
| 6636 | int parent_level; |
| 6637 | int ret = 0; |
| 6638 | int wret; |
| 6639 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6640 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 6641 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6642 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6643 | if (!path) |
| 6644 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6645 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6646 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6647 | if (!wc) { |
| 6648 | btrfs_free_path(path); |
| 6649 | return -ENOMEM; |
| 6650 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6651 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6652 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6653 | parent_level = btrfs_header_level(parent); |
| 6654 | extent_buffer_get(parent); |
| 6655 | path->nodes[parent_level] = parent; |
| 6656 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 6657 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 6658 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6659 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6660 | path->nodes[level] = node; |
| 6661 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 6662 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6663 | |
| 6664 | wc->refs[parent_level] = 1; |
| 6665 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 6666 | wc->level = level; |
| 6667 | wc->shared_level = -1; |
| 6668 | wc->stage = DROP_REFERENCE; |
| 6669 | wc->update_ref = 0; |
| 6670 | wc->keep_locks = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 6671 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6672 | |
| 6673 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6674 | wret = walk_down_tree(trans, root, path, wc); |
| 6675 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6676 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6677 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6678 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6679 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6680 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6681 | if (wret < 0) |
| 6682 | ret = wret; |
| 6683 | if (wret != 0) |
| 6684 | break; |
| 6685 | } |
| 6686 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 6687 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6688 | btrfs_free_path(path); |
| 6689 | return ret; |
| 6690 | } |
| 6691 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6692 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 6693 | { |
| 6694 | u64 num_devices; |
| 6695 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | |
| 6696 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 6697 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 6698 | /* |
| 6699 | * we add in the count of missing devices because we want |
| 6700 | * to make sure that any RAID levels on a degraded FS |
| 6701 | * continue to be honored. |
| 6702 | */ |
| 6703 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 6704 | root->fs_info->fs_devices->missing_devices; |
| 6705 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6706 | if (num_devices == 1) { |
| 6707 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6708 | stripped = flags & ~stripped; |
| 6709 | |
| 6710 | /* turn raid0 into single device chunks */ |
| 6711 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 6712 | return stripped; |
| 6713 | |
| 6714 | /* turn mirroring into duplication */ |
| 6715 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6716 | BTRFS_BLOCK_GROUP_RAID10)) |
| 6717 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
| 6718 | return flags; |
| 6719 | } else { |
| 6720 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6721 | if (flags & stripped) |
| 6722 | return flags; |
| 6723 | |
| 6724 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 6725 | stripped = flags & ~stripped; |
| 6726 | |
| 6727 | /* switch duplicated blocks with raid1 */ |
| 6728 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 6729 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 6730 | |
| 6731 | /* turn single device chunks into raid0 */ |
| 6732 | return stripped | BTRFS_BLOCK_GROUP_RAID0; |
| 6733 | } |
| 6734 | return flags; |
| 6735 | } |
| 6736 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6737 | 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] | 6738 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6739 | struct btrfs_space_info *sinfo = cache->space_info; |
| 6740 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6741 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6742 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6743 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 6744 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6745 | /* |
| 6746 | * We need some metadata space and system metadata space for |
| 6747 | * allocating chunks in some corner cases until we force to set |
| 6748 | * it to be readonly. |
| 6749 | */ |
| 6750 | if ((sinfo->flags & |
| 6751 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 6752 | !force) |
| 6753 | min_allocable_bytes = 1 * 1024 * 1024; |
| 6754 | else |
| 6755 | min_allocable_bytes = 0; |
| 6756 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6757 | spin_lock(&sinfo->lock); |
| 6758 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 6759 | |
| 6760 | if (cache->ro) { |
| 6761 | ret = 0; |
| 6762 | goto out; |
| 6763 | } |
| 6764 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6765 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 6766 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 6767 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6768 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 6769 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
| 6770 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6771 | sinfo->bytes_readonly += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6772 | cache->ro = 1; |
| 6773 | ret = 0; |
| 6774 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 6775 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6776 | spin_unlock(&cache->lock); |
| 6777 | spin_unlock(&sinfo->lock); |
| 6778 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 6779 | } |
| 6780 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6781 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 6782 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6783 | |
| 6784 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6785 | struct btrfs_trans_handle *trans; |
| 6786 | u64 alloc_flags; |
| 6787 | int ret; |
| 6788 | |
| 6789 | BUG_ON(cache->ro); |
| 6790 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 6791 | trans = btrfs_join_transaction(root); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6792 | BUG_ON(IS_ERR(trans)); |
| 6793 | |
| 6794 | alloc_flags = update_block_group_flags(root, cache->flags); |
| 6795 | if (alloc_flags != cache->flags) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6796 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6797 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6798 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6799 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6800 | if (!ret) |
| 6801 | goto out; |
| 6802 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6803 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6804 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6805 | if (ret < 0) |
| 6806 | goto out; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 6807 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6808 | out: |
| 6809 | btrfs_end_transaction(trans, root); |
| 6810 | return ret; |
| 6811 | } |
| 6812 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 6813 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 6814 | struct btrfs_root *root, u64 type) |
| 6815 | { |
| 6816 | u64 alloc_flags = get_alloc_profile(root, type); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 6817 | return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
| 6818 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 6819 | } |
| 6820 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 6821 | /* |
| 6822 | * helper to account the unused space of all the readonly block group in the |
| 6823 | * list. takes mirrors into account. |
| 6824 | */ |
| 6825 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 6826 | { |
| 6827 | struct btrfs_block_group_cache *block_group; |
| 6828 | u64 free_bytes = 0; |
| 6829 | int factor; |
| 6830 | |
| 6831 | list_for_each_entry(block_group, groups_list, list) { |
| 6832 | spin_lock(&block_group->lock); |
| 6833 | |
| 6834 | if (!block_group->ro) { |
| 6835 | spin_unlock(&block_group->lock); |
| 6836 | continue; |
| 6837 | } |
| 6838 | |
| 6839 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 6840 | BTRFS_BLOCK_GROUP_RAID10 | |
| 6841 | BTRFS_BLOCK_GROUP_DUP)) |
| 6842 | factor = 2; |
| 6843 | else |
| 6844 | factor = 1; |
| 6845 | |
| 6846 | free_bytes += (block_group->key.offset - |
| 6847 | btrfs_block_group_used(&block_group->item)) * |
| 6848 | factor; |
| 6849 | |
| 6850 | spin_unlock(&block_group->lock); |
| 6851 | } |
| 6852 | |
| 6853 | return free_bytes; |
| 6854 | } |
| 6855 | |
| 6856 | /* |
| 6857 | * helper to account the unused space of all the readonly block group in the |
| 6858 | * space_info. takes mirrors into account. |
| 6859 | */ |
| 6860 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 6861 | { |
| 6862 | int i; |
| 6863 | u64 free_bytes = 0; |
| 6864 | |
| 6865 | spin_lock(&sinfo->lock); |
| 6866 | |
| 6867 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
| 6868 | if (!list_empty(&sinfo->block_groups[i])) |
| 6869 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 6870 | &sinfo->block_groups[i]); |
| 6871 | |
| 6872 | spin_unlock(&sinfo->lock); |
| 6873 | |
| 6874 | return free_bytes; |
| 6875 | } |
| 6876 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6877 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
| 6878 | struct btrfs_block_group_cache *cache) |
| 6879 | { |
| 6880 | struct btrfs_space_info *sinfo = cache->space_info; |
| 6881 | u64 num_bytes; |
| 6882 | |
| 6883 | BUG_ON(!cache->ro); |
| 6884 | |
| 6885 | spin_lock(&sinfo->lock); |
| 6886 | spin_lock(&cache->lock); |
| 6887 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 6888 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 6889 | sinfo->bytes_readonly -= num_bytes; |
| 6890 | cache->ro = 0; |
| 6891 | spin_unlock(&cache->lock); |
| 6892 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6893 | return 0; |
| 6894 | } |
| 6895 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6896 | /* |
| 6897 | * checks to see if its even possible to relocate this block group. |
| 6898 | * |
| 6899 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 6900 | * ok to go ahead and try. |
| 6901 | */ |
| 6902 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6903 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6904 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6905 | struct btrfs_space_info *space_info; |
| 6906 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 6907 | struct btrfs_device *device; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6908 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 6909 | u64 dev_min = 1; |
| 6910 | u64 dev_nr = 0; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6911 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6912 | int full = 0; |
| 6913 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6914 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6915 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6916 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6917 | /* odd, couldn't find the block group, leave it alone */ |
| 6918 | if (!block_group) |
| 6919 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 6920 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6921 | min_free = btrfs_block_group_used(&block_group->item); |
| 6922 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6923 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6924 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6925 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6926 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6927 | space_info = block_group->space_info; |
| 6928 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 6929 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6930 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6931 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6932 | /* |
| 6933 | * 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] | 6934 | * relocate it unless we're able to allocate a new chunk below. |
| 6935 | * |
| 6936 | * Otherwise, we need to make sure we have room in the space to handle |
| 6937 | * 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] | 6938 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 6939 | if ((space_info->total_bytes != block_group->key.offset) && |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6940 | (space_info->bytes_used + space_info->bytes_reserved + |
| 6941 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 6942 | min_free < space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6943 | spin_unlock(&space_info->lock); |
| 6944 | goto out; |
| 6945 | } |
| 6946 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 6947 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6948 | /* |
| 6949 | * ok we don't have enough space, but maybe we have free space on our |
| 6950 | * devices to allocate new chunks for relocation, so loop through our |
| 6951 | * alloc devices and guess if we have enough space. However, if we |
| 6952 | * were marked as full, then we know there aren't enough chunks, and we |
| 6953 | * can just return. |
| 6954 | */ |
| 6955 | ret = -1; |
| 6956 | if (full) |
| 6957 | goto out; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 6958 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6959 | /* |
| 6960 | * index: |
| 6961 | * 0: raid10 |
| 6962 | * 1: raid1 |
| 6963 | * 2: dup |
| 6964 | * 3: raid0 |
| 6965 | * 4: single |
| 6966 | */ |
| 6967 | index = get_block_group_index(block_group); |
| 6968 | if (index == 0) { |
| 6969 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 6970 | /* Divide by 2 */ |
| 6971 | min_free >>= 1; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6972 | } else if (index == 1) { |
| 6973 | dev_min = 2; |
| 6974 | } else if (index == 2) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 6975 | /* Multiply by 2 */ |
| 6976 | min_free <<= 1; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6977 | } else if (index == 3) { |
| 6978 | dev_min = fs_devices->rw_devices; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 6979 | do_div(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6980 | } |
| 6981 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6982 | mutex_lock(&root->fs_info->chunk_mutex); |
| 6983 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 6984 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 6985 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6986 | /* |
| 6987 | * check to make sure we can actually find a chunk with enough |
| 6988 | * space to fit our block group in. |
| 6989 | */ |
| 6990 | if (device->total_bytes > device->bytes_used + min_free) { |
| 6991 | ret = find_free_dev_extent(NULL, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 6992 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6993 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6994 | dev_nr++; |
| 6995 | |
| 6996 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 6997 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 6998 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6999 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 7000 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7001 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7002 | mutex_unlock(&root->fs_info->chunk_mutex); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7003 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7004 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 7005 | return ret; |
| 7006 | } |
| 7007 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 7008 | static int find_first_block_group(struct btrfs_root *root, |
| 7009 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7010 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7011 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7012 | struct btrfs_key found_key; |
| 7013 | struct extent_buffer *leaf; |
| 7014 | int slot; |
| 7015 | |
| 7016 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 7017 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7018 | goto out; |
| 7019 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7020 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7021 | slot = path->slots[0]; |
| 7022 | leaf = path->nodes[0]; |
| 7023 | if (slot >= btrfs_header_nritems(leaf)) { |
| 7024 | ret = btrfs_next_leaf(root, path); |
| 7025 | if (ret == 0) |
| 7026 | continue; |
| 7027 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7028 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7029 | break; |
| 7030 | } |
| 7031 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 7032 | |
| 7033 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7034 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 7035 | ret = 0; |
| 7036 | goto out; |
| 7037 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7038 | path->slots[0]++; |
| 7039 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7040 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7041 | return ret; |
| 7042 | } |
| 7043 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7044 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 7045 | { |
| 7046 | struct btrfs_block_group_cache *block_group; |
| 7047 | u64 last = 0; |
| 7048 | |
| 7049 | while (1) { |
| 7050 | struct inode *inode; |
| 7051 | |
| 7052 | block_group = btrfs_lookup_first_block_group(info, last); |
| 7053 | while (block_group) { |
| 7054 | spin_lock(&block_group->lock); |
| 7055 | if (block_group->iref) |
| 7056 | break; |
| 7057 | spin_unlock(&block_group->lock); |
| 7058 | block_group = next_block_group(info->tree_root, |
| 7059 | block_group); |
| 7060 | } |
| 7061 | if (!block_group) { |
| 7062 | if (last == 0) |
| 7063 | break; |
| 7064 | last = 0; |
| 7065 | continue; |
| 7066 | } |
| 7067 | |
| 7068 | inode = block_group->inode; |
| 7069 | block_group->iref = 0; |
| 7070 | block_group->inode = NULL; |
| 7071 | spin_unlock(&block_group->lock); |
| 7072 | iput(inode); |
| 7073 | last = block_group->key.objectid + block_group->key.offset; |
| 7074 | btrfs_put_block_group(block_group); |
| 7075 | } |
| 7076 | } |
| 7077 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7078 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 7079 | { |
| 7080 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7081 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7082 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7083 | struct rb_node *n; |
| 7084 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7085 | down_write(&info->extent_commit_sem); |
| 7086 | while (!list_empty(&info->caching_block_groups)) { |
| 7087 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 7088 | struct btrfs_caching_control, list); |
| 7089 | list_del(&caching_ctl->list); |
| 7090 | put_caching_control(caching_ctl); |
| 7091 | } |
| 7092 | up_write(&info->extent_commit_sem); |
| 7093 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7094 | spin_lock(&info->block_group_cache_lock); |
| 7095 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 7096 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 7097 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7098 | rb_erase(&block_group->cache_node, |
| 7099 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7100 | spin_unlock(&info->block_group_cache_lock); |
| 7101 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7102 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7103 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7104 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7105 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7106 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7107 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7108 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7109 | /* |
| 7110 | * We haven't cached this block group, which means we could |
| 7111 | * possibly have excluded extents on this block group. |
| 7112 | */ |
| 7113 | if (block_group->cached == BTRFS_CACHE_NO) |
| 7114 | free_excluded_extents(info->extent_root, block_group); |
| 7115 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7116 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 7117 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 7118 | |
| 7119 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7120 | } |
| 7121 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7122 | |
| 7123 | /* now that all the block groups are freed, go through and |
| 7124 | * free all the space_info structs. This is only called during |
| 7125 | * the final stages of unmount, and so we know nobody is |
| 7126 | * using them. We call synchronize_rcu() once before we start, |
| 7127 | * just to be on the safe side. |
| 7128 | */ |
| 7129 | synchronize_rcu(); |
| 7130 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 7131 | release_global_block_rsv(info); |
| 7132 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7133 | while(!list_empty(&info->space_info)) { |
| 7134 | space_info = list_entry(info->space_info.next, |
| 7135 | struct btrfs_space_info, |
| 7136 | list); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7137 | if (space_info->bytes_pinned > 0 || |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 7138 | space_info->bytes_reserved > 0 || |
| 7139 | space_info->bytes_may_use > 0) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7140 | WARN_ON(1); |
| 7141 | dump_space_info(space_info, 0, 0); |
| 7142 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 7143 | list_del(&space_info->list); |
| 7144 | kfree(space_info); |
| 7145 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7146 | return 0; |
| 7147 | } |
| 7148 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7149 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 7150 | struct btrfs_block_group_cache *cache) |
| 7151 | { |
| 7152 | int index = get_block_group_index(cache); |
| 7153 | |
| 7154 | down_write(&space_info->groups_sem); |
| 7155 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 7156 | up_write(&space_info->groups_sem); |
| 7157 | } |
| 7158 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7159 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 7160 | { |
| 7161 | struct btrfs_path *path; |
| 7162 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7163 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7164 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7165 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7166 | struct btrfs_key key; |
| 7167 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7168 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7169 | int need_clear = 0; |
| 7170 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7171 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 7172 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7173 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7174 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7175 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7176 | path = btrfs_alloc_path(); |
| 7177 | if (!path) |
| 7178 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 7179 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7180 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 7181 | cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 7182 | if (btrfs_test_opt(root, SPACE_CACHE) && |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 7183 | btrfs_super_generation(root->fs_info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7184 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 7185 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 7186 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7187 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7188 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7189 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7190 | if (ret > 0) |
| 7191 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7192 | if (ret != 0) |
| 7193 | goto error; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7194 | leaf = path->nodes[0]; |
| 7195 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7196 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7197 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7198 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7199 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7200 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7201 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7202 | GFP_NOFS); |
| 7203 | if (!cache->free_space_ctl) { |
| 7204 | kfree(cache); |
| 7205 | ret = -ENOMEM; |
| 7206 | goto error; |
| 7207 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 7208 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7209 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7210 | spin_lock_init(&cache->lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7211 | cache->fs_info = info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7212 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7213 | INIT_LIST_HEAD(&cache->cluster_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7214 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7215 | if (need_clear) |
| 7216 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 7217 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7218 | read_extent_buffer(leaf, &cache->item, |
| 7219 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 7220 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7221 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7222 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7223 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7224 | btrfs_release_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7225 | cache->flags = btrfs_block_group_flags(&cache->item); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7226 | cache->sectorsize = root->sectorsize; |
| 7227 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7228 | btrfs_init_free_space_ctl(cache); |
| 7229 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7230 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 7231 | * We need to exclude the super stripes now so that the space |
| 7232 | * info has super bytes accounted for, otherwise we'll think |
| 7233 | * we have more space than we actually do. |
| 7234 | */ |
| 7235 | exclude_super_stripes(root, cache); |
| 7236 | |
| 7237 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7238 | * check for two cases, either we are full, and therefore |
| 7239 | * don't need to bother with the caching work since we won't |
| 7240 | * find any space, or we are empty, and we can just add all |
| 7241 | * the space in and be done with it. This saves us _alot_ of |
| 7242 | * time, particularly in the full case. |
| 7243 | */ |
| 7244 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7245 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7246 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7247 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7248 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7249 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7250 | cache->cached = BTRFS_CACHE_FINISHED; |
| 7251 | add_new_free_space(cache, root->fs_info, |
| 7252 | found_key.objectid, |
| 7253 | found_key.objectid + |
| 7254 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7255 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7256 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 7257 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7258 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 7259 | btrfs_block_group_used(&cache->item), |
| 7260 | &space_info); |
| 7261 | BUG_ON(ret); |
| 7262 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7263 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7264 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7265 | spin_unlock(&cache->space_info->lock); |
| 7266 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7267 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7268 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7269 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7270 | BUG_ON(ret); |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 7271 | |
| 7272 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 7273 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7274 | set_block_group_ro(cache, 1); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7275 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7276 | |
| 7277 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 7278 | if (!(get_alloc_profile(root, space_info->flags) & |
| 7279 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 7280 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7281 | BTRFS_BLOCK_GROUP_DUP))) |
| 7282 | continue; |
| 7283 | /* |
| 7284 | * avoid allocating from un-mirrored block group if there are |
| 7285 | * mirrored block groups. |
| 7286 | */ |
| 7287 | list_for_each_entry(cache, &space_info->block_groups[3], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7288 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7289 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 7290 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7291 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7292 | |
| 7293 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7294 | ret = 0; |
| 7295 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7296 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 7297 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 7298 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7299 | |
| 7300 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 7301 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7302 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7303 | u64 size) |
| 7304 | { |
| 7305 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7306 | struct btrfs_root *extent_root; |
| 7307 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7308 | |
| 7309 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7310 | |
Chris Mason | 12fcfd2 | 2009-03-24 10:24:20 -0400 | [diff] [blame] | 7311 | root->fs_info->last_trans_log_full_commit = trans->transid; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7312 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 7313 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7314 | if (!cache) |
| 7315 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7316 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 7317 | GFP_NOFS); |
| 7318 | if (!cache->free_space_ctl) { |
| 7319 | kfree(cache); |
| 7320 | return -ENOMEM; |
| 7321 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7322 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 7323 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7324 | cache->key.offset = size; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7325 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7326 | cache->sectorsize = root->sectorsize; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7327 | cache->fs_info = root->fs_info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7328 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 7329 | atomic_set(&cache->count, 1); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7330 | spin_lock_init(&cache->lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7331 | INIT_LIST_HEAD(&cache->list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7332 | INIT_LIST_HEAD(&cache->cluster_list); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 7333 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 7334 | btrfs_init_free_space_ctl(cache); |
| 7335 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7336 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7337 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 7338 | cache->flags = type; |
| 7339 | btrfs_set_block_group_flags(&cache->item, type); |
| 7340 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7341 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7342 | cache->cached = BTRFS_CACHE_FINISHED; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7343 | exclude_super_stripes(root, cache); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7344 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7345 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 7346 | chunk_offset + size); |
| 7347 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7348 | free_excluded_extents(root, cache); |
| 7349 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7350 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 7351 | &cache->space_info); |
| 7352 | BUG_ON(ret); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7353 | |
| 7354 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7355 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 7356 | spin_unlock(&cache->space_info->lock); |
| 7357 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 7358 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7359 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 7360 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 7361 | BUG_ON(ret); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 7362 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7363 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 7364 | sizeof(cache->item)); |
| 7365 | BUG_ON(ret); |
| 7366 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 7367 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7368 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 7369 | return 0; |
| 7370 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7371 | |
| 7372 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 7373 | struct btrfs_root *root, u64 group_start) |
| 7374 | { |
| 7375 | struct btrfs_path *path; |
| 7376 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7377 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7378 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7379 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7380 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7381 | int ret; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7382 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7383 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7384 | root = root->fs_info->extent_root; |
| 7385 | |
| 7386 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 7387 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7388 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7389 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 7390 | /* |
| 7391 | * Free the reserved super bytes from this block group before |
| 7392 | * remove it. |
| 7393 | */ |
| 7394 | free_excluded_extents(root, block_group); |
| 7395 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7396 | memcpy(&key, &block_group->key, sizeof(key)); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7397 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 7398 | BTRFS_BLOCK_GROUP_RAID1 | |
| 7399 | BTRFS_BLOCK_GROUP_RAID10)) |
| 7400 | factor = 2; |
| 7401 | else |
| 7402 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7403 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7404 | /* make sure this block group isn't part of an allocation cluster */ |
| 7405 | cluster = &root->fs_info->data_alloc_cluster; |
| 7406 | spin_lock(&cluster->refill_lock); |
| 7407 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7408 | spin_unlock(&cluster->refill_lock); |
| 7409 | |
| 7410 | /* |
| 7411 | * make sure this block group isn't part of a metadata |
| 7412 | * allocation cluster |
| 7413 | */ |
| 7414 | cluster = &root->fs_info->meta_alloc_cluster; |
| 7415 | spin_lock(&cluster->refill_lock); |
| 7416 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 7417 | spin_unlock(&cluster->refill_lock); |
| 7418 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7419 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7420 | if (!path) { |
| 7421 | ret = -ENOMEM; |
| 7422 | goto out; |
| 7423 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7424 | |
Ilya Dryomov | 10b2f34 | 2011-10-02 13:56:53 +0300 | [diff] [blame] | 7425 | inode = lookup_free_space_inode(tree_root, block_group, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7426 | if (!IS_ERR(inode)) { |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 7427 | ret = btrfs_orphan_add(trans, inode); |
| 7428 | BUG_ON(ret); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7429 | clear_nlink(inode); |
| 7430 | /* One for the block groups ref */ |
| 7431 | spin_lock(&block_group->lock); |
| 7432 | if (block_group->iref) { |
| 7433 | block_group->iref = 0; |
| 7434 | block_group->inode = NULL; |
| 7435 | spin_unlock(&block_group->lock); |
| 7436 | iput(inode); |
| 7437 | } else { |
| 7438 | spin_unlock(&block_group->lock); |
| 7439 | } |
| 7440 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 7441 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7442 | } |
| 7443 | |
| 7444 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 7445 | key.offset = block_group->key.objectid; |
| 7446 | key.type = 0; |
| 7447 | |
| 7448 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 7449 | if (ret < 0) |
| 7450 | goto out; |
| 7451 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7452 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7453 | if (ret == 0) { |
| 7454 | ret = btrfs_del_item(trans, tree_root, path); |
| 7455 | if (ret) |
| 7456 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7457 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7458 | } |
| 7459 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7460 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7461 | rb_erase(&block_group->cache_node, |
| 7462 | &root->fs_info->block_group_cache_tree); |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 7463 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7464 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7465 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 7466 | /* |
| 7467 | * we must use list_del_init so people can check to see if they |
| 7468 | * are still on the list after taking the semaphore |
| 7469 | */ |
| 7470 | list_del_init(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7471 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7472 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7473 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7474 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 7475 | |
| 7476 | btrfs_remove_free_space_cache(block_group); |
| 7477 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7478 | spin_lock(&block_group->space_info->lock); |
| 7479 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 7480 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 7481 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7482 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7483 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 7484 | memcpy(&key, &block_group->key, sizeof(key)); |
| 7485 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 7486 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 7487 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 7488 | btrfs_put_block_group(block_group); |
| 7489 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 7490 | |
| 7491 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 7492 | if (ret > 0) |
| 7493 | ret = -EIO; |
| 7494 | if (ret < 0) |
| 7495 | goto out; |
| 7496 | |
| 7497 | ret = btrfs_del_item(trans, root, path); |
| 7498 | out: |
| 7499 | btrfs_free_path(path); |
| 7500 | return ret; |
| 7501 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7502 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7503 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 7504 | { |
| 7505 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7506 | struct btrfs_super_block *disk_super; |
| 7507 | u64 features; |
| 7508 | u64 flags; |
| 7509 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7510 | int ret; |
| 7511 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 7512 | disk_super = fs_info->super_copy; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7513 | if (!btrfs_super_root(disk_super)) |
| 7514 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7515 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7516 | features = btrfs_super_incompat_flags(disk_super); |
| 7517 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 7518 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7519 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7520 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 7521 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7522 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7523 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7524 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 7525 | if (mixed) { |
| 7526 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 7527 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7528 | } else { |
| 7529 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 7530 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7531 | if (ret) |
| 7532 | goto out; |
| 7533 | |
| 7534 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 7535 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 7536 | } |
| 7537 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 7538 | return ret; |
| 7539 | } |
| 7540 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7541 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 7542 | { |
| 7543 | return unpin_extent_range(root, start, end); |
| 7544 | } |
| 7545 | |
| 7546 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 7547 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7548 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 7549 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 7550 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 7551 | |
| 7552 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 7553 | { |
| 7554 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 7555 | struct btrfs_block_group_cache *cache = NULL; |
| 7556 | u64 group_trimmed; |
| 7557 | u64 start; |
| 7558 | u64 end; |
| 7559 | u64 trimmed = 0; |
| 7560 | int ret = 0; |
| 7561 | |
| 7562 | cache = btrfs_lookup_block_group(fs_info, range->start); |
| 7563 | |
| 7564 | while (cache) { |
| 7565 | if (cache->key.objectid >= (range->start + range->len)) { |
| 7566 | btrfs_put_block_group(cache); |
| 7567 | break; |
| 7568 | } |
| 7569 | |
| 7570 | start = max(range->start, cache->key.objectid); |
| 7571 | end = min(range->start + range->len, |
| 7572 | cache->key.objectid + cache->key.offset); |
| 7573 | |
| 7574 | if (end - start >= range->minlen) { |
| 7575 | if (!block_group_cache_done(cache)) { |
| 7576 | ret = cache_block_group(cache, NULL, root, 0); |
| 7577 | if (!ret) |
| 7578 | wait_block_group_cache_done(cache); |
| 7579 | } |
| 7580 | ret = btrfs_trim_block_group(cache, |
| 7581 | &group_trimmed, |
| 7582 | start, |
| 7583 | end, |
| 7584 | range->minlen); |
| 7585 | |
| 7586 | trimmed += group_trimmed; |
| 7587 | if (ret) { |
| 7588 | btrfs_put_block_group(cache); |
| 7589 | break; |
| 7590 | } |
| 7591 | } |
| 7592 | |
| 7593 | cache = next_block_group(fs_info->tree_root, cache); |
| 7594 | } |
| 7595 | |
| 7596 | range->len = trimmed; |
| 7597 | return ret; |
| 7598 | } |