delayed-ref.c 27 KB

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