delayed-ref.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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_fs_info *fs_info,
  219. struct btrfs_delayed_ref_root *delayed_refs,
  220. u64 seq)
  221. {
  222. struct seq_list *elem;
  223. int ret = 0;
  224. spin_lock(&fs_info->tree_mod_seq_lock);
  225. if (!list_empty(&fs_info->tree_mod_seq_list)) {
  226. elem = list_first_entry(&fs_info->tree_mod_seq_list,
  227. struct seq_list, list);
  228. if (seq >= elem->seq) {
  229. pr_debug("holding back delayed_ref %llu, lowest is "
  230. "%llu (%p)\n", seq, elem->seq, delayed_refs);
  231. ret = 1;
  232. }
  233. }
  234. spin_unlock(&fs_info->tree_mod_seq_lock);
  235. return ret;
  236. }
  237. int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
  238. struct list_head *cluster, u64 start)
  239. {
  240. int count = 0;
  241. struct btrfs_delayed_ref_root *delayed_refs;
  242. struct rb_node *node;
  243. struct btrfs_delayed_ref_node *ref;
  244. struct btrfs_delayed_ref_head *head;
  245. delayed_refs = &trans->transaction->delayed_refs;
  246. if (start == 0) {
  247. node = rb_first(&delayed_refs->root);
  248. } else {
  249. ref = NULL;
  250. find_ref_head(&delayed_refs->root, start + 1, &ref, 1);
  251. if (ref) {
  252. node = &ref->rb_node;
  253. } else
  254. node = rb_first(&delayed_refs->root);
  255. }
  256. again:
  257. while (node && count < 32) {
  258. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  259. if (btrfs_delayed_ref_is_head(ref)) {
  260. head = btrfs_delayed_node_to_head(ref);
  261. if (list_empty(&head->cluster)) {
  262. list_add_tail(&head->cluster, cluster);
  263. delayed_refs->run_delayed_start =
  264. head->node.bytenr;
  265. count++;
  266. WARN_ON(delayed_refs->num_heads_ready == 0);
  267. delayed_refs->num_heads_ready--;
  268. } else if (count) {
  269. /* the goal of the clustering is to find extents
  270. * that are likely to end up in the same extent
  271. * leaf on disk. So, we don't want them spread
  272. * all over the tree. Stop now if we've hit
  273. * a head that was already in use
  274. */
  275. break;
  276. }
  277. }
  278. node = rb_next(node);
  279. }
  280. if (count) {
  281. return 0;
  282. } else if (start) {
  283. /*
  284. * we've gone to the end of the rbtree without finding any
  285. * clusters. start from the beginning and try again
  286. */
  287. start = 0;
  288. node = rb_first(&delayed_refs->root);
  289. goto again;
  290. }
  291. return 1;
  292. }
  293. /*
  294. * helper function to update an extent delayed ref in the
  295. * rbtree. existing and update must both have the same
  296. * bytenr and parent
  297. *
  298. * This may free existing if the update cancels out whatever
  299. * operation it was doing.
  300. */
  301. static noinline void
  302. update_existing_ref(struct btrfs_trans_handle *trans,
  303. struct btrfs_delayed_ref_root *delayed_refs,
  304. struct btrfs_delayed_ref_node *existing,
  305. struct btrfs_delayed_ref_node *update)
  306. {
  307. if (update->action != existing->action) {
  308. /*
  309. * this is effectively undoing either an add or a
  310. * drop. We decrement the ref_mod, and if it goes
  311. * down to zero we just delete the entry without
  312. * every changing the extent allocation tree.
  313. */
  314. existing->ref_mod--;
  315. if (existing->ref_mod == 0) {
  316. rb_erase(&existing->rb_node,
  317. &delayed_refs->root);
  318. existing->in_tree = 0;
  319. btrfs_put_delayed_ref(existing);
  320. delayed_refs->num_entries--;
  321. if (trans->delayed_ref_updates)
  322. trans->delayed_ref_updates--;
  323. } else {
  324. WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
  325. existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
  326. }
  327. } else {
  328. WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
  329. existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
  330. /*
  331. * the action on the existing ref matches
  332. * the action on the ref we're trying to add.
  333. * Bump the ref_mod by one so the backref that
  334. * is eventually added/removed has the correct
  335. * reference count
  336. */
  337. existing->ref_mod += update->ref_mod;
  338. }
  339. }
  340. /*
  341. * helper function to update the accounting in the head ref
  342. * existing and update must have the same bytenr
  343. */
  344. static noinline void
  345. update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
  346. struct btrfs_delayed_ref_node *update)
  347. {
  348. struct btrfs_delayed_ref_head *existing_ref;
  349. struct btrfs_delayed_ref_head *ref;
  350. existing_ref = btrfs_delayed_node_to_head(existing);
  351. ref = btrfs_delayed_node_to_head(update);
  352. BUG_ON(existing_ref->is_data != ref->is_data);
  353. if (ref->must_insert_reserved) {
  354. /* if the extent was freed and then
  355. * reallocated before the delayed ref
  356. * entries were processed, we can end up
  357. * with an existing head ref without
  358. * the must_insert_reserved flag set.
  359. * Set it again here
  360. */
  361. existing_ref->must_insert_reserved = ref->must_insert_reserved;
  362. /*
  363. * update the num_bytes so we make sure the accounting
  364. * is done correctly
  365. */
  366. existing->num_bytes = update->num_bytes;
  367. }
  368. if (ref->extent_op) {
  369. if (!existing_ref->extent_op) {
  370. existing_ref->extent_op = ref->extent_op;
  371. } else {
  372. if (ref->extent_op->update_key) {
  373. memcpy(&existing_ref->extent_op->key,
  374. &ref->extent_op->key,
  375. sizeof(ref->extent_op->key));
  376. existing_ref->extent_op->update_key = 1;
  377. }
  378. if (ref->extent_op->update_flags) {
  379. existing_ref->extent_op->flags_to_set |=
  380. ref->extent_op->flags_to_set;
  381. existing_ref->extent_op->update_flags = 1;
  382. }
  383. kfree(ref->extent_op);
  384. }
  385. }
  386. /*
  387. * update the reference mod on the head to reflect this new operation
  388. */
  389. existing->ref_mod += update->ref_mod;
  390. }
  391. /*
  392. * helper function to actually insert a head node into the rbtree.
  393. * this does all the dirty work in terms of maintaining the correct
  394. * overall modification count.
  395. */
  396. static noinline void add_delayed_ref_head(struct btrfs_fs_info *fs_info,
  397. struct btrfs_trans_handle *trans,
  398. struct btrfs_delayed_ref_node *ref,
  399. u64 bytenr, u64 num_bytes,
  400. int action, int is_data)
  401. {
  402. struct btrfs_delayed_ref_node *existing;
  403. struct btrfs_delayed_ref_head *head_ref = NULL;
  404. struct btrfs_delayed_ref_root *delayed_refs;
  405. int count_mod = 1;
  406. int must_insert_reserved = 0;
  407. /*
  408. * the head node stores the sum of all the mods, so dropping a ref
  409. * should drop the sum in the head node by one.
  410. */
  411. if (action == BTRFS_UPDATE_DELAYED_HEAD)
  412. count_mod = 0;
  413. else if (action == BTRFS_DROP_DELAYED_REF)
  414. count_mod = -1;
  415. /*
  416. * BTRFS_ADD_DELAYED_EXTENT means that we need to update
  417. * the reserved accounting when the extent is finally added, or
  418. * if a later modification deletes the delayed ref without ever
  419. * inserting the extent into the extent allocation tree.
  420. * ref->must_insert_reserved is the flag used to record
  421. * that accounting mods are required.
  422. *
  423. * Once we record must_insert_reserved, switch the action to
  424. * BTRFS_ADD_DELAYED_REF because other special casing is not required.
  425. */
  426. if (action == BTRFS_ADD_DELAYED_EXTENT)
  427. must_insert_reserved = 1;
  428. else
  429. must_insert_reserved = 0;
  430. delayed_refs = &trans->transaction->delayed_refs;
  431. /* first set the basic ref node struct up */
  432. atomic_set(&ref->refs, 1);
  433. ref->bytenr = bytenr;
  434. ref->num_bytes = num_bytes;
  435. ref->ref_mod = count_mod;
  436. ref->type = 0;
  437. ref->action = 0;
  438. ref->is_head = 1;
  439. ref->in_tree = 1;
  440. ref->seq = 0;
  441. head_ref = btrfs_delayed_node_to_head(ref);
  442. head_ref->must_insert_reserved = must_insert_reserved;
  443. head_ref->is_data = is_data;
  444. INIT_LIST_HEAD(&head_ref->cluster);
  445. mutex_init(&head_ref->mutex);
  446. trace_btrfs_delayed_ref_head(ref, head_ref, action);
  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(head_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. }
  462. /*
  463. * helper to insert a delayed tree ref into the rbtree.
  464. */
  465. static noinline void add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  466. 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. int for_cow)
  471. {
  472. struct btrfs_delayed_ref_node *existing;
  473. struct btrfs_delayed_tree_ref *full_ref;
  474. struct btrfs_delayed_ref_root *delayed_refs;
  475. u64 seq = 0;
  476. if (action == BTRFS_ADD_DELAYED_EXTENT)
  477. action = BTRFS_ADD_DELAYED_REF;
  478. delayed_refs = &trans->transaction->delayed_refs;
  479. /* first set the basic ref node struct up */
  480. atomic_set(&ref->refs, 1);
  481. ref->bytenr = bytenr;
  482. ref->num_bytes = num_bytes;
  483. ref->ref_mod = 1;
  484. ref->action = action;
  485. ref->is_head = 0;
  486. ref->in_tree = 1;
  487. if (need_ref_seq(for_cow, ref_root))
  488. seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  489. ref->seq = seq;
  490. full_ref = btrfs_delayed_node_to_tree_ref(ref);
  491. full_ref->parent = parent;
  492. full_ref->root = ref_root;
  493. if (parent)
  494. ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
  495. else
  496. ref->type = BTRFS_TREE_BLOCK_REF_KEY;
  497. full_ref->level = level;
  498. trace_btrfs_delayed_tree_ref(ref, full_ref, action);
  499. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  500. if (existing) {
  501. update_existing_ref(trans, delayed_refs, existing, ref);
  502. /*
  503. * we've updated the existing ref, free the newly
  504. * allocated ref
  505. */
  506. kfree(full_ref);
  507. } else {
  508. delayed_refs->num_entries++;
  509. trans->delayed_ref_updates++;
  510. }
  511. }
  512. /*
  513. * helper to insert a delayed data ref into the rbtree.
  514. */
  515. static noinline void add_delayed_data_ref(struct btrfs_fs_info *fs_info,
  516. struct btrfs_trans_handle *trans,
  517. struct btrfs_delayed_ref_node *ref,
  518. u64 bytenr, u64 num_bytes, u64 parent,
  519. u64 ref_root, u64 owner, u64 offset,
  520. int action, int for_cow)
  521. {
  522. struct btrfs_delayed_ref_node *existing;
  523. struct btrfs_delayed_data_ref *full_ref;
  524. struct btrfs_delayed_ref_root *delayed_refs;
  525. u64 seq = 0;
  526. if (action == BTRFS_ADD_DELAYED_EXTENT)
  527. action = BTRFS_ADD_DELAYED_REF;
  528. delayed_refs = &trans->transaction->delayed_refs;
  529. /* first set the basic ref node struct up */
  530. atomic_set(&ref->refs, 1);
  531. ref->bytenr = bytenr;
  532. ref->num_bytes = num_bytes;
  533. ref->ref_mod = 1;
  534. ref->action = action;
  535. ref->is_head = 0;
  536. ref->in_tree = 1;
  537. if (need_ref_seq(for_cow, ref_root))
  538. seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  539. ref->seq = seq;
  540. full_ref = btrfs_delayed_node_to_data_ref(ref);
  541. full_ref->parent = parent;
  542. full_ref->root = ref_root;
  543. if (parent)
  544. ref->type = BTRFS_SHARED_DATA_REF_KEY;
  545. else
  546. ref->type = BTRFS_EXTENT_DATA_REF_KEY;
  547. full_ref->objectid = owner;
  548. full_ref->offset = offset;
  549. trace_btrfs_delayed_data_ref(ref, full_ref, action);
  550. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  551. if (existing) {
  552. update_existing_ref(trans, delayed_refs, existing, ref);
  553. /*
  554. * we've updated the existing ref, free the newly
  555. * allocated ref
  556. */
  557. kfree(full_ref);
  558. } else {
  559. delayed_refs->num_entries++;
  560. trans->delayed_ref_updates++;
  561. }
  562. }
  563. /*
  564. * add a delayed tree ref. This does all of the accounting required
  565. * to make sure the delayed ref is eventually processed before this
  566. * transaction commits.
  567. */
  568. int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  569. struct btrfs_trans_handle *trans,
  570. u64 bytenr, u64 num_bytes, u64 parent,
  571. u64 ref_root, int level, int action,
  572. struct btrfs_delayed_extent_op *extent_op,
  573. int for_cow)
  574. {
  575. struct btrfs_delayed_tree_ref *ref;
  576. struct btrfs_delayed_ref_head *head_ref;
  577. struct btrfs_delayed_ref_root *delayed_refs;
  578. BUG_ON(extent_op && extent_op->is_data);
  579. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  580. if (!ref)
  581. return -ENOMEM;
  582. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  583. if (!head_ref) {
  584. kfree(ref);
  585. return -ENOMEM;
  586. }
  587. head_ref->extent_op = extent_op;
  588. delayed_refs = &trans->transaction->delayed_refs;
  589. spin_lock(&delayed_refs->lock);
  590. /*
  591. * insert both the head node and the new ref without dropping
  592. * the spin lock
  593. */
  594. add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  595. num_bytes, action, 0);
  596. add_delayed_tree_ref(fs_info, trans, &ref->node, bytenr,
  597. num_bytes, parent, ref_root, level, action,
  598. for_cow);
  599. if (!need_ref_seq(for_cow, ref_root) &&
  600. waitqueue_active(&fs_info->tree_mod_seq_wait))
  601. wake_up(&fs_info->tree_mod_seq_wait);
  602. spin_unlock(&delayed_refs->lock);
  603. if (need_ref_seq(for_cow, ref_root))
  604. btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
  605. return 0;
  606. }
  607. /*
  608. * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
  609. */
  610. int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
  611. struct btrfs_trans_handle *trans,
  612. u64 bytenr, u64 num_bytes,
  613. u64 parent, u64 ref_root,
  614. u64 owner, u64 offset, int action,
  615. struct btrfs_delayed_extent_op *extent_op,
  616. int for_cow)
  617. {
  618. struct btrfs_delayed_data_ref *ref;
  619. struct btrfs_delayed_ref_head *head_ref;
  620. struct btrfs_delayed_ref_root *delayed_refs;
  621. BUG_ON(extent_op && !extent_op->is_data);
  622. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  623. if (!ref)
  624. return -ENOMEM;
  625. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  626. if (!head_ref) {
  627. kfree(ref);
  628. return -ENOMEM;
  629. }
  630. head_ref->extent_op = extent_op;
  631. delayed_refs = &trans->transaction->delayed_refs;
  632. spin_lock(&delayed_refs->lock);
  633. /*
  634. * insert both the head node and the new ref without dropping
  635. * the spin lock
  636. */
  637. add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  638. num_bytes, action, 1);
  639. add_delayed_data_ref(fs_info, trans, &ref->node, bytenr,
  640. num_bytes, parent, ref_root, owner, offset,
  641. action, for_cow);
  642. if (!need_ref_seq(for_cow, ref_root) &&
  643. waitqueue_active(&fs_info->tree_mod_seq_wait))
  644. wake_up(&fs_info->tree_mod_seq_wait);
  645. spin_unlock(&delayed_refs->lock);
  646. if (need_ref_seq(for_cow, ref_root))
  647. btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
  648. return 0;
  649. }
  650. int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
  651. struct btrfs_trans_handle *trans,
  652. u64 bytenr, u64 num_bytes,
  653. struct btrfs_delayed_extent_op *extent_op)
  654. {
  655. struct btrfs_delayed_ref_head *head_ref;
  656. struct btrfs_delayed_ref_root *delayed_refs;
  657. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  658. if (!head_ref)
  659. return -ENOMEM;
  660. head_ref->extent_op = extent_op;
  661. delayed_refs = &trans->transaction->delayed_refs;
  662. spin_lock(&delayed_refs->lock);
  663. add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  664. num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
  665. extent_op->is_data);
  666. if (waitqueue_active(&fs_info->tree_mod_seq_wait))
  667. wake_up(&fs_info->tree_mod_seq_wait);
  668. spin_unlock(&delayed_refs->lock);
  669. return 0;
  670. }
  671. /*
  672. * this does a simple search for the head node for a given extent.
  673. * It must be called with the delayed ref spinlock held, and it returns
  674. * the head node if any where found, or NULL if not.
  675. */
  676. struct btrfs_delayed_ref_head *
  677. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
  678. {
  679. struct btrfs_delayed_ref_node *ref;
  680. struct btrfs_delayed_ref_root *delayed_refs;
  681. delayed_refs = &trans->transaction->delayed_refs;
  682. ref = find_ref_head(&delayed_refs->root, bytenr, NULL, 0);
  683. if (ref)
  684. return btrfs_delayed_node_to_head(ref);
  685. return NULL;
  686. }