ref-cache.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2008 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 "ctree.h"
  20. #include "ref-cache.h"
  21. #include "transaction.h"
  22. struct btrfs_leaf_ref *btrfs_alloc_leaf_ref(struct btrfs_root *root,
  23. int nr_extents)
  24. {
  25. struct btrfs_leaf_ref *ref;
  26. size_t size = btrfs_leaf_ref_size(nr_extents);
  27. ref = kmalloc(size, GFP_NOFS);
  28. if (ref) {
  29. spin_lock(&root->fs_info->ref_cache_lock);
  30. root->fs_info->total_ref_cache_size += size;
  31. spin_unlock(&root->fs_info->ref_cache_lock);
  32. memset(ref, 0, sizeof(*ref));
  33. atomic_set(&ref->usage, 1);
  34. INIT_LIST_HEAD(&ref->list);
  35. }
  36. return ref;
  37. }
  38. void btrfs_free_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
  39. {
  40. if (!ref)
  41. return;
  42. WARN_ON(atomic_read(&ref->usage) == 0);
  43. if (atomic_dec_and_test(&ref->usage)) {
  44. size_t size = btrfs_leaf_ref_size(ref->nritems);
  45. BUG_ON(ref->in_tree);
  46. kfree(ref);
  47. spin_lock(&root->fs_info->ref_cache_lock);
  48. root->fs_info->total_ref_cache_size -= size;
  49. spin_unlock(&root->fs_info->ref_cache_lock);
  50. }
  51. }
  52. static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
  53. struct rb_node *node)
  54. {
  55. struct rb_node ** p = &root->rb_node;
  56. struct rb_node * parent = NULL;
  57. struct btrfs_leaf_ref *entry;
  58. while(*p) {
  59. parent = *p;
  60. entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
  61. WARN_ON(!entry->in_tree);
  62. if (bytenr < entry->bytenr)
  63. p = &(*p)->rb_left;
  64. else if (bytenr > entry->bytenr)
  65. p = &(*p)->rb_right;
  66. else
  67. return parent;
  68. }
  69. entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
  70. entry->in_tree = 1;
  71. rb_link_node(node, parent, p);
  72. rb_insert_color(node, root);
  73. return NULL;
  74. }
  75. static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
  76. {
  77. struct rb_node * n = root->rb_node;
  78. struct btrfs_leaf_ref *entry;
  79. while(n) {
  80. entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
  81. WARN_ON(!entry->in_tree);
  82. if (bytenr < entry->bytenr)
  83. n = n->rb_left;
  84. else if (bytenr > entry->bytenr)
  85. n = n->rb_right;
  86. else
  87. return n;
  88. }
  89. return NULL;
  90. }
  91. int btrfs_remove_leaf_refs(struct btrfs_root *root, u64 max_root_gen)
  92. {
  93. struct btrfs_leaf_ref *ref = NULL;
  94. struct btrfs_leaf_ref_tree *tree = root->ref_tree;
  95. if (!tree)
  96. return 0;
  97. spin_lock(&tree->lock);
  98. while(!list_empty(&tree->list)) {
  99. ref = list_entry(tree->list.next, struct btrfs_leaf_ref, list);
  100. BUG_ON(!ref->in_tree);
  101. if (ref->root_gen > max_root_gen)
  102. break;
  103. rb_erase(&ref->rb_node, &tree->root);
  104. ref->in_tree = 0;
  105. list_del_init(&ref->list);
  106. spin_unlock(&tree->lock);
  107. btrfs_free_leaf_ref(root, ref);
  108. cond_resched();
  109. spin_lock(&tree->lock);
  110. }
  111. spin_unlock(&tree->lock);
  112. return 0;
  113. }
  114. struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root,
  115. u64 bytenr)
  116. {
  117. struct rb_node *rb;
  118. struct btrfs_leaf_ref *ref = NULL;
  119. struct btrfs_leaf_ref_tree *tree = root->ref_tree;
  120. if (!tree)
  121. return NULL;
  122. spin_lock(&tree->lock);
  123. rb = tree_search(&tree->root, bytenr);
  124. if (rb)
  125. ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
  126. if (ref)
  127. atomic_inc(&ref->usage);
  128. spin_unlock(&tree->lock);
  129. return ref;
  130. }
  131. int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
  132. {
  133. int ret = 0;
  134. struct rb_node *rb;
  135. struct btrfs_leaf_ref_tree *tree = root->ref_tree;
  136. spin_lock(&tree->lock);
  137. rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node);
  138. if (rb) {
  139. ret = -EEXIST;
  140. } else {
  141. atomic_inc(&ref->usage);
  142. list_add_tail(&ref->list, &tree->list);
  143. }
  144. spin_unlock(&tree->lock);
  145. return ret;
  146. }
  147. int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
  148. {
  149. struct btrfs_leaf_ref_tree *tree = root->ref_tree;
  150. BUG_ON(!ref->in_tree);
  151. spin_lock(&tree->lock);
  152. rb_erase(&ref->rb_node, &tree->root);
  153. ref->in_tree = 0;
  154. list_del_init(&ref->list);
  155. spin_unlock(&tree->lock);
  156. btrfs_free_leaf_ref(root, ref);
  157. return 0;
  158. }