delayed-ref.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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/sort.h>
  20. #include "ctree.h"
  21. #include "delayed-ref.h"
  22. #include "transaction.h"
  23. /*
  24. * delayed back reference update tracking. For subvolume trees
  25. * we queue up extent allocations and backref maintenance for
  26. * delayed processing. This avoids deep call chains where we
  27. * add extents in the middle of btrfs_search_slot, and it allows
  28. * us to buffer up frequently modified backrefs in an rb tree instead
  29. * of hammering updates on the extent allocation tree.
  30. */
  31. /*
  32. * compare two delayed tree backrefs with same bytenr and type
  33. */
  34. static int comp_tree_refs(struct btrfs_delayed_tree_ref *ref2,
  35. struct btrfs_delayed_tree_ref *ref1)
  36. {
  37. if (ref1->node.type == BTRFS_TREE_BLOCK_REF_KEY) {
  38. if (ref1->root < ref2->root)
  39. return -1;
  40. if (ref1->root > ref2->root)
  41. return 1;
  42. } else {
  43. if (ref1->parent < ref2->parent)
  44. return -1;
  45. if (ref1->parent > ref2->parent)
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. /*
  51. * compare two delayed data backrefs with same bytenr and type
  52. */
  53. static int comp_data_refs(struct btrfs_delayed_data_ref *ref2,
  54. struct btrfs_delayed_data_ref *ref1)
  55. {
  56. if (ref1->node.type == BTRFS_EXTENT_DATA_REF_KEY) {
  57. if (ref1->root < ref2->root)
  58. return -1;
  59. if (ref1->root > ref2->root)
  60. return 1;
  61. if (ref1->objectid < ref2->objectid)
  62. return -1;
  63. if (ref1->objectid > ref2->objectid)
  64. return 1;
  65. if (ref1->offset < ref2->offset)
  66. return -1;
  67. if (ref1->offset > ref2->offset)
  68. return 1;
  69. } else {
  70. if (ref1->parent < ref2->parent)
  71. return -1;
  72. if (ref1->parent > ref2->parent)
  73. return 1;
  74. }
  75. return 0;
  76. }
  77. /*
  78. * entries in the rb tree are ordered by the byte number of the extent,
  79. * type of the delayed backrefs and content of delayed backrefs.
  80. */
  81. static int comp_entry(struct btrfs_delayed_ref_node *ref2,
  82. struct btrfs_delayed_ref_node *ref1)
  83. {
  84. if (ref1->bytenr < ref2->bytenr)
  85. return -1;
  86. if (ref1->bytenr > ref2->bytenr)
  87. return 1;
  88. if (ref1->is_head && ref2->is_head)
  89. return 0;
  90. if (ref2->is_head)
  91. return -1;
  92. if (ref1->is_head)
  93. return 1;
  94. if (ref1->type < ref2->type)
  95. return -1;
  96. if (ref1->type > ref2->type)
  97. return 1;
  98. if (ref1->type == BTRFS_TREE_BLOCK_REF_KEY ||
  99. ref1->type == BTRFS_SHARED_BLOCK_REF_KEY) {
  100. return comp_tree_refs(btrfs_delayed_node_to_tree_ref(ref2),
  101. btrfs_delayed_node_to_tree_ref(ref1));
  102. } else if (ref1->type == BTRFS_EXTENT_DATA_REF_KEY ||
  103. ref1->type == BTRFS_SHARED_DATA_REF_KEY) {
  104. return comp_data_refs(btrfs_delayed_node_to_data_ref(ref2),
  105. btrfs_delayed_node_to_data_ref(ref1));
  106. }
  107. BUG();
  108. return 0;
  109. }
  110. /*
  111. * insert a new ref into the rbtree. This returns any existing refs
  112. * for the same (bytenr,parent) tuple, or NULL if the new node was properly
  113. * inserted.
  114. */
  115. static struct btrfs_delayed_ref_node *tree_insert(struct rb_root *root,
  116. struct rb_node *node)
  117. {
  118. struct rb_node **p = &root->rb_node;
  119. struct rb_node *parent_node = NULL;
  120. struct btrfs_delayed_ref_node *entry;
  121. struct btrfs_delayed_ref_node *ins;
  122. int cmp;
  123. ins = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  124. while (*p) {
  125. parent_node = *p;
  126. entry = rb_entry(parent_node, struct btrfs_delayed_ref_node,
  127. rb_node);
  128. cmp = comp_entry(entry, ins);
  129. if (cmp < 0)
  130. p = &(*p)->rb_left;
  131. else if (cmp > 0)
  132. p = &(*p)->rb_right;
  133. else
  134. return entry;
  135. }
  136. rb_link_node(node, parent_node, p);
  137. rb_insert_color(node, root);
  138. return NULL;
  139. }
  140. /*
  141. * find an head entry based on bytenr. This returns the delayed ref
  142. * head if it was able to find one, or NULL if nothing was in that spot
  143. */
  144. static struct btrfs_delayed_ref_node *find_ref_head(struct rb_root *root,
  145. u64 bytenr,
  146. struct btrfs_delayed_ref_node **last)
  147. {
  148. struct rb_node *n = root->rb_node;
  149. struct btrfs_delayed_ref_node *entry;
  150. int cmp;
  151. while (n) {
  152. entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
  153. WARN_ON(!entry->in_tree);
  154. if (last)
  155. *last = entry;
  156. if (bytenr < entry->bytenr)
  157. cmp = -1;
  158. else if (bytenr > entry->bytenr)
  159. cmp = 1;
  160. else if (!btrfs_delayed_ref_is_head(entry))
  161. cmp = 1;
  162. else
  163. cmp = 0;
  164. if (cmp < 0)
  165. n = n->rb_left;
  166. else if (cmp > 0)
  167. n = n->rb_right;
  168. else
  169. return entry;
  170. }
  171. return NULL;
  172. }
  173. int btrfs_delayed_ref_lock(struct btrfs_trans_handle *trans,
  174. struct btrfs_delayed_ref_head *head)
  175. {
  176. struct btrfs_delayed_ref_root *delayed_refs;
  177. delayed_refs = &trans->transaction->delayed_refs;
  178. assert_spin_locked(&delayed_refs->lock);
  179. if (mutex_trylock(&head->mutex))
  180. return 0;
  181. atomic_inc(&head->node.refs);
  182. spin_unlock(&delayed_refs->lock);
  183. mutex_lock(&head->mutex);
  184. spin_lock(&delayed_refs->lock);
  185. if (!head->node.in_tree) {
  186. mutex_unlock(&head->mutex);
  187. btrfs_put_delayed_ref(&head->node);
  188. return -EAGAIN;
  189. }
  190. btrfs_put_delayed_ref(&head->node);
  191. return 0;
  192. }
  193. int btrfs_find_ref_cluster(struct btrfs_trans_handle *trans,
  194. struct list_head *cluster, u64 start)
  195. {
  196. int count = 0;
  197. struct btrfs_delayed_ref_root *delayed_refs;
  198. struct rb_node *node;
  199. struct btrfs_delayed_ref_node *ref;
  200. struct btrfs_delayed_ref_head *head;
  201. delayed_refs = &trans->transaction->delayed_refs;
  202. if (start == 0) {
  203. node = rb_first(&delayed_refs->root);
  204. } else {
  205. ref = NULL;
  206. find_ref_head(&delayed_refs->root, start, &ref);
  207. if (ref) {
  208. struct btrfs_delayed_ref_node *tmp;
  209. node = rb_prev(&ref->rb_node);
  210. while (node) {
  211. tmp = rb_entry(node,
  212. struct btrfs_delayed_ref_node,
  213. rb_node);
  214. if (tmp->bytenr < start)
  215. break;
  216. ref = tmp;
  217. node = rb_prev(&ref->rb_node);
  218. }
  219. node = &ref->rb_node;
  220. } else
  221. node = rb_first(&delayed_refs->root);
  222. }
  223. again:
  224. while (node && count < 32) {
  225. ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
  226. if (btrfs_delayed_ref_is_head(ref)) {
  227. head = btrfs_delayed_node_to_head(ref);
  228. if (list_empty(&head->cluster)) {
  229. list_add_tail(&head->cluster, cluster);
  230. delayed_refs->run_delayed_start =
  231. head->node.bytenr;
  232. count++;
  233. WARN_ON(delayed_refs->num_heads_ready == 0);
  234. delayed_refs->num_heads_ready--;
  235. } else if (count) {
  236. /* the goal of the clustering is to find extents
  237. * that are likely to end up in the same extent
  238. * leaf on disk. So, we don't want them spread
  239. * all over the tree. Stop now if we've hit
  240. * a head that was already in use
  241. */
  242. break;
  243. }
  244. }
  245. node = rb_next(node);
  246. }
  247. if (count) {
  248. return 0;
  249. } else if (start) {
  250. /*
  251. * we've gone to the end of the rbtree without finding any
  252. * clusters. start from the beginning and try again
  253. */
  254. start = 0;
  255. node = rb_first(&delayed_refs->root);
  256. goto again;
  257. }
  258. return 1;
  259. }
  260. /*
  261. * This checks to see if there are any delayed refs in the
  262. * btree for a given bytenr. It returns one if it finds any
  263. * and zero otherwise.
  264. *
  265. * If it only finds a head node, it returns 0.
  266. *
  267. * The idea is to use this when deciding if you can safely delete an
  268. * extent from the extent allocation tree. There may be a pending
  269. * ref in the rbtree that adds or removes references, so as long as this
  270. * returns one you need to leave the BTRFS_EXTENT_ITEM in the extent
  271. * allocation tree.
  272. */
  273. int btrfs_delayed_ref_pending(struct btrfs_trans_handle *trans, u64 bytenr)
  274. {
  275. struct btrfs_delayed_ref_node *ref;
  276. struct btrfs_delayed_ref_root *delayed_refs;
  277. struct rb_node *prev_node;
  278. int ret = 0;
  279. delayed_refs = &trans->transaction->delayed_refs;
  280. spin_lock(&delayed_refs->lock);
  281. ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
  282. if (ref) {
  283. prev_node = rb_prev(&ref->rb_node);
  284. if (!prev_node)
  285. goto out;
  286. ref = rb_entry(prev_node, struct btrfs_delayed_ref_node,
  287. rb_node);
  288. if (ref->bytenr == bytenr)
  289. ret = 1;
  290. }
  291. out:
  292. spin_unlock(&delayed_refs->lock);
  293. return ret;
  294. }
  295. /*
  296. * helper function to lookup reference count and flags of extent.
  297. *
  298. * the head node for delayed ref is used to store the sum of all the
  299. * reference count modifications queued up in the rbtree. the head
  300. * node may also store the extent flags to set. This way you can check
  301. * to see what the reference count and extent flags would be if all of
  302. * the delayed refs are not processed.
  303. */
  304. int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
  305. struct btrfs_root *root, u64 bytenr,
  306. u64 num_bytes, u64 *refs, u64 *flags)
  307. {
  308. struct btrfs_delayed_ref_node *ref;
  309. struct btrfs_delayed_ref_head *head;
  310. struct btrfs_delayed_ref_root *delayed_refs;
  311. struct btrfs_path *path;
  312. struct btrfs_extent_item *ei;
  313. struct extent_buffer *leaf;
  314. struct btrfs_key key;
  315. u32 item_size;
  316. u64 num_refs;
  317. u64 extent_flags;
  318. int ret;
  319. path = btrfs_alloc_path();
  320. if (!path)
  321. return -ENOMEM;
  322. key.objectid = bytenr;
  323. key.type = BTRFS_EXTENT_ITEM_KEY;
  324. key.offset = num_bytes;
  325. delayed_refs = &trans->transaction->delayed_refs;
  326. again:
  327. ret = btrfs_search_slot(trans, root->fs_info->extent_root,
  328. &key, path, 0, 0);
  329. if (ret < 0)
  330. goto out;
  331. if (ret == 0) {
  332. leaf = path->nodes[0];
  333. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  334. if (item_size >= sizeof(*ei)) {
  335. ei = btrfs_item_ptr(leaf, path->slots[0],
  336. struct btrfs_extent_item);
  337. num_refs = btrfs_extent_refs(leaf, ei);
  338. extent_flags = btrfs_extent_flags(leaf, ei);
  339. } else {
  340. #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
  341. struct btrfs_extent_item_v0 *ei0;
  342. BUG_ON(item_size != sizeof(*ei0));
  343. ei0 = btrfs_item_ptr(leaf, path->slots[0],
  344. struct btrfs_extent_item_v0);
  345. num_refs = btrfs_extent_refs_v0(leaf, ei0);
  346. /* FIXME: this isn't correct for data */
  347. extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  348. #else
  349. BUG();
  350. #endif
  351. }
  352. BUG_ON(num_refs == 0);
  353. } else {
  354. num_refs = 0;
  355. extent_flags = 0;
  356. ret = 0;
  357. }
  358. spin_lock(&delayed_refs->lock);
  359. ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
  360. if (ref) {
  361. head = btrfs_delayed_node_to_head(ref);
  362. if (!mutex_trylock(&head->mutex)) {
  363. atomic_inc(&ref->refs);
  364. spin_unlock(&delayed_refs->lock);
  365. btrfs_release_path(root->fs_info->extent_root, path);
  366. mutex_lock(&head->mutex);
  367. mutex_unlock(&head->mutex);
  368. btrfs_put_delayed_ref(ref);
  369. goto again;
  370. }
  371. if (head->extent_op && head->extent_op->update_flags)
  372. extent_flags |= head->extent_op->flags_to_set;
  373. else
  374. BUG_ON(num_refs == 0);
  375. num_refs += ref->ref_mod;
  376. mutex_unlock(&head->mutex);
  377. }
  378. WARN_ON(num_refs == 0);
  379. if (refs)
  380. *refs = num_refs;
  381. if (flags)
  382. *flags = extent_flags;
  383. out:
  384. spin_unlock(&delayed_refs->lock);
  385. btrfs_free_path(path);
  386. return ret;
  387. }
  388. /*
  389. * helper function to update an extent delayed ref in the
  390. * rbtree. existing and update must both have the same
  391. * bytenr and parent
  392. *
  393. * This may free existing if the update cancels out whatever
  394. * operation it was doing.
  395. */
  396. static noinline void
  397. update_existing_ref(struct btrfs_trans_handle *trans,
  398. struct btrfs_delayed_ref_root *delayed_refs,
  399. struct btrfs_delayed_ref_node *existing,
  400. struct btrfs_delayed_ref_node *update)
  401. {
  402. if (update->action != existing->action) {
  403. /*
  404. * this is effectively undoing either an add or a
  405. * drop. We decrement the ref_mod, and if it goes
  406. * down to zero we just delete the entry without
  407. * every changing the extent allocation tree.
  408. */
  409. existing->ref_mod--;
  410. if (existing->ref_mod == 0) {
  411. rb_erase(&existing->rb_node,
  412. &delayed_refs->root);
  413. existing->in_tree = 0;
  414. btrfs_put_delayed_ref(existing);
  415. delayed_refs->num_entries--;
  416. if (trans->delayed_ref_updates)
  417. trans->delayed_ref_updates--;
  418. } else {
  419. WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
  420. existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
  421. }
  422. } else {
  423. WARN_ON(existing->type == BTRFS_TREE_BLOCK_REF_KEY ||
  424. existing->type == BTRFS_SHARED_BLOCK_REF_KEY);
  425. /*
  426. * the action on the existing ref matches
  427. * the action on the ref we're trying to add.
  428. * Bump the ref_mod by one so the backref that
  429. * is eventually added/removed has the correct
  430. * reference count
  431. */
  432. existing->ref_mod += update->ref_mod;
  433. }
  434. }
  435. /*
  436. * helper function to update the accounting in the head ref
  437. * existing and update must have the same bytenr
  438. */
  439. static noinline void
  440. update_existing_head_ref(struct btrfs_delayed_ref_node *existing,
  441. struct btrfs_delayed_ref_node *update)
  442. {
  443. struct btrfs_delayed_ref_head *existing_ref;
  444. struct btrfs_delayed_ref_head *ref;
  445. existing_ref = btrfs_delayed_node_to_head(existing);
  446. ref = btrfs_delayed_node_to_head(update);
  447. BUG_ON(existing_ref->is_data != ref->is_data);
  448. if (ref->must_insert_reserved) {
  449. /* if the extent was freed and then
  450. * reallocated before the delayed ref
  451. * entries were processed, we can end up
  452. * with an existing head ref without
  453. * the must_insert_reserved flag set.
  454. * Set it again here
  455. */
  456. existing_ref->must_insert_reserved = ref->must_insert_reserved;
  457. /*
  458. * update the num_bytes so we make sure the accounting
  459. * is done correctly
  460. */
  461. existing->num_bytes = update->num_bytes;
  462. }
  463. if (ref->extent_op) {
  464. if (!existing_ref->extent_op) {
  465. existing_ref->extent_op = ref->extent_op;
  466. } else {
  467. if (ref->extent_op->update_key) {
  468. memcpy(&existing_ref->extent_op->key,
  469. &ref->extent_op->key,
  470. sizeof(ref->extent_op->key));
  471. existing_ref->extent_op->update_key = 1;
  472. }
  473. if (ref->extent_op->update_flags) {
  474. existing_ref->extent_op->flags_to_set |=
  475. ref->extent_op->flags_to_set;
  476. existing_ref->extent_op->update_flags = 1;
  477. }
  478. kfree(ref->extent_op);
  479. }
  480. }
  481. /*
  482. * update the reference mod on the head to reflect this new operation
  483. */
  484. existing->ref_mod += update->ref_mod;
  485. }
  486. /*
  487. * helper function to actually insert a head node into the rbtree.
  488. * this does all the dirty work in terms of maintaining the correct
  489. * overall modification count.
  490. */
  491. static noinline int add_delayed_ref_head(struct btrfs_trans_handle *trans,
  492. struct btrfs_delayed_ref_node *ref,
  493. u64 bytenr, u64 num_bytes,
  494. int action, int is_data)
  495. {
  496. struct btrfs_delayed_ref_node *existing;
  497. struct btrfs_delayed_ref_head *head_ref = NULL;
  498. struct btrfs_delayed_ref_root *delayed_refs;
  499. int count_mod = 1;
  500. int must_insert_reserved = 0;
  501. /*
  502. * the head node stores the sum of all the mods, so dropping a ref
  503. * should drop the sum in the head node by one.
  504. */
  505. if (action == BTRFS_UPDATE_DELAYED_HEAD)
  506. count_mod = 0;
  507. else if (action == BTRFS_DROP_DELAYED_REF)
  508. count_mod = -1;
  509. /*
  510. * BTRFS_ADD_DELAYED_EXTENT means that we need to update
  511. * the reserved accounting when the extent is finally added, or
  512. * if a later modification deletes the delayed ref without ever
  513. * inserting the extent into the extent allocation tree.
  514. * ref->must_insert_reserved is the flag used to record
  515. * that accounting mods are required.
  516. *
  517. * Once we record must_insert_reserved, switch the action to
  518. * BTRFS_ADD_DELAYED_REF because other special casing is not required.
  519. */
  520. if (action == BTRFS_ADD_DELAYED_EXTENT)
  521. must_insert_reserved = 1;
  522. else
  523. must_insert_reserved = 0;
  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 = count_mod;
  530. ref->type = 0;
  531. ref->action = 0;
  532. ref->is_head = 1;
  533. ref->in_tree = 1;
  534. head_ref = btrfs_delayed_node_to_head(ref);
  535. head_ref->must_insert_reserved = must_insert_reserved;
  536. head_ref->is_data = is_data;
  537. INIT_LIST_HEAD(&head_ref->cluster);
  538. mutex_init(&head_ref->mutex);
  539. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  540. if (existing) {
  541. update_existing_head_ref(existing, ref);
  542. /*
  543. * we've updated the existing ref, free the newly
  544. * allocated ref
  545. */
  546. kfree(ref);
  547. } else {
  548. delayed_refs->num_heads++;
  549. delayed_refs->num_heads_ready++;
  550. delayed_refs->num_entries++;
  551. trans->delayed_ref_updates++;
  552. }
  553. return 0;
  554. }
  555. /*
  556. * helper to insert a delayed tree ref into the rbtree.
  557. */
  558. static noinline int add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  559. struct btrfs_delayed_ref_node *ref,
  560. u64 bytenr, u64 num_bytes, u64 parent,
  561. u64 ref_root, int level, int action)
  562. {
  563. struct btrfs_delayed_ref_node *existing;
  564. struct btrfs_delayed_tree_ref *full_ref;
  565. struct btrfs_delayed_ref_root *delayed_refs;
  566. if (action == BTRFS_ADD_DELAYED_EXTENT)
  567. action = BTRFS_ADD_DELAYED_REF;
  568. delayed_refs = &trans->transaction->delayed_refs;
  569. /* first set the basic ref node struct up */
  570. atomic_set(&ref->refs, 1);
  571. ref->bytenr = bytenr;
  572. ref->num_bytes = num_bytes;
  573. ref->ref_mod = 1;
  574. ref->action = action;
  575. ref->is_head = 0;
  576. ref->in_tree = 1;
  577. full_ref = btrfs_delayed_node_to_tree_ref(ref);
  578. if (parent) {
  579. full_ref->parent = parent;
  580. ref->type = BTRFS_SHARED_BLOCK_REF_KEY;
  581. } else {
  582. full_ref->root = ref_root;
  583. ref->type = BTRFS_TREE_BLOCK_REF_KEY;
  584. }
  585. full_ref->level = level;
  586. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  587. if (existing) {
  588. update_existing_ref(trans, delayed_refs, existing, ref);
  589. /*
  590. * we've updated the existing ref, free the newly
  591. * allocated ref
  592. */
  593. kfree(ref);
  594. } else {
  595. delayed_refs->num_entries++;
  596. trans->delayed_ref_updates++;
  597. }
  598. return 0;
  599. }
  600. /*
  601. * helper to insert a delayed data ref into the rbtree.
  602. */
  603. static noinline int add_delayed_data_ref(struct btrfs_trans_handle *trans,
  604. struct btrfs_delayed_ref_node *ref,
  605. u64 bytenr, u64 num_bytes, u64 parent,
  606. u64 ref_root, u64 owner, u64 offset,
  607. int action)
  608. {
  609. struct btrfs_delayed_ref_node *existing;
  610. struct btrfs_delayed_data_ref *full_ref;
  611. struct btrfs_delayed_ref_root *delayed_refs;
  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. full_ref = btrfs_delayed_node_to_data_ref(ref);
  624. if (parent) {
  625. full_ref->parent = parent;
  626. ref->type = BTRFS_SHARED_DATA_REF_KEY;
  627. } else {
  628. full_ref->root = ref_root;
  629. ref->type = BTRFS_EXTENT_DATA_REF_KEY;
  630. }
  631. full_ref->objectid = owner;
  632. full_ref->offset = offset;
  633. existing = tree_insert(&delayed_refs->root, &ref->rb_node);
  634. if (existing) {
  635. update_existing_ref(trans, delayed_refs, existing, ref);
  636. /*
  637. * we've updated the existing ref, free the newly
  638. * allocated ref
  639. */
  640. kfree(ref);
  641. } else {
  642. delayed_refs->num_entries++;
  643. trans->delayed_ref_updates++;
  644. }
  645. return 0;
  646. }
  647. /*
  648. * add a delayed tree ref. This does all of the accounting required
  649. * to make sure the delayed ref is eventually processed before this
  650. * transaction commits.
  651. */
  652. int btrfs_add_delayed_tree_ref(struct btrfs_trans_handle *trans,
  653. u64 bytenr, u64 num_bytes, u64 parent,
  654. u64 ref_root, int level, int action,
  655. struct btrfs_delayed_extent_op *extent_op)
  656. {
  657. struct btrfs_delayed_tree_ref *ref;
  658. struct btrfs_delayed_ref_head *head_ref;
  659. struct btrfs_delayed_ref_root *delayed_refs;
  660. int ret;
  661. BUG_ON(extent_op && extent_op->is_data);
  662. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  663. if (!ref)
  664. return -ENOMEM;
  665. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  666. if (!head_ref) {
  667. kfree(ref);
  668. return -ENOMEM;
  669. }
  670. head_ref->extent_op = extent_op;
  671. delayed_refs = &trans->transaction->delayed_refs;
  672. spin_lock(&delayed_refs->lock);
  673. /*
  674. * insert both the head node and the new ref without dropping
  675. * the spin lock
  676. */
  677. ret = add_delayed_ref_head(trans, &head_ref->node, bytenr, num_bytes,
  678. action, 0);
  679. BUG_ON(ret);
  680. ret = add_delayed_tree_ref(trans, &ref->node, bytenr, num_bytes,
  681. parent, ref_root, level, action);
  682. BUG_ON(ret);
  683. spin_unlock(&delayed_refs->lock);
  684. return 0;
  685. }
  686. /*
  687. * add a delayed data ref. it's similar to btrfs_add_delayed_tree_ref.
  688. */
  689. int btrfs_add_delayed_data_ref(struct btrfs_trans_handle *trans,
  690. u64 bytenr, u64 num_bytes,
  691. u64 parent, u64 ref_root,
  692. u64 owner, u64 offset, int action,
  693. struct btrfs_delayed_extent_op *extent_op)
  694. {
  695. struct btrfs_delayed_data_ref *ref;
  696. struct btrfs_delayed_ref_head *head_ref;
  697. struct btrfs_delayed_ref_root *delayed_refs;
  698. int ret;
  699. BUG_ON(extent_op && !extent_op->is_data);
  700. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  701. if (!ref)
  702. return -ENOMEM;
  703. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  704. if (!head_ref) {
  705. kfree(ref);
  706. return -ENOMEM;
  707. }
  708. head_ref->extent_op = extent_op;
  709. delayed_refs = &trans->transaction->delayed_refs;
  710. spin_lock(&delayed_refs->lock);
  711. /*
  712. * insert both the head node and the new ref without dropping
  713. * the spin lock
  714. */
  715. ret = add_delayed_ref_head(trans, &head_ref->node, bytenr, num_bytes,
  716. action, 1);
  717. BUG_ON(ret);
  718. ret = add_delayed_data_ref(trans, &ref->node, bytenr, num_bytes,
  719. parent, ref_root, owner, offset, action);
  720. BUG_ON(ret);
  721. spin_unlock(&delayed_refs->lock);
  722. return 0;
  723. }
  724. int btrfs_add_delayed_extent_op(struct btrfs_trans_handle *trans,
  725. u64 bytenr, u64 num_bytes,
  726. struct btrfs_delayed_extent_op *extent_op)
  727. {
  728. struct btrfs_delayed_ref_head *head_ref;
  729. struct btrfs_delayed_ref_root *delayed_refs;
  730. int ret;
  731. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  732. if (!head_ref)
  733. return -ENOMEM;
  734. head_ref->extent_op = extent_op;
  735. delayed_refs = &trans->transaction->delayed_refs;
  736. spin_lock(&delayed_refs->lock);
  737. ret = add_delayed_ref_head(trans, &head_ref->node, bytenr,
  738. num_bytes, BTRFS_UPDATE_DELAYED_HEAD,
  739. extent_op->is_data);
  740. BUG_ON(ret);
  741. spin_unlock(&delayed_refs->lock);
  742. return 0;
  743. }
  744. /*
  745. * this does a simple search for the head node for a given extent.
  746. * It must be called with the delayed ref spinlock held, and it returns
  747. * the head node if any where found, or NULL if not.
  748. */
  749. struct btrfs_delayed_ref_head *
  750. btrfs_find_delayed_ref_head(struct btrfs_trans_handle *trans, u64 bytenr)
  751. {
  752. struct btrfs_delayed_ref_node *ref;
  753. struct btrfs_delayed_ref_root *delayed_refs;
  754. delayed_refs = &trans->transaction->delayed_refs;
  755. ref = find_ref_head(&delayed_refs->root, bytenr, NULL);
  756. if (ref)
  757. return btrfs_delayed_node_to_head(ref);
  758. return NULL;
  759. }
  760. /*
  761. * add a delayed ref to the tree. This does all of the accounting required
  762. * to make sure the delayed ref is eventually processed before this
  763. * transaction commits.
  764. *
  765. * The main point of this call is to add and remove a backreference in a single
  766. * shot, taking the lock only once, and only searching for the head node once.
  767. *
  768. * It is the same as doing a ref add and delete in two separate calls.
  769. */
  770. #if 0
  771. int btrfs_update_delayed_ref(struct btrfs_trans_handle *trans,
  772. u64 bytenr, u64 num_bytes, u64 orig_parent,
  773. u64 parent, u64 orig_ref_root, u64 ref_root,
  774. u64 orig_ref_generation, u64 ref_generation,
  775. u64 owner_objectid, int pin)
  776. {
  777. struct btrfs_delayed_ref *ref;
  778. struct btrfs_delayed_ref *old_ref;
  779. struct btrfs_delayed_ref_head *head_ref;
  780. struct btrfs_delayed_ref_root *delayed_refs;
  781. int ret;
  782. ref = kmalloc(sizeof(*ref), GFP_NOFS);
  783. if (!ref)
  784. return -ENOMEM;
  785. old_ref = kmalloc(sizeof(*old_ref), GFP_NOFS);
  786. if (!old_ref) {
  787. kfree(ref);
  788. return -ENOMEM;
  789. }
  790. /*
  791. * the parent = 0 case comes from cases where we don't actually
  792. * know the parent yet. It will get updated later via a add/drop
  793. * pair.
  794. */
  795. if (parent == 0)
  796. parent = bytenr;
  797. if (orig_parent == 0)
  798. orig_parent = bytenr;
  799. head_ref = kmalloc(sizeof(*head_ref), GFP_NOFS);
  800. if (!head_ref) {
  801. kfree(ref);
  802. kfree(old_ref);
  803. return -ENOMEM;
  804. }
  805. delayed_refs = &trans->transaction->delayed_refs;
  806. spin_lock(&delayed_refs->lock);
  807. /*
  808. * insert both the head node and the new ref without dropping
  809. * the spin lock
  810. */
  811. ret = __btrfs_add_delayed_ref(trans, &head_ref->node, bytenr, num_bytes,
  812. (u64)-1, 0, 0, 0,
  813. BTRFS_UPDATE_DELAYED_HEAD, 0);
  814. BUG_ON(ret);
  815. ret = __btrfs_add_delayed_ref(trans, &ref->node, bytenr, num_bytes,
  816. parent, ref_root, ref_generation,
  817. owner_objectid, BTRFS_ADD_DELAYED_REF, 0);
  818. BUG_ON(ret);
  819. ret = __btrfs_add_delayed_ref(trans, &old_ref->node, bytenr, num_bytes,
  820. orig_parent, orig_ref_root,
  821. orig_ref_generation, owner_objectid,
  822. BTRFS_DROP_DELAYED_REF, pin);
  823. BUG_ON(ret);
  824. spin_unlock(&delayed_refs->lock);
  825. return 0;
  826. }
  827. #endif