delayed-ref.c 26 KB

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