delayed-ref.c 25 KB

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