ulist.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2011 STRATO AG
  3. * written by Arne Jansen <sensille@gmx.net>
  4. * Distributed under the GNU GPL license version 2.
  5. */
  6. #include <linux/slab.h>
  7. #include "ulist.h"
  8. #include "ctree.h"
  9. /*
  10. * ulist is a generic data structure to hold a collection of unique u64
  11. * values. The only operations it supports is adding to the list and
  12. * enumerating it.
  13. * It is possible to store an auxiliary value along with the key.
  14. *
  15. * A sample usage for ulists is the enumeration of directed graphs without
  16. * visiting a node twice. The pseudo-code could look like this:
  17. *
  18. * ulist = ulist_alloc();
  19. * ulist_add(ulist, root);
  20. * ULIST_ITER_INIT(&uiter);
  21. *
  22. * while ((elem = ulist_next(ulist, &uiter)) {
  23. * for (all child nodes n in elem)
  24. * ulist_add(ulist, n);
  25. * do something useful with the node;
  26. * }
  27. * ulist_free(ulist);
  28. *
  29. * This assumes the graph nodes are adressable by u64. This stems from the
  30. * usage for tree enumeration in btrfs, where the logical addresses are
  31. * 64 bit.
  32. *
  33. * It is also useful for tree enumeration which could be done elegantly
  34. * recursively, but is not possible due to kernel stack limitations. The
  35. * loop would be similar to the above.
  36. */
  37. /**
  38. * ulist_init - freshly initialize a ulist
  39. * @ulist: the ulist to initialize
  40. *
  41. * Note: don't use this function to init an already used ulist, use
  42. * ulist_reinit instead.
  43. */
  44. void ulist_init(struct ulist *ulist)
  45. {
  46. INIT_LIST_HEAD(&ulist->nodes);
  47. ulist->root = RB_ROOT;
  48. ulist->nnodes = 0;
  49. }
  50. /**
  51. * ulist_fini - free up additionally allocated memory for the ulist
  52. * @ulist: the ulist from which to free the additional memory
  53. *
  54. * This is useful in cases where the base 'struct ulist' has been statically
  55. * allocated.
  56. */
  57. static void ulist_fini(struct ulist *ulist)
  58. {
  59. struct ulist_node *node;
  60. struct ulist_node *next;
  61. list_for_each_entry_safe(node, next, &ulist->nodes, list) {
  62. kfree(node);
  63. }
  64. ulist->root = RB_ROOT;
  65. INIT_LIST_HEAD(&ulist->nodes);
  66. }
  67. /**
  68. * ulist_reinit - prepare a ulist for reuse
  69. * @ulist: ulist to be reused
  70. *
  71. * Free up all additional memory allocated for the list elements and reinit
  72. * the ulist.
  73. */
  74. void ulist_reinit(struct ulist *ulist)
  75. {
  76. ulist_fini(ulist);
  77. ulist_init(ulist);
  78. }
  79. /**
  80. * ulist_alloc - dynamically allocate a ulist
  81. * @gfp_mask: allocation flags to for base allocation
  82. *
  83. * The allocated ulist will be returned in an initialized state.
  84. */
  85. struct ulist *ulist_alloc(gfp_t gfp_mask)
  86. {
  87. struct ulist *ulist = kmalloc(sizeof(*ulist), gfp_mask);
  88. if (!ulist)
  89. return NULL;
  90. ulist_init(ulist);
  91. return ulist;
  92. }
  93. /**
  94. * ulist_free - free dynamically allocated ulist
  95. * @ulist: ulist to free
  96. *
  97. * It is not necessary to call ulist_fini before.
  98. */
  99. void ulist_free(struct ulist *ulist)
  100. {
  101. if (!ulist)
  102. return;
  103. ulist_fini(ulist);
  104. kfree(ulist);
  105. }
  106. static struct ulist_node *ulist_rbtree_search(struct ulist *ulist, u64 val)
  107. {
  108. struct rb_node *n = ulist->root.rb_node;
  109. struct ulist_node *u = NULL;
  110. while (n) {
  111. u = rb_entry(n, struct ulist_node, rb_node);
  112. if (u->val < val)
  113. n = n->rb_right;
  114. else if (u->val > val)
  115. n = n->rb_left;
  116. else
  117. return u;
  118. }
  119. return NULL;
  120. }
  121. static int ulist_rbtree_insert(struct ulist *ulist, struct ulist_node *ins)
  122. {
  123. struct rb_node **p = &ulist->root.rb_node;
  124. struct rb_node *parent = NULL;
  125. struct ulist_node *cur = NULL;
  126. while (*p) {
  127. parent = *p;
  128. cur = rb_entry(parent, struct ulist_node, rb_node);
  129. if (cur->val < ins->val)
  130. p = &(*p)->rb_right;
  131. else if (cur->val > ins->val)
  132. p = &(*p)->rb_left;
  133. else
  134. return -EEXIST;
  135. }
  136. rb_link_node(&ins->rb_node, parent, p);
  137. rb_insert_color(&ins->rb_node, &ulist->root);
  138. return 0;
  139. }
  140. /**
  141. * ulist_add - add an element to the ulist
  142. * @ulist: ulist to add the element to
  143. * @val: value to add to ulist
  144. * @aux: auxiliary value to store along with val
  145. * @gfp_mask: flags to use for allocation
  146. *
  147. * Note: locking must be provided by the caller. In case of rwlocks write
  148. * locking is needed
  149. *
  150. * Add an element to a ulist. The @val will only be added if it doesn't
  151. * already exist. If it is added, the auxiliary value @aux is stored along with
  152. * it. In case @val already exists in the ulist, @aux is ignored, even if
  153. * it differs from the already stored value.
  154. *
  155. * ulist_add returns 0 if @val already exists in ulist and 1 if @val has been
  156. * inserted.
  157. * In case of allocation failure -ENOMEM is returned and the ulist stays
  158. * unaltered.
  159. */
  160. int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask)
  161. {
  162. return ulist_add_merge(ulist, val, aux, NULL, gfp_mask);
  163. }
  164. int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
  165. u64 *old_aux, gfp_t gfp_mask)
  166. {
  167. int ret;
  168. struct ulist_node *node;
  169. node = ulist_rbtree_search(ulist, val);
  170. if (node) {
  171. if (old_aux)
  172. *old_aux = node->aux;
  173. return 0;
  174. }
  175. node = kmalloc(sizeof(*node), gfp_mask);
  176. if (!node)
  177. return -ENOMEM;
  178. node->val = val;
  179. node->aux = aux;
  180. #ifdef CONFIG_BTRFS_DEBUG
  181. node->seqnum = ulist->nnodes;
  182. #endif
  183. ret = ulist_rbtree_insert(ulist, node);
  184. ASSERT(!ret);
  185. list_add_tail(&node->list, &ulist->nodes);
  186. ulist->nnodes++;
  187. return 1;
  188. }
  189. /**
  190. * ulist_next - iterate ulist
  191. * @ulist: ulist to iterate
  192. * @uiter: iterator variable, initialized with ULIST_ITER_INIT(&iterator)
  193. *
  194. * Note: locking must be provided by the caller. In case of rwlocks only read
  195. * locking is needed
  196. *
  197. * This function is used to iterate an ulist.
  198. * It returns the next element from the ulist or %NULL when the
  199. * end is reached. No guarantee is made with respect to the order in which
  200. * the elements are returned. They might neither be returned in order of
  201. * addition nor in ascending order.
  202. * It is allowed to call ulist_add during an enumeration. Newly added items
  203. * are guaranteed to show up in the running enumeration.
  204. */
  205. struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_iterator *uiter)
  206. {
  207. struct ulist_node *node;
  208. if (list_empty(&ulist->nodes))
  209. return NULL;
  210. if (uiter->cur_list && uiter->cur_list->next == &ulist->nodes)
  211. return NULL;
  212. if (uiter->cur_list) {
  213. uiter->cur_list = uiter->cur_list->next;
  214. } else {
  215. uiter->cur_list = ulist->nodes.next;
  216. #ifdef CONFIG_BTRFS_DEBUG
  217. uiter->i = 0;
  218. #endif
  219. }
  220. node = list_entry(uiter->cur_list, struct ulist_node, list);
  221. #ifdef CONFIG_BTRFS_DEBUG
  222. ASSERT(node->seqnum == uiter->i);
  223. ASSERT(uiter->i >= 0 && uiter->i < ulist->nnodes);
  224. uiter->i++;
  225. #endif
  226. return node;
  227. }