delayed-ref.c 25 KB

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