delayed-ref.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. #include <linux/sched.h>
  19. #include <linux/sort.h>
  20. #include <linux/ftrace.h>
  21. #include "ctree.h"
  22. #include "delayed-ref.h"
  23. #include "transaction.h"
  24. /*
  25. * delayed back reference update tracking. For subvolume trees
  26. * we queue up extent allocations and backref maintenance for
  27. * delayed processing. This avoids deep call chains where we
  28. * add extents in the middle of btrfs_search_slot, and it allows
  29. * us to buffer up frequently modified backrefs in an rb tree instead
  30. * of hammering updates on the extent allocation tree.
  31. *
  32. * Right now this code is only used for reference counted trees, but
  33. * the long term goal is to get rid of the similar code for delayed
  34. * extent tree modifications.
  35. */
  36. /*
  37. * entries in the rb tree are ordered by the byte number of the extent
  38. * and by the byte number of the parent block.
  39. */
  40. static int comp_entry(struct btrfs_delayed_ref_node *ref,
  41. u64 bytenr, u64 parent)
  42. {
  43. if (bytenr < ref->bytenr)
  44. return -1;
  45. if (bytenr > ref->bytenr)
  46. return 1;
  47. if (parent < ref->parent)
  48. return -1;
  49. if (parent > ref->parent)
  50. return 1;
  51. return 0;
  52. }
  53. /*
  54. * insert a new ref into the rbtree. This returns any existing refs
  55. * for the same (bytenr,parent) tuple, or NULL if the new node was properly
  56. * inserted.
  57. */
  58. static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root,
  59. u64 bytenr, u64 parent,
  60. struct rb_node *node)
  61. {
  62. struct rb_node **p = &root->rb_node;
  63. struct rb_node *parent_node = NULL;
  64. struct btrfs_delayed_ref_node *entry;
  65. int cmp;
  66. while (*p) {
  67. parent_node = *p;
  68. entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
  69. rb_node);
  70. cmp = comp_entry(entry, bytenr, parent);
  71. if (cmp < 0)
  72. p = &(*p)->rb_left;
  73. else if (cmp > 0)
  74. p = &(*p)->rb_right;
  75. else
  76. return entry;
  77. }
  78. entry = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  79. rb_link_node(node, parent_node, p);
  80. rb_insert_color(node, root);
  81. return NULL;
  82. }
  83. /*
  84. * find an entry based on (bytenr,parent). This returns the delayed
  85. * ref if it was able to find one, or NULL if nothing was in that spot
  86. */
  87. static struct btrfs_delayed_ref_node *tree_search(struct rb_root *root,
  88. u64 bytenr, u64 parent,
  89. struct btrfs_delayed_ref_node **last)
  90. {
  91. struct rb_node *n = root->rb_node;
  92. struct btrfs_delayed_ref_node *entry;
  93. int cmp;
  94. while (n) {
  95. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  96. WARN_ON(!entry->in_tree);
  97. if (last)
  98. *last = entry;
  99. cmp = comp_entry(entry, bytenr, parent);
  100. if (cmp < 0)
  101. n = n->rb_left;
  102. else if (cmp > 0)
  103. n = n->rb_right;
  104. else
  105. return entry;
  106. }
  107. return NULL;
  108. }
  109. int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
  110. struct btrfs_delayed_ref_head *head)
  111. {
  112. struct btrfs_delayed_ref_root *delayed_refs;
  113. delayed_refs = &trans->transaction->delayed_refs;
  114. assert_spin_locked(&delayed_refs->lock);
  115. if (mutex_trylock(&head->mutex))
  116. return 0;
  117. atomic_inc(&head->node.refs);
  118. spin_unlock(&delayed_refs->lock);
  119. mutex_lock(&head->mutex);
  120. spin_lock(&delayed_refs->lock);
  121. if (!head->node.in_tree) {
  122. mutex_unlock(&head->mutex);
  123. btrfs_put_delayed_ref(&head->node);
  124. return -EAGAIN;
  125. }
  126. btrfs_put_delayed_ref(&head->node);
  127. return 0;
  128. }
  129. int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
  130. struct list_head *cluster, u64 start)
  131. {
  132. int count = 0;
  133. struct btrfs_delayed_ref_root *delayed_refs;
  134. struct rb_node *node;
  135. struct btrfs_delayed_ref_node *ref;
  136. struct btrfs_delayed_ref_head *head;
  137. delayed_refs = &trans->transaction->delayed_refs;
  138. if (start == 0) {
  139. node = rb_first(&delayed_refs->root);
  140. } else {
  141. ref = NULL;
  142. tree_search(&delayed_refs->root, start, (u64)-1, &ref);
  143. if (ref) {
  144. struct btrfs_delayed_ref_node *tmp;
  145. node = rb_prev(&ref->rb_node);
  146. while (node) {
  147. tmp = rb_entry(node,
  148. struct btrfs_delayed_ref_node,
  149. rb_node);
  150. if (tmp->bytenr < start)
  151. break;
  152. ref = tmp;
  153. node = rb_prev(&ref->rb_node);
  154. }
  155. node = &ref->rb_node;
  156. } else
  157. node = rb_first(&delayed_refs->root);
  158. }
  159. again:
  160. while (node && count < 32) {
  161. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  162. if (btrfs_delayed_ref_is_head(ref)) {
  163. head = btrfs_delayed_node_to_head(ref);
  164. if (list_empty(&head->cluster)) {
  165. list_add_tail(&head->cluster, cluster);
  166. delayed_refs->run_delayed_start =
  167. head->node.bytenr;
  168. count++;
  169. WARN_ON(delayed_refs->num_heads_ready == 0);
  170. delayed_refs->num_heads_ready--;
  171. } else if (count) {
  172. /* the goal of the clustering is to find extents
  173. * that are likely to end up in the same extent
  174. * leaf on disk. So, we don't want them spread
  175. * all over the tree. Stop now if we've hit
  176. * a head that was already in use
  177. */
  178. break;
  179. }
  180. }
  181. node = rb_next(node);
  182. }
  183. if (count) {
  184. return 0;
  185. } else if (start) {
  186. /*
  187. * we've gone to the end of the rbtree without finding any
  188. * clusters. start from the beginning and try again
  189. */
  190. start = 0;
  191. node = rb_first(&delayed_refs->root);
  192. goto again;
  193. }
  194. return 1;
  195. }
  196. /*
  197. * This checks to see if there are any delayed refs in the
  198. * btree for a given bytenr. It returns one if it finds any
  199. * and zero otherwise.
  200. *
  201. * If it only finds a head node, it returns 0.
  202. *
  203. * The idea is to use this when deciding if you can safely delete an
  204. * extent from the extent allocation tree. There may be a pending
  205. * ref in the rbtree that adds or removes references, so as long as this
  206. * returns one you need to leave the BTRFS_EXTENT_ITEM in the extent
  207. * allocation tree.
  208. */
  209. int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr)
  210. {
  211. struct btrfs_delayed_ref_node *ref;
  212. struct btrfs_delayed_ref_root *delayed_refs;
  213. struct rb_node *prev_node;
  214. int ret = 0;
  215. delayed_refs = &trans->transaction->delayed_refs;
  216. spin_lock(&delayed_refs->lock);
  217. ref = tree_search(&delayed_refs->root, bytenr, (u64)-1, NULL);
  218. if (ref) {
  219. prev_node = rb_prev(&ref->rb_node);
  220. if (!prev_node)
  221. goto out;
  222. ref = rb_entry(prev_node, struct btrfs_delayed_ref_node,
  223. rb_node);
  224. if (ref->bytenr == bytenr)
  225. ret = 1;
  226. }
  227. out:
  228. spin_unlock(&delayed_refs->lock);
  229. return ret;
  230. }
  231. /*
  232. * helper function to lookup reference count
  233. *
  234. * the head node for delayed ref is used to store the sum of all the
  235. * reference count modifications queued up in the rbtree. This way you
  236. * can check to see what the reference count would be if all of the
  237. * delayed refs are processed.
  238. */
  239. int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
  240. struct btrfs_root *root, u64 bytenr,
  241. u64 num_bytes, u32 *refs)
  242. {
  243. struct btrfs_delayed_ref_node *ref;
  244. struct btrfs_delayed_ref_head *head;
  245. struct btrfs_delayed_ref_root *delayed_refs;
  246. struct btrfs_path *path;
  247. struct extent_buffer *leaf;
  248. struct btrfs_extent_item *ei;
  249. struct btrfs_key key;
  250. u32 num_refs;
  251. int ret;
  252. path = btrfs_alloc_path();
  253. if (!path)
  254. return -ENOMEM;
  255. key.objectid = bytenr;
  256. key.type = BTRFS_EXTENT_ITEM_KEY;
  257. key.offset = num_bytes;
  258. delayed_refs = &trans->transaction->delayed_refs;
  259. again:
  260. ret = btrfs_search_slot(trans, root->fs_info->extent_root,
  261. &key, path, 0, 0);
  262. if (ret < 0)
  263. goto out;
  264. if (ret == 0) {
  265. leaf = path->nodes[0];
  266. ei = btrfs_item_ptr(leaf, path->slots[0],
  267. struct btrfs_extent_item);
  268. num_refs = btrfs_extent_refs(leaf, ei);
  269. } else {
  270. num_refs = 0;
  271. ret = 0;
  272. }
  273. spin_lock(&delayed_refs->lock);
  274. ref = tree_search(&delayed_refs->root, bytenr, (u64)-1, NULL);
  275. if (ref) {
  276. head = btrfs_delayed_node_to_head(ref);
  277. if (mutex_trylock(&head->mutex)) {
  278. num_refs += ref->ref_mod;
  279. mutex_unlock(&head->mutex);
  280. *refs = num_refs;
  281. goto out;
  282. }
  283. atomic_inc(&ref->refs);
  284. spin_unlock(&delayed_refs->lock);
  285. btrfs_release_path(root->fs_info->extent_root, path);
  286. mutex_lock(&head->mutex);
  287. mutex_unlock(&head->mutex);
  288. btrfs_put_delayed_ref(ref);
  289. goto again;
  290. } else {
  291. *refs = num_refs;
  292. }
  293. out:
  294. spin_unlock(&delayed_refs->lock);
  295. btrfs_free_path(path);
  296. return ret;
  297. }
  298. /*
  299. * helper function to update an extent delayed ref in the
  300. * rbtree. existing and update must both have the same
  301. * bytenr and parent
  302. *
  303. * This may free existing if the update cancels out whatever
  304. * operation it was doing.
  305. */
  306. static noinline void
  307. update_existing_ref(struct btrfs_trans_handle *trans,
  308. struct btrfs_delayed_ref_root *delayed_refs,
  309. struct btrfs_delayed_ref_node *existing,
  310. struct btrfs_delayed_ref_node *update)
  311. {
  312. struct btrfs_delayed_ref *existing_ref;
  313. struct btrfs_delayed_ref *ref;
  314. existing_ref = btrfs_delayed_node_to_ref(existing);
  315. ref = btrfs_delayed_node_to_ref(update);
  316. if (ref->pin)
  317. existing_ref->pin = 1;
  318. if (ref->action != existing_ref->action) {
  319. /*
  320. * this is effectively undoing either an add or a
  321. * drop. We decrement the ref_mod, and if it goes
  322. * down to zero we just delete the entry without
  323. * every changing the extent allocation tree.
  324. */
  325. existing->ref_mod--;
  326. if (existing->ref_mod == 0) {
  327. rb_erase(&existing->rb_node,
  328. &delayed_refs->root);
  329. existing->in_tree = 0;
  330. btrfs_put_delayed_ref(existing);
  331. delayed_refs->num_entries--;
  332. if (trans->delayed_ref_updates)
  333. trans->delayed_ref_updates--;
  334. }
  335. } else {
  336. if (existing_ref->action == BTRFS_ADD_DELAYED_REF) {
  337. /* if we're adding refs, make sure all the
  338. * details match up. The extent could
  339. * have been totally freed and reallocated
  340. * by a different owner before the delayed
  341. * ref entries were removed.
  342. */
  343. existing_ref->owner_objectid = ref->owner_objectid;
  344. existing_ref->generation = ref->generation;
  345. existing_ref->root = ref->root;
  346. existing->num_bytes = update->num_bytes;
  347. }
  348. /*
  349. * the action on the existing ref matches
  350. * the action on the ref we're trying to add.
  351. * Bump the ref_mod by one so the backref that
  352. * is eventually added/removed has the correct
  353. * reference count
  354. */
  355. existing->ref_mod += update->ref_mod;
  356. }
  357. }
  358. /*
  359. * helper function to update the accounting in the head ref
  360. * existing and update must have the same bytenr
  361. */
  362. static noinline void
  363. update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
  364. struct btrfs_delayed_ref_node *update)
  365. {
  366. struct btrfs_delayed_ref_head *existing_ref;
  367. struct btrfs_delayed_ref_head *ref;
  368. existing_ref = btrfs_delayed_node_to_head(existing);
  369. ref = btrfs_delayed_node_to_head(update);
  370. if (ref->must_insert_reserved) {
  371. /* if the extent was freed and then
  372. * reallocated before the delayed ref
  373. * entries were processed, we can end up
  374. * with an existing head ref without
  375. * the must_insert_reserved flag set.
  376. * Set it again here
  377. */
  378. existing_ref->must_insert_reserved = ref->must_insert_reserved;
  379. /*
  380. * update the num_bytes so we make sure the accounting
  381. * is done correctly
  382. */
  383. existing->num_bytes = update->num_bytes;
  384. }
  385. /*
  386. * update the reference mod on the head to reflect this new operation
  387. */
  388. existing->ref_mod += update->ref_mod;
  389. }
  390. /*
  391. * helper function to actually insert a delayed ref into the rbtree.
  392. * this does all the dirty work in terms of maintaining the correct
  393. * overall modification count in the head node and properly dealing
  394. * with updating existing nodes as new modifications are queued.
  395. */
  396. static noinline int __btrfs_add_delayed_ref(struct btrfs_trans_handle *trans,
  397. struct btrfs_delayed_ref_node *ref,
  398. u64 bytenr, u64 num_bytes, u64 parent, u64 ref_root,
  399. u64 ref_generation, u64 owner_objectid, int action,
  400. int pin)
  401. {
  402. struct btrfs_delayed_ref_node *existing;
  403. struct btrfs_delayed_ref *full_ref;
  404. struct btrfs_delayed_ref_head *head_ref = NULL;
  405. struct btrfs_delayed_ref_root *delayed_refs;
  406. int count_mod = 1;
  407. int must_insert_reserved = 0;
  408. /*
  409. * the head node stores the sum of all the mods, so dropping a ref
  410. * should drop the sum in the head node by one.
  411. */
  412. if (parent == (u64)-1 && action == BTRFS_DROP_DELAYED_REF)
  413. count_mod = -1;
  414. /*
  415. * BTRFS_ADD_DELAYED_EXTENT means that we need to update
  416. * the reserved accounting when the extent is finally added, or
  417. * if a later modification deletes the delayed ref without ever
  418. * inserting the extent into the extent allocation tree.
  419. * ref->must_insert_reserved is the flag used to record
  420. * that accounting mods are required.
  421. *
  422. * Once we record must_insert_reserved, switch the action to
  423. * BTRFS_ADD_DELAYED_REF because other special casing is not required.
  424. */
  425. if (action == BTRFS_ADD_DELAYED_EXTENT) {
  426. must_insert_reserved = 1;
  427. action = BTRFS_ADD_DELAYED_REF;
  428. } else {
  429. must_insert_reserved = 0;
  430. }
  431. delayed_refs = &trans->transaction->delayed_refs;
  432. /* first set the basic ref node struct up */
  433. atomic_set(&ref->refs, 1);
  434. ref->bytenr = bytenr;
  435. ref->parent = parent;
  436. ref->ref_mod = count_mod;
  437. ref->in_tree = 1;
  438. ref->num_bytes = num_bytes;
  439. if (btrfs_delayed_ref_is_head(ref)) {
  440. head_ref = btrfs_delayed_node_to_head(ref);
  441. head_ref->must_insert_reserved = must_insert_reserved;
  442. INIT_LIST_HEAD(&head_ref->cluster);
  443. mutex_init(&head_ref->mutex);
  444. } else {
  445. full_ref = btrfs_delayed_node_to_ref(ref);
  446. full_ref->root = ref_root;
  447. full_ref->generation = ref_generation;
  448. full_ref->owner_objectid = owner_objectid;
  449. full_ref->pin = pin;
  450. full_ref->action = action;
  451. }
  452. existing = tree_insert(&delayed_refs->root, bytenr,
  453. parent, &ref->rb_node);
  454. if (existing) {
  455. if (btrfs_delayed_ref_is_head(ref))
  456. update_existing_head_ref(existing, ref);
  457. else
  458. update_existing_ref(trans, delayed_refs, existing, ref);
  459. /*
  460. * we've updated the existing ref, free the newly
  461. * allocated ref
  462. */
  463. kfree(ref);
  464. } else {
  465. if (btrfs_delayed_ref_is_head(ref)) {
  466. delayed_refs->num_heads++;
  467. delayed_refs->num_heads_ready++;
  468. }
  469. delayed_refs->num_entries++;
  470. trans->delayed_ref_updates++;
  471. }
  472. return 0;
  473. }
  474. /*
  475. * add a delayed ref to the tree. This does all of the accounting required
  476. * to make sure the delayed ref is eventually processed before this
  477. * transaction commits.
  478. */
  479. int btrfs_add_delayed_ref(struct btrfs_trans_handle *trans,
  480. u64 bytenr, u64 num_bytes, u64 parent, u64 ref_root,
  481. u64 ref_generation, u64 owner_objectid, int action,
  482. int pin)
  483. {
  484. struct btrfs_delayed_ref *ref;
  485. struct btrfs_delayed_ref_head *head_ref;
  486. struct btrfs_delayed_ref_root *delayed_refs;
  487. int ret;
  488. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  489. if (!ref)
  490. return -ENOMEM;
  491. /*
  492. * the parent = 0 case comes from cases where we don't actually
  493. * know the parent yet. It will get updated later via a add/drop
  494. * pair.
  495. */
  496. if (parent == 0)
  497. parent = bytenr;
  498. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  499. if (!head_ref) {
  500. kfree(ref);
  501. return -ENOMEM;
  502. }
  503. delayed_refs = &trans->transaction->delayed_refs;
  504. spin_lock(&delayed_refs->lock);
  505. /*
  506. * insert both the head node and the new ref without dropping
  507. * the spin lock
  508. */
  509. ret = __btrfs_add_delayed_ref(trans, &head_ref->node, bytenr, num_bytes,
  510. (u64)-1, 0, 0, 0, action, pin);
  511. BUG_ON(ret);
  512. ret = __btrfs_add_delayed_ref(trans, &ref->node, bytenr, num_bytes,
  513. parent, ref_root, ref_generation,
  514. owner_objectid, action, pin);
  515. BUG_ON(ret);
  516. spin_unlock(&delayed_refs->lock);
  517. return 0;
  518. }
  519. /*
  520. * this does a simple search for the head node for a given extent.
  521. * It must be called with the delayed ref spinlock held, and it returns
  522. * the head node if any where found, or NULL if not.
  523. */
  524. struct btrfs_delayed_ref_head *
  525. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
  526. {
  527. struct btrfs_delayed_ref_node *ref;
  528. struct btrfs_delayed_ref_root *delayed_refs;
  529. delayed_refs = &trans->transaction->delayed_refs;
  530. ref = tree_search(&delayed_refs->root, bytenr, (u64)-1, NULL);
  531. if (ref)
  532. return btrfs_delayed_node_to_head(ref);
  533. return NULL;
  534. }
  535. /*
  536. * add a delayed ref to the tree. This does all of the accounting required
  537. * to make sure the delayed ref is eventually processed before this
  538. * transaction commits.
  539. *
  540. * The main point of this call is to add and remove a backreference in a single
  541. * shot, taking the lock only once, and only searching for the head node once.
  542. *
  543. * It is the same as doing a ref add and delete in two separate calls.
  544. */
  545. int btrfs_update_delayed_ref(struct btrfs_trans_handle *trans,
  546. u64 bytenr, u64 num_bytes, u64 orig_parent,
  547. u64 parent, u64 orig_ref_root, u64 ref_root,
  548. u64 orig_ref_generation, u64 ref_generation,
  549. u64 owner_objectid, int pin)
  550. {
  551. struct btrfs_delayed_ref *ref;
  552. struct btrfs_delayed_ref *old_ref;
  553. struct btrfs_delayed_ref_head *head_ref;
  554. struct btrfs_delayed_ref_root *delayed_refs;
  555. int ret;
  556. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  557. if (!ref)
  558. return -ENOMEM;
  559. old_ref = kmalloc(sizeof(*old_ref), GFP_NOFS);
  560. if (!old_ref) {
  561. kfree(ref);
  562. return -ENOMEM;
  563. }
  564. /*
  565. * the parent = 0 case comes from cases where we don't actually
  566. * know the parent yet. It will get updated later via a add/drop
  567. * pair.
  568. */
  569. if (parent == 0)
  570. parent = bytenr;
  571. if (orig_parent == 0)
  572. orig_parent = bytenr;
  573. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  574. if (!head_ref) {
  575. kfree(ref);
  576. kfree(old_ref);
  577. return -ENOMEM;
  578. }
  579. delayed_refs = &trans->transaction->delayed_refs;
  580. spin_lock(&delayed_refs->lock);
  581. /*
  582. * insert both the head node and the new ref without dropping
  583. * the spin lock
  584. */
  585. ret = __btrfs_add_delayed_ref(trans, &head_ref->node, bytenr, num_bytes,
  586. (u64)-1, 0, 0, 0,
  587. BTRFS_ADD_DELAYED_REF, 0);
  588. BUG_ON(ret);
  589. ret = __btrfs_add_delayed_ref(trans, &ref->node, bytenr, num_bytes,
  590. parent, ref_root, ref_generation,
  591. owner_objectid, BTRFS_ADD_DELAYED_REF, 0);
  592. BUG_ON(ret);
  593. ret = __btrfs_add_delayed_ref(trans, &old_ref->node, bytenr, num_bytes,
  594. orig_parent, orig_ref_root,
  595. orig_ref_generation, owner_objectid,
  596. BTRFS_DROP_DELAYED_REF, pin);
  597. BUG_ON(ret);
  598. spin_unlock(&delayed_refs->lock);
  599. return 0;
  600. }