delayed-ref.c 25 KB

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