delayed-ref.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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_fs_info *fs_info,
  365. struct btrfs_trans_handle *trans,
  366. struct btrfs_delayed_ref_node *ref,
  367. u64 bytenr, u64 num_bytes,
  368. int action, int is_data)
  369. {
  370. struct btrfs_delayed_ref_node *existing;
  371. struct btrfs_delayed_ref_head *head_ref = NULL;
  372. struct btrfs_delayed_ref_root *delayed_refs;
  373. int count_mod = 1;
  374. int must_insert_reserved = 0;
  375. /*
  376. * the head node stores the sum of all the mods, so dropping a ref
  377. * should drop the sum in the head node by one.
  378. */
  379. if (action == BTRFS_UPDATE_DELAYED_HEAD)
  380. count_mod = 0;
  381. else if (action == BTRFS_DROP_DELAYED_REF)
  382. count_mod = -1;
  383. /*
  384. * BTRFS_ADD_DELAYED_EXTENT means that we need to update
  385. * the reserved accounting when the extent is finally added, or
  386. * if a later modification deletes the delayed ref without ever
  387. * inserting the extent into the extent allocation tree.
  388. * ref->must_insert_reserved is the flag used to record
  389. * that accounting mods are required.
  390. *
  391. * Once we record must_insert_reserved, switch the action to
  392. * BTRFS_ADD_DELAYED_REF because other special casing is not required.
  393. */
  394. if (action == BTRFS_ADD_DELAYED_EXTENT)
  395. must_insert_reserved = 1;
  396. else
  397. must_insert_reserved = 0;
  398. delayed_refs = &trans->transaction->delayed_refs;
  399. /* first set the basic ref node struct up */
  400. atomic_set(&ref->refs, 1);
  401. ref->bytenr = bytenr;
  402. ref->num_bytes = num_bytes;
  403. ref->ref_mod = count_mod;
  404. ref->type = 0;
  405. ref->action = 0;
  406. ref->is_head = 1;
  407. ref->in_tree = 1;
  408. head_ref = btrfs_delayed_node_to_head(ref);
  409. head_ref->must_insert_reserved = must_insert_reserved;
  410. head_ref->is_data = is_data;
  411. INIT_LIST_HEAD(&head_ref->cluster);
  412. mutex_init(&head_ref->mutex);
  413. trace_btrfs_delayed_ref_head(ref, head_ref, action);
  414. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  415. if (existing) {
  416. update_existing_head_ref(existing, ref);
  417. /*
  418. * we've updated the existing ref, free the newly
  419. * allocated ref
  420. */
  421. kfree(ref);
  422. } else {
  423. delayed_refs->num_heads++;
  424. delayed_refs->num_heads_ready++;
  425. delayed_refs->num_entries++;
  426. trans->delayed_ref_updates++;
  427. }
  428. return 0;
  429. }
  430. /*
  431. * helper to insert a delayed tree ref into the rbtree.
  432. */
  433. static noinline int add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  434. struct btrfs_trans_handle *trans,
  435. struct btrfs_delayed_ref_node *ref,
  436. u64 bytenr, u64 num_bytes, u64 parent,
  437. u64 ref_root, int level, int action,
  438. int for_cow)
  439. {
  440. struct btrfs_delayed_ref_node *existing;
  441. struct btrfs_delayed_tree_ref *full_ref;
  442. struct btrfs_delayed_ref_root *delayed_refs;
  443. if (action == BTRFS_ADD_DELAYED_EXTENT)
  444. action = BTRFS_ADD_DELAYED_REF;
  445. delayed_refs = &trans->transaction->delayed_refs;
  446. /* first set the basic ref node struct up */
  447. atomic_set(&ref->refs, 1);
  448. ref->bytenr = bytenr;
  449. ref->num_bytes = num_bytes;
  450. ref->ref_mod = 1;
  451. ref->action = action;
  452. ref->is_head = 0;
  453. ref->in_tree = 1;
  454. full_ref = btrfs_delayed_node_to_tree_ref(ref);
  455. if (parent) {
  456. full_ref->parent = parent;
  457. ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
  458. } else {
  459. full_ref->root = ref_root;
  460. ref->type = BTRFS_TREE_BLOCK_REF_KEY;
  461. }
  462. full_ref->level = level;
  463. trace_btrfs_delayed_tree_ref(ref, full_ref, action);
  464. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  465. if (existing) {
  466. update_existing_ref(trans, delayed_refs, existing, ref);
  467. /*
  468. * we've updated the existing ref, free the newly
  469. * allocated ref
  470. */
  471. kfree(ref);
  472. } else {
  473. delayed_refs->num_entries++;
  474. trans->delayed_ref_updates++;
  475. }
  476. return 0;
  477. }
  478. /*
  479. * helper to insert a delayed data ref into the rbtree.
  480. */
  481. static noinline int add_delayed_data_ref(struct btrfs_fs_info *fs_info,
  482. struct btrfs_trans_handle *trans,
  483. struct btrfs_delayed_ref_node *ref,
  484. u64 bytenr, u64 num_bytes, u64 parent,
  485. u64 ref_root, u64 owner, u64 offset,
  486. int action, int for_cow)
  487. {
  488. struct btrfs_delayed_ref_node *existing;
  489. struct btrfs_delayed_data_ref *full_ref;
  490. struct btrfs_delayed_ref_root *delayed_refs;
  491. if (action == BTRFS_ADD_DELAYED_EXTENT)
  492. action = BTRFS_ADD_DELAYED_REF;
  493. delayed_refs = &trans->transaction->delayed_refs;
  494. /* first set the basic ref node struct up */
  495. atomic_set(&ref->refs, 1);
  496. ref->bytenr = bytenr;
  497. ref->num_bytes = num_bytes;
  498. ref->ref_mod = 1;
  499. ref->action = action;
  500. ref->is_head = 0;
  501. ref->in_tree = 1;
  502. full_ref = btrfs_delayed_node_to_data_ref(ref);
  503. if (parent) {
  504. full_ref->parent = parent;
  505. ref->type = BTRFS_SHARED_DATA_REF_KEY;
  506. } else {
  507. full_ref->root = ref_root;
  508. ref->type = BTRFS_EXTENT_DATA_REF_KEY;
  509. }
  510. full_ref->objectid = owner;
  511. full_ref->offset = offset;
  512. trace_btrfs_delayed_data_ref(ref, full_ref, action);
  513. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  514. if (existing) {
  515. update_existing_ref(trans, delayed_refs, existing, ref);
  516. /*
  517. * we've updated the existing ref, free the newly
  518. * allocated ref
  519. */
  520. kfree(ref);
  521. } else {
  522. delayed_refs->num_entries++;
  523. trans->delayed_ref_updates++;
  524. }
  525. return 0;
  526. }
  527. /*
  528. * add a delayed tree ref. This does all of the accounting required
  529. * to make sure the delayed ref is eventually processed before this
  530. * transaction commits.
  531. */
  532. int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  533. struct btrfs_trans_handle *trans,
  534. u64 bytenr, u64 num_bytes, u64 parent,
  535. u64 ref_root, int level, int action,
  536. struct btrfs_delayed_extent_op *extent_op,
  537. int for_cow)
  538. {
  539. struct btrfs_delayed_tree_ref *ref;
  540. struct btrfs_delayed_ref_head *head_ref;
  541. struct btrfs_delayed_ref_root *delayed_refs;
  542. int ret;
  543. BUG_ON(extent_op && extent_op->is_data);
  544. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  545. if (!ref)
  546. return -ENOMEM;
  547. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  548. if (!head_ref) {
  549. kfree(ref);
  550. return -ENOMEM;
  551. }
  552. head_ref->extent_op = extent_op;
  553. delayed_refs = &trans->transaction->delayed_refs;
  554. spin_lock(&delayed_refs->lock);
  555. /*
  556. * insert both the head node and the new ref without dropping
  557. * the spin lock
  558. */
  559. ret = add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  560. num_bytes, action, 0);
  561. BUG_ON(ret);
  562. ret = add_delayed_tree_ref(fs_info, trans, &ref->node, bytenr,
  563. num_bytes, parent, ref_root, level, action,
  564. for_cow);
  565. BUG_ON(ret);
  566. spin_unlock(&delayed_refs->lock);
  567. return 0;
  568. }
  569. /*
  570. * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
  571. */
  572. int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
  573. struct btrfs_trans_handle *trans,
  574. u64 bytenr, u64 num_bytes,
  575. u64 parent, u64 ref_root,
  576. u64 owner, u64 offset, int action,
  577. struct btrfs_delayed_extent_op *extent_op,
  578. int for_cow)
  579. {
  580. struct btrfs_delayed_data_ref *ref;
  581. struct btrfs_delayed_ref_head *head_ref;
  582. struct btrfs_delayed_ref_root *delayed_refs;
  583. int ret;
  584. BUG_ON(extent_op && !extent_op->is_data);
  585. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  586. if (!ref)
  587. return -ENOMEM;
  588. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  589. if (!head_ref) {
  590. kfree(ref);
  591. return -ENOMEM;
  592. }
  593. head_ref->extent_op = extent_op;
  594. delayed_refs = &trans->transaction->delayed_refs;
  595. spin_lock(&delayed_refs->lock);
  596. /*
  597. * insert both the head node and the new ref without dropping
  598. * the spin lock
  599. */
  600. ret = add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  601. num_bytes, action, 1);
  602. BUG_ON(ret);
  603. ret = add_delayed_data_ref(fs_info, trans, &ref->node, bytenr,
  604. num_bytes, parent, ref_root, owner, offset,
  605. action, for_cow);
  606. BUG_ON(ret);
  607. spin_unlock(&delayed_refs->lock);
  608. return 0;
  609. }
  610. int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
  611. struct btrfs_trans_handle *trans,
  612. u64 bytenr, u64 num_bytes,
  613. struct btrfs_delayed_extent_op *extent_op)
  614. {
  615. struct btrfs_delayed_ref_head *head_ref;
  616. struct btrfs_delayed_ref_root *delayed_refs;
  617. int ret;
  618. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  619. if (!head_ref)
  620. return -ENOMEM;
  621. head_ref->extent_op = extent_op;
  622. delayed_refs = &trans->transaction->delayed_refs;
  623. spin_lock(&delayed_refs->lock);
  624. ret = add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  625. num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
  626. extent_op->is_data);
  627. BUG_ON(ret);
  628. spin_unlock(&delayed_refs->lock);
  629. return 0;
  630. }
  631. /*
  632. * this does a simple search for the head node for a given extent.
  633. * It must be called with the delayed ref spinlock held, and it returns
  634. * the head node if any where found, or NULL if not.
  635. */
  636. struct btrfs_delayed_ref_head *
  637. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
  638. {
  639. struct btrfs_delayed_ref_node *ref;
  640. struct btrfs_delayed_ref_root *delayed_refs;
  641. delayed_refs = &trans->transaction->delayed_refs;
  642. ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
  643. if (ref)
  644. return btrfs_delayed_node_to_head(ref);
  645. return NULL;
  646. }