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