delayed-ref.c 20 KB

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