delayed-ref.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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->root < ref2->root)
  39. return -1;
  40. if (ref1->root > ref2->root)
  41. return 1;
  42. if (ref1->parent < ref2->parent)
  43. return -1;
  44. if (ref1->parent > ref2->parent)
  45. return 1;
  46. return 0;
  47. }
  48. /*
  49. * compare two delayed data backrefs with same bytenr and type
  50. */
  51. static int comp_data_refs(struct btrfs_delayed_data_ref *ref2,
  52. struct btrfs_delayed_data_ref *ref1)
  53. {
  54. if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
  55. if (ref1->root < ref2->root)
  56. return -1;
  57. if (ref1->root > ref2->root)
  58. return 1;
  59. if (ref1->objectid < ref2->objectid)
  60. return -1;
  61. if (ref1->objectid > ref2->objectid)
  62. return 1;
  63. if (ref1->offset < ref2->offset)
  64. return -1;
  65. if (ref1->offset > ref2->offset)
  66. return 1;
  67. } else {
  68. if (ref1->parent < ref2->parent)
  69. return -1;
  70. if (ref1->parent > ref2->parent)
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. /*
  76. * entries in the rb tree are ordered by the byte number of the extent,
  77. * type of the delayed backrefs and content of delayed backrefs.
  78. */
  79. static int comp_entry(struct btrfs_delayed_ref_node *ref2,
  80. struct btrfs_delayed_ref_node *ref1,
  81. bool compare_seq)
  82. {
  83. if (ref1->bytenr < ref2->bytenr)
  84. return -1;
  85. if (ref1->bytenr > ref2->bytenr)
  86. return 1;
  87. if (ref1->is_head && ref2->is_head)
  88. return 0;
  89. if (ref2->is_head)
  90. return -1;
  91. if (ref1->is_head)
  92. return 1;
  93. if (ref1->type < ref2->type)
  94. return -1;
  95. if (ref1->type > ref2->type)
  96. return 1;
  97. /* merging of sequenced refs is not allowed */
  98. if (compare_seq) {
  99. if (ref1->seq < ref2->seq)
  100. return -1;
  101. if (ref1->seq > ref2->seq)
  102. return 1;
  103. }
  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, 1);
  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. static void inline drop_delayed_ref(struct btrfs_trans_handle *trans,
  219. struct btrfs_delayed_ref_root *delayed_refs,
  220. struct btrfs_delayed_ref_node *ref)
  221. {
  222. rb_erase(&ref->rb_node, &delayed_refs->root);
  223. ref->in_tree = 0;
  224. btrfs_put_delayed_ref(ref);
  225. delayed_refs->num_entries--;
  226. if (trans->delayed_ref_updates)
  227. trans->delayed_ref_updates--;
  228. }
  229. static int merge_ref(struct btrfs_trans_handle *trans,
  230. struct btrfs_delayed_ref_root *delayed_refs,
  231. struct btrfs_delayed_ref_node *ref, u64 seq)
  232. {
  233. struct rb_node *node;
  234. int merged = 0;
  235. int mod = 0;
  236. int done = 0;
  237. node = rb_prev(&ref->rb_node);
  238. while (node) {
  239. struct btrfs_delayed_ref_node *next;
  240. next = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  241. node = rb_prev(node);
  242. if (next->bytenr != ref->bytenr)
  243. break;
  244. if (seq && next->seq >= seq)
  245. break;
  246. if (comp_entry(ref, next, 0))
  247. continue;
  248. if (ref->action == next->action) {
  249. mod = next->ref_mod;
  250. } else {
  251. if (ref->ref_mod < next->ref_mod) {
  252. struct btrfs_delayed_ref_node *tmp;
  253. tmp = ref;
  254. ref = next;
  255. next = tmp;
  256. done = 1;
  257. }
  258. mod = -next->ref_mod;
  259. }
  260. merged++;
  261. drop_delayed_ref(trans, delayed_refs, next);
  262. ref->ref_mod += mod;
  263. if (ref->ref_mod == 0) {
  264. drop_delayed_ref(trans, delayed_refs, ref);
  265. break;
  266. } else {
  267. /*
  268. * You can't have multiples of the same ref on a tree
  269. * block.
  270. */
  271. WARN_ON(ref->type == BTRFS_TREE_BLOCK_REF_KEY ||
  272. ref->type == BTRFS_SHARED_BLOCK_REF_KEY);
  273. }
  274. if (done)
  275. break;
  276. node = rb_prev(&ref->rb_node);
  277. }
  278. return merged;
  279. }
  280. void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
  281. struct btrfs_fs_info *fs_info,
  282. struct btrfs_delayed_ref_root *delayed_refs,
  283. struct btrfs_delayed_ref_head *head)
  284. {
  285. struct rb_node *node;
  286. u64 seq = 0;
  287. spin_lock(&fs_info->tree_mod_seq_lock);
  288. if (!list_empty(&fs_info->tree_mod_seq_list)) {
  289. struct seq_list *elem;
  290. elem = list_first_entry(&fs_info->tree_mod_seq_list,
  291. struct seq_list, list);
  292. seq = elem->seq;
  293. }
  294. spin_unlock(&fs_info->tree_mod_seq_lock);
  295. node = rb_prev(&head->node.rb_node);
  296. while (node) {
  297. struct btrfs_delayed_ref_node *ref;
  298. ref = rb_entry(node, struct btrfs_delayed_ref_node,
  299. rb_node);
  300. if (ref->bytenr != head->node.bytenr)
  301. break;
  302. /* We can't merge refs that are outside of our seq count */
  303. if (seq && ref->seq >= seq)
  304. break;
  305. if (merge_ref(trans, delayed_refs, ref, seq))
  306. node = rb_prev(&head->node.rb_node);
  307. else
  308. node = rb_prev(node);
  309. }
  310. }
  311. int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
  312. struct btrfs_delayed_ref_root *delayed_refs,
  313. u64 seq)
  314. {
  315. struct seq_list *elem;
  316. int ret = 0;
  317. spin_lock(&fs_info->tree_mod_seq_lock);
  318. if (!list_empty(&fs_info->tree_mod_seq_list)) {
  319. elem = list_first_entry(&fs_info->tree_mod_seq_list,
  320. struct seq_list, list);
  321. if (seq >= elem->seq) {
  322. pr_debug("holding back delayed_ref %llu, lowest is "
  323. "%llu (%p)\n", seq, elem->seq, delayed_refs);
  324. ret = 1;
  325. }
  326. }
  327. spin_unlock(&fs_info->tree_mod_seq_lock);
  328. return ret;
  329. }
  330. int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
  331. struct list_head *cluster, u64 start)
  332. {
  333. int count = 0;
  334. struct btrfs_delayed_ref_root *delayed_refs;
  335. struct rb_node *node;
  336. struct btrfs_delayed_ref_node *ref;
  337. struct btrfs_delayed_ref_head *head;
  338. delayed_refs = &trans->transaction->delayed_refs;
  339. if (start == 0) {
  340. node = rb_first(&delayed_refs->root);
  341. } else {
  342. ref = NULL;
  343. find_ref_head(&delayed_refs->root, start + 1, &ref, 1);
  344. if (ref) {
  345. node = &ref->rb_node;
  346. } else
  347. node = rb_first(&delayed_refs->root);
  348. }
  349. again:
  350. while (node && count < 32) {
  351. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  352. if (btrfs_delayed_ref_is_head(ref)) {
  353. head = btrfs_delayed_node_to_head(ref);
  354. if (list_empty(&head->cluster)) {
  355. list_add_tail(&head->cluster, cluster);
  356. delayed_refs->run_delayed_start =
  357. head->node.bytenr;
  358. count++;
  359. WARN_ON(delayed_refs->num_heads_ready == 0);
  360. delayed_refs->num_heads_ready--;
  361. } else if (count) {
  362. /* the goal of the clustering is to find extents
  363. * that are likely to end up in the same extent
  364. * leaf on disk. So, we don't want them spread
  365. * all over the tree. Stop now if we've hit
  366. * a head that was already in use
  367. */
  368. break;
  369. }
  370. }
  371. node = rb_next(node);
  372. }
  373. if (count) {
  374. return 0;
  375. } else if (start) {
  376. /*
  377. * we've gone to the end of the rbtree without finding any
  378. * clusters. start from the beginning and try again
  379. */
  380. start = 0;
  381. node = rb_first(&delayed_refs->root);
  382. goto again;
  383. }
  384. return 1;
  385. }
  386. /*
  387. * helper function to update an extent delayed ref in the
  388. * rbtree. existing and update must both have the same
  389. * bytenr and parent
  390. *
  391. * This may free existing if the update cancels out whatever
  392. * operation it was doing.
  393. */
  394. static noinline void
  395. update_existing_ref(struct btrfs_trans_handle *trans,
  396. struct btrfs_delayed_ref_root *delayed_refs,
  397. struct btrfs_delayed_ref_node *existing,
  398. struct btrfs_delayed_ref_node *update)
  399. {
  400. if (update->action != existing->action) {
  401. /*
  402. * this is effectively undoing either an add or a
  403. * drop. We decrement the ref_mod, and if it goes
  404. * down to zero we just delete the entry without
  405. * every changing the extent allocation tree.
  406. */
  407. existing->ref_mod--;
  408. if (existing->ref_mod == 0)
  409. drop_delayed_ref(trans, delayed_refs, existing);
  410. else
  411. WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
  412. existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
  413. } else {
  414. WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
  415. existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
  416. /*
  417. * the action on the existing ref matches
  418. * the action on the ref we're trying to add.
  419. * Bump the ref_mod by one so the backref that
  420. * is eventually added/removed has the correct
  421. * reference count
  422. */
  423. existing->ref_mod += update->ref_mod;
  424. }
  425. }
  426. /*
  427. * helper function to update the accounting in the head ref
  428. * existing and update must have the same bytenr
  429. */
  430. static noinline void
  431. update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
  432. struct btrfs_delayed_ref_node *update)
  433. {
  434. struct btrfs_delayed_ref_head *existing_ref;
  435. struct btrfs_delayed_ref_head *ref;
  436. existing_ref = btrfs_delayed_node_to_head(existing);
  437. ref = btrfs_delayed_node_to_head(update);
  438. BUG_ON(existing_ref->is_data != ref->is_data);
  439. if (ref->must_insert_reserved) {
  440. /* if the extent was freed and then
  441. * reallocated before the delayed ref
  442. * entries were processed, we can end up
  443. * with an existing head ref without
  444. * the must_insert_reserved flag set.
  445. * Set it again here
  446. */
  447. existing_ref->must_insert_reserved = ref->must_insert_reserved;
  448. /*
  449. * update the num_bytes so we make sure the accounting
  450. * is done correctly
  451. */
  452. existing->num_bytes = update->num_bytes;
  453. }
  454. if (ref->extent_op) {
  455. if (!existing_ref->extent_op) {
  456. existing_ref->extent_op = ref->extent_op;
  457. } else {
  458. if (ref->extent_op->update_key) {
  459. memcpy(&existing_ref->extent_op->key,
  460. &ref->extent_op->key,
  461. sizeof(ref->extent_op->key));
  462. existing_ref->extent_op->update_key = 1;
  463. }
  464. if (ref->extent_op->update_flags) {
  465. existing_ref->extent_op->flags_to_set |=
  466. ref->extent_op->flags_to_set;
  467. existing_ref->extent_op->update_flags = 1;
  468. }
  469. kfree(ref->extent_op);
  470. }
  471. }
  472. /*
  473. * update the reference mod on the head to reflect this new operation
  474. */
  475. existing->ref_mod += update->ref_mod;
  476. }
  477. /*
  478. * helper function to actually insert a head node into the rbtree.
  479. * this does all the dirty work in terms of maintaining the correct
  480. * overall modification count.
  481. */
  482. static noinline void add_delayed_ref_head(struct btrfs_fs_info *fs_info,
  483. struct btrfs_trans_handle *trans,
  484. struct btrfs_delayed_ref_node *ref,
  485. u64 bytenr, u64 num_bytes,
  486. int action, int is_data)
  487. {
  488. struct btrfs_delayed_ref_node *existing;
  489. struct btrfs_delayed_ref_head *head_ref = NULL;
  490. struct btrfs_delayed_ref_root *delayed_refs;
  491. int count_mod = 1;
  492. int must_insert_reserved = 0;
  493. /*
  494. * the head node stores the sum of all the mods, so dropping a ref
  495. * should drop the sum in the head node by one.
  496. */
  497. if (action == BTRFS_UPDATE_DELAYED_HEAD)
  498. count_mod = 0;
  499. else if (action == BTRFS_DROP_DELAYED_REF)
  500. count_mod = -1;
  501. /*
  502. * BTRFS_ADD_DELAYED_EXTENT means that we need to update
  503. * the reserved accounting when the extent is finally added, or
  504. * if a later modification deletes the delayed ref without ever
  505. * inserting the extent into the extent allocation tree.
  506. * ref->must_insert_reserved is the flag used to record
  507. * that accounting mods are required.
  508. *
  509. * Once we record must_insert_reserved, switch the action to
  510. * BTRFS_ADD_DELAYED_REF because other special casing is not required.
  511. */
  512. if (action == BTRFS_ADD_DELAYED_EXTENT)
  513. must_insert_reserved = 1;
  514. else
  515. must_insert_reserved = 0;
  516. delayed_refs = &trans->transaction->delayed_refs;
  517. /* first set the basic ref node struct up */
  518. atomic_set(&ref->refs, 1);
  519. ref->bytenr = bytenr;
  520. ref->num_bytes = num_bytes;
  521. ref->ref_mod = count_mod;
  522. ref->type = 0;
  523. ref->action = 0;
  524. ref->is_head = 1;
  525. ref->in_tree = 1;
  526. ref->seq = 0;
  527. head_ref = btrfs_delayed_node_to_head(ref);
  528. head_ref->must_insert_reserved = must_insert_reserved;
  529. head_ref->is_data = is_data;
  530. INIT_LIST_HEAD(&head_ref->cluster);
  531. mutex_init(&head_ref->mutex);
  532. trace_btrfs_delayed_ref_head(ref, head_ref, action);
  533. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  534. if (existing) {
  535. update_existing_head_ref(existing, ref);
  536. /*
  537. * we've updated the existing ref, free the newly
  538. * allocated ref
  539. */
  540. kfree(head_ref);
  541. } else {
  542. delayed_refs->num_heads++;
  543. delayed_refs->num_heads_ready++;
  544. delayed_refs->num_entries++;
  545. trans->delayed_ref_updates++;
  546. }
  547. }
  548. /*
  549. * helper to insert a delayed tree ref into the rbtree.
  550. */
  551. static noinline void add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  552. struct btrfs_trans_handle *trans,
  553. struct btrfs_delayed_ref_node *ref,
  554. u64 bytenr, u64 num_bytes, u64 parent,
  555. u64 ref_root, int level, int action,
  556. int for_cow)
  557. {
  558. struct btrfs_delayed_ref_node *existing;
  559. struct btrfs_delayed_tree_ref *full_ref;
  560. struct btrfs_delayed_ref_root *delayed_refs;
  561. u64 seq = 0;
  562. if (action == BTRFS_ADD_DELAYED_EXTENT)
  563. action = BTRFS_ADD_DELAYED_REF;
  564. delayed_refs = &trans->transaction->delayed_refs;
  565. /* first set the basic ref node struct up */
  566. atomic_set(&ref->refs, 1);
  567. ref->bytenr = bytenr;
  568. ref->num_bytes = num_bytes;
  569. ref->ref_mod = 1;
  570. ref->action = action;
  571. ref->is_head = 0;
  572. ref->in_tree = 1;
  573. if (need_ref_seq(for_cow, ref_root))
  574. seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  575. ref->seq = seq;
  576. full_ref = btrfs_delayed_node_to_tree_ref(ref);
  577. full_ref->parent = parent;
  578. full_ref->root = ref_root;
  579. if (parent)
  580. ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
  581. else
  582. ref->type = BTRFS_TREE_BLOCK_REF_KEY;
  583. full_ref->level = level;
  584. trace_btrfs_delayed_tree_ref(ref, full_ref, action);
  585. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  586. if (existing) {
  587. update_existing_ref(trans, delayed_refs, existing, ref);
  588. /*
  589. * we've updated the existing ref, free the newly
  590. * allocated ref
  591. */
  592. kfree(full_ref);
  593. } else {
  594. delayed_refs->num_entries++;
  595. trans->delayed_ref_updates++;
  596. }
  597. }
  598. /*
  599. * helper to insert a delayed data ref into the rbtree.
  600. */
  601. static noinline void add_delayed_data_ref(struct btrfs_fs_info *fs_info,
  602. struct btrfs_trans_handle *trans,
  603. struct btrfs_delayed_ref_node *ref,
  604. u64 bytenr, u64 num_bytes, u64 parent,
  605. u64 ref_root, u64 owner, u64 offset,
  606. int action, int for_cow)
  607. {
  608. struct btrfs_delayed_ref_node *existing;
  609. struct btrfs_delayed_data_ref *full_ref;
  610. struct btrfs_delayed_ref_root *delayed_refs;
  611. u64 seq = 0;
  612. if (action == BTRFS_ADD_DELAYED_EXTENT)
  613. action = BTRFS_ADD_DELAYED_REF;
  614. delayed_refs = &trans->transaction->delayed_refs;
  615. /* first set the basic ref node struct up */
  616. atomic_set(&ref->refs, 1);
  617. ref->bytenr = bytenr;
  618. ref->num_bytes = num_bytes;
  619. ref->ref_mod = 1;
  620. ref->action = action;
  621. ref->is_head = 0;
  622. ref->in_tree = 1;
  623. if (need_ref_seq(for_cow, ref_root))
  624. seq = btrfs_get_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
  625. ref->seq = seq;
  626. full_ref = btrfs_delayed_node_to_data_ref(ref);
  627. full_ref->parent = parent;
  628. full_ref->root = ref_root;
  629. if (parent)
  630. ref->type = BTRFS_SHARED_DATA_REF_KEY;
  631. else
  632. ref->type = BTRFS_EXTENT_DATA_REF_KEY;
  633. full_ref->objectid = owner;
  634. full_ref->offset = offset;
  635. trace_btrfs_delayed_data_ref(ref, full_ref, action);
  636. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  637. if (existing) {
  638. update_existing_ref(trans, delayed_refs, existing, ref);
  639. /*
  640. * we've updated the existing ref, free the newly
  641. * allocated ref
  642. */
  643. kfree(full_ref);
  644. } else {
  645. delayed_refs->num_entries++;
  646. trans->delayed_ref_updates++;
  647. }
  648. }
  649. /*
  650. * add a delayed tree ref. This does all of the accounting required
  651. * to make sure the delayed ref is eventually processed before this
  652. * transaction commits.
  653. */
  654. int btrfs_add_delayed_tree_ref(struct btrfs_fs_info *fs_info,
  655. struct btrfs_trans_handle *trans,
  656. u64 bytenr, u64 num_bytes, u64 parent,
  657. u64 ref_root, int level, int action,
  658. struct btrfs_delayed_extent_op *extent_op,
  659. int for_cow)
  660. {
  661. struct btrfs_delayed_tree_ref *ref;
  662. struct btrfs_delayed_ref_head *head_ref;
  663. struct btrfs_delayed_ref_root *delayed_refs;
  664. BUG_ON(extent_op && extent_op->is_data);
  665. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  666. if (!ref)
  667. return -ENOMEM;
  668. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  669. if (!head_ref) {
  670. kfree(ref);
  671. return -ENOMEM;
  672. }
  673. head_ref->extent_op = extent_op;
  674. delayed_refs = &trans->transaction->delayed_refs;
  675. spin_lock(&delayed_refs->lock);
  676. /*
  677. * insert both the head node and the new ref without dropping
  678. * the spin lock
  679. */
  680. add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  681. num_bytes, action, 0);
  682. add_delayed_tree_ref(fs_info, trans, &ref->node, bytenr,
  683. num_bytes, parent, ref_root, level, action,
  684. for_cow);
  685. spin_unlock(&delayed_refs->lock);
  686. if (need_ref_seq(for_cow, ref_root))
  687. btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
  688. return 0;
  689. }
  690. /*
  691. * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
  692. */
  693. int btrfs_add_delayed_data_ref(struct btrfs_fs_info *fs_info,
  694. struct btrfs_trans_handle *trans,
  695. u64 bytenr, u64 num_bytes,
  696. u64 parent, u64 ref_root,
  697. u64 owner, u64 offset, int action,
  698. struct btrfs_delayed_extent_op *extent_op,
  699. int for_cow)
  700. {
  701. struct btrfs_delayed_data_ref *ref;
  702. struct btrfs_delayed_ref_head *head_ref;
  703. struct btrfs_delayed_ref_root *delayed_refs;
  704. BUG_ON(extent_op && !extent_op->is_data);
  705. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  706. if (!ref)
  707. return -ENOMEM;
  708. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  709. if (!head_ref) {
  710. kfree(ref);
  711. return -ENOMEM;
  712. }
  713. head_ref->extent_op = extent_op;
  714. delayed_refs = &trans->transaction->delayed_refs;
  715. spin_lock(&delayed_refs->lock);
  716. /*
  717. * insert both the head node and the new ref without dropping
  718. * the spin lock
  719. */
  720. add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  721. num_bytes, action, 1);
  722. add_delayed_data_ref(fs_info, trans, &ref->node, bytenr,
  723. num_bytes, parent, ref_root, owner, offset,
  724. action, for_cow);
  725. spin_unlock(&delayed_refs->lock);
  726. if (need_ref_seq(for_cow, ref_root))
  727. btrfs_qgroup_record_ref(trans, &ref->node, extent_op);
  728. return 0;
  729. }
  730. int btrfs_add_delayed_extent_op(struct btrfs_fs_info *fs_info,
  731. struct btrfs_trans_handle *trans,
  732. u64 bytenr, u64 num_bytes,
  733. struct btrfs_delayed_extent_op *extent_op)
  734. {
  735. struct btrfs_delayed_ref_head *head_ref;
  736. struct btrfs_delayed_ref_root *delayed_refs;
  737. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  738. if (!head_ref)
  739. return -ENOMEM;
  740. head_ref->extent_op = extent_op;
  741. delayed_refs = &trans->transaction->delayed_refs;
  742. spin_lock(&delayed_refs->lock);
  743. add_delayed_ref_head(fs_info, trans, &head_ref->node, bytenr,
  744. num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
  745. extent_op->is_data);
  746. spin_unlock(&delayed_refs->lock);
  747. return 0;
  748. }
  749. /*
  750. * this does a simple search for the head node for a given extent.
  751. * It must be called with the delayed ref spinlock held, and it returns
  752. * the head node if any where found, or NULL if not.
  753. */
  754. struct btrfs_delayed_ref_head *
  755. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
  756. {
  757. struct btrfs_delayed_ref_node *ref;
  758. struct btrfs_delayed_ref_root *delayed_refs;
  759. delayed_refs = &trans->transaction->delayed_refs;
  760. ref = find_ref_head(&delayed_refs->root, bytenr, NULL, 0);
  761. if (ref)
  762. return btrfs_delayed_node_to_head(ref);
  763. return NULL;
  764. }