delayed-ref.c 26 KB

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