delayed-ref.c 26 KB

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