delayed-ref.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. * This checks to see if there are any delayed refs in the
  263. * btree for a given bytenr. It returns one if it finds any
  264. * and zero otherwise.
  265. *
  266. * If it only finds a head node, it returns 0.
  267. *
  268. * The idea is to use this when deciding if you can safely delete an
  269. * extent from the extent allocation tree. There may be a pending
  270. * ref in the rbtree that adds or removes references, so as long as this
  271. * returns one you need to leave the BTRFS_EXTENT_ITEM in the extent
  272. * allocation tree.
  273. */
  274. int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr)
  275. {
  276. struct btrfs_delayed_ref_node *ref;
  277. struct btrfs_delayed_ref_root *delayed_refs;
  278. struct rb_node *prev_node;
  279. int ret = 0;
  280. delayed_refs = &trans->transaction->delayed_refs;
  281. spin_lock(&delayed_refs->lock);
  282. ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
  283. if (ref) {
  284. prev_node = rb_prev(&ref->rb_node);
  285. if (!prev_node)
  286. goto out;
  287. ref = rb_entry(prev_node, struct btrfs_delayed_ref_node,
  288. rb_node);
  289. if (ref->bytenr == bytenr)
  290. ret = 1;
  291. }
  292. out:
  293. spin_unlock(&delayed_refs->lock);
  294. return ret;
  295. }
  296. /*
  297. * helper function to update an extent delayed ref in the
  298. * rbtree. existing and update must both have the same
  299. * bytenr and parent
  300. *
  301. * This may free existing if the update cancels out whatever
  302. * operation it was doing.
  303. */
  304. static noinline void
  305. update_existing_ref(struct btrfs_trans_handle *trans,
  306. struct btrfs_delayed_ref_root *delayed_refs,
  307. struct btrfs_delayed_ref_node *existing,
  308. struct btrfs_delayed_ref_node *update)
  309. {
  310. if (update->action != existing->action) {
  311. /*
  312. * this is effectively undoing either an add or a
  313. * drop. We decrement the ref_mod, and if it goes
  314. * down to zero we just delete the entry without
  315. * every changing the extent allocation tree.
  316. */
  317. existing->ref_mod--;
  318. if (existing->ref_mod == 0) {
  319. rb_erase(&existing->rb_node,
  320. &delayed_refs->root);
  321. existing->in_tree = 0;
  322. btrfs_put_delayed_ref(existing);
  323. delayed_refs->num_entries--;
  324. if (trans->delayed_ref_updates)
  325. trans->delayed_ref_updates--;
  326. } else {
  327. WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
  328. existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
  329. }
  330. } else {
  331. WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
  332. existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
  333. /*
  334. * the action on the existing ref matches
  335. * the action on the ref we're trying to add.
  336. * Bump the ref_mod by one so the backref that
  337. * is eventually added/removed has the correct
  338. * reference count
  339. */
  340. existing->ref_mod += update->ref_mod;
  341. }
  342. }
  343. /*
  344. * helper function to update the accounting in the head ref
  345. * existing and update must have the same bytenr
  346. */
  347. static noinline void
  348. update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
  349. struct btrfs_delayed_ref_node *update)
  350. {
  351. struct btrfs_delayed_ref_head *existing_ref;
  352. struct btrfs_delayed_ref_head *ref;
  353. existing_ref = btrfs_delayed_node_to_head(existing);
  354. ref = btrfs_delayed_node_to_head(update);
  355. BUG_ON(existing_ref->is_data != ref->is_data);
  356. if (ref->must_insert_reserved) {
  357. /* if the extent was freed and then
  358. * reallocated before the delayed ref
  359. * entries were processed, we can end up
  360. * with an existing head ref without
  361. * the must_insert_reserved flag set.
  362. * Set it again here
  363. */
  364. existing_ref->must_insert_reserved = ref->must_insert_reserved;
  365. /*
  366. * update the num_bytes so we make sure the accounting
  367. * is done correctly
  368. */
  369. existing->num_bytes = update->num_bytes;
  370. }
  371. if (ref->extent_op) {
  372. if (!existing_ref->extent_op) {
  373. existing_ref->extent_op = ref->extent_op;
  374. } else {
  375. if (ref->extent_op->update_key) {
  376. memcpy(&existing_ref->extent_op->key,
  377. &ref->extent_op->key,
  378. sizeof(ref->extent_op->key));
  379. existing_ref->extent_op->update_key = 1;
  380. }
  381. if (ref->extent_op->update_flags) {
  382. existing_ref->extent_op->flags_to_set |=
  383. ref->extent_op->flags_to_set;
  384. existing_ref->extent_op->update_flags = 1;
  385. }
  386. kfree(ref->extent_op);
  387. }
  388. }
  389. /*
  390. * update the reference mod on the head to reflect this new operation
  391. */
  392. existing->ref_mod += update->ref_mod;
  393. }
  394. /*
  395. * helper function to actually insert a head node into the rbtree.
  396. * this does all the dirty work in terms of maintaining the correct
  397. * overall modification count.
  398. */
  399. static noinline int add_delayed_ref_head(struct btrfs_trans_handle *trans,
  400. struct btrfs_delayed_ref_node *ref,
  401. u64 bytenr, u64 num_bytes,
  402. int action, int is_data)
  403. {
  404. struct btrfs_delayed_ref_node *existing;
  405. struct btrfs_delayed_ref_head *head_ref = NULL;
  406. struct btrfs_delayed_ref_root *delayed_refs;
  407. int count_mod = 1;
  408. int must_insert_reserved = 0;
  409. /*
  410. * the head node stores the sum of all the mods, so dropping a ref
  411. * should drop the sum in the head node by one.
  412. */
  413. if (action == BTRFS_UPDATE_DELAYED_HEAD)
  414. count_mod = 0;
  415. else if (action == BTRFS_DROP_DELAYED_REF)
  416. count_mod = -1;
  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. else
  431. must_insert_reserved = 0;
  432. delayed_refs = &trans->transaction->delayed_refs;
  433. /* first set the basic ref node struct up */
  434. atomic_set(&ref->refs, 1);
  435. ref->bytenr = bytenr;
  436. ref->num_bytes = num_bytes;
  437. ref->ref_mod = count_mod;
  438. ref->type = 0;
  439. ref->action = 0;
  440. ref->is_head = 1;
  441. ref->in_tree = 1;
  442. head_ref = btrfs_delayed_node_to_head(ref);
  443. head_ref->must_insert_reserved = must_insert_reserved;
  444. head_ref->is_data = is_data;
  445. INIT_LIST_HEAD(&head_ref->cluster);
  446. mutex_init(&head_ref->mutex);
  447. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  448. if (existing) {
  449. update_existing_head_ref(existing, ref);
  450. /*
  451. * we've updated the existing ref, free the newly
  452. * allocated ref
  453. */
  454. kfree(ref);
  455. } else {
  456. delayed_refs->num_heads++;
  457. delayed_refs->num_heads_ready++;
  458. delayed_refs->num_entries++;
  459. trans->delayed_ref_updates++;
  460. }
  461. return 0;
  462. }
  463. /*
  464. * helper to insert a delayed tree ref into the rbtree.
  465. */
  466. static noinline int add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  467. struct btrfs_delayed_ref_node *ref,
  468. u64 bytenr, u64 num_bytes, u64 parent,
  469. u64 ref_root, int level, int action)
  470. {
  471. struct btrfs_delayed_ref_node *existing;
  472. struct btrfs_delayed_tree_ref *full_ref;
  473. struct btrfs_delayed_ref_root *delayed_refs;
  474. if (action == BTRFS_ADD_DELAYED_EXTENT)
  475. action = BTRFS_ADD_DELAYED_REF;
  476. delayed_refs = &trans->transaction->delayed_refs;
  477. /* first set the basic ref node struct up */
  478. atomic_set(&ref->refs, 1);
  479. ref->bytenr = bytenr;
  480. ref->num_bytes = num_bytes;
  481. ref->ref_mod = 1;
  482. ref->action = action;
  483. ref->is_head = 0;
  484. ref->in_tree = 1;
  485. full_ref = btrfs_delayed_node_to_tree_ref(ref);
  486. if (parent) {
  487. full_ref->parent = parent;
  488. ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
  489. } else {
  490. full_ref->root = ref_root;
  491. ref->type = BTRFS_TREE_BLOCK_REF_KEY;
  492. }
  493. full_ref->level = level;
  494. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  495. if (existing) {
  496. update_existing_ref(trans, delayed_refs, existing, ref);
  497. /*
  498. * we've updated the existing ref, free the newly
  499. * allocated ref
  500. */
  501. kfree(ref);
  502. } else {
  503. delayed_refs->num_entries++;
  504. trans->delayed_ref_updates++;
  505. }
  506. return 0;
  507. }
  508. /*
  509. * helper to insert a delayed data ref into the rbtree.
  510. */
  511. static noinline int add_delayed_data_ref(struct btrfs_trans_handle *trans,
  512. struct btrfs_delayed_ref_node *ref,
  513. u64 bytenr, u64 num_bytes, u64 parent,
  514. u64 ref_root, u64 owner, u64 offset,
  515. int action)
  516. {
  517. struct btrfs_delayed_ref_node *existing;
  518. struct btrfs_delayed_data_ref *full_ref;
  519. struct btrfs_delayed_ref_root *delayed_refs;
  520. if (action == BTRFS_ADD_DELAYED_EXTENT)
  521. action = BTRFS_ADD_DELAYED_REF;
  522. delayed_refs = &trans->transaction->delayed_refs;
  523. /* first set the basic ref node struct up */
  524. atomic_set(&ref->refs, 1);
  525. ref->bytenr = bytenr;
  526. ref->num_bytes = num_bytes;
  527. ref->ref_mod = 1;
  528. ref->action = action;
  529. ref->is_head = 0;
  530. ref->in_tree = 1;
  531. full_ref = btrfs_delayed_node_to_data_ref(ref);
  532. if (parent) {
  533. full_ref->parent = parent;
  534. ref->type = BTRFS_SHARED_DATA_REF_KEY;
  535. } else {
  536. full_ref->root = ref_root;
  537. ref->type = BTRFS_EXTENT_DATA_REF_KEY;
  538. }
  539. full_ref->objectid = owner;
  540. full_ref->offset = offset;
  541. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  542. if (existing) {
  543. update_existing_ref(trans, delayed_refs, existing, ref);
  544. /*
  545. * we've updated the existing ref, free the newly
  546. * allocated ref
  547. */
  548. kfree(ref);
  549. } else {
  550. delayed_refs->num_entries++;
  551. trans->delayed_ref_updates++;
  552. }
  553. return 0;
  554. }
  555. /*
  556. * add a delayed tree ref. This does all of the accounting required
  557. * to make sure the delayed ref is eventually processed before this
  558. * transaction commits.
  559. */
  560. int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  561. u64 bytenr, u64 num_bytes, u64 parent,
  562. u64 ref_root, int level, int action,
  563. struct btrfs_delayed_extent_op *extent_op)
  564. {
  565. struct btrfs_delayed_tree_ref *ref;
  566. struct btrfs_delayed_ref_head *head_ref;
  567. struct btrfs_delayed_ref_root *delayed_refs;
  568. int ret;
  569. BUG_ON(extent_op && extent_op->is_data);
  570. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  571. if (!ref)
  572. return -ENOMEM;
  573. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  574. if (!head_ref) {
  575. kfree(ref);
  576. return -ENOMEM;
  577. }
  578. head_ref->extent_op = extent_op;
  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 = add_delayed_ref_head(trans, &head_ref->node, bytenr, num_bytes,
  586. action, 0);
  587. BUG_ON(ret);
  588. ret = add_delayed_tree_ref(trans, &ref->node, bytenr, num_bytes,
  589. parent, ref_root, level, action);
  590. BUG_ON(ret);
  591. spin_unlock(&delayed_refs->lock);
  592. return 0;
  593. }
  594. /*
  595. * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
  596. */
  597. int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
  598. u64 bytenr, u64 num_bytes,
  599. u64 parent, u64 ref_root,
  600. u64 owner, u64 offset, int action,
  601. struct btrfs_delayed_extent_op *extent_op)
  602. {
  603. struct btrfs_delayed_data_ref *ref;
  604. struct btrfs_delayed_ref_head *head_ref;
  605. struct btrfs_delayed_ref_root *delayed_refs;
  606. int ret;
  607. BUG_ON(extent_op && !extent_op->is_data);
  608. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  609. if (!ref)
  610. return -ENOMEM;
  611. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  612. if (!head_ref) {
  613. kfree(ref);
  614. return -ENOMEM;
  615. }
  616. head_ref->extent_op = extent_op;
  617. delayed_refs = &trans->transaction->delayed_refs;
  618. spin_lock(&delayed_refs->lock);
  619. /*
  620. * insert both the head node and the new ref without dropping
  621. * the spin lock
  622. */
  623. ret = add_delayed_ref_head(trans, &head_ref->node, bytenr, num_bytes,
  624. action, 1);
  625. BUG_ON(ret);
  626. ret = add_delayed_data_ref(trans, &ref->node, bytenr, num_bytes,
  627. parent, ref_root, owner, offset, action);
  628. BUG_ON(ret);
  629. spin_unlock(&delayed_refs->lock);
  630. return 0;
  631. }
  632. int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
  633. u64 bytenr, u64 num_bytes,
  634. struct btrfs_delayed_extent_op *extent_op)
  635. {
  636. struct btrfs_delayed_ref_head *head_ref;
  637. struct btrfs_delayed_ref_root *delayed_refs;
  638. int ret;
  639. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  640. if (!head_ref)
  641. return -ENOMEM;
  642. head_ref->extent_op = extent_op;
  643. delayed_refs = &trans->transaction->delayed_refs;
  644. spin_lock(&delayed_refs->lock);
  645. ret = add_delayed_ref_head(trans, &head_ref->node, bytenr,
  646. num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
  647. extent_op->is_data);
  648. BUG_ON(ret);
  649. spin_unlock(&delayed_refs->lock);
  650. return 0;
  651. }
  652. /*
  653. * this does a simple search for the head node for a given extent.
  654. * It must be called with the delayed ref spinlock held, and it returns
  655. * the head node if any where found, or NULL if not.
  656. */
  657. struct btrfs_delayed_ref_head *
  658. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
  659. {
  660. struct btrfs_delayed_ref_node *ref;
  661. struct btrfs_delayed_ref_root *delayed_refs;
  662. delayed_refs = &trans->transaction->delayed_refs;
  663. ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
  664. if (ref)
  665. return btrfs_delayed_node_to_head(ref);
  666. return NULL;
  667. }
  668. /*
  669. * add a delayed ref to the tree. This does all of the accounting required
  670. * to make sure the delayed ref is eventually processed before this
  671. * transaction commits.
  672. *
  673. * The main point of this call is to add and remove a backreference in a single
  674. * shot, taking the lock only once, and only searching for the head node once.
  675. *
  676. * It is the same as doing a ref add and delete in two separate calls.
  677. */
  678. #if 0
  679. int btrfs_update_delayed_ref(struct btrfs_trans_handle *trans,
  680. u64 bytenr, u64 num_bytes, u64 orig_parent,
  681. u64 parent, u64 orig_ref_root, u64 ref_root,
  682. u64 orig_ref_generation, u64 ref_generation,
  683. u64 owner_objectid, int pin)
  684. {
  685. struct btrfs_delayed_ref *ref;
  686. struct btrfs_delayed_ref *old_ref;
  687. struct btrfs_delayed_ref_head *head_ref;
  688. struct btrfs_delayed_ref_root *delayed_refs;
  689. int ret;
  690. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  691. if (!ref)
  692. return -ENOMEM;
  693. old_ref = kmalloc(sizeof(*old_ref), GFP_NOFS);
  694. if (!old_ref) {
  695. kfree(ref);
  696. return -ENOMEM;
  697. }
  698. /*
  699. * the parent = 0 case comes from cases where we don't actually
  700. * know the parent yet. It will get updated later via a add/drop
  701. * pair.
  702. */
  703. if (parent == 0)
  704. parent = bytenr;
  705. if (orig_parent == 0)
  706. orig_parent = bytenr;
  707. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  708. if (!head_ref) {
  709. kfree(ref);
  710. kfree(old_ref);
  711. return -ENOMEM;
  712. }
  713. delayed_refs = &trans->transaction->delayed_refs;
  714. spin_lock(&delayed_refs->lock);
  715. /*
  716. * insert both the head node and the new ref without dropping
  717. * the spin lock
  718. */
  719. ret = __btrfs_add_delayed_ref(trans, &head_ref->node, bytenr, num_bytes,
  720. (u64)-1, 0, 0, 0,
  721. BTRFS_UPDATE_DELAYED_HEAD, 0);
  722. BUG_ON(ret);
  723. ret = __btrfs_add_delayed_ref(trans, &ref->node, bytenr, num_bytes,
  724. parent, ref_root, ref_generation,
  725. owner_objectid, BTRFS_ADD_DELAYED_REF, 0);
  726. BUG_ON(ret);
  727. ret = __btrfs_add_delayed_ref(trans, &old_ref->node, bytenr, num_bytes,
  728. orig_parent, orig_ref_root,
  729. orig_ref_generation, owner_objectid,
  730. BTRFS_DROP_DELAYED_REF, pin);
  731. BUG_ON(ret);
  732. spin_unlock(&delayed_refs->lock);
  733. return 0;
  734. }
  735. #endif