Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | */ |
| 18 | |
| 19 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 21 | #include <linux/sort.h> |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 22 | #include "ctree.h" |
| 23 | #include "delayed-ref.h" |
| 24 | #include "transaction.h" |
| 25 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 26 | struct kmem_cache *btrfs_delayed_ref_head_cachep; |
| 27 | struct kmem_cache *btrfs_delayed_tree_ref_cachep; |
| 28 | struct kmem_cache *btrfs_delayed_data_ref_cachep; |
| 29 | struct kmem_cache *btrfs_delayed_extent_op_cachep; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 30 | /* |
| 31 | * delayed back reference update tracking. For subvolume trees |
| 32 | * we queue up extent allocations and backref maintenance for |
| 33 | * delayed processing. This avoids deep call chains where we |
| 34 | * add extents in the middle of btrfs_search_slot, and it allows |
| 35 | * us to buffer up frequently modified backrefs in an rb tree instead |
| 36 | * of hammering updates on the extent allocation tree. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 40 | * compare two delayed tree backrefs with same bytenr and type |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 41 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 42 | static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2, |
Josef Bacik | 41b0fc4 | 2013-04-01 20:36:28 -0400 | [diff] [blame^] | 43 | struct btrfs_delayed_tree_ref *ref1, int type) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 44 | { |
Josef Bacik | 41b0fc4 | 2013-04-01 20:36:28 -0400 | [diff] [blame^] | 45 | if (type == BTRFS_TREE_BLOCK_REF_KEY) { |
| 46 | if (ref1->root < ref2->root) |
| 47 | return -1; |
| 48 | if (ref1->root > ref2->root) |
| 49 | return 1; |
| 50 | } else { |
| 51 | if (ref1->parent < ref2->parent) |
| 52 | return -1; |
| 53 | if (ref1->parent > ref2->parent) |
| 54 | return 1; |
| 55 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * compare two delayed data backrefs with same bytenr and type |
| 61 | */ |
| 62 | static int comp_data_refs(struct btrfs_delayed_data_ref *ref2, |
| 63 | struct btrfs_delayed_data_ref *ref1) |
| 64 | { |
| 65 | if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) { |
| 66 | if (ref1->root < ref2->root) |
| 67 | return -1; |
| 68 | if (ref1->root > ref2->root) |
| 69 | return 1; |
| 70 | if (ref1->objectid < ref2->objectid) |
| 71 | return -1; |
| 72 | if (ref1->objectid > ref2->objectid) |
| 73 | return 1; |
| 74 | if (ref1->offset < ref2->offset) |
| 75 | return -1; |
| 76 | if (ref1->offset > ref2->offset) |
| 77 | return 1; |
| 78 | } else { |
| 79 | if (ref1->parent < ref2->parent) |
| 80 | return -1; |
| 81 | if (ref1->parent > ref2->parent) |
| 82 | return 1; |
| 83 | } |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * entries in the rb tree are ordered by the byte number of the extent, |
| 89 | * type of the delayed backrefs and content of delayed backrefs. |
| 90 | */ |
| 91 | static int comp_entry(struct btrfs_delayed_ref_node *ref2, |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 92 | struct btrfs_delayed_ref_node *ref1, |
| 93 | bool compare_seq) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 94 | { |
| 95 | if (ref1->bytenr < ref2->bytenr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 96 | return -1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 97 | if (ref1->bytenr > ref2->bytenr) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 98 | return 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 99 | if (ref1->is_head && ref2->is_head) |
| 100 | return 0; |
| 101 | if (ref2->is_head) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 102 | return -1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 103 | if (ref1->is_head) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 104 | return 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 105 | if (ref1->type < ref2->type) |
| 106 | return -1; |
| 107 | if (ref1->type > ref2->type) |
| 108 | return 1; |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 109 | /* merging of sequenced refs is not allowed */ |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 110 | if (compare_seq) { |
| 111 | if (ref1->seq < ref2->seq) |
| 112 | return -1; |
| 113 | if (ref1->seq > ref2->seq) |
| 114 | return 1; |
| 115 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 116 | if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 117 | ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) { |
| 118 | return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2), |
Josef Bacik | 41b0fc4 | 2013-04-01 20:36:28 -0400 | [diff] [blame^] | 119 | btrfs_delayed_node_to_tree_ref(ref1), |
| 120 | ref1->type); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 121 | } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY || |
| 122 | ref1->type == BTRFS_SHARED_DATA_REF_KEY) { |
| 123 | return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2), |
| 124 | btrfs_delayed_node_to_data_ref(ref1)); |
| 125 | } |
| 126 | BUG(); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * insert a new ref into the rbtree. This returns any existing refs |
| 132 | * for the same (bytenr,parent) tuple, or NULL if the new node was properly |
| 133 | * inserted. |
| 134 | */ |
| 135 | static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root, |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 136 | struct rb_node *node) |
| 137 | { |
| 138 | struct rb_node **p = &root->rb_node; |
| 139 | struct rb_node *parent_node = NULL; |
| 140 | struct btrfs_delayed_ref_node *entry; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 141 | struct btrfs_delayed_ref_node *ins; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 142 | int cmp; |
| 143 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 144 | ins = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 145 | while (*p) { |
| 146 | parent_node = *p; |
| 147 | entry = rb_entry(parent_node, struct btrfs_delayed_ref_node, |
| 148 | rb_node); |
| 149 | |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 150 | cmp = comp_entry(entry, ins, 1); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 151 | if (cmp < 0) |
| 152 | p = &(*p)->rb_left; |
| 153 | else if (cmp > 0) |
| 154 | p = &(*p)->rb_right; |
| 155 | else |
| 156 | return entry; |
| 157 | } |
| 158 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 159 | rb_link_node(node, parent_node, p); |
| 160 | rb_insert_color(node, root); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 165 | * find an head entry based on bytenr. This returns the delayed ref |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 166 | * head if it was able to find one, or NULL if nothing was in that spot. |
| 167 | * If return_bigger is given, the next bigger entry is returned if no exact |
| 168 | * match is found. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 169 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 170 | static struct btrfs_delayed_ref_node *find_ref_head(struct rb_root *root, |
| 171 | u64 bytenr, |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 172 | struct btrfs_delayed_ref_node **last, |
| 173 | int return_bigger) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 174 | { |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 175 | struct rb_node *n; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 176 | struct btrfs_delayed_ref_node *entry; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 177 | int cmp = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 178 | |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 179 | again: |
| 180 | n = root->rb_node; |
| 181 | entry = NULL; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 182 | while (n) { |
| 183 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); |
| 184 | WARN_ON(!entry->in_tree); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 185 | if (last) |
| 186 | *last = entry; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 187 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 188 | if (bytenr < entry->bytenr) |
| 189 | cmp = -1; |
| 190 | else if (bytenr > entry->bytenr) |
| 191 | cmp = 1; |
| 192 | else if (!btrfs_delayed_ref_is_head(entry)) |
| 193 | cmp = 1; |
| 194 | else |
| 195 | cmp = 0; |
| 196 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 197 | if (cmp < 0) |
| 198 | n = n->rb_left; |
| 199 | else if (cmp > 0) |
| 200 | n = n->rb_right; |
| 201 | else |
| 202 | return entry; |
| 203 | } |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 204 | if (entry && return_bigger) { |
| 205 | if (cmp > 0) { |
| 206 | n = rb_next(&entry->rb_node); |
| 207 | if (!n) |
| 208 | n = rb_first(root); |
| 209 | entry = rb_entry(n, struct btrfs_delayed_ref_node, |
| 210 | rb_node); |
| 211 | bytenr = entry->bytenr; |
| 212 | return_bigger = 0; |
| 213 | goto again; |
| 214 | } |
| 215 | return entry; |
| 216 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 220 | int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans, |
| 221 | struct btrfs_delayed_ref_head *head) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 222 | { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 223 | struct btrfs_delayed_ref_root *delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 224 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 225 | delayed_refs = &trans->transaction->delayed_refs; |
| 226 | assert_spin_locked(&delayed_refs->lock); |
| 227 | if (mutex_trylock(&head->mutex)) |
| 228 | return 0; |
| 229 | |
| 230 | atomic_inc(&head->node.refs); |
| 231 | spin_unlock(&delayed_refs->lock); |
| 232 | |
| 233 | mutex_lock(&head->mutex); |
| 234 | spin_lock(&delayed_refs->lock); |
| 235 | if (!head->node.in_tree) { |
| 236 | mutex_unlock(&head->mutex); |
| 237 | btrfs_put_delayed_ref(&head->node); |
| 238 | return -EAGAIN; |
| 239 | } |
| 240 | btrfs_put_delayed_ref(&head->node); |
| 241 | return 0; |
| 242 | } |
| 243 | |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 244 | static void inline drop_delayed_ref(struct btrfs_trans_handle *trans, |
| 245 | struct btrfs_delayed_ref_root *delayed_refs, |
| 246 | struct btrfs_delayed_ref_node *ref) |
| 247 | { |
| 248 | rb_erase(&ref->rb_node, &delayed_refs->root); |
| 249 | ref->in_tree = 0; |
| 250 | btrfs_put_delayed_ref(ref); |
| 251 | delayed_refs->num_entries--; |
| 252 | if (trans->delayed_ref_updates) |
| 253 | trans->delayed_ref_updates--; |
| 254 | } |
| 255 | |
| 256 | static int merge_ref(struct btrfs_trans_handle *trans, |
| 257 | struct btrfs_delayed_ref_root *delayed_refs, |
| 258 | struct btrfs_delayed_ref_node *ref, u64 seq) |
| 259 | { |
| 260 | struct rb_node *node; |
| 261 | int merged = 0; |
| 262 | int mod = 0; |
| 263 | int done = 0; |
| 264 | |
| 265 | node = rb_prev(&ref->rb_node); |
| 266 | while (node) { |
| 267 | struct btrfs_delayed_ref_node *next; |
| 268 | |
| 269 | next = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
| 270 | node = rb_prev(node); |
| 271 | if (next->bytenr != ref->bytenr) |
| 272 | break; |
| 273 | if (seq && next->seq >= seq) |
| 274 | break; |
| 275 | if (comp_entry(ref, next, 0)) |
| 276 | continue; |
| 277 | |
| 278 | if (ref->action == next->action) { |
| 279 | mod = next->ref_mod; |
| 280 | } else { |
| 281 | if (ref->ref_mod < next->ref_mod) { |
| 282 | struct btrfs_delayed_ref_node *tmp; |
| 283 | |
| 284 | tmp = ref; |
| 285 | ref = next; |
| 286 | next = tmp; |
| 287 | done = 1; |
| 288 | } |
| 289 | mod = -next->ref_mod; |
| 290 | } |
| 291 | |
| 292 | merged++; |
| 293 | drop_delayed_ref(trans, delayed_refs, next); |
| 294 | ref->ref_mod += mod; |
| 295 | if (ref->ref_mod == 0) { |
| 296 | drop_delayed_ref(trans, delayed_refs, ref); |
| 297 | break; |
| 298 | } else { |
| 299 | /* |
| 300 | * You can't have multiples of the same ref on a tree |
| 301 | * block. |
| 302 | */ |
| 303 | WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 304 | ref->type == BTRFS_SHARED_BLOCK_REF_KEY); |
| 305 | } |
| 306 | |
| 307 | if (done) |
| 308 | break; |
| 309 | node = rb_prev(&ref->rb_node); |
| 310 | } |
| 311 | |
| 312 | return merged; |
| 313 | } |
| 314 | |
| 315 | void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans, |
| 316 | struct btrfs_fs_info *fs_info, |
| 317 | struct btrfs_delayed_ref_root *delayed_refs, |
| 318 | struct btrfs_delayed_ref_head *head) |
| 319 | { |
| 320 | struct rb_node *node; |
| 321 | u64 seq = 0; |
| 322 | |
| 323 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 324 | if (!list_empty(&fs_info->tree_mod_seq_list)) { |
| 325 | struct seq_list *elem; |
| 326 | |
| 327 | elem = list_first_entry(&fs_info->tree_mod_seq_list, |
| 328 | struct seq_list, list); |
| 329 | seq = elem->seq; |
| 330 | } |
| 331 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 332 | |
| 333 | node = rb_prev(&head->node.rb_node); |
| 334 | while (node) { |
| 335 | struct btrfs_delayed_ref_node *ref; |
| 336 | |
| 337 | ref = rb_entry(node, struct btrfs_delayed_ref_node, |
| 338 | rb_node); |
| 339 | if (ref->bytenr != head->node.bytenr) |
| 340 | break; |
| 341 | |
| 342 | /* We can't merge refs that are outside of our seq count */ |
| 343 | if (seq && ref->seq >= seq) |
| 344 | break; |
| 345 | if (merge_ref(trans, delayed_refs, ref, seq)) |
| 346 | node = rb_prev(&head->node.rb_node); |
| 347 | else |
| 348 | node = rb_prev(node); |
| 349 | } |
| 350 | } |
| 351 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 352 | int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info, |
| 353 | struct btrfs_delayed_ref_root *delayed_refs, |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 354 | u64 seq) |
| 355 | { |
| 356 | struct seq_list *elem; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 357 | int ret = 0; |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 358 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 359 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 360 | if (!list_empty(&fs_info->tree_mod_seq_list)) { |
| 361 | elem = list_first_entry(&fs_info->tree_mod_seq_list, |
| 362 | struct seq_list, list); |
| 363 | if (seq >= elem->seq) { |
| 364 | pr_debug("holding back delayed_ref %llu, lowest is " |
| 365 | "%llu (%p)\n", seq, elem->seq, delayed_refs); |
| 366 | ret = 1; |
| 367 | } |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 368 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 369 | |
| 370 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 371 | return ret; |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 374 | int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans, |
| 375 | struct list_head *cluster, u64 start) |
| 376 | { |
| 377 | int count = 0; |
| 378 | struct btrfs_delayed_ref_root *delayed_refs; |
| 379 | struct rb_node *node; |
| 380 | struct btrfs_delayed_ref_node *ref; |
| 381 | struct btrfs_delayed_ref_head *head; |
| 382 | |
| 383 | delayed_refs = &trans->transaction->delayed_refs; |
| 384 | if (start == 0) { |
| 385 | node = rb_first(&delayed_refs->root); |
| 386 | } else { |
| 387 | ref = NULL; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 388 | find_ref_head(&delayed_refs->root, start + 1, &ref, 1); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 389 | if (ref) { |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 390 | node = &ref->rb_node; |
| 391 | } else |
| 392 | node = rb_first(&delayed_refs->root); |
| 393 | } |
| 394 | again: |
| 395 | while (node && count < 32) { |
| 396 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 397 | if (btrfs_delayed_ref_is_head(ref)) { |
| 398 | head = btrfs_delayed_node_to_head(ref); |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 399 | if (list_empty(&head->cluster)) { |
| 400 | list_add_tail(&head->cluster, cluster); |
| 401 | delayed_refs->run_delayed_start = |
| 402 | head->node.bytenr; |
| 403 | count++; |
| 404 | |
| 405 | WARN_ON(delayed_refs->num_heads_ready == 0); |
| 406 | delayed_refs->num_heads_ready--; |
| 407 | } else if (count) { |
| 408 | /* the goal of the clustering is to find extents |
| 409 | * that are likely to end up in the same extent |
| 410 | * leaf on disk. So, we don't want them spread |
| 411 | * all over the tree. Stop now if we've hit |
| 412 | * a head that was already in use |
| 413 | */ |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 414 | break; |
| 415 | } |
| 416 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 417 | node = rb_next(node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 418 | } |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 419 | if (count) { |
| 420 | return 0; |
| 421 | } else if (start) { |
| 422 | /* |
| 423 | * we've gone to the end of the rbtree without finding any |
| 424 | * clusters. start from the beginning and try again |
| 425 | */ |
| 426 | start = 0; |
| 427 | node = rb_first(&delayed_refs->root); |
| 428 | goto again; |
| 429 | } |
| 430 | return 1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Miao Xie | 093486c | 2012-12-19 08:10:10 +0000 | [diff] [blame] | 433 | void btrfs_release_ref_cluster(struct list_head *cluster) |
| 434 | { |
| 435 | struct list_head *pos, *q; |
| 436 | |
| 437 | list_for_each_safe(pos, q, cluster) |
| 438 | list_del_init(pos); |
| 439 | } |
| 440 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 441 | /* |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 442 | * helper function to update an extent delayed ref in the |
| 443 | * rbtree. existing and update must both have the same |
| 444 | * bytenr and parent |
| 445 | * |
| 446 | * This may free existing if the update cancels out whatever |
| 447 | * operation it was doing. |
| 448 | */ |
| 449 | static noinline void |
| 450 | update_existing_ref(struct btrfs_trans_handle *trans, |
| 451 | struct btrfs_delayed_ref_root *delayed_refs, |
| 452 | struct btrfs_delayed_ref_node *existing, |
| 453 | struct btrfs_delayed_ref_node *update) |
| 454 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 455 | if (update->action != existing->action) { |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 456 | /* |
| 457 | * this is effectively undoing either an add or a |
| 458 | * drop. We decrement the ref_mod, and if it goes |
| 459 | * down to zero we just delete the entry without |
| 460 | * every changing the extent allocation tree. |
| 461 | */ |
| 462 | existing->ref_mod--; |
Josef Bacik | ae1e206 | 2012-08-07 16:00:32 -0400 | [diff] [blame] | 463 | if (existing->ref_mod == 0) |
| 464 | drop_delayed_ref(trans, delayed_refs, existing); |
| 465 | else |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 466 | WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 467 | existing->type == BTRFS_SHARED_BLOCK_REF_KEY); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 468 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 469 | WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY || |
| 470 | existing->type == BTRFS_SHARED_BLOCK_REF_KEY); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 471 | /* |
| 472 | * the action on the existing ref matches |
| 473 | * the action on the ref we're trying to add. |
| 474 | * Bump the ref_mod by one so the backref that |
| 475 | * is eventually added/removed has the correct |
| 476 | * reference count |
| 477 | */ |
| 478 | existing->ref_mod += update->ref_mod; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * helper function to update the accounting in the head ref |
| 484 | * existing and update must have the same bytenr |
| 485 | */ |
| 486 | static noinline void |
| 487 | update_existing_head_ref(struct btrfs_delayed_ref_node *existing, |
| 488 | struct btrfs_delayed_ref_node *update) |
| 489 | { |
| 490 | struct btrfs_delayed_ref_head *existing_ref; |
| 491 | struct btrfs_delayed_ref_head *ref; |
| 492 | |
| 493 | existing_ref = btrfs_delayed_node_to_head(existing); |
| 494 | ref = btrfs_delayed_node_to_head(update); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 495 | BUG_ON(existing_ref->is_data != ref->is_data); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 496 | |
| 497 | if (ref->must_insert_reserved) { |
| 498 | /* if the extent was freed and then |
| 499 | * reallocated before the delayed ref |
| 500 | * entries were processed, we can end up |
| 501 | * with an existing head ref without |
| 502 | * the must_insert_reserved flag set. |
| 503 | * Set it again here |
| 504 | */ |
| 505 | existing_ref->must_insert_reserved = ref->must_insert_reserved; |
| 506 | |
| 507 | /* |
| 508 | * update the num_bytes so we make sure the accounting |
| 509 | * is done correctly |
| 510 | */ |
| 511 | existing->num_bytes = update->num_bytes; |
| 512 | |
| 513 | } |
| 514 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 515 | if (ref->extent_op) { |
| 516 | if (!existing_ref->extent_op) { |
| 517 | existing_ref->extent_op = ref->extent_op; |
| 518 | } else { |
| 519 | if (ref->extent_op->update_key) { |
| 520 | memcpy(&existing_ref->extent_op->key, |
| 521 | &ref->extent_op->key, |
| 522 | sizeof(ref->extent_op->key)); |
| 523 | existing_ref->extent_op->update_key = 1; |
| 524 | } |
| 525 | if (ref->extent_op->update_flags) { |
| 526 | existing_ref->extent_op->flags_to_set |= |
| 527 | ref->extent_op->flags_to_set; |
| 528 | existing_ref->extent_op->update_flags = 1; |
| 529 | } |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 530 | btrfs_free_delayed_extent_op(ref->extent_op); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 531 | } |
| 532 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 533 | /* |
| 534 | * update the reference mod on the head to reflect this new operation |
| 535 | */ |
| 536 | existing->ref_mod += update->ref_mod; |
| 537 | } |
| 538 | |
| 539 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 540 | * helper function to actually insert a head node into the rbtree. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 541 | * this does all the dirty work in terms of maintaining the correct |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 542 | * overall modification count. |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 543 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 544 | static noinline void add_delayed_ref_head(struct btrfs_fs_info *fs_info, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 545 | struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 546 | struct btrfs_delayed_ref_node *ref, |
| 547 | u64 bytenr, u64 num_bytes, |
| 548 | int action, int is_data) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 549 | { |
| 550 | struct btrfs_delayed_ref_node *existing; |
Chris Mason | c3e69d5 | 2009-03-13 10:17:05 -0400 | [diff] [blame] | 551 | struct btrfs_delayed_ref_head *head_ref = NULL; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 552 | struct btrfs_delayed_ref_root *delayed_refs; |
| 553 | int count_mod = 1; |
| 554 | int must_insert_reserved = 0; |
| 555 | |
| 556 | /* |
| 557 | * the head node stores the sum of all the mods, so dropping a ref |
| 558 | * should drop the sum in the head node by one. |
| 559 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 560 | if (action == BTRFS_UPDATE_DELAYED_HEAD) |
| 561 | count_mod = 0; |
| 562 | else if (action == BTRFS_DROP_DELAYED_REF) |
| 563 | count_mod = -1; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * BTRFS_ADD_DELAYED_EXTENT means that we need to update |
| 567 | * the reserved accounting when the extent is finally added, or |
| 568 | * if a later modification deletes the delayed ref without ever |
| 569 | * inserting the extent into the extent allocation tree. |
| 570 | * ref->must_insert_reserved is the flag used to record |
| 571 | * that accounting mods are required. |
| 572 | * |
| 573 | * Once we record must_insert_reserved, switch the action to |
| 574 | * BTRFS_ADD_DELAYED_REF because other special casing is not required. |
| 575 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 576 | if (action == BTRFS_ADD_DELAYED_EXTENT) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 577 | must_insert_reserved = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 578 | else |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 579 | must_insert_reserved = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 580 | |
| 581 | delayed_refs = &trans->transaction->delayed_refs; |
| 582 | |
| 583 | /* first set the basic ref node struct up */ |
| 584 | atomic_set(&ref->refs, 1); |
| 585 | ref->bytenr = bytenr; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 586 | ref->num_bytes = num_bytes; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 587 | ref->ref_mod = count_mod; |
| 588 | ref->type = 0; |
| 589 | ref->action = 0; |
| 590 | ref->is_head = 1; |
| 591 | ref->in_tree = 1; |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 592 | ref->seq = 0; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 593 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 594 | head_ref = btrfs_delayed_node_to_head(ref); |
| 595 | head_ref->must_insert_reserved = must_insert_reserved; |
| 596 | head_ref->is_data = is_data; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 597 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 598 | INIT_LIST_HEAD(&head_ref->cluster); |
| 599 | mutex_init(&head_ref->mutex); |
| 600 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 601 | trace_btrfs_delayed_ref_head(ref, head_ref, action); |
| 602 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 603 | existing = tree_insert(&delayed_refs->root, &ref->rb_node); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 604 | |
| 605 | if (existing) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 606 | update_existing_head_ref(existing, ref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 607 | /* |
| 608 | * we've updated the existing ref, free the newly |
| 609 | * allocated ref |
| 610 | */ |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 611 | kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 612 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 613 | delayed_refs->num_heads++; |
| 614 | delayed_refs->num_heads_ready++; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 615 | delayed_refs->num_entries++; |
| 616 | trans->delayed_ref_updates++; |
| 617 | } |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 621 | * helper to insert a delayed tree ref into the rbtree. |
| 622 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 623 | static noinline void add_delayed_tree_ref(struct btrfs_fs_info *fs_info, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 624 | struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 625 | struct btrfs_delayed_ref_node *ref, |
| 626 | u64 bytenr, u64 num_bytes, u64 parent, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 627 | u64 ref_root, int level, int action, |
| 628 | int for_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 629 | { |
| 630 | struct btrfs_delayed_ref_node *existing; |
| 631 | struct btrfs_delayed_tree_ref *full_ref; |
| 632 | struct btrfs_delayed_ref_root *delayed_refs; |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 633 | u64 seq = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 634 | |
| 635 | if (action == BTRFS_ADD_DELAYED_EXTENT) |
| 636 | action = BTRFS_ADD_DELAYED_REF; |
| 637 | |
| 638 | delayed_refs = &trans->transaction->delayed_refs; |
| 639 | |
| 640 | /* first set the basic ref node struct up */ |
| 641 | atomic_set(&ref->refs, 1); |
| 642 | ref->bytenr = bytenr; |
| 643 | ref->num_bytes = num_bytes; |
| 644 | ref->ref_mod = 1; |
| 645 | ref->action = action; |
| 646 | ref->is_head = 0; |
| 647 | ref->in_tree = 1; |
| 648 | |
Jan Schmidt | 546adb0 | 2012-06-14 16:37:44 +0200 | [diff] [blame] | 649 | if (need_ref_seq(for_cow, ref_root)) |
| 650 | seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem); |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 651 | ref->seq = seq; |
| 652 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 653 | full_ref = btrfs_delayed_node_to_tree_ref(ref); |
Arne Jansen | eebe063 | 2011-09-14 14:01:24 +0200 | [diff] [blame] | 654 | full_ref->parent = parent; |
| 655 | full_ref->root = ref_root; |
| 656 | if (parent) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 657 | ref->type = BTRFS_SHARED_BLOCK_REF_KEY; |
Arne Jansen | eebe063 | 2011-09-14 14:01:24 +0200 | [diff] [blame] | 658 | else |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 659 | ref->type = BTRFS_TREE_BLOCK_REF_KEY; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 660 | full_ref->level = level; |
| 661 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 662 | trace_btrfs_delayed_tree_ref(ref, full_ref, action); |
| 663 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 664 | existing = tree_insert(&delayed_refs->root, &ref->rb_node); |
| 665 | |
| 666 | if (existing) { |
| 667 | update_existing_ref(trans, delayed_refs, existing, ref); |
| 668 | /* |
| 669 | * we've updated the existing ref, free the newly |
| 670 | * allocated ref |
| 671 | */ |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 672 | kmem_cache_free(btrfs_delayed_tree_ref_cachep, full_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 673 | } else { |
| 674 | delayed_refs->num_entries++; |
| 675 | trans->delayed_ref_updates++; |
| 676 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* |
| 680 | * helper to insert a delayed data ref into the rbtree. |
| 681 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 682 | static noinline void add_delayed_data_ref(struct btrfs_fs_info *fs_info, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 683 | struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 684 | struct btrfs_delayed_ref_node *ref, |
| 685 | u64 bytenr, u64 num_bytes, u64 parent, |
| 686 | u64 ref_root, u64 owner, u64 offset, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 687 | int action, int for_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 688 | { |
| 689 | struct btrfs_delayed_ref_node *existing; |
| 690 | struct btrfs_delayed_data_ref *full_ref; |
| 691 | struct btrfs_delayed_ref_root *delayed_refs; |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 692 | u64 seq = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 693 | |
| 694 | if (action == BTRFS_ADD_DELAYED_EXTENT) |
| 695 | action = BTRFS_ADD_DELAYED_REF; |
| 696 | |
| 697 | delayed_refs = &trans->transaction->delayed_refs; |
| 698 | |
| 699 | /* first set the basic ref node struct up */ |
| 700 | atomic_set(&ref->refs, 1); |
| 701 | ref->bytenr = bytenr; |
| 702 | ref->num_bytes = num_bytes; |
| 703 | ref->ref_mod = 1; |
| 704 | ref->action = action; |
| 705 | ref->is_head = 0; |
| 706 | ref->in_tree = 1; |
| 707 | |
Jan Schmidt | 546adb0 | 2012-06-14 16:37:44 +0200 | [diff] [blame] | 708 | if (need_ref_seq(for_cow, ref_root)) |
| 709 | seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem); |
Arne Jansen | 00f04b8 | 2011-09-14 12:37:00 +0200 | [diff] [blame] | 710 | ref->seq = seq; |
| 711 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 712 | full_ref = btrfs_delayed_node_to_data_ref(ref); |
Arne Jansen | eebe063 | 2011-09-14 14:01:24 +0200 | [diff] [blame] | 713 | full_ref->parent = parent; |
| 714 | full_ref->root = ref_root; |
| 715 | if (parent) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 716 | ref->type = BTRFS_SHARED_DATA_REF_KEY; |
Arne Jansen | eebe063 | 2011-09-14 14:01:24 +0200 | [diff] [blame] | 717 | else |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 718 | ref->type = BTRFS_EXTENT_DATA_REF_KEY; |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 719 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 720 | full_ref->objectid = owner; |
| 721 | full_ref->offset = offset; |
| 722 | |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 723 | trace_btrfs_delayed_data_ref(ref, full_ref, action); |
| 724 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 725 | existing = tree_insert(&delayed_refs->root, &ref->rb_node); |
| 726 | |
| 727 | if (existing) { |
| 728 | update_existing_ref(trans, delayed_refs, existing, ref); |
| 729 | /* |
| 730 | * we've updated the existing ref, free the newly |
| 731 | * allocated ref |
| 732 | */ |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 733 | kmem_cache_free(btrfs_delayed_data_ref_cachep, full_ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 734 | } else { |
| 735 | delayed_refs->num_entries++; |
| 736 | trans->delayed_ref_updates++; |
| 737 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* |
| 741 | * add a delayed tree ref. This does all of the accounting required |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 742 | * to make sure the delayed ref is eventually processed before this |
| 743 | * transaction commits. |
| 744 | */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 745 | int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info, |
| 746 | struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 747 | u64 bytenr, u64 num_bytes, u64 parent, |
| 748 | u64 ref_root, int level, int action, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 749 | struct btrfs_delayed_extent_op *extent_op, |
| 750 | int for_cow) |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 751 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 752 | struct btrfs_delayed_tree_ref *ref; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 753 | struct btrfs_delayed_ref_head *head_ref; |
| 754 | struct btrfs_delayed_ref_root *delayed_refs; |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 755 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 756 | BUG_ON(extent_op && extent_op->is_data); |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 757 | ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 758 | if (!ref) |
| 759 | return -ENOMEM; |
| 760 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 761 | head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 762 | if (!head_ref) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 763 | kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 764 | return -ENOMEM; |
| 765 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 766 | |
| 767 | head_ref->extent_op = extent_op; |
| 768 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 769 | delayed_refs = &trans->transaction->delayed_refs; |
| 770 | spin_lock(&delayed_refs->lock); |
| 771 | |
| 772 | /* |
| 773 | * insert both the head node and the new ref without dropping |
| 774 | * the spin lock |
| 775 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 776 | add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 777 | num_bytes, action, 0); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 778 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 779 | add_delayed_tree_ref(fs_info, trans, &ref->node, bytenr, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 780 | num_bytes, parent, ref_root, level, action, |
| 781 | for_cow); |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 782 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | 546adb0 | 2012-06-14 16:37:44 +0200 | [diff] [blame] | 783 | if (need_ref_seq(for_cow, ref_root)) |
| 784 | btrfs_qgroup_record_ref(trans, &ref->node, extent_op); |
Jan Schmidt | 95a0607 | 2012-05-29 17:06:54 +0200 | [diff] [blame] | 785 | |
Chris Mason | 56bec29 | 2009-03-13 10:10:06 -0400 | [diff] [blame] | 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 790 | * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref. |
| 791 | */ |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 792 | int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info, |
| 793 | struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 794 | u64 bytenr, u64 num_bytes, |
| 795 | u64 parent, u64 ref_root, |
| 796 | u64 owner, u64 offset, int action, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 797 | struct btrfs_delayed_extent_op *extent_op, |
| 798 | int for_cow) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 799 | { |
| 800 | struct btrfs_delayed_data_ref *ref; |
| 801 | struct btrfs_delayed_ref_head *head_ref; |
| 802 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 803 | |
| 804 | BUG_ON(extent_op && !extent_op->is_data); |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 805 | ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 806 | if (!ref) |
| 807 | return -ENOMEM; |
| 808 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 809 | head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 810 | if (!head_ref) { |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 811 | kmem_cache_free(btrfs_delayed_data_ref_cachep, ref); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 812 | return -ENOMEM; |
| 813 | } |
| 814 | |
| 815 | head_ref->extent_op = extent_op; |
| 816 | |
| 817 | delayed_refs = &trans->transaction->delayed_refs; |
| 818 | spin_lock(&delayed_refs->lock); |
| 819 | |
| 820 | /* |
| 821 | * insert both the head node and the new ref without dropping |
| 822 | * the spin lock |
| 823 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 824 | add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 825 | num_bytes, action, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 826 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 827 | add_delayed_data_ref(fs_info, trans, &ref->node, bytenr, |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 828 | num_bytes, parent, ref_root, owner, offset, |
| 829 | action, for_cow); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 830 | spin_unlock(&delayed_refs->lock); |
Jan Schmidt | 546adb0 | 2012-06-14 16:37:44 +0200 | [diff] [blame] | 831 | if (need_ref_seq(for_cow, ref_root)) |
| 832 | btrfs_qgroup_record_ref(trans, &ref->node, extent_op); |
Jan Schmidt | 95a0607 | 2012-05-29 17:06:54 +0200 | [diff] [blame] | 833 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | |
Arne Jansen | 66d7e7f | 2011-09-12 15:26:38 +0200 | [diff] [blame] | 837 | int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info, |
| 838 | struct btrfs_trans_handle *trans, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 839 | u64 bytenr, u64 num_bytes, |
| 840 | struct btrfs_delayed_extent_op *extent_op) |
| 841 | { |
| 842 | struct btrfs_delayed_ref_head *head_ref; |
| 843 | struct btrfs_delayed_ref_root *delayed_refs; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 844 | |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 845 | head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 846 | if (!head_ref) |
| 847 | return -ENOMEM; |
| 848 | |
| 849 | head_ref->extent_op = extent_op; |
| 850 | |
| 851 | delayed_refs = &trans->transaction->delayed_refs; |
| 852 | spin_lock(&delayed_refs->lock); |
| 853 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 854 | add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 855 | num_bytes, BTRFS_UPDATE_DELAYED_HEAD, |
| 856 | extent_op->is_data); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 857 | |
| 858 | spin_unlock(&delayed_refs->lock); |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | /* |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 863 | * this does a simple search for the head node for a given extent. |
| 864 | * It must be called with the delayed ref spinlock held, and it returns |
| 865 | * the head node if any where found, or NULL if not. |
| 866 | */ |
| 867 | struct btrfs_delayed_ref_head * |
| 868 | btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr) |
| 869 | { |
| 870 | struct btrfs_delayed_ref_node *ref; |
| 871 | struct btrfs_delayed_ref_root *delayed_refs; |
| 872 | |
| 873 | delayed_refs = &trans->transaction->delayed_refs; |
Arne Jansen | d1270cd | 2011-09-13 15:16:43 +0200 | [diff] [blame] | 874 | ref = find_ref_head(&delayed_refs->root, bytenr, NULL, 0); |
Chris Mason | 1887be6 | 2009-03-13 10:11:24 -0400 | [diff] [blame] | 875 | if (ref) |
| 876 | return btrfs_delayed_node_to_head(ref); |
| 877 | return NULL; |
| 878 | } |
Miao Xie | 78a6184 | 2012-11-21 02:21:28 +0000 | [diff] [blame] | 879 | |
| 880 | void btrfs_delayed_ref_exit(void) |
| 881 | { |
| 882 | if (btrfs_delayed_ref_head_cachep) |
| 883 | kmem_cache_destroy(btrfs_delayed_ref_head_cachep); |
| 884 | if (btrfs_delayed_tree_ref_cachep) |
| 885 | kmem_cache_destroy(btrfs_delayed_tree_ref_cachep); |
| 886 | if (btrfs_delayed_data_ref_cachep) |
| 887 | kmem_cache_destroy(btrfs_delayed_data_ref_cachep); |
| 888 | if (btrfs_delayed_extent_op_cachep) |
| 889 | kmem_cache_destroy(btrfs_delayed_extent_op_cachep); |
| 890 | } |
| 891 | |
| 892 | int btrfs_delayed_ref_init(void) |
| 893 | { |
| 894 | btrfs_delayed_ref_head_cachep = kmem_cache_create( |
| 895 | "btrfs_delayed_ref_head", |
| 896 | sizeof(struct btrfs_delayed_ref_head), 0, |
| 897 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 898 | if (!btrfs_delayed_ref_head_cachep) |
| 899 | goto fail; |
| 900 | |
| 901 | btrfs_delayed_tree_ref_cachep = kmem_cache_create( |
| 902 | "btrfs_delayed_tree_ref", |
| 903 | sizeof(struct btrfs_delayed_tree_ref), 0, |
| 904 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 905 | if (!btrfs_delayed_tree_ref_cachep) |
| 906 | goto fail; |
| 907 | |
| 908 | btrfs_delayed_data_ref_cachep = kmem_cache_create( |
| 909 | "btrfs_delayed_data_ref", |
| 910 | sizeof(struct btrfs_delayed_data_ref), 0, |
| 911 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 912 | if (!btrfs_delayed_data_ref_cachep) |
| 913 | goto fail; |
| 914 | |
| 915 | btrfs_delayed_extent_op_cachep = kmem_cache_create( |
| 916 | "btrfs_delayed_extent_op", |
| 917 | sizeof(struct btrfs_delayed_extent_op), 0, |
| 918 | SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL); |
| 919 | if (!btrfs_delayed_extent_op_cachep) |
| 920 | goto fail; |
| 921 | |
| 922 | return 0; |
| 923 | fail: |
| 924 | btrfs_delayed_ref_exit(); |
| 925 | return -ENOMEM; |
| 926 | } |