ulist.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2011 STRATO AG
  4. * written by Arne Jansen <sensille@gmx.net>
  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 addressable 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_release - 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. void ulist_release(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_release(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_release before.
  98. */
  99. void ulist_free(struct ulist *ulist)
  100. {
  101. if (!ulist)
  102. return;
  103. ulist_release(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 void ulist_rbtree_erase(struct ulist *ulist, struct ulist_node *node)
  122. {
  123. rb_erase(&node->rb_node, &ulist->root);
  124. list_del(&node->list);
  125. kfree(node);
  126. BUG_ON(ulist->nnodes == 0);
  127. ulist->nnodes--;
  128. }
  129. static int ulist_rbtree_insert(struct ulist *ulist, struct ulist_node *ins)
  130. {
  131. struct rb_node **p = &ulist->root.rb_node;
  132. struct rb_node *parent = NULL;
  133. struct ulist_node *cur = NULL;
  134. while (*p) {
  135. parent = *p;
  136. cur = rb_entry(parent, struct ulist_node, rb_node);
  137. if (cur->val < ins->val)
  138. p = &(*p)->rb_right;
  139. else if (cur->val > ins->val)
  140. p = &(*p)->rb_left;
  141. else
  142. return -EEXIST;
  143. }
  144. rb_link_node(&ins->rb_node, parent, p);
  145. rb_insert_color(&ins->rb_node, &ulist->root);
  146. return 0;
  147. }
  148. /**
  149. * ulist_add - add an element to the ulist
  150. * @ulist: ulist to add the element to
  151. * @val: value to add to ulist
  152. * @aux: auxiliary value to store along with val
  153. * @gfp_mask: flags to use for allocation
  154. *
  155. * Note: locking must be provided by the caller. In case of rwlocks write
  156. * locking is needed
  157. *
  158. * Add an element to a ulist. The @val will only be added if it doesn't
  159. * already exist. If it is added, the auxiliary value @aux is stored along with
  160. * it. In case @val already exists in the ulist, @aux is ignored, even if
  161. * it differs from the already stored value.
  162. *
  163. * ulist_add returns 0 if @val already exists in ulist and 1 if @val has been
  164. * inserted.
  165. * In case of allocation failure -ENOMEM is returned and the ulist stays
  166. * unaltered.
  167. */
  168. int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask)
  169. {
  170. return ulist_add_merge(ulist, val, aux, NULL, gfp_mask);
  171. }
  172. int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
  173. u64 *old_aux, gfp_t gfp_mask)
  174. {
  175. int ret;
  176. struct ulist_node *node;
  177. node = ulist_rbtree_search(ulist, val);
  178. if (node) {
  179. if (old_aux)
  180. *old_aux = node->aux;
  181. return 0;
  182. }
  183. node = kmalloc(sizeof(*node), gfp_mask);
  184. if (!node)
  185. return -ENOMEM;
  186. node->val = val;
  187. node->aux = aux;
  188. ret = ulist_rbtree_insert(ulist, node);
  189. ASSERT(!ret);
  190. list_add_tail(&node->list, &ulist->nodes);
  191. ulist->nnodes++;
  192. return 1;
  193. }
  194. /*
  195. * ulist_del - delete one node from ulist
  196. * @ulist: ulist to remove node from
  197. * @val: value to delete
  198. * @aux: aux to delete
  199. *
  200. * The deletion will only be done when *BOTH* val and aux matches.
  201. * Return 0 for successful delete.
  202. * Return > 0 for not found.
  203. */
  204. int ulist_del(struct ulist *ulist, u64 val, u64 aux)
  205. {
  206. struct ulist_node *node;
  207. node = ulist_rbtree_search(ulist, val);
  208. /* Not found */
  209. if (!node)
  210. return 1;
  211. if (node->aux != aux)
  212. return 1;
  213. /* Found and delete */
  214. ulist_rbtree_erase(ulist, node);
  215. return 0;
  216. }
  217. /**
  218. * ulist_next - iterate ulist
  219. * @ulist: ulist to iterate
  220. * @uiter: iterator variable, initialized with ULIST_ITER_INIT(&iterator)
  221. *
  222. * Note: locking must be provided by the caller. In case of rwlocks only read
  223. * locking is needed
  224. *
  225. * This function is used to iterate an ulist.
  226. * It returns the next element from the ulist or %NULL when the
  227. * end is reached. No guarantee is made with respect to the order in which
  228. * the elements are returned. They might neither be returned in order of
  229. * addition nor in ascending order.
  230. * It is allowed to call ulist_add during an enumeration. Newly added items
  231. * are guaranteed to show up in the running enumeration.
  232. */
  233. struct ulist_node *ulist_next(struct ulist *ulist, struct ulist_iterator *uiter)
  234. {
  235. struct ulist_node *node;
  236. if (list_empty(&ulist->nodes))
  237. return NULL;
  238. if (uiter->cur_list && uiter->cur_list->next == &ulist->nodes)
  239. return NULL;
  240. if (uiter->cur_list) {
  241. uiter->cur_list = uiter->cur_list->next;
  242. } else {
  243. uiter->cur_list = ulist->nodes.next;
  244. }
  245. node = list_entry(uiter->cur_list, struct ulist_node, list);
  246. return node;
  247. }