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