delayed-ref.c 18 KB

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