blob: 03bdf355107abe2b191cecd5e4544cdaafe2473a [file] [log] [blame]
Chris Mason56bec292009-03-13 10:10:06 -04001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason56bec292009-03-13 10:10:06 -040021#include <linux/sort.h>
Chris Mason56bec292009-03-13 10:10:06 -040022#include "ctree.h"
23#include "delayed-ref.h"
24#include "transaction.h"
Qu Wenruo3368d002015-04-16 14:34:17 +080025#include "qgroup.h"
Chris Mason56bec292009-03-13 10:10:06 -040026
Miao Xie78a61842012-11-21 02:21:28 +000027struct kmem_cache *btrfs_delayed_ref_head_cachep;
28struct kmem_cache *btrfs_delayed_tree_ref_cachep;
29struct kmem_cache *btrfs_delayed_data_ref_cachep;
30struct kmem_cache *btrfs_delayed_extent_op_cachep;
Chris Mason56bec292009-03-13 10:10:06 -040031/*
32 * delayed back reference update tracking. For subvolume trees
33 * we queue up extent allocations and backref maintenance for
34 * delayed processing. This avoids deep call chains where we
35 * add extents in the middle of btrfs_search_slot, and it allows
36 * us to buffer up frequently modified backrefs in an rb tree instead
37 * of hammering updates on the extent allocation tree.
Chris Mason56bec292009-03-13 10:10:06 -040038 */
39
40/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -040041 * compare two delayed tree backrefs with same bytenr and type
Chris Mason56bec292009-03-13 10:10:06 -040042 */
Josef Bacikc7ad7c82017-10-19 14:15:58 -040043static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref1,
44 struct btrfs_delayed_tree_ref *ref2)
Chris Mason56bec292009-03-13 10:10:06 -040045{
Josef Bacik3b60d432017-09-29 15:43:58 -040046 if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) {
Josef Bacik41b0fc42013-04-01 20:36:28 -040047 if (ref1->root < ref2->root)
48 return -1;
49 if (ref1->root > ref2->root)
50 return 1;
51 } else {
52 if (ref1->parent < ref2->parent)
53 return -1;
54 if (ref1->parent > ref2->parent)
55 return 1;
56 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -040057 return 0;
58}
59
60/*
61 * compare two delayed data backrefs with same bytenr and type
62 */
Josef Bacikc7ad7c82017-10-19 14:15:58 -040063static int comp_data_refs(struct btrfs_delayed_data_ref *ref1,
64 struct btrfs_delayed_data_ref *ref2)
Yan Zheng5d4f98a2009-06-10 10:45:14 -040065{
66 if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
67 if (ref1->root < ref2->root)
68 return -1;
69 if (ref1->root > ref2->root)
70 return 1;
71 if (ref1->objectid < ref2->objectid)
72 return -1;
73 if (ref1->objectid > ref2->objectid)
74 return 1;
75 if (ref1->offset < ref2->offset)
76 return -1;
77 if (ref1->offset > ref2->offset)
78 return 1;
79 } else {
80 if (ref1->parent < ref2->parent)
81 return -1;
82 if (ref1->parent > ref2->parent)
83 return 1;
84 }
85 return 0;
86}
87
Josef Bacik1d148e52017-10-19 14:15:59 -040088static int comp_refs(struct btrfs_delayed_ref_node *ref1,
89 struct btrfs_delayed_ref_node *ref2,
90 bool check_seq)
91{
92 int ret = 0;
93
94 if (ref1->type < ref2->type)
95 return -1;
96 if (ref1->type > ref2->type)
97 return 1;
98 if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
99 ref1->type == BTRFS_SHARED_BLOCK_REF_KEY)
100 ret = comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref1),
101 btrfs_delayed_node_to_tree_ref(ref2));
102 else
103 ret = comp_data_refs(btrfs_delayed_node_to_data_ref(ref1),
104 btrfs_delayed_node_to_data_ref(ref2));
105 if (ret)
106 return ret;
107 if (check_seq) {
108 if (ref1->seq < ref2->seq)
109 return -1;
110 if (ref1->seq > ref2->seq)
111 return 1;
112 }
113 return 0;
114}
115
Liu Boc46effa2013-10-14 12:59:45 +0800116/* insert a new ref to head ref rbtree */
117static struct btrfs_delayed_ref_head *htree_insert(struct rb_root *root,
118 struct rb_node *node)
119{
120 struct rb_node **p = &root->rb_node;
121 struct rb_node *parent_node = NULL;
122 struct btrfs_delayed_ref_head *entry;
123 struct btrfs_delayed_ref_head *ins;
124 u64 bytenr;
125
126 ins = rb_entry(node, struct btrfs_delayed_ref_head, href_node);
Josef Bacikd2788502017-09-29 15:43:57 -0400127 bytenr = ins->bytenr;
Liu Boc46effa2013-10-14 12:59:45 +0800128 while (*p) {
129 parent_node = *p;
130 entry = rb_entry(parent_node, struct btrfs_delayed_ref_head,
131 href_node);
132
Josef Bacikd2788502017-09-29 15:43:57 -0400133 if (bytenr < entry->bytenr)
Liu Boc46effa2013-10-14 12:59:45 +0800134 p = &(*p)->rb_left;
Josef Bacikd2788502017-09-29 15:43:57 -0400135 else if (bytenr > entry->bytenr)
Liu Boc46effa2013-10-14 12:59:45 +0800136 p = &(*p)->rb_right;
137 else
138 return entry;
139 }
140
141 rb_link_node(node, parent_node, p);
142 rb_insert_color(node, root);
143 return NULL;
144}
145
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400146static struct btrfs_delayed_ref_node* tree_insert(struct rb_root *root,
147 struct btrfs_delayed_ref_node *ins)
148{
149 struct rb_node **p = &root->rb_node;
150 struct rb_node *node = &ins->ref_node;
151 struct rb_node *parent_node = NULL;
152 struct btrfs_delayed_ref_node *entry;
153
154 while (*p) {
155 int comp;
156
157 parent_node = *p;
158 entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
159 ref_node);
160 comp = comp_refs(ins, entry, true);
161 if (comp < 0)
162 p = &(*p)->rb_left;
163 else if (comp > 0)
164 p = &(*p)->rb_right;
165 else
166 return entry;
167 }
168
169 rb_link_node(node, parent_node, p);
170 rb_insert_color(node, root);
171 return NULL;
172}
173
Chris Mason56bec292009-03-13 10:10:06 -0400174/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400175 * find an head entry based on bytenr. This returns the delayed ref
Arne Jansend1270cd2011-09-13 15:16:43 +0200176 * head if it was able to find one, or NULL if nothing was in that spot.
177 * If return_bigger is given, the next bigger entry is returned if no exact
178 * match is found.
Chris Mason56bec292009-03-13 10:10:06 -0400179 */
Liu Boc46effa2013-10-14 12:59:45 +0800180static struct btrfs_delayed_ref_head *
181find_ref_head(struct rb_root *root, u64 bytenr,
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000182 int return_bigger)
Chris Mason56bec292009-03-13 10:10:06 -0400183{
Arne Jansend1270cd2011-09-13 15:16:43 +0200184 struct rb_node *n;
Liu Boc46effa2013-10-14 12:59:45 +0800185 struct btrfs_delayed_ref_head *entry;
Chris Mason56bec292009-03-13 10:10:06 -0400186
Arne Jansend1270cd2011-09-13 15:16:43 +0200187 n = root->rb_node;
188 entry = NULL;
Chris Mason56bec292009-03-13 10:10:06 -0400189 while (n) {
Liu Boc46effa2013-10-14 12:59:45 +0800190 entry = rb_entry(n, struct btrfs_delayed_ref_head, href_node);
Chris Mason56bec292009-03-13 10:10:06 -0400191
Josef Bacikd2788502017-09-29 15:43:57 -0400192 if (bytenr < entry->bytenr)
Chris Mason56bec292009-03-13 10:10:06 -0400193 n = n->rb_left;
Josef Bacikd2788502017-09-29 15:43:57 -0400194 else if (bytenr > entry->bytenr)
Chris Mason56bec292009-03-13 10:10:06 -0400195 n = n->rb_right;
196 else
197 return entry;
198 }
Arne Jansend1270cd2011-09-13 15:16:43 +0200199 if (entry && return_bigger) {
Josef Bacikd2788502017-09-29 15:43:57 -0400200 if (bytenr > entry->bytenr) {
Liu Boc46effa2013-10-14 12:59:45 +0800201 n = rb_next(&entry->href_node);
Arne Jansend1270cd2011-09-13 15:16:43 +0200202 if (!n)
203 n = rb_first(root);
Liu Boc46effa2013-10-14 12:59:45 +0800204 entry = rb_entry(n, struct btrfs_delayed_ref_head,
205 href_node);
Filipe Manana6103fb42014-02-12 15:07:52 +0000206 return entry;
Arne Jansend1270cd2011-09-13 15:16:43 +0200207 }
208 return entry;
209 }
Chris Mason56bec292009-03-13 10:10:06 -0400210 return NULL;
211}
212
Chris Masonc3e69d52009-03-13 10:17:05 -0400213int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
214 struct btrfs_delayed_ref_head *head)
Chris Mason56bec292009-03-13 10:10:06 -0400215{
Chris Masonc3e69d52009-03-13 10:17:05 -0400216 struct btrfs_delayed_ref_root *delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400217
Chris Masonc3e69d52009-03-13 10:17:05 -0400218 delayed_refs = &trans->transaction->delayed_refs;
219 assert_spin_locked(&delayed_refs->lock);
220 if (mutex_trylock(&head->mutex))
221 return 0;
222
Josef Bacikd2788502017-09-29 15:43:57 -0400223 refcount_inc(&head->refs);
Chris Masonc3e69d52009-03-13 10:17:05 -0400224 spin_unlock(&delayed_refs->lock);
225
226 mutex_lock(&head->mutex);
227 spin_lock(&delayed_refs->lock);
Josef Bacikd2788502017-09-29 15:43:57 -0400228 if (RB_EMPTY_NODE(&head->href_node)) {
Chris Masonc3e69d52009-03-13 10:17:05 -0400229 mutex_unlock(&head->mutex);
Josef Bacikd2788502017-09-29 15:43:57 -0400230 btrfs_put_delayed_ref_head(head);
Chris Masonc3e69d52009-03-13 10:17:05 -0400231 return -EAGAIN;
232 }
Josef Bacikd2788502017-09-29 15:43:57 -0400233 btrfs_put_delayed_ref_head(head);
Chris Masonc3e69d52009-03-13 10:17:05 -0400234 return 0;
235}
236
Stefan Behrens35a36212013-08-14 18:12:25 +0200237static inline void drop_delayed_ref(struct btrfs_trans_handle *trans,
Josef Bacikae1e2062012-08-07 16:00:32 -0400238 struct btrfs_delayed_ref_root *delayed_refs,
Josef Bacikd7df2c72014-01-23 09:21:38 -0500239 struct btrfs_delayed_ref_head *head,
Josef Bacikae1e2062012-08-07 16:00:32 -0400240 struct btrfs_delayed_ref_node *ref)
241{
Josef Bacikd2788502017-09-29 15:43:57 -0400242 assert_spin_locked(&head->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400243 rb_erase(&ref->ref_node, &head->ref_tree);
244 RB_CLEAR_NODE(&ref->ref_node);
Josef Bacikd2788502017-09-29 15:43:57 -0400245 if (!list_empty(&ref->add_list))
246 list_del(&ref->add_list);
Josef Bacikae1e2062012-08-07 16:00:32 -0400247 ref->in_tree = 0;
248 btrfs_put_delayed_ref(ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500249 atomic_dec(&delayed_refs->num_entries);
Josef Bacikae1e2062012-08-07 16:00:32 -0400250 if (trans->delayed_ref_updates)
251 trans->delayed_ref_updates--;
252}
253
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100254static bool merge_ref(struct btrfs_trans_handle *trans,
255 struct btrfs_delayed_ref_root *delayed_refs,
256 struct btrfs_delayed_ref_head *head,
257 struct btrfs_delayed_ref_node *ref,
258 u64 seq)
259{
260 struct btrfs_delayed_ref_node *next;
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400261 struct rb_node *node = rb_next(&ref->ref_node);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100262 bool done = false;
263
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400264 while (!done && node) {
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100265 int mod;
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100266
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400267 next = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
268 node = rb_next(node);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100269 if (seq && next->seq >= seq)
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400270 break;
Josef Bacik1d148e52017-10-19 14:15:59 -0400271 if (comp_refs(ref, next, false))
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400272 break;
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100273
274 if (ref->action == next->action) {
275 mod = next->ref_mod;
276 } else {
277 if (ref->ref_mod < next->ref_mod) {
278 swap(ref, next);
279 done = true;
280 }
281 mod = -next->ref_mod;
282 }
283
284 drop_delayed_ref(trans, delayed_refs, head, next);
285 ref->ref_mod += mod;
286 if (ref->ref_mod == 0) {
287 drop_delayed_ref(trans, delayed_refs, head, ref);
288 done = true;
289 } else {
290 /*
291 * Can't have multiples of the same ref on a tree block.
292 */
293 WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
294 ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
295 }
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100296 }
297
298 return done;
299}
300
301void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
302 struct btrfs_fs_info *fs_info,
303 struct btrfs_delayed_ref_root *delayed_refs,
304 struct btrfs_delayed_ref_head *head)
305{
306 struct btrfs_delayed_ref_node *ref;
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400307 struct rb_node *node;
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100308 u64 seq = 0;
309
310 assert_spin_locked(&head->lock);
311
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400312 if (RB_EMPTY_ROOT(&head->ref_tree))
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100313 return;
314
315 /* We don't have too many refs to merge for data. */
316 if (head->is_data)
317 return;
318
319 spin_lock(&fs_info->tree_mod_seq_lock);
320 if (!list_empty(&fs_info->tree_mod_seq_list)) {
321 struct seq_list *elem;
322
323 elem = list_first_entry(&fs_info->tree_mod_seq_list,
324 struct seq_list, list);
325 seq = elem->seq;
326 }
327 spin_unlock(&fs_info->tree_mod_seq_lock);
328
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400329again:
330 for (node = rb_first(&head->ref_tree); node; node = rb_next(node)) {
331 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100332 if (seq && ref->seq >= seq)
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100333 continue;
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400334 if (merge_ref(trans, delayed_refs, head, ref, seq))
335 goto again;
Filipe Manana2c3cf7d2015-10-22 09:47:34 +0100336 }
337}
338
Jan Schmidt097b8a72012-06-21 11:08:04 +0200339int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
340 struct btrfs_delayed_ref_root *delayed_refs,
Arne Jansen00f04b82011-09-14 12:37:00 +0200341 u64 seq)
342{
343 struct seq_list *elem;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200344 int ret = 0;
Arne Jansen00f04b82011-09-14 12:37:00 +0200345
Jan Schmidt097b8a72012-06-21 11:08:04 +0200346 spin_lock(&fs_info->tree_mod_seq_lock);
347 if (!list_empty(&fs_info->tree_mod_seq_list)) {
348 elem = list_first_entry(&fs_info->tree_mod_seq_list,
349 struct seq_list, list);
350 if (seq >= elem->seq) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -0400351 btrfs_debug(fs_info,
352 "holding back delayed_ref %#x.%x, lowest is %#x.%x (%p)",
353 (u32)(seq >> 32), (u32)seq,
354 (u32)(elem->seq >> 32), (u32)elem->seq,
355 delayed_refs);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200356 ret = 1;
357 }
Arne Jansen00f04b82011-09-14 12:37:00 +0200358 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200359
360 spin_unlock(&fs_info->tree_mod_seq_lock);
361 return ret;
Arne Jansen00f04b82011-09-14 12:37:00 +0200362}
363
Josef Bacikd7df2c72014-01-23 09:21:38 -0500364struct btrfs_delayed_ref_head *
365btrfs_select_ref_head(struct btrfs_trans_handle *trans)
Chris Masonc3e69d52009-03-13 10:17:05 -0400366{
Chris Masonc3e69d52009-03-13 10:17:05 -0400367 struct btrfs_delayed_ref_root *delayed_refs;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500368 struct btrfs_delayed_ref_head *head;
369 u64 start;
370 bool loop = false;
Chris Masonc3e69d52009-03-13 10:17:05 -0400371
372 delayed_refs = &trans->transaction->delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +0800373
Chris Masonc3e69d52009-03-13 10:17:05 -0400374again:
Josef Bacikd7df2c72014-01-23 09:21:38 -0500375 start = delayed_refs->run_delayed_start;
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000376 head = find_ref_head(&delayed_refs->href_root, start, 1);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500377 if (!head && !loop) {
378 delayed_refs->run_delayed_start = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -0400379 start = 0;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500380 loop = true;
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000381 head = find_ref_head(&delayed_refs->href_root, start, 1);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500382 if (!head)
383 return NULL;
384 } else if (!head && loop) {
385 return NULL;
Chris Masonc3e69d52009-03-13 10:17:05 -0400386 }
Chris Mason56bec292009-03-13 10:10:06 -0400387
Josef Bacikd7df2c72014-01-23 09:21:38 -0500388 while (head->processing) {
389 struct rb_node *node;
Miao Xie093486c2012-12-19 08:10:10 +0000390
Josef Bacikd7df2c72014-01-23 09:21:38 -0500391 node = rb_next(&head->href_node);
392 if (!node) {
393 if (loop)
394 return NULL;
395 delayed_refs->run_delayed_start = 0;
396 start = 0;
397 loop = true;
398 goto again;
399 }
400 head = rb_entry(node, struct btrfs_delayed_ref_head,
401 href_node);
402 }
403
404 head->processing = 1;
405 WARN_ON(delayed_refs->num_heads_ready == 0);
406 delayed_refs->num_heads_ready--;
Josef Bacikd2788502017-09-29 15:43:57 -0400407 delayed_refs->run_delayed_start = head->bytenr +
408 head->num_bytes;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500409 return head;
Miao Xie093486c2012-12-19 08:10:10 +0000410}
411
Chris Mason56bec292009-03-13 10:10:06 -0400412/*
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800413 * Helper to insert the ref_node to the tail or merge with tail.
414 *
415 * Return 0 for insert.
416 * Return >0 for merge.
417 */
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400418static int insert_delayed_ref(struct btrfs_trans_handle *trans,
419 struct btrfs_delayed_ref_root *root,
420 struct btrfs_delayed_ref_head *href,
421 struct btrfs_delayed_ref_node *ref)
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800422{
423 struct btrfs_delayed_ref_node *exist;
424 int mod;
425 int ret = 0;
426
427 spin_lock(&href->lock);
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400428 exist = tree_insert(&href->ref_tree, ref);
429 if (!exist)
430 goto inserted;
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800431
432 /* Now we are sure we can merge */
433 ret = 1;
434 if (exist->action == ref->action) {
435 mod = ref->ref_mod;
436 } else {
437 /* Need to change action */
438 if (exist->ref_mod < ref->ref_mod) {
439 exist->action = ref->action;
440 mod = -exist->ref_mod;
441 exist->ref_mod = ref->ref_mod;
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +0800442 if (ref->action == BTRFS_ADD_DELAYED_REF)
443 list_add_tail(&exist->add_list,
444 &href->ref_add_list);
445 else if (ref->action == BTRFS_DROP_DELAYED_REF) {
446 ASSERT(!list_empty(&exist->add_list));
447 list_del(&exist->add_list);
448 } else {
449 ASSERT(0);
450 }
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800451 } else
452 mod = -ref->ref_mod;
453 }
454 exist->ref_mod += mod;
455
456 /* remove existing tail if its ref_mod is zero */
457 if (exist->ref_mod == 0)
458 drop_delayed_ref(trans, root, href, exist);
459 spin_unlock(&href->lock);
460 return ret;
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400461inserted:
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +0800462 if (ref->action == BTRFS_ADD_DELAYED_REF)
463 list_add_tail(&ref->add_list, &href->ref_add_list);
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800464 atomic_inc(&root->num_entries);
465 trans->delayed_ref_updates++;
466 spin_unlock(&href->lock);
467 return ret;
468}
469
470/*
Chris Mason56bec292009-03-13 10:10:06 -0400471 * helper function to update the accounting in the head ref
472 * existing and update must have the same bytenr
473 */
474static noinline void
Josef Bacik12621332015-02-03 07:50:16 -0800475update_existing_head_ref(struct btrfs_delayed_ref_root *delayed_refs,
Josef Bacikd2788502017-09-29 15:43:57 -0400476 struct btrfs_delayed_ref_head *existing,
477 struct btrfs_delayed_ref_head *update,
Omar Sandoval7be07912017-06-06 16:45:30 -0700478 int *old_ref_mod_ret)
Chris Mason56bec292009-03-13 10:10:06 -0400479{
Josef Bacik12621332015-02-03 07:50:16 -0800480 int old_ref_mod;
Chris Mason56bec292009-03-13 10:10:06 -0400481
Josef Bacikd2788502017-09-29 15:43:57 -0400482 BUG_ON(existing->is_data != update->is_data);
Chris Mason56bec292009-03-13 10:10:06 -0400483
Josef Bacikd2788502017-09-29 15:43:57 -0400484 spin_lock(&existing->lock);
485 if (update->must_insert_reserved) {
Chris Mason56bec292009-03-13 10:10:06 -0400486 /* if the extent was freed and then
487 * reallocated before the delayed ref
488 * entries were processed, we can end up
489 * with an existing head ref without
490 * the must_insert_reserved flag set.
491 * Set it again here
492 */
Josef Bacikd2788502017-09-29 15:43:57 -0400493 existing->must_insert_reserved = update->must_insert_reserved;
Chris Mason56bec292009-03-13 10:10:06 -0400494
495 /*
496 * update the num_bytes so we make sure the accounting
497 * is done correctly
498 */
499 existing->num_bytes = update->num_bytes;
500
501 }
502
Josef Bacikd2788502017-09-29 15:43:57 -0400503 if (update->extent_op) {
504 if (!existing->extent_op) {
505 existing->extent_op = update->extent_op;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400506 } else {
Josef Bacikd2788502017-09-29 15:43:57 -0400507 if (update->extent_op->update_key) {
508 memcpy(&existing->extent_op->key,
509 &update->extent_op->key,
510 sizeof(update->extent_op->key));
511 existing->extent_op->update_key = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400512 }
Josef Bacikd2788502017-09-29 15:43:57 -0400513 if (update->extent_op->update_flags) {
514 existing->extent_op->flags_to_set |=
515 update->extent_op->flags_to_set;
516 existing->extent_op->update_flags = true;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400517 }
Josef Bacikd2788502017-09-29 15:43:57 -0400518 btrfs_free_delayed_extent_op(update->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400519 }
520 }
Chris Mason56bec292009-03-13 10:10:06 -0400521 /*
Josef Bacikd7df2c72014-01-23 09:21:38 -0500522 * update the reference mod on the head to reflect this new operation,
523 * only need the lock for this case cause we could be processing it
524 * currently, for refs we just added we know we're a-ok.
Chris Mason56bec292009-03-13 10:10:06 -0400525 */
Josef Bacikd2788502017-09-29 15:43:57 -0400526 old_ref_mod = existing->total_ref_mod;
Omar Sandoval7be07912017-06-06 16:45:30 -0700527 if (old_ref_mod_ret)
528 *old_ref_mod_ret = old_ref_mod;
Chris Mason56bec292009-03-13 10:10:06 -0400529 existing->ref_mod += update->ref_mod;
Josef Bacikd2788502017-09-29 15:43:57 -0400530 existing->total_ref_mod += update->ref_mod;
Josef Bacik12621332015-02-03 07:50:16 -0800531
532 /*
533 * If we are going to from a positive ref mod to a negative or vice
534 * versa we need to make sure to adjust pending_csums accordingly.
535 */
Josef Bacikd2788502017-09-29 15:43:57 -0400536 if (existing->is_data) {
537 if (existing->total_ref_mod >= 0 && old_ref_mod < 0)
Josef Bacik12621332015-02-03 07:50:16 -0800538 delayed_refs->pending_csums -= existing->num_bytes;
Josef Bacikd2788502017-09-29 15:43:57 -0400539 if (existing->total_ref_mod < 0 && old_ref_mod >= 0)
Josef Bacik12621332015-02-03 07:50:16 -0800540 delayed_refs->pending_csums += existing->num_bytes;
541 }
Josef Bacikd2788502017-09-29 15:43:57 -0400542 spin_unlock(&existing->lock);
Chris Mason56bec292009-03-13 10:10:06 -0400543}
544
545/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400546 * helper function to actually insert a head node into the rbtree.
Chris Mason56bec292009-03-13 10:10:06 -0400547 * this does all the dirty work in terms of maintaining the correct
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400548 * overall modification count.
Chris Mason56bec292009-03-13 10:10:06 -0400549 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500550static noinline struct btrfs_delayed_ref_head *
551add_delayed_ref_head(struct btrfs_fs_info *fs_info,
552 struct btrfs_trans_handle *trans,
Josef Bacikd2788502017-09-29 15:43:57 -0400553 struct btrfs_delayed_ref_head *head_ref,
Qu Wenruo3368d002015-04-16 14:34:17 +0800554 struct btrfs_qgroup_extent_record *qrecord,
Qu Wenruo5846a3c2015-10-26 14:11:18 +0800555 u64 bytenr, u64 num_bytes, u64 ref_root, u64 reserved,
Omar Sandoval7be07912017-06-06 16:45:30 -0700556 int action, int is_data, int *qrecord_inserted_ret,
557 int *old_ref_mod, int *new_ref_mod)
Chris Mason56bec292009-03-13 10:10:06 -0400558{
Josef Bacikd7df2c72014-01-23 09:21:38 -0500559 struct btrfs_delayed_ref_head *existing;
Chris Mason56bec292009-03-13 10:10:06 -0400560 struct btrfs_delayed_ref_root *delayed_refs;
561 int count_mod = 1;
562 int must_insert_reserved = 0;
Qu Wenruofb235dc2017-02-15 10:43:03 +0800563 int qrecord_inserted = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400564
Qu Wenruo5846a3c2015-10-26 14:11:18 +0800565 /* If reserved is provided, it must be a data extent. */
566 BUG_ON(!is_data && reserved);
567
Chris Mason56bec292009-03-13 10:10:06 -0400568 /*
569 * the head node stores the sum of all the mods, so dropping a ref
570 * should drop the sum in the head node by one.
571 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400572 if (action == BTRFS_UPDATE_DELAYED_HEAD)
573 count_mod = 0;
574 else if (action == BTRFS_DROP_DELAYED_REF)
575 count_mod = -1;
Chris Mason56bec292009-03-13 10:10:06 -0400576
577 /*
578 * BTRFS_ADD_DELAYED_EXTENT means that we need to update
579 * the reserved accounting when the extent is finally added, or
580 * if a later modification deletes the delayed ref without ever
581 * inserting the extent into the extent allocation tree.
582 * ref->must_insert_reserved is the flag used to record
583 * that accounting mods are required.
584 *
585 * Once we record must_insert_reserved, switch the action to
586 * BTRFS_ADD_DELAYED_REF because other special casing is not required.
587 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400588 if (action == BTRFS_ADD_DELAYED_EXTENT)
Chris Mason56bec292009-03-13 10:10:06 -0400589 must_insert_reserved = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400590 else
Chris Mason56bec292009-03-13 10:10:06 -0400591 must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400592
593 delayed_refs = &trans->transaction->delayed_refs;
594
Josef Bacikd2788502017-09-29 15:43:57 -0400595 refcount_set(&head_ref->refs, 1);
596 head_ref->bytenr = bytenr;
597 head_ref->num_bytes = num_bytes;
598 head_ref->ref_mod = count_mod;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400599 head_ref->must_insert_reserved = must_insert_reserved;
600 head_ref->is_data = is_data;
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400601 head_ref->ref_tree = RB_ROOT;
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +0800602 INIT_LIST_HEAD(&head_ref->ref_add_list);
Josef Bacikd2788502017-09-29 15:43:57 -0400603 RB_CLEAR_NODE(&head_ref->href_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500604 head_ref->processing = 0;
Josef Bacik12621332015-02-03 07:50:16 -0800605 head_ref->total_ref_mod = count_mod;
Qu Wenruof64d5ca2015-09-08 17:08:36 +0800606 head_ref->qgroup_reserved = 0;
607 head_ref->qgroup_ref_root = 0;
Josef Bacikd2788502017-09-29 15:43:57 -0400608 spin_lock_init(&head_ref->lock);
609 mutex_init(&head_ref->mutex);
Chris Mason56bec292009-03-13 10:10:06 -0400610
Qu Wenruo3368d002015-04-16 14:34:17 +0800611 /* Record qgroup extent info if provided */
612 if (qrecord) {
Qu Wenruo5846a3c2015-10-26 14:11:18 +0800613 if (ref_root && reserved) {
614 head_ref->qgroup_ref_root = ref_root;
615 head_ref->qgroup_reserved = reserved;
616 }
617
Qu Wenruo3368d002015-04-16 14:34:17 +0800618 qrecord->bytenr = bytenr;
619 qrecord->num_bytes = num_bytes;
620 qrecord->old_roots = NULL;
621
Qu Wenruo50b3e042016-10-18 09:31:27 +0800622 if(btrfs_qgroup_trace_extent_nolock(fs_info,
Qu Wenruocb93b522016-08-15 10:36:50 +0800623 delayed_refs, qrecord))
Qu Wenruo3368d002015-04-16 14:34:17 +0800624 kfree(qrecord);
Qu Wenruofb235dc2017-02-15 10:43:03 +0800625 else
626 qrecord_inserted = 1;
Qu Wenruo3368d002015-04-16 14:34:17 +0800627 }
628
Josef Bacikd2788502017-09-29 15:43:57 -0400629 trace_add_delayed_ref_head(fs_info, head_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000630
Josef Bacikd7df2c72014-01-23 09:21:38 -0500631 existing = htree_insert(&delayed_refs->href_root,
632 &head_ref->href_node);
Chris Mason56bec292009-03-13 10:10:06 -0400633 if (existing) {
Qu Wenruo5846a3c2015-10-26 14:11:18 +0800634 WARN_ON(ref_root && reserved && existing->qgroup_ref_root
635 && existing->qgroup_reserved);
Josef Bacikd2788502017-09-29 15:43:57 -0400636 update_existing_head_ref(delayed_refs, existing, head_ref,
Omar Sandoval7be07912017-06-06 16:45:30 -0700637 old_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -0400638 /*
639 * we've updated the existing ref, free the newly
640 * allocated ref
641 */
Miao Xie78a61842012-11-21 02:21:28 +0000642 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -0500643 head_ref = existing;
Chris Mason56bec292009-03-13 10:10:06 -0400644 } else {
Omar Sandoval7be07912017-06-06 16:45:30 -0700645 if (old_ref_mod)
646 *old_ref_mod = 0;
Josef Bacik12621332015-02-03 07:50:16 -0800647 if (is_data && count_mod < 0)
648 delayed_refs->pending_csums += num_bytes;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400649 delayed_refs->num_heads++;
650 delayed_refs->num_heads_ready++;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500651 atomic_inc(&delayed_refs->num_entries);
Chris Mason56bec292009-03-13 10:10:06 -0400652 trans->delayed_ref_updates++;
653 }
Qu Wenruofb235dc2017-02-15 10:43:03 +0800654 if (qrecord_inserted_ret)
655 *qrecord_inserted_ret = qrecord_inserted;
Omar Sandoval7be07912017-06-06 16:45:30 -0700656 if (new_ref_mod)
657 *new_ref_mod = head_ref->total_ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500658 return head_ref;
Chris Mason56bec292009-03-13 10:10:06 -0400659}
660
661/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400662 * helper to insert a delayed tree ref into the rbtree.
663 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500664static noinline void
665add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
666 struct btrfs_trans_handle *trans,
667 struct btrfs_delayed_ref_head *head_ref,
668 struct btrfs_delayed_ref_node *ref, u64 bytenr,
669 u64 num_bytes, u64 parent, u64 ref_root, int level,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100670 int action)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400671{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400672 struct btrfs_delayed_tree_ref *full_ref;
673 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200674 u64 seq = 0;
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800675 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400676
677 if (action == BTRFS_ADD_DELAYED_EXTENT)
678 action = BTRFS_ADD_DELAYED_REF;
679
Josef Bacikfcebe452014-05-13 17:30:47 -0700680 if (is_fstree(ref_root))
681 seq = atomic64_read(&fs_info->tree_mod_seq);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400682 delayed_refs = &trans->transaction->delayed_refs;
683
684 /* first set the basic ref node struct up */
Elena Reshetova6df8cdf2017-03-03 10:55:15 +0200685 refcount_set(&ref->refs, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400686 ref->bytenr = bytenr;
687 ref->num_bytes = num_bytes;
688 ref->ref_mod = 1;
689 ref->action = action;
690 ref->is_head = 0;
691 ref->in_tree = 1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200692 ref->seq = seq;
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400693 RB_CLEAR_NODE(&ref->ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +0800694 INIT_LIST_HEAD(&ref->add_list);
Arne Jansen00f04b82011-09-14 12:37:00 +0200695
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400696 full_ref = btrfs_delayed_node_to_tree_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200697 full_ref->parent = parent;
698 full_ref->root = ref_root;
699 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400700 ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200701 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400702 ref->type = BTRFS_TREE_BLOCK_REF_KEY;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400703 full_ref->level = level;
704
Jeff Mahoneybc074522016-06-09 17:27:55 -0400705 trace_add_delayed_tree_ref(fs_info, ref, full_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000706
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400707 ret = insert_delayed_ref(trans, delayed_refs, head_ref, ref);
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800708
709 /*
710 * XXX: memory should be freed at the same level allocated.
711 * But bad practice is anywhere... Follow it now. Need cleanup.
712 */
713 if (ret > 0)
Miao Xie78a61842012-11-21 02:21:28 +0000714 kmem_cache_free(btrfs_delayed_tree_ref_cachep, full_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400715}
716
717/*
718 * helper to insert a delayed data ref into the rbtree.
719 */
Josef Bacikd7df2c72014-01-23 09:21:38 -0500720static noinline void
721add_delayed_data_ref(struct btrfs_fs_info *fs_info,
722 struct btrfs_trans_handle *trans,
723 struct btrfs_delayed_ref_head *head_ref,
724 struct btrfs_delayed_ref_node *ref, u64 bytenr,
725 u64 num_bytes, u64 parent, u64 ref_root, u64 owner,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100726 u64 offset, int action)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400727{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400728 struct btrfs_delayed_data_ref *full_ref;
729 struct btrfs_delayed_ref_root *delayed_refs;
Arne Jansen00f04b82011-09-14 12:37:00 +0200730 u64 seq = 0;
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800731 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400732
733 if (action == BTRFS_ADD_DELAYED_EXTENT)
734 action = BTRFS_ADD_DELAYED_REF;
735
736 delayed_refs = &trans->transaction->delayed_refs;
737
Josef Bacikfcebe452014-05-13 17:30:47 -0700738 if (is_fstree(ref_root))
739 seq = atomic64_read(&fs_info->tree_mod_seq);
740
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400741 /* first set the basic ref node struct up */
Elena Reshetova6df8cdf2017-03-03 10:55:15 +0200742 refcount_set(&ref->refs, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400743 ref->bytenr = bytenr;
744 ref->num_bytes = num_bytes;
745 ref->ref_mod = 1;
746 ref->action = action;
747 ref->is_head = 0;
748 ref->in_tree = 1;
Arne Jansen00f04b82011-09-14 12:37:00 +0200749 ref->seq = seq;
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400750 RB_CLEAR_NODE(&ref->ref_node);
Wang Xiaoguang1d57ee92016-10-26 18:07:33 +0800751 INIT_LIST_HEAD(&ref->add_list);
Arne Jansen00f04b82011-09-14 12:37:00 +0200752
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400753 full_ref = btrfs_delayed_node_to_data_ref(ref);
Arne Janseneebe0632011-09-14 14:01:24 +0200754 full_ref->parent = parent;
755 full_ref->root = ref_root;
756 if (parent)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400757 ref->type = BTRFS_SHARED_DATA_REF_KEY;
Arne Janseneebe0632011-09-14 14:01:24 +0200758 else
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400759 ref->type = BTRFS_EXTENT_DATA_REF_KEY;
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200760
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400761 full_ref->objectid = owner;
762 full_ref->offset = offset;
763
Jeff Mahoneybc074522016-06-09 17:27:55 -0400764 trace_add_delayed_data_ref(fs_info, ref, full_ref, action);
liubo1abe9b82011-03-24 11:18:59 +0000765
Josef Bacik0e0adbc2017-10-19 14:16:00 -0400766 ret = insert_delayed_ref(trans, delayed_refs, head_ref, ref);
Qu Wenruoc6fc2452015-03-30 17:03:00 +0800767 if (ret > 0)
Miao Xie78a61842012-11-21 02:21:28 +0000768 kmem_cache_free(btrfs_delayed_data_ref_cachep, full_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400769}
770
771/*
772 * add a delayed tree ref. This does all of the accounting required
Chris Mason56bec292009-03-13 10:10:06 -0400773 * to make sure the delayed ref is eventually processed before this
774 * transaction commits.
775 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200776int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
777 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400778 u64 bytenr, u64 num_bytes, u64 parent,
779 u64 ref_root, int level, int action,
Omar Sandoval7be07912017-06-06 16:45:30 -0700780 struct btrfs_delayed_extent_op *extent_op,
781 int *old_ref_mod, int *new_ref_mod)
Chris Mason56bec292009-03-13 10:10:06 -0400782{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400783 struct btrfs_delayed_tree_ref *ref;
Chris Mason56bec292009-03-13 10:10:06 -0400784 struct btrfs_delayed_ref_head *head_ref;
785 struct btrfs_delayed_ref_root *delayed_refs;
Qu Wenruo3368d002015-04-16 14:34:17 +0800786 struct btrfs_qgroup_extent_record *record = NULL;
Qu Wenruofb235dc2017-02-15 10:43:03 +0800787 int qrecord_inserted;
Chris Mason56bec292009-03-13 10:10:06 -0400788
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400789 BUG_ON(extent_op && extent_op->is_data);
Miao Xie78a61842012-11-21 02:21:28 +0000790 ref = kmem_cache_alloc(btrfs_delayed_tree_ref_cachep, GFP_NOFS);
Chris Mason56bec292009-03-13 10:10:06 -0400791 if (!ref)
792 return -ENOMEM;
793
Miao Xie78a61842012-11-21 02:21:28 +0000794 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Dan Carpenter5a5003df2015-06-24 17:32:33 +0300795 if (!head_ref)
796 goto free_ref;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400797
Josef Bacikafcdd122016-09-02 15:40:02 -0400798 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
799 is_fstree(ref_root)) {
Qu Wenruo3368d002015-04-16 14:34:17 +0800800 record = kmalloc(sizeof(*record), GFP_NOFS);
Dan Carpenter5a5003df2015-06-24 17:32:33 +0300801 if (!record)
802 goto free_head_ref;
Qu Wenruo3368d002015-04-16 14:34:17 +0800803 }
804
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400805 head_ref->extent_op = extent_op;
806
Chris Mason56bec292009-03-13 10:10:06 -0400807 delayed_refs = &trans->transaction->delayed_refs;
808 spin_lock(&delayed_refs->lock);
809
810 /*
811 * insert both the head node and the new ref without dropping
812 * the spin lock
813 */
Josef Bacikd2788502017-09-29 15:43:57 -0400814 head_ref = add_delayed_ref_head(fs_info, trans, head_ref, record,
Qu Wenruofb235dc2017-02-15 10:43:03 +0800815 bytenr, num_bytes, 0, 0, action, 0,
Omar Sandoval7be07912017-06-06 16:45:30 -0700816 &qrecord_inserted, old_ref_mod,
817 new_ref_mod);
Chris Mason56bec292009-03-13 10:10:06 -0400818
Josef Bacikd7df2c72014-01-23 09:21:38 -0500819 add_delayed_tree_ref(fs_info, trans, head_ref, &ref->node, bytenr,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100820 num_bytes, parent, ref_root, level, action);
Chris Mason56bec292009-03-13 10:10:06 -0400821 spin_unlock(&delayed_refs->lock);
Jan Schmidt95a06072012-05-29 17:06:54 +0200822
Qu Wenruofb235dc2017-02-15 10:43:03 +0800823 if (qrecord_inserted)
Nikolay Borisov952bd3d2018-01-29 15:53:01 +0200824 btrfs_qgroup_trace_extent_post(fs_info, record);
825
Chris Mason56bec292009-03-13 10:10:06 -0400826 return 0;
Dan Carpenter5a5003df2015-06-24 17:32:33 +0300827
828free_head_ref:
829 kmem_cache_free(btrfs_delayed_ref_head_cachep, head_ref);
830free_ref:
831 kmem_cache_free(btrfs_delayed_tree_ref_cachep, ref);
832
833 return -ENOMEM;
Chris Mason56bec292009-03-13 10:10:06 -0400834}
835
836/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400837 * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
838 */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200839int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
840 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400841 u64 bytenr, u64 num_bytes,
842 u64 parent, u64 ref_root,
Omar Sandoval7be07912017-06-06 16:45:30 -0700843 u64 owner, u64 offset, u64 reserved, int action,
844 int *old_ref_mod, int *new_ref_mod)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845{
846 struct btrfs_delayed_data_ref *ref;
847 struct btrfs_delayed_ref_head *head_ref;
848 struct btrfs_delayed_ref_root *delayed_refs;
Qu Wenruo3368d002015-04-16 14:34:17 +0800849 struct btrfs_qgroup_extent_record *record = NULL;
Qu Wenruofb235dc2017-02-15 10:43:03 +0800850 int qrecord_inserted;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400851
Miao Xie78a61842012-11-21 02:21:28 +0000852 ref = kmem_cache_alloc(btrfs_delayed_data_ref_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853 if (!ref)
854 return -ENOMEM;
855
Miao Xie78a61842012-11-21 02:21:28 +0000856 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400857 if (!head_ref) {
Miao Xie78a61842012-11-21 02:21:28 +0000858 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400859 return -ENOMEM;
860 }
861
Josef Bacikafcdd122016-09-02 15:40:02 -0400862 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) &&
863 is_fstree(ref_root)) {
Qu Wenruo3368d002015-04-16 14:34:17 +0800864 record = kmalloc(sizeof(*record), GFP_NOFS);
865 if (!record) {
866 kmem_cache_free(btrfs_delayed_data_ref_cachep, ref);
867 kmem_cache_free(btrfs_delayed_ref_head_cachep,
868 head_ref);
869 return -ENOMEM;
870 }
871 }
872
Jeff Mahoneyfef394f2016-12-13 14:39:34 -0500873 head_ref->extent_op = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874
875 delayed_refs = &trans->transaction->delayed_refs;
876 spin_lock(&delayed_refs->lock);
877
878 /*
879 * insert both the head node and the new ref without dropping
880 * the spin lock
881 */
Josef Bacikd2788502017-09-29 15:43:57 -0400882 head_ref = add_delayed_ref_head(fs_info, trans, head_ref, record,
Qu Wenruo5846a3c2015-10-26 14:11:18 +0800883 bytenr, num_bytes, ref_root, reserved,
Omar Sandoval7be07912017-06-06 16:45:30 -0700884 action, 1, &qrecord_inserted,
885 old_ref_mod, new_ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886
Josef Bacikd7df2c72014-01-23 09:21:38 -0500887 add_delayed_data_ref(fs_info, trans, head_ref, &ref->node, bytenr,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200888 num_bytes, parent, ref_root, owner, offset,
Filipe Mananab06c4bf2015-10-23 07:52:54 +0100889 action);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400890 spin_unlock(&delayed_refs->lock);
Jan Schmidt95a06072012-05-29 17:06:54 +0200891
Qu Wenruofb235dc2017-02-15 10:43:03 +0800892 if (qrecord_inserted)
893 return btrfs_qgroup_trace_extent_post(fs_info, record);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400894 return 0;
895}
896
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200897int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
898 struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 u64 bytenr, u64 num_bytes,
900 struct btrfs_delayed_extent_op *extent_op)
901{
902 struct btrfs_delayed_ref_head *head_ref;
903 struct btrfs_delayed_ref_root *delayed_refs;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400904
Miao Xie78a61842012-11-21 02:21:28 +0000905 head_ref = kmem_cache_alloc(btrfs_delayed_ref_head_cachep, GFP_NOFS);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 if (!head_ref)
907 return -ENOMEM;
908
909 head_ref->extent_op = extent_op;
910
911 delayed_refs = &trans->transaction->delayed_refs;
912 spin_lock(&delayed_refs->lock);
913
Josef Bacikd2788502017-09-29 15:43:57 -0400914 add_delayed_ref_head(fs_info, trans, head_ref, NULL, bytenr,
Qu Wenruo5846a3c2015-10-26 14:11:18 +0800915 num_bytes, 0, 0, BTRFS_UPDATE_DELAYED_HEAD,
Omar Sandoval7be07912017-06-06 16:45:30 -0700916 extent_op->is_data, NULL, NULL, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917
918 spin_unlock(&delayed_refs->lock);
919 return 0;
920}
921
922/*
Chris Mason1887be62009-03-13 10:11:24 -0400923 * this does a simple search for the head node for a given extent.
924 * It must be called with the delayed ref spinlock held, and it returns
925 * the head node if any where found, or NULL if not.
926 */
927struct btrfs_delayed_ref_head *
Liu Bof72ad18e2017-01-30 12:24:37 -0800928btrfs_find_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs, u64 bytenr)
Chris Mason1887be62009-03-13 10:11:24 -0400929{
Filipe Manana85fdfdf2014-02-12 15:07:53 +0000930 return find_ref_head(&delayed_refs->href_root, bytenr, 0);
Chris Mason1887be62009-03-13 10:11:24 -0400931}
Miao Xie78a61842012-11-21 02:21:28 +0000932
David Sterbae67c7182018-02-19 17:24:18 +0100933void __cold btrfs_delayed_ref_exit(void)
Miao Xie78a61842012-11-21 02:21:28 +0000934{
Kinglong Mee5598e902016-01-29 21:36:35 +0800935 kmem_cache_destroy(btrfs_delayed_ref_head_cachep);
936 kmem_cache_destroy(btrfs_delayed_tree_ref_cachep);
937 kmem_cache_destroy(btrfs_delayed_data_ref_cachep);
938 kmem_cache_destroy(btrfs_delayed_extent_op_cachep);
Miao Xie78a61842012-11-21 02:21:28 +0000939}
940
Liu Bof5c29bd2017-11-02 17:21:50 -0600941int __init btrfs_delayed_ref_init(void)
Miao Xie78a61842012-11-21 02:21:28 +0000942{
943 btrfs_delayed_ref_head_cachep = kmem_cache_create(
944 "btrfs_delayed_ref_head",
945 sizeof(struct btrfs_delayed_ref_head), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300946 SLAB_MEM_SPREAD, NULL);
Miao Xie78a61842012-11-21 02:21:28 +0000947 if (!btrfs_delayed_ref_head_cachep)
948 goto fail;
949
950 btrfs_delayed_tree_ref_cachep = kmem_cache_create(
951 "btrfs_delayed_tree_ref",
952 sizeof(struct btrfs_delayed_tree_ref), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300953 SLAB_MEM_SPREAD, NULL);
Miao Xie78a61842012-11-21 02:21:28 +0000954 if (!btrfs_delayed_tree_ref_cachep)
955 goto fail;
956
957 btrfs_delayed_data_ref_cachep = kmem_cache_create(
958 "btrfs_delayed_data_ref",
959 sizeof(struct btrfs_delayed_data_ref), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300960 SLAB_MEM_SPREAD, NULL);
Miao Xie78a61842012-11-21 02:21:28 +0000961 if (!btrfs_delayed_data_ref_cachep)
962 goto fail;
963
964 btrfs_delayed_extent_op_cachep = kmem_cache_create(
965 "btrfs_delayed_extent_op",
966 sizeof(struct btrfs_delayed_extent_op), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +0300967 SLAB_MEM_SPREAD, NULL);
Miao Xie78a61842012-11-21 02:21:28 +0000968 if (!btrfs_delayed_extent_op_cachep)
969 goto fail;
970
971 return 0;
972fail:
973 btrfs_delayed_ref_exit();
974 return -ENOMEM;
975}