delayed-ref.c 26 KB

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