delayed-ref.c 19 KB

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