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> |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 27 | #include <linux/percpu_counter.h> |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 28 | #include "hash.h" |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 29 | #include "tree-log.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 30 | #include "disk-io.h" |
| 31 | #include "print-tree.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 32 | #include "volumes.h" |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 33 | #include "raid56.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" |
Miao Xie | 3fed40c | 2012-09-13 04:51:36 -0600 | [diff] [blame] | 36 | #include "math.h" |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 37 | #include "sysfs.h" |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 38 | #include "qgroup.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 39 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 40 | #undef SCRAMBLE_DELAYED_REFS |
| 41 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 42 | /* |
| 43 | * control flags for do_chunk_alloc's force field |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 44 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
| 45 | * if we really need one. |
| 46 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 47 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
| 48 | * if we have very few chunks already allocated. This is |
| 49 | * used as part of the clustering code to help make sure |
| 50 | * we have a good pool of storage to cluster in, without |
| 51 | * filling the FS with empty chunks |
| 52 | * |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 53 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
| 54 | * |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 55 | */ |
| 56 | enum { |
| 57 | CHUNK_ALLOC_NO_FORCE = 0, |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 58 | CHUNK_ALLOC_LIMITED = 1, |
| 59 | CHUNK_ALLOC_FORCE = 2, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 60 | }; |
| 61 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 62 | /* |
| 63 | * Control how reservations are dealt with. |
| 64 | * |
| 65 | * RESERVE_FREE - freeing a reservation. |
| 66 | * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for |
| 67 | * ENOSPC accounting |
| 68 | * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update |
| 69 | * bytes_may_use as the ENOSPC accounting is done elsewhere |
| 70 | */ |
| 71 | enum { |
| 72 | RESERVE_FREE = 0, |
| 73 | RESERVE_ALLOC = 1, |
| 74 | RESERVE_ALLOC_NO_ACCOUNT = 2, |
| 75 | }; |
| 76 | |
Liu Bo | c53d613 | 2012-12-27 09:01:19 +0000 | [diff] [blame] | 77 | static int update_block_group(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 78 | u64 bytenr, u64 num_bytes, int alloc); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 79 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 80 | struct btrfs_root *root, |
| 81 | u64 bytenr, u64 num_bytes, u64 parent, |
| 82 | u64 root_objectid, u64 owner_objectid, |
| 83 | u64 owner_offset, int refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 84 | struct btrfs_delayed_extent_op *extra_op, |
| 85 | int no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 86 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 87 | struct extent_buffer *leaf, |
| 88 | struct btrfs_extent_item *ei); |
| 89 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 90 | struct btrfs_root *root, |
| 91 | u64 parent, u64 root_objectid, |
| 92 | u64 flags, u64 owner, u64 offset, |
| 93 | struct btrfs_key *ins, int ref_mod); |
| 94 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 95 | struct btrfs_root *root, |
| 96 | u64 parent, u64 root_objectid, |
| 97 | u64 flags, struct btrfs_disk_key *key, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 98 | int level, struct btrfs_key *ins, |
| 99 | int no_quota); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 100 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 101 | struct btrfs_root *extent_root, u64 flags, |
| 102 | int force); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 103 | static int find_next_key(struct btrfs_path *path, int level, |
| 104 | struct btrfs_key *key); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 105 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 106 | int dump_block_groups); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 107 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 108 | u64 num_bytes, int reserve); |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 109 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 110 | u64 num_bytes); |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 111 | int btrfs_pin_extent(struct btrfs_root *root, |
| 112 | u64 bytenr, u64 num_bytes, int reserved); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 113 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 114 | static noinline int |
| 115 | block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 116 | { |
| 117 | smp_mb(); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 118 | return cache->cached == BTRFS_CACHE_FINISHED || |
| 119 | cache->cached == BTRFS_CACHE_ERROR; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 122 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 123 | { |
| 124 | return (cache->flags & bits) == bits; |
| 125 | } |
| 126 | |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 127 | static void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 128 | { |
| 129 | atomic_inc(&cache->count); |
| 130 | } |
| 131 | |
| 132 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) |
| 133 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 134 | if (atomic_dec_and_test(&cache->count)) { |
| 135 | WARN_ON(cache->pinned > 0); |
| 136 | WARN_ON(cache->reserved > 0); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 137 | kfree(cache->free_space_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 138 | kfree(cache); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 139 | } |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 142 | /* |
| 143 | * this adds the block group to the fs_info rb tree for the block group |
| 144 | * cache |
| 145 | */ |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 146 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 147 | struct btrfs_block_group_cache *block_group) |
| 148 | { |
| 149 | struct rb_node **p; |
| 150 | struct rb_node *parent = NULL; |
| 151 | struct btrfs_block_group_cache *cache; |
| 152 | |
| 153 | spin_lock(&info->block_group_cache_lock); |
| 154 | p = &info->block_group_cache_tree.rb_node; |
| 155 | |
| 156 | while (*p) { |
| 157 | parent = *p; |
| 158 | cache = rb_entry(parent, struct btrfs_block_group_cache, |
| 159 | cache_node); |
| 160 | if (block_group->key.objectid < cache->key.objectid) { |
| 161 | p = &(*p)->rb_left; |
| 162 | } else if (block_group->key.objectid > cache->key.objectid) { |
| 163 | p = &(*p)->rb_right; |
| 164 | } else { |
| 165 | spin_unlock(&info->block_group_cache_lock); |
| 166 | return -EEXIST; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | rb_link_node(&block_group->cache_node, parent, p); |
| 171 | rb_insert_color(&block_group->cache_node, |
| 172 | &info->block_group_cache_tree); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 173 | |
| 174 | if (info->first_logical_byte > block_group->key.objectid) |
| 175 | info->first_logical_byte = block_group->key.objectid; |
| 176 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 177 | spin_unlock(&info->block_group_cache_lock); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * This will return the block group at or after bytenr if contains is 0, else |
| 184 | * it will return the block group that contains the bytenr |
| 185 | */ |
| 186 | static struct btrfs_block_group_cache * |
| 187 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, |
| 188 | int contains) |
| 189 | { |
| 190 | struct btrfs_block_group_cache *cache, *ret = NULL; |
| 191 | struct rb_node *n; |
| 192 | u64 end, start; |
| 193 | |
| 194 | spin_lock(&info->block_group_cache_lock); |
| 195 | n = info->block_group_cache_tree.rb_node; |
| 196 | |
| 197 | while (n) { |
| 198 | cache = rb_entry(n, struct btrfs_block_group_cache, |
| 199 | cache_node); |
| 200 | end = cache->key.objectid + cache->key.offset - 1; |
| 201 | start = cache->key.objectid; |
| 202 | |
| 203 | if (bytenr < start) { |
| 204 | if (!contains && (!ret || start < ret->key.objectid)) |
| 205 | ret = cache; |
| 206 | n = n->rb_left; |
| 207 | } else if (bytenr > start) { |
| 208 | if (contains && bytenr <= end) { |
| 209 | ret = cache; |
| 210 | break; |
| 211 | } |
| 212 | n = n->rb_right; |
| 213 | } else { |
| 214 | ret = cache; |
| 215 | break; |
| 216 | } |
| 217 | } |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 218 | if (ret) { |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 219 | btrfs_get_block_group(ret); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 220 | if (bytenr == 0 && info->first_logical_byte > ret->key.objectid) |
| 221 | info->first_logical_byte = ret->key.objectid; |
| 222 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 223 | spin_unlock(&info->block_group_cache_lock); |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 228 | static int add_excluded_extent(struct btrfs_root *root, |
| 229 | u64 start, u64 num_bytes) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 230 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 231 | u64 end = start + num_bytes - 1; |
| 232 | set_extent_bits(&root->fs_info->freed_extents[0], |
| 233 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 234 | set_extent_bits(&root->fs_info->freed_extents[1], |
| 235 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 236 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 239 | static void free_excluded_extents(struct btrfs_root *root, |
| 240 | struct btrfs_block_group_cache *cache) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 241 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 242 | u64 start, end; |
| 243 | |
| 244 | start = cache->key.objectid; |
| 245 | end = start + cache->key.offset - 1; |
| 246 | |
| 247 | clear_extent_bits(&root->fs_info->freed_extents[0], |
| 248 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 249 | clear_extent_bits(&root->fs_info->freed_extents[1], |
| 250 | start, end, EXTENT_UPTODATE, GFP_NOFS); |
| 251 | } |
| 252 | |
| 253 | static int exclude_super_stripes(struct btrfs_root *root, |
| 254 | struct btrfs_block_group_cache *cache) |
| 255 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 256 | u64 bytenr; |
| 257 | u64 *logical; |
| 258 | int stripe_len; |
| 259 | int i, nr, ret; |
| 260 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 261 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
| 262 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; |
| 263 | cache->bytes_super += stripe_len; |
| 264 | ret = add_excluded_extent(root, cache->key.objectid, |
| 265 | stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 266 | if (ret) |
| 267 | return ret; |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 270 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
| 271 | bytenr = btrfs_sb_offset(i); |
| 272 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, |
| 273 | cache->key.objectid, bytenr, |
| 274 | 0, &logical, &nr, &stripe_len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 275 | if (ret) |
| 276 | return ret; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 277 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 278 | while (nr--) { |
Josef Bacik | 51bf5f0 | 2013-04-23 12:55:21 -0400 | [diff] [blame] | 279 | u64 start, len; |
| 280 | |
| 281 | if (logical[nr] > cache->key.objectid + |
| 282 | cache->key.offset) |
| 283 | continue; |
| 284 | |
| 285 | if (logical[nr] + stripe_len <= cache->key.objectid) |
| 286 | continue; |
| 287 | |
| 288 | start = logical[nr]; |
| 289 | if (start < cache->key.objectid) { |
| 290 | start = cache->key.objectid; |
| 291 | len = (logical[nr] + stripe_len) - start; |
| 292 | } else { |
| 293 | len = min_t(u64, stripe_len, |
| 294 | cache->key.objectid + |
| 295 | cache->key.offset - start); |
| 296 | } |
| 297 | |
| 298 | cache->bytes_super += len; |
| 299 | ret = add_excluded_extent(root, start, len); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 300 | if (ret) { |
| 301 | kfree(logical); |
| 302 | return ret; |
| 303 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 304 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 305 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 306 | kfree(logical); |
| 307 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 311 | static struct btrfs_caching_control * |
| 312 | get_caching_control(struct btrfs_block_group_cache *cache) |
| 313 | { |
| 314 | struct btrfs_caching_control *ctl; |
| 315 | |
| 316 | spin_lock(&cache->lock); |
| 317 | if (cache->cached != BTRFS_CACHE_STARTED) { |
| 318 | spin_unlock(&cache->lock); |
| 319 | return NULL; |
| 320 | } |
| 321 | |
Josef Bacik | dde5abe | 2010-09-16 16:17:03 -0400 | [diff] [blame] | 322 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
| 323 | if (!cache->caching_ctl) { |
| 324 | spin_unlock(&cache->lock); |
| 325 | return NULL; |
| 326 | } |
| 327 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 328 | ctl = cache->caching_ctl; |
| 329 | atomic_inc(&ctl->count); |
| 330 | spin_unlock(&cache->lock); |
| 331 | return ctl; |
| 332 | } |
| 333 | |
| 334 | static void put_caching_control(struct btrfs_caching_control *ctl) |
| 335 | { |
| 336 | if (atomic_dec_and_test(&ctl->count)) |
| 337 | kfree(ctl); |
| 338 | } |
| 339 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 340 | /* |
| 341 | * this is only called by cache_block_group, since we could have freed extents |
| 342 | * we need to check the pinned_extents for any extents that can't be used yet |
| 343 | * since their free space will be released as soon as the transaction commits. |
| 344 | */ |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 345 | 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] | 346 | struct btrfs_fs_info *info, u64 start, u64 end) |
| 347 | { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 348 | u64 extent_start, extent_end, size, total_added = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 349 | int ret; |
| 350 | |
| 351 | while (start < end) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 352 | ret = find_first_extent_bit(info->pinned_extents, start, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 353 | &extent_start, &extent_end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 354 | EXTENT_DIRTY | EXTENT_UPTODATE, |
| 355 | NULL); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 356 | if (ret) |
| 357 | break; |
| 358 | |
Yan, Zheng | 06b2331 | 2009-11-26 09:31:11 +0000 | [diff] [blame] | 359 | if (extent_start <= start) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 360 | start = extent_end + 1; |
| 361 | } else if (extent_start > start && extent_start < end) { |
| 362 | size = extent_start - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 363 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 364 | ret = btrfs_add_free_space(block_group, start, |
| 365 | size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 366 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 367 | start = extent_end + 1; |
| 368 | } else { |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (start < end) { |
| 374 | size = end - start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 375 | total_added += size; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 376 | ret = btrfs_add_free_space(block_group, start, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 377 | BUG_ON(ret); /* -ENOMEM or logic error */ |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 378 | } |
| 379 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 380 | return total_added; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 381 | } |
| 382 | |
Qu Wenruo | d458b05 | 2014-02-28 10:46:19 +0800 | [diff] [blame] | 383 | static noinline void caching_thread(struct btrfs_work *work) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 384 | { |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 385 | struct btrfs_block_group_cache *block_group; |
| 386 | struct btrfs_fs_info *fs_info; |
| 387 | struct btrfs_caching_control *caching_ctl; |
| 388 | struct btrfs_root *extent_root; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 389 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 390 | struct extent_buffer *leaf; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 391 | struct btrfs_key key; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 392 | u64 total_found = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 393 | u64 last = 0; |
| 394 | u32 nritems; |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 395 | int ret = -ENOMEM; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 396 | |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 397 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
| 398 | block_group = caching_ctl->block_group; |
| 399 | fs_info = block_group->fs_info; |
| 400 | extent_root = fs_info->extent_root; |
| 401 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 402 | path = btrfs_alloc_path(); |
| 403 | if (!path) |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 404 | goto out; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 405 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 406 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 407 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 408 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 409 | * We don't want to deadlock with somebody trying to allocate a new |
| 410 | * extent for the extent root while also trying to search the extent |
| 411 | * root to add free space. So we skip locking and search the commit |
| 412 | * root, since its read-only |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 413 | */ |
| 414 | path->skip_locking = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 415 | path->search_commit_root = 1; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 416 | path->reada = 1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 417 | |
Yan Zheng | e4404d6 | 2008-12-12 10:03:26 -0500 | [diff] [blame] | 418 | key.objectid = last; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 419 | key.offset = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 420 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 421 | again: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 422 | mutex_lock(&caching_ctl->mutex); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 423 | /* need to make sure the commit_root doesn't disappear */ |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 424 | down_read(&fs_info->commit_root_sem); |
Chris Mason | 013f1b1 | 2009-07-31 14:57:55 -0400 | [diff] [blame] | 425 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 426 | next: |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 427 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 428 | if (ret < 0) |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 429 | goto err; |
Yan Zheng | a512bbf | 2008-12-08 16:46:26 -0500 | [diff] [blame] | 430 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 431 | leaf = path->nodes[0]; |
| 432 | nritems = btrfs_header_nritems(leaf); |
| 433 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 434 | while (1) { |
David Sterba | 7841cb2 | 2011-05-31 18:07:27 +0200 | [diff] [blame] | 435 | if (btrfs_fs_closing(fs_info) > 1) { |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 436 | last = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 437 | break; |
Yan Zheng | f25784b | 2009-07-28 08:41:57 -0400 | [diff] [blame] | 438 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 439 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 440 | if (path->slots[0] < nritems) { |
| 441 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 442 | } else { |
| 443 | ret = find_next_key(path, 0, &key); |
| 444 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 445 | break; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 446 | |
Josef Bacik | c9ea7b2 | 2013-09-19 10:02:11 -0400 | [diff] [blame] | 447 | if (need_resched() || |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 448 | rwsem_is_contended(&fs_info->commit_root_sem)) { |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 449 | caching_ctl->progress = last; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 450 | btrfs_release_path(path); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 451 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 452 | mutex_unlock(&caching_ctl->mutex); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 453 | cond_resched(); |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 454 | goto again; |
| 455 | } |
Josef Bacik | 0a3896d | 2013-04-19 14:37:26 -0400 | [diff] [blame] | 456 | |
| 457 | ret = btrfs_next_leaf(extent_root, path); |
| 458 | if (ret < 0) |
| 459 | goto err; |
| 460 | if (ret) |
| 461 | break; |
Josef Bacik | 589d8ad | 2011-05-11 17:30:53 -0400 | [diff] [blame] | 462 | leaf = path->nodes[0]; |
| 463 | nritems = btrfs_header_nritems(leaf); |
| 464 | continue; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 465 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 466 | |
Liu Bo | 52ee28d | 2013-07-11 17:51:15 +0800 | [diff] [blame] | 467 | if (key.objectid < last) { |
| 468 | key.objectid = last; |
| 469 | key.offset = 0; |
| 470 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 471 | |
| 472 | caching_ctl->progress = last; |
| 473 | btrfs_release_path(path); |
| 474 | goto next; |
| 475 | } |
| 476 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 477 | if (key.objectid < block_group->key.objectid) { |
| 478 | path->slots[0]++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 479 | continue; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 480 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 481 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 482 | if (key.objectid >= block_group->key.objectid + |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 483 | block_group->key.offset) |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 484 | break; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 485 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 486 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
| 487 | key.type == BTRFS_METADATA_ITEM_KEY) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 488 | total_found += add_new_free_space(block_group, |
| 489 | fs_info, last, |
| 490 | key.objectid); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 491 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
| 492 | last = key.objectid + |
| 493 | fs_info->tree_root->leafsize; |
| 494 | else |
| 495 | last = key.objectid + key.offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 496 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 497 | if (total_found > (1024 * 1024 * 2)) { |
| 498 | total_found = 0; |
| 499 | wake_up(&caching_ctl->wait); |
| 500 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 501 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 502 | path->slots[0]++; |
| 503 | } |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 504 | ret = 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 505 | |
| 506 | total_found += add_new_free_space(block_group, fs_info, last, |
| 507 | block_group->key.objectid + |
| 508 | block_group->key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 509 | caching_ctl->progress = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 510 | |
| 511 | spin_lock(&block_group->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 512 | block_group->caching_ctl = NULL; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 513 | block_group->cached = BTRFS_CACHE_FINISHED; |
| 514 | spin_unlock(&block_group->lock); |
| 515 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 516 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 517 | btrfs_free_path(path); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 518 | up_read(&fs_info->commit_root_sem); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 519 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 520 | free_excluded_extents(extent_root, block_group); |
| 521 | |
| 522 | mutex_unlock(&caching_ctl->mutex); |
Josef Bacik | bab39bf | 2011-06-30 14:42:28 -0400 | [diff] [blame] | 523 | out: |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 524 | if (ret) { |
| 525 | spin_lock(&block_group->lock); |
| 526 | block_group->caching_ctl = NULL; |
| 527 | block_group->cached = BTRFS_CACHE_ERROR; |
| 528 | spin_unlock(&block_group->lock); |
| 529 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 530 | wake_up(&caching_ctl->wait); |
| 531 | |
| 532 | put_caching_control(caching_ctl); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 533 | btrfs_put_block_group(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 534 | } |
| 535 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 536 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 537 | int load_cache_only) |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 538 | { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 539 | DEFINE_WAIT(wait); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 540 | struct btrfs_fs_info *fs_info = cache->fs_info; |
| 541 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 542 | int ret = 0; |
| 543 | |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 544 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 545 | if (!caching_ctl) |
| 546 | return -ENOMEM; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 547 | |
| 548 | INIT_LIST_HEAD(&caching_ctl->list); |
| 549 | mutex_init(&caching_ctl->mutex); |
| 550 | init_waitqueue_head(&caching_ctl->wait); |
| 551 | caching_ctl->block_group = cache; |
| 552 | caching_ctl->progress = cache->key.objectid; |
| 553 | atomic_set(&caching_ctl->count, 1); |
Qu Wenruo | e66f0bb | 2014-02-28 10:46:12 +0800 | [diff] [blame] | 554 | btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 555 | |
| 556 | spin_lock(&cache->lock); |
| 557 | /* |
| 558 | * This should be a rare occasion, but this could happen I think in the |
| 559 | * case where one thread starts to load the space cache info, and then |
| 560 | * some other thread starts a transaction commit which tries to do an |
| 561 | * allocation while the other thread is still loading the space cache |
| 562 | * info. The previous loop should have kept us from choosing this block |
| 563 | * group, but if we've moved to the state where we will wait on caching |
| 564 | * block groups we need to first check if we're doing a fast load here, |
| 565 | * so we can wait for it to finish, otherwise we could end up allocating |
| 566 | * from a block group who's cache gets evicted for one reason or |
| 567 | * another. |
| 568 | */ |
| 569 | while (cache->cached == BTRFS_CACHE_FAST) { |
| 570 | struct btrfs_caching_control *ctl; |
| 571 | |
| 572 | ctl = cache->caching_ctl; |
| 573 | atomic_inc(&ctl->count); |
| 574 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); |
| 575 | spin_unlock(&cache->lock); |
| 576 | |
| 577 | schedule(); |
| 578 | |
| 579 | finish_wait(&ctl->wait, &wait); |
| 580 | put_caching_control(ctl); |
| 581 | spin_lock(&cache->lock); |
| 582 | } |
| 583 | |
| 584 | if (cache->cached != BTRFS_CACHE_NO) { |
| 585 | spin_unlock(&cache->lock); |
| 586 | kfree(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 587 | return 0; |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 588 | } |
| 589 | WARN_ON(cache->caching_ctl); |
| 590 | cache->caching_ctl = caching_ctl; |
| 591 | cache->cached = BTRFS_CACHE_FAST; |
| 592 | spin_unlock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 593 | |
Josef Bacik | d53ba47 | 2012-04-12 16:03:57 -0400 | [diff] [blame] | 594 | if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 595 | ret = load_free_space_cache(fs_info, cache); |
| 596 | |
| 597 | spin_lock(&cache->lock); |
| 598 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 599 | cache->caching_ctl = NULL; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 600 | cache->cached = BTRFS_CACHE_FINISHED; |
| 601 | cache->last_byte_to_unpin = (u64)-1; |
| 602 | } else { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 603 | if (load_cache_only) { |
| 604 | cache->caching_ctl = NULL; |
| 605 | cache->cached = BTRFS_CACHE_NO; |
| 606 | } else { |
| 607 | cache->cached = BTRFS_CACHE_STARTED; |
| 608 | } |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 609 | } |
| 610 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 611 | wake_up(&caching_ctl->wait); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 612 | if (ret == 1) { |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 613 | put_caching_control(caching_ctl); |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 614 | free_excluded_extents(fs_info->extent_root, cache); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 615 | return 0; |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 616 | } |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 617 | } else { |
| 618 | /* |
| 619 | * We are not going to do the fast caching, set cached to the |
| 620 | * appropriate value and wakeup any waiters. |
| 621 | */ |
| 622 | spin_lock(&cache->lock); |
| 623 | if (load_cache_only) { |
| 624 | cache->caching_ctl = NULL; |
| 625 | cache->cached = BTRFS_CACHE_NO; |
| 626 | } else { |
| 627 | cache->cached = BTRFS_CACHE_STARTED; |
| 628 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 629 | spin_unlock(&cache->lock); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 630 | wake_up(&caching_ctl->wait); |
| 631 | } |
| 632 | |
| 633 | if (load_cache_only) { |
| 634 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 635 | return 0; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 636 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 637 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 638 | down_write(&fs_info->commit_root_sem); |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 639 | atomic_inc(&caching_ctl->count); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 640 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 641 | up_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 642 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 643 | btrfs_get_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 644 | |
Qu Wenruo | e66f0bb | 2014-02-28 10:46:12 +0800 | [diff] [blame] | 645 | btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 646 | |
Josef Bacik | ef8bbdf | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 647 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 648 | } |
| 649 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 650 | /* |
| 651 | * return the block group that starts at or after bytenr |
| 652 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 653 | static struct btrfs_block_group_cache * |
| 654 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 655 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 656 | struct btrfs_block_group_cache *cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 657 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 658 | cache = block_group_cache_tree_search(info, bytenr, 0); |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 659 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 660 | return cache; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 661 | } |
| 662 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 663 | /* |
Sankar P | 9f55684 | 2009-05-14 13:52:22 -0400 | [diff] [blame] | 664 | * return the block group that contains the given bytenr |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 665 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 666 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
| 667 | struct btrfs_fs_info *info, |
| 668 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 669 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 670 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 671 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 672 | cache = block_group_cache_tree_search(info, bytenr, 1); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 673 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 674 | return cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 675 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 676 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 677 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 678 | u64 flags) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 679 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 680 | struct list_head *head = &info->space_info; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 681 | struct btrfs_space_info *found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 682 | |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 683 | flags &= BTRFS_BLOCK_GROUP_TYPE_MASK; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 684 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 685 | rcu_read_lock(); |
| 686 | list_for_each_entry_rcu(found, head, list) { |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 687 | if (found->flags & flags) { |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 688 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 689 | return found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 690 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 691 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 692 | rcu_read_unlock(); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 693 | return NULL; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 694 | } |
| 695 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 696 | /* |
| 697 | * after adding space to the filesystem, we need to clear the full flags |
| 698 | * on all the space infos. |
| 699 | */ |
| 700 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) |
| 701 | { |
| 702 | struct list_head *head = &info->space_info; |
| 703 | struct btrfs_space_info *found; |
| 704 | |
| 705 | rcu_read_lock(); |
| 706 | list_for_each_entry_rcu(found, head, list) |
| 707 | found->full = 0; |
| 708 | rcu_read_unlock(); |
| 709 | } |
| 710 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 711 | /* simple helper to search for an existing extent at a given offset */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 712 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 713 | { |
| 714 | int ret; |
| 715 | struct btrfs_key key; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 716 | struct btrfs_path *path; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 717 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 718 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 719 | if (!path) |
| 720 | return -ENOMEM; |
| 721 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 722 | key.objectid = start; |
| 723 | key.offset = len; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 724 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 725 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, |
| 726 | 0, 0); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 727 | if (ret > 0) { |
| 728 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 729 | if (key.objectid == start && |
| 730 | key.type == BTRFS_METADATA_ITEM_KEY) |
| 731 | ret = 0; |
| 732 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 733 | btrfs_free_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 734 | return ret; |
| 735 | } |
| 736 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 737 | /* |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 738 | * helper function to lookup reference count and flags of a tree block. |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 739 | * |
| 740 | * the head node for delayed ref is used to store the sum of all the |
| 741 | * reference count modifications queued up in the rbtree. the head |
| 742 | * node may also store the extent flags to set. This way you can check |
| 743 | * to see what the reference count and extent flags would be if all of |
| 744 | * the delayed refs are not processed. |
| 745 | */ |
| 746 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, |
| 747 | struct btrfs_root *root, u64 bytenr, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 748 | u64 offset, int metadata, u64 *refs, u64 *flags) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 749 | { |
| 750 | struct btrfs_delayed_ref_head *head; |
| 751 | struct btrfs_delayed_ref_root *delayed_refs; |
| 752 | struct btrfs_path *path; |
| 753 | struct btrfs_extent_item *ei; |
| 754 | struct extent_buffer *leaf; |
| 755 | struct btrfs_key key; |
| 756 | u32 item_size; |
| 757 | u64 num_refs; |
| 758 | u64 extent_flags; |
| 759 | int ret; |
| 760 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 761 | /* |
| 762 | * If we don't have skinny metadata, don't bother doing anything |
| 763 | * different |
| 764 | */ |
| 765 | if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) { |
| 766 | offset = root->leafsize; |
| 767 | metadata = 0; |
| 768 | } |
| 769 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 770 | path = btrfs_alloc_path(); |
| 771 | if (!path) |
| 772 | return -ENOMEM; |
| 773 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 774 | if (!trans) { |
| 775 | path->skip_locking = 1; |
| 776 | path->search_commit_root = 1; |
| 777 | } |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 778 | |
| 779 | search_again: |
| 780 | key.objectid = bytenr; |
| 781 | key.offset = offset; |
| 782 | if (metadata) |
| 783 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 784 | else |
| 785 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 786 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 787 | again: |
| 788 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, |
| 789 | &key, path, 0, 0); |
| 790 | if (ret < 0) |
| 791 | goto out_free; |
| 792 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 793 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
Filipe David Borba Manana | 74be951 | 2013-07-05 23:12:06 +0100 | [diff] [blame] | 794 | if (path->slots[0]) { |
| 795 | path->slots[0]--; |
| 796 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 797 | path->slots[0]); |
| 798 | if (key.objectid == bytenr && |
| 799 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 800 | key.offset == root->leafsize) |
| 801 | ret = 0; |
| 802 | } |
| 803 | if (ret) { |
| 804 | key.objectid = bytenr; |
| 805 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 806 | key.offset = root->leafsize; |
| 807 | btrfs_release_path(path); |
| 808 | goto again; |
| 809 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 810 | } |
| 811 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 812 | if (ret == 0) { |
| 813 | leaf = path->nodes[0]; |
| 814 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 815 | if (item_size >= sizeof(*ei)) { |
| 816 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 817 | struct btrfs_extent_item); |
| 818 | num_refs = btrfs_extent_refs(leaf, ei); |
| 819 | extent_flags = btrfs_extent_flags(leaf, ei); |
| 820 | } else { |
| 821 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 822 | struct btrfs_extent_item_v0 *ei0; |
| 823 | BUG_ON(item_size != sizeof(*ei0)); |
| 824 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 825 | struct btrfs_extent_item_v0); |
| 826 | num_refs = btrfs_extent_refs_v0(leaf, ei0); |
| 827 | /* FIXME: this isn't correct for data */ |
| 828 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 829 | #else |
| 830 | BUG(); |
| 831 | #endif |
| 832 | } |
| 833 | BUG_ON(num_refs == 0); |
| 834 | } else { |
| 835 | num_refs = 0; |
| 836 | extent_flags = 0; |
| 837 | ret = 0; |
| 838 | } |
| 839 | |
| 840 | if (!trans) |
| 841 | goto out; |
| 842 | |
| 843 | delayed_refs = &trans->transaction->delayed_refs; |
| 844 | spin_lock(&delayed_refs->lock); |
| 845 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 846 | if (head) { |
| 847 | if (!mutex_trylock(&head->mutex)) { |
| 848 | atomic_inc(&head->node.refs); |
| 849 | spin_unlock(&delayed_refs->lock); |
| 850 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 851 | btrfs_release_path(path); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 852 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 853 | /* |
| 854 | * Mutex was contended, block until it's released and try |
| 855 | * again |
| 856 | */ |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 857 | mutex_lock(&head->mutex); |
| 858 | mutex_unlock(&head->mutex); |
| 859 | btrfs_put_delayed_ref(&head->node); |
Filipe David Borba Manana | 639eefc | 2013-12-08 00:26:29 +0000 | [diff] [blame] | 860 | goto search_again; |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 861 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 862 | spin_lock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 863 | if (head->extent_op && head->extent_op->update_flags) |
| 864 | extent_flags |= head->extent_op->flags_to_set; |
| 865 | else |
| 866 | BUG_ON(num_refs == 0); |
| 867 | |
| 868 | num_refs += head->node.ref_mod; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 869 | spin_unlock(&head->lock); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 870 | mutex_unlock(&head->mutex); |
| 871 | } |
| 872 | spin_unlock(&delayed_refs->lock); |
| 873 | out: |
| 874 | WARN_ON(num_refs == 0); |
| 875 | if (refs) |
| 876 | *refs = num_refs; |
| 877 | if (flags) |
| 878 | *flags = extent_flags; |
| 879 | out_free: |
| 880 | btrfs_free_path(path); |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | /* |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 885 | * Back reference rules. Back refs have three main goals: |
| 886 | * |
| 887 | * 1) differentiate between all holders of references to an extent so that |
| 888 | * when a reference is dropped we can make sure it was a valid reference |
| 889 | * before freeing the extent. |
| 890 | * |
| 891 | * 2) Provide enough information to quickly find the holders of an extent |
| 892 | * if we notice a given block is corrupted or bad. |
| 893 | * |
| 894 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 895 | * maintenance. This is actually the same as #2, but with a slightly |
| 896 | * different use case. |
| 897 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 898 | * There are two kinds of back refs. The implicit back refs is optimized |
| 899 | * for pointers in non-shared tree blocks. For a given pointer in a block, |
| 900 | * back refs of this kind provide information about the block's owner tree |
| 901 | * and the pointer's key. These information allow us to find the block by |
| 902 | * b-tree searching. The full back refs is for pointers in tree blocks not |
| 903 | * referenced by their owner trees. The location of tree block is recorded |
| 904 | * in the back refs. Actually the full back refs is generic, and can be |
| 905 | * used in all cases the implicit back refs is used. The major shortcoming |
| 906 | * of the full back refs is its overhead. Every time a tree block gets |
| 907 | * COWed, we have to update back refs entry for all pointers in it. |
| 908 | * |
| 909 | * For a newly allocated tree block, we use implicit back refs for |
| 910 | * pointers in it. This means most tree related operations only involve |
| 911 | * implicit back refs. For a tree block created in old transaction, the |
| 912 | * only way to drop a reference to it is COW it. So we can detect the |
| 913 | * event that tree block loses its owner tree's reference and do the |
| 914 | * back refs conversion. |
| 915 | * |
| 916 | * When a tree block is COW'd through a tree, there are four cases: |
| 917 | * |
| 918 | * The reference count of the block is one and the tree is the block's |
| 919 | * owner tree. Nothing to do in this case. |
| 920 | * |
| 921 | * The reference count of the block is one and the tree is not the |
| 922 | * block's owner tree. In this case, full back refs is used for pointers |
| 923 | * in the block. Remove these full back refs, add implicit back refs for |
| 924 | * every pointers in the new block. |
| 925 | * |
| 926 | * The reference count of the block is greater than one and the tree is |
| 927 | * the block's owner tree. In this case, implicit back refs is used for |
| 928 | * pointers in the block. Add full back refs for every pointers in the |
| 929 | * block, increase lower level extents' reference counts. The original |
| 930 | * implicit back refs are entailed to the new block. |
| 931 | * |
| 932 | * The reference count of the block is greater than one and the tree is |
| 933 | * not the block's owner tree. Add implicit back refs for every pointer in |
| 934 | * the new block, increase lower level extents' reference count. |
| 935 | * |
| 936 | * Back Reference Key composing: |
| 937 | * |
| 938 | * The key objectid corresponds to the first byte in the extent, |
| 939 | * The key type is used to differentiate between types of back refs. |
| 940 | * There are different meanings of the key offset for different types |
| 941 | * of back refs. |
| 942 | * |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 943 | * File extents can be referenced by: |
| 944 | * |
| 945 | * - multiple snapshots, subvolumes, or different generations in one subvol |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 946 | * - different files inside a single subvolume |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 947 | * - different offsets inside a file (bookend extents in file.c) |
| 948 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 949 | * The extent ref structure for the implicit back refs has fields for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 950 | * |
| 951 | * - Objectid of the subvolume root |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 952 | * - objectid of the file holding the reference |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 953 | * - original offset in the file |
| 954 | * - how many bookend extents |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 955 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 956 | * The key offset for the implicit back refs is hash of the first |
| 957 | * three fields. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 958 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 959 | * The extent ref structure for the full back refs has field for: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 960 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 961 | * - number of pointers in the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 962 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 963 | * The key offset for the implicit back refs is the first byte of |
| 964 | * the tree leaf |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 965 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 966 | * When a file extent is allocated, The implicit back refs is used. |
| 967 | * the fields are filled in: |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 968 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 969 | * (root_key.objectid, inode objectid, offset in file, 1) |
| 970 | * |
| 971 | * When a file extent is removed file truncation, we find the |
| 972 | * corresponding implicit back refs and check the following fields: |
| 973 | * |
| 974 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 975 | * |
| 976 | * Btree extents can be referenced by: |
| 977 | * |
| 978 | * - Different subvolumes |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 979 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 980 | * Both the implicit back refs and the full back refs for tree blocks |
| 981 | * only consist of key. The key offset for the implicit back refs is |
| 982 | * objectid of block's owner tree. The key offset for the full back refs |
| 983 | * is the first byte of parent block. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 984 | * |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 985 | * When implicit back refs is used, information about the lowest key and |
| 986 | * level of the tree block are required. These information are stored in |
| 987 | * tree block info structure. |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 988 | */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 989 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 990 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 991 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, |
| 992 | struct btrfs_root *root, |
| 993 | struct btrfs_path *path, |
| 994 | u64 owner, u32 extra_size) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 995 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 996 | struct btrfs_extent_item *item; |
| 997 | struct btrfs_extent_item_v0 *ei0; |
| 998 | struct btrfs_extent_ref_v0 *ref0; |
| 999 | struct btrfs_tree_block_info *bi; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1000 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1001 | struct btrfs_key key; |
| 1002 | struct btrfs_key found_key; |
| 1003 | u32 new_size = sizeof(*item); |
| 1004 | u64 refs; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1005 | int ret; |
| 1006 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1007 | leaf = path->nodes[0]; |
| 1008 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1009 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1010 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1011 | ei0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1012 | struct btrfs_extent_item_v0); |
| 1013 | refs = btrfs_extent_refs_v0(leaf, ei0); |
| 1014 | |
| 1015 | if (owner == (u64)-1) { |
| 1016 | while (1) { |
| 1017 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 1018 | ret = btrfs_next_leaf(root, path); |
| 1019 | if (ret < 0) |
| 1020 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1021 | BUG_ON(ret > 0); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1022 | leaf = path->nodes[0]; |
| 1023 | } |
| 1024 | btrfs_item_key_to_cpu(leaf, &found_key, |
| 1025 | path->slots[0]); |
| 1026 | BUG_ON(key.objectid != found_key.objectid); |
| 1027 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { |
| 1028 | path->slots[0]++; |
| 1029 | continue; |
| 1030 | } |
| 1031 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1032 | struct btrfs_extent_ref_v0); |
| 1033 | owner = btrfs_ref_objectid_v0(leaf, ref0); |
| 1034 | break; |
| 1035 | } |
| 1036 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1037 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1038 | |
| 1039 | if (owner < BTRFS_FIRST_FREE_OBJECTID) |
| 1040 | new_size += sizeof(*bi); |
| 1041 | |
| 1042 | new_size -= sizeof(*ei0); |
| 1043 | ret = btrfs_search_slot(trans, root, &key, path, |
| 1044 | new_size + extra_size, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1045 | if (ret < 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1046 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1047 | BUG_ON(ret); /* Corruption */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1048 | |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 1049 | btrfs_extend_item(root, path, new_size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1050 | |
| 1051 | leaf = path->nodes[0]; |
| 1052 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1053 | btrfs_set_extent_refs(leaf, item, refs); |
| 1054 | /* FIXME: get real generation */ |
| 1055 | btrfs_set_extent_generation(leaf, item, 0); |
| 1056 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1057 | btrfs_set_extent_flags(leaf, item, |
| 1058 | BTRFS_EXTENT_FLAG_TREE_BLOCK | |
| 1059 | BTRFS_BLOCK_FLAG_FULL_BACKREF); |
| 1060 | bi = (struct btrfs_tree_block_info *)(item + 1); |
| 1061 | /* FIXME: get first key of the block */ |
| 1062 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); |
| 1063 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
| 1064 | } else { |
| 1065 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); |
| 1066 | } |
| 1067 | btrfs_mark_buffer_dirty(leaf); |
| 1068 | return 0; |
| 1069 | } |
| 1070 | #endif |
| 1071 | |
| 1072 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) |
| 1073 | { |
| 1074 | u32 high_crc = ~(u32)0; |
| 1075 | u32 low_crc = ~(u32)0; |
| 1076 | __le64 lenum; |
| 1077 | |
| 1078 | lenum = cpu_to_le64(root_objectid); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1079 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1080 | lenum = cpu_to_le64(owner); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1081 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1082 | lenum = cpu_to_le64(offset); |
Filipe David Borba Manana | 14a958e | 2014-01-12 02:22:46 +0000 | [diff] [blame] | 1083 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1084 | |
| 1085 | return ((u64)high_crc << 31) ^ (u64)low_crc; |
| 1086 | } |
| 1087 | |
| 1088 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, |
| 1089 | struct btrfs_extent_data_ref *ref) |
| 1090 | { |
| 1091 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), |
| 1092 | btrfs_extent_data_ref_objectid(leaf, ref), |
| 1093 | btrfs_extent_data_ref_offset(leaf, ref)); |
| 1094 | } |
| 1095 | |
| 1096 | static int match_extent_data_ref(struct extent_buffer *leaf, |
| 1097 | struct btrfs_extent_data_ref *ref, |
| 1098 | u64 root_objectid, u64 owner, u64 offset) |
| 1099 | { |
| 1100 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || |
| 1101 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || |
| 1102 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 1103 | return 0; |
| 1104 | return 1; |
| 1105 | } |
| 1106 | |
| 1107 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1108 | struct btrfs_root *root, |
| 1109 | struct btrfs_path *path, |
| 1110 | u64 bytenr, u64 parent, |
| 1111 | u64 root_objectid, |
| 1112 | u64 owner, u64 offset) |
| 1113 | { |
| 1114 | struct btrfs_key key; |
| 1115 | struct btrfs_extent_data_ref *ref; |
| 1116 | struct extent_buffer *leaf; |
| 1117 | u32 nritems; |
| 1118 | int ret; |
| 1119 | int recow; |
| 1120 | int err = -ENOENT; |
| 1121 | |
| 1122 | key.objectid = bytenr; |
| 1123 | if (parent) { |
| 1124 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1125 | key.offset = parent; |
| 1126 | } else { |
| 1127 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1128 | key.offset = hash_extent_data_ref(root_objectid, |
| 1129 | owner, offset); |
| 1130 | } |
| 1131 | again: |
| 1132 | recow = 0; |
| 1133 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1134 | if (ret < 0) { |
| 1135 | err = ret; |
| 1136 | goto fail; |
| 1137 | } |
| 1138 | |
| 1139 | if (parent) { |
| 1140 | if (!ret) |
| 1141 | return 0; |
| 1142 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1143 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1144 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1145 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1146 | if (ret < 0) { |
| 1147 | err = ret; |
| 1148 | goto fail; |
| 1149 | } |
| 1150 | if (!ret) |
| 1151 | return 0; |
| 1152 | #endif |
| 1153 | goto fail; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1157 | nritems = btrfs_header_nritems(leaf); |
| 1158 | while (1) { |
| 1159 | if (path->slots[0] >= nritems) { |
| 1160 | ret = btrfs_next_leaf(root, path); |
| 1161 | if (ret < 0) |
| 1162 | err = ret; |
| 1163 | if (ret) |
| 1164 | goto fail; |
| 1165 | |
| 1166 | leaf = path->nodes[0]; |
| 1167 | nritems = btrfs_header_nritems(leaf); |
| 1168 | recow = 1; |
| 1169 | } |
| 1170 | |
| 1171 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1172 | if (key.objectid != bytenr || |
| 1173 | key.type != BTRFS_EXTENT_DATA_REF_KEY) |
| 1174 | goto fail; |
| 1175 | |
| 1176 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1177 | struct btrfs_extent_data_ref); |
| 1178 | |
| 1179 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1180 | owner, offset)) { |
| 1181 | if (recow) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1182 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1183 | goto again; |
| 1184 | } |
| 1185 | err = 0; |
| 1186 | break; |
| 1187 | } |
| 1188 | path->slots[0]++; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1189 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1190 | fail: |
| 1191 | return err; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1192 | } |
| 1193 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1194 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1195 | struct btrfs_root *root, |
| 1196 | struct btrfs_path *path, |
| 1197 | u64 bytenr, u64 parent, |
| 1198 | u64 root_objectid, u64 owner, |
| 1199 | u64 offset, int refs_to_add) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1200 | { |
| 1201 | struct btrfs_key key; |
| 1202 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1203 | u32 size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1204 | u32 num_refs; |
| 1205 | int ret; |
| 1206 | |
| 1207 | key.objectid = bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1208 | if (parent) { |
| 1209 | key.type = BTRFS_SHARED_DATA_REF_KEY; |
| 1210 | key.offset = parent; |
| 1211 | size = sizeof(struct btrfs_shared_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1212 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1213 | key.type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1214 | key.offset = hash_extent_data_ref(root_objectid, |
| 1215 | owner, offset); |
| 1216 | size = sizeof(struct btrfs_extent_data_ref); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1217 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1218 | |
| 1219 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
| 1220 | if (ret && ret != -EEXIST) |
| 1221 | goto fail; |
| 1222 | |
| 1223 | leaf = path->nodes[0]; |
| 1224 | if (parent) { |
| 1225 | struct btrfs_shared_data_ref *ref; |
| 1226 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1227 | struct btrfs_shared_data_ref); |
| 1228 | if (ret == 0) { |
| 1229 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); |
| 1230 | } else { |
| 1231 | num_refs = btrfs_shared_data_ref_count(leaf, ref); |
| 1232 | num_refs += refs_to_add; |
| 1233 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); |
| 1234 | } |
| 1235 | } else { |
| 1236 | struct btrfs_extent_data_ref *ref; |
| 1237 | while (ret == -EEXIST) { |
| 1238 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1239 | struct btrfs_extent_data_ref); |
| 1240 | if (match_extent_data_ref(leaf, ref, root_objectid, |
| 1241 | owner, offset)) |
| 1242 | break; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1243 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1244 | key.offset++; |
| 1245 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1246 | size); |
| 1247 | if (ret && ret != -EEXIST) |
| 1248 | goto fail; |
| 1249 | |
| 1250 | leaf = path->nodes[0]; |
| 1251 | } |
| 1252 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 1253 | struct btrfs_extent_data_ref); |
| 1254 | if (ret == 0) { |
| 1255 | btrfs_set_extent_data_ref_root(leaf, ref, |
| 1256 | root_objectid); |
| 1257 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 1258 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 1259 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); |
| 1260 | } else { |
| 1261 | num_refs = btrfs_extent_data_ref_count(leaf, ref); |
| 1262 | num_refs += refs_to_add; |
| 1263 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); |
| 1264 | } |
| 1265 | } |
| 1266 | btrfs_mark_buffer_dirty(leaf); |
| 1267 | ret = 0; |
| 1268 | fail: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1269 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1270 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1271 | } |
| 1272 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1273 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
| 1274 | struct btrfs_root *root, |
| 1275 | struct btrfs_path *path, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1276 | int refs_to_drop, int *last_ref) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1277 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1278 | struct btrfs_key key; |
| 1279 | struct btrfs_extent_data_ref *ref1 = NULL; |
| 1280 | struct btrfs_shared_data_ref *ref2 = NULL; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1281 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1282 | u32 num_refs = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1283 | int ret = 0; |
| 1284 | |
| 1285 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1286 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1287 | |
| 1288 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1289 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1290 | struct btrfs_extent_data_ref); |
| 1291 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1292 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1293 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1294 | struct btrfs_shared_data_ref); |
| 1295 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1296 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1297 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1298 | struct btrfs_extent_ref_v0 *ref0; |
| 1299 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1300 | struct btrfs_extent_ref_v0); |
| 1301 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1302 | #endif |
| 1303 | } else { |
| 1304 | BUG(); |
| 1305 | } |
| 1306 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1307 | BUG_ON(num_refs < refs_to_drop); |
| 1308 | num_refs -= refs_to_drop; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1309 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1310 | if (num_refs == 0) { |
| 1311 | ret = btrfs_del_item(trans, root, path); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1312 | *last_ref = 1; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1313 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1314 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1315 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); |
| 1316 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) |
| 1317 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); |
| 1318 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1319 | else { |
| 1320 | struct btrfs_extent_ref_v0 *ref0; |
| 1321 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1322 | struct btrfs_extent_ref_v0); |
| 1323 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); |
| 1324 | } |
| 1325 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1326 | btrfs_mark_buffer_dirty(leaf); |
| 1327 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1328 | return ret; |
| 1329 | } |
| 1330 | |
| 1331 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
| 1332 | struct btrfs_path *path, |
| 1333 | struct btrfs_extent_inline_ref *iref) |
| 1334 | { |
| 1335 | struct btrfs_key key; |
| 1336 | struct extent_buffer *leaf; |
| 1337 | struct btrfs_extent_data_ref *ref1; |
| 1338 | struct btrfs_shared_data_ref *ref2; |
| 1339 | u32 num_refs = 0; |
| 1340 | |
| 1341 | leaf = path->nodes[0]; |
| 1342 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 1343 | if (iref) { |
| 1344 | if (btrfs_extent_inline_ref_type(leaf, iref) == |
| 1345 | BTRFS_EXTENT_DATA_REF_KEY) { |
| 1346 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1347 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1348 | } else { |
| 1349 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1350 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1351 | } |
| 1352 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1353 | ref1 = btrfs_item_ptr(leaf, path->slots[0], |
| 1354 | struct btrfs_extent_data_ref); |
| 1355 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); |
| 1356 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1357 | ref2 = btrfs_item_ptr(leaf, path->slots[0], |
| 1358 | struct btrfs_shared_data_ref); |
| 1359 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); |
| 1360 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1361 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { |
| 1362 | struct btrfs_extent_ref_v0 *ref0; |
| 1363 | ref0 = btrfs_item_ptr(leaf, path->slots[0], |
| 1364 | struct btrfs_extent_ref_v0); |
| 1365 | num_refs = btrfs_ref_count_v0(leaf, ref0); |
| 1366 | #endif |
| 1367 | } else { |
| 1368 | WARN_ON(1); |
| 1369 | } |
| 1370 | return num_refs; |
| 1371 | } |
| 1372 | |
| 1373 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1374 | struct btrfs_root *root, |
| 1375 | struct btrfs_path *path, |
| 1376 | u64 bytenr, u64 parent, |
| 1377 | u64 root_objectid) |
| 1378 | { |
| 1379 | struct btrfs_key key; |
| 1380 | int ret; |
| 1381 | |
| 1382 | key.objectid = bytenr; |
| 1383 | if (parent) { |
| 1384 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1385 | key.offset = parent; |
| 1386 | } else { |
| 1387 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1388 | key.offset = root_objectid; |
| 1389 | } |
| 1390 | |
| 1391 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1392 | if (ret > 0) |
| 1393 | ret = -ENOENT; |
| 1394 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1395 | if (ret == -ENOENT && parent) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1396 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1397 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
| 1398 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 1399 | if (ret > 0) |
| 1400 | ret = -ENOENT; |
| 1401 | } |
| 1402 | #endif |
| 1403 | return ret; |
| 1404 | } |
| 1405 | |
| 1406 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
| 1407 | struct btrfs_root *root, |
| 1408 | struct btrfs_path *path, |
| 1409 | u64 bytenr, u64 parent, |
| 1410 | u64 root_objectid) |
| 1411 | { |
| 1412 | struct btrfs_key key; |
| 1413 | int ret; |
| 1414 | |
| 1415 | key.objectid = bytenr; |
| 1416 | if (parent) { |
| 1417 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1418 | key.offset = parent; |
| 1419 | } else { |
| 1420 | key.type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1421 | key.offset = root_objectid; |
| 1422 | } |
| 1423 | |
| 1424 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1425 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1426 | return ret; |
| 1427 | } |
| 1428 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1429 | static inline int extent_ref_type(u64 parent, u64 owner) |
| 1430 | { |
| 1431 | int type; |
| 1432 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1433 | if (parent > 0) |
| 1434 | type = BTRFS_SHARED_BLOCK_REF_KEY; |
| 1435 | else |
| 1436 | type = BTRFS_TREE_BLOCK_REF_KEY; |
| 1437 | } else { |
| 1438 | if (parent > 0) |
| 1439 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 1440 | else |
| 1441 | type = BTRFS_EXTENT_DATA_REF_KEY; |
| 1442 | } |
| 1443 | return type; |
| 1444 | } |
| 1445 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1446 | static int find_next_key(struct btrfs_path *path, int level, |
| 1447 | struct btrfs_key *key) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1448 | |
| 1449 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1450 | for (; level < BTRFS_MAX_LEVEL; level++) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1451 | if (!path->nodes[level]) |
| 1452 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1453 | if (path->slots[level] + 1 >= |
| 1454 | btrfs_header_nritems(path->nodes[level])) |
| 1455 | continue; |
| 1456 | if (level == 0) |
| 1457 | btrfs_item_key_to_cpu(path->nodes[level], key, |
| 1458 | path->slots[level] + 1); |
| 1459 | else |
| 1460 | btrfs_node_key_to_cpu(path->nodes[level], key, |
| 1461 | path->slots[level] + 1); |
| 1462 | return 0; |
| 1463 | } |
| 1464 | return 1; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * look for inline back ref. if back ref is found, *ref_ret is set |
| 1469 | * to the address of inline back ref, and 0 is returned. |
| 1470 | * |
| 1471 | * if back ref isn't found, *ref_ret is set to the address where it |
| 1472 | * should be inserted, and -ENOENT is returned. |
| 1473 | * |
| 1474 | * if insert is true and there are too many inline back refs, the path |
| 1475 | * points to the extent item, and -EAGAIN is returned. |
| 1476 | * |
| 1477 | * NOTE: inline back refs are ordered in the same way that back ref |
| 1478 | * items in the tree are ordered. |
| 1479 | */ |
| 1480 | static noinline_for_stack |
| 1481 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1482 | struct btrfs_root *root, |
| 1483 | struct btrfs_path *path, |
| 1484 | struct btrfs_extent_inline_ref **ref_ret, |
| 1485 | u64 bytenr, u64 num_bytes, |
| 1486 | u64 parent, u64 root_objectid, |
| 1487 | u64 owner, u64 offset, int insert) |
| 1488 | { |
| 1489 | struct btrfs_key key; |
| 1490 | struct extent_buffer *leaf; |
| 1491 | struct btrfs_extent_item *ei; |
| 1492 | struct btrfs_extent_inline_ref *iref; |
| 1493 | u64 flags; |
| 1494 | u64 item_size; |
| 1495 | unsigned long ptr; |
| 1496 | unsigned long end; |
| 1497 | int extra_size; |
| 1498 | int type; |
| 1499 | int want; |
| 1500 | int ret; |
| 1501 | int err = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1502 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 1503 | SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1504 | |
| 1505 | key.objectid = bytenr; |
| 1506 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1507 | key.offset = num_bytes; |
| 1508 | |
| 1509 | want = extent_ref_type(parent, owner); |
| 1510 | if (insert) { |
| 1511 | extra_size = btrfs_extent_inline_ref_size(want); |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1512 | path->keep_locks = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1513 | } else |
| 1514 | extra_size = -1; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1515 | |
| 1516 | /* |
| 1517 | * Owner is our parent level, so we can just add one to get the level |
| 1518 | * for the block we are interested in. |
| 1519 | */ |
| 1520 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1521 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 1522 | key.offset = owner; |
| 1523 | } |
| 1524 | |
| 1525 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1526 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
| 1527 | if (ret < 0) { |
| 1528 | err = ret; |
| 1529 | goto out; |
| 1530 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1531 | |
| 1532 | /* |
| 1533 | * We may be a newly converted file system which still has the old fat |
| 1534 | * extent entries for metadata, so try and see if we have one of those. |
| 1535 | */ |
| 1536 | if (ret > 0 && skinny_metadata) { |
| 1537 | skinny_metadata = false; |
| 1538 | if (path->slots[0]) { |
| 1539 | path->slots[0]--; |
| 1540 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 1541 | path->slots[0]); |
| 1542 | if (key.objectid == bytenr && |
| 1543 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1544 | key.offset == num_bytes) |
| 1545 | ret = 0; |
| 1546 | } |
| 1547 | if (ret) { |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 1548 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1549 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 1550 | key.offset = num_bytes; |
| 1551 | btrfs_release_path(path); |
| 1552 | goto again; |
| 1553 | } |
| 1554 | } |
| 1555 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1556 | if (ret && !insert) { |
| 1557 | err = -ENOENT; |
| 1558 | goto out; |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 1559 | } else if (WARN_ON(ret)) { |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1560 | err = -EIO; |
Josef Bacik | 492104c | 2013-03-08 15:41:02 -0500 | [diff] [blame] | 1561 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1562 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1563 | |
| 1564 | leaf = path->nodes[0]; |
| 1565 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1566 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 1567 | if (item_size < sizeof(*ei)) { |
| 1568 | if (!insert) { |
| 1569 | err = -ENOENT; |
| 1570 | goto out; |
| 1571 | } |
| 1572 | ret = convert_extent_item_v0(trans, root, path, owner, |
| 1573 | extra_size); |
| 1574 | if (ret < 0) { |
| 1575 | err = ret; |
| 1576 | goto out; |
| 1577 | } |
| 1578 | leaf = path->nodes[0]; |
| 1579 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1580 | } |
| 1581 | #endif |
| 1582 | BUG_ON(item_size < sizeof(*ei)); |
| 1583 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1584 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1585 | flags = btrfs_extent_flags(leaf, ei); |
| 1586 | |
| 1587 | ptr = (unsigned long)(ei + 1); |
| 1588 | end = (unsigned long)ei + item_size; |
| 1589 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 1590 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1591 | ptr += sizeof(struct btrfs_tree_block_info); |
| 1592 | BUG_ON(ptr > end); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | err = -ENOENT; |
| 1596 | while (1) { |
| 1597 | if (ptr >= end) { |
| 1598 | WARN_ON(ptr > end); |
| 1599 | break; |
| 1600 | } |
| 1601 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1602 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1603 | if (want < type) |
| 1604 | break; |
| 1605 | if (want > type) { |
| 1606 | ptr += btrfs_extent_inline_ref_size(type); |
| 1607 | continue; |
| 1608 | } |
| 1609 | |
| 1610 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1611 | struct btrfs_extent_data_ref *dref; |
| 1612 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1613 | if (match_extent_data_ref(leaf, dref, root_objectid, |
| 1614 | owner, offset)) { |
| 1615 | err = 0; |
| 1616 | break; |
| 1617 | } |
| 1618 | if (hash_extent_data_ref_item(leaf, dref) < |
| 1619 | hash_extent_data_ref(root_objectid, owner, offset)) |
| 1620 | break; |
| 1621 | } else { |
| 1622 | u64 ref_offset; |
| 1623 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); |
| 1624 | if (parent > 0) { |
| 1625 | if (parent == ref_offset) { |
| 1626 | err = 0; |
| 1627 | break; |
| 1628 | } |
| 1629 | if (ref_offset < parent) |
| 1630 | break; |
| 1631 | } else { |
| 1632 | if (root_objectid == ref_offset) { |
| 1633 | err = 0; |
| 1634 | break; |
| 1635 | } |
| 1636 | if (ref_offset < root_objectid) |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | ptr += btrfs_extent_inline_ref_size(type); |
| 1641 | } |
| 1642 | if (err == -ENOENT && insert) { |
| 1643 | if (item_size + extra_size >= |
| 1644 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { |
| 1645 | err = -EAGAIN; |
| 1646 | goto out; |
| 1647 | } |
| 1648 | /* |
| 1649 | * To add new inline back ref, we have to make sure |
| 1650 | * there is no corresponding back ref item. |
| 1651 | * For simplicity, we just do not add new inline back |
| 1652 | * ref if there is any kind of item for this block |
| 1653 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 1654 | if (find_next_key(path, 0, &key) == 0 && |
| 1655 | key.objectid == bytenr && |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1656 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1657 | err = -EAGAIN; |
| 1658 | goto out; |
| 1659 | } |
| 1660 | } |
| 1661 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; |
| 1662 | out: |
Yan Zheng | 85d4198 | 2009-06-11 08:51:10 -0400 | [diff] [blame] | 1663 | if (insert) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1664 | path->keep_locks = 0; |
| 1665 | btrfs_unlock_up_safe(path, 1); |
| 1666 | } |
| 1667 | return err; |
| 1668 | } |
| 1669 | |
| 1670 | /* |
| 1671 | * helper to add new inline back ref |
| 1672 | */ |
| 1673 | static noinline_for_stack |
Tsutomu Itoh | fd279fa | 2013-04-16 05:19:11 +0000 | [diff] [blame] | 1674 | void setup_inline_extent_backref(struct btrfs_root *root, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1675 | struct btrfs_path *path, |
| 1676 | struct btrfs_extent_inline_ref *iref, |
| 1677 | u64 parent, u64 root_objectid, |
| 1678 | u64 owner, u64 offset, int refs_to_add, |
| 1679 | struct btrfs_delayed_extent_op *extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1680 | { |
| 1681 | struct extent_buffer *leaf; |
| 1682 | struct btrfs_extent_item *ei; |
| 1683 | unsigned long ptr; |
| 1684 | unsigned long end; |
| 1685 | unsigned long item_offset; |
| 1686 | u64 refs; |
| 1687 | int size; |
| 1688 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1689 | |
| 1690 | leaf = path->nodes[0]; |
| 1691 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1692 | item_offset = (unsigned long)iref - (unsigned long)ei; |
| 1693 | |
| 1694 | type = extent_ref_type(parent, owner); |
| 1695 | size = btrfs_extent_inline_ref_size(type); |
| 1696 | |
Tsutomu Itoh | 4b90c68 | 2013-04-16 05:18:49 +0000 | [diff] [blame] | 1697 | btrfs_extend_item(root, path, size); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1698 | |
| 1699 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1700 | refs = btrfs_extent_refs(leaf, ei); |
| 1701 | refs += refs_to_add; |
| 1702 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1703 | if (extent_op) |
| 1704 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1705 | |
| 1706 | ptr = (unsigned long)ei + item_offset; |
| 1707 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); |
| 1708 | if (ptr < end - size) |
| 1709 | memmove_extent_buffer(leaf, ptr + size, ptr, |
| 1710 | end - size - ptr); |
| 1711 | |
| 1712 | iref = (struct btrfs_extent_inline_ref *)ptr; |
| 1713 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 1714 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1715 | struct btrfs_extent_data_ref *dref; |
| 1716 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1717 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); |
| 1718 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); |
| 1719 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); |
| 1720 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); |
| 1721 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1722 | struct btrfs_shared_data_ref *sref; |
| 1723 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1724 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); |
| 1725 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1726 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 1727 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 1728 | } else { |
| 1729 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 1730 | } |
| 1731 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 1735 | struct btrfs_root *root, |
| 1736 | struct btrfs_path *path, |
| 1737 | struct btrfs_extent_inline_ref **ref_ret, |
| 1738 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1739 | u64 root_objectid, u64 owner, u64 offset) |
| 1740 | { |
| 1741 | int ret; |
| 1742 | |
| 1743 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, |
| 1744 | bytenr, num_bytes, parent, |
| 1745 | root_objectid, owner, offset, 0); |
| 1746 | if (ret != -ENOENT) |
| 1747 | return ret; |
| 1748 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 1749 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1750 | *ref_ret = NULL; |
| 1751 | |
| 1752 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1753 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, |
| 1754 | root_objectid); |
| 1755 | } else { |
| 1756 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, |
| 1757 | root_objectid, owner, offset); |
| 1758 | } |
| 1759 | return ret; |
| 1760 | } |
| 1761 | |
| 1762 | /* |
| 1763 | * helper to update/remove inline back ref |
| 1764 | */ |
| 1765 | static noinline_for_stack |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1766 | void update_inline_extent_backref(struct btrfs_root *root, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1767 | struct btrfs_path *path, |
| 1768 | struct btrfs_extent_inline_ref *iref, |
| 1769 | int refs_to_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1770 | struct btrfs_delayed_extent_op *extent_op, |
| 1771 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1772 | { |
| 1773 | struct extent_buffer *leaf; |
| 1774 | struct btrfs_extent_item *ei; |
| 1775 | struct btrfs_extent_data_ref *dref = NULL; |
| 1776 | struct btrfs_shared_data_ref *sref = NULL; |
| 1777 | unsigned long ptr; |
| 1778 | unsigned long end; |
| 1779 | u32 item_size; |
| 1780 | int size; |
| 1781 | int type; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1782 | u64 refs; |
| 1783 | |
| 1784 | leaf = path->nodes[0]; |
| 1785 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 1786 | refs = btrfs_extent_refs(leaf, ei); |
| 1787 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); |
| 1788 | refs += refs_to_mod; |
| 1789 | btrfs_set_extent_refs(leaf, ei, refs); |
| 1790 | if (extent_op) |
| 1791 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 1792 | |
| 1793 | type = btrfs_extent_inline_ref_type(leaf, iref); |
| 1794 | |
| 1795 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 1796 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 1797 | refs = btrfs_extent_data_ref_count(leaf, dref); |
| 1798 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { |
| 1799 | sref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 1800 | refs = btrfs_shared_data_ref_count(leaf, sref); |
| 1801 | } else { |
| 1802 | refs = 1; |
| 1803 | BUG_ON(refs_to_mod != -1); |
| 1804 | } |
| 1805 | |
| 1806 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
| 1807 | refs += refs_to_mod; |
| 1808 | |
| 1809 | if (refs > 0) { |
| 1810 | if (type == BTRFS_EXTENT_DATA_REF_KEY) |
| 1811 | btrfs_set_extent_data_ref_count(leaf, dref, refs); |
| 1812 | else |
| 1813 | btrfs_set_shared_data_ref_count(leaf, sref, refs); |
| 1814 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1815 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1816 | size = btrfs_extent_inline_ref_size(type); |
| 1817 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 1818 | ptr = (unsigned long)iref; |
| 1819 | end = (unsigned long)ei + item_size; |
| 1820 | if (ptr + size < end) |
| 1821 | memmove_extent_buffer(leaf, ptr, ptr + size, |
| 1822 | end - ptr - size); |
| 1823 | item_size -= size; |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1824 | btrfs_truncate_item(root, path, item_size, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1825 | } |
| 1826 | btrfs_mark_buffer_dirty(leaf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | static noinline_for_stack |
| 1830 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, |
| 1831 | struct btrfs_root *root, |
| 1832 | struct btrfs_path *path, |
| 1833 | u64 bytenr, u64 num_bytes, u64 parent, |
| 1834 | u64 root_objectid, u64 owner, |
| 1835 | u64 offset, int refs_to_add, |
| 1836 | struct btrfs_delayed_extent_op *extent_op) |
| 1837 | { |
| 1838 | struct btrfs_extent_inline_ref *iref; |
| 1839 | int ret; |
| 1840 | |
| 1841 | ret = lookup_inline_extent_backref(trans, root, path, &iref, |
| 1842 | bytenr, num_bytes, parent, |
| 1843 | root_objectid, owner, offset, 1); |
| 1844 | if (ret == 0) { |
| 1845 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1846 | update_inline_extent_backref(root, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1847 | refs_to_add, extent_op, NULL); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1848 | } else if (ret == -ENOENT) { |
Tsutomu Itoh | fd279fa | 2013-04-16 05:19:11 +0000 | [diff] [blame] | 1849 | setup_inline_extent_backref(root, path, iref, parent, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1850 | root_objectid, owner, offset, |
| 1851 | refs_to_add, extent_op); |
| 1852 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1853 | } |
| 1854 | return ret; |
| 1855 | } |
| 1856 | |
| 1857 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
| 1858 | struct btrfs_root *root, |
| 1859 | struct btrfs_path *path, |
| 1860 | u64 bytenr, u64 parent, u64 root_objectid, |
| 1861 | u64 owner, u64 offset, int refs_to_add) |
| 1862 | { |
| 1863 | int ret; |
| 1864 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 1865 | BUG_ON(refs_to_add != 1); |
| 1866 | ret = insert_tree_block_ref(trans, root, path, bytenr, |
| 1867 | parent, root_objectid); |
| 1868 | } else { |
| 1869 | ret = insert_extent_data_ref(trans, root, path, bytenr, |
| 1870 | parent, root_objectid, |
| 1871 | owner, offset, refs_to_add); |
| 1872 | } |
| 1873 | return ret; |
| 1874 | } |
| 1875 | |
| 1876 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
| 1877 | struct btrfs_root *root, |
| 1878 | struct btrfs_path *path, |
| 1879 | struct btrfs_extent_inline_ref *iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1880 | int refs_to_drop, int is_data, int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1881 | { |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 1882 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1883 | |
| 1884 | BUG_ON(!is_data && refs_to_drop != 1); |
| 1885 | if (iref) { |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1886 | update_inline_extent_backref(root, path, iref, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1887 | -refs_to_drop, NULL, last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1888 | } else if (is_data) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1889 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop, |
| 1890 | last_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1891 | } else { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1892 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1893 | ret = btrfs_del_item(trans, root, path); |
| 1894 | } |
| 1895 | return ret; |
| 1896 | } |
| 1897 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1898 | static int btrfs_issue_discard(struct block_device *bdev, |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1899 | u64 start, u64 len) |
| 1900 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1901 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1902 | } |
Chris Mason | 15916de | 2008-11-19 21:17:22 -0500 | [diff] [blame] | 1903 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1904 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1905 | u64 num_bytes, u64 *actual_bytes) |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1906 | { |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1907 | int ret; |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1908 | u64 discarded_bytes = 0; |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1909 | struct btrfs_bio *bbio = NULL; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1910 | |
Christoph Hellwig | e244a0a | 2009-10-14 09:24:59 -0400 | [diff] [blame] | 1911 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1912 | /* Tell the block device(s) that the sectors can be discarded */ |
Stefan Behrens | 3ec706c | 2012-11-05 15:46:42 +0100 | [diff] [blame] | 1913 | ret = btrfs_map_block(root->fs_info, REQ_DISCARD, |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1914 | bytenr, &num_bytes, &bbio, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1915 | /* Error condition is -ENOMEM */ |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1916 | if (!ret) { |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1917 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1918 | int i; |
| 1919 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1920 | |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1921 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1922 | if (!stripe->dev->can_discard) |
| 1923 | continue; |
| 1924 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1925 | ret = btrfs_issue_discard(stripe->dev->bdev, |
| 1926 | stripe->physical, |
| 1927 | stripe->length); |
| 1928 | if (!ret) |
| 1929 | discarded_bytes += stripe->length; |
| 1930 | else if (ret != -EOPNOTSUPP) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1931 | break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */ |
Josef Bacik | d5e2003 | 2011-08-04 14:52:27 +0000 | [diff] [blame] | 1932 | |
| 1933 | /* |
| 1934 | * Just in case we get back EOPNOTSUPP for some reason, |
| 1935 | * just ignore the return value so we don't screw up |
| 1936 | * people calling discard_extent. |
| 1937 | */ |
| 1938 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1939 | } |
Jan Schmidt | a1d3c47 | 2011-08-04 17:15:33 +0200 | [diff] [blame] | 1940 | kfree(bbio); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1941 | } |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 1942 | |
| 1943 | if (actual_bytes) |
| 1944 | *actual_bytes = discarded_bytes; |
| 1945 | |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1946 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 1947 | if (ret == -EOPNOTSUPP) |
| 1948 | ret = 0; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1949 | return ret; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 1950 | } |
| 1951 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 1952 | /* Can return -ENOMEM */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1953 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 1954 | struct btrfs_root *root, |
| 1955 | u64 bytenr, u64 num_bytes, u64 parent, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1956 | u64 root_objectid, u64 owner, u64 offset, |
| 1957 | int no_quota) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1958 | { |
| 1959 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1960 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1961 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1962 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
| 1963 | root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1964 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1965 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1966 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 1967 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1968 | parent, root_objectid, (int)owner, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1969 | BTRFS_ADD_DELAYED_REF, NULL, no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1970 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 1971 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 1972 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1973 | parent, root_objectid, owner, offset, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1974 | BTRFS_ADD_DELAYED_REF, NULL, no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1975 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1976 | return ret; |
| 1977 | } |
| 1978 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1979 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1980 | struct btrfs_root *root, |
| 1981 | u64 bytenr, u64 num_bytes, |
| 1982 | u64 parent, u64 root_objectid, |
| 1983 | u64 owner, u64 offset, int refs_to_add, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1984 | int no_quota, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1985 | struct btrfs_delayed_extent_op *extent_op) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1986 | { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1987 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1988 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1989 | struct extent_buffer *leaf; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1990 | struct btrfs_extent_item *item; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1991 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1992 | u64 refs; |
| 1993 | int ret; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 1994 | enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_ADD_EXCL; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1995 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1996 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1997 | if (!path) |
| 1998 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1999 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2000 | if (!is_fstree(root_objectid) || !root->fs_info->quota_enabled) |
| 2001 | no_quota = 1; |
| 2002 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2003 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2004 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2005 | /* this will setup the path even if it fails to insert the back ref */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2006 | ret = insert_inline_extent_backref(trans, fs_info->extent_root, path, |
| 2007 | bytenr, num_bytes, parent, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2008 | root_objectid, owner, offset, |
| 2009 | refs_to_add, extent_op); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2010 | if ((ret < 0 && ret != -EAGAIN) || (!ret && no_quota)) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2011 | goto out; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2012 | /* |
| 2013 | * Ok we were able to insert an inline extent and it appears to be a new |
| 2014 | * reference, deal with the qgroup accounting. |
| 2015 | */ |
| 2016 | if (!ret && !no_quota) { |
| 2017 | ASSERT(root->fs_info->quota_enabled); |
| 2018 | leaf = path->nodes[0]; |
| 2019 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2020 | item = btrfs_item_ptr(leaf, path->slots[0], |
| 2021 | struct btrfs_extent_item); |
| 2022 | if (btrfs_extent_refs(leaf, item) > (u64)refs_to_add) |
| 2023 | type = BTRFS_QGROUP_OPER_ADD_SHARED; |
| 2024 | btrfs_release_path(path); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2025 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2026 | ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid, |
| 2027 | bytenr, num_bytes, type, 0); |
| 2028 | goto out; |
| 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * Ok we had -EAGAIN which means we didn't have space to insert and |
| 2033 | * inline extent ref, so just update the reference count and add a |
| 2034 | * normal backref. |
| 2035 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2036 | leaf = path->nodes[0]; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2037 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2038 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2039 | refs = btrfs_extent_refs(leaf, item); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2040 | if (refs) |
| 2041 | type = BTRFS_QGROUP_OPER_ADD_SHARED; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2042 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); |
| 2043 | if (extent_op) |
| 2044 | __run_delayed_extent_op(extent_op, leaf, item); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2045 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2046 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2047 | btrfs_release_path(path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2048 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2049 | if (!no_quota) { |
| 2050 | ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid, |
| 2051 | bytenr, num_bytes, type, 0); |
| 2052 | if (ret) |
| 2053 | goto out; |
| 2054 | } |
| 2055 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 2056 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2057 | path->leave_spinning = 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2058 | /* now insert the actual backref */ |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2059 | ret = insert_extent_backref(trans, root->fs_info->extent_root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2060 | path, bytenr, parent, root_objectid, |
| 2061 | owner, offset, refs_to_add); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2062 | if (ret) |
| 2063 | btrfs_abort_transaction(trans, root, ret); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2064 | out: |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2065 | btrfs_free_path(path); |
Liu Bo | 30d133f | 2013-10-11 16:30:23 +0800 | [diff] [blame] | 2066 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2067 | } |
| 2068 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2069 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
| 2070 | struct btrfs_root *root, |
| 2071 | struct btrfs_delayed_ref_node *node, |
| 2072 | struct btrfs_delayed_extent_op *extent_op, |
| 2073 | int insert_reserved) |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2074 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2075 | int ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2076 | struct btrfs_delayed_data_ref *ref; |
| 2077 | struct btrfs_key ins; |
| 2078 | u64 parent = 0; |
| 2079 | u64 ref_root = 0; |
| 2080 | u64 flags = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2081 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2082 | ins.objectid = node->bytenr; |
| 2083 | ins.offset = node->num_bytes; |
| 2084 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2085 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2086 | ref = btrfs_delayed_node_to_data_ref(node); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2087 | trace_run_delayed_data_ref(node, ref, node->action); |
| 2088 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2089 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2090 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2091 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2092 | |
| 2093 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2094 | if (extent_op) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2095 | flags |= extent_op->flags_to_set; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2096 | ret = alloc_reserved_file_extent(trans, root, |
| 2097 | parent, ref_root, flags, |
| 2098 | ref->objectid, ref->offset, |
| 2099 | &ins, node->ref_mod); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2100 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2101 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2102 | node->num_bytes, parent, |
| 2103 | ref_root, ref->objectid, |
| 2104 | ref->offset, node->ref_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2105 | node->no_quota, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2106 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2107 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2108 | node->num_bytes, parent, |
| 2109 | ref_root, ref->objectid, |
| 2110 | ref->offset, node->ref_mod, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2111 | extent_op, node->no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2112 | } else { |
| 2113 | BUG(); |
| 2114 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2115 | return ret; |
| 2116 | } |
| 2117 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2118 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
| 2119 | struct extent_buffer *leaf, |
| 2120 | struct btrfs_extent_item *ei) |
| 2121 | { |
| 2122 | u64 flags = btrfs_extent_flags(leaf, ei); |
| 2123 | if (extent_op->update_flags) { |
| 2124 | flags |= extent_op->flags_to_set; |
| 2125 | btrfs_set_extent_flags(leaf, ei, flags); |
| 2126 | } |
| 2127 | |
| 2128 | if (extent_op->update_key) { |
| 2129 | struct btrfs_tree_block_info *bi; |
| 2130 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); |
| 2131 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 2132 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, |
| 2137 | struct btrfs_root *root, |
| 2138 | struct btrfs_delayed_ref_node *node, |
| 2139 | struct btrfs_delayed_extent_op *extent_op) |
| 2140 | { |
| 2141 | struct btrfs_key key; |
| 2142 | struct btrfs_path *path; |
| 2143 | struct btrfs_extent_item *ei; |
| 2144 | struct extent_buffer *leaf; |
| 2145 | u32 item_size; |
| 2146 | int ret; |
| 2147 | int err = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2148 | int metadata = !extent_op->is_data; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2149 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2150 | if (trans->aborted) |
| 2151 | return 0; |
| 2152 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2153 | if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) |
| 2154 | metadata = 0; |
| 2155 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2156 | path = btrfs_alloc_path(); |
| 2157 | if (!path) |
| 2158 | return -ENOMEM; |
| 2159 | |
| 2160 | key.objectid = node->bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2161 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2162 | if (metadata) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2163 | key.type = BTRFS_METADATA_ITEM_KEY; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2164 | key.offset = extent_op->level; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2165 | } else { |
| 2166 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2167 | key.offset = node->num_bytes; |
| 2168 | } |
| 2169 | |
| 2170 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2171 | path->reada = 1; |
| 2172 | path->leave_spinning = 1; |
| 2173 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, |
| 2174 | path, 0, 1); |
| 2175 | if (ret < 0) { |
| 2176 | err = ret; |
| 2177 | goto out; |
| 2178 | } |
| 2179 | if (ret > 0) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2180 | if (metadata) { |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2181 | if (path->slots[0] > 0) { |
| 2182 | path->slots[0]--; |
| 2183 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 2184 | path->slots[0]); |
| 2185 | if (key.objectid == node->bytenr && |
| 2186 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 2187 | key.offset == node->num_bytes) |
| 2188 | ret = 0; |
| 2189 | } |
| 2190 | if (ret > 0) { |
| 2191 | btrfs_release_path(path); |
| 2192 | metadata = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2193 | |
Filipe David Borba Manana | 5599488 | 2013-10-18 15:42:56 +0100 | [diff] [blame] | 2194 | key.objectid = node->bytenr; |
| 2195 | key.offset = node->num_bytes; |
| 2196 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 2197 | goto again; |
| 2198 | } |
| 2199 | } else { |
| 2200 | err = -EIO; |
| 2201 | goto out; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2202 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | leaf = path->nodes[0]; |
| 2206 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2207 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2208 | if (item_size < sizeof(*ei)) { |
| 2209 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, |
| 2210 | path, (u64)-1, 0); |
| 2211 | if (ret < 0) { |
| 2212 | err = ret; |
| 2213 | goto out; |
| 2214 | } |
| 2215 | leaf = path->nodes[0]; |
| 2216 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2217 | } |
| 2218 | #endif |
| 2219 | BUG_ON(item_size < sizeof(*ei)); |
| 2220 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2221 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 2222 | |
| 2223 | btrfs_mark_buffer_dirty(leaf); |
| 2224 | out: |
| 2225 | btrfs_free_path(path); |
| 2226 | return err; |
| 2227 | } |
| 2228 | |
| 2229 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
| 2230 | struct btrfs_root *root, |
| 2231 | struct btrfs_delayed_ref_node *node, |
| 2232 | struct btrfs_delayed_extent_op *extent_op, |
| 2233 | int insert_reserved) |
| 2234 | { |
| 2235 | int ret = 0; |
| 2236 | struct btrfs_delayed_tree_ref *ref; |
| 2237 | struct btrfs_key ins; |
| 2238 | u64 parent = 0; |
| 2239 | u64 ref_root = 0; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2240 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 2241 | SKINNY_METADATA); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2242 | |
| 2243 | ref = btrfs_delayed_node_to_tree_ref(node); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2244 | trace_run_delayed_tree_ref(node, ref, node->action); |
| 2245 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2246 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2247 | parent = ref->parent; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2248 | ref_root = ref->root; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2249 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2250 | ins.objectid = node->bytenr; |
| 2251 | if (skinny_metadata) { |
| 2252 | ins.offset = ref->level; |
| 2253 | ins.type = BTRFS_METADATA_ITEM_KEY; |
| 2254 | } else { |
| 2255 | ins.offset = node->num_bytes; |
| 2256 | ins.type = BTRFS_EXTENT_ITEM_KEY; |
| 2257 | } |
| 2258 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2259 | BUG_ON(node->ref_mod != 1); |
| 2260 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 2261 | BUG_ON(!extent_op || !extent_op->update_flags); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2262 | ret = alloc_reserved_tree_block(trans, root, |
| 2263 | parent, ref_root, |
| 2264 | extent_op->flags_to_set, |
| 2265 | &extent_op->key, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2266 | ref->level, &ins, |
| 2267 | node->no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2268 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
| 2269 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, |
| 2270 | node->num_bytes, parent, ref_root, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2271 | ref->level, 0, 1, node->no_quota, |
| 2272 | extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2273 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
| 2274 | ret = __btrfs_free_extent(trans, root, node->bytenr, |
| 2275 | node->num_bytes, parent, ref_root, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2276 | ref->level, 0, 1, extent_op, |
| 2277 | node->no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2278 | } else { |
| 2279 | BUG(); |
| 2280 | } |
| 2281 | return ret; |
| 2282 | } |
| 2283 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2284 | /* helper function to actually process a single delayed ref entry */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2285 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
| 2286 | struct btrfs_root *root, |
| 2287 | struct btrfs_delayed_ref_node *node, |
| 2288 | struct btrfs_delayed_extent_op *extent_op, |
| 2289 | int insert_reserved) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2290 | { |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2291 | int ret = 0; |
| 2292 | |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2293 | if (trans->aborted) { |
| 2294 | if (insert_reserved) |
| 2295 | btrfs_pin_extent(root, node->bytenr, |
| 2296 | node->num_bytes, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2297 | return 0; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2298 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2299 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2300 | if (btrfs_delayed_ref_is_head(node)) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2301 | struct btrfs_delayed_ref_head *head; |
| 2302 | /* |
| 2303 | * we've hit the end of the chain and we were supposed |
| 2304 | * to insert this extent into the tree. But, it got |
| 2305 | * deleted before we ever needed to insert it, so all |
| 2306 | * we have to do is clean up the accounting |
| 2307 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2308 | BUG_ON(extent_op); |
| 2309 | head = btrfs_delayed_node_to_head(node); |
Liu Bo | 599c75e | 2013-07-16 19:03:36 +0800 | [diff] [blame] | 2310 | trace_run_delayed_ref_head(node, head, node->action); |
| 2311 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2312 | if (insert_reserved) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2313 | btrfs_pin_extent(root, node->bytenr, |
| 2314 | node->num_bytes, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2315 | if (head->is_data) { |
| 2316 | ret = btrfs_del_csums(trans, root, |
| 2317 | node->bytenr, |
| 2318 | node->num_bytes); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2319 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2320 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2321 | return ret; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2322 | } |
Josef Bacik | eb09967 | 2009-02-12 09:27:38 -0500 | [diff] [blame] | 2323 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2324 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 2325 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
| 2326 | ret = run_delayed_tree_ref(trans, root, node, extent_op, |
| 2327 | insert_reserved); |
| 2328 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 2329 | node->type == BTRFS_SHARED_DATA_REF_KEY) |
| 2330 | ret = run_delayed_data_ref(trans, root, node, extent_op, |
| 2331 | insert_reserved); |
| 2332 | else |
| 2333 | BUG(); |
| 2334 | return ret; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2335 | } |
| 2336 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2337 | static noinline struct btrfs_delayed_ref_node * |
| 2338 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2339 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2340 | struct rb_node *node; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2341 | struct btrfs_delayed_ref_node *ref, *last = NULL;; |
| 2342 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2343 | /* |
| 2344 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. |
| 2345 | * this prevents ref count from going down to zero when |
| 2346 | * there still are pending delayed ref. |
| 2347 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2348 | node = rb_first(&head->ref_root); |
| 2349 | while (node) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2350 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 2351 | rb_node); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2352 | if (ref->action == BTRFS_ADD_DELAYED_REF) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2353 | return ref; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2354 | else if (last == NULL) |
| 2355 | last = ref; |
| 2356 | node = rb_next(node); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2357 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2358 | return last; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2359 | } |
| 2360 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2361 | /* |
| 2362 | * Returns 0 on success or if called with an already aborted transaction. |
| 2363 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. |
| 2364 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2365 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2366 | struct btrfs_root *root, |
| 2367 | unsigned long nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2368 | { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2369 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2370 | struct btrfs_delayed_ref_node *ref; |
| 2371 | struct btrfs_delayed_ref_head *locked_ref = NULL; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2372 | struct btrfs_delayed_extent_op *extent_op; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2373 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2374 | ktime_t start = ktime_get(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2375 | int ret; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2376 | unsigned long count = 0; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2377 | unsigned long actual_count = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2378 | int must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2379 | |
| 2380 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2381 | while (1) { |
| 2382 | if (!locked_ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2383 | if (count >= nr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2384 | break; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2385 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2386 | spin_lock(&delayed_refs->lock); |
| 2387 | locked_ref = btrfs_select_ref_head(trans); |
| 2388 | if (!locked_ref) { |
| 2389 | spin_unlock(&delayed_refs->lock); |
| 2390 | break; |
| 2391 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2392 | |
| 2393 | /* grab the lock that says we are going to process |
| 2394 | * all the refs for this head */ |
| 2395 | ret = btrfs_delayed_ref_lock(trans, locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2396 | spin_unlock(&delayed_refs->lock); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2397 | /* |
| 2398 | * we may have dropped the spin lock to get the head |
| 2399 | * mutex lock, and that might have given someone else |
| 2400 | * time to free the head. If that's true, it has been |
| 2401 | * removed from our list and we can move on. |
| 2402 | */ |
| 2403 | if (ret == -EAGAIN) { |
| 2404 | locked_ref = NULL; |
| 2405 | count++; |
| 2406 | continue; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | /* |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2411 | * We need to try and merge add/drops of the same ref since we |
| 2412 | * can run into issues with relocate dropping the implicit ref |
| 2413 | * and then it being added back again before the drop can |
| 2414 | * finish. If we merged anything we need to re-loop so we can |
| 2415 | * get a good ref. |
| 2416 | */ |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2417 | spin_lock(&locked_ref->lock); |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 2418 | btrfs_merge_delayed_refs(trans, fs_info, delayed_refs, |
| 2419 | locked_ref); |
| 2420 | |
| 2421 | /* |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2422 | * locked_ref is the head node, so we have to go one |
| 2423 | * node back for any delayed ref updates |
| 2424 | */ |
| 2425 | ref = select_delayed_ref(locked_ref); |
| 2426 | |
| 2427 | if (ref && ref->seq && |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 2428 | btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2429 | spin_unlock(&locked_ref->lock); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2430 | btrfs_delayed_ref_unlock(locked_ref); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2431 | spin_lock(&delayed_refs->lock); |
| 2432 | locked_ref->processing = 0; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2433 | delayed_refs->num_heads_ready++; |
| 2434 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2435 | locked_ref = NULL; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2436 | cond_resched(); |
Josef Bacik | 27a377d | 2014-02-07 13:57:59 -0500 | [diff] [blame] | 2437 | count++; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 2438 | continue; |
| 2439 | } |
| 2440 | |
| 2441 | /* |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2442 | * record the must insert reserved flag before we |
| 2443 | * drop the spin lock. |
| 2444 | */ |
| 2445 | must_insert_reserved = locked_ref->must_insert_reserved; |
| 2446 | locked_ref->must_insert_reserved = 0; |
| 2447 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2448 | extent_op = locked_ref->extent_op; |
| 2449 | locked_ref->extent_op = NULL; |
| 2450 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2451 | if (!ref) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2452 | |
| 2453 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2454 | /* All delayed refs have been processed, Go ahead |
| 2455 | * and send the head node to run_one_delayed_ref, |
| 2456 | * so that any accounting fixes can happen |
| 2457 | */ |
| 2458 | ref = &locked_ref->node; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2459 | |
| 2460 | if (extent_op && must_insert_reserved) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2461 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2462 | extent_op = NULL; |
| 2463 | } |
| 2464 | |
| 2465 | if (extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2466 | spin_unlock(&locked_ref->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2467 | ret = run_delayed_extent_op(trans, root, |
| 2468 | ref, extent_op); |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2469 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2470 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2471 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 2472 | /* |
| 2473 | * Need to reset must_insert_reserved if |
| 2474 | * there was an error so the abort stuff |
| 2475 | * can cleanup the reserved space |
| 2476 | * properly. |
| 2477 | */ |
| 2478 | if (must_insert_reserved) |
| 2479 | locked_ref->must_insert_reserved = 1; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2480 | locked_ref->processing = 0; |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2481 | btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret); |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2482 | btrfs_delayed_ref_unlock(locked_ref); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2483 | return ret; |
| 2484 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2485 | continue; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2486 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2487 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2488 | /* |
| 2489 | * Need to drop our head ref lock and re-aqcuire the |
| 2490 | * delayed ref lock and then re-check to make sure |
| 2491 | * nobody got added. |
| 2492 | */ |
| 2493 | spin_unlock(&locked_ref->lock); |
| 2494 | spin_lock(&delayed_refs->lock); |
| 2495 | spin_lock(&locked_ref->lock); |
Josef Bacik | 573a075 | 2014-03-27 19:41:34 -0400 | [diff] [blame] | 2496 | if (rb_first(&locked_ref->ref_root) || |
| 2497 | locked_ref->extent_op) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2498 | spin_unlock(&locked_ref->lock); |
| 2499 | spin_unlock(&delayed_refs->lock); |
| 2500 | continue; |
| 2501 | } |
| 2502 | ref->in_tree = 0; |
| 2503 | delayed_refs->num_heads--; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2504 | rb_erase(&locked_ref->href_node, |
| 2505 | &delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2506 | spin_unlock(&delayed_refs->lock); |
| 2507 | } else { |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2508 | actual_count++; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2509 | ref->in_tree = 0; |
| 2510 | rb_erase(&ref->rb_node, &locked_ref->ref_root); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2511 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2512 | atomic_dec(&delayed_refs->num_entries); |
| 2513 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2514 | if (!btrfs_delayed_ref_is_head(ref)) { |
Arne Jansen | 22cd2e7 | 2012-08-09 00:16:53 -0600 | [diff] [blame] | 2515 | /* |
| 2516 | * when we play the delayed ref, also correct the |
| 2517 | * ref_mod on head |
| 2518 | */ |
| 2519 | switch (ref->action) { |
| 2520 | case BTRFS_ADD_DELAYED_REF: |
| 2521 | case BTRFS_ADD_DELAYED_EXTENT: |
| 2522 | locked_ref->node.ref_mod -= ref->ref_mod; |
| 2523 | break; |
| 2524 | case BTRFS_DROP_DELAYED_REF: |
| 2525 | locked_ref->node.ref_mod += ref->ref_mod; |
| 2526 | break; |
| 2527 | default: |
| 2528 | WARN_ON(1); |
| 2529 | } |
| 2530 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2531 | spin_unlock(&locked_ref->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2532 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2533 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2534 | must_insert_reserved); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2535 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2536 | btrfs_free_delayed_extent_op(extent_op); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2537 | if (ret) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2538 | locked_ref->processing = 0; |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2539 | btrfs_delayed_ref_unlock(locked_ref); |
| 2540 | btrfs_put_delayed_ref(ref); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 2541 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2542 | return ret; |
| 2543 | } |
| 2544 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2545 | /* |
| 2546 | * If this node is a head, that means all the refs in this head |
| 2547 | * have been dealt with, and we will pick the next head to deal |
| 2548 | * with, so we must unlock the head and drop it from the cluster |
| 2549 | * list before we release it. |
| 2550 | */ |
| 2551 | if (btrfs_delayed_ref_is_head(ref)) { |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 2552 | btrfs_delayed_ref_unlock(locked_ref); |
| 2553 | locked_ref = NULL; |
| 2554 | } |
| 2555 | btrfs_put_delayed_ref(ref); |
| 2556 | count++; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2557 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2558 | } |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2559 | |
| 2560 | /* |
| 2561 | * We don't want to include ref heads since we can have empty ref heads |
| 2562 | * and those will drastically skew our runtime down since we just do |
| 2563 | * accounting, no actual extent tree updates. |
| 2564 | */ |
| 2565 | if (actual_count > 0) { |
| 2566 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 2567 | u64 avg; |
| 2568 | |
| 2569 | /* |
| 2570 | * We weigh the current average higher than our current runtime |
| 2571 | * to avoid large swings in the average. |
| 2572 | */ |
| 2573 | spin_lock(&delayed_refs->lock); |
| 2574 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; |
| 2575 | avg = div64_u64(avg, 4); |
| 2576 | fs_info->avg_delayed_ref_runtime = avg; |
| 2577 | spin_unlock(&delayed_refs->lock); |
| 2578 | } |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2579 | return 0; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2580 | } |
| 2581 | |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2582 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2583 | /* |
| 2584 | * Normally delayed refs get processed in ascending bytenr order. This |
| 2585 | * correlates in most cases to the order added. To expose dependencies on this |
| 2586 | * order, we start to process the tree in the middle instead of the beginning |
| 2587 | */ |
| 2588 | static u64 find_middle(struct rb_root *root) |
| 2589 | { |
| 2590 | struct rb_node *n = root->rb_node; |
| 2591 | struct btrfs_delayed_ref_node *entry; |
| 2592 | int alt = 1; |
| 2593 | u64 middle; |
| 2594 | u64 first = 0, last = 0; |
| 2595 | |
| 2596 | n = rb_first(root); |
| 2597 | if (n) { |
| 2598 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2599 | first = entry->bytenr; |
| 2600 | } |
| 2601 | n = rb_last(root); |
| 2602 | if (n) { |
| 2603 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2604 | last = entry->bytenr; |
| 2605 | } |
| 2606 | n = root->rb_node; |
| 2607 | |
| 2608 | while (n) { |
| 2609 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 2610 | WARN_ON(!entry->in_tree); |
| 2611 | |
| 2612 | middle = entry->bytenr; |
| 2613 | |
| 2614 | if (alt) |
| 2615 | n = n->rb_left; |
| 2616 | else |
| 2617 | n = n->rb_right; |
| 2618 | |
| 2619 | alt = 1 - alt; |
| 2620 | } |
| 2621 | return middle; |
| 2622 | } |
| 2623 | #endif |
| 2624 | |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2625 | static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads) |
| 2626 | { |
| 2627 | u64 num_bytes; |
| 2628 | |
| 2629 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + |
| 2630 | sizeof(struct btrfs_extent_inline_ref)); |
| 2631 | if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) |
| 2632 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
| 2633 | |
| 2634 | /* |
| 2635 | * We don't ever fill up leaves all the way so multiply by 2 just to be |
| 2636 | * closer to what we're really going to want to ouse. |
| 2637 | */ |
| 2638 | return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root)); |
| 2639 | } |
| 2640 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2641 | int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 2642 | struct btrfs_root *root) |
| 2643 | { |
| 2644 | struct btrfs_block_rsv *global_rsv; |
| 2645 | u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; |
| 2646 | u64 num_bytes; |
| 2647 | int ret = 0; |
| 2648 | |
| 2649 | num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
| 2650 | num_heads = heads_to_leaves(root, num_heads); |
| 2651 | if (num_heads > 1) |
| 2652 | num_bytes += (num_heads - 1) * root->leafsize; |
| 2653 | num_bytes <<= 1; |
| 2654 | global_rsv = &root->fs_info->global_block_rsv; |
| 2655 | |
| 2656 | /* |
| 2657 | * If we can't allocate any more chunks lets make sure we have _lots_ of |
| 2658 | * wiggle room since running delayed refs can create more delayed refs. |
| 2659 | */ |
| 2660 | if (global_rsv->space_info->full) |
| 2661 | num_bytes <<= 1; |
| 2662 | |
| 2663 | spin_lock(&global_rsv->lock); |
| 2664 | if (global_rsv->reserved <= num_bytes) |
| 2665 | ret = 1; |
| 2666 | spin_unlock(&global_rsv->lock); |
| 2667 | return ret; |
| 2668 | } |
| 2669 | |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2670 | int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, |
| 2671 | struct btrfs_root *root) |
| 2672 | { |
| 2673 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2674 | u64 num_entries = |
| 2675 | atomic_read(&trans->transaction->delayed_refs.num_entries); |
| 2676 | u64 avg_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame^] | 2677 | u64 val; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2678 | |
| 2679 | smp_mb(); |
| 2680 | avg_runtime = fs_info->avg_delayed_ref_runtime; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame^] | 2681 | val = num_entries * avg_runtime; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2682 | if (num_entries * avg_runtime >= NSEC_PER_SEC) |
| 2683 | return 1; |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame^] | 2684 | if (val >= NSEC_PER_SEC / 2) |
| 2685 | return 2; |
Josef Bacik | 0a2b2a8 | 2014-01-23 10:54:11 -0500 | [diff] [blame] | 2686 | |
| 2687 | return btrfs_check_space_for_delayed_refs(trans, root); |
| 2688 | } |
| 2689 | |
Chris Mason | a79b7d4 | 2014-05-22 16:18:52 -0700 | [diff] [blame^] | 2690 | struct async_delayed_refs { |
| 2691 | struct btrfs_root *root; |
| 2692 | int count; |
| 2693 | int error; |
| 2694 | int sync; |
| 2695 | struct completion wait; |
| 2696 | struct btrfs_work work; |
| 2697 | }; |
| 2698 | |
| 2699 | static void delayed_ref_async_start(struct btrfs_work *work) |
| 2700 | { |
| 2701 | struct async_delayed_refs *async; |
| 2702 | struct btrfs_trans_handle *trans; |
| 2703 | int ret; |
| 2704 | |
| 2705 | async = container_of(work, struct async_delayed_refs, work); |
| 2706 | |
| 2707 | trans = btrfs_join_transaction(async->root); |
| 2708 | if (IS_ERR(trans)) { |
| 2709 | async->error = PTR_ERR(trans); |
| 2710 | goto done; |
| 2711 | } |
| 2712 | |
| 2713 | /* |
| 2714 | * trans->sync means that when we call end_transaciton, we won't |
| 2715 | * wait on delayed refs |
| 2716 | */ |
| 2717 | trans->sync = true; |
| 2718 | ret = btrfs_run_delayed_refs(trans, async->root, async->count); |
| 2719 | if (ret) |
| 2720 | async->error = ret; |
| 2721 | |
| 2722 | ret = btrfs_end_transaction(trans, async->root); |
| 2723 | if (ret && !async->error) |
| 2724 | async->error = ret; |
| 2725 | done: |
| 2726 | if (async->sync) |
| 2727 | complete(&async->wait); |
| 2728 | else |
| 2729 | kfree(async); |
| 2730 | } |
| 2731 | |
| 2732 | int btrfs_async_run_delayed_refs(struct btrfs_root *root, |
| 2733 | unsigned long count, int wait) |
| 2734 | { |
| 2735 | struct async_delayed_refs *async; |
| 2736 | int ret; |
| 2737 | |
| 2738 | async = kmalloc(sizeof(*async), GFP_NOFS); |
| 2739 | if (!async) |
| 2740 | return -ENOMEM; |
| 2741 | |
| 2742 | async->root = root->fs_info->tree_root; |
| 2743 | async->count = count; |
| 2744 | async->error = 0; |
| 2745 | if (wait) |
| 2746 | async->sync = 1; |
| 2747 | else |
| 2748 | async->sync = 0; |
| 2749 | init_completion(&async->wait); |
| 2750 | |
| 2751 | btrfs_init_work(&async->work, delayed_ref_async_start, |
| 2752 | NULL, NULL); |
| 2753 | |
| 2754 | btrfs_queue_work(root->fs_info->extent_workers, &async->work); |
| 2755 | |
| 2756 | if (wait) { |
| 2757 | wait_for_completion(&async->wait); |
| 2758 | ret = async->error; |
| 2759 | kfree(async); |
| 2760 | return ret; |
| 2761 | } |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2765 | /* |
| 2766 | * this starts processing the delayed reference count updates and |
| 2767 | * extent insertions we have queued up so far. count can be |
| 2768 | * 0, which means to process everything in the tree at the start |
| 2769 | * of the run (but not newly added entries), or it can be some target |
| 2770 | * number you'd like to process. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2771 | * |
| 2772 | * Returns 0 on success or if called with an aborted transaction |
| 2773 | * Returns <0 on error and aborts the transaction |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2774 | */ |
| 2775 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
| 2776 | struct btrfs_root *root, unsigned long count) |
| 2777 | { |
| 2778 | struct rb_node *node; |
| 2779 | struct btrfs_delayed_ref_root *delayed_refs; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2780 | struct btrfs_delayed_ref_head *head; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2781 | int ret; |
| 2782 | int run_all = count == (unsigned long)-1; |
| 2783 | int run_most = 0; |
| 2784 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2785 | /* We'll clean this up in btrfs_cleanup_transaction */ |
| 2786 | if (trans->aborted) |
| 2787 | return 0; |
| 2788 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2789 | if (root == root->fs_info->extent_root) |
| 2790 | root = root->fs_info->tree_root; |
| 2791 | |
| 2792 | delayed_refs = &trans->transaction->delayed_refs; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2793 | if (count == 0) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2794 | count = atomic_read(&delayed_refs->num_entries) * 2; |
Chris Mason | bb72170 | 2013-01-29 18:44:12 -0500 | [diff] [blame] | 2795 | run_most = 1; |
| 2796 | } |
| 2797 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2798 | again: |
Arne Jansen | 709c048 | 2011-09-12 12:22:57 +0200 | [diff] [blame] | 2799 | #ifdef SCRAMBLE_DELAYED_REFS |
| 2800 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); |
| 2801 | #endif |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2802 | ret = __btrfs_run_delayed_refs(trans, root, count); |
| 2803 | if (ret < 0) { |
| 2804 | btrfs_abort_transaction(trans, root, ret); |
| 2805 | return ret; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2806 | } |
| 2807 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2808 | if (run_all) { |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2809 | if (!list_empty(&trans->new_bgs)) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2810 | btrfs_create_pending_block_groups(trans, root); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 2811 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2812 | spin_lock(&delayed_refs->lock); |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2813 | node = rb_first(&delayed_refs->href_root); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2814 | if (!node) { |
| 2815 | spin_unlock(&delayed_refs->lock); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2816 | goto out; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2817 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 2818 | count = (unsigned long)-1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2819 | |
| 2820 | while (node) { |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2821 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
| 2822 | href_node); |
| 2823 | if (btrfs_delayed_ref_is_head(&head->node)) { |
| 2824 | struct btrfs_delayed_ref_node *ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2825 | |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2826 | ref = &head->node; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2827 | atomic_inc(&ref->refs); |
| 2828 | |
| 2829 | spin_unlock(&delayed_refs->lock); |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2830 | /* |
| 2831 | * Mutex was contended, block until it's |
| 2832 | * released and try again |
| 2833 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2834 | mutex_lock(&head->mutex); |
| 2835 | mutex_unlock(&head->mutex); |
| 2836 | |
| 2837 | btrfs_put_delayed_ref(ref); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 2838 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2839 | goto again; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 2840 | } else { |
| 2841 | WARN_ON(1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2842 | } |
| 2843 | node = rb_next(node); |
| 2844 | } |
| 2845 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2846 | cond_resched(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 2847 | goto again; |
| 2848 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2849 | out: |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 2850 | ret = btrfs_delayed_qgroup_accounting(trans, root->fs_info); |
| 2851 | if (ret) |
| 2852 | return ret; |
Jan Schmidt | edf3927 | 2012-06-28 18:04:55 +0200 | [diff] [blame] | 2853 | assert_qgroups_uptodate(trans); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 2854 | return 0; |
| 2855 | } |
| 2856 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2857 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
| 2858 | struct btrfs_root *root, |
| 2859 | u64 bytenr, u64 num_bytes, u64 flags, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2860 | int level, int is_data) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2861 | { |
| 2862 | struct btrfs_delayed_extent_op *extent_op; |
| 2863 | int ret; |
| 2864 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2865 | extent_op = btrfs_alloc_delayed_extent_op(); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2866 | if (!extent_op) |
| 2867 | return -ENOMEM; |
| 2868 | |
| 2869 | extent_op->flags_to_set = flags; |
| 2870 | extent_op->update_flags = 1; |
| 2871 | extent_op->update_key = 0; |
| 2872 | extent_op->is_data = is_data ? 1 : 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 2873 | extent_op->level = level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2874 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 2875 | ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr, |
| 2876 | num_bytes, extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2877 | if (ret) |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 2878 | btrfs_free_delayed_extent_op(extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2879 | return ret; |
| 2880 | } |
| 2881 | |
| 2882 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, |
| 2883 | struct btrfs_root *root, |
| 2884 | struct btrfs_path *path, |
| 2885 | u64 objectid, u64 offset, u64 bytenr) |
| 2886 | { |
| 2887 | struct btrfs_delayed_ref_head *head; |
| 2888 | struct btrfs_delayed_ref_node *ref; |
| 2889 | struct btrfs_delayed_data_ref *data_ref; |
| 2890 | struct btrfs_delayed_ref_root *delayed_refs; |
| 2891 | struct rb_node *node; |
| 2892 | int ret = 0; |
| 2893 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2894 | delayed_refs = &trans->transaction->delayed_refs; |
| 2895 | spin_lock(&delayed_refs->lock); |
| 2896 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2897 | if (!head) { |
| 2898 | spin_unlock(&delayed_refs->lock); |
| 2899 | return 0; |
| 2900 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2901 | |
| 2902 | if (!mutex_trylock(&head->mutex)) { |
| 2903 | atomic_inc(&head->node.refs); |
| 2904 | spin_unlock(&delayed_refs->lock); |
| 2905 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2906 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2907 | |
David Sterba | 8cc33e5 | 2011-05-02 15:29:25 +0200 | [diff] [blame] | 2908 | /* |
| 2909 | * Mutex was contended, block until it's released and let |
| 2910 | * caller try again |
| 2911 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2912 | mutex_lock(&head->mutex); |
| 2913 | mutex_unlock(&head->mutex); |
| 2914 | btrfs_put_delayed_ref(&head->node); |
| 2915 | return -EAGAIN; |
| 2916 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2917 | spin_unlock(&delayed_refs->lock); |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 2918 | |
| 2919 | spin_lock(&head->lock); |
| 2920 | node = rb_first(&head->ref_root); |
| 2921 | while (node) { |
| 2922 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 2923 | node = rb_next(node); |
| 2924 | |
| 2925 | /* If it's a shared ref we know a cross reference exists */ |
| 2926 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { |
| 2927 | ret = 1; |
| 2928 | break; |
| 2929 | } |
| 2930 | |
| 2931 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
| 2932 | |
| 2933 | /* |
| 2934 | * If our ref doesn't match the one we're currently looking at |
| 2935 | * then we have a cross reference. |
| 2936 | */ |
| 2937 | if (data_ref->root != root->root_key.objectid || |
| 2938 | data_ref->objectid != objectid || |
| 2939 | data_ref->offset != offset) { |
| 2940 | ret = 1; |
| 2941 | break; |
| 2942 | } |
| 2943 | } |
| 2944 | spin_unlock(&head->lock); |
| 2945 | mutex_unlock(&head->mutex); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2946 | return ret; |
| 2947 | } |
| 2948 | |
| 2949 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, |
| 2950 | struct btrfs_root *root, |
| 2951 | struct btrfs_path *path, |
| 2952 | u64 objectid, u64 offset, u64 bytenr) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2953 | { |
| 2954 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2955 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2956 | struct btrfs_extent_data_ref *ref; |
| 2957 | struct btrfs_extent_inline_ref *iref; |
| 2958 | struct btrfs_extent_item *ei; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2959 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2960 | u32 item_size; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2961 | int ret; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2962 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2963 | key.objectid = bytenr; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2964 | key.offset = (u64)-1; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2965 | key.type = BTRFS_EXTENT_ITEM_KEY; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2966 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2967 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2968 | if (ret < 0) |
| 2969 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 2970 | BUG_ON(ret == 0); /* Corruption */ |
Yan Zheng | 80ff385 | 2008-10-30 14:20:02 -0400 | [diff] [blame] | 2971 | |
| 2972 | ret = -ENOENT; |
| 2973 | if (path->slots[0] == 0) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2974 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2975 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 2976 | path->slots[0]--; |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 2977 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2978 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2979 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2980 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2981 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2982 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2983 | ret = 1; |
| 2984 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2985 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 2986 | if (item_size < sizeof(*ei)) { |
| 2987 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); |
| 2988 | goto out; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 2989 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2990 | #endif |
| 2991 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
| 2992 | |
| 2993 | if (item_size != sizeof(*ei) + |
| 2994 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) |
| 2995 | goto out; |
| 2996 | |
| 2997 | if (btrfs_extent_generation(leaf, ei) <= |
| 2998 | btrfs_root_last_snapshot(&root->root_item)) |
| 2999 | goto out; |
| 3000 | |
| 3001 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); |
| 3002 | if (btrfs_extent_inline_ref_type(leaf, iref) != |
| 3003 | BTRFS_EXTENT_DATA_REF_KEY) |
| 3004 | goto out; |
| 3005 | |
| 3006 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 3007 | if (btrfs_extent_refs(leaf, ei) != |
| 3008 | btrfs_extent_data_ref_count(leaf, ref) || |
| 3009 | btrfs_extent_data_ref_root(leaf, ref) != |
| 3010 | root->root_key.objectid || |
| 3011 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || |
| 3012 | btrfs_extent_data_ref_offset(leaf, ref) != offset) |
| 3013 | goto out; |
| 3014 | |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3015 | ret = 0; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 3016 | out: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3017 | return ret; |
| 3018 | } |
| 3019 | |
| 3020 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, |
| 3021 | struct btrfs_root *root, |
| 3022 | u64 objectid, u64 offset, u64 bytenr) |
| 3023 | { |
| 3024 | struct btrfs_path *path; |
| 3025 | int ret; |
| 3026 | int ret2; |
| 3027 | |
| 3028 | path = btrfs_alloc_path(); |
| 3029 | if (!path) |
| 3030 | return -ENOENT; |
| 3031 | |
| 3032 | do { |
| 3033 | ret = check_committed_ref(trans, root, path, objectid, |
| 3034 | offset, bytenr); |
| 3035 | if (ret && ret != -ENOENT) |
| 3036 | goto out; |
| 3037 | |
| 3038 | ret2 = check_delayed_ref(trans, root, path, objectid, |
| 3039 | offset, bytenr); |
| 3040 | } while (ret2 == -EAGAIN); |
| 3041 | |
| 3042 | if (ret2 && ret2 != -ENOENT) { |
| 3043 | ret = ret2; |
| 3044 | goto out; |
| 3045 | } |
| 3046 | |
| 3047 | if (ret != -ENOENT || ret2 != -ENOENT) |
| 3048 | ret = 0; |
| 3049 | out: |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3050 | btrfs_free_path(path); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3051 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
| 3052 | WARN_ON(ret > 0); |
Yan Zheng | f321e49 | 2008-07-30 09:26:11 -0400 | [diff] [blame] | 3053 | return ret; |
| 3054 | } |
| 3055 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3056 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3057 | struct btrfs_root *root, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3058 | struct extent_buffer *buf, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3059 | int full_backref, int inc, int no_quota) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3060 | { |
| 3061 | u64 bytenr; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3062 | u64 num_bytes; |
| 3063 | u64 parent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3064 | u64 ref_root; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3065 | u32 nritems; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3066 | struct btrfs_key key; |
| 3067 | struct btrfs_file_extent_item *fi; |
| 3068 | int i; |
| 3069 | int level; |
| 3070 | int ret = 0; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3071 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3072 | u64, u64, u64, u64, u64, u64, int); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3073 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 3074 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 3075 | if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state))) |
| 3076 | return 0; |
| 3077 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3078 | ref_root = btrfs_header_owner(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3079 | nritems = btrfs_header_nritems(buf); |
| 3080 | level = btrfs_header_level(buf); |
| 3081 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 3082 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3083 | return 0; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3084 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3085 | if (inc) |
| 3086 | process_func = btrfs_inc_extent_ref; |
| 3087 | else |
| 3088 | process_func = btrfs_free_extent; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3089 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3090 | if (full_backref) |
| 3091 | parent = buf->start; |
| 3092 | else |
| 3093 | parent = 0; |
| 3094 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3095 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3096 | if (level == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3097 | btrfs_item_key_to_cpu(buf, &key, i); |
| 3098 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3099 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3100 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3101 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3102 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3103 | BTRFS_FILE_EXTENT_INLINE) |
| 3104 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3105 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 3106 | if (bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3107 | continue; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3108 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3109 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); |
| 3110 | key.offset -= btrfs_file_extent_offset(buf, fi); |
| 3111 | ret = process_func(trans, root, bytenr, num_bytes, |
| 3112 | parent, ref_root, key.objectid, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3113 | key.offset, no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3114 | if (ret) |
| 3115 | goto fail; |
Chris Mason | b7a9f29 | 2009-02-04 09:23:45 -0500 | [diff] [blame] | 3116 | } else { |
| 3117 | bytenr = btrfs_node_blockptr(buf, i); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3118 | num_bytes = btrfs_level_size(root, level - 1); |
| 3119 | ret = process_func(trans, root, bytenr, num_bytes, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 3120 | parent, ref_root, level - 1, 0, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3121 | no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3122 | if (ret) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3123 | goto fail; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3124 | } |
| 3125 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3126 | return 0; |
| 3127 | fail: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3128 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3129 | } |
| 3130 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3131 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3132 | struct extent_buffer *buf, int full_backref, int no_quota) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3133 | { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3134 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1, no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3135 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3136 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3137 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3138 | struct extent_buffer *buf, int full_backref, int no_quota) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3139 | { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 3140 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0, no_quota); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3141 | } |
| 3142 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3143 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 3144 | struct btrfs_root *root, |
| 3145 | struct btrfs_path *path, |
| 3146 | struct btrfs_block_group_cache *cache) |
| 3147 | { |
| 3148 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3149 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3150 | unsigned long bi; |
| 3151 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3152 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3153 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3154 | if (ret < 0) |
| 3155 | goto fail; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3156 | BUG_ON(ret); /* Corruption */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3157 | |
| 3158 | leaf = path->nodes[0]; |
| 3159 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 3160 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 3161 | btrfs_mark_buffer_dirty(leaf); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3162 | btrfs_release_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3163 | fail: |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3164 | if (ret) { |
| 3165 | btrfs_abort_transaction(trans, root, ret); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3166 | return ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3167 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3168 | return 0; |
| 3169 | |
| 3170 | } |
| 3171 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3172 | static struct btrfs_block_group_cache * |
| 3173 | next_block_group(struct btrfs_root *root, |
| 3174 | struct btrfs_block_group_cache *cache) |
| 3175 | { |
| 3176 | struct rb_node *node; |
| 3177 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 3178 | node = rb_next(&cache->cache_node); |
| 3179 | btrfs_put_block_group(cache); |
| 3180 | if (node) { |
| 3181 | cache = rb_entry(node, struct btrfs_block_group_cache, |
| 3182 | cache_node); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 3183 | btrfs_get_block_group(cache); |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3184 | } else |
| 3185 | cache = NULL; |
| 3186 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 3187 | return cache; |
| 3188 | } |
| 3189 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3190 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
| 3191 | struct btrfs_trans_handle *trans, |
| 3192 | struct btrfs_path *path) |
| 3193 | { |
| 3194 | struct btrfs_root *root = block_group->fs_info->tree_root; |
| 3195 | struct inode *inode = NULL; |
| 3196 | u64 alloc_hint = 0; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3197 | int dcs = BTRFS_DC_ERROR; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3198 | int num_pages = 0; |
| 3199 | int retries = 0; |
| 3200 | int ret = 0; |
| 3201 | |
| 3202 | /* |
| 3203 | * If this block group is smaller than 100 megs don't bother caching the |
| 3204 | * block group. |
| 3205 | */ |
| 3206 | if (block_group->key.offset < (100 * 1024 * 1024)) { |
| 3207 | spin_lock(&block_group->lock); |
| 3208 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3209 | spin_unlock(&block_group->lock); |
| 3210 | return 0; |
| 3211 | } |
| 3212 | |
| 3213 | again: |
| 3214 | inode = lookup_free_space_inode(root, block_group, path); |
| 3215 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { |
| 3216 | ret = PTR_ERR(inode); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3217 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3218 | goto out; |
| 3219 | } |
| 3220 | |
| 3221 | if (IS_ERR(inode)) { |
| 3222 | BUG_ON(retries); |
| 3223 | retries++; |
| 3224 | |
| 3225 | if (block_group->ro) |
| 3226 | goto out_free; |
| 3227 | |
| 3228 | ret = create_free_space_inode(root, trans, block_group, path); |
| 3229 | if (ret) |
| 3230 | goto out_free; |
| 3231 | goto again; |
| 3232 | } |
| 3233 | |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3234 | /* We've already setup this transaction, go ahead and exit */ |
| 3235 | if (block_group->cache_generation == trans->transid && |
| 3236 | i_size_read(inode)) { |
| 3237 | dcs = BTRFS_DC_SETUP; |
| 3238 | goto out_put; |
| 3239 | } |
| 3240 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3241 | /* |
| 3242 | * We want to set the generation to 0, that way if anything goes wrong |
| 3243 | * from here on out we know not to trust this cache when we load up next |
| 3244 | * time. |
| 3245 | */ |
| 3246 | BTRFS_I(inode)->generation = 0; |
| 3247 | ret = btrfs_update_inode(trans, root, inode); |
| 3248 | WARN_ON(ret); |
| 3249 | |
| 3250 | if (i_size_read(inode) > 0) { |
Miao Xie | 7b61cd9 | 2013-05-13 13:55:09 +0000 | [diff] [blame] | 3251 | ret = btrfs_check_trunc_cache_free_space(root, |
| 3252 | &root->fs_info->global_block_rsv); |
| 3253 | if (ret) |
| 3254 | goto out_put; |
| 3255 | |
Filipe David Borba Manana | 7451432 | 2013-09-20 14:46:51 +0100 | [diff] [blame] | 3256 | ret = btrfs_truncate_free_space_cache(root, trans, inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3257 | if (ret) |
| 3258 | goto out_put; |
| 3259 | } |
| 3260 | |
| 3261 | spin_lock(&block_group->lock); |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 3262 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
| 3263 | !btrfs_test_opt(root, SPACE_CACHE)) { |
| 3264 | /* |
| 3265 | * don't bother trying to write stuff out _if_ |
| 3266 | * a) we're not cached, |
| 3267 | * b) we're with nospace_cache mount option. |
| 3268 | */ |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3269 | dcs = BTRFS_DC_WRITTEN; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3270 | spin_unlock(&block_group->lock); |
| 3271 | goto out_put; |
| 3272 | } |
| 3273 | spin_unlock(&block_group->lock); |
| 3274 | |
Josef Bacik | 6fc823b | 2012-08-06 13:46:38 -0600 | [diff] [blame] | 3275 | /* |
| 3276 | * Try to preallocate enough space based on how big the block group is. |
| 3277 | * Keep in mind this has to include any pinned space which could end up |
| 3278 | * taking up quite a bit since it's not folded into the other space |
| 3279 | * cache. |
| 3280 | */ |
| 3281 | num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3282 | if (!num_pages) |
| 3283 | num_pages = 1; |
| 3284 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3285 | num_pages *= 16; |
| 3286 | num_pages *= PAGE_CACHE_SIZE; |
| 3287 | |
| 3288 | ret = btrfs_check_data_free_space(inode, num_pages); |
| 3289 | if (ret) |
| 3290 | goto out_put; |
| 3291 | |
| 3292 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, |
| 3293 | num_pages, num_pages, |
| 3294 | &alloc_hint); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3295 | if (!ret) |
| 3296 | dcs = BTRFS_DC_SETUP; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3297 | btrfs_free_reserved_data_space(inode, num_pages); |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 3298 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3299 | out_put: |
| 3300 | iput(inode); |
| 3301 | out_free: |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 3302 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3303 | out: |
| 3304 | spin_lock(&block_group->lock); |
Josef Bacik | e65cbb9 | 2011-12-13 16:04:54 -0500 | [diff] [blame] | 3305 | if (!ret && dcs == BTRFS_DC_SETUP) |
Josef Bacik | 5b0e95b | 2011-10-06 08:58:24 -0400 | [diff] [blame] | 3306 | block_group->cache_generation = trans->transid; |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 3307 | block_group->disk_cache_state = dcs; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3308 | spin_unlock(&block_group->lock); |
| 3309 | |
| 3310 | return ret; |
| 3311 | } |
| 3312 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3313 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 3314 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3315 | { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3316 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3317 | int err = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3318 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3319 | u64 last = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3320 | |
| 3321 | path = btrfs_alloc_path(); |
| 3322 | if (!path) |
| 3323 | return -ENOMEM; |
| 3324 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3325 | again: |
| 3326 | while (1) { |
| 3327 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3328 | while (cache) { |
| 3329 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
| 3330 | break; |
| 3331 | cache = next_block_group(root, cache); |
| 3332 | } |
| 3333 | if (!cache) { |
| 3334 | if (last == 0) |
| 3335 | break; |
| 3336 | last = 0; |
| 3337 | continue; |
| 3338 | } |
| 3339 | err = cache_save_setup(cache, trans, path); |
| 3340 | last = cache->key.objectid + cache->key.offset; |
| 3341 | btrfs_put_block_group(cache); |
| 3342 | } |
| 3343 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3344 | while (1) { |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3345 | if (last == 0) { |
| 3346 | err = btrfs_run_delayed_refs(trans, root, |
| 3347 | (unsigned long)-1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3348 | if (err) /* File system offline */ |
| 3349 | goto out; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3350 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3351 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3352 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3353 | while (cache) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3354 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
| 3355 | btrfs_put_block_group(cache); |
| 3356 | goto again; |
| 3357 | } |
| 3358 | |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3359 | if (cache->dirty) |
| 3360 | break; |
| 3361 | cache = next_block_group(root, cache); |
| 3362 | } |
| 3363 | if (!cache) { |
| 3364 | if (last == 0) |
| 3365 | break; |
| 3366 | last = 0; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 3367 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3368 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3369 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3370 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
| 3371 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3372 | cache->dirty = 0; |
| 3373 | last = cache->key.objectid + cache->key.offset; |
| 3374 | |
| 3375 | err = write_one_cache_group(trans, root, path, cache); |
Filipe David Borba Manana | e84cc14 | 2013-09-09 19:49:43 +0100 | [diff] [blame] | 3376 | btrfs_put_block_group(cache); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3377 | if (err) /* File system offline */ |
| 3378 | goto out; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3379 | } |
Yan Zheng | 4a8c9a6 | 2009-07-22 10:07:05 -0400 | [diff] [blame] | 3380 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3381 | while (1) { |
| 3382 | /* |
| 3383 | * I don't think this is needed since we're just marking our |
| 3384 | * preallocated extent as written, but just in case it can't |
| 3385 | * hurt. |
| 3386 | */ |
| 3387 | if (last == 0) { |
| 3388 | err = btrfs_run_delayed_refs(trans, root, |
| 3389 | (unsigned long)-1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3390 | if (err) /* File system offline */ |
| 3391 | goto out; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
| 3395 | while (cache) { |
| 3396 | /* |
| 3397 | * Really this shouldn't happen, but it could if we |
| 3398 | * couldn't write the entire preallocated extent and |
| 3399 | * splitting the extent resulted in a new block. |
| 3400 | */ |
| 3401 | if (cache->dirty) { |
| 3402 | btrfs_put_block_group(cache); |
| 3403 | goto again; |
| 3404 | } |
| 3405 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
| 3406 | break; |
| 3407 | cache = next_block_group(root, cache); |
| 3408 | } |
| 3409 | if (!cache) { |
| 3410 | if (last == 0) |
| 3411 | break; |
| 3412 | last = 0; |
| 3413 | continue; |
| 3414 | } |
| 3415 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3416 | err = btrfs_write_out_cache(root, trans, cache, path); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3417 | |
| 3418 | /* |
| 3419 | * If we didn't have an error then the cache state is still |
| 3420 | * NEED_WRITE, so we can set it to WRITTEN. |
| 3421 | */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3422 | if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE) |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3423 | cache->disk_cache_state = BTRFS_DC_WRITTEN; |
| 3424 | last = cache->key.objectid + cache->key.offset; |
| 3425 | btrfs_put_block_group(cache); |
| 3426 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3427 | out: |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 3428 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3429 | btrfs_free_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3430 | return err; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 3431 | } |
| 3432 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3433 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
| 3434 | { |
| 3435 | struct btrfs_block_group_cache *block_group; |
| 3436 | int readonly = 0; |
| 3437 | |
| 3438 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
| 3439 | if (!block_group || block_group->ro) |
| 3440 | readonly = 1; |
| 3441 | if (block_group) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 3442 | btrfs_put_block_group(block_group); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 3443 | return readonly; |
| 3444 | } |
| 3445 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3446 | static const char *alloc_name(u64 flags) |
| 3447 | { |
| 3448 | switch (flags) { |
| 3449 | case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA: |
| 3450 | return "mixed"; |
| 3451 | case BTRFS_BLOCK_GROUP_METADATA: |
| 3452 | return "metadata"; |
| 3453 | case BTRFS_BLOCK_GROUP_DATA: |
| 3454 | return "data"; |
| 3455 | case BTRFS_BLOCK_GROUP_SYSTEM: |
| 3456 | return "system"; |
| 3457 | default: |
| 3458 | WARN_ON(1); |
| 3459 | return "invalid-combination"; |
| 3460 | }; |
| 3461 | } |
| 3462 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3463 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 3464 | u64 total_bytes, u64 bytes_used, |
| 3465 | struct btrfs_space_info **space_info) |
| 3466 | { |
| 3467 | struct btrfs_space_info *found; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3468 | int i; |
| 3469 | int factor; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3470 | int ret; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3471 | |
| 3472 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | |
| 3473 | BTRFS_BLOCK_GROUP_RAID10)) |
| 3474 | factor = 2; |
| 3475 | else |
| 3476 | factor = 1; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3477 | |
| 3478 | found = __find_space_info(info, flags); |
| 3479 | if (found) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3480 | spin_lock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3481 | found->total_bytes += total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3482 | found->disk_total += total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3483 | found->bytes_used += bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3484 | found->disk_used += bytes_used * factor; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 3485 | found->full = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3486 | spin_unlock(&found->lock); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3487 | *space_info = found; |
| 3488 | return 0; |
| 3489 | } |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3490 | found = kzalloc(sizeof(*found), GFP_NOFS); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3491 | if (!found) |
| 3492 | return -ENOMEM; |
| 3493 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3494 | ret = percpu_counter_init(&found->total_bytes_pinned, 0); |
| 3495 | if (ret) { |
| 3496 | kfree(found); |
| 3497 | return ret; |
| 3498 | } |
| 3499 | |
Miao Xie | 536cd96 | 2013-12-17 12:01:12 +0800 | [diff] [blame] | 3500 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3501 | INIT_LIST_HEAD(&found->block_groups[i]); |
Miao Xie | 536cd96 | 2013-12-17 12:01:12 +0800 | [diff] [blame] | 3502 | kobject_init(&found->block_group_kobjs[i], &btrfs_raid_ktype); |
| 3503 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3504 | init_rwsem(&found->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 3505 | spin_lock_init(&found->lock); |
Ilya Dryomov | 52ba692 | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3506 | found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3507 | found->total_bytes = total_bytes; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 3508 | found->disk_total = total_bytes * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3509 | found->bytes_used = bytes_used; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3510 | found->disk_used = bytes_used * factor; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3511 | found->bytes_pinned = 0; |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 3512 | found->bytes_reserved = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3513 | found->bytes_readonly = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3514 | found->bytes_may_use = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3515 | found->full = 0; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3516 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3517 | found->chunk_alloc = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 3518 | found->flush = 0; |
| 3519 | init_waitqueue_head(&found->wait); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3520 | |
| 3521 | ret = kobject_init_and_add(&found->kobj, &space_info_ktype, |
| 3522 | info->space_info_kobj, "%s", |
| 3523 | alloc_name(found->flags)); |
| 3524 | if (ret) { |
| 3525 | kfree(found); |
| 3526 | return ret; |
| 3527 | } |
| 3528 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3529 | *space_info = found; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 3530 | list_add_rcu(&found->list, &info->space_info); |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3531 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3532 | info->data_sinfo = found; |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 3533 | |
| 3534 | return ret; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3535 | } |
| 3536 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3537 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 3538 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 3539 | u64 extra_flags = chunk_to_extended(flags) & |
| 3540 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3541 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3542 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3543 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3544 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 3545 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3546 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 3547 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3548 | fs_info->avail_system_alloc_bits |= extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3549 | write_sequnlock(&fs_info->profiles_lock); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 3550 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3551 | |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3552 | /* |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3553 | * returns target flags in extended format or 0 if restripe for this |
| 3554 | * chunk_type is not in progress |
Ilya Dryomov | c6664b4 | 2012-04-12 16:03:56 -0400 | [diff] [blame] | 3555 | * |
| 3556 | * should be called with either volume_mutex or balance_lock held |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3557 | */ |
| 3558 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
| 3559 | { |
| 3560 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
| 3561 | u64 target = 0; |
| 3562 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3563 | if (!bctl) |
| 3564 | return 0; |
| 3565 | |
| 3566 | if (flags & BTRFS_BLOCK_GROUP_DATA && |
| 3567 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3568 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; |
| 3569 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && |
| 3570 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3571 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; |
| 3572 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && |
| 3573 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { |
| 3574 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; |
| 3575 | } |
| 3576 | |
| 3577 | return target; |
| 3578 | } |
| 3579 | |
| 3580 | /* |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3581 | * @flags: available profiles in extended format (see ctree.h) |
| 3582 | * |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3583 | * Returns reduced profile in chunk format. If profile changing is in |
| 3584 | * progress (either running or paused) picks the target profile (if it's |
| 3585 | * already available), otherwise falls back to plain reducing. |
Ilya Dryomov | a46d11a | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3586 | */ |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 3587 | static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3588 | { |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 3589 | /* |
| 3590 | * we add in the count of missing devices because we want |
| 3591 | * to make sure that any RAID levels on a degraded FS |
| 3592 | * continue to be honored. |
| 3593 | */ |
| 3594 | u64 num_devices = root->fs_info->fs_devices->rw_devices + |
| 3595 | root->fs_info->fs_devices->missing_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3596 | u64 target; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3597 | u64 tmp; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3598 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3599 | /* |
| 3600 | * see if restripe for this chunk_type is in progress, if so |
| 3601 | * try to reduce to the target profile |
| 3602 | */ |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3603 | spin_lock(&root->fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3604 | target = get_restripe_target(root->fs_info, flags); |
| 3605 | if (target) { |
| 3606 | /* pick target profile only if it's already available */ |
| 3607 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3608 | spin_unlock(&root->fs_info->balance_lock); |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 3609 | return extended_to_chunk(target); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 3610 | } |
| 3611 | } |
| 3612 | spin_unlock(&root->fs_info->balance_lock); |
| 3613 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3614 | /* First, mask out the RAID levels which aren't possible */ |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3615 | if (num_devices == 1) |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3616 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 | |
| 3617 | BTRFS_BLOCK_GROUP_RAID5); |
| 3618 | if (num_devices < 3) |
| 3619 | flags &= ~BTRFS_BLOCK_GROUP_RAID6; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 3620 | if (num_devices < 4) |
| 3621 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; |
| 3622 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3623 | tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 | |
| 3624 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3625 | BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10); |
| 3626 | flags &= ~tmp; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3627 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3628 | if (tmp & BTRFS_BLOCK_GROUP_RAID6) |
| 3629 | tmp = BTRFS_BLOCK_GROUP_RAID6; |
| 3630 | else if (tmp & BTRFS_BLOCK_GROUP_RAID5) |
| 3631 | tmp = BTRFS_BLOCK_GROUP_RAID5; |
| 3632 | else if (tmp & BTRFS_BLOCK_GROUP_RAID10) |
| 3633 | tmp = BTRFS_BLOCK_GROUP_RAID10; |
| 3634 | else if (tmp & BTRFS_BLOCK_GROUP_RAID1) |
| 3635 | tmp = BTRFS_BLOCK_GROUP_RAID1; |
| 3636 | else if (tmp & BTRFS_BLOCK_GROUP_RAID0) |
| 3637 | tmp = BTRFS_BLOCK_GROUP_RAID0; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3638 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3639 | return extended_to_chunk(flags | tmp); |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 3640 | } |
| 3641 | |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 3642 | static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3643 | { |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3644 | unsigned seq; |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 3645 | u64 flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3646 | |
| 3647 | do { |
Filipe Manana | f8213bd | 2014-04-24 15:15:29 +0100 | [diff] [blame] | 3648 | flags = orig_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 3649 | seq = read_seqbegin(&root->fs_info->profiles_lock); |
| 3650 | |
| 3651 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 3652 | flags |= root->fs_info->avail_data_alloc_bits; |
| 3653 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 3654 | flags |= root->fs_info->avail_system_alloc_bits; |
| 3655 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 3656 | flags |= root->fs_info->avail_metadata_alloc_bits; |
| 3657 | } while (read_seqretry(&root->fs_info->profiles_lock, seq)); |
Ilya Dryomov | 6fef8df | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 3658 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3659 | return btrfs_reduce_alloc_profile(root, flags); |
| 3660 | } |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3661 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 3662 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3663 | { |
| 3664 | u64 flags; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3665 | u64 ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3666 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 3667 | if (data) |
| 3668 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 3669 | else if (root == root->fs_info->chunk_root) |
| 3670 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 3671 | else |
| 3672 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 3673 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3674 | ret = get_alloc_profile(root, flags); |
| 3675 | return ret; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3676 | } |
| 3677 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3678 | /* |
| 3679 | * This will check the space that the inode allocates from to make sure we have |
| 3680 | * enough space for bytes. |
| 3681 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3682 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3683 | { |
| 3684 | struct btrfs_space_info *data_sinfo; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3685 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3686 | struct btrfs_fs_info *fs_info = root->fs_info; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3687 | u64 used; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3688 | int ret = 0, committed = 0, alloc_chunk = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3689 | |
| 3690 | /* make sure bytes are sectorsize aligned */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3691 | bytes = ALIGN(bytes, root->sectorsize); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3692 | |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 3693 | if (btrfs_is_free_space_inode(inode)) { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3694 | committed = 1; |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 3695 | ASSERT(current->journal_info); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3696 | } |
| 3697 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3698 | data_sinfo = fs_info->data_sinfo; |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3699 | if (!data_sinfo) |
| 3700 | goto alloc; |
| 3701 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3702 | again: |
| 3703 | /* make sure we have enough space to handle the data first */ |
| 3704 | spin_lock(&data_sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 3705 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
| 3706 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + |
| 3707 | data_sinfo->bytes_may_use; |
Josef Bacik | ab6e2410 | 2010-03-19 14:38:13 +0000 | [diff] [blame] | 3708 | |
| 3709 | if (used + bytes > data_sinfo->total_bytes) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3710 | struct btrfs_trans_handle *trans; |
| 3711 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3712 | /* |
| 3713 | * if we don't have enough free bytes in this space then we need |
| 3714 | * to alloc a new chunk. |
| 3715 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 3716 | if (!data_sinfo->full && alloc_chunk) { |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3717 | u64 alloc_target; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3718 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3719 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3720 | spin_unlock(&data_sinfo->lock); |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3721 | alloc: |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3722 | alloc_target = btrfs_get_alloc_profile(root, 1); |
Miao Xie | 9dced18 | 2013-10-25 17:33:36 +0800 | [diff] [blame] | 3723 | /* |
| 3724 | * It is ugly that we don't call nolock join |
| 3725 | * transaction for the free space inode case here. |
| 3726 | * But it is safe because we only do the data space |
| 3727 | * reservation for the free space cache in the |
| 3728 | * transaction context, the common join transaction |
| 3729 | * just increase the counter of the current transaction |
| 3730 | * handler, doesn't try to acquire the trans_lock of |
| 3731 | * the fs. |
| 3732 | */ |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3733 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3734 | if (IS_ERR(trans)) |
| 3735 | return PTR_ERR(trans); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3736 | |
| 3737 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3738 | alloc_target, |
| 3739 | CHUNK_ALLOC_NO_FORCE); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3740 | btrfs_end_transaction(trans, root); |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3741 | if (ret < 0) { |
| 3742 | if (ret != -ENOSPC) |
| 3743 | return ret; |
| 3744 | else |
| 3745 | goto commit_trans; |
| 3746 | } |
Chris Mason | 33b4d47 | 2009-09-22 14:45:50 -0400 | [diff] [blame] | 3747 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3748 | if (!data_sinfo) |
| 3749 | data_sinfo = fs_info->data_sinfo; |
| 3750 | |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3751 | goto again; |
| 3752 | } |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3753 | |
| 3754 | /* |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3755 | * If we don't have enough pinned space to deal with this |
| 3756 | * allocation don't bother committing the transaction. |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3757 | */ |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3758 | if (percpu_counter_compare(&data_sinfo->total_bytes_pinned, |
| 3759 | bytes) < 0) |
Josef Bacik | f2bb8f5 | 2011-05-25 13:10:16 -0400 | [diff] [blame] | 3760 | committed = 1; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3761 | spin_unlock(&data_sinfo->lock); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3762 | |
| 3763 | /* commit the current transaction and try again */ |
Miao Xie | d52a5b5 | 2011-01-05 10:07:18 +0000 | [diff] [blame] | 3764 | commit_trans: |
Josef Bacik | a4abeea | 2011-04-11 17:25:13 -0400 | [diff] [blame] | 3765 | if (!committed && |
| 3766 | !atomic_read(&root->fs_info->open_ioctl_trans)) { |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3767 | committed = 1; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 3768 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 3769 | trans = btrfs_join_transaction(root); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 3770 | if (IS_ERR(trans)) |
| 3771 | return PTR_ERR(trans); |
Josef Bacik | 4e06bdd | 2009-02-20 10:59:53 -0500 | [diff] [blame] | 3772 | ret = btrfs_commit_transaction(trans, root); |
| 3773 | if (ret) |
| 3774 | return ret; |
| 3775 | goto again; |
| 3776 | } |
| 3777 | |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 3778 | trace_btrfs_space_reservation(root->fs_info, |
| 3779 | "space_info:enospc", |
| 3780 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3781 | return -ENOSPC; |
| 3782 | } |
| 3783 | data_sinfo->bytes_may_use += bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 3784 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3785 | data_sinfo->flags, bytes, 1); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3786 | spin_unlock(&data_sinfo->lock); |
| 3787 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3788 | return 0; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3792 | * Called if we need to clear a data reservation for this inode. |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3793 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3794 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3795 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 3796 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3797 | struct btrfs_space_info *data_sinfo; |
| 3798 | |
| 3799 | /* make sure bytes are sectorsize aligned */ |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 3800 | bytes = ALIGN(bytes, root->sectorsize); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3801 | |
Li Zefan | b4d7c3c | 2012-07-09 20:21:07 -0600 | [diff] [blame] | 3802 | data_sinfo = root->fs_info->data_sinfo; |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3803 | spin_lock(&data_sinfo->lock); |
Josef Bacik | 7ee9e44 | 2013-06-21 16:37:03 -0400 | [diff] [blame] | 3804 | WARN_ON(data_sinfo->bytes_may_use < bytes); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3805 | data_sinfo->bytes_may_use -= bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 3806 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3807 | data_sinfo->flags, bytes, 0); |
Josef Bacik | 6a63209 | 2009-02-20 11:00:09 -0500 | [diff] [blame] | 3808 | spin_unlock(&data_sinfo->lock); |
| 3809 | } |
| 3810 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3811 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
| 3812 | { |
| 3813 | struct list_head *head = &info->space_info; |
| 3814 | struct btrfs_space_info *found; |
| 3815 | |
| 3816 | rcu_read_lock(); |
| 3817 | list_for_each_entry_rcu(found, head, list) { |
| 3818 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3819 | found->force_alloc = CHUNK_ALLOC_FORCE; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3820 | } |
| 3821 | rcu_read_unlock(); |
| 3822 | } |
| 3823 | |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 3824 | static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global) |
| 3825 | { |
| 3826 | return (global->size << 1); |
| 3827 | } |
| 3828 | |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3829 | static int should_alloc_chunk(struct btrfs_root *root, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3830 | struct btrfs_space_info *sinfo, int force) |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3831 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3832 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3833 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3834 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
Chris Mason | e5bc245 | 2010-10-26 13:37:56 -0400 | [diff] [blame] | 3835 | u64 thresh; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3836 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3837 | if (force == CHUNK_ALLOC_FORCE) |
| 3838 | return 1; |
| 3839 | |
| 3840 | /* |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3841 | * We need to take into account the global rsv because for all intents |
| 3842 | * and purposes it's used space. Don't worry about locking the |
| 3843 | * global_rsv, it doesn't change except when the transaction commits. |
| 3844 | */ |
Josef Bacik | 54338b5 | 2012-08-14 16:20:52 -0400 | [diff] [blame] | 3845 | if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 3846 | num_allocated += calc_global_rsv_need_space(global_rsv); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 3847 | |
| 3848 | /* |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3849 | * in limited mode, we want to have some free space up to |
| 3850 | * about 1% of the FS size. |
| 3851 | */ |
| 3852 | if (force == CHUNK_ALLOC_LIMITED) { |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 3853 | thresh = btrfs_super_total_bytes(root->fs_info->super_copy); |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3854 | thresh = max_t(u64, 64 * 1024 * 1024, |
| 3855 | div_factor_fine(thresh, 1)); |
| 3856 | |
| 3857 | if (num_bytes - num_allocated < thresh) |
| 3858 | return 1; |
| 3859 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3860 | |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3861 | if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8)) |
Josef Bacik | 14ed0ca | 2010-10-15 15:23:48 -0400 | [diff] [blame] | 3862 | return 0; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3863 | return 1; |
| 3864 | } |
| 3865 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3866 | static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type) |
| 3867 | { |
| 3868 | u64 num_dev; |
| 3869 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 3870 | if (type & (BTRFS_BLOCK_GROUP_RAID10 | |
| 3871 | BTRFS_BLOCK_GROUP_RAID0 | |
| 3872 | BTRFS_BLOCK_GROUP_RAID5 | |
| 3873 | BTRFS_BLOCK_GROUP_RAID6)) |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3874 | num_dev = root->fs_info->fs_devices->rw_devices; |
| 3875 | else if (type & BTRFS_BLOCK_GROUP_RAID1) |
| 3876 | num_dev = 2; |
| 3877 | else |
| 3878 | num_dev = 1; /* DUP or single */ |
| 3879 | |
| 3880 | /* metadata for updaing devices and chunk tree */ |
| 3881 | return btrfs_calc_trans_metadata_size(root, num_dev + 1); |
| 3882 | } |
| 3883 | |
| 3884 | static void check_system_chunk(struct btrfs_trans_handle *trans, |
| 3885 | struct btrfs_root *root, u64 type) |
| 3886 | { |
| 3887 | struct btrfs_space_info *info; |
| 3888 | u64 left; |
| 3889 | u64 thresh; |
| 3890 | |
| 3891 | info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 3892 | spin_lock(&info->lock); |
| 3893 | left = info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 3894 | info->bytes_reserved - info->bytes_readonly; |
| 3895 | spin_unlock(&info->lock); |
| 3896 | |
| 3897 | thresh = get_system_chunk_thresh(root, type); |
| 3898 | if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 3899 | btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu", |
| 3900 | left, thresh, type); |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3901 | dump_space_info(info, 0, 0); |
| 3902 | } |
| 3903 | |
| 3904 | if (left < thresh) { |
| 3905 | u64 flags; |
| 3906 | |
| 3907 | flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0); |
| 3908 | btrfs_alloc_chunk(trans, root, flags); |
| 3909 | } |
| 3910 | } |
| 3911 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3912 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3913 | struct btrfs_root *extent_root, u64 flags, int force) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3914 | { |
| 3915 | struct btrfs_space_info *space_info; |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3916 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3917 | int wait_for_alloc = 0; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 3918 | int ret = 0; |
| 3919 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3920 | /* Don't re-enter if we're already allocating a chunk */ |
| 3921 | if (trans->allocating_chunk) |
| 3922 | return -ENOSPC; |
| 3923 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3924 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3925 | if (!space_info) { |
| 3926 | ret = update_space_info(extent_root->fs_info, flags, |
| 3927 | 0, 0, &space_info); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3928 | BUG_ON(ret); /* -ENOMEM */ |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 3929 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3930 | BUG_ON(!space_info); /* Logic error */ |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3931 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3932 | again: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3933 | spin_lock(&space_info->lock); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 3934 | if (force < space_info->force_alloc) |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3935 | force = space_info->force_alloc; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3936 | if (space_info->full) { |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 3937 | if (should_alloc_chunk(extent_root, space_info, force)) |
| 3938 | ret = -ENOSPC; |
| 3939 | else |
| 3940 | ret = 0; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3941 | spin_unlock(&space_info->lock); |
Filipe David Borba Manana | 09fb99a | 2013-08-05 16:25:12 +0100 | [diff] [blame] | 3942 | return ret; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3943 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 3944 | |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 3945 | if (!should_alloc_chunk(extent_root, space_info, force)) { |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3946 | spin_unlock(&space_info->lock); |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3947 | return 0; |
| 3948 | } else if (space_info->chunk_alloc) { |
| 3949 | wait_for_alloc = 1; |
| 3950 | } else { |
| 3951 | space_info->chunk_alloc = 1; |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3952 | } |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 3953 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 3954 | spin_unlock(&space_info->lock); |
| 3955 | |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 3956 | mutex_lock(&fs_info->chunk_mutex); |
| 3957 | |
| 3958 | /* |
| 3959 | * The chunk_mutex is held throughout the entirety of a chunk |
| 3960 | * allocation, so once we've acquired the chunk_mutex we know that the |
| 3961 | * other guy is done and we need to recheck and see if we should |
| 3962 | * allocate. |
| 3963 | */ |
| 3964 | if (wait_for_alloc) { |
| 3965 | mutex_unlock(&fs_info->chunk_mutex); |
| 3966 | wait_for_alloc = 0; |
| 3967 | goto again; |
| 3968 | } |
| 3969 | |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3970 | trans->allocating_chunk = true; |
| 3971 | |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3972 | /* |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 3973 | * If we have mixed data/metadata chunks we want to make sure we keep |
| 3974 | * allocating mixed chunks instead of individual chunks. |
| 3975 | */ |
| 3976 | if (btrfs_mixed_space_info(space_info)) |
| 3977 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); |
| 3978 | |
| 3979 | /* |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3980 | * if we're doing a data chunk, go ahead and make sure that |
| 3981 | * we keep a reasonable number of metadata chunks allocated in the |
| 3982 | * FS as well. |
| 3983 | */ |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 3984 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
Josef Bacik | 97e728d | 2009-04-21 17:40:57 -0400 | [diff] [blame] | 3985 | fs_info->data_chunk_allocations++; |
| 3986 | if (!(fs_info->data_chunk_allocations % |
| 3987 | fs_info->metadata_ratio)) |
| 3988 | force_metadata_allocation(fs_info); |
| 3989 | } |
| 3990 | |
Liu Bo | 15d1ff8 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 3991 | /* |
| 3992 | * Check if we have enough space in SYSTEM chunk because we may need |
| 3993 | * to update devices. |
| 3994 | */ |
| 3995 | check_system_chunk(trans, extent_root, flags); |
| 3996 | |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 3997 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
Josef Bacik | c6b305a | 2012-12-18 09:16:16 -0500 | [diff] [blame] | 3998 | trans->allocating_chunk = false; |
Mark Fasheh | 92b8e897 | 2011-07-12 10:57:59 -0700 | [diff] [blame] | 3999 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4000 | spin_lock(&space_info->lock); |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4001 | if (ret < 0 && ret != -ENOSPC) |
| 4002 | goto out; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4003 | if (ret) |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4004 | space_info->full = 1; |
Yan, Zheng | 424499d | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4005 | else |
| 4006 | ret = 1; |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4007 | |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 4008 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
Alexandre Oliva | a81cb9a | 2013-02-21 21:15:14 +0000 | [diff] [blame] | 4009 | out: |
Josef Bacik | 6d74119 | 2011-04-11 20:20:11 -0400 | [diff] [blame] | 4010 | space_info->chunk_alloc = 0; |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 4011 | spin_unlock(&space_info->lock); |
Dan Carpenter | a25c75d | 2012-04-18 09:59:29 +0300 | [diff] [blame] | 4012 | mutex_unlock(&fs_info->chunk_mutex); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 4013 | return ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 4014 | } |
| 4015 | |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4016 | static int can_overcommit(struct btrfs_root *root, |
| 4017 | struct btrfs_space_info *space_info, u64 bytes, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4018 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4019 | { |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4020 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4021 | u64 profile = btrfs_get_alloc_profile(root, 0); |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4022 | u64 space_size; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4023 | u64 avail; |
| 4024 | u64 used; |
| 4025 | |
| 4026 | used = space_info->bytes_used + space_info->bytes_reserved + |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4027 | space_info->bytes_pinned + space_info->bytes_readonly; |
| 4028 | |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4029 | /* |
| 4030 | * We only want to allow over committing if we have lots of actual space |
| 4031 | * free, but if we don't have enough space to handle the global reserve |
| 4032 | * space then we could end up having a real enospc problem when trying |
| 4033 | * to allocate a chunk or some other such important allocation. |
| 4034 | */ |
Miao Xie | 3c76cd8 | 2013-04-25 10:12:38 +0000 | [diff] [blame] | 4035 | spin_lock(&global_rsv->lock); |
| 4036 | space_size = calc_global_rsv_need_space(global_rsv); |
| 4037 | spin_unlock(&global_rsv->lock); |
| 4038 | if (used + space_size >= space_info->total_bytes) |
Josef Bacik | 96f1bb5 | 2013-01-30 17:02:51 -0500 | [diff] [blame] | 4039 | return 0; |
| 4040 | |
| 4041 | used += space_info->bytes_may_use; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4042 | |
| 4043 | spin_lock(&root->fs_info->free_chunk_lock); |
| 4044 | avail = root->fs_info->free_chunk_space; |
| 4045 | spin_unlock(&root->fs_info->free_chunk_lock); |
| 4046 | |
| 4047 | /* |
| 4048 | * If we have dup, raid1 or raid10 then only half of the free |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 4049 | * space is actually useable. For raid56, the space info used |
| 4050 | * doesn't include the parity drive, so we don't have to |
| 4051 | * change the math |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4052 | */ |
| 4053 | if (profile & (BTRFS_BLOCK_GROUP_DUP | |
| 4054 | BTRFS_BLOCK_GROUP_RAID1 | |
| 4055 | BTRFS_BLOCK_GROUP_RAID10)) |
| 4056 | avail >>= 1; |
| 4057 | |
| 4058 | /* |
Miao Xie | 561c294 | 2012-10-16 11:32:18 +0000 | [diff] [blame] | 4059 | * If we aren't flushing all things, let us overcommit up to |
| 4060 | * 1/2th of the space. If we can flush, don't let us overcommit |
| 4061 | * too much, let it overcommit up to 1/8 of the space. |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4062 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4063 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4064 | avail >>= 3; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4065 | else |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4066 | avail >>= 1; |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4067 | |
Josef Bacik | 14575ae | 2013-09-17 10:48:00 -0400 | [diff] [blame] | 4068 | if (used + bytes < space_info->total_bytes + avail) |
Josef Bacik | a80c8dc | 2012-09-06 16:59:33 -0400 | [diff] [blame] | 4069 | return 1; |
| 4070 | return 0; |
| 4071 | } |
| 4072 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 4073 | static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root, |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4074 | unsigned long nr_pages, int nr_items) |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4075 | { |
| 4076 | struct super_block *sb = root->fs_info->sb; |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4077 | |
Josef Bacik | 925a6ef | 2013-06-20 12:31:27 -0400 | [diff] [blame] | 4078 | if (down_read_trylock(&sb->s_umount)) { |
| 4079 | writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE); |
| 4080 | up_read(&sb->s_umount); |
| 4081 | } else { |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4082 | /* |
| 4083 | * We needn't worry the filesystem going from r/w to r/o though |
| 4084 | * we don't acquire ->s_umount mutex, because the filesystem |
| 4085 | * should guarantee the delalloc inodes list be empty after |
| 4086 | * the filesystem is readonly(all dirty pages are written to |
| 4087 | * the disk). |
| 4088 | */ |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4089 | btrfs_start_delalloc_roots(root->fs_info, 0, nr_items); |
Josef Bacik | 98ad69c | 2013-04-04 11:55:49 -0400 | [diff] [blame] | 4090 | if (!current->journal_info) |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4091 | btrfs_wait_ordered_roots(root->fs_info, nr_items); |
Miao Xie | da633a4 | 2012-12-20 11:19:09 +0000 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4095 | static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim) |
| 4096 | { |
| 4097 | u64 bytes; |
| 4098 | int nr; |
| 4099 | |
| 4100 | bytes = btrfs_calc_trans_metadata_size(root, 1); |
| 4101 | nr = (int)div64_u64(to_reclaim, bytes); |
| 4102 | if (!nr) |
| 4103 | nr = 1; |
| 4104 | return nr; |
| 4105 | } |
| 4106 | |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4107 | #define EXTENT_SIZE_PER_ITEM (256 * 1024) |
| 4108 | |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4109 | /* |
| 4110 | * shrink metadata reservation for delalloc |
| 4111 | */ |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4112 | static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, |
| 4113 | bool wait_ordered) |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4114 | { |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4115 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4116 | struct btrfs_space_info *space_info; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4117 | struct btrfs_trans_handle *trans; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4118 | u64 delalloc_bytes; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4119 | u64 max_reclaim; |
Josef Bacik | b1953bc | 2011-01-21 21:10:01 +0000 | [diff] [blame] | 4120 | long time_left; |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4121 | unsigned long nr_pages; |
| 4122 | int loops; |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4123 | int items; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4124 | enum btrfs_reserve_flush_enum flush; |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4125 | |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4126 | /* Calc the number of the pages we need flush for space reservation */ |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4127 | items = calc_reclaim_items_nr(root, to_reclaim); |
| 4128 | to_reclaim = items * EXTENT_SIZE_PER_ITEM; |
Miao Xie | c61a16a | 2013-11-04 23:13:23 +0800 | [diff] [blame] | 4129 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4130 | trans = (struct btrfs_trans_handle *)current->journal_info; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 4131 | block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4132 | space_info = block_rsv->space_info; |
Chris Mason | bf9022e | 2010-10-26 13:40:45 -0400 | [diff] [blame] | 4133 | |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4134 | delalloc_bytes = percpu_counter_sum_positive( |
| 4135 | &root->fs_info->delalloc_bytes); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4136 | if (delalloc_bytes == 0) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4137 | if (trans) |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4138 | return; |
Miao Xie | 38c135a | 2013-11-04 23:13:21 +0800 | [diff] [blame] | 4139 | if (wait_ordered) |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4140 | btrfs_wait_ordered_roots(root->fs_info, items); |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4141 | return; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4142 | } |
| 4143 | |
Miao Xie | d3ee29e3 | 2013-11-04 23:13:20 +0800 | [diff] [blame] | 4144 | loops = 0; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4145 | while (delalloc_bytes && loops < 3) { |
| 4146 | max_reclaim = min(delalloc_bytes, to_reclaim); |
| 4147 | nr_pages = max_reclaim >> PAGE_CACHE_SHIFT; |
Miao Xie | 6c255e6 | 2014-03-06 13:55:01 +0800 | [diff] [blame] | 4148 | btrfs_writeback_inodes_sb_nr(root, nr_pages, items); |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4149 | /* |
| 4150 | * We need to wait for the async pages to actually start before |
| 4151 | * we do anything. |
| 4152 | */ |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4153 | max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages); |
| 4154 | if (!max_reclaim) |
| 4155 | goto skip_async; |
Josef Bacik | dea31f5 | 2012-09-06 16:47:00 -0400 | [diff] [blame] | 4156 | |
Miao Xie | 9f3a074 | 2013-11-04 23:13:24 +0800 | [diff] [blame] | 4157 | if (max_reclaim <= nr_pages) |
| 4158 | max_reclaim = 0; |
| 4159 | else |
| 4160 | max_reclaim -= nr_pages; |
| 4161 | |
| 4162 | wait_event(root->fs_info->async_submit_wait, |
| 4163 | atomic_read(&root->fs_info->async_delalloc_pages) <= |
| 4164 | (int)max_reclaim); |
| 4165 | skip_async: |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4166 | if (!trans) |
| 4167 | flush = BTRFS_RESERVE_FLUSH_ALL; |
| 4168 | else |
| 4169 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4170 | spin_lock(&space_info->lock); |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4171 | if (can_overcommit(root, space_info, orig, flush)) { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4172 | spin_unlock(&space_info->lock); |
| 4173 | break; |
| 4174 | } |
Josef Bacik | 0019f10 | 2010-10-15 15:18:40 -0400 | [diff] [blame] | 4175 | spin_unlock(&space_info->lock); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4176 | |
Chris Mason | 36e39c4 | 2011-03-12 07:08:42 -0500 | [diff] [blame] | 4177 | loops++; |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4178 | if (wait_ordered && !trans) { |
Miao Xie | b024419 | 2013-11-04 23:13:25 +0800 | [diff] [blame] | 4179 | btrfs_wait_ordered_roots(root->fs_info, items); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4180 | } else { |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4181 | time_left = schedule_timeout_killable(1); |
Josef Bacik | f104d04 | 2011-10-14 13:56:58 -0400 | [diff] [blame] | 4182 | if (time_left) |
| 4183 | break; |
| 4184 | } |
Miao Xie | 963d678 | 2013-01-29 10:10:51 +0000 | [diff] [blame] | 4185 | delalloc_bytes = percpu_counter_sum_positive( |
| 4186 | &root->fs_info->delalloc_bytes); |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4187 | } |
Yan, Zheng | 5da9d01 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4188 | } |
| 4189 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4190 | /** |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4191 | * maybe_commit_transaction - possibly commit the transaction if its ok to |
| 4192 | * @root - the root we're allocating for |
| 4193 | * @bytes - the number of bytes we want to reserve |
| 4194 | * @force - force the commit |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4195 | * |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4196 | * This will check to make sure that committing the transaction will actually |
| 4197 | * get us somewhere and then commit the transaction if it does. Otherwise it |
| 4198 | * will return -ENOSPC. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4199 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4200 | static int may_commit_transaction(struct btrfs_root *root, |
| 4201 | struct btrfs_space_info *space_info, |
| 4202 | u64 bytes, int force) |
| 4203 | { |
| 4204 | struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv; |
| 4205 | struct btrfs_trans_handle *trans; |
| 4206 | |
| 4207 | trans = (struct btrfs_trans_handle *)current->journal_info; |
| 4208 | if (trans) |
| 4209 | return -EAGAIN; |
| 4210 | |
| 4211 | if (force) |
| 4212 | goto commit; |
| 4213 | |
| 4214 | /* See if there is enough pinned space to make this reservation */ |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4215 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
Miao Xie | 0424c54 | 2014-03-06 13:54:59 +0800 | [diff] [blame] | 4216 | bytes) >= 0) |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4217 | goto commit; |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4218 | |
| 4219 | /* |
| 4220 | * See if there is some space in the delayed insertion reservation for |
| 4221 | * this reservation. |
| 4222 | */ |
| 4223 | if (space_info != delayed_rsv->space_info) |
| 4224 | return -ENOSPC; |
| 4225 | |
| 4226 | spin_lock(&delayed_rsv->lock); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 4227 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
| 4228 | bytes - delayed_rsv->size) >= 0) { |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4229 | spin_unlock(&delayed_rsv->lock); |
| 4230 | return -ENOSPC; |
| 4231 | } |
| 4232 | spin_unlock(&delayed_rsv->lock); |
| 4233 | |
| 4234 | commit: |
| 4235 | trans = btrfs_join_transaction(root); |
| 4236 | if (IS_ERR(trans)) |
| 4237 | return -ENOSPC; |
| 4238 | |
| 4239 | return btrfs_commit_transaction(trans, root); |
| 4240 | } |
| 4241 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4242 | enum flush_state { |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4243 | FLUSH_DELAYED_ITEMS_NR = 1, |
| 4244 | FLUSH_DELAYED_ITEMS = 2, |
| 4245 | FLUSH_DELALLOC = 3, |
| 4246 | FLUSH_DELALLOC_WAIT = 4, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4247 | ALLOC_CHUNK = 5, |
| 4248 | COMMIT_TRANS = 6, |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4249 | }; |
| 4250 | |
| 4251 | static int flush_space(struct btrfs_root *root, |
| 4252 | struct btrfs_space_info *space_info, u64 num_bytes, |
| 4253 | u64 orig_bytes, int state) |
| 4254 | { |
| 4255 | struct btrfs_trans_handle *trans; |
| 4256 | int nr; |
Josef Bacik | f4c738c | 2012-07-02 17:10:51 -0400 | [diff] [blame] | 4257 | int ret = 0; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4258 | |
| 4259 | switch (state) { |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4260 | case FLUSH_DELAYED_ITEMS_NR: |
| 4261 | case FLUSH_DELAYED_ITEMS: |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4262 | if (state == FLUSH_DELAYED_ITEMS_NR) |
| 4263 | nr = calc_reclaim_items_nr(root, num_bytes) * 2; |
| 4264 | else |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4265 | nr = -1; |
Miao Xie | 18cd8ea | 2013-11-04 23:13:22 +0800 | [diff] [blame] | 4266 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4267 | trans = btrfs_join_transaction(root); |
| 4268 | if (IS_ERR(trans)) { |
| 4269 | ret = PTR_ERR(trans); |
| 4270 | break; |
| 4271 | } |
| 4272 | ret = btrfs_run_delayed_items_nr(trans, root, nr); |
| 4273 | btrfs_end_transaction(trans, root); |
| 4274 | break; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4275 | case FLUSH_DELALLOC: |
| 4276 | case FLUSH_DELALLOC_WAIT: |
Miao Xie | 24af7dd | 2014-03-06 13:55:00 +0800 | [diff] [blame] | 4277 | shrink_delalloc(root, num_bytes * 2, orig_bytes, |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4278 | state == FLUSH_DELALLOC_WAIT); |
| 4279 | break; |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4280 | case ALLOC_CHUNK: |
| 4281 | trans = btrfs_join_transaction(root); |
| 4282 | if (IS_ERR(trans)) { |
| 4283 | ret = PTR_ERR(trans); |
| 4284 | break; |
| 4285 | } |
| 4286 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 4287 | btrfs_get_alloc_profile(root, 0), |
| 4288 | CHUNK_ALLOC_NO_FORCE); |
| 4289 | btrfs_end_transaction(trans, root); |
| 4290 | if (ret == -ENOSPC) |
| 4291 | ret = 0; |
| 4292 | break; |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4293 | case COMMIT_TRANS: |
| 4294 | ret = may_commit_transaction(root, space_info, orig_bytes, 0); |
| 4295 | break; |
| 4296 | default: |
| 4297 | ret = -ENOSPC; |
| 4298 | break; |
| 4299 | } |
| 4300 | |
| 4301 | return ret; |
| 4302 | } |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4303 | |
| 4304 | static inline u64 |
| 4305 | btrfs_calc_reclaim_metadata_size(struct btrfs_root *root, |
| 4306 | struct btrfs_space_info *space_info) |
| 4307 | { |
| 4308 | u64 used; |
| 4309 | u64 expected; |
| 4310 | u64 to_reclaim; |
| 4311 | |
| 4312 | to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024, |
| 4313 | 16 * 1024 * 1024); |
| 4314 | spin_lock(&space_info->lock); |
| 4315 | if (can_overcommit(root, space_info, to_reclaim, |
| 4316 | BTRFS_RESERVE_FLUSH_ALL)) { |
| 4317 | to_reclaim = 0; |
| 4318 | goto out; |
| 4319 | } |
| 4320 | |
| 4321 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4322 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4323 | space_info->bytes_may_use; |
| 4324 | if (can_overcommit(root, space_info, 1024 * 1024, |
| 4325 | BTRFS_RESERVE_FLUSH_ALL)) |
| 4326 | expected = div_factor_fine(space_info->total_bytes, 95); |
| 4327 | else |
| 4328 | expected = div_factor_fine(space_info->total_bytes, 90); |
| 4329 | |
| 4330 | if (used > expected) |
| 4331 | to_reclaim = used - expected; |
| 4332 | else |
| 4333 | to_reclaim = 0; |
| 4334 | to_reclaim = min(to_reclaim, space_info->bytes_may_use + |
| 4335 | space_info->bytes_reserved); |
| 4336 | out: |
| 4337 | spin_unlock(&space_info->lock); |
| 4338 | |
| 4339 | return to_reclaim; |
| 4340 | } |
| 4341 | |
| 4342 | static inline int need_do_async_reclaim(struct btrfs_space_info *space_info, |
| 4343 | struct btrfs_fs_info *fs_info, u64 used) |
| 4344 | { |
| 4345 | return (used >= div_factor_fine(space_info->total_bytes, 98) && |
| 4346 | !btrfs_fs_closing(fs_info) && |
| 4347 | !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state)); |
| 4348 | } |
| 4349 | |
| 4350 | static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info, |
| 4351 | struct btrfs_fs_info *fs_info) |
| 4352 | { |
| 4353 | u64 used; |
| 4354 | |
| 4355 | spin_lock(&space_info->lock); |
| 4356 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4357 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4358 | space_info->bytes_may_use; |
| 4359 | if (need_do_async_reclaim(space_info, fs_info, used)) { |
| 4360 | spin_unlock(&space_info->lock); |
| 4361 | return 1; |
| 4362 | } |
| 4363 | spin_unlock(&space_info->lock); |
| 4364 | |
| 4365 | return 0; |
| 4366 | } |
| 4367 | |
| 4368 | static void btrfs_async_reclaim_metadata_space(struct work_struct *work) |
| 4369 | { |
| 4370 | struct btrfs_fs_info *fs_info; |
| 4371 | struct btrfs_space_info *space_info; |
| 4372 | u64 to_reclaim; |
| 4373 | int flush_state; |
| 4374 | |
| 4375 | fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work); |
| 4376 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 4377 | |
| 4378 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
| 4379 | space_info); |
| 4380 | if (!to_reclaim) |
| 4381 | return; |
| 4382 | |
| 4383 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
| 4384 | do { |
| 4385 | flush_space(fs_info->fs_root, space_info, to_reclaim, |
| 4386 | to_reclaim, flush_state); |
| 4387 | flush_state++; |
| 4388 | if (!btrfs_need_do_async_reclaim(space_info, fs_info)) |
| 4389 | return; |
| 4390 | } while (flush_state <= COMMIT_TRANS); |
| 4391 | |
| 4392 | if (btrfs_need_do_async_reclaim(space_info, fs_info)) |
| 4393 | queue_work(system_unbound_wq, work); |
| 4394 | } |
| 4395 | |
| 4396 | void btrfs_init_async_reclaim_work(struct work_struct *work) |
| 4397 | { |
| 4398 | INIT_WORK(work, btrfs_async_reclaim_metadata_space); |
| 4399 | } |
| 4400 | |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4401 | /** |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4402 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space |
| 4403 | * @root - the root we're allocating for |
| 4404 | * @block_rsv - the block_rsv we're allocating for |
| 4405 | * @orig_bytes - the number of bytes we want |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 4406 | * @flush - whether or not we can flush to make our reservation |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4407 | * |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4408 | * This will reserve orgi_bytes number of bytes from the space info associated |
| 4409 | * with the block_rsv. If there is not enough space it will make an attempt to |
| 4410 | * flush out space to make room. It will do this by flushing delalloc if |
| 4411 | * possible or committing the transaction. If flush is 0 then no attempts to |
| 4412 | * regain reservations will be made and this will fail if there is not enough |
| 4413 | * space already. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4414 | */ |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4415 | static int reserve_metadata_bytes(struct btrfs_root *root, |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4416 | struct btrfs_block_rsv *block_rsv, |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4417 | u64 orig_bytes, |
| 4418 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4419 | { |
| 4420 | struct btrfs_space_info *space_info = block_rsv->space_info; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4421 | u64 used; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4422 | u64 num_bytes = orig_bytes; |
Josef Bacik | 67b0fd6 | 2012-09-24 13:42:00 -0400 | [diff] [blame] | 4423 | int flush_state = FLUSH_DELAYED_ITEMS_NR; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4424 | int ret = 0; |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4425 | bool flushing = false; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4426 | |
| 4427 | again: |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4428 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4429 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4430 | /* |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4431 | * We only want to wait if somebody other than us is flushing and we |
| 4432 | * are actually allowed to flush all things. |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4433 | */ |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4434 | while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing && |
| 4435 | space_info->flush) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4436 | spin_unlock(&space_info->lock); |
| 4437 | /* |
| 4438 | * If we have a trans handle we can't wait because the flusher |
| 4439 | * may have to commit the transaction, which would mean we would |
| 4440 | * deadlock since we are waiting for the flusher to finish, but |
| 4441 | * hold the current transaction open. |
| 4442 | */ |
Josef Bacik | 663350a | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4443 | if (current->journal_info) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4444 | return -EAGAIN; |
Arne Jansen | b9688bb | 2012-04-18 10:27:16 +0200 | [diff] [blame] | 4445 | ret = wait_event_killable(space_info->wait, !space_info->flush); |
| 4446 | /* Must have been killed, return */ |
| 4447 | if (ret) |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4448 | return -EINTR; |
| 4449 | |
| 4450 | spin_lock(&space_info->lock); |
| 4451 | } |
| 4452 | |
| 4453 | ret = -ENOSPC; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4454 | used = space_info->bytes_used + space_info->bytes_reserved + |
| 4455 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 4456 | space_info->bytes_may_use; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4457 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4458 | /* |
| 4459 | * The idea here is that we've not already over-reserved the block group |
| 4460 | * then we can go ahead and save our reservation first and then start |
| 4461 | * flushing if we need to. Otherwise if we've already overcommitted |
| 4462 | * lets start flushing stuff first and then come back and try to make |
| 4463 | * our reservation. |
| 4464 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4465 | if (used <= space_info->total_bytes) { |
| 4466 | if (used + orig_bytes <= space_info->total_bytes) { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4467 | space_info->bytes_may_use += orig_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4468 | trace_btrfs_space_reservation(root->fs_info, |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4469 | "space_info", space_info->flags, orig_bytes, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4470 | ret = 0; |
| 4471 | } else { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4472 | /* |
| 4473 | * Ok set num_bytes to orig_bytes since we aren't |
| 4474 | * overocmmitted, this way we only try and reclaim what |
| 4475 | * we need. |
| 4476 | */ |
| 4477 | num_bytes = orig_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4478 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4479 | } else { |
| 4480 | /* |
| 4481 | * Ok we're over committed, set num_bytes to the overcommitted |
| 4482 | * amount plus the amount of bytes that we need for this |
| 4483 | * reservation. |
| 4484 | */ |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4485 | num_bytes = used - space_info->total_bytes + |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4486 | (orig_bytes * 2); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4487 | } |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4488 | |
Josef Bacik | 44734ed | 2012-09-28 16:04:19 -0400 | [diff] [blame] | 4489 | if (ret && can_overcommit(root, space_info, orig_bytes, flush)) { |
| 4490 | space_info->bytes_may_use += orig_bytes; |
| 4491 | trace_btrfs_space_reservation(root->fs_info, "space_info", |
| 4492 | space_info->flags, orig_bytes, |
| 4493 | 1); |
| 4494 | ret = 0; |
Josef Bacik | 2bf6475 | 2011-09-26 17:12:22 -0400 | [diff] [blame] | 4495 | } |
| 4496 | |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4497 | /* |
| 4498 | * Couldn't make our reservation, save our place so while we're trying |
| 4499 | * to reclaim space we can actually use it instead of somebody else |
| 4500 | * stealing it from us. |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4501 | * |
| 4502 | * We make the other tasks wait for the flush only when we can flush |
| 4503 | * all things. |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4504 | */ |
Josef Bacik | 72bcd99 | 2012-12-18 15:16:34 -0500 | [diff] [blame] | 4505 | if (ret && flush != BTRFS_RESERVE_NO_FLUSH) { |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4506 | flushing = true; |
| 4507 | space_info->flush = 1; |
Miao Xie | 21c7e75 | 2014-05-13 17:29:04 -0700 | [diff] [blame] | 4508 | } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
| 4509 | used += orig_bytes; |
| 4510 | if (need_do_async_reclaim(space_info, root->fs_info, used) && |
| 4511 | !work_busy(&root->fs_info->async_reclaim_work)) |
| 4512 | queue_work(system_unbound_wq, |
| 4513 | &root->fs_info->async_reclaim_work); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4514 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4515 | spin_unlock(&space_info->lock); |
| 4516 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4517 | if (!ret || flush == BTRFS_RESERVE_NO_FLUSH) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4518 | goto out; |
| 4519 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4520 | ret = flush_space(root, space_info, num_bytes, orig_bytes, |
| 4521 | flush_state); |
| 4522 | flush_state++; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4523 | |
| 4524 | /* |
| 4525 | * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock |
| 4526 | * would happen. So skip delalloc flush. |
| 4527 | */ |
| 4528 | if (flush == BTRFS_RESERVE_FLUSH_LIMIT && |
| 4529 | (flush_state == FLUSH_DELALLOC || |
| 4530 | flush_state == FLUSH_DELALLOC_WAIT)) |
| 4531 | flush_state = ALLOC_CHUNK; |
| 4532 | |
Josef Bacik | 96c3f43 | 2012-06-21 14:05:49 -0400 | [diff] [blame] | 4533 | if (!ret) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4534 | goto again; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4535 | else if (flush == BTRFS_RESERVE_FLUSH_LIMIT && |
| 4536 | flush_state < COMMIT_TRANS) |
| 4537 | goto again; |
| 4538 | else if (flush == BTRFS_RESERVE_FLUSH_ALL && |
| 4539 | flush_state <= COMMIT_TRANS) |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4540 | goto again; |
| 4541 | |
| 4542 | out: |
Josef Bacik | 5d80366 | 2013-02-07 16:06:02 -0500 | [diff] [blame] | 4543 | if (ret == -ENOSPC && |
| 4544 | unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) { |
| 4545 | struct btrfs_block_rsv *global_rsv = |
| 4546 | &root->fs_info->global_block_rsv; |
| 4547 | |
| 4548 | if (block_rsv != global_rsv && |
| 4549 | !block_rsv_use_bytes(global_rsv, orig_bytes)) |
| 4550 | ret = 0; |
| 4551 | } |
Jeff Mahoney | cab45e2 | 2013-10-16 16:27:01 -0400 | [diff] [blame] | 4552 | if (ret == -ENOSPC) |
| 4553 | trace_btrfs_space_reservation(root->fs_info, |
| 4554 | "space_info:enospc", |
| 4555 | space_info->flags, orig_bytes, 1); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4556 | if (flushing) { |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4557 | spin_lock(&space_info->lock); |
Josef Bacik | fdb5eff | 2011-06-07 16:07:44 -0400 | [diff] [blame] | 4558 | space_info->flush = 0; |
| 4559 | wake_up_all(&space_info->wait); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4560 | spin_unlock(&space_info->lock); |
| 4561 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4562 | return ret; |
| 4563 | } |
| 4564 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4565 | static struct btrfs_block_rsv *get_block_rsv( |
| 4566 | const struct btrfs_trans_handle *trans, |
| 4567 | const struct btrfs_root *root) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4568 | { |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 4569 | struct btrfs_block_rsv *block_rsv = NULL; |
| 4570 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 4571 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 4572 | block_rsv = trans->block_rsv; |
| 4573 | |
| 4574 | if (root == root->fs_info->csum_root && trans->adding_csums) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4575 | block_rsv = trans->block_rsv; |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 4576 | |
Stefan Behrens | f7a81ea | 2013-08-15 17:11:19 +0200 | [diff] [blame] | 4577 | if (root == root->fs_info->uuid_root) |
| 4578 | block_rsv = trans->block_rsv; |
| 4579 | |
Josef Bacik | 4c13d75 | 2011-08-30 11:31:29 -0400 | [diff] [blame] | 4580 | if (!block_rsv) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4581 | block_rsv = root->block_rsv; |
| 4582 | |
| 4583 | if (!block_rsv) |
| 4584 | block_rsv = &root->fs_info->empty_block_rsv; |
| 4585 | |
| 4586 | return block_rsv; |
| 4587 | } |
| 4588 | |
| 4589 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
| 4590 | u64 num_bytes) |
| 4591 | { |
| 4592 | int ret = -ENOSPC; |
| 4593 | spin_lock(&block_rsv->lock); |
| 4594 | if (block_rsv->reserved >= num_bytes) { |
| 4595 | block_rsv->reserved -= num_bytes; |
| 4596 | if (block_rsv->reserved < block_rsv->size) |
| 4597 | block_rsv->full = 0; |
| 4598 | ret = 0; |
| 4599 | } |
| 4600 | spin_unlock(&block_rsv->lock); |
| 4601 | return ret; |
| 4602 | } |
| 4603 | |
| 4604 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, |
| 4605 | u64 num_bytes, int update_size) |
| 4606 | { |
| 4607 | spin_lock(&block_rsv->lock); |
| 4608 | block_rsv->reserved += num_bytes; |
| 4609 | if (update_size) |
| 4610 | block_rsv->size += num_bytes; |
| 4611 | else if (block_rsv->reserved >= block_rsv->size) |
| 4612 | block_rsv->full = 1; |
| 4613 | spin_unlock(&block_rsv->lock); |
| 4614 | } |
| 4615 | |
Josef Bacik | d52be81 | 2013-05-29 14:54:47 -0400 | [diff] [blame] | 4616 | int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, |
| 4617 | struct btrfs_block_rsv *dest, u64 num_bytes, |
| 4618 | int min_factor) |
| 4619 | { |
| 4620 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
| 4621 | u64 min_bytes; |
| 4622 | |
| 4623 | if (global_rsv->space_info != dest->space_info) |
| 4624 | return -ENOSPC; |
| 4625 | |
| 4626 | spin_lock(&global_rsv->lock); |
| 4627 | min_bytes = div_factor(global_rsv->size, min_factor); |
| 4628 | if (global_rsv->reserved < min_bytes + num_bytes) { |
| 4629 | spin_unlock(&global_rsv->lock); |
| 4630 | return -ENOSPC; |
| 4631 | } |
| 4632 | global_rsv->reserved -= num_bytes; |
| 4633 | if (global_rsv->reserved < global_rsv->size) |
| 4634 | global_rsv->full = 0; |
| 4635 | spin_unlock(&global_rsv->lock); |
| 4636 | |
| 4637 | block_rsv_add_bytes(dest, num_bytes, 1); |
| 4638 | return 0; |
| 4639 | } |
| 4640 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4641 | static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, |
| 4642 | struct btrfs_block_rsv *block_rsv, |
David Sterba | 62a45b6 | 2011-04-20 15:52:26 +0200 | [diff] [blame] | 4643 | struct btrfs_block_rsv *dest, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4644 | { |
| 4645 | struct btrfs_space_info *space_info = block_rsv->space_info; |
| 4646 | |
| 4647 | spin_lock(&block_rsv->lock); |
| 4648 | if (num_bytes == (u64)-1) |
| 4649 | num_bytes = block_rsv->size; |
| 4650 | block_rsv->size -= num_bytes; |
| 4651 | if (block_rsv->reserved >= block_rsv->size) { |
| 4652 | num_bytes = block_rsv->reserved - block_rsv->size; |
| 4653 | block_rsv->reserved = block_rsv->size; |
| 4654 | block_rsv->full = 1; |
| 4655 | } else { |
| 4656 | num_bytes = 0; |
| 4657 | } |
| 4658 | spin_unlock(&block_rsv->lock); |
| 4659 | |
| 4660 | if (num_bytes > 0) { |
| 4661 | if (dest) { |
Josef Bacik | e9e2289 | 2011-01-24 21:43:19 +0000 | [diff] [blame] | 4662 | spin_lock(&dest->lock); |
| 4663 | if (!dest->full) { |
| 4664 | u64 bytes_to_add; |
| 4665 | |
| 4666 | bytes_to_add = dest->size - dest->reserved; |
| 4667 | bytes_to_add = min(num_bytes, bytes_to_add); |
| 4668 | dest->reserved += bytes_to_add; |
| 4669 | if (dest->reserved >= dest->size) |
| 4670 | dest->full = 1; |
| 4671 | num_bytes -= bytes_to_add; |
| 4672 | } |
| 4673 | spin_unlock(&dest->lock); |
| 4674 | } |
| 4675 | if (num_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4676 | spin_lock(&space_info->lock); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4677 | space_info->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4678 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4679 | space_info->flags, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4680 | spin_unlock(&space_info->lock); |
| 4681 | } |
| 4682 | } |
| 4683 | } |
| 4684 | |
| 4685 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
| 4686 | struct btrfs_block_rsv *dst, u64 num_bytes) |
| 4687 | { |
| 4688 | int ret; |
| 4689 | |
| 4690 | ret = block_rsv_use_bytes(src, num_bytes); |
| 4691 | if (ret) |
| 4692 | return ret; |
| 4693 | |
| 4694 | block_rsv_add_bytes(dst, num_bytes, 1); |
| 4695 | return 0; |
| 4696 | } |
| 4697 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4698 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4699 | { |
| 4700 | memset(rsv, 0, sizeof(*rsv)); |
| 4701 | spin_lock_init(&rsv->lock); |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4702 | rsv->type = type; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4703 | } |
| 4704 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4705 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root, |
| 4706 | unsigned short type) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4707 | { |
| 4708 | struct btrfs_block_rsv *block_rsv; |
| 4709 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4710 | |
| 4711 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
| 4712 | if (!block_rsv) |
| 4713 | return NULL; |
| 4714 | |
Miao Xie | 66d8f3d | 2012-09-06 04:02:28 -0600 | [diff] [blame] | 4715 | btrfs_init_block_rsv(block_rsv, type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4716 | block_rsv->space_info = __find_space_info(fs_info, |
| 4717 | BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4718 | return block_rsv; |
| 4719 | } |
| 4720 | |
| 4721 | void btrfs_free_block_rsv(struct btrfs_root *root, |
| 4722 | struct btrfs_block_rsv *rsv) |
| 4723 | { |
Josef Bacik | 2aaa665 | 2012-08-29 14:27:18 -0400 | [diff] [blame] | 4724 | if (!rsv) |
| 4725 | return; |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 4726 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 4727 | kfree(rsv); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4728 | } |
| 4729 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4730 | int btrfs_block_rsv_add(struct btrfs_root *root, |
| 4731 | struct btrfs_block_rsv *block_rsv, u64 num_bytes, |
| 4732 | enum btrfs_reserve_flush_enum flush) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4733 | { |
| 4734 | int ret; |
| 4735 | |
| 4736 | if (num_bytes == 0) |
| 4737 | return 0; |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 4738 | |
Miao Xie | 61b520a | 2011-11-10 20:45:05 -0500 | [diff] [blame] | 4739 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4740 | if (!ret) { |
| 4741 | block_rsv_add_bytes(block_rsv, num_bytes, 1); |
| 4742 | return 0; |
| 4743 | } |
| 4744 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4745 | return ret; |
| 4746 | } |
| 4747 | |
Josef Bacik | 4a92b1b | 2011-08-30 12:34:28 -0400 | [diff] [blame] | 4748 | int btrfs_block_rsv_check(struct btrfs_root *root, |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4749 | struct btrfs_block_rsv *block_rsv, int min_factor) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4750 | { |
| 4751 | u64 num_bytes = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4752 | int ret = -ENOSPC; |
| 4753 | |
| 4754 | if (!block_rsv) |
| 4755 | return 0; |
| 4756 | |
| 4757 | spin_lock(&block_rsv->lock); |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4758 | num_bytes = div_factor(block_rsv->size, min_factor); |
| 4759 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4760 | ret = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4761 | spin_unlock(&block_rsv->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4762 | |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4763 | return ret; |
| 4764 | } |
| 4765 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 4766 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
| 4767 | struct btrfs_block_rsv *block_rsv, u64 min_reserved, |
| 4768 | enum btrfs_reserve_flush_enum flush) |
Josef Bacik | 36ba022 | 2011-10-18 12:15:48 -0400 | [diff] [blame] | 4769 | { |
| 4770 | u64 num_bytes = 0; |
| 4771 | int ret = -ENOSPC; |
| 4772 | |
| 4773 | if (!block_rsv) |
| 4774 | return 0; |
| 4775 | |
| 4776 | spin_lock(&block_rsv->lock); |
| 4777 | num_bytes = min_reserved; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4778 | if (block_rsv->reserved >= num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4779 | ret = 0; |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4780 | else |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4781 | num_bytes -= block_rsv->reserved; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4782 | spin_unlock(&block_rsv->lock); |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4783 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4784 | if (!ret) |
| 4785 | return 0; |
| 4786 | |
Miao Xie | aa38a71 | 2011-11-18 17:43:00 +0800 | [diff] [blame] | 4787 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
Josef Bacik | dabdb64 | 2011-08-08 12:50:18 -0400 | [diff] [blame] | 4788 | if (!ret) { |
| 4789 | block_rsv_add_bytes(block_rsv, num_bytes, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4790 | return 0; |
| 4791 | } |
| 4792 | |
Josef Bacik | 13553e5 | 2011-08-08 13:33:21 -0400 | [diff] [blame] | 4793 | return ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, |
| 4797 | struct btrfs_block_rsv *dst_rsv, |
| 4798 | u64 num_bytes) |
| 4799 | { |
| 4800 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4801 | } |
| 4802 | |
| 4803 | void btrfs_block_rsv_release(struct btrfs_root *root, |
| 4804 | struct btrfs_block_rsv *block_rsv, |
| 4805 | u64 num_bytes) |
| 4806 | { |
| 4807 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Liu Bo | 1750458 | 2013-12-29 21:44:50 +0800 | [diff] [blame] | 4808 | if (global_rsv == block_rsv || |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4809 | block_rsv->space_info != global_rsv->space_info) |
| 4810 | global_rsv = NULL; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4811 | block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv, |
| 4812 | num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4813 | } |
| 4814 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4815 | /* |
| 4816 | * helper to calculate size of global block reservation. |
| 4817 | * the desired value is sum of space used by extent tree, |
| 4818 | * checksum tree and root tree |
| 4819 | */ |
| 4820 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
| 4821 | { |
| 4822 | struct btrfs_space_info *sinfo; |
| 4823 | u64 num_bytes; |
| 4824 | u64 meta_used; |
| 4825 | u64 data_used; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 4826 | int csum_size = btrfs_super_csum_size(fs_info->super_copy); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4827 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4828 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
| 4829 | spin_lock(&sinfo->lock); |
| 4830 | data_used = sinfo->bytes_used; |
| 4831 | spin_unlock(&sinfo->lock); |
| 4832 | |
| 4833 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
| 4834 | spin_lock(&sinfo->lock); |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 4835 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
| 4836 | data_used = 0; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4837 | meta_used = sinfo->bytes_used; |
| 4838 | spin_unlock(&sinfo->lock); |
| 4839 | |
| 4840 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
| 4841 | csum_size * 2; |
| 4842 | num_bytes += div64_u64(data_used + meta_used, 50); |
| 4843 | |
| 4844 | if (num_bytes * 3 > meta_used) |
Chris Mason | 8e62c2d | 2012-04-12 13:46:48 -0400 | [diff] [blame] | 4845 | num_bytes = div64_u64(meta_used, 3); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4846 | |
| 4847 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
| 4848 | } |
| 4849 | |
| 4850 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4851 | { |
| 4852 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
| 4853 | struct btrfs_space_info *sinfo = block_rsv->space_info; |
| 4854 | u64 num_bytes; |
| 4855 | |
| 4856 | num_bytes = calc_global_metadata_size(fs_info); |
| 4857 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4858 | spin_lock(&sinfo->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 4859 | spin_lock(&block_rsv->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4860 | |
Josef Bacik | fdf30d1 | 2013-03-26 15:31:45 -0400 | [diff] [blame] | 4861 | block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4862 | |
| 4863 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
Josef Bacik | 6d48755 | 2010-10-15 15:13:32 -0400 | [diff] [blame] | 4864 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
| 4865 | sinfo->bytes_may_use; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4866 | |
| 4867 | if (sinfo->total_bytes > num_bytes) { |
| 4868 | num_bytes = sinfo->total_bytes - num_bytes; |
| 4869 | block_rsv->reserved += num_bytes; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4870 | sinfo->bytes_may_use += num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4871 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4872 | sinfo->flags, num_bytes, 1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | if (block_rsv->reserved >= block_rsv->size) { |
| 4876 | num_bytes = block_rsv->reserved - block_rsv->size; |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 4877 | sinfo->bytes_may_use -= num_bytes; |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4878 | trace_btrfs_space_reservation(fs_info, "space_info", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4879 | sinfo->flags, num_bytes, 0); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4880 | block_rsv->reserved = block_rsv->size; |
| 4881 | block_rsv->full = 1; |
| 4882 | } |
David Sterba | 182608c | 2011-05-05 13:13:16 +0200 | [diff] [blame] | 4883 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4884 | spin_unlock(&block_rsv->lock); |
Stefan Behrens | 1f699d3 | 2012-04-27 12:41:46 -0400 | [diff] [blame] | 4885 | spin_unlock(&sinfo->lock); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4886 | } |
| 4887 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4888 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4889 | { |
| 4890 | struct btrfs_space_info *space_info; |
| 4891 | |
| 4892 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
| 4893 | fs_info->chunk_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4894 | |
| 4895 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4896 | fs_info->global_block_rsv.space_info = space_info; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4897 | fs_info->delalloc_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4898 | fs_info->trans_block_rsv.space_info = space_info; |
| 4899 | fs_info->empty_block_rsv.space_info = space_info; |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4900 | fs_info->delayed_block_rsv.space_info = space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4901 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4902 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
| 4903 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; |
| 4904 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; |
| 4905 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; |
Stefan Behrens | 3a6cad9 | 2013-05-16 14:48:19 +0000 | [diff] [blame] | 4906 | if (fs_info->quota_root) |
| 4907 | fs_info->quota_root->block_rsv = &fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4908 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4909 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4910 | update_global_block_rsv(fs_info); |
| 4911 | } |
| 4912 | |
| 4913 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
| 4914 | { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4915 | block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, |
| 4916 | (u64)-1); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4917 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
| 4918 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); |
| 4919 | WARN_ON(fs_info->trans_block_rsv.size > 0); |
| 4920 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); |
| 4921 | WARN_ON(fs_info->chunk_block_rsv.size > 0); |
| 4922 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); |
Josef Bacik | 6d668dd | 2011-11-03 22:54:25 -0400 | [diff] [blame] | 4923 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
| 4924 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4925 | } |
| 4926 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4927 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
| 4928 | struct btrfs_root *root) |
| 4929 | { |
Josef Bacik | 0e72110 | 2012-06-26 16:13:18 -0400 | [diff] [blame] | 4930 | if (!trans->block_rsv) |
| 4931 | return; |
| 4932 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4933 | if (!trans->bytes_reserved) |
| 4934 | return; |
| 4935 | |
Chris Mason | e77266e | 2012-02-24 10:39:05 -0500 | [diff] [blame] | 4936 | trace_btrfs_space_reservation(root->fs_info, "transaction", |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 4937 | trans->transid, trans->bytes_reserved, 0); |
Josef Bacik | b24e03d | 2011-10-14 14:40:17 -0400 | [diff] [blame] | 4938 | btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4939 | trans->bytes_reserved = 0; |
| 4940 | } |
| 4941 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4942 | /* Can only return 0 or -ENOSPC */ |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4943 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
| 4944 | struct inode *inode) |
| 4945 | { |
| 4946 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 4947 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); |
| 4948 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
| 4949 | |
| 4950 | /* |
Josef Bacik | fcb80c2 | 2011-05-03 10:40:22 -0400 | [diff] [blame] | 4951 | * We need to hold space in order to delete our orphan item once we've |
| 4952 | * added it, so this takes the reservation so we can release it later |
| 4953 | * when we are truly done with the orphan item. |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4954 | */ |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4955 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4956 | trace_btrfs_space_reservation(root->fs_info, "orphan", |
| 4957 | btrfs_ino(inode), num_bytes, 1); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4958 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
| 4959 | } |
| 4960 | |
| 4961 | void btrfs_orphan_release_metadata(struct inode *inode) |
| 4962 | { |
| 4963 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | ff5714c | 2011-05-28 07:00:39 -0400 | [diff] [blame] | 4964 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 4965 | trace_btrfs_space_reservation(root->fs_info, "orphan", |
| 4966 | btrfs_ino(inode), num_bytes, 0); |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 4967 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
| 4968 | } |
| 4969 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 4970 | /* |
| 4971 | * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation |
| 4972 | * root: the root of the parent directory |
| 4973 | * rsv: block reservation |
| 4974 | * items: the number of items that we need do reservation |
| 4975 | * qgroup_reserved: used to return the reserved size in qgroup |
| 4976 | * |
| 4977 | * This function is used to reserve the space for snapshot/subvolume |
| 4978 | * creation and deletion. Those operations are different with the |
| 4979 | * common file/directory operations, they change two fs/file trees |
| 4980 | * and root tree, the number of items that the qgroup reserves is |
| 4981 | * different with the free space reservation. So we can not use |
| 4982 | * the space reseravtion mechanism in start_transaction(). |
| 4983 | */ |
| 4984 | int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, |
| 4985 | struct btrfs_block_rsv *rsv, |
| 4986 | int items, |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 4987 | u64 *qgroup_reserved, |
| 4988 | bool use_global_rsv) |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 4989 | { |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 4990 | u64 num_bytes; |
| 4991 | int ret; |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 4992 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 4993 | |
| 4994 | if (root->fs_info->quota_enabled) { |
| 4995 | /* One for parent inode, two for dir entries */ |
| 4996 | num_bytes = 3 * root->leafsize; |
| 4997 | ret = btrfs_qgroup_reserve(root, num_bytes); |
| 4998 | if (ret) |
| 4999 | return ret; |
| 5000 | } else { |
| 5001 | num_bytes = 0; |
| 5002 | } |
| 5003 | |
| 5004 | *qgroup_reserved = num_bytes; |
| 5005 | |
| 5006 | num_bytes = btrfs_calc_trans_metadata_size(root, items); |
| 5007 | rsv->space_info = __find_space_info(root->fs_info, |
| 5008 | BTRFS_BLOCK_GROUP_METADATA); |
| 5009 | ret = btrfs_block_rsv_add(root, rsv, num_bytes, |
| 5010 | BTRFS_RESERVE_FLUSH_ALL); |
Jeff Mahoney | ee3441b | 2013-07-09 16:37:21 -0400 | [diff] [blame] | 5011 | |
| 5012 | if (ret == -ENOSPC && use_global_rsv) |
| 5013 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes); |
| 5014 | |
Miao Xie | d5c1207 | 2013-02-28 10:04:33 +0000 | [diff] [blame] | 5015 | if (ret) { |
| 5016 | if (*qgroup_reserved) |
| 5017 | btrfs_qgroup_free(root, *qgroup_reserved); |
| 5018 | } |
| 5019 | |
| 5020 | return ret; |
| 5021 | } |
| 5022 | |
| 5023 | void btrfs_subvolume_release_metadata(struct btrfs_root *root, |
| 5024 | struct btrfs_block_rsv *rsv, |
| 5025 | u64 qgroup_reserved) |
| 5026 | { |
| 5027 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
| 5028 | if (qgroup_reserved) |
| 5029 | btrfs_qgroup_free(root, qgroup_reserved); |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 5030 | } |
| 5031 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5032 | /** |
| 5033 | * drop_outstanding_extent - drop an outstanding extent |
| 5034 | * @inode: the inode we're dropping the extent for |
| 5035 | * |
| 5036 | * This is called when we are freeing up an outstanding extent, either called |
| 5037 | * after an error or after an extent is written. This will return the number of |
| 5038 | * reserved extents that need to be freed. This must be called with |
| 5039 | * BTRFS_I(inode)->lock held. |
| 5040 | */ |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5041 | static unsigned drop_outstanding_extent(struct inode *inode) |
| 5042 | { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5043 | unsigned drop_inode_space = 0; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5044 | unsigned dropped_extents = 0; |
| 5045 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5046 | BUG_ON(!BTRFS_I(inode)->outstanding_extents); |
| 5047 | BTRFS_I(inode)->outstanding_extents--; |
| 5048 | |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5049 | if (BTRFS_I(inode)->outstanding_extents == 0 && |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5050 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 5051 | &BTRFS_I(inode)->runtime_flags)) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5052 | drop_inode_space = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5053 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5054 | /* |
| 5055 | * If we have more or the same amount of outsanding extents than we have |
| 5056 | * reserved then we need to leave the reserved extents count alone. |
| 5057 | */ |
| 5058 | if (BTRFS_I(inode)->outstanding_extents >= |
| 5059 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5060 | return drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5061 | |
| 5062 | dropped_extents = BTRFS_I(inode)->reserved_extents - |
| 5063 | BTRFS_I(inode)->outstanding_extents; |
| 5064 | BTRFS_I(inode)->reserved_extents -= dropped_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5065 | return dropped_extents + drop_inode_space; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5066 | } |
| 5067 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5068 | /** |
| 5069 | * calc_csum_metadata_size - return the amount of metada space that must be |
| 5070 | * reserved/free'd for the given bytes. |
| 5071 | * @inode: the inode we're manipulating |
| 5072 | * @num_bytes: the number of bytes in question |
| 5073 | * @reserve: 1 if we are reserving space, 0 if we are freeing space |
| 5074 | * |
| 5075 | * This adjusts the number of csum_bytes in the inode and then returns the |
| 5076 | * correct amount of metadata that must either be reserved or freed. We |
| 5077 | * calculate how many checksums we can fit into one leaf and then divide the |
| 5078 | * number of bytes that will need to be checksumed by this value to figure out |
| 5079 | * how many checksums will be required. If we are adding bytes then the number |
| 5080 | * may go up and we will return the number of additional bytes that must be |
| 5081 | * reserved. If it is going down we will return the number of bytes that must |
| 5082 | * be freed. |
| 5083 | * |
| 5084 | * This must be called with BTRFS_I(inode)->lock held. |
| 5085 | */ |
| 5086 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, |
| 5087 | int reserve) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5088 | { |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5089 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5090 | u64 csum_size; |
| 5091 | int num_csums_per_leaf; |
| 5092 | int num_csums; |
| 5093 | int old_csums; |
| 5094 | |
| 5095 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && |
| 5096 | BTRFS_I(inode)->csum_bytes == 0) |
| 5097 | return 0; |
| 5098 | |
| 5099 | old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 5100 | if (reserve) |
| 5101 | BTRFS_I(inode)->csum_bytes += num_bytes; |
| 5102 | else |
| 5103 | BTRFS_I(inode)->csum_bytes -= num_bytes; |
| 5104 | csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item); |
| 5105 | num_csums_per_leaf = (int)div64_u64(csum_size, |
| 5106 | sizeof(struct btrfs_csum_item) + |
| 5107 | sizeof(struct btrfs_disk_key)); |
| 5108 | num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); |
| 5109 | num_csums = num_csums + num_csums_per_leaf - 1; |
| 5110 | num_csums = num_csums / num_csums_per_leaf; |
| 5111 | |
| 5112 | old_csums = old_csums + num_csums_per_leaf - 1; |
| 5113 | old_csums = old_csums / num_csums_per_leaf; |
| 5114 | |
| 5115 | /* No change, no need to reserve more */ |
| 5116 | if (old_csums == num_csums) |
| 5117 | return 0; |
| 5118 | |
| 5119 | if (reserve) |
| 5120 | return btrfs_calc_trans_metadata_size(root, |
| 5121 | num_csums - old_csums); |
| 5122 | |
| 5123 | return btrfs_calc_trans_metadata_size(root, old_csums - num_csums); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
| 5127 | { |
| 5128 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 5129 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5130 | u64 to_reserve = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5131 | u64 csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5132 | unsigned nr_extents = 0; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5133 | int extra_reserve = 0; |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5134 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; |
Jan Schmidt | eb6b88d | 2013-01-27 23:26:00 -0700 | [diff] [blame] | 5135 | int ret = 0; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5136 | bool delalloc_lock = true; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5137 | u64 to_free = 0; |
| 5138 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5139 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5140 | /* If we are a free space inode we need to not flush since we will be in |
| 5141 | * the middle of a transaction commit. We also don't need the delalloc |
| 5142 | * mutex since we won't race with anybody. We need this mostly to make |
| 5143 | * lockdep shut its filthy mouth. |
| 5144 | */ |
| 5145 | if (btrfs_is_free_space_inode(inode)) { |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5146 | flush = BTRFS_RESERVE_NO_FLUSH; |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5147 | delalloc_lock = false; |
| 5148 | } |
Josef Bacik | c09544e | 2011-08-30 10:19:10 -0400 | [diff] [blame] | 5149 | |
Miao Xie | 08e007d | 2012-10-16 11:33:38 +0000 | [diff] [blame] | 5150 | if (flush != BTRFS_RESERVE_NO_FLUSH && |
| 5151 | btrfs_transaction_in_commit(root->fs_info)) |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5152 | schedule_timeout(1); |
| 5153 | |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5154 | if (delalloc_lock) |
| 5155 | mutex_lock(&BTRFS_I(inode)->delalloc_mutex); |
| 5156 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5157 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 8bb8ab2 | 2010-10-15 16:52:49 -0400 | [diff] [blame] | 5158 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5159 | spin_lock(&BTRFS_I(inode)->lock); |
| 5160 | BTRFS_I(inode)->outstanding_extents++; |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5161 | |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5162 | if (BTRFS_I(inode)->outstanding_extents > |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5163 | BTRFS_I(inode)->reserved_extents) |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5164 | nr_extents = BTRFS_I(inode)->outstanding_extents - |
| 5165 | BTRFS_I(inode)->reserved_extents; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5166 | |
| 5167 | /* |
| 5168 | * Add an item to reserve for updating the inode when we complete the |
| 5169 | * delalloc io. |
| 5170 | */ |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5171 | if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 5172 | &BTRFS_I(inode)->runtime_flags)) { |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5173 | nr_extents++; |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5174 | extra_reserve = 1; |
Josef Bacik | 7fd2ae2 | 2011-11-08 15:47:34 -0500 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5178 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5179 | csum_bytes = BTRFS_I(inode)->csum_bytes; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5180 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 57a45ced | 2011-01-25 16:30:38 -0500 | [diff] [blame] | 5181 | |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5182 | if (root->fs_info->quota_enabled) { |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5183 | ret = btrfs_qgroup_reserve(root, num_bytes + |
| 5184 | nr_extents * root->leafsize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5185 | if (ret) |
| 5186 | goto out_fail; |
Wang Shilong | a9870c0 | 2013-03-01 11:33:01 +0000 | [diff] [blame] | 5187 | } |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5188 | |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5189 | ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush); |
| 5190 | if (unlikely(ret)) { |
| 5191 | if (root->fs_info->quota_enabled) |
Miao Xie | 4b5829a | 2012-12-05 10:53:25 +0000 | [diff] [blame] | 5192 | btrfs_qgroup_free(root, num_bytes + |
| 5193 | nr_extents * root->leafsize); |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5194 | goto out_fail; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5195 | } |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5196 | |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5197 | spin_lock(&BTRFS_I(inode)->lock); |
| 5198 | if (extra_reserve) { |
Josef Bacik | 72ac3c0 | 2012-05-23 14:13:11 -0400 | [diff] [blame] | 5199 | set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
| 5200 | &BTRFS_I(inode)->runtime_flags); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5201 | nr_extents--; |
| 5202 | } |
| 5203 | BTRFS_I(inode)->reserved_extents += nr_extents; |
| 5204 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | c64c2bd | 2012-12-14 13:48:14 -0500 | [diff] [blame] | 5205 | |
| 5206 | if (delalloc_lock) |
| 5207 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
Josef Bacik | 660d3f6 | 2011-12-09 11:18:51 -0500 | [diff] [blame] | 5208 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5209 | if (to_reserve) |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 5210 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5211 | btrfs_ino(inode), to_reserve, 1); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5212 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
| 5213 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5214 | return 0; |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5215 | |
| 5216 | out_fail: |
| 5217 | spin_lock(&BTRFS_I(inode)->lock); |
| 5218 | dropped = drop_outstanding_extent(inode); |
| 5219 | /* |
| 5220 | * If the inodes csum_bytes is the same as the original |
| 5221 | * csum_bytes then we know we haven't raced with any free()ers |
| 5222 | * so we can just reduce our inodes csum bytes and carry on. |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5223 | */ |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 5224 | if (BTRFS_I(inode)->csum_bytes == csum_bytes) { |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5225 | calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | f4881bc | 2013-03-25 16:03:35 -0400 | [diff] [blame] | 5226 | } else { |
| 5227 | u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes; |
| 5228 | u64 bytes; |
| 5229 | |
| 5230 | /* |
| 5231 | * This is tricky, but first we need to figure out how much we |
| 5232 | * free'd from any free-ers that occured during this |
| 5233 | * reservation, so we reset ->csum_bytes to the csum_bytes |
| 5234 | * before we dropped our lock, and then call the free for the |
| 5235 | * number of bytes that were freed while we were trying our |
| 5236 | * reservation. |
| 5237 | */ |
| 5238 | bytes = csum_bytes - BTRFS_I(inode)->csum_bytes; |
| 5239 | BTRFS_I(inode)->csum_bytes = csum_bytes; |
| 5240 | to_free = calc_csum_metadata_size(inode, bytes, 0); |
| 5241 | |
| 5242 | |
| 5243 | /* |
| 5244 | * Now we need to see how much we would have freed had we not |
| 5245 | * been making this reservation and our ->csum_bytes were not |
| 5246 | * artificially inflated. |
| 5247 | */ |
| 5248 | BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes; |
| 5249 | bytes = csum_bytes - orig_csum_bytes; |
| 5250 | bytes = calc_csum_metadata_size(inode, bytes, 0); |
| 5251 | |
| 5252 | /* |
| 5253 | * Now reset ->csum_bytes to what it should be. If bytes is |
| 5254 | * more than to_free then we would have free'd more space had we |
| 5255 | * not had an artificially high ->csum_bytes, so we need to free |
| 5256 | * the remainder. If bytes is the same or less then we don't |
| 5257 | * need to do anything, the other free-ers did the correct |
| 5258 | * thing. |
| 5259 | */ |
| 5260 | BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes; |
| 5261 | if (bytes > to_free) |
| 5262 | to_free = bytes - to_free; |
| 5263 | else |
| 5264 | to_free = 0; |
| 5265 | } |
Wang Shilong | 88e081bf | 2013-03-01 11:36:01 +0000 | [diff] [blame] | 5266 | spin_unlock(&BTRFS_I(inode)->lock); |
| 5267 | if (dropped) |
| 5268 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
| 5269 | |
| 5270 | if (to_free) { |
| 5271 | btrfs_block_rsv_release(root, block_rsv, to_free); |
| 5272 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
| 5273 | btrfs_ino(inode), to_free, 0); |
| 5274 | } |
| 5275 | if (delalloc_lock) |
| 5276 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); |
| 5277 | return ret; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5278 | } |
| 5279 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5280 | /** |
| 5281 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode |
| 5282 | * @inode: the inode to release the reservation for |
| 5283 | * @num_bytes: the number of bytes we're releasing |
| 5284 | * |
| 5285 | * This will release the metadata reservation for an inode. This can be called |
| 5286 | * once we complete IO for a given set of bytes to release their metadata |
| 5287 | * reservations. |
| 5288 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5289 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
| 5290 | { |
| 5291 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5292 | u64 to_free = 0; |
| 5293 | unsigned dropped; |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5294 | |
| 5295 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5296 | spin_lock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5297 | dropped = drop_outstanding_extent(inode); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5298 | |
Miao Xie | 0934856 | 2013-02-07 10:12:07 +0000 | [diff] [blame] | 5299 | if (num_bytes) |
| 5300 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5301 | spin_unlock(&BTRFS_I(inode)->lock); |
Josef Bacik | 9e0baf6 | 2011-07-15 15:16:44 +0000 | [diff] [blame] | 5302 | if (dropped > 0) |
| 5303 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5304 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5305 | trace_btrfs_space_reservation(root->fs_info, "delalloc", |
| 5306 | btrfs_ino(inode), to_free, 0); |
Arne Jansen | c556723 | 2011-09-14 15:44:05 +0200 | [diff] [blame] | 5307 | if (root->fs_info->quota_enabled) { |
| 5308 | btrfs_qgroup_free(root, num_bytes + |
| 5309 | dropped * root->leafsize); |
| 5310 | } |
| 5311 | |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5312 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, |
| 5313 | to_free); |
| 5314 | } |
| 5315 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5316 | /** |
| 5317 | * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc |
| 5318 | * @inode: inode we're writing to |
| 5319 | * @num_bytes: the number of bytes we want to allocate |
| 5320 | * |
| 5321 | * This will do the following things |
| 5322 | * |
| 5323 | * o reserve space in the data space info for num_bytes |
| 5324 | * o reserve space in the metadata space info based on number of outstanding |
| 5325 | * extents and how much csums will be needed |
| 5326 | * o add to the inodes ->delalloc_bytes |
| 5327 | * o add it to the fs_info's delalloc inodes list. |
| 5328 | * |
| 5329 | * This will return 0 for success and -ENOSPC if there is no space left. |
| 5330 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5331 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
| 5332 | { |
| 5333 | int ret; |
| 5334 | |
| 5335 | ret = btrfs_check_data_free_space(inode, num_bytes); |
| 5336 | if (ret) |
| 5337 | return ret; |
| 5338 | |
| 5339 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); |
| 5340 | if (ret) { |
| 5341 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 5342 | return ret; |
| 5343 | } |
| 5344 | |
| 5345 | return 0; |
| 5346 | } |
| 5347 | |
Josef Bacik | 7709cde | 2011-08-04 10:25:02 -0400 | [diff] [blame] | 5348 | /** |
| 5349 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
| 5350 | * @inode: inode we're releasing space for |
| 5351 | * @num_bytes: the number of bytes we want to free up |
| 5352 | * |
| 5353 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is |
| 5354 | * called in the case that we don't need the metadata AND data reservations |
| 5355 | * anymore. So if there is an error or we insert an inline extent. |
| 5356 | * |
| 5357 | * This function will release the metadata space that was not used and will |
| 5358 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes |
| 5359 | * list if there are no delalloc bytes left. |
| 5360 | */ |
Yan, Zheng | 0ca1f7c | 2010-05-16 10:48:47 -0400 | [diff] [blame] | 5361 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
| 5362 | { |
| 5363 | btrfs_delalloc_release_metadata(inode, num_bytes); |
| 5364 | btrfs_free_reserved_data_space(inode, num_bytes); |
| 5365 | } |
| 5366 | |
Liu Bo | c53d613 | 2012-12-27 09:01:19 +0000 | [diff] [blame] | 5367 | static int update_block_group(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5368 | u64 bytenr, u64 num_bytes, int alloc) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5369 | { |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5370 | struct btrfs_block_group_cache *cache = NULL; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5371 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5372 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5373 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5374 | u64 byte_in_group; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5375 | int factor; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 5376 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5377 | /* block accounting for super block */ |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 5378 | spin_lock(&info->delalloc_root_lock); |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5379 | old_val = btrfs_super_bytes_used(info->super_copy); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5380 | if (alloc) |
| 5381 | old_val += num_bytes; |
| 5382 | else |
| 5383 | old_val -= num_bytes; |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 5384 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
Miao Xie | eb73c1b | 2013-05-15 07:48:22 +0000 | [diff] [blame] | 5385 | spin_unlock(&info->delalloc_root_lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5386 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5387 | while (total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5388 | cache = btrfs_lookup_block_group(info, bytenr); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5389 | if (!cache) |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5390 | return -ENOENT; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5391 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 5392 | BTRFS_BLOCK_GROUP_RAID1 | |
| 5393 | BTRFS_BLOCK_GROUP_RAID10)) |
| 5394 | factor = 2; |
| 5395 | else |
| 5396 | factor = 1; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 5397 | /* |
| 5398 | * If this block group has free space cache written out, we |
| 5399 | * need to make sure to load it if we are removing space. This |
| 5400 | * is because we need the unpinning stage to actually add the |
| 5401 | * space back to the block group, otherwise we will leak space. |
| 5402 | */ |
| 5403 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 5404 | cache_block_group(cache, 1); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5405 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5406 | byte_in_group = bytenr - cache->key.objectid; |
| 5407 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5408 | |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5409 | spin_lock(&cache->space_info->lock); |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5410 | spin_lock(&cache->lock); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5411 | |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 5412 | if (btrfs_test_opt(root, SPACE_CACHE) && |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 5413 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
| 5414 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
| 5415 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5416 | cache->dirty = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5417 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5418 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5419 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5420 | old_val += num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5421 | btrfs_set_block_group_used(&cache->item, old_val); |
| 5422 | cache->reserved -= num_bytes; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5423 | cache->space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5424 | cache->space_info->bytes_used += num_bytes; |
| 5425 | cache->space_info->disk_used += num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5426 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5427 | spin_unlock(&cache->space_info->lock); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5428 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5429 | old_val -= num_bytes; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5430 | btrfs_set_block_group_used(&cache->item, old_val); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5431 | cache->pinned += num_bytes; |
| 5432 | cache->space_info->bytes_pinned += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5433 | cache->space_info->bytes_used -= num_bytes; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 5434 | cache->space_info->disk_used -= num_bytes * factor; |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 5435 | spin_unlock(&cache->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5436 | spin_unlock(&cache->space_info->lock); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5437 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5438 | set_extent_dirty(info->pinned_extents, |
| 5439 | bytenr, bytenr + num_bytes - 1, |
| 5440 | GFP_NOFS | __GFP_NOFAIL); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 5441 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5442 | btrfs_put_block_group(cache); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5443 | total -= num_bytes; |
| 5444 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5445 | } |
| 5446 | return 0; |
| 5447 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 5448 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5449 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
| 5450 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5451 | struct btrfs_block_group_cache *cache; |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5452 | u64 bytenr; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5453 | |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 5454 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 5455 | bytenr = root->fs_info->first_logical_byte; |
| 5456 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 5457 | |
| 5458 | if (bytenr < (u64)-1) |
| 5459 | return bytenr; |
| 5460 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5461 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); |
| 5462 | if (!cache) |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5463 | return 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 5464 | |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5465 | bytenr = cache->key.objectid; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5466 | btrfs_put_block_group(cache); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 5467 | |
| 5468 | return bytenr; |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 5469 | } |
| 5470 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5471 | static int pin_down_extent(struct btrfs_root *root, |
| 5472 | struct btrfs_block_group_cache *cache, |
| 5473 | u64 bytenr, u64 num_bytes, int reserved) |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 5474 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5475 | spin_lock(&cache->space_info->lock); |
| 5476 | spin_lock(&cache->lock); |
| 5477 | cache->pinned += num_bytes; |
| 5478 | cache->space_info->bytes_pinned += num_bytes; |
| 5479 | if (reserved) { |
| 5480 | cache->reserved -= num_bytes; |
| 5481 | cache->space_info->bytes_reserved -= num_bytes; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 5482 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5483 | spin_unlock(&cache->lock); |
| 5484 | spin_unlock(&cache->space_info->lock); |
| 5485 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5486 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
| 5487 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 5488 | if (reserved) |
| 5489 | trace_btrfs_reserved_extent_free(root, bytenr, num_bytes); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 5490 | return 0; |
| 5491 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 5492 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5493 | /* |
| 5494 | * this function must be called within transaction |
| 5495 | */ |
| 5496 | int btrfs_pin_extent(struct btrfs_root *root, |
| 5497 | u64 bytenr, u64 num_bytes, int reserved) |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5498 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5499 | struct btrfs_block_group_cache *cache; |
| 5500 | |
| 5501 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5502 | BUG_ON(!cache); /* Logic error */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5503 | |
| 5504 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); |
| 5505 | |
| 5506 | btrfs_put_block_group(cache); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5507 | return 0; |
| 5508 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 5509 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5510 | /* |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5511 | * this function must be called within transaction |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5512 | */ |
Liu Bo | dcfac41 | 2012-12-27 09:01:20 +0000 | [diff] [blame] | 5513 | int btrfs_pin_extent_for_log_replay(struct btrfs_root *root, |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5514 | u64 bytenr, u64 num_bytes) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5515 | { |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5516 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5517 | int ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5518 | |
| 5519 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5520 | if (!cache) |
| 5521 | return -EINVAL; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5522 | |
| 5523 | /* |
| 5524 | * pull in the free space cache (if any) so that our pin |
| 5525 | * removes the free space from the cache. We have load_only set |
| 5526 | * to one because the slow code to read in the free extents does check |
| 5527 | * the pinned extents. |
| 5528 | */ |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 5529 | cache_block_group(cache, 1); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5530 | |
| 5531 | pin_down_extent(root, cache, bytenr, num_bytes, 0); |
| 5532 | |
| 5533 | /* remove us from the free space cache (if we're there at all) */ |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5534 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5535 | btrfs_put_block_group(cache); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 5536 | return ret; |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 5537 | } |
| 5538 | |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 5539 | static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes) |
| 5540 | { |
| 5541 | int ret; |
| 5542 | struct btrfs_block_group_cache *block_group; |
| 5543 | struct btrfs_caching_control *caching_ctl; |
| 5544 | |
| 5545 | block_group = btrfs_lookup_block_group(root->fs_info, start); |
| 5546 | if (!block_group) |
| 5547 | return -EINVAL; |
| 5548 | |
| 5549 | cache_block_group(block_group, 0); |
| 5550 | caching_ctl = get_caching_control(block_group); |
| 5551 | |
| 5552 | if (!caching_ctl) { |
| 5553 | /* Logic error */ |
| 5554 | BUG_ON(!block_group_cache_done(block_group)); |
| 5555 | ret = btrfs_remove_free_space(block_group, start, num_bytes); |
| 5556 | } else { |
| 5557 | mutex_lock(&caching_ctl->mutex); |
| 5558 | |
| 5559 | if (start >= caching_ctl->progress) { |
| 5560 | ret = add_excluded_extent(root, start, num_bytes); |
| 5561 | } else if (start + num_bytes <= caching_ctl->progress) { |
| 5562 | ret = btrfs_remove_free_space(block_group, |
| 5563 | start, num_bytes); |
| 5564 | } else { |
| 5565 | num_bytes = caching_ctl->progress - start; |
| 5566 | ret = btrfs_remove_free_space(block_group, |
| 5567 | start, num_bytes); |
| 5568 | if (ret) |
| 5569 | goto out_lock; |
| 5570 | |
| 5571 | num_bytes = (start + num_bytes) - |
| 5572 | caching_ctl->progress; |
| 5573 | start = caching_ctl->progress; |
| 5574 | ret = add_excluded_extent(root, start, num_bytes); |
| 5575 | } |
| 5576 | out_lock: |
| 5577 | mutex_unlock(&caching_ctl->mutex); |
| 5578 | put_caching_control(caching_ctl); |
| 5579 | } |
| 5580 | btrfs_put_block_group(block_group); |
| 5581 | return ret; |
| 5582 | } |
| 5583 | |
| 5584 | int btrfs_exclude_logged_extents(struct btrfs_root *log, |
| 5585 | struct extent_buffer *eb) |
| 5586 | { |
| 5587 | struct btrfs_file_extent_item *item; |
| 5588 | struct btrfs_key key; |
| 5589 | int found_type; |
| 5590 | int i; |
| 5591 | |
| 5592 | if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) |
| 5593 | return 0; |
| 5594 | |
| 5595 | for (i = 0; i < btrfs_header_nritems(eb); i++) { |
| 5596 | btrfs_item_key_to_cpu(eb, &key, i); |
| 5597 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
| 5598 | continue; |
| 5599 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); |
| 5600 | found_type = btrfs_file_extent_type(eb, item); |
| 5601 | if (found_type == BTRFS_FILE_EXTENT_INLINE) |
| 5602 | continue; |
| 5603 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) |
| 5604 | continue; |
| 5605 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); |
| 5606 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); |
| 5607 | __exclude_logged_extent(log, key.objectid, key.offset); |
| 5608 | } |
| 5609 | |
| 5610 | return 0; |
| 5611 | } |
| 5612 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5613 | /** |
| 5614 | * btrfs_update_reserved_bytes - update the block_group and space info counters |
| 5615 | * @cache: The cache we are manipulating |
| 5616 | * @num_bytes: The number of bytes in question |
| 5617 | * @reserve: One of the reservation enums |
| 5618 | * |
| 5619 | * This is called by the allocator when it reserves space, or by somebody who is |
| 5620 | * freeing space that was never actually used on disk. For example if you |
| 5621 | * reserve some space for a new leaf in transaction A and before transaction A |
| 5622 | * commits you free that leaf, you call this with reserve set to 0 in order to |
| 5623 | * clear the reservation. |
| 5624 | * |
| 5625 | * Metadata reservations should be called with RESERVE_ALLOC so we do the proper |
| 5626 | * ENOSPC accounting. For data we handle the reservation through clearing the |
| 5627 | * delalloc bits in the io_tree. We have to do this since we could end up |
| 5628 | * allocating less disk space for the amount of data we have reserved in the |
| 5629 | * case of compression. |
| 5630 | * |
| 5631 | * If this is a reservation and the block group has become read only we cannot |
| 5632 | * make the reservation and return -EAGAIN, otherwise this function always |
| 5633 | * succeeds. |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5634 | */ |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5635 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
| 5636 | u64 num_bytes, int reserve) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5637 | { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5638 | struct btrfs_space_info *space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5639 | int ret = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5640 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5641 | spin_lock(&space_info->lock); |
| 5642 | spin_lock(&cache->lock); |
| 5643 | if (reserve != RESERVE_FREE) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5644 | if (cache->ro) { |
| 5645 | ret = -EAGAIN; |
| 5646 | } else { |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5647 | cache->reserved += num_bytes; |
| 5648 | space_info->bytes_reserved += num_bytes; |
| 5649 | if (reserve == RESERVE_ALLOC) { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 5650 | trace_btrfs_space_reservation(cache->fs_info, |
Liu Bo | 2bcc032 | 2012-03-29 09:57:44 -0400 | [diff] [blame] | 5651 | "space_info", space_info->flags, |
| 5652 | num_bytes, 0); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5653 | space_info->bytes_may_use -= num_bytes; |
| 5654 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5655 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5656 | } else { |
| 5657 | if (cache->ro) |
| 5658 | space_info->bytes_readonly += num_bytes; |
| 5659 | cache->reserved -= num_bytes; |
| 5660 | space_info->bytes_reserved -= num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5661 | } |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 5662 | spin_unlock(&cache->lock); |
| 5663 | spin_unlock(&space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5664 | return ret; |
| 5665 | } |
| 5666 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5667 | void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5668 | struct btrfs_root *root) |
| 5669 | { |
| 5670 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5671 | struct btrfs_caching_control *next; |
| 5672 | struct btrfs_caching_control *caching_ctl; |
| 5673 | struct btrfs_block_group_cache *cache; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5674 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5675 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 5676 | down_write(&fs_info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5677 | |
| 5678 | list_for_each_entry_safe(caching_ctl, next, |
| 5679 | &fs_info->caching_block_groups, list) { |
| 5680 | cache = caching_ctl->block_group; |
| 5681 | if (block_group_cache_done(cache)) { |
| 5682 | cache->last_byte_to_unpin = (u64)-1; |
| 5683 | list_del_init(&caching_ctl->list); |
| 5684 | put_caching_control(caching_ctl); |
| 5685 | } else { |
| 5686 | cache->last_byte_to_unpin = caching_ctl->progress; |
| 5687 | } |
| 5688 | } |
| 5689 | |
| 5690 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 5691 | fs_info->pinned_extents = &fs_info->freed_extents[1]; |
| 5692 | else |
| 5693 | fs_info->pinned_extents = &fs_info->freed_extents[0]; |
| 5694 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 5695 | up_write(&fs_info->commit_root_sem); |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5696 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5697 | list_for_each_entry_rcu(space_info, &fs_info->space_info, list) |
| 5698 | percpu_counter_set(&space_info->total_bytes_pinned, 0); |
| 5699 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 5700 | update_global_block_rsv(fs_info); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5701 | } |
| 5702 | |
| 5703 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 5704 | { |
| 5705 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5706 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5707 | struct btrfs_space_info *space_info; |
| 5708 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5709 | u64 len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5710 | bool readonly; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5711 | |
| 5712 | while (start <= end) { |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5713 | readonly = false; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5714 | if (!cache || |
| 5715 | start >= cache->key.objectid + cache->key.offset) { |
| 5716 | if (cache) |
| 5717 | btrfs_put_block_group(cache); |
| 5718 | cache = btrfs_lookup_block_group(fs_info, start); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5719 | BUG_ON(!cache); /* Logic error */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | len = cache->key.objectid + cache->key.offset - start; |
| 5723 | len = min(len, end + 1 - start); |
| 5724 | |
| 5725 | if (start < cache->last_byte_to_unpin) { |
| 5726 | len = min(len, cache->last_byte_to_unpin - start); |
| 5727 | btrfs_add_free_space(cache, start, len); |
| 5728 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5729 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5730 | start += len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5731 | space_info = cache->space_info; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5732 | |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5733 | spin_lock(&space_info->lock); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5734 | spin_lock(&cache->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5735 | cache->pinned -= len; |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5736 | space_info->bytes_pinned -= len; |
| 5737 | if (cache->ro) { |
| 5738 | space_info->bytes_readonly += len; |
| 5739 | readonly = true; |
| 5740 | } |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 5741 | spin_unlock(&cache->lock); |
Josef Bacik | 7b398f8 | 2012-10-22 15:52:28 -0400 | [diff] [blame] | 5742 | if (!readonly && global_rsv->space_info == space_info) { |
| 5743 | spin_lock(&global_rsv->lock); |
| 5744 | if (!global_rsv->full) { |
| 5745 | len = min(len, global_rsv->size - |
| 5746 | global_rsv->reserved); |
| 5747 | global_rsv->reserved += len; |
| 5748 | space_info->bytes_may_use += len; |
| 5749 | if (global_rsv->reserved >= global_rsv->size) |
| 5750 | global_rsv->full = 1; |
| 5751 | } |
| 5752 | spin_unlock(&global_rsv->lock); |
| 5753 | } |
| 5754 | spin_unlock(&space_info->lock); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5755 | } |
| 5756 | |
| 5757 | if (cache) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 5758 | btrfs_put_block_group(cache); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 5759 | return 0; |
| 5760 | } |
| 5761 | |
| 5762 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5763 | struct btrfs_root *root) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5764 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5765 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 5766 | struct extent_io_tree *unpin; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5767 | u64 start; |
| 5768 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5769 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5770 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5771 | if (trans->aborted) |
| 5772 | return 0; |
| 5773 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5774 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
| 5775 | unpin = &fs_info->freed_extents[1]; |
| 5776 | else |
| 5777 | unpin = &fs_info->freed_extents[0]; |
| 5778 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5779 | while (1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5780 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
Josef Bacik | e613887 | 2012-09-27 17:07:30 -0400 | [diff] [blame] | 5781 | EXTENT_DIRTY, NULL); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5782 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5783 | break; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5784 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 5785 | if (btrfs_test_opt(root, DISCARD)) |
| 5786 | ret = btrfs_discard_extent(root, start, |
| 5787 | end + 1 - start, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 5788 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 5789 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 5790 | unpin_extent_range(root, start, end); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5791 | cond_resched(); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5792 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 5793 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 5794 | return 0; |
| 5795 | } |
| 5796 | |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 5797 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes, |
| 5798 | u64 owner, u64 root_objectid) |
| 5799 | { |
| 5800 | struct btrfs_space_info *space_info; |
| 5801 | u64 flags; |
| 5802 | |
| 5803 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
| 5804 | if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID) |
| 5805 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 5806 | else |
| 5807 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 5808 | } else { |
| 5809 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 5810 | } |
| 5811 | |
| 5812 | space_info = __find_space_info(fs_info, flags); |
| 5813 | BUG_ON(!space_info); /* Logic bug */ |
| 5814 | percpu_counter_add(&space_info->total_bytes_pinned, num_bytes); |
| 5815 | } |
| 5816 | |
| 5817 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5818 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
| 5819 | struct btrfs_root *root, |
| 5820 | u64 bytenr, u64 num_bytes, u64 parent, |
| 5821 | u64 root_objectid, u64 owner_objectid, |
| 5822 | u64 owner_offset, int refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 5823 | struct btrfs_delayed_extent_op *extent_op, |
| 5824 | int no_quota) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5825 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 5826 | struct btrfs_key key; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5827 | struct btrfs_path *path; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 5828 | struct btrfs_fs_info *info = root->fs_info; |
| 5829 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5830 | struct extent_buffer *leaf; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5831 | struct btrfs_extent_item *ei; |
| 5832 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 5833 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5834 | int is_data; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5835 | int extent_slot = 0; |
| 5836 | int found_extent = 0; |
| 5837 | int num_to_del = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5838 | u32 item_size; |
| 5839 | u64 refs; |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 5840 | int last_ref = 0; |
| 5841 | enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_SUB_EXCL; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5842 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 5843 | SKINNY_METADATA); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 5844 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 5845 | if (!info->quota_enabled || !is_fstree(root_objectid)) |
| 5846 | no_quota = 1; |
| 5847 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 5848 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5849 | if (!path) |
| 5850 | return -ENOMEM; |
| 5851 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 5852 | path->reada = 1; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5853 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5854 | |
| 5855 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; |
| 5856 | BUG_ON(!is_data && refs_to_drop != 1); |
| 5857 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5858 | if (is_data) |
| 5859 | skinny_metadata = 0; |
| 5860 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5861 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
| 5862 | bytenr, num_bytes, parent, |
| 5863 | root_objectid, owner_objectid, |
| 5864 | owner_offset); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5865 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5866 | extent_slot = path->slots[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5867 | while (extent_slot >= 0) { |
| 5868 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5869 | extent_slot); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5870 | if (key.objectid != bytenr) |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5871 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5872 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
| 5873 | key.offset == num_bytes) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5874 | found_extent = 1; |
| 5875 | break; |
| 5876 | } |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5877 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
| 5878 | key.offset == owner_objectid) { |
| 5879 | found_extent = 1; |
| 5880 | break; |
| 5881 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5882 | if (path->slots[0] - extent_slot > 5) |
| 5883 | break; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5884 | extent_slot--; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 5885 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5886 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 5887 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); |
| 5888 | if (found_extent && item_size < sizeof(*ei)) |
| 5889 | found_extent = 0; |
| 5890 | #endif |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5891 | if (!found_extent) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5892 | BUG_ON(iref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 5893 | ret = remove_extent_backref(trans, extent_root, path, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5894 | NULL, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 5895 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5896 | if (ret) { |
| 5897 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5898 | goto out; |
| 5899 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5900 | btrfs_release_path(path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5901 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5902 | |
| 5903 | key.objectid = bytenr; |
| 5904 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5905 | key.offset = num_bytes; |
| 5906 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5907 | if (!is_data && skinny_metadata) { |
| 5908 | key.type = BTRFS_METADATA_ITEM_KEY; |
| 5909 | key.offset = owner_objectid; |
| 5910 | } |
| 5911 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5912 | ret = btrfs_search_slot(trans, extent_root, |
| 5913 | &key, path, -1, 1); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5914 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
| 5915 | /* |
| 5916 | * Couldn't find our skinny metadata item, |
| 5917 | * see if we have ye olde extent item. |
| 5918 | */ |
| 5919 | path->slots[0]--; |
| 5920 | btrfs_item_key_to_cpu(path->nodes[0], &key, |
| 5921 | path->slots[0]); |
| 5922 | if (key.objectid == bytenr && |
| 5923 | key.type == BTRFS_EXTENT_ITEM_KEY && |
| 5924 | key.offset == num_bytes) |
| 5925 | ret = 0; |
| 5926 | } |
| 5927 | |
| 5928 | if (ret > 0 && skinny_metadata) { |
| 5929 | skinny_metadata = false; |
Filipe Manana | 9ce49a0 | 2014-04-24 15:15:28 +0100 | [diff] [blame] | 5930 | key.objectid = bytenr; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 5931 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5932 | key.offset = num_bytes; |
| 5933 | btrfs_release_path(path); |
| 5934 | ret = btrfs_search_slot(trans, extent_root, |
| 5935 | &key, path, -1, 1); |
| 5936 | } |
| 5937 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5938 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5939 | btrfs_err(info, "umm, got %d back from search, was looking for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5940 | ret, bytenr); |
Josef Bacik | b783e62 | 2011-07-13 15:03:50 +0000 | [diff] [blame] | 5941 | if (ret > 0) |
| 5942 | btrfs_print_leaf(extent_root, |
| 5943 | path->nodes[0]); |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 5944 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5945 | if (ret < 0) { |
| 5946 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5947 | goto out; |
| 5948 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 5949 | extent_slot = path->slots[0]; |
| 5950 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 5951 | } else if (WARN_ON(ret == -ENOENT)) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5952 | btrfs_print_leaf(extent_root, path->nodes[0]); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5953 | btrfs_err(info, |
| 5954 | "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5955 | bytenr, parent, root_objectid, owner_objectid, |
| 5956 | owner_offset); |
Josef Bacik | c4a050b | 2014-03-14 16:36:53 -0400 | [diff] [blame] | 5957 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5958 | goto out; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 5959 | } else { |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5960 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5961 | goto out; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5962 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5963 | |
| 5964 | leaf = path->nodes[0]; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5965 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 5966 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
| 5967 | if (item_size < sizeof(*ei)) { |
| 5968 | BUG_ON(found_extent || extent_slot != path->slots[0]); |
| 5969 | ret = convert_extent_item_v0(trans, extent_root, path, |
| 5970 | owner_objectid, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5971 | if (ret < 0) { |
| 5972 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5973 | goto out; |
| 5974 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5975 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5976 | btrfs_release_path(path); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5977 | path->leave_spinning = 1; |
| 5978 | |
| 5979 | key.objectid = bytenr; |
| 5980 | key.type = BTRFS_EXTENT_ITEM_KEY; |
| 5981 | key.offset = num_bytes; |
| 5982 | |
| 5983 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 5984 | -1, 1); |
| 5985 | if (ret) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 5986 | btrfs_err(info, "umm, got %d back from search, was looking for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 5987 | ret, bytenr); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5988 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 5989 | } |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 5990 | if (ret < 0) { |
| 5991 | btrfs_abort_transaction(trans, extent_root, ret); |
| 5992 | goto out; |
| 5993 | } |
| 5994 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 5995 | extent_slot = path->slots[0]; |
| 5996 | leaf = path->nodes[0]; |
| 5997 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
| 5998 | } |
| 5999 | #endif |
| 6000 | BUG_ON(item_size < sizeof(*ei)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6001 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 6002 | struct btrfs_extent_item); |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6003 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
| 6004 | key.type == BTRFS_EXTENT_ITEM_KEY) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6005 | struct btrfs_tree_block_info *bi; |
| 6006 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); |
| 6007 | bi = (struct btrfs_tree_block_info *)(ei + 1); |
| 6008 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6009 | } |
| 6010 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6011 | refs = btrfs_extent_refs(leaf, ei); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 6012 | if (refs < refs_to_drop) { |
| 6013 | btrfs_err(info, "trying to drop %d refs but we only have %Lu " |
David Sterba | 351fd35 | 2014-05-15 16:48:20 +0200 | [diff] [blame] | 6014 | "for bytenr %Lu", refs_to_drop, refs, bytenr); |
Josef Bacik | 32b0253 | 2013-04-24 16:38:50 -0400 | [diff] [blame] | 6015 | ret = -EINVAL; |
| 6016 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6017 | goto out; |
| 6018 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6019 | refs -= refs_to_drop; |
| 6020 | |
| 6021 | if (refs > 0) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6022 | type = BTRFS_QGROUP_OPER_SUB_SHARED; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6023 | if (extent_op) |
| 6024 | __run_delayed_extent_op(extent_op, leaf, ei); |
| 6025 | /* |
| 6026 | * In the case of inline back ref, reference count will |
| 6027 | * be updated by remove_extent_backref |
| 6028 | */ |
| 6029 | if (iref) { |
| 6030 | BUG_ON(!found_extent); |
| 6031 | } else { |
| 6032 | btrfs_set_extent_refs(leaf, ei, refs); |
| 6033 | btrfs_mark_buffer_dirty(leaf); |
| 6034 | } |
| 6035 | if (found_extent) { |
| 6036 | ret = remove_extent_backref(trans, extent_root, path, |
| 6037 | iref, refs_to_drop, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6038 | is_data, &last_ref); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6039 | if (ret) { |
| 6040 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6041 | goto out; |
| 6042 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6043 | } |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6044 | add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid, |
| 6045 | root_objectid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6046 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6047 | if (found_extent) { |
| 6048 | BUG_ON(is_data && refs_to_drop != |
| 6049 | extent_data_ref_count(root, path, iref)); |
| 6050 | if (iref) { |
| 6051 | BUG_ON(path->slots[0] != extent_slot); |
| 6052 | } else { |
| 6053 | BUG_ON(path->slots[0] != extent_slot + 1); |
| 6054 | path->slots[0] = extent_slot; |
| 6055 | num_to_del = 2; |
| 6056 | } |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 6057 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6058 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6059 | last_ref = 1; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 6060 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 6061 | num_to_del); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6062 | if (ret) { |
| 6063 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6064 | goto out; |
| 6065 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 6066 | btrfs_release_path(path); |
David Woodhouse | 21af804 | 2008-08-12 14:13:26 +0100 | [diff] [blame] | 6067 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6068 | if (is_data) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 6069 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6070 | if (ret) { |
| 6071 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6072 | goto out; |
| 6073 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 6074 | } |
| 6075 | |
Liu Bo | c53d613 | 2012-12-27 09:01:19 +0000 | [diff] [blame] | 6076 | ret = update_block_group(root, bytenr, num_bytes, 0); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 6077 | if (ret) { |
| 6078 | btrfs_abort_transaction(trans, extent_root, ret); |
| 6079 | goto out; |
| 6080 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6081 | } |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6082 | btrfs_release_path(path); |
| 6083 | |
| 6084 | /* Deal with the quota accounting */ |
| 6085 | if (!ret && last_ref && !no_quota) { |
| 6086 | int mod_seq = 0; |
| 6087 | |
| 6088 | if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID && |
| 6089 | type == BTRFS_QGROUP_OPER_SUB_SHARED) |
| 6090 | mod_seq = 1; |
| 6091 | |
| 6092 | ret = btrfs_qgroup_record_ref(trans, info, root_objectid, |
| 6093 | bytenr, num_bytes, type, |
| 6094 | mod_seq); |
| 6095 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6096 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 6097 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 6098 | return ret; |
| 6099 | } |
| 6100 | |
| 6101 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6102 | * 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] | 6103 | * delayed ref for that extent as well. This searches the delayed ref tree for |
| 6104 | * a given extent, and if there are no other delayed refs to be processed, it |
| 6105 | * removes it from the tree. |
| 6106 | */ |
| 6107 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, |
| 6108 | struct btrfs_root *root, u64 bytenr) |
| 6109 | { |
| 6110 | struct btrfs_delayed_ref_head *head; |
| 6111 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6112 | int ret = 0; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6113 | |
| 6114 | delayed_refs = &trans->transaction->delayed_refs; |
| 6115 | spin_lock(&delayed_refs->lock); |
| 6116 | head = btrfs_find_delayed_ref_head(trans, bytenr); |
| 6117 | if (!head) |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 6118 | goto out_delayed_unlock; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6119 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6120 | spin_lock(&head->lock); |
| 6121 | if (rb_first(&head->ref_root)) |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6122 | goto out; |
| 6123 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6124 | if (head->extent_op) { |
| 6125 | if (!head->must_insert_reserved) |
| 6126 | goto out; |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 6127 | btrfs_free_delayed_extent_op(head->extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6128 | head->extent_op = NULL; |
| 6129 | } |
| 6130 | |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6131 | /* |
| 6132 | * waiting for the lock here would deadlock. If someone else has it |
| 6133 | * locked they are already in the process of dropping it anyway |
| 6134 | */ |
| 6135 | if (!mutex_trylock(&head->mutex)) |
| 6136 | goto out; |
| 6137 | |
| 6138 | /* |
| 6139 | * at this point we have a head with no other entries. Go |
| 6140 | * ahead and process it. |
| 6141 | */ |
| 6142 | head->node.in_tree = 0; |
Liu Bo | c46effa | 2013-10-14 12:59:45 +0800 | [diff] [blame] | 6143 | rb_erase(&head->href_node, &delayed_refs->href_root); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6144 | |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6145 | atomic_dec(&delayed_refs->num_entries); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6146 | |
| 6147 | /* |
| 6148 | * we don't take a ref on the node because we're removing it from the |
| 6149 | * tree, so we just steal the ref the tree was holding. |
| 6150 | */ |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6151 | delayed_refs->num_heads--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6152 | if (head->processing == 0) |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 6153 | delayed_refs->num_heads_ready--; |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6154 | head->processing = 0; |
| 6155 | spin_unlock(&head->lock); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6156 | spin_unlock(&delayed_refs->lock); |
| 6157 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6158 | BUG_ON(head->extent_op); |
| 6159 | if (head->must_insert_reserved) |
| 6160 | ret = 1; |
| 6161 | |
| 6162 | mutex_unlock(&head->mutex); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6163 | btrfs_put_delayed_ref(&head->node); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6164 | return ret; |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6165 | out: |
Josef Bacik | d7df2c7 | 2014-01-23 09:21:38 -0500 | [diff] [blame] | 6166 | spin_unlock(&head->lock); |
Chris Mason | cf93da7 | 2014-01-29 07:02:40 -0800 | [diff] [blame] | 6167 | |
| 6168 | out_delayed_unlock: |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 6169 | spin_unlock(&delayed_refs->lock); |
| 6170 | return 0; |
| 6171 | } |
| 6172 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6173 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
| 6174 | struct btrfs_root *root, |
| 6175 | struct extent_buffer *buf, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6176 | u64 parent, int last_ref) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6177 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6178 | struct btrfs_block_group_cache *cache = NULL; |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6179 | int pin = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6180 | int ret; |
| 6181 | |
| 6182 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6183 | ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, |
| 6184 | buf->start, buf->len, |
| 6185 | parent, root->root_key.objectid, |
| 6186 | btrfs_header_level(buf), |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 6187 | BTRFS_DROP_DELAYED_REF, NULL, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6188 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6189 | } |
| 6190 | |
| 6191 | if (!last_ref) |
| 6192 | return; |
| 6193 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6194 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6195 | |
| 6196 | if (btrfs_header_generation(buf) == trans->transid) { |
| 6197 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 6198 | ret = check_ref_cleanup(trans, root, buf->start); |
| 6199 | if (!ret) |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 6200 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6201 | } |
| 6202 | |
| 6203 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
| 6204 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 6205 | goto out; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); |
| 6209 | |
| 6210 | btrfs_add_free_space(cache, buf->start, buf->len); |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6211 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE); |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 6212 | trace_btrfs_reserved_extent_free(root, buf->start, buf->len); |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6213 | pin = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6214 | } |
| 6215 | out: |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6216 | if (pin) |
| 6217 | add_pinned_bytes(root->fs_info, buf->len, |
| 6218 | btrfs_header_level(buf), |
| 6219 | root->root_key.objectid); |
| 6220 | |
Josef Bacik | a826d6d | 2011-03-16 13:42:43 -0400 | [diff] [blame] | 6221 | /* |
| 6222 | * Deleting the buffer, clear the corrupt flag since it doesn't matter |
| 6223 | * anymore. |
| 6224 | */ |
| 6225 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6226 | btrfs_put_block_group(cache); |
| 6227 | } |
| 6228 | |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6229 | /* Can return -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6230 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 6231 | u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6232 | u64 owner, u64 offset, int no_quota) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6233 | { |
| 6234 | int ret; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6235 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6236 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 6237 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 6238 | if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state))) |
| 6239 | return 0; |
| 6240 | #endif |
Josef Bacik | b150a4f | 2013-06-19 15:00:04 -0400 | [diff] [blame] | 6241 | add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid); |
| 6242 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6243 | /* |
| 6244 | * tree log blocks never actually go into the extent allocation |
| 6245 | * tree, just update pinning info and exit early. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6246 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6247 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
| 6248 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6249 | /* unlocks the pinned mutex */ |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6250 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6251 | ret = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6252 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6253 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
| 6254 | num_bytes, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6255 | parent, root_objectid, (int)owner, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6256 | BTRFS_DROP_DELAYED_REF, NULL, no_quota); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6257 | } else { |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 6258 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
| 6259 | num_bytes, |
| 6260 | parent, root_objectid, owner, |
| 6261 | offset, BTRFS_DROP_DELAYED_REF, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6262 | NULL, no_quota); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 6263 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6264 | return ret; |
| 6265 | } |
| 6266 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6267 | static u64 stripe_align(struct btrfs_root *root, |
| 6268 | struct btrfs_block_group_cache *cache, |
| 6269 | u64 val, u64 num_bytes) |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 6270 | { |
Qu Wenruo | fda2832 | 2013-02-26 08:10:22 +0000 | [diff] [blame] | 6271 | u64 ret = ALIGN(val, root->stripesize); |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 6272 | return ret; |
| 6273 | } |
| 6274 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6275 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6276 | * when we wait for progress in the block group caching, its because |
| 6277 | * our allocation attempt failed at least once. So, we must sleep |
| 6278 | * and let some progress happen before we try again. |
| 6279 | * |
| 6280 | * This function will sleep at least once waiting for new free space to |
| 6281 | * show up, and then it will check the block group free space numbers |
| 6282 | * for our min num_bytes. Another option is to have it go ahead |
| 6283 | * and look in the rbtree for a free extent of a given size, but this |
| 6284 | * is a good start. |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6285 | * |
| 6286 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using |
| 6287 | * any of the information in this block group. |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6288 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6289 | static noinline void |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6290 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
| 6291 | u64 num_bytes) |
| 6292 | { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6293 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6294 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6295 | caching_ctl = get_caching_control(cache); |
| 6296 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6297 | return; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6298 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6299 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 6300 | (cache->free_space_ctl->free_space >= num_bytes)); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6301 | |
| 6302 | put_caching_control(caching_ctl); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | static noinline int |
| 6306 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) |
| 6307 | { |
| 6308 | struct btrfs_caching_control *caching_ctl; |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6309 | int ret = 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6310 | |
| 6311 | caching_ctl = get_caching_control(cache); |
| 6312 | if (!caching_ctl) |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6313 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6314 | |
| 6315 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6316 | if (cache->cached == BTRFS_CACHE_ERROR) |
| 6317 | ret = -EIO; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6318 | put_caching_control(caching_ctl); |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6319 | return ret; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6320 | } |
| 6321 | |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 6322 | int __get_raid_index(u64 flags) |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6323 | { |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6324 | if (flags & BTRFS_BLOCK_GROUP_RAID10) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6325 | return BTRFS_RAID_RAID10; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6326 | else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6327 | return BTRFS_RAID_RAID1; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6328 | else if (flags & BTRFS_BLOCK_GROUP_DUP) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6329 | return BTRFS_RAID_DUP; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6330 | else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 6331 | return BTRFS_RAID_RAID0; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6332 | else if (flags & BTRFS_BLOCK_GROUP_RAID5) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6333 | return BTRFS_RAID_RAID5; |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6334 | else if (flags & BTRFS_BLOCK_GROUP_RAID6) |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6335 | return BTRFS_RAID_RAID6; |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6336 | |
Chris Mason | e942f88 | 2013-02-20 14:06:05 -0500 | [diff] [blame] | 6337 | return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6338 | } |
| 6339 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 6340 | int get_block_group_index(struct btrfs_block_group_cache *cache) |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6341 | { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 6342 | return __get_raid_index(cache->flags); |
Ilya Dryomov | 7738a53 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 6343 | } |
| 6344 | |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 6345 | static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = { |
| 6346 | [BTRFS_RAID_RAID10] = "raid10", |
| 6347 | [BTRFS_RAID_RAID1] = "raid1", |
| 6348 | [BTRFS_RAID_DUP] = "dup", |
| 6349 | [BTRFS_RAID_RAID0] = "raid0", |
| 6350 | [BTRFS_RAID_SINGLE] = "single", |
| 6351 | [BTRFS_RAID_RAID5] = "raid5", |
| 6352 | [BTRFS_RAID_RAID6] = "raid6", |
| 6353 | }; |
| 6354 | |
Jeff Mahoney | 1b8e5df | 2013-11-20 16:50:23 -0500 | [diff] [blame] | 6355 | static const char *get_raid_name(enum btrfs_raid_types type) |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 6356 | { |
| 6357 | if (type >= BTRFS_NR_RAID_TYPES) |
| 6358 | return NULL; |
| 6359 | |
| 6360 | return btrfs_raid_type_names[type]; |
| 6361 | } |
| 6362 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6363 | enum btrfs_loop_type { |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 6364 | LOOP_CACHING_NOWAIT = 0, |
| 6365 | LOOP_CACHING_WAIT = 1, |
| 6366 | LOOP_ALLOC_CHUNK = 2, |
| 6367 | LOOP_NO_EMPTY_SIZE = 3, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6368 | }; |
| 6369 | |
| 6370 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6371 | * walks the btree of allocated extents and find a hole of a given size. |
| 6372 | * The key ins is changed to record the hole: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6373 | * ins->objectid == start position |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 6374 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6375 | * ins->offset == the size of the hole. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6376 | * Any available blocks before search_start are skipped. |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6377 | * |
| 6378 | * If there is no suitable free space, we will record the max size of |
| 6379 | * the free space extent currently. |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6380 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6381 | static noinline int find_free_extent(struct btrfs_root *orig_root, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6382 | u64 num_bytes, u64 empty_size, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 6383 | u64 hint_byte, struct btrfs_key *ins, |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6384 | u64 flags) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6385 | { |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6386 | int ret = 0; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6387 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6388 | struct btrfs_free_cluster *last_ptr = NULL; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6389 | struct btrfs_block_group_cache *block_group = NULL; |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 6390 | u64 search_start = 0; |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6391 | u64 max_extent_size = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6392 | int empty_cluster = 2 * 1024 * 1024; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6393 | struct btrfs_space_info *space_info; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6394 | int loop = 0; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6395 | int index = __get_raid_index(flags); |
| 6396 | int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ? |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6397 | RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6398 | bool failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6399 | bool failed_alloc = false; |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 6400 | bool use_cluster = true; |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 6401 | bool have_caching_bg = false; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6402 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6403 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 6404 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6405 | ins->objectid = 0; |
| 6406 | ins->offset = 0; |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 6407 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6408 | trace_find_free_extent(orig_root, num_bytes, empty_size, flags); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 6409 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6410 | space_info = __find_space_info(root->fs_info, flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 6411 | if (!space_info) { |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6412 | btrfs_err(root->fs_info, "No space info for %llu", flags); |
Josef Bacik | 1b1d1f6 | 2010-03-19 20:49:55 +0000 | [diff] [blame] | 6413 | return -ENOSPC; |
| 6414 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6415 | |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 6416 | /* |
| 6417 | * If the space info is for both data and metadata it means we have a |
| 6418 | * small filesystem and we can't use the clustering stuff. |
| 6419 | */ |
| 6420 | if (btrfs_mixed_space_info(space_info)) |
| 6421 | use_cluster = false; |
| 6422 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6423 | if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6424 | last_ptr = &root->fs_info->meta_alloc_cluster; |
Chris Mason | 536ac8a | 2009-02-12 09:41:38 -0500 | [diff] [blame] | 6425 | if (!btrfs_test_opt(root, SSD)) |
| 6426 | empty_cluster = 64 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6427 | } |
| 6428 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6429 | if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
Josef Bacik | 6737773 | 2010-09-16 16:19:09 -0400 | [diff] [blame] | 6430 | btrfs_test_opt(root, SSD)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6431 | last_ptr = &root->fs_info->data_alloc_cluster; |
| 6432 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6433 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6434 | if (last_ptr) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6435 | spin_lock(&last_ptr->lock); |
| 6436 | if (last_ptr->block_group) |
| 6437 | hint_byte = last_ptr->window_start; |
| 6438 | spin_unlock(&last_ptr->lock); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6439 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6440 | |
Chris Mason | a061fc8 | 2008-05-07 11:43:44 -0400 | [diff] [blame] | 6441 | search_start = max(search_start, first_logical_byte(root, 0)); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 6442 | search_start = max(search_start, hint_byte); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 6443 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6444 | if (!last_ptr) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6445 | empty_cluster = 0; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6446 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6447 | if (search_start == hint_byte) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6448 | block_group = btrfs_lookup_block_group(root->fs_info, |
| 6449 | search_start); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6450 | /* |
| 6451 | * we don't want to use the block group if it doesn't match our |
| 6452 | * allocation bits, or if its not cached. |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 6453 | * |
| 6454 | * However if we are re-searching with an ideal block group |
| 6455 | * 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] | 6456 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6457 | if (block_group && block_group_bits(block_group, flags) && |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 6458 | block_group->cached != BTRFS_CACHE_NO) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6459 | down_read(&space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6460 | if (list_empty(&block_group->list) || |
| 6461 | block_group->ro) { |
| 6462 | /* |
| 6463 | * someone is removing this block group, |
| 6464 | * we can't jump into the have_block_group |
| 6465 | * target because our list pointers are not |
| 6466 | * valid |
| 6467 | */ |
| 6468 | btrfs_put_block_group(block_group); |
| 6469 | up_read(&space_info->groups_sem); |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 6470 | } else { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6471 | index = get_block_group_index(block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6472 | goto have_block_group; |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 6473 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6474 | } else if (block_group) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6475 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6476 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 6477 | } |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6478 | search: |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 6479 | have_caching_bg = false; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6480 | down_read(&space_info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6481 | list_for_each_entry(block_group, &space_info->block_groups[index], |
| 6482 | list) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6483 | u64 offset; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6484 | int cached; |
Chris Mason | 8a1413a | 2008-11-10 16:13:54 -0500 | [diff] [blame] | 6485 | |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 6486 | btrfs_get_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6487 | search_start = block_group->key.objectid; |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 6488 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 6489 | /* |
| 6490 | * this can happen if we end up cycling through all the |
| 6491 | * raid types, but we want to make sure we only allocate |
| 6492 | * for the proper type. |
| 6493 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6494 | if (!block_group_bits(block_group, flags)) { |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 6495 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
| 6496 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6497 | BTRFS_BLOCK_GROUP_RAID5 | |
| 6498 | BTRFS_BLOCK_GROUP_RAID6 | |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 6499 | BTRFS_BLOCK_GROUP_RAID10; |
| 6500 | |
| 6501 | /* |
| 6502 | * if they asked for extra copies and this block group |
| 6503 | * doesn't provide them, bail. This does allow us to |
| 6504 | * fill raid0 from raid1. |
| 6505 | */ |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6506 | if ((flags & extra) && !(block_group->flags & extra)) |
Chris Mason | 83a50de | 2010-12-13 15:06:46 -0500 | [diff] [blame] | 6507 | goto loop; |
| 6508 | } |
| 6509 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6510 | have_block_group: |
Josef Bacik | 291c7d2 | 2011-11-14 13:52:14 -0500 | [diff] [blame] | 6511 | cached = block_group_cache_done(block_group); |
| 6512 | if (unlikely(!cached)) { |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 6513 | ret = cache_block_group(block_group, 0); |
Chris Mason | 1d4284b | 2012-03-28 20:31:37 -0400 | [diff] [blame] | 6514 | BUG_ON(ret < 0); |
| 6515 | ret = 0; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 6516 | } |
| 6517 | |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 6518 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
| 6519 | goto loop; |
Josef Bacik | ea6a478 | 2008-11-20 12:16:16 -0500 | [diff] [blame] | 6520 | if (unlikely(block_group->ro)) |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6521 | goto loop; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6522 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6523 | /* |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6524 | * Ok we want to try and use the cluster allocator, so |
| 6525 | * lets look there |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6526 | */ |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6527 | if (last_ptr) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6528 | struct btrfs_block_group_cache *used_block_group; |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 6529 | unsigned long aligned_cluster; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6530 | /* |
| 6531 | * the refill lock keeps out other |
| 6532 | * people trying to start a new cluster |
| 6533 | */ |
| 6534 | spin_lock(&last_ptr->refill_lock); |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6535 | used_block_group = last_ptr->block_group; |
| 6536 | if (used_block_group != block_group && |
| 6537 | (!used_block_group || |
| 6538 | used_block_group->ro || |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6539 | !block_group_bits(used_block_group, flags))) |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6540 | goto refill_cluster; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6541 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6542 | if (used_block_group != block_group) |
| 6543 | btrfs_get_block_group(used_block_group); |
| 6544 | |
| 6545 | offset = btrfs_alloc_from_cluster(used_block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6546 | last_ptr, |
| 6547 | num_bytes, |
| 6548 | used_block_group->key.objectid, |
| 6549 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6550 | if (offset) { |
| 6551 | /* we have a block, we're done */ |
| 6552 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 6553 | trace_btrfs_reserve_extent_cluster(root, |
Miao Xie | 89d4346 | 2014-01-15 20:00:57 +0800 | [diff] [blame] | 6554 | used_block_group, |
| 6555 | search_start, num_bytes); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6556 | if (used_block_group != block_group) { |
| 6557 | btrfs_put_block_group(block_group); |
| 6558 | block_group = used_block_group; |
| 6559 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6560 | goto checks; |
| 6561 | } |
| 6562 | |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6563 | WARN_ON(last_ptr->block_group != used_block_group); |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6564 | if (used_block_group != block_group) |
Alexandre Oliva | 274bd4f | 2011-12-07 20:08:40 -0500 | [diff] [blame] | 6565 | btrfs_put_block_group(used_block_group); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 6566 | refill_cluster: |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6567 | /* If we are on LOOP_NO_EMPTY_SIZE, we can't |
| 6568 | * set up a new clusters, so lets just skip it |
| 6569 | * and let the allocator find whatever block |
| 6570 | * it can find. If we reach this point, we |
| 6571 | * will have tried the cluster allocator |
| 6572 | * plenty of times and not have found |
| 6573 | * anything, so we are likely way too |
| 6574 | * fragmented for the clustering stuff to find |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 6575 | * anything. |
| 6576 | * |
| 6577 | * However, if the cluster is taken from the |
| 6578 | * current block group, release the cluster |
| 6579 | * first, so that we stand a better chance of |
| 6580 | * succeeding in the unclustered |
| 6581 | * allocation. */ |
| 6582 | if (loop >= LOOP_NO_EMPTY_SIZE && |
| 6583 | last_ptr->block_group != block_group) { |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6584 | spin_unlock(&last_ptr->refill_lock); |
| 6585 | goto unclustered_alloc; |
| 6586 | } |
| 6587 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6588 | /* |
| 6589 | * this cluster didn't work out, free it and |
| 6590 | * start over |
| 6591 | */ |
| 6592 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
| 6593 | |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 6594 | if (loop >= LOOP_NO_EMPTY_SIZE) { |
| 6595 | spin_unlock(&last_ptr->refill_lock); |
| 6596 | goto unclustered_alloc; |
| 6597 | } |
| 6598 | |
Chris Mason | 8de972b | 2013-01-04 15:39:43 -0500 | [diff] [blame] | 6599 | aligned_cluster = max_t(unsigned long, |
| 6600 | empty_cluster + empty_size, |
| 6601 | block_group->full_stripe_len); |
| 6602 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6603 | /* allocate a cluster in this block group */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6604 | ret = btrfs_find_space_cluster(root, block_group, |
| 6605 | last_ptr, search_start, |
| 6606 | num_bytes, |
| 6607 | aligned_cluster); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6608 | if (ret == 0) { |
| 6609 | /* |
| 6610 | * now pull our allocation out of this |
| 6611 | * cluster |
| 6612 | */ |
| 6613 | offset = btrfs_alloc_from_cluster(block_group, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6614 | last_ptr, |
| 6615 | num_bytes, |
| 6616 | search_start, |
| 6617 | &max_extent_size); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6618 | if (offset) { |
| 6619 | /* we found one, proceed */ |
| 6620 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 6621 | trace_btrfs_reserve_extent_cluster(root, |
| 6622 | block_group, search_start, |
| 6623 | num_bytes); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6624 | goto checks; |
| 6625 | } |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6626 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
| 6627 | && !failed_cluster_refill) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6628 | spin_unlock(&last_ptr->refill_lock); |
| 6629 | |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6630 | failed_cluster_refill = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6631 | wait_block_group_cache_progress(block_group, |
| 6632 | num_bytes + empty_cluster + empty_size); |
| 6633 | goto have_block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6634 | } |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6635 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6636 | /* |
| 6637 | * at this point we either didn't find a cluster |
| 6638 | * or we weren't able to allocate a block from our |
| 6639 | * cluster. Free the cluster we've been trying |
| 6640 | * to use, and go to the next block group |
| 6641 | */ |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6642 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6643 | spin_unlock(&last_ptr->refill_lock); |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6644 | goto loop; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6645 | } |
| 6646 | |
Alexandre Oliva | 062c05c | 2011-12-07 19:50:42 -0500 | [diff] [blame] | 6647 | unclustered_alloc: |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 6648 | spin_lock(&block_group->free_space_ctl->tree_lock); |
| 6649 | if (cached && |
| 6650 | block_group->free_space_ctl->free_space < |
| 6651 | num_bytes + empty_cluster + empty_size) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6652 | if (block_group->free_space_ctl->free_space > |
| 6653 | max_extent_size) |
| 6654 | max_extent_size = |
| 6655 | block_group->free_space_ctl->free_space; |
Alexandre Oliva | a5f6f71 | 2011-12-12 04:48:19 -0200 | [diff] [blame] | 6656 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 6657 | goto loop; |
| 6658 | } |
| 6659 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
| 6660 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6661 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6662 | num_bytes, empty_size, |
| 6663 | &max_extent_size); |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6664 | /* |
| 6665 | * If we didn't find a chunk, and we haven't failed on this |
| 6666 | * block group before, and this block group is in the middle of |
| 6667 | * caching and we are ok with waiting, then go ahead and wait |
| 6668 | * for progress to be made, and set failed_alloc to true. |
| 6669 | * |
| 6670 | * If failed_alloc is true then we've already waited on this |
| 6671 | * block group once and should move on to the next block group. |
| 6672 | */ |
| 6673 | if (!offset && !failed_alloc && !cached && |
| 6674 | loop > LOOP_CACHING_NOWAIT) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6675 | wait_block_group_cache_progress(block_group, |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6676 | num_bytes + empty_size); |
| 6677 | failed_alloc = true; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6678 | goto have_block_group; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6679 | } else if (!offset) { |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 6680 | if (!cached) |
| 6681 | have_caching_bg = true; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6682 | goto loop; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6683 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6684 | checks: |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6685 | search_start = stripe_align(root, block_group, |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 6686 | offset, num_bytes); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 6687 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6688 | /* move on to the next group */ |
| 6689 | if (search_start + num_bytes > |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6690 | block_group->key.objectid + block_group->key.offset) { |
| 6691 | btrfs_add_free_space(block_group, offset, num_bytes); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6692 | goto loop; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6693 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6694 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6695 | if (offset < search_start) |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6696 | btrfs_add_free_space(block_group, offset, |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 6697 | search_start - offset); |
| 6698 | BUG_ON(offset > search_start); |
| 6699 | |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6700 | ret = btrfs_update_reserved_bytes(block_group, num_bytes, |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 6701 | alloc_type); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6702 | if (ret == -EAGAIN) { |
Miao Xie | 215a63d | 2014-01-15 20:00:56 +0800 | [diff] [blame] | 6703 | btrfs_add_free_space(block_group, offset, num_bytes); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6704 | goto loop; |
| 6705 | } |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6706 | |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6707 | /* we are all good, lets return */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 6708 | ins->objectid = search_start; |
| 6709 | ins->offset = num_bytes; |
| 6710 | |
Josef Bacik | 3f7de03 | 2011-11-10 08:29:20 -0500 | [diff] [blame] | 6711 | trace_btrfs_reserve_extent(orig_root, block_group, |
| 6712 | search_start, num_bytes); |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6713 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6714 | break; |
| 6715 | loop: |
Josef Bacik | 0a24325 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 6716 | failed_cluster_refill = false; |
Josef Bacik | 1cdda9b | 2009-10-06 10:04:28 -0400 | [diff] [blame] | 6717 | failed_alloc = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6718 | BUG_ON(index != get_block_group_index(block_group)); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6719 | btrfs_put_block_group(block_group); |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6720 | } |
| 6721 | up_read(&space_info->groups_sem); |
Chris Mason | f5a31e1 | 2008-11-10 11:47:09 -0500 | [diff] [blame] | 6722 | |
Miao Xie | 60d2adb | 2011-09-09 17:34:35 +0800 | [diff] [blame] | 6723 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
| 6724 | goto search; |
| 6725 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6726 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
| 6727 | goto search; |
| 6728 | |
Josef Bacik | 285ff5a | 2012-01-13 15:27:45 -0500 | [diff] [blame] | 6729 | /* |
Josef Bacik | ccf0e72 | 2009-11-10 21:23:48 -0500 | [diff] [blame] | 6730 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
| 6731 | * caching kthreads as we move along |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6732 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
| 6733 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again |
| 6734 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try |
| 6735 | * again |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6736 | */ |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6737 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6738 | index = 0; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6739 | loop++; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6740 | if (loop == LOOP_ALLOC_CHUNK) { |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6741 | struct btrfs_trans_handle *trans; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 6742 | int exist = 0; |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6743 | |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 6744 | trans = current->journal_info; |
| 6745 | if (trans) |
| 6746 | exist = 1; |
| 6747 | else |
| 6748 | trans = btrfs_join_transaction(root); |
| 6749 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6750 | if (IS_ERR(trans)) { |
| 6751 | ret = PTR_ERR(trans); |
| 6752 | goto out; |
| 6753 | } |
| 6754 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6755 | ret = do_chunk_alloc(trans, root, flags, |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 6756 | CHUNK_ALLOC_FORCE); |
| 6757 | /* |
| 6758 | * Do not bail out on ENOSPC since we |
| 6759 | * can do more things. |
| 6760 | */ |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6761 | if (ret < 0 && ret != -ENOSPC) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 6762 | btrfs_abort_transaction(trans, |
| 6763 | root, ret); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6764 | else |
| 6765 | ret = 0; |
Wang Shilong | f017f15 | 2014-03-13 13:19:47 +0800 | [diff] [blame] | 6766 | if (!exist) |
| 6767 | btrfs_end_transaction(trans, root); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6768 | if (ret) |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 6769 | goto out; |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6770 | } |
| 6771 | |
| 6772 | if (loop == LOOP_NO_EMPTY_SIZE) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6773 | empty_size = 0; |
| 6774 | empty_cluster = 0; |
| 6775 | } |
Chris Mason | 42e70e7 | 2008-11-07 18:17:11 -0500 | [diff] [blame] | 6776 | |
Josef Bacik | 723bda2 | 2011-05-27 16:11:38 -0400 | [diff] [blame] | 6777 | goto search; |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6778 | } else if (!ins->objectid) { |
| 6779 | ret = -ENOSPC; |
Josef Bacik | d82a6f1d | 2011-05-11 15:26:06 -0400 | [diff] [blame] | 6780 | } else if (ins->objectid) { |
Josef Bacik | 2552d17 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 6781 | ret = 0; |
| 6782 | } |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6783 | out: |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6784 | if (ret == -ENOSPC) |
| 6785 | ins->offset = max_extent_size; |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 6786 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6787 | } |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 6788 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6789 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
| 6790 | int dump_block_groups) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6791 | { |
| 6792 | struct btrfs_block_group_cache *cache; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6793 | int index = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6794 | |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6795 | spin_lock(&info->lock); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6796 | printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6797 | info->flags, |
| 6798 | info->total_bytes - info->bytes_used - info->bytes_pinned - |
| 6799 | info->bytes_reserved - info->bytes_readonly, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 6800 | (info->full) ? "" : "not "); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6801 | printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, " |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 6802 | "reserved=%llu, may_use=%llu, readonly=%llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6803 | info->total_bytes, info->bytes_used, info->bytes_pinned, |
| 6804 | info->bytes_reserved, info->bytes_may_use, |
| 6805 | info->bytes_readonly); |
Josef Bacik | 9ed74f2 | 2009-09-11 16:12:44 -0400 | [diff] [blame] | 6806 | spin_unlock(&info->lock); |
| 6807 | |
| 6808 | if (!dump_block_groups) |
| 6809 | return; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6810 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6811 | down_read(&info->groups_sem); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6812 | again: |
| 6813 | list_for_each_entry(cache, &info->block_groups[index], list) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6814 | spin_lock(&cache->lock); |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 6815 | printk(KERN_INFO "BTRFS: " |
| 6816 | "block group %llu has %llu bytes, " |
| 6817 | "%llu used %llu pinned %llu reserved %s\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6818 | cache->key.objectid, cache->key.offset, |
| 6819 | btrfs_block_group_used(&cache->item), cache->pinned, |
| 6820 | cache->reserved, cache->ro ? "[readonly]" : ""); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6821 | btrfs_dump_free_space(cache, bytes); |
| 6822 | spin_unlock(&cache->lock); |
| 6823 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 6824 | if (++index < BTRFS_NR_RAID_TYPES) |
| 6825 | goto again; |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 6826 | up_read(&info->groups_sem); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6827 | } |
Zheng Yan | e856981 | 2008-09-26 10:05:48 -0400 | [diff] [blame] | 6828 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6829 | int btrfs_reserve_extent(struct btrfs_root *root, |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 6830 | u64 num_bytes, u64 min_alloc_size, |
| 6831 | u64 empty_size, u64 hint_byte, |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6832 | struct btrfs_key *ins, int is_data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6833 | { |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6834 | bool final_tried = false; |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6835 | u64 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 6836 | int ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6837 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6838 | flags = btrfs_get_alloc_profile(root, is_data); |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 6839 | again: |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 6840 | WARN_ON(num_bytes < root->sectorsize); |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 6841 | ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins, |
| 6842 | flags); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 6843 | |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6844 | if (ret == -ENOSPC) { |
Miao Xie | a482039 | 2013-09-09 13:19:42 +0800 | [diff] [blame] | 6845 | if (!final_tried && ins->offset) { |
| 6846 | num_bytes = min(num_bytes >> 1, ins->offset); |
Zach Brown | 24542bf | 2012-11-16 00:04:43 +0000 | [diff] [blame] | 6847 | num_bytes = round_down(num_bytes, root->sectorsize); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6848 | num_bytes = max(num_bytes, min_alloc_size); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6849 | if (num_bytes == min_alloc_size) |
| 6850 | final_tried = true; |
| 6851 | goto again; |
| 6852 | } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 6853 | struct btrfs_space_info *sinfo; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6854 | |
David Sterba | b6919a5 | 2013-04-29 13:39:40 +0000 | [diff] [blame] | 6855 | sinfo = __find_space_info(root->fs_info, flags); |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6856 | btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6857 | flags, num_bytes); |
Jeff Mahoney | 5380428 | 2012-03-01 14:56:28 +0100 | [diff] [blame] | 6858 | if (sinfo) |
| 6859 | dump_space_info(sinfo, num_bytes, 1); |
Miao Xie | 9e622d6 | 2012-01-26 15:01:12 -0500 | [diff] [blame] | 6860 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6861 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6862 | |
| 6863 | return ret; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6864 | } |
| 6865 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6866 | static int __btrfs_free_reserved_extent(struct btrfs_root *root, |
| 6867 | u64 start, u64 len, int pin) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 6868 | { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6869 | struct btrfs_block_group_cache *cache; |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6870 | int ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6871 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6872 | cache = btrfs_lookup_block_group(root->fs_info, start); |
| 6873 | if (!cache) { |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6874 | btrfs_err(root->fs_info, "Unable to find block group for %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6875 | start); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6876 | return -ENOSPC; |
| 6877 | } |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6878 | |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 6879 | if (btrfs_test_opt(root, DISCARD)) |
| 6880 | ret = btrfs_discard_extent(root, start, len, NULL); |
Liu Hui | 1f3c79a | 2009-01-05 15:57:51 -0500 | [diff] [blame] | 6881 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6882 | if (pin) |
| 6883 | pin_down_extent(root, cache, start, len, 1); |
| 6884 | else { |
| 6885 | btrfs_add_free_space(cache, start, len); |
| 6886 | btrfs_update_reserved_bytes(cache, len, RESERVE_FREE); |
| 6887 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 6888 | btrfs_put_block_group(cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 6889 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 6890 | trace_btrfs_reserved_extent_free(root, start, len); |
| 6891 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6892 | return ret; |
| 6893 | } |
| 6894 | |
Chris Mason | e688b725 | 2011-10-31 20:52:39 -0400 | [diff] [blame] | 6895 | int btrfs_free_reserved_extent(struct btrfs_root *root, |
| 6896 | u64 start, u64 len) |
| 6897 | { |
| 6898 | return __btrfs_free_reserved_extent(root, start, len, 0); |
| 6899 | } |
| 6900 | |
| 6901 | int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, |
| 6902 | u64 start, u64 len) |
| 6903 | { |
| 6904 | return __btrfs_free_reserved_extent(root, start, len, 1); |
| 6905 | } |
| 6906 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6907 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 6908 | struct btrfs_root *root, |
| 6909 | u64 parent, u64 root_objectid, |
| 6910 | u64 flags, u64 owner, u64 offset, |
| 6911 | struct btrfs_key *ins, int ref_mod) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6912 | { |
| 6913 | int ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6914 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6915 | struct btrfs_extent_item *extent_item; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6916 | struct btrfs_extent_inline_ref *iref; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6917 | struct btrfs_path *path; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6918 | struct extent_buffer *leaf; |
| 6919 | int type; |
| 6920 | u32 size; |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 6921 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6922 | if (parent > 0) |
| 6923 | type = BTRFS_SHARED_DATA_REF_KEY; |
| 6924 | else |
| 6925 | type = BTRFS_EXTENT_DATA_REF_KEY; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 6926 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6927 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6928 | |
| 6929 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 6930 | if (!path) |
| 6931 | return -ENOMEM; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6932 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 6933 | path->leave_spinning = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6934 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 6935 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6936 | if (ret) { |
| 6937 | btrfs_free_path(path); |
| 6938 | return ret; |
| 6939 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 6940 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6941 | leaf = path->nodes[0]; |
| 6942 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6943 | struct btrfs_extent_item); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6944 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
| 6945 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 6946 | btrfs_set_extent_flags(leaf, extent_item, |
| 6947 | flags | BTRFS_EXTENT_FLAG_DATA); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6948 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6949 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
| 6950 | btrfs_set_extent_inline_ref_type(leaf, iref, type); |
| 6951 | if (parent > 0) { |
| 6952 | struct btrfs_shared_data_ref *ref; |
| 6953 | ref = (struct btrfs_shared_data_ref *)(iref + 1); |
| 6954 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 6955 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); |
| 6956 | } else { |
| 6957 | struct btrfs_extent_data_ref *ref; |
| 6958 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); |
| 6959 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); |
| 6960 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); |
| 6961 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); |
| 6962 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); |
| 6963 | } |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 6964 | |
| 6965 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 6966 | btrfs_free_path(path); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 6967 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6968 | /* Always set parent to 0 here since its exclusive anyway. */ |
| 6969 | ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid, |
| 6970 | ins->objectid, ins->offset, |
| 6971 | BTRFS_QGROUP_OPER_ADD_EXCL, 0); |
| 6972 | if (ret) |
| 6973 | return ret; |
| 6974 | |
Liu Bo | c53d613 | 2012-12-27 09:01:19 +0000 | [diff] [blame] | 6975 | ret = update_block_group(root, ins->objectid, ins->offset, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 6976 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 6977 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 6978 | ins->objectid, ins->offset); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 6979 | BUG(); |
| 6980 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 6981 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 6982 | return ret; |
| 6983 | } |
| 6984 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6985 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
| 6986 | struct btrfs_root *root, |
| 6987 | u64 parent, u64 root_objectid, |
| 6988 | u64 flags, struct btrfs_disk_key *key, |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 6989 | int level, struct btrfs_key *ins, |
| 6990 | int no_quota) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 6991 | { |
| 6992 | int ret; |
| 6993 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 6994 | struct btrfs_extent_item *extent_item; |
| 6995 | struct btrfs_tree_block_info *block_info; |
| 6996 | struct btrfs_extent_inline_ref *iref; |
| 6997 | struct btrfs_path *path; |
| 6998 | struct extent_buffer *leaf; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 6999 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7000 | u64 num_bytes = ins->offset; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7001 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 7002 | SKINNY_METADATA); |
| 7003 | |
| 7004 | if (!skinny_metadata) |
| 7005 | size += sizeof(*block_info); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7006 | |
| 7007 | path = btrfs_alloc_path(); |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 7008 | if (!path) { |
| 7009 | btrfs_free_and_pin_reserved_extent(root, ins->objectid, |
| 7010 | root->leafsize); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 7011 | return -ENOMEM; |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 7012 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7013 | |
| 7014 | path->leave_spinning = 1; |
| 7015 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
| 7016 | ins, size); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7017 | if (ret) { |
Josef Bacik | 857cc2f | 2013-10-07 15:21:08 -0400 | [diff] [blame] | 7018 | btrfs_free_and_pin_reserved_extent(root, ins->objectid, |
| 7019 | root->leafsize); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7020 | btrfs_free_path(path); |
| 7021 | return ret; |
| 7022 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7023 | |
| 7024 | leaf = path->nodes[0]; |
| 7025 | extent_item = btrfs_item_ptr(leaf, path->slots[0], |
| 7026 | struct btrfs_extent_item); |
| 7027 | btrfs_set_extent_refs(leaf, extent_item, 1); |
| 7028 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); |
| 7029 | btrfs_set_extent_flags(leaf, extent_item, |
| 7030 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7031 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7032 | if (skinny_metadata) { |
| 7033 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7034 | num_bytes = root->leafsize; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7035 | } else { |
| 7036 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); |
| 7037 | btrfs_set_tree_block_key(leaf, block_info, key); |
| 7038 | btrfs_set_tree_block_level(leaf, block_info, level); |
| 7039 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); |
| 7040 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7041 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7042 | if (parent > 0) { |
| 7043 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 7044 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 7045 | BTRFS_SHARED_BLOCK_REF_KEY); |
| 7046 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); |
| 7047 | } else { |
| 7048 | btrfs_set_extent_inline_ref_type(leaf, iref, |
| 7049 | BTRFS_TREE_BLOCK_REF_KEY); |
| 7050 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); |
| 7051 | } |
| 7052 | |
| 7053 | btrfs_mark_buffer_dirty(leaf); |
| 7054 | btrfs_free_path(path); |
| 7055 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 7056 | if (!no_quota) { |
| 7057 | ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid, |
| 7058 | ins->objectid, num_bytes, |
| 7059 | BTRFS_QGROUP_OPER_ADD_EXCL, 0); |
| 7060 | if (ret) |
| 7061 | return ret; |
| 7062 | } |
| 7063 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7064 | ret = update_block_group(root, ins->objectid, root->leafsize, 1); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7065 | if (ret) { /* -ENOENT, logic error */ |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7066 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 7067 | ins->objectid, ins->offset); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7068 | BUG(); |
| 7069 | } |
Josef Bacik | 0be5dc6 | 2013-10-07 15:18:52 -0400 | [diff] [blame] | 7070 | |
| 7071 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->leafsize); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7072 | return ret; |
| 7073 | } |
| 7074 | |
| 7075 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
| 7076 | struct btrfs_root *root, |
| 7077 | u64 root_objectid, u64 owner, |
| 7078 | u64 offset, struct btrfs_key *ins) |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7079 | { |
| 7080 | int ret; |
Chris Mason | 1c2308f8 | 2008-09-23 13:14:13 -0400 | [diff] [blame] | 7081 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7082 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 7083 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7084 | ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid, |
| 7085 | ins->offset, 0, |
| 7086 | root_objectid, owner, offset, |
| 7087 | BTRFS_ADD_DELAYED_EXTENT, NULL, 0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 7088 | return ret; |
| 7089 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7090 | |
| 7091 | /* |
| 7092 | * this is used by the tree logging recovery code. It records that |
| 7093 | * an extent has been allocated and makes sure to clear the free |
| 7094 | * space cache bits as well |
| 7095 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7096 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
| 7097 | struct btrfs_root *root, |
| 7098 | u64 root_objectid, u64 owner, u64 offset, |
| 7099 | struct btrfs_key *ins) |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7100 | { |
| 7101 | int ret; |
| 7102 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 7103 | |
| 7104 | /* |
| 7105 | * Mixed block groups will exclude before processing the log so we only |
| 7106 | * need to do the exlude dance if this fs isn't mixed. |
| 7107 | */ |
| 7108 | if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) { |
| 7109 | ret = __exclude_logged_extent(root, ins->objectid, ins->offset); |
| 7110 | if (ret) |
| 7111 | return ret; |
| 7112 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7113 | |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7114 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
Josef Bacik | 8c2a1a3 | 2013-06-06 13:19:32 -0400 | [diff] [blame] | 7115 | if (!block_group) |
| 7116 | return -EINVAL; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 7117 | |
Josef Bacik | fb25e91 | 2011-07-26 17:00:46 -0400 | [diff] [blame] | 7118 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, |
| 7119 | RESERVE_ALLOC_NO_ACCOUNT); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7120 | BUG_ON(ret); /* logic error */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7121 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
| 7122 | 0, owner, offset, ins, 1); |
Josef Bacik | b50c6e2 | 2013-04-25 15:55:30 -0400 | [diff] [blame] | 7123 | btrfs_put_block_group(block_group); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 7124 | return ret; |
| 7125 | } |
| 7126 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 7127 | static struct extent_buffer * |
| 7128 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 7129 | u64 bytenr, u32 blocksize, int level) |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7130 | { |
| 7131 | struct extent_buffer *buf; |
| 7132 | |
| 7133 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); |
| 7134 | if (!buf) |
| 7135 | return ERR_PTR(-ENOMEM); |
| 7136 | btrfs_set_header_generation(buf, trans->transid); |
Chris Mason | 85d4e46 | 2011-07-26 16:11:19 -0400 | [diff] [blame] | 7137 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7138 | btrfs_tree_lock(buf); |
| 7139 | clean_tree_block(trans, root, buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 7140 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 7141 | |
| 7142 | btrfs_set_lock_blocking(buf); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7143 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 7144 | |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 7145 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
Yan, Zheng | 8cef4e1 | 2009-11-12 09:33:26 +0000 | [diff] [blame] | 7146 | /* |
| 7147 | * we allow two log transactions at a time, use different |
| 7148 | * EXENT bit to differentiate dirty pages. |
| 7149 | */ |
| 7150 | if (root->log_transid % 2 == 0) |
| 7151 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
| 7152 | buf->start + buf->len - 1, GFP_NOFS); |
| 7153 | else |
| 7154 | set_extent_new(&root->dirty_log_pages, buf->start, |
| 7155 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | d0c803c | 2008-09-11 16:17:57 -0400 | [diff] [blame] | 7156 | } else { |
| 7157 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 7158 | buf->start + buf->len - 1, GFP_NOFS); |
| 7159 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7160 | trans->blocks_used++; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 7161 | /* this returns a buffer locked for blocking */ |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 7162 | return buf; |
| 7163 | } |
| 7164 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7165 | static struct btrfs_block_rsv * |
| 7166 | use_block_rsv(struct btrfs_trans_handle *trans, |
| 7167 | struct btrfs_root *root, u32 blocksize) |
| 7168 | { |
| 7169 | struct btrfs_block_rsv *block_rsv; |
Josef Bacik | 68a8227 | 2011-01-24 21:43:20 +0000 | [diff] [blame] | 7170 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7171 | int ret; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 7172 | bool global_updated = false; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7173 | |
| 7174 | block_rsv = get_block_rsv(trans, root); |
| 7175 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7176 | if (unlikely(block_rsv->size == 0)) |
| 7177 | goto try_reserve; |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 7178 | again: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7179 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
| 7180 | if (!ret) |
| 7181 | return block_rsv; |
| 7182 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7183 | if (block_rsv->failfast) |
| 7184 | return ERR_PTR(ret); |
| 7185 | |
Miao Xie | d88033d | 2013-05-13 13:55:12 +0000 | [diff] [blame] | 7186 | if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { |
| 7187 | global_updated = true; |
| 7188 | update_global_block_rsv(root->fs_info); |
| 7189 | goto again; |
| 7190 | } |
| 7191 | |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7192 | if (btrfs_test_opt(root, ENOSPC_DEBUG)) { |
| 7193 | static DEFINE_RATELIMIT_STATE(_rs, |
| 7194 | DEFAULT_RATELIMIT_INTERVAL * 10, |
| 7195 | /*DEFAULT_RATELIMIT_BURST*/ 1); |
| 7196 | if (__ratelimit(&_rs)) |
| 7197 | WARN(1, KERN_DEBUG |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7198 | "BTRFS: block rsv returned %d\n", ret); |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7199 | } |
| 7200 | try_reserve: |
| 7201 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, |
| 7202 | BTRFS_RESERVE_NO_FLUSH); |
| 7203 | if (!ret) |
| 7204 | return block_rsv; |
| 7205 | /* |
| 7206 | * If we couldn't reserve metadata bytes try and use some from |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 7207 | * the global reserve if its space type is the same as the global |
| 7208 | * reservation. |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7209 | */ |
Miao Xie | 5881cfc | 2013-05-13 13:55:11 +0000 | [diff] [blame] | 7210 | if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL && |
| 7211 | block_rsv->space_info == global_rsv->space_info) { |
Miao Xie | b586b32 | 2013-05-13 13:55:10 +0000 | [diff] [blame] | 7212 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
| 7213 | if (!ret) |
| 7214 | return global_rsv; |
| 7215 | } |
| 7216 | return ERR_PTR(ret); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7217 | } |
| 7218 | |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 7219 | static void unuse_block_rsv(struct btrfs_fs_info *fs_info, |
| 7220 | struct btrfs_block_rsv *block_rsv, u32 blocksize) |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7221 | { |
| 7222 | block_rsv_add_bytes(block_rsv, blocksize, 0); |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 7223 | block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7224 | } |
| 7225 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7226 | /* |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7227 | * finds a free extent and does all the dirty work required for allocation |
| 7228 | * returns the key for the extent through ins, and a tree buffer for |
| 7229 | * the first block of the extent through buf. |
| 7230 | * |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7231 | * returns the tree buffer or NULL. |
| 7232 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7233 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7234 | struct btrfs_root *root, u32 blocksize, |
| 7235 | u64 parent, u64 root_objectid, |
| 7236 | struct btrfs_disk_key *key, int level, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 7237 | u64 hint, u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7238 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 7239 | struct btrfs_key ins; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7240 | struct btrfs_block_rsv *block_rsv; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 7241 | struct extent_buffer *buf; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7242 | u64 flags = 0; |
| 7243 | int ret; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7244 | bool skinny_metadata = btrfs_fs_incompat(root->fs_info, |
| 7245 | SKINNY_METADATA); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7246 | |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 7247 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
| 7248 | if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state))) { |
| 7249 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
| 7250 | blocksize, level); |
| 7251 | if (!IS_ERR(buf)) |
| 7252 | root->alloc_bytenr += blocksize; |
| 7253 | return buf; |
| 7254 | } |
| 7255 | #endif |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7256 | block_rsv = use_block_rsv(trans, root, blocksize); |
| 7257 | if (IS_ERR(block_rsv)) |
| 7258 | return ERR_CAST(block_rsv); |
| 7259 | |
Josef Bacik | 0036158 | 2013-08-14 14:02:47 -0400 | [diff] [blame] | 7260 | ret = btrfs_reserve_extent(root, blocksize, blocksize, |
Josef Bacik | 81c9ad2 | 2012-01-18 10:56:06 -0500 | [diff] [blame] | 7261 | empty_size, hint, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7262 | if (ret) { |
Josef Bacik | 8c2a3ca | 2012-01-10 10:31:31 -0500 | [diff] [blame] | 7263 | unuse_block_rsv(root->fs_info, block_rsv, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 7264 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7265 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 7266 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 7267 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
| 7268 | blocksize, level); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7269 | BUG_ON(IS_ERR(buf)); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7270 | |
| 7271 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
| 7272 | if (parent == 0) |
| 7273 | parent = ins.objectid; |
| 7274 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 7275 | } else |
| 7276 | BUG_ON(parent > 0); |
| 7277 | |
| 7278 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { |
| 7279 | struct btrfs_delayed_extent_op *extent_op; |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 7280 | extent_op = btrfs_alloc_delayed_extent_op(); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7281 | BUG_ON(!extent_op); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7282 | if (key) |
| 7283 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); |
| 7284 | else |
| 7285 | memset(&extent_op->key, 0, sizeof(extent_op->key)); |
| 7286 | extent_op->flags_to_set = flags; |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7287 | if (skinny_metadata) |
| 7288 | extent_op->update_key = 0; |
| 7289 | else |
| 7290 | extent_op->update_key = 1; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7291 | extent_op->update_flags = 1; |
| 7292 | extent_op->is_data = 0; |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 7293 | extent_op->level = level; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7294 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7295 | ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, |
| 7296 | ins.objectid, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7297 | ins.offset, parent, root_objectid, |
| 7298 | level, BTRFS_ADD_DELAYED_EXTENT, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 7299 | extent_op, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7300 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7301 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7302 | return buf; |
| 7303 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 7304 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7305 | struct walk_control { |
| 7306 | u64 refs[BTRFS_MAX_LEVEL]; |
| 7307 | u64 flags[BTRFS_MAX_LEVEL]; |
| 7308 | struct btrfs_key update_progress; |
| 7309 | int stage; |
| 7310 | int level; |
| 7311 | int shared_level; |
| 7312 | int update_ref; |
| 7313 | int keep_locks; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7314 | int reada_slot; |
| 7315 | int reada_count; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7316 | int for_reloc; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7317 | }; |
| 7318 | |
| 7319 | #define DROP_REFERENCE 1 |
| 7320 | #define UPDATE_BACKREF 2 |
| 7321 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7322 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
| 7323 | struct btrfs_root *root, |
| 7324 | struct walk_control *wc, |
| 7325 | struct btrfs_path *path) |
| 7326 | { |
| 7327 | u64 bytenr; |
| 7328 | u64 generation; |
| 7329 | u64 refs; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7330 | u64 flags; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7331 | u32 nritems; |
| 7332 | u32 blocksize; |
| 7333 | struct btrfs_key key; |
| 7334 | struct extent_buffer *eb; |
| 7335 | int ret; |
| 7336 | int slot; |
| 7337 | int nread = 0; |
| 7338 | |
| 7339 | if (path->slots[wc->level] < wc->reada_slot) { |
| 7340 | wc->reada_count = wc->reada_count * 2 / 3; |
| 7341 | wc->reada_count = max(wc->reada_count, 2); |
| 7342 | } else { |
| 7343 | wc->reada_count = wc->reada_count * 3 / 2; |
| 7344 | wc->reada_count = min_t(int, wc->reada_count, |
| 7345 | BTRFS_NODEPTRS_PER_BLOCK(root)); |
| 7346 | } |
| 7347 | |
| 7348 | eb = path->nodes[wc->level]; |
| 7349 | nritems = btrfs_header_nritems(eb); |
| 7350 | blocksize = btrfs_level_size(root, wc->level - 1); |
| 7351 | |
| 7352 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
| 7353 | if (nread >= wc->reada_count) |
| 7354 | break; |
| 7355 | |
| 7356 | cond_resched(); |
| 7357 | bytenr = btrfs_node_blockptr(eb, slot); |
| 7358 | generation = btrfs_node_ptr_generation(eb, slot); |
| 7359 | |
| 7360 | if (slot == path->slots[wc->level]) |
| 7361 | goto reada; |
| 7362 | |
| 7363 | if (wc->stage == UPDATE_BACKREF && |
| 7364 | generation <= root->root_key.offset) |
| 7365 | continue; |
| 7366 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7367 | /* We don't lock the tree block, it's OK to be racy here */ |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7368 | ret = btrfs_lookup_extent_info(trans, root, bytenr, |
| 7369 | wc->level - 1, 1, &refs, |
| 7370 | &flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7371 | /* We don't care about errors in readahead. */ |
| 7372 | if (ret < 0) |
| 7373 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7374 | BUG_ON(refs == 0); |
| 7375 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7376 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7377 | if (refs == 1) |
| 7378 | goto reada; |
| 7379 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7380 | if (wc->level == 1 && |
| 7381 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 7382 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7383 | if (!wc->update_ref || |
| 7384 | generation <= root->root_key.offset) |
| 7385 | continue; |
| 7386 | btrfs_node_key_to_cpu(eb, &key, slot); |
| 7387 | ret = btrfs_comp_cpu_keys(&key, |
| 7388 | &wc->update_progress); |
| 7389 | if (ret < 0) |
| 7390 | continue; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7391 | } else { |
| 7392 | if (wc->level == 1 && |
| 7393 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 7394 | continue; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7395 | } |
| 7396 | reada: |
| 7397 | ret = readahead_tree_block(root, bytenr, blocksize, |
| 7398 | generation); |
| 7399 | if (ret) |
| 7400 | break; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7401 | nread++; |
| 7402 | } |
| 7403 | wc->reada_slot = slot; |
| 7404 | } |
| 7405 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 7406 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 7407 | * helper to process tree block while walking down the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7408 | * |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7409 | * when wc->stage == UPDATE_BACKREF, this function updates |
| 7410 | * back refs for pointers in the block. |
| 7411 | * |
| 7412 | * NOTE: return value 1 means we should stop walking down. |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7413 | */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7414 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
| 7415 | struct btrfs_root *root, |
| 7416 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7417 | struct walk_control *wc, int lookup_info) |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7418 | { |
| 7419 | int level = wc->level; |
| 7420 | struct extent_buffer *eb = path->nodes[level]; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7421 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 7422 | int ret; |
| 7423 | |
| 7424 | if (wc->stage == UPDATE_BACKREF && |
| 7425 | btrfs_header_owner(eb) != root->root_key.objectid) |
| 7426 | return 1; |
| 7427 | |
| 7428 | /* |
| 7429 | * when reference count of tree block is 1, it won't increase |
| 7430 | * again. once full backref flag is set, we never clear it. |
| 7431 | */ |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7432 | if (lookup_info && |
| 7433 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || |
| 7434 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7435 | BUG_ON(!path->locks[level]); |
| 7436 | ret = btrfs_lookup_extent_info(trans, root, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7437 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7438 | &wc->refs[level], |
| 7439 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7440 | BUG_ON(ret == -ENOMEM); |
| 7441 | if (ret) |
| 7442 | return ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7443 | BUG_ON(wc->refs[level] == 0); |
| 7444 | } |
| 7445 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7446 | if (wc->stage == DROP_REFERENCE) { |
| 7447 | if (wc->refs[level] > 1) |
| 7448 | return 1; |
| 7449 | |
| 7450 | if (path->locks[level] && !wc->keep_locks) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7451 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7452 | path->locks[level] = 0; |
| 7453 | } |
| 7454 | return 0; |
| 7455 | } |
| 7456 | |
| 7457 | /* wc->stage == UPDATE_BACKREF */ |
| 7458 | if (!(wc->flags[level] & flag)) { |
| 7459 | BUG_ON(!path->locks[level]); |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7460 | ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7461 | BUG_ON(ret); /* -ENOMEM */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7462 | ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7463 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7464 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 7465 | eb->len, flag, |
| 7466 | btrfs_header_level(eb), 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7467 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7468 | wc->flags[level] |= flag; |
| 7469 | } |
| 7470 | |
| 7471 | /* |
| 7472 | * the block is shared by multiple trees, so it's not good to |
| 7473 | * keep the tree lock |
| 7474 | */ |
| 7475 | if (path->locks[level] && level > 0) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7476 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7477 | path->locks[level] = 0; |
| 7478 | } |
| 7479 | return 0; |
| 7480 | } |
| 7481 | |
| 7482 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 7483 | * helper to process tree block pointer. |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7484 | * |
| 7485 | * when wc->stage == DROP_REFERENCE, this function checks |
| 7486 | * reference count of the block pointed to. if the block |
| 7487 | * is shared and we need update back refs for the subtree |
| 7488 | * rooted at the block, this function changes wc->stage to |
| 7489 | * UPDATE_BACKREF. if the block is shared and there is no |
| 7490 | * need to update back, this function drops the reference |
| 7491 | * to the block. |
| 7492 | * |
| 7493 | * NOTE: return value 1 means we should stop walking down. |
| 7494 | */ |
| 7495 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, |
| 7496 | struct btrfs_root *root, |
| 7497 | struct btrfs_path *path, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7498 | struct walk_control *wc, int *lookup_info) |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7499 | { |
| 7500 | u64 bytenr; |
| 7501 | u64 generation; |
| 7502 | u64 parent; |
| 7503 | u32 blocksize; |
| 7504 | struct btrfs_key key; |
| 7505 | struct extent_buffer *next; |
| 7506 | int level = wc->level; |
| 7507 | int reada = 0; |
| 7508 | int ret = 0; |
| 7509 | |
| 7510 | generation = btrfs_node_ptr_generation(path->nodes[level], |
| 7511 | path->slots[level]); |
| 7512 | /* |
| 7513 | * if the lower level block was created before the snapshot |
| 7514 | * was created, we know there is no need to update back refs |
| 7515 | * for the subtree |
| 7516 | */ |
| 7517 | if (wc->stage == UPDATE_BACKREF && |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7518 | generation <= root->root_key.offset) { |
| 7519 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7520 | return 1; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7521 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7522 | |
| 7523 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); |
| 7524 | blocksize = btrfs_level_size(root, level - 1); |
| 7525 | |
| 7526 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
| 7527 | if (!next) { |
| 7528 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 7529 | if (!next) |
| 7530 | return -ENOMEM; |
Josef Bacik | b2aaaa3 | 2013-07-05 17:05:38 -0400 | [diff] [blame] | 7531 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
| 7532 | level - 1); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7533 | reada = 1; |
| 7534 | } |
| 7535 | btrfs_tree_lock(next); |
| 7536 | btrfs_set_lock_blocking(next); |
| 7537 | |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7538 | ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1, |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7539 | &wc->refs[level - 1], |
| 7540 | &wc->flags[level - 1]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7541 | if (ret < 0) { |
| 7542 | btrfs_tree_unlock(next); |
| 7543 | return ret; |
| 7544 | } |
| 7545 | |
Simon Kirby | c2cf52e | 2013-03-19 22:41:23 +0000 | [diff] [blame] | 7546 | if (unlikely(wc->refs[level - 1] == 0)) { |
| 7547 | btrfs_err(root->fs_info, "Missing references."); |
| 7548 | BUG(); |
| 7549 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7550 | *lookup_info = 0; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7551 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7552 | if (wc->stage == DROP_REFERENCE) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7553 | if (wc->refs[level - 1] > 1) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7554 | if (level == 1 && |
| 7555 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 7556 | goto skip; |
| 7557 | |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7558 | if (!wc->update_ref || |
| 7559 | generation <= root->root_key.offset) |
| 7560 | goto skip; |
| 7561 | |
| 7562 | btrfs_node_key_to_cpu(path->nodes[level], &key, |
| 7563 | path->slots[level]); |
| 7564 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); |
| 7565 | if (ret < 0) |
| 7566 | goto skip; |
| 7567 | |
| 7568 | wc->stage = UPDATE_BACKREF; |
| 7569 | wc->shared_level = level - 1; |
| 7570 | } |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7571 | } else { |
| 7572 | if (level == 1 && |
| 7573 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) |
| 7574 | goto skip; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7575 | } |
| 7576 | |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 7577 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7578 | btrfs_tree_unlock(next); |
| 7579 | free_extent_buffer(next); |
| 7580 | next = NULL; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7581 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7582 | } |
| 7583 | |
| 7584 | if (!next) { |
| 7585 | if (reada && level == 1) |
| 7586 | reada_walk_down(trans, root, wc, path); |
| 7587 | next = read_tree_block(root, bytenr, blocksize, generation); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 7588 | if (!next || !extent_buffer_uptodate(next)) { |
| 7589 | free_extent_buffer(next); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 7590 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 7591 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7592 | btrfs_tree_lock(next); |
| 7593 | btrfs_set_lock_blocking(next); |
| 7594 | } |
| 7595 | |
| 7596 | level--; |
| 7597 | BUG_ON(level != btrfs_header_level(next)); |
| 7598 | path->nodes[level] = next; |
| 7599 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7600 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7601 | wc->level = level; |
| 7602 | if (wc->level == 1) |
| 7603 | wc->reada_slot = 0; |
| 7604 | return 0; |
| 7605 | skip: |
| 7606 | wc->refs[level - 1] = 0; |
| 7607 | wc->flags[level - 1] = 0; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7608 | if (wc->stage == DROP_REFERENCE) { |
| 7609 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 7610 | parent = path->nodes[level]->start; |
| 7611 | } else { |
| 7612 | BUG_ON(root->root_key.objectid != |
| 7613 | btrfs_header_owner(path->nodes[level])); |
| 7614 | parent = 0; |
| 7615 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7616 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7617 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7618 | root->root_key.objectid, level - 1, 0, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7619 | BUG_ON(ret); /* -ENOMEM */ |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7620 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7621 | btrfs_tree_unlock(next); |
| 7622 | free_extent_buffer(next); |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7623 | *lookup_info = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7624 | return 1; |
| 7625 | } |
| 7626 | |
| 7627 | /* |
Liu Bo | 2c016dc | 2012-12-26 15:32:17 +0800 | [diff] [blame] | 7628 | * helper to process tree block while walking up the tree. |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7629 | * |
| 7630 | * when wc->stage == DROP_REFERENCE, this function drops |
| 7631 | * reference count on the block. |
| 7632 | * |
| 7633 | * when wc->stage == UPDATE_BACKREF, this function changes |
| 7634 | * wc->stage back to DROP_REFERENCE if we changed wc->stage |
| 7635 | * to UPDATE_BACKREF previously while processing the block. |
| 7636 | * |
| 7637 | * NOTE: return value 1 means we should stop walking up. |
| 7638 | */ |
| 7639 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, |
| 7640 | struct btrfs_root *root, |
| 7641 | struct btrfs_path *path, |
| 7642 | struct walk_control *wc) |
| 7643 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7644 | int ret; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7645 | int level = wc->level; |
| 7646 | struct extent_buffer *eb = path->nodes[level]; |
| 7647 | u64 parent = 0; |
| 7648 | |
| 7649 | if (wc->stage == UPDATE_BACKREF) { |
| 7650 | BUG_ON(wc->shared_level < level); |
| 7651 | if (level < wc->shared_level) |
| 7652 | goto out; |
| 7653 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7654 | ret = find_next_key(path, level + 1, &wc->update_progress); |
| 7655 | if (ret > 0) |
| 7656 | wc->update_ref = 0; |
| 7657 | |
| 7658 | wc->stage = DROP_REFERENCE; |
| 7659 | wc->shared_level = -1; |
| 7660 | path->slots[level] = 0; |
| 7661 | |
| 7662 | /* |
| 7663 | * check reference count again if the block isn't locked. |
| 7664 | * we should start walking down the tree again if reference |
| 7665 | * count is one. |
| 7666 | */ |
| 7667 | if (!path->locks[level]) { |
| 7668 | BUG_ON(level == 0); |
| 7669 | btrfs_tree_lock(eb); |
| 7670 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7671 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7672 | |
| 7673 | ret = btrfs_lookup_extent_info(trans, root, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7674 | eb->start, level, 1, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7675 | &wc->refs[level], |
| 7676 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7677 | if (ret < 0) { |
| 7678 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 7679 | path->locks[level] = 0; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7680 | return ret; |
| 7681 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7682 | BUG_ON(wc->refs[level] == 0); |
| 7683 | if (wc->refs[level] == 1) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7684 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
Liu Bo | 3268a24 | 2012-12-28 09:33:19 +0000 | [diff] [blame] | 7685 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7686 | return 1; |
| 7687 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7688 | } |
| 7689 | } |
| 7690 | |
| 7691 | /* wc->stage == DROP_REFERENCE */ |
| 7692 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); |
| 7693 | |
| 7694 | if (wc->refs[level] == 1) { |
| 7695 | if (level == 0) { |
| 7696 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7697 | ret = btrfs_dec_ref(trans, root, eb, 1, |
| 7698 | wc->for_reloc); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7699 | else |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7700 | ret = btrfs_dec_ref(trans, root, eb, 0, |
| 7701 | wc->for_reloc); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7702 | BUG_ON(ret); /* -ENOMEM */ |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7703 | } |
| 7704 | /* make block locked assertion in clean_tree_block happy */ |
| 7705 | if (!path->locks[level] && |
| 7706 | btrfs_header_generation(eb) == trans->transid) { |
| 7707 | btrfs_tree_lock(eb); |
| 7708 | btrfs_set_lock_blocking(eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7709 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7710 | } |
| 7711 | clean_tree_block(trans, root, eb); |
| 7712 | } |
| 7713 | |
| 7714 | if (eb == root->node) { |
| 7715 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 7716 | parent = eb->start; |
| 7717 | else |
| 7718 | BUG_ON(root->root_key.objectid != |
| 7719 | btrfs_header_owner(eb)); |
| 7720 | } else { |
| 7721 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) |
| 7722 | parent = path->nodes[level + 1]->start; |
| 7723 | else |
| 7724 | BUG_ON(root->root_key.objectid != |
| 7725 | btrfs_header_owner(path->nodes[level + 1])); |
| 7726 | } |
| 7727 | |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 7728 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7729 | out: |
| 7730 | wc->refs[level] = 0; |
| 7731 | wc->flags[level] = 0; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 7732 | return 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7733 | } |
| 7734 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7735 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, |
| 7736 | struct btrfs_root *root, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7737 | struct btrfs_path *path, |
| 7738 | struct walk_control *wc) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7739 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7740 | int level = wc->level; |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7741 | int lookup_info = 1; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7742 | int ret; |
| 7743 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7744 | while (level >= 0) { |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7745 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7746 | if (ret > 0) |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7747 | break; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7748 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7749 | if (level == 0) |
| 7750 | break; |
| 7751 | |
Yan, Zheng | 7a7965f | 2010-02-01 02:41:17 +0000 | [diff] [blame] | 7752 | if (path->slots[level] >= |
| 7753 | btrfs_header_nritems(path->nodes[level])) |
| 7754 | break; |
| 7755 | |
Yan, Zheng | 94fcca9 | 2009-10-09 09:25:16 -0400 | [diff] [blame] | 7756 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7757 | if (ret > 0) { |
| 7758 | path->slots[level]++; |
| 7759 | continue; |
Miao Xie | 90d2c51d | 2010-03-25 12:37:12 +0000 | [diff] [blame] | 7760 | } else if (ret < 0) |
| 7761 | return ret; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7762 | level = wc->level; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7763 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7764 | return 0; |
| 7765 | } |
| 7766 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 7767 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 7768 | struct btrfs_root *root, |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7769 | struct btrfs_path *path, |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7770 | struct walk_control *wc, int max_level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7771 | { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7772 | int level = wc->level; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7773 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7774 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7775 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
| 7776 | while (level < max_level && path->nodes[level]) { |
| 7777 | wc->level = level; |
| 7778 | if (path->slots[level] + 1 < |
| 7779 | btrfs_header_nritems(path->nodes[level])) { |
| 7780 | path->slots[level]++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7781 | return 0; |
| 7782 | } else { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7783 | ret = walk_up_proc(trans, root, path, wc); |
| 7784 | if (ret > 0) |
| 7785 | return 0; |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 7786 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7787 | if (path->locks[level]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7788 | btrfs_tree_unlock_rw(path->nodes[level], |
| 7789 | path->locks[level]); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7790 | path->locks[level] = 0; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 7791 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7792 | free_extent_buffer(path->nodes[level]); |
| 7793 | path->nodes[level] = NULL; |
| 7794 | level++; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7795 | } |
| 7796 | } |
| 7797 | return 1; |
| 7798 | } |
| 7799 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 7800 | /* |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7801 | * drop a subvolume tree. |
| 7802 | * |
| 7803 | * this function traverses the tree freeing any blocks that only |
| 7804 | * referenced by the tree. |
| 7805 | * |
| 7806 | * when a shared tree block is found. this function decreases its |
| 7807 | * reference count by one. if update_ref is true, this function |
| 7808 | * also make sure backrefs for the shared block and all lower level |
| 7809 | * blocks are properly updated. |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 7810 | * |
| 7811 | * If called with for_reloc == 0, may exit early with -EAGAIN |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 7812 | */ |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 7813 | int btrfs_drop_snapshot(struct btrfs_root *root, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7814 | struct btrfs_block_rsv *block_rsv, int update_ref, |
| 7815 | int for_reloc) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7816 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7817 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7818 | struct btrfs_trans_handle *trans; |
| 7819 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7820 | struct btrfs_root_item *root_item = &root->root_item; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7821 | struct walk_control *wc; |
| 7822 | struct btrfs_key key; |
| 7823 | int err = 0; |
| 7824 | int ret; |
| 7825 | int level; |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 7826 | bool root_dropped = false; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7827 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 7828 | path = btrfs_alloc_path(); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 7829 | if (!path) { |
| 7830 | err = -ENOMEM; |
| 7831 | goto out; |
| 7832 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7833 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7834 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 7835 | if (!wc) { |
| 7836 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 7837 | err = -ENOMEM; |
| 7838 | goto out; |
Mark Fasheh | 38a1a91 | 2011-07-13 10:59:59 -0700 | [diff] [blame] | 7839 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7840 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7841 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7842 | if (IS_ERR(trans)) { |
| 7843 | err = PTR_ERR(trans); |
| 7844 | goto out_free; |
| 7845 | } |
Tsutomu Itoh | 98d5dc1 | 2011-01-20 06:19:37 +0000 | [diff] [blame] | 7846 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7847 | if (block_rsv) |
| 7848 | trans->block_rsv = block_rsv; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7849 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7850 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7851 | level = btrfs_header_level(root->node); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7852 | path->nodes[level] = btrfs_lock_root_node(root); |
| 7853 | btrfs_set_lock_blocking(path->nodes[level]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7854 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 7855 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7856 | memset(&wc->update_progress, 0, |
| 7857 | sizeof(wc->update_progress)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7858 | } else { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7859 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7860 | memcpy(&wc->update_progress, &key, |
| 7861 | sizeof(wc->update_progress)); |
| 7862 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 7863 | level = root_item->drop_level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7864 | BUG_ON(level == 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 7865 | path->lowest_level = level; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7866 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 7867 | path->lowest_level = 0; |
| 7868 | if (ret < 0) { |
| 7869 | err = ret; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7870 | goto out_end_trans; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 7871 | } |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7872 | WARN_ON(ret > 0); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7873 | |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 7874 | /* |
| 7875 | * unlock our path, this is safe because only this |
| 7876 | * function is allowed to delete this snapshot |
| 7877 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 7878 | btrfs_unlock_up_safe(path, 0); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 7879 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7880 | level = btrfs_header_level(root->node); |
| 7881 | while (1) { |
| 7882 | btrfs_tree_lock(path->nodes[level]); |
| 7883 | btrfs_set_lock_blocking(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 7884 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7885 | |
| 7886 | ret = btrfs_lookup_extent_info(trans, root, |
| 7887 | path->nodes[level]->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 7888 | level, 1, &wc->refs[level], |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7889 | &wc->flags[level]); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7890 | if (ret < 0) { |
| 7891 | err = ret; |
| 7892 | goto out_end_trans; |
| 7893 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7894 | BUG_ON(wc->refs[level] == 0); |
| 7895 | |
| 7896 | if (level == root_item->drop_level) |
| 7897 | break; |
| 7898 | |
| 7899 | btrfs_tree_unlock(path->nodes[level]); |
Josef Bacik | fec386a | 2013-07-15 12:41:42 -0400 | [diff] [blame] | 7900 | path->locks[level] = 0; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7901 | WARN_ON(wc->refs[level] != 1); |
| 7902 | level--; |
| 7903 | } |
| 7904 | } |
| 7905 | |
| 7906 | wc->level = level; |
| 7907 | wc->shared_level = -1; |
| 7908 | wc->stage = DROP_REFERENCE; |
| 7909 | wc->update_ref = update_ref; |
| 7910 | wc->keep_locks = 0; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 7911 | wc->for_reloc = for_reloc; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 7912 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7913 | |
| 7914 | while (1) { |
David Sterba | 9d1a2a3 | 2013-03-12 15:13:28 +0000 | [diff] [blame] | 7915 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7916 | ret = walk_down_tree(trans, root, path, wc); |
| 7917 | if (ret < 0) { |
| 7918 | err = ret; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 7919 | break; |
| 7920 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7921 | |
| 7922 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
| 7923 | if (ret < 0) { |
| 7924 | err = ret; |
| 7925 | break; |
| 7926 | } |
| 7927 | |
| 7928 | if (ret > 0) { |
| 7929 | BUG_ON(wc->stage != DROP_REFERENCE); |
| 7930 | break; |
| 7931 | } |
| 7932 | |
| 7933 | if (wc->stage == DROP_REFERENCE) { |
| 7934 | level = wc->level; |
| 7935 | btrfs_node_key(path->nodes[level], |
| 7936 | &root_item->drop_progress, |
| 7937 | path->slots[level]); |
| 7938 | root_item->drop_level = level; |
| 7939 | } |
| 7940 | |
| 7941 | BUG_ON(wc->level == 0); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 7942 | if (btrfs_should_end_transaction(trans, tree_root) || |
| 7943 | (!for_reloc && btrfs_need_cleaner_sleep(root))) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7944 | ret = btrfs_update_root(trans, tree_root, |
| 7945 | &root->root_key, |
| 7946 | root_item); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7947 | if (ret) { |
| 7948 | btrfs_abort_transaction(trans, tree_root, ret); |
| 7949 | err = ret; |
| 7950 | goto out_end_trans; |
| 7951 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7952 | |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7953 | btrfs_end_transaction_throttle(trans, tree_root); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 7954 | if (!for_reloc && btrfs_need_cleaner_sleep(root)) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 7955 | pr_debug("BTRFS: drop snapshot early exit\n"); |
Josef Bacik | 3c8f242 | 2013-07-15 11:57:06 -0400 | [diff] [blame] | 7956 | err = -EAGAIN; |
| 7957 | goto out_free; |
| 7958 | } |
| 7959 | |
Yan, Zheng | a22285a | 2010-05-16 10:48:46 -0400 | [diff] [blame] | 7960 | trans = btrfs_start_transaction(tree_root, 0); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7961 | if (IS_ERR(trans)) { |
| 7962 | err = PTR_ERR(trans); |
| 7963 | goto out_free; |
| 7964 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 7965 | if (block_rsv) |
| 7966 | trans->block_rsv = block_rsv; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 7967 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 7968 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 7969 | btrfs_release_path(path); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7970 | if (err) |
| 7971 | goto out_end_trans; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7972 | |
| 7973 | ret = btrfs_del_root(trans, tree_root, &root->root_key); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7974 | if (ret) { |
| 7975 | btrfs_abort_transaction(trans, tree_root, ret); |
| 7976 | goto out_end_trans; |
| 7977 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 7978 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7979 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7980 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
| 7981 | NULL, NULL); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 7982 | if (ret < 0) { |
| 7983 | btrfs_abort_transaction(trans, tree_root, ret); |
| 7984 | err = ret; |
| 7985 | goto out_end_trans; |
| 7986 | } else if (ret > 0) { |
Josef Bacik | 84cd948 | 2010-12-08 12:24:01 -0500 | [diff] [blame] | 7987 | /* if we fail to delete the orphan item this time |
| 7988 | * around, it'll get picked up the next time. |
| 7989 | * |
| 7990 | * The most common failure here is just -ENOENT. |
| 7991 | */ |
| 7992 | btrfs_del_orphan_item(trans, tree_root, |
| 7993 | root->root_key.objectid); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7994 | } |
| 7995 | } |
| 7996 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 7997 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
Miao Xie | cb517ea | 2013-05-15 07:48:19 +0000 | [diff] [blame] | 7998 | btrfs_drop_and_free_fs_root(tree_root->fs_info, root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 7999 | } else { |
| 8000 | free_extent_buffer(root->node); |
| 8001 | free_extent_buffer(root->commit_root); |
Miao Xie | b0feb9d | 2013-05-15 07:48:20 +0000 | [diff] [blame] | 8002 | btrfs_put_fs_root(root); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 8003 | } |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8004 | root_dropped = true; |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8005 | out_end_trans: |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 8006 | btrfs_end_transaction_throttle(trans, tree_root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8007 | out_free: |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8008 | kfree(wc); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 8009 | btrfs_free_path(path); |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 8010 | out: |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8011 | /* |
| 8012 | * So if we need to stop dropping the snapshot for whatever reason we |
| 8013 | * need to make sure to add it back to the dead root list so that we |
| 8014 | * keep trying to do the work later. This also cleans up roots if we |
| 8015 | * don't have it in the radix (like when we recover after a power fail |
| 8016 | * or unmount) so we don't leak memory. |
| 8017 | */ |
Josef Bacik | b37b39c | 2013-07-23 16:57:15 -0400 | [diff] [blame] | 8018 | if (!for_reloc && root_dropped == false) |
Josef Bacik | d29a9f6 | 2013-07-17 19:30:20 -0400 | [diff] [blame] | 8019 | btrfs_add_dead_root(root); |
Wang Shilong | 90515e7 | 2014-01-07 17:26:58 +0800 | [diff] [blame] | 8020 | if (err && err != -EAGAIN) |
Tsutomu Itoh | cb1b69f | 2011-08-09 07:11:13 +0000 | [diff] [blame] | 8021 | btrfs_std_error(root->fs_info, err); |
Jeff Mahoney | 2c53679 | 2011-10-03 23:22:41 -0400 | [diff] [blame] | 8022 | return err; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 8023 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8024 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8025 | /* |
| 8026 | * drop subtree rooted at tree block 'node'. |
| 8027 | * |
| 8028 | * NOTE: this function will unlock and release tree block 'node' |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8029 | * only used by relocation code |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8030 | */ |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8031 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
| 8032 | struct btrfs_root *root, |
| 8033 | struct extent_buffer *node, |
| 8034 | struct extent_buffer *parent) |
| 8035 | { |
| 8036 | struct btrfs_path *path; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8037 | struct walk_control *wc; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8038 | int level; |
| 8039 | int parent_level; |
| 8040 | int ret = 0; |
| 8041 | int wret; |
| 8042 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8043 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
| 8044 | |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8045 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 8046 | if (!path) |
| 8047 | return -ENOMEM; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8048 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8049 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 8050 | if (!wc) { |
| 8051 | btrfs_free_path(path); |
| 8052 | return -ENOMEM; |
| 8053 | } |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8054 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 8055 | btrfs_assert_tree_locked(parent); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8056 | parent_level = btrfs_header_level(parent); |
| 8057 | extent_buffer_get(parent); |
| 8058 | path->nodes[parent_level] = parent; |
| 8059 | path->slots[parent_level] = btrfs_header_nritems(parent); |
| 8060 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 8061 | btrfs_assert_tree_locked(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8062 | level = btrfs_header_level(node); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8063 | path->nodes[level] = node; |
| 8064 | path->slots[level] = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 8065 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8066 | |
| 8067 | wc->refs[parent_level] = 1; |
| 8068 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 8069 | wc->level = level; |
| 8070 | wc->shared_level = -1; |
| 8071 | wc->stage = DROP_REFERENCE; |
| 8072 | wc->update_ref = 0; |
| 8073 | wc->keep_locks = 1; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 8074 | wc->for_reloc = 1; |
Yan, Zheng | 1c4850e | 2009-09-21 15:55:59 -0400 | [diff] [blame] | 8075 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8076 | |
| 8077 | while (1) { |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8078 | wret = walk_down_tree(trans, root, path, wc); |
| 8079 | if (wret < 0) { |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8080 | ret = wret; |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8081 | break; |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8082 | } |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8083 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8084 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8085 | if (wret < 0) |
| 8086 | ret = wret; |
| 8087 | if (wret != 0) |
| 8088 | break; |
| 8089 | } |
| 8090 | |
Yan Zheng | 2c47e605 | 2009-06-27 21:07:35 -0400 | [diff] [blame] | 8091 | kfree(wc); |
Yan Zheng | f82d02d | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8092 | btrfs_free_path(path); |
| 8093 | return ret; |
| 8094 | } |
| 8095 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8096 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
| 8097 | { |
| 8098 | u64 num_devices; |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8099 | u64 stripped; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8100 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8101 | /* |
| 8102 | * if restripe for this chunk_type is on pick target profile and |
| 8103 | * return, otherwise do the usual balance |
| 8104 | */ |
| 8105 | stripped = get_restripe_target(root->fs_info, flags); |
| 8106 | if (stripped) |
| 8107 | return extended_to_chunk(stripped); |
Ilya Dryomov | e4d8ec0 | 2012-01-16 22:04:48 +0200 | [diff] [blame] | 8108 | |
Chris Mason | cd02dca | 2010-12-13 14:56:23 -0500 | [diff] [blame] | 8109 | /* |
| 8110 | * we add in the count of missing devices because we want |
| 8111 | * to make sure that any RAID levels on a degraded FS |
| 8112 | * continue to be honored. |
| 8113 | */ |
| 8114 | num_devices = root->fs_info->fs_devices->rw_devices + |
| 8115 | root->fs_info->fs_devices->missing_devices; |
| 8116 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8117 | stripped = BTRFS_BLOCK_GROUP_RAID0 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8118 | BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 | |
Ilya Dryomov | fc67c45 | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8119 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
| 8120 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8121 | if (num_devices == 1) { |
| 8122 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 8123 | stripped = flags & ~stripped; |
| 8124 | |
| 8125 | /* turn raid0 into single device chunks */ |
| 8126 | if (flags & BTRFS_BLOCK_GROUP_RAID0) |
| 8127 | return stripped; |
| 8128 | |
| 8129 | /* turn mirroring into duplication */ |
| 8130 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 8131 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8132 | return stripped | BTRFS_BLOCK_GROUP_DUP; |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8133 | } else { |
| 8134 | /* they already had raid on here, just return */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8135 | if (flags & stripped) |
| 8136 | return flags; |
| 8137 | |
| 8138 | stripped |= BTRFS_BLOCK_GROUP_DUP; |
| 8139 | stripped = flags & ~stripped; |
| 8140 | |
| 8141 | /* switch duplicated blocks with raid1 */ |
| 8142 | if (flags & BTRFS_BLOCK_GROUP_DUP) |
| 8143 | return stripped | BTRFS_BLOCK_GROUP_RAID1; |
| 8144 | |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 8145 | /* this is drive concat, leave it alone */ |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8146 | } |
Ilya Dryomov | e3176ca | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 8147 | |
Chris Mason | ec44a35 | 2008-04-28 15:29:52 -0400 | [diff] [blame] | 8148 | return flags; |
| 8149 | } |
| 8150 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8151 | 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] | 8152 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8153 | struct btrfs_space_info *sinfo = cache->space_info; |
| 8154 | u64 num_bytes; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8155 | u64 min_allocable_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8156 | int ret = -ENOSPC; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8157 | |
Chris Mason | c286ac4 | 2008-07-22 23:06:41 -0400 | [diff] [blame] | 8158 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8159 | /* |
| 8160 | * We need some metadata space and system metadata space for |
| 8161 | * allocating chunks in some corner cases until we force to set |
| 8162 | * it to be readonly. |
| 8163 | */ |
| 8164 | if ((sinfo->flags & |
| 8165 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && |
| 8166 | !force) |
| 8167 | min_allocable_bytes = 1 * 1024 * 1024; |
| 8168 | else |
| 8169 | min_allocable_bytes = 0; |
| 8170 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8171 | spin_lock(&sinfo->lock); |
| 8172 | spin_lock(&cache->lock); |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 8173 | |
| 8174 | if (cache->ro) { |
| 8175 | ret = 0; |
| 8176 | goto out; |
| 8177 | } |
| 8178 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8179 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 8180 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
Chris Mason | 7d9eb12 | 2008-07-08 14:19:17 -0400 | [diff] [blame] | 8181 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8182 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + |
Josef Bacik | 37be25b | 2011-08-05 10:25:38 -0400 | [diff] [blame] | 8183 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
| 8184 | min_allocable_bytes <= sinfo->total_bytes) { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8185 | sinfo->bytes_readonly += num_bytes; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8186 | cache->ro = 1; |
| 8187 | ret = 0; |
| 8188 | } |
WuBo | 61cfea9 | 2011-07-26 03:30:11 +0000 | [diff] [blame] | 8189 | out: |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8190 | spin_unlock(&cache->lock); |
| 8191 | spin_unlock(&sinfo->lock); |
| 8192 | return ret; |
Chris Mason | 0ef3e66 | 2008-05-24 14:04:53 -0400 | [diff] [blame] | 8193 | } |
| 8194 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8195 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
| 8196 | struct btrfs_block_group_cache *cache) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8197 | |
| 8198 | { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8199 | struct btrfs_trans_handle *trans; |
| 8200 | u64 alloc_flags; |
| 8201 | int ret; |
| 8202 | |
| 8203 | BUG_ON(cache->ro); |
| 8204 | |
Josef Bacik | 7a7eaa4 | 2011-04-13 12:54:33 -0400 | [diff] [blame] | 8205 | trans = btrfs_join_transaction(root); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8206 | if (IS_ERR(trans)) |
| 8207 | return PTR_ERR(trans); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8208 | |
| 8209 | alloc_flags = update_block_group_flags(root, cache->flags); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8210 | if (alloc_flags != cache->flags) { |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 8211 | ret = do_chunk_alloc(trans, root, alloc_flags, |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 8212 | CHUNK_ALLOC_FORCE); |
| 8213 | if (ret < 0) |
| 8214 | goto out; |
| 8215 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8216 | |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8217 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8218 | if (!ret) |
| 8219 | goto out; |
| 8220 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 8221 | ret = do_chunk_alloc(trans, root, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 8222 | CHUNK_ALLOC_FORCE); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8223 | if (ret < 0) |
| 8224 | goto out; |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8225 | ret = set_block_group_ro(cache, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8226 | out: |
| 8227 | btrfs_end_transaction(trans, root); |
| 8228 | return ret; |
| 8229 | } |
| 8230 | |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 8231 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
| 8232 | struct btrfs_root *root, u64 type) |
| 8233 | { |
| 8234 | u64 alloc_flags = get_alloc_profile(root, type); |
Josef Bacik | 698d008 | 2012-09-12 14:08:47 -0400 | [diff] [blame] | 8235 | return do_chunk_alloc(trans, root, alloc_flags, |
Chris Mason | 0e4f8f8 | 2011-04-15 16:05:44 -0400 | [diff] [blame] | 8236 | CHUNK_ALLOC_FORCE); |
Chris Mason | c87f08c | 2011-02-16 13:57:04 -0500 | [diff] [blame] | 8237 | } |
| 8238 | |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 8239 | /* |
| 8240 | * helper to account the unused space of all the readonly block group in the |
| 8241 | * list. takes mirrors into account. |
| 8242 | */ |
| 8243 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) |
| 8244 | { |
| 8245 | struct btrfs_block_group_cache *block_group; |
| 8246 | u64 free_bytes = 0; |
| 8247 | int factor; |
| 8248 | |
| 8249 | list_for_each_entry(block_group, groups_list, list) { |
| 8250 | spin_lock(&block_group->lock); |
| 8251 | |
| 8252 | if (!block_group->ro) { |
| 8253 | spin_unlock(&block_group->lock); |
| 8254 | continue; |
| 8255 | } |
| 8256 | |
| 8257 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | |
| 8258 | BTRFS_BLOCK_GROUP_RAID10 | |
| 8259 | BTRFS_BLOCK_GROUP_DUP)) |
| 8260 | factor = 2; |
| 8261 | else |
| 8262 | factor = 1; |
| 8263 | |
| 8264 | free_bytes += (block_group->key.offset - |
| 8265 | btrfs_block_group_used(&block_group->item)) * |
| 8266 | factor; |
| 8267 | |
| 8268 | spin_unlock(&block_group->lock); |
| 8269 | } |
| 8270 | |
| 8271 | return free_bytes; |
| 8272 | } |
| 8273 | |
| 8274 | /* |
| 8275 | * helper to account the unused space of all the readonly block group in the |
| 8276 | * space_info. takes mirrors into account. |
| 8277 | */ |
| 8278 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
| 8279 | { |
| 8280 | int i; |
| 8281 | u64 free_bytes = 0; |
| 8282 | |
| 8283 | spin_lock(&sinfo->lock); |
| 8284 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 8285 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
Miao Xie | 6d07bce | 2011-01-05 10:07:31 +0000 | [diff] [blame] | 8286 | if (!list_empty(&sinfo->block_groups[i])) |
| 8287 | free_bytes += __btrfs_get_ro_block_group_free_space( |
| 8288 | &sinfo->block_groups[i]); |
| 8289 | |
| 8290 | spin_unlock(&sinfo->lock); |
| 8291 | |
| 8292 | return free_bytes; |
| 8293 | } |
| 8294 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 8295 | void btrfs_set_block_group_rw(struct btrfs_root *root, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8296 | struct btrfs_block_group_cache *cache) |
| 8297 | { |
| 8298 | struct btrfs_space_info *sinfo = cache->space_info; |
| 8299 | u64 num_bytes; |
| 8300 | |
| 8301 | BUG_ON(!cache->ro); |
| 8302 | |
| 8303 | spin_lock(&sinfo->lock); |
| 8304 | spin_lock(&cache->lock); |
| 8305 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
| 8306 | cache->bytes_super - btrfs_block_group_used(&cache->item); |
| 8307 | sinfo->bytes_readonly -= num_bytes; |
| 8308 | cache->ro = 0; |
| 8309 | spin_unlock(&cache->lock); |
| 8310 | spin_unlock(&sinfo->lock); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 8311 | } |
| 8312 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8313 | /* |
| 8314 | * checks to see if its even possible to relocate this block group. |
| 8315 | * |
| 8316 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its |
| 8317 | * ok to go ahead and try. |
| 8318 | */ |
| 8319 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8320 | { |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8321 | struct btrfs_block_group_cache *block_group; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8322 | struct btrfs_space_info *space_info; |
| 8323 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
| 8324 | struct btrfs_device *device; |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8325 | struct btrfs_trans_handle *trans; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8326 | u64 min_free; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 8327 | u64 dev_min = 1; |
| 8328 | u64 dev_nr = 0; |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8329 | u64 target; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8330 | int index; |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8331 | int full = 0; |
| 8332 | int ret = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8333 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8334 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8335 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8336 | /* odd, couldn't find the block group, leave it alone */ |
| 8337 | if (!block_group) |
| 8338 | return -1; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8339 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8340 | min_free = btrfs_block_group_used(&block_group->item); |
| 8341 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8342 | /* no bytes used, we're good */ |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8343 | if (!min_free) |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8344 | goto out; |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 8345 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8346 | space_info = block_group->space_info; |
| 8347 | spin_lock(&space_info->lock); |
Chris Mason | 323da79 | 2008-05-09 11:46:48 -0400 | [diff] [blame] | 8348 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8349 | full = space_info->full; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8350 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8351 | /* |
| 8352 | * 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] | 8353 | * relocate it unless we're able to allocate a new chunk below. |
| 8354 | * |
| 8355 | * Otherwise, we need to make sure we have room in the space to handle |
| 8356 | * 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] | 8357 | */ |
Chris Mason | 7ce618d | 2009-09-22 14:48:44 -0400 | [diff] [blame] | 8358 | if ((space_info->total_bytes != block_group->key.offset) && |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8359 | (space_info->bytes_used + space_info->bytes_reserved + |
| 8360 | space_info->bytes_pinned + space_info->bytes_readonly + |
| 8361 | min_free < space_info->total_bytes)) { |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8362 | spin_unlock(&space_info->lock); |
| 8363 | goto out; |
| 8364 | } |
| 8365 | spin_unlock(&space_info->lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8366 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8367 | /* |
| 8368 | * ok we don't have enough space, but maybe we have free space on our |
| 8369 | * devices to allocate new chunks for relocation, so loop through our |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8370 | * alloc devices and guess if we have enough space. if this block |
| 8371 | * group is going to be restriped, run checks against the target |
| 8372 | * profile instead of the current one. |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8373 | */ |
| 8374 | ret = -1; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 8375 | |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8376 | /* |
| 8377 | * index: |
| 8378 | * 0: raid10 |
| 8379 | * 1: raid1 |
| 8380 | * 2: dup |
| 8381 | * 3: raid0 |
| 8382 | * 4: single |
| 8383 | */ |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8384 | target = get_restripe_target(root->fs_info, block_group->flags); |
| 8385 | if (target) { |
Liu Bo | 31e5022 | 2012-11-21 14:18:10 +0000 | [diff] [blame] | 8386 | index = __get_raid_index(extended_to_chunk(target)); |
Ilya Dryomov | 4a5e98f | 2012-03-27 17:09:17 +0300 | [diff] [blame] | 8387 | } else { |
| 8388 | /* |
| 8389 | * this is just a balance, so if we were marked as full |
| 8390 | * we know there is no space for a new chunk |
| 8391 | */ |
| 8392 | if (full) |
| 8393 | goto out; |
| 8394 | |
| 8395 | index = get_block_group_index(block_group); |
| 8396 | } |
| 8397 | |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 8398 | if (index == BTRFS_RAID_RAID10) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8399 | dev_min = 4; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 8400 | /* Divide by 2 */ |
| 8401 | min_free >>= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 8402 | } else if (index == BTRFS_RAID_RAID1) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8403 | dev_min = 2; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 8404 | } else if (index == BTRFS_RAID_DUP) { |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 8405 | /* Multiply by 2 */ |
| 8406 | min_free <<= 1; |
Miao Xie | e6ec716 | 2013-01-17 05:38:51 +0000 | [diff] [blame] | 8407 | } else if (index == BTRFS_RAID_RAID0) { |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8408 | dev_min = fs_devices->rw_devices; |
Josef Bacik | 6719db6 | 2011-08-20 08:29:51 -0400 | [diff] [blame] | 8409 | do_div(min_free, dev_min); |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8410 | } |
| 8411 | |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8412 | /* We need to do this so that we can look at pending chunks */ |
| 8413 | trans = btrfs_join_transaction(root); |
| 8414 | if (IS_ERR(trans)) { |
| 8415 | ret = PTR_ERR(trans); |
| 8416 | goto out; |
| 8417 | } |
| 8418 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8419 | mutex_lock(&root->fs_info->chunk_mutex); |
| 8420 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 8421 | u64 dev_offset; |
Chris Mason | ea8c281 | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 8422 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8423 | /* |
| 8424 | * check to make sure we can actually find a chunk with enough |
| 8425 | * space to fit our block group in. |
| 8426 | */ |
Stefan Behrens | 63a212a | 2012-11-05 18:29:28 +0100 | [diff] [blame] | 8427 | if (device->total_bytes > device->bytes_used + min_free && |
| 8428 | !device->is_tgtdev_for_dev_replace) { |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8429 | ret = find_free_dev_extent(trans, device, min_free, |
Miao Xie | 7bfc837 | 2011-01-05 10:07:26 +0000 | [diff] [blame] | 8430 | &dev_offset, NULL); |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8431 | if (!ret) |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8432 | dev_nr++; |
| 8433 | |
| 8434 | if (dev_nr >= dev_min) |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8435 | break; |
liubo | cdcb725 | 2011-08-03 10:15:25 +0000 | [diff] [blame] | 8436 | |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8437 | ret = -1; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 8438 | } |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8439 | } |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8440 | mutex_unlock(&root->fs_info->chunk_mutex); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8441 | btrfs_end_transaction(trans, root); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8442 | out: |
Josef Bacik | ba1bf48 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8443 | btrfs_put_block_group(block_group); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 8444 | return ret; |
| 8445 | } |
| 8446 | |
Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 8447 | static int find_first_block_group(struct btrfs_root *root, |
| 8448 | struct btrfs_path *path, struct btrfs_key *key) |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8449 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8450 | int ret = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8451 | struct btrfs_key found_key; |
| 8452 | struct extent_buffer *leaf; |
| 8453 | int slot; |
| 8454 | |
| 8455 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 8456 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8457 | goto out; |
| 8458 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8459 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8460 | slot = path->slots[0]; |
| 8461 | leaf = path->nodes[0]; |
| 8462 | if (slot >= btrfs_header_nritems(leaf)) { |
| 8463 | ret = btrfs_next_leaf(root, path); |
| 8464 | if (ret == 0) |
| 8465 | continue; |
| 8466 | if (ret < 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8467 | goto out; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8468 | break; |
| 8469 | } |
| 8470 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 8471 | |
| 8472 | if (found_key.objectid >= key->objectid && |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8473 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
| 8474 | ret = 0; |
| 8475 | goto out; |
| 8476 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8477 | path->slots[0]++; |
| 8478 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8479 | out: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8480 | return ret; |
| 8481 | } |
| 8482 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8483 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
| 8484 | { |
| 8485 | struct btrfs_block_group_cache *block_group; |
| 8486 | u64 last = 0; |
| 8487 | |
| 8488 | while (1) { |
| 8489 | struct inode *inode; |
| 8490 | |
| 8491 | block_group = btrfs_lookup_first_block_group(info, last); |
| 8492 | while (block_group) { |
| 8493 | spin_lock(&block_group->lock); |
| 8494 | if (block_group->iref) |
| 8495 | break; |
| 8496 | spin_unlock(&block_group->lock); |
| 8497 | block_group = next_block_group(info->tree_root, |
| 8498 | block_group); |
| 8499 | } |
| 8500 | if (!block_group) { |
| 8501 | if (last == 0) |
| 8502 | break; |
| 8503 | last = 0; |
| 8504 | continue; |
| 8505 | } |
| 8506 | |
| 8507 | inode = block_group->inode; |
| 8508 | block_group->iref = 0; |
| 8509 | block_group->inode = NULL; |
| 8510 | spin_unlock(&block_group->lock); |
| 8511 | iput(inode); |
| 8512 | last = block_group->key.objectid + block_group->key.offset; |
| 8513 | btrfs_put_block_group(block_group); |
| 8514 | } |
| 8515 | } |
| 8516 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8517 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 8518 | { |
| 8519 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8520 | struct btrfs_space_info *space_info; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8521 | struct btrfs_caching_control *caching_ctl; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8522 | struct rb_node *n; |
| 8523 | |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 8524 | down_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8525 | while (!list_empty(&info->caching_block_groups)) { |
| 8526 | caching_ctl = list_entry(info->caching_block_groups.next, |
| 8527 | struct btrfs_caching_control, list); |
| 8528 | list_del(&caching_ctl->list); |
| 8529 | put_caching_control(caching_ctl); |
| 8530 | } |
Josef Bacik | 9e351cc | 2014-03-13 15:42:13 -0400 | [diff] [blame] | 8531 | up_write(&info->commit_root_sem); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8532 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8533 | spin_lock(&info->block_group_cache_lock); |
| 8534 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { |
| 8535 | block_group = rb_entry(n, struct btrfs_block_group_cache, |
| 8536 | cache_node); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8537 | rb_erase(&block_group->cache_node, |
| 8538 | &info->block_group_cache_tree); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8539 | spin_unlock(&info->block_group_cache_lock); |
| 8540 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8541 | down_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8542 | list_del(&block_group->list); |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 8543 | up_write(&block_group->space_info->groups_sem); |
Yan Zheng | d2fb343 | 2008-12-11 16:30:39 -0500 | [diff] [blame] | 8544 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8545 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8546 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8547 | |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8548 | /* |
| 8549 | * We haven't cached this block group, which means we could |
| 8550 | * possibly have excluded extents on this block group. |
| 8551 | */ |
Josef Bacik | 36cce92 | 2013-08-05 11:15:21 -0400 | [diff] [blame] | 8552 | if (block_group->cached == BTRFS_CACHE_NO || |
| 8553 | block_group->cached == BTRFS_CACHE_ERROR) |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8554 | free_excluded_extents(info->extent_root, block_group); |
| 8555 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8556 | btrfs_remove_free_space_cache(block_group); |
Josef Bacik | 11dfe35 | 2009-11-13 20:12:59 +0000 | [diff] [blame] | 8557 | btrfs_put_block_group(block_group); |
Yan Zheng | d899e05 | 2008-10-30 14:25:28 -0400 | [diff] [blame] | 8558 | |
| 8559 | spin_lock(&info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8560 | } |
| 8561 | spin_unlock(&info->block_group_cache_lock); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8562 | |
| 8563 | /* now that all the block groups are freed, go through and |
| 8564 | * free all the space_info structs. This is only called during |
| 8565 | * the final stages of unmount, and so we know nobody is |
| 8566 | * using them. We call synchronize_rcu() once before we start, |
| 8567 | * just to be on the safe side. |
| 8568 | */ |
| 8569 | synchronize_rcu(); |
| 8570 | |
Yan, Zheng | 8929ecfa | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 8571 | release_global_block_rsv(info); |
| 8572 | |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 8573 | while (!list_empty(&info->space_info)) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 8574 | int i; |
| 8575 | |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8576 | space_info = list_entry(info->space_info.next, |
| 8577 | struct btrfs_space_info, |
| 8578 | list); |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 8579 | if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) { |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8580 | if (WARN_ON(space_info->bytes_pinned > 0 || |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 8581 | space_info->bytes_reserved > 0 || |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 8582 | space_info->bytes_may_use > 0)) { |
David Sterba | b069e0c | 2013-02-08 21:28:17 +0000 | [diff] [blame] | 8583 | dump_space_info(space_info, 0, 0); |
| 8584 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8585 | } |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8586 | list_del(&space_info->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 8587 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
| 8588 | struct kobject *kobj; |
| 8589 | kobj = &space_info->block_group_kobjs[i]; |
| 8590 | if (kobj->parent) { |
| 8591 | kobject_del(kobj); |
| 8592 | kobject_put(kobj); |
| 8593 | } |
| 8594 | } |
| 8595 | kobject_del(&space_info->kobj); |
| 8596 | kobject_put(&space_info->kobj); |
Chris Mason | 4184ea7 | 2009-03-10 12:39:20 -0400 | [diff] [blame] | 8597 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8598 | return 0; |
| 8599 | } |
| 8600 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8601 | static void __link_block_group(struct btrfs_space_info *space_info, |
| 8602 | struct btrfs_block_group_cache *cache) |
| 8603 | { |
| 8604 | int index = get_block_group_index(cache); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 8605 | bool first = false; |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8606 | |
| 8607 | down_write(&space_info->groups_sem); |
Jeff Mahoney | ed55b6a | 2014-03-26 14:11:26 -0400 | [diff] [blame] | 8608 | if (list_empty(&space_info->block_groups[index])) |
| 8609 | first = true; |
| 8610 | list_add_tail(&cache->list, &space_info->block_groups[index]); |
| 8611 | up_write(&space_info->groups_sem); |
| 8612 | |
| 8613 | if (first) { |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 8614 | struct kobject *kobj = &space_info->block_group_kobjs[index]; |
| 8615 | int ret; |
| 8616 | |
| 8617 | kobject_get(&space_info->kobj); /* put in release */ |
Miao Xie | 536cd96 | 2013-12-17 12:01:12 +0800 | [diff] [blame] | 8618 | ret = kobject_add(kobj, &space_info->kobj, "%s", |
| 8619 | get_raid_name(index)); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 8620 | if (ret) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 8621 | pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n"); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 8622 | kobject_put(&space_info->kobj); |
| 8623 | } |
| 8624 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8625 | } |
| 8626 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 8627 | static struct btrfs_block_group_cache * |
| 8628 | btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size) |
| 8629 | { |
| 8630 | struct btrfs_block_group_cache *cache; |
| 8631 | |
| 8632 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
| 8633 | if (!cache) |
| 8634 | return NULL; |
| 8635 | |
| 8636 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
| 8637 | GFP_NOFS); |
| 8638 | if (!cache->free_space_ctl) { |
| 8639 | kfree(cache); |
| 8640 | return NULL; |
| 8641 | } |
| 8642 | |
| 8643 | cache->key.objectid = start; |
| 8644 | cache->key.offset = size; |
| 8645 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
| 8646 | |
| 8647 | cache->sectorsize = root->sectorsize; |
| 8648 | cache->fs_info = root->fs_info; |
| 8649 | cache->full_stripe_len = btrfs_full_stripe_len(root, |
| 8650 | &root->fs_info->mapping_tree, |
| 8651 | start); |
| 8652 | atomic_set(&cache->count, 1); |
| 8653 | spin_lock_init(&cache->lock); |
| 8654 | INIT_LIST_HEAD(&cache->list); |
| 8655 | INIT_LIST_HEAD(&cache->cluster_list); |
| 8656 | INIT_LIST_HEAD(&cache->new_bg_list); |
| 8657 | btrfs_init_free_space_ctl(cache); |
| 8658 | |
| 8659 | return cache; |
| 8660 | } |
| 8661 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8662 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 8663 | { |
| 8664 | struct btrfs_path *path; |
| 8665 | int ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8666 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8667 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8668 | struct btrfs_space_info *space_info; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8669 | struct btrfs_key key; |
| 8670 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8671 | struct extent_buffer *leaf; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8672 | int need_clear = 0; |
| 8673 | u64 cache_gen; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8674 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 8675 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8676 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8677 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8678 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8679 | path = btrfs_alloc_path(); |
| 8680 | if (!path) |
| 8681 | return -ENOMEM; |
Josef Bacik | 026fd31 | 2011-05-13 10:32:11 -0400 | [diff] [blame] | 8682 | path->reada = 1; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8683 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 8684 | cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); |
Josef Bacik | 73bc187 | 2011-10-03 14:07:49 -0400 | [diff] [blame] | 8685 | if (btrfs_test_opt(root, SPACE_CACHE) && |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 8686 | btrfs_super_generation(root->fs_info->super_copy) != cache_gen) |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8687 | need_clear = 1; |
Josef Bacik | 88c2ba3 | 2010-09-21 14:21:34 -0400 | [diff] [blame] | 8688 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
| 8689 | need_clear = 1; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8690 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 8691 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8692 | ret = find_first_block_group(root, path, &key); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8693 | if (ret > 0) |
| 8694 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8695 | if (ret != 0) |
| 8696 | goto error; |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 8697 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8698 | leaf = path->nodes[0]; |
| 8699 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 8700 | |
| 8701 | cache = btrfs_create_block_group_cache(root, found_key.objectid, |
| 8702 | found_key.offset); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8703 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8704 | ret = -ENOMEM; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8705 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8706 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8707 | |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 8708 | if (need_clear) { |
| 8709 | /* |
| 8710 | * When we mount with old space cache, we need to |
| 8711 | * set BTRFS_DC_CLEAR and set dirty flag. |
| 8712 | * |
| 8713 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we |
| 8714 | * truncate the old free space cache inode and |
| 8715 | * setup a new one. |
| 8716 | * b) Setting 'dirty flag' makes sure that we flush |
| 8717 | * the new space cache info onto disk. |
| 8718 | */ |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8719 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
Liu Bo | cf7c1ef | 2012-07-06 03:31:34 -0600 | [diff] [blame] | 8720 | if (btrfs_test_opt(root, SPACE_CACHE)) |
| 8721 | cache->dirty = 1; |
| 8722 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8723 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 8724 | read_extent_buffer(leaf, &cache->item, |
| 8725 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 8726 | sizeof(cache->item)); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 8727 | cache->flags = btrfs_block_group_flags(&cache->item); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8728 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8729 | key.objectid = found_key.objectid + found_key.offset; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 8730 | btrfs_release_path(path); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 8731 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8732 | /* |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8733 | * We need to exclude the super stripes now so that the space |
| 8734 | * info has super bytes accounted for, otherwise we'll think |
| 8735 | * we have more space than we actually do. |
| 8736 | */ |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 8737 | ret = exclude_super_stripes(root, cache); |
| 8738 | if (ret) { |
| 8739 | /* |
| 8740 | * We may have excluded something, so call this just in |
| 8741 | * case. |
| 8742 | */ |
| 8743 | free_excluded_extents(root, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 8744 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 8745 | goto error; |
| 8746 | } |
Josef Bacik | 3c14874 | 2011-02-02 15:53:47 +0000 | [diff] [blame] | 8747 | |
| 8748 | /* |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8749 | * check for two cases, either we are full, and therefore |
| 8750 | * don't need to bother with the caching work since we won't |
| 8751 | * find any space, or we are empty, and we can just add all |
| 8752 | * the space in and be done with it. This saves us _alot_ of |
| 8753 | * time, particularly in the full case. |
| 8754 | */ |
| 8755 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8756 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8757 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8758 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8759 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8760 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8761 | cache->cached = BTRFS_CACHE_FINISHED; |
| 8762 | add_new_free_space(cache, root->fs_info, |
| 8763 | found_key.objectid, |
| 8764 | found_key.objectid + |
| 8765 | found_key.offset); |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8766 | free_excluded_extents(root, cache); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8767 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 8768 | |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 8769 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8770 | if (ret) { |
| 8771 | btrfs_remove_free_space_cache(cache); |
| 8772 | btrfs_put_block_group(cache); |
| 8773 | goto error; |
| 8774 | } |
| 8775 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8776 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 8777 | btrfs_block_group_used(&cache->item), |
| 8778 | &space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 8779 | if (ret) { |
| 8780 | btrfs_remove_free_space_cache(cache); |
| 8781 | spin_lock(&info->block_group_cache_lock); |
| 8782 | rb_erase(&cache->cache_node, |
| 8783 | &info->block_group_cache_tree); |
| 8784 | spin_unlock(&info->block_group_cache_lock); |
| 8785 | btrfs_put_block_group(cache); |
| 8786 | goto error; |
| 8787 | } |
| 8788 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8789 | cache->space_info = space_info; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8790 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8791 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8792 | spin_unlock(&cache->space_info->lock); |
| 8793 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8794 | __link_block_group(space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8795 | |
Chris Mason | 75ccf47 | 2008-09-30 19:24:06 -0400 | [diff] [blame] | 8796 | set_avail_alloc_bits(root->fs_info, cache->flags); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 8797 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8798 | set_block_group_ro(cache, 1); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8799 | } |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8800 | |
| 8801 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { |
| 8802 | if (!(get_alloc_profile(root, space_info->flags) & |
| 8803 | (BTRFS_BLOCK_GROUP_RAID10 | |
| 8804 | BTRFS_BLOCK_GROUP_RAID1 | |
David Woodhouse | 53b381b | 2013-01-29 18:40:14 -0500 | [diff] [blame] | 8805 | BTRFS_BLOCK_GROUP_RAID5 | |
| 8806 | BTRFS_BLOCK_GROUP_RAID6 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8807 | BTRFS_BLOCK_GROUP_DUP))) |
| 8808 | continue; |
| 8809 | /* |
| 8810 | * avoid allocating from un-mirrored block group if there are |
| 8811 | * mirrored block groups. |
| 8812 | */ |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 8813 | list_for_each_entry(cache, |
| 8814 | &space_info->block_groups[BTRFS_RAID_RAID0], |
| 8815 | list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8816 | set_block_group_ro(cache, 1); |
chandan | 1095cc0 | 2013-07-16 12:28:56 +0530 | [diff] [blame] | 8817 | list_for_each_entry(cache, |
| 8818 | &space_info->block_groups[BTRFS_RAID_SINGLE], |
| 8819 | list) |
Miao Xie | 199c36e | 2011-07-15 10:34:36 +0000 | [diff] [blame] | 8820 | set_block_group_ro(cache, 1); |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8821 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8822 | |
| 8823 | init_global_block_rsv(info); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8824 | ret = 0; |
| 8825 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8826 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 8827 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 8828 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8829 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 8830 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, |
| 8831 | struct btrfs_root *root) |
| 8832 | { |
| 8833 | struct btrfs_block_group_cache *block_group, *tmp; |
| 8834 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 8835 | struct btrfs_block_group_item item; |
| 8836 | struct btrfs_key key; |
| 8837 | int ret = 0; |
| 8838 | |
| 8839 | list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, |
| 8840 | new_bg_list) { |
| 8841 | list_del_init(&block_group->new_bg_list); |
| 8842 | |
| 8843 | if (ret) |
| 8844 | continue; |
| 8845 | |
| 8846 | spin_lock(&block_group->lock); |
| 8847 | memcpy(&item, &block_group->item, sizeof(item)); |
| 8848 | memcpy(&key, &block_group->key, sizeof(key)); |
| 8849 | spin_unlock(&block_group->lock); |
| 8850 | |
| 8851 | ret = btrfs_insert_item(trans, extent_root, &key, &item, |
| 8852 | sizeof(item)); |
| 8853 | if (ret) |
| 8854 | btrfs_abort_transaction(trans, extent_root, ret); |
Josef Bacik | 6df9a95 | 2013-06-27 13:22:46 -0400 | [diff] [blame] | 8855 | ret = btrfs_finish_chunk_alloc(trans, extent_root, |
| 8856 | key.objectid, key.offset); |
| 8857 | if (ret) |
| 8858 | btrfs_abort_transaction(trans, extent_root, ret); |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 8859 | } |
| 8860 | } |
| 8861 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8862 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 8863 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 8864 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8865 | u64 size) |
| 8866 | { |
| 8867 | int ret; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8868 | struct btrfs_root *extent_root; |
| 8869 | struct btrfs_block_group_cache *cache; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8870 | |
| 8871 | extent_root = root->fs_info->extent_root; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8872 | |
Miao Xie | 995946d | 2014-04-02 19:51:06 +0800 | [diff] [blame] | 8873 | btrfs_set_log_full_commit(root->fs_info, trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 8874 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 8875 | cache = btrfs_create_block_group_cache(root, chunk_offset, size); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 8876 | if (!cache) |
| 8877 | return -ENOMEM; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame] | 8878 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8879 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8880 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8881 | btrfs_set_block_group_flags(&cache->item, type); |
| 8882 | |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 8883 | cache->flags = type; |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8884 | cache->last_byte_to_unpin = (u64)-1; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8885 | cache->cached = BTRFS_CACHE_FINISHED; |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 8886 | ret = exclude_super_stripes(root, cache); |
| 8887 | if (ret) { |
| 8888 | /* |
| 8889 | * We may have excluded something, so call this just in |
| 8890 | * case. |
| 8891 | */ |
| 8892 | free_excluded_extents(root, cache); |
Miao Xie | 920e4a5 | 2014-01-15 20:00:55 +0800 | [diff] [blame] | 8893 | btrfs_put_block_group(cache); |
Josef Bacik | 835d974 | 2013-03-19 12:13:25 -0400 | [diff] [blame] | 8894 | return ret; |
| 8895 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8896 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 8897 | add_new_free_space(cache, root->fs_info, chunk_offset, |
| 8898 | chunk_offset + size); |
| 8899 | |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 8900 | free_excluded_extents(root, cache); |
| 8901 | |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 8902 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
| 8903 | if (ret) { |
| 8904 | btrfs_remove_free_space_cache(cache); |
| 8905 | btrfs_put_block_group(cache); |
| 8906 | return ret; |
| 8907 | } |
| 8908 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8909 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 8910 | &cache->space_info); |
Josef Bacik | 8c579fe | 2013-04-02 12:40:42 -0400 | [diff] [blame] | 8911 | if (ret) { |
| 8912 | btrfs_remove_free_space_cache(cache); |
| 8913 | spin_lock(&root->fs_info->block_group_cache_lock); |
| 8914 | rb_erase(&cache->cache_node, |
| 8915 | &root->fs_info->block_group_cache_tree); |
| 8916 | spin_unlock(&root->fs_info->block_group_cache_lock); |
| 8917 | btrfs_put_block_group(cache); |
| 8918 | return ret; |
| 8919 | } |
Li Zefan | c7c144d | 2011-12-07 10:39:22 +0800 | [diff] [blame] | 8920 | update_global_block_rsv(root->fs_info); |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8921 | |
| 8922 | spin_lock(&cache->space_info->lock); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 8923 | cache->space_info->bytes_readonly += cache->bytes_super; |
Josef Bacik | 1b2da37 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 8924 | spin_unlock(&cache->space_info->lock); |
| 8925 | |
Yan, Zheng | b742bb8 | 2010-05-16 10:46:24 -0400 | [diff] [blame] | 8926 | __link_block_group(cache->space_info, cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8927 | |
Josef Bacik | ea658ba | 2012-09-11 16:57:25 -0400 | [diff] [blame] | 8928 | list_add_tail(&cache->new_bg_list, &trans->new_bgs); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8929 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 8930 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 8931 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 8932 | return 0; |
| 8933 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8934 | |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8935 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 8936 | { |
Ilya Dryomov | 899c81e | 2012-03-27 17:09:16 +0300 | [diff] [blame] | 8937 | u64 extra_flags = chunk_to_extended(flags) & |
| 8938 | BTRFS_EXTENDED_PROFILE_MASK; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8939 | |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 8940 | write_seqlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8941 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 8942 | fs_info->avail_data_alloc_bits &= ~extra_flags; |
| 8943 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 8944 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; |
| 8945 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 8946 | fs_info->avail_system_alloc_bits &= ~extra_flags; |
Miao Xie | de98ced | 2013-01-29 10:13:12 +0000 | [diff] [blame] | 8947 | write_sequnlock(&fs_info->profiles_lock); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8948 | } |
| 8949 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8950 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
| 8951 | struct btrfs_root *root, u64 group_start) |
| 8952 | { |
| 8953 | struct btrfs_path *path; |
| 8954 | struct btrfs_block_group_cache *block_group; |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8955 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8956 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8957 | struct btrfs_key key; |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 8958 | struct inode *inode; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8959 | int ret; |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8960 | int index; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8961 | int factor; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8962 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8963 | root = root->fs_info->extent_root; |
| 8964 | |
| 8965 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); |
| 8966 | BUG_ON(!block_group); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 8967 | BUG_ON(!block_group->ro); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8968 | |
liubo | 9f7c43c | 2011-03-07 02:13:33 +0000 | [diff] [blame] | 8969 | /* |
| 8970 | * Free the reserved super bytes from this block group before |
| 8971 | * remove it. |
| 8972 | */ |
| 8973 | free_excluded_extents(root, block_group); |
| 8974 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8975 | memcpy(&key, &block_group->key, sizeof(key)); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 8976 | index = get_block_group_index(block_group); |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 8977 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
| 8978 | BTRFS_BLOCK_GROUP_RAID1 | |
| 8979 | BTRFS_BLOCK_GROUP_RAID10)) |
| 8980 | factor = 2; |
| 8981 | else |
| 8982 | factor = 1; |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8983 | |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 8984 | /* make sure this block group isn't part of an allocation cluster */ |
| 8985 | cluster = &root->fs_info->data_alloc_cluster; |
| 8986 | spin_lock(&cluster->refill_lock); |
| 8987 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8988 | spin_unlock(&cluster->refill_lock); |
| 8989 | |
| 8990 | /* |
| 8991 | * make sure this block group isn't part of a metadata |
| 8992 | * allocation cluster |
| 8993 | */ |
| 8994 | cluster = &root->fs_info->meta_alloc_cluster; |
| 8995 | spin_lock(&cluster->refill_lock); |
| 8996 | btrfs_return_cluster_to_free_space(block_group, cluster); |
| 8997 | spin_unlock(&cluster->refill_lock); |
| 8998 | |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 8999 | path = btrfs_alloc_path(); |
Mark Fasheh | d8926bb | 2011-07-13 10:38:47 -0700 | [diff] [blame] | 9000 | if (!path) { |
| 9001 | ret = -ENOMEM; |
| 9002 | goto out; |
| 9003 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9004 | |
Ilya Dryomov | 10b2f34 | 2011-10-02 13:56:53 +0300 | [diff] [blame] | 9005 | inode = lookup_free_space_inode(tree_root, block_group, path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9006 | if (!IS_ERR(inode)) { |
Tsutomu Itoh | b532402 | 2011-07-19 07:27:20 +0000 | [diff] [blame] | 9007 | ret = btrfs_orphan_add(trans, inode); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 9008 | if (ret) { |
| 9009 | btrfs_add_delayed_iput(inode); |
| 9010 | goto out; |
| 9011 | } |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9012 | clear_nlink(inode); |
| 9013 | /* One for the block groups ref */ |
| 9014 | spin_lock(&block_group->lock); |
| 9015 | if (block_group->iref) { |
| 9016 | block_group->iref = 0; |
| 9017 | block_group->inode = NULL; |
| 9018 | spin_unlock(&block_group->lock); |
| 9019 | iput(inode); |
| 9020 | } else { |
| 9021 | spin_unlock(&block_group->lock); |
| 9022 | } |
| 9023 | /* One for our lookup ref */ |
Josef Bacik | 455757c | 2011-09-19 12:26:24 -0400 | [diff] [blame] | 9024 | btrfs_add_delayed_iput(inode); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9025 | } |
| 9026 | |
| 9027 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 9028 | key.offset = block_group->key.objectid; |
| 9029 | key.type = 0; |
| 9030 | |
| 9031 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
| 9032 | if (ret < 0) |
| 9033 | goto out; |
| 9034 | if (ret > 0) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 9035 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9036 | if (ret == 0) { |
| 9037 | ret = btrfs_del_item(trans, tree_root, path); |
| 9038 | if (ret) |
| 9039 | goto out; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 9040 | btrfs_release_path(path); |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9041 | } |
| 9042 | |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 9043 | spin_lock(&root->fs_info->block_group_cache_lock); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9044 | rb_erase(&block_group->cache_node, |
| 9045 | &root->fs_info->block_group_cache_tree); |
Liu Bo | a1897fd | 2012-12-27 09:01:23 +0000 | [diff] [blame] | 9046 | |
| 9047 | if (root->fs_info->first_logical_byte == block_group->key.objectid) |
| 9048 | root->fs_info->first_logical_byte = (u64)-1; |
Yan Zheng | 3dfdb93 | 2009-01-21 10:49:16 -0500 | [diff] [blame] | 9049 | spin_unlock(&root->fs_info->block_group_cache_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9050 | |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9051 | down_write(&block_group->space_info->groups_sem); |
Chris Mason | 44fb551 | 2009-06-04 15:34:51 -0400 | [diff] [blame] | 9052 | /* |
| 9053 | * we must use list_del_init so people can check to see if they |
| 9054 | * are still on the list after taking the semaphore |
| 9055 | */ |
| 9056 | list_del_init(&block_group->list); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9057 | if (list_empty(&block_group->space_info->block_groups[index])) { |
| 9058 | kobject_del(&block_group->space_info->block_group_kobjs[index]); |
| 9059 | kobject_put(&block_group->space_info->block_group_kobjs[index]); |
Ilya Dryomov | 10ea00f | 2012-01-16 22:04:47 +0200 | [diff] [blame] | 9060 | clear_avail_alloc_bits(root->fs_info, block_group->flags); |
Jeff Mahoney | 6ab0a20 | 2013-11-01 13:07:04 -0400 | [diff] [blame] | 9061 | } |
Josef Bacik | 80eb234 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 9062 | up_write(&block_group->space_info->groups_sem); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9063 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9064 | if (block_group->cached == BTRFS_CACHE_STARTED) |
Yan Zheng | 11833d6 | 2009-09-11 16:11:19 -0400 | [diff] [blame] | 9065 | wait_block_group_cache_done(block_group); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 9066 | |
| 9067 | btrfs_remove_free_space_cache(block_group); |
| 9068 | |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 9069 | spin_lock(&block_group->space_info->lock); |
| 9070 | block_group->space_info->total_bytes -= block_group->key.offset; |
| 9071 | block_group->space_info->bytes_readonly -= block_group->key.offset; |
Josef Bacik | 89a5589 | 2010-10-14 14:52:27 -0400 | [diff] [blame] | 9072 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 9073 | spin_unlock(&block_group->space_info->lock); |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 9074 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 9075 | memcpy(&key, &block_group->key, sizeof(key)); |
| 9076 | |
Chris Mason | 283bb19 | 2009-07-24 16:30:55 -0400 | [diff] [blame] | 9077 | btrfs_clear_space_info_full(root->fs_info); |
Yan Zheng | c146afa | 2008-11-12 14:34:12 -0500 | [diff] [blame] | 9078 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 9079 | btrfs_put_block_group(block_group); |
| 9080 | btrfs_put_block_group(block_group); |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 9081 | |
| 9082 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 9083 | if (ret > 0) |
| 9084 | ret = -EIO; |
| 9085 | if (ret < 0) |
| 9086 | goto out; |
| 9087 | |
| 9088 | ret = btrfs_del_item(trans, root, path); |
| 9089 | out: |
| 9090 | btrfs_free_path(path); |
| 9091 | return ret; |
| 9092 | } |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 9093 | |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 9094 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
| 9095 | { |
| 9096 | struct btrfs_space_info *space_info; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 9097 | struct btrfs_super_block *disk_super; |
| 9098 | u64 features; |
| 9099 | u64 flags; |
| 9100 | int mixed = 0; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 9101 | int ret; |
| 9102 | |
David Sterba | 6c41761 | 2011-04-13 15:41:04 +0200 | [diff] [blame] | 9103 | disk_super = fs_info->super_copy; |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 9104 | if (!btrfs_super_root(disk_super)) |
| 9105 | return 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 9106 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 9107 | features = btrfs_super_incompat_flags(disk_super); |
| 9108 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 9109 | mixed = 1; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 9110 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 9111 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
| 9112 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 9113 | if (ret) |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 9114 | goto out; |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 9115 | |
liubo | 1aba86d | 2011-04-08 08:44:37 +0000 | [diff] [blame] | 9116 | if (mixed) { |
| 9117 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; |
| 9118 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 9119 | } else { |
| 9120 | flags = BTRFS_BLOCK_GROUP_METADATA; |
| 9121 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 9122 | if (ret) |
| 9123 | goto out; |
| 9124 | |
| 9125 | flags = BTRFS_BLOCK_GROUP_DATA; |
| 9126 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); |
| 9127 | } |
| 9128 | out: |
liubo | c59021f | 2011-03-07 02:13:14 +0000 | [diff] [blame] | 9129 | return ret; |
| 9130 | } |
| 9131 | |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 9132 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
| 9133 | { |
| 9134 | return unpin_extent_range(root, start, end); |
| 9135 | } |
| 9136 | |
| 9137 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 9138 | u64 num_bytes, u64 *actual_bytes) |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 9139 | { |
Li Dongyang | 5378e60 | 2011-03-24 10:24:27 +0000 | [diff] [blame] | 9140 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
liubo | acce952 | 2011-01-06 19:30:25 +0800 | [diff] [blame] | 9141 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 9142 | |
| 9143 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
| 9144 | { |
| 9145 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 9146 | struct btrfs_block_group_cache *cache = NULL; |
| 9147 | u64 group_trimmed; |
| 9148 | u64 start; |
| 9149 | u64 end; |
| 9150 | u64 trimmed = 0; |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 9151 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 9152 | int ret = 0; |
| 9153 | |
Liu Bo | 2cac13e | 2012-02-09 18:17:41 +0800 | [diff] [blame] | 9154 | /* |
| 9155 | * try to trim all FS space, our block group may start from non-zero. |
| 9156 | */ |
| 9157 | if (range->len == total_bytes) |
| 9158 | cache = btrfs_lookup_first_block_group(fs_info, range->start); |
| 9159 | else |
| 9160 | cache = btrfs_lookup_block_group(fs_info, range->start); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 9161 | |
| 9162 | while (cache) { |
| 9163 | if (cache->key.objectid >= (range->start + range->len)) { |
| 9164 | btrfs_put_block_group(cache); |
| 9165 | break; |
| 9166 | } |
| 9167 | |
| 9168 | start = max(range->start, cache->key.objectid); |
| 9169 | end = min(range->start + range->len, |
| 9170 | cache->key.objectid + cache->key.offset); |
| 9171 | |
| 9172 | if (end - start >= range->minlen) { |
| 9173 | if (!block_group_cache_done(cache)) { |
Liu Bo | f6373bf | 2012-12-27 09:01:18 +0000 | [diff] [blame] | 9174 | ret = cache_block_group(cache, 0); |
Josef Bacik | 1be41b7 | 2013-06-12 13:56:06 -0400 | [diff] [blame] | 9175 | if (ret) { |
| 9176 | btrfs_put_block_group(cache); |
| 9177 | break; |
| 9178 | } |
| 9179 | ret = wait_block_group_cache_done(cache); |
| 9180 | if (ret) { |
| 9181 | btrfs_put_block_group(cache); |
| 9182 | break; |
| 9183 | } |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 9184 | } |
| 9185 | ret = btrfs_trim_block_group(cache, |
| 9186 | &group_trimmed, |
| 9187 | start, |
| 9188 | end, |
| 9189 | range->minlen); |
| 9190 | |
| 9191 | trimmed += group_trimmed; |
| 9192 | if (ret) { |
| 9193 | btrfs_put_block_group(cache); |
| 9194 | break; |
| 9195 | } |
| 9196 | } |
| 9197 | |
| 9198 | cache = next_block_group(fs_info->tree_root, cache); |
| 9199 | } |
| 9200 | |
| 9201 | range->len = trimmed; |
| 9202 | return ret; |
| 9203 | } |
Miao Xie | 8257b2d | 2014-03-06 13:38:19 +0800 | [diff] [blame] | 9204 | |
| 9205 | /* |
| 9206 | * btrfs_{start,end}_write() is similar to mnt_{want, drop}_write(), |
| 9207 | * they are used to prevent the some tasks writing data into the page cache |
| 9208 | * by nocow before the subvolume is snapshoted, but flush the data into |
| 9209 | * the disk after the snapshot creation. |
| 9210 | */ |
| 9211 | void btrfs_end_nocow_write(struct btrfs_root *root) |
| 9212 | { |
| 9213 | percpu_counter_dec(&root->subv_writers->counter); |
| 9214 | /* |
| 9215 | * Make sure counter is updated before we wake up |
| 9216 | * waiters. |
| 9217 | */ |
| 9218 | smp_mb(); |
| 9219 | if (waitqueue_active(&root->subv_writers->wait)) |
| 9220 | wake_up(&root->subv_writers->wait); |
| 9221 | } |
| 9222 | |
| 9223 | int btrfs_start_nocow_write(struct btrfs_root *root) |
| 9224 | { |
| 9225 | if (unlikely(atomic_read(&root->will_be_snapshoted))) |
| 9226 | return 0; |
| 9227 | |
| 9228 | percpu_counter_inc(&root->subv_writers->counter); |
| 9229 | /* |
| 9230 | * Make sure counter is updated before we check for snapshot creation. |
| 9231 | */ |
| 9232 | smp_mb(); |
| 9233 | if (unlikely(atomic_read(&root->will_be_snapshoted))) { |
| 9234 | btrfs_end_nocow_write(root); |
| 9235 | return 0; |
| 9236 | } |
| 9237 | return 1; |
| 9238 | } |