ulist.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 "messages.h"
  8. #include "ulist.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. * Freshly initialize a ulist.
  39. *
  40. * @ulist: the ulist to initialize
  41. *
  42. * Note: don't use this function to init an already used ulist, use
  43. * ulist_reinit instead.
  44. */
  45. void ulist_init(struct ulist *ulist)
  46. {
  47. INIT_LIST_HEAD(&ulist->nodes);
  48. ulist->root = RB_ROOT;
  49. ulist->nnodes = 0;
  50. ulist->prealloc = NULL;
  51. }
  52. /*
  53. * Free up additionally allocated memory for the ulist.
  54. *
  55. * @ulist: the ulist from which to free the additional memory
  56. *
  57. * This is useful in cases where the base 'struct ulist' has been statically
  58. * allocated.
  59. */
  60. void ulist_release(struct ulist *ulist)
  61. {
  62. struct ulist_node *node;
  63. struct ulist_node *next;
  64. list_for_each_entry_safe(node, next, &ulist->nodes, list) {
  65. kfree(node);
  66. }
  67. kfree(ulist->prealloc);
  68. ulist->prealloc = NULL;
  69. ulist->root = RB_ROOT;
  70. INIT_LIST_HEAD(&ulist->nodes);
  71. }
  72. /*
  73. * Prepare a ulist for reuse.
  74. *
  75. * @ulist: ulist to be reused
  76. *
  77. * Free up all additional memory allocated for the list elements and reinit
  78. * the ulist.
  79. */
  80. void ulist_reinit(struct ulist *ulist)
  81. {
  82. ulist_release(ulist);
  83. ulist_init(ulist);
  84. }
  85. /*
  86. * Dynamically allocate a ulist.
  87. *
  88. * @gfp_mask: allocation flags to for base allocation
  89. *
  90. * The allocated ulist will be returned in an initialized state.
  91. */
  92. struct ulist *ulist_alloc(gfp_t gfp_mask)
  93. {
  94. struct ulist *ulist = kmalloc(sizeof(*ulist), gfp_mask);
  95. if (!ulist)
  96. return NULL;
  97. ulist_init(ulist);
  98. return ulist;
  99. }
  100. void ulist_prealloc(struct ulist *ulist, gfp_t gfp_mask)
  101. {
  102. if (!ulist->prealloc)
  103. ulist->prealloc = kzalloc(sizeof(*ulist->prealloc), gfp_mask);
  104. }
  105. /*
  106. * Free dynamically allocated ulist.
  107. *
  108. * @ulist: ulist to free
  109. *
  110. * It is not necessary to call ulist_release before.
  111. */
  112. void ulist_free(struct ulist *ulist)
  113. {
  114. if (!ulist)
  115. return;
  116. ulist_release(ulist);
  117. kfree(ulist);
  118. }
  119. static struct ulist_node *ulist_rbtree_search(struct ulist *ulist, u64 val)
  120. {
  121. struct rb_node *n = ulist->root.rb_node;
  122. struct ulist_node *u = NULL;
  123. while (n) {
  124. u = rb_entry(n, struct ulist_node, rb_node);
  125. if (u->val < val)
  126. n = n->rb_right;
  127. else if (u->val > val)
  128. n = n->rb_left;
  129. else
  130. return u;
  131. }
  132. return NULL;
  133. }
  134. static void ulist_rbtree_erase(struct ulist *ulist, struct ulist_node *node)
  135. {
  136. rb_erase(&node->rb_node, &ulist->root);
  137. list_del(&node->list);
  138. kfree(node);
  139. BUG_ON(ulist->nnodes == 0);
  140. ulist->nnodes--;
  141. }
  142. static int ulist_rbtree_insert(struct ulist *ulist, struct ulist_node *ins)
  143. {
  144. struct rb_node **p = &ulist->root.rb_node;
  145. struct rb_node *parent = NULL;
  146. struct ulist_node *cur = NULL;
  147. while (*p) {
  148. parent = *p;
  149. cur = rb_entry(parent, struct ulist_node, rb_node);
  150. if (cur->val < ins->val)
  151. p = &(*p)->rb_right;
  152. else if (cur->val > ins->val)
  153. p = &(*p)->rb_left;
  154. else
  155. return -EEXIST;
  156. }
  157. rb_link_node(&ins->rb_node, parent, p);
  158. rb_insert_color(&ins->rb_node, &ulist->root);
  159. return 0;
  160. }
  161. /*
  162. * Add an element to the ulist.
  163. *
  164. * @ulist: ulist to add the element to
  165. * @val: value to add to ulist
  166. * @aux: auxiliary value to store along with val
  167. * @gfp_mask: flags to use for allocation
  168. *
  169. * Note: locking must be provided by the caller. In case of rwlocks write
  170. * locking is needed
  171. *
  172. * Add an element to a ulist. The @val will only be added if it doesn't
  173. * already exist. If it is added, the auxiliary value @aux is stored along with
  174. * it. In case @val already exists in the ulist, @aux is ignored, even if
  175. * it differs from the already stored value.
  176. *
  177. * ulist_add returns 0 if @val already exists in ulist and 1 if @val has been
  178. * inserted.
  179. * In case of allocation failure -ENOMEM is returned and the ulist stays
  180. * unaltered.
  181. */
  182. int ulist_add(struct ulist *ulist, u64 val, u64 aux, gfp_t gfp_mask)
  183. {
  184. return ulist_add_merge(ulist, val, aux, NULL, gfp_mask);
  185. }
  186. int ulist_add_merge(struct ulist *ulist, u64 val, u64 aux,
  187. u64 *old_aux, gfp_t gfp_mask)
  188. {
  189. int ret;
  190. struct ulist_node *node;
  191. node = ulist_rbtree_search(ulist, val);
  192. if (node) {
  193. if (old_aux)
  194. *old_aux = node->aux;
  195. return 0;
  196. }
  197. if (ulist->prealloc) {
  198. node = ulist->prealloc;
  199. ulist->prealloc = NULL;
  200. } else {
  201. node = kmalloc(sizeof(*node), gfp_mask);
  202. if (!node)
  203. return -ENOMEM;
  204. }
  205. node->val = val;
  206. node->aux = aux;
  207. ret = ulist_rbtree_insert(ulist, node);
  208. ASSERT(!ret);
  209. list_add_tail(&node->list, &ulist->nodes);
  210. ulist->nnodes++;
  211. return 1;
  212. }
  213. /*
  214. * Delete one node from ulist.
  215. *
  216. * @ulist: ulist to remove node from
  217. * @val: value to delete
  218. * @aux: aux to delete
  219. *
  220. * The deletion will only be done when *BOTH* val and aux matches.
  221. * Return 0 for successful delete.
  222. * Return > 0 for not found.
  223. */
  224. int ulist_del(struct ulist *ulist, u64 val, u64 aux)
  225. {
  226. struct ulist_node *node;
  227. node = ulist_rbtree_search(ulist, val);
  228. /* Not found */
  229. if (!node)
  230. return 1;
  231. if (node->aux != aux)
  232. return 1;
  233. /* Found and delete */
  234. ulist_rbtree_erase(ulist, node);
  235. return 0;
  236. }
  237. /*
  238. * Iterate ulist.
  239. *
  240. * @ulist: ulist to iterate
  241. * @uiter: iterator variable, initialized with ULIST_ITER_INIT(&iterator)
  242. *
  243. * Note: locking must be provided by the caller. In case of rwlocks only read
  244. * locking is needed
  245. *
  246. * This function is used to iterate an ulist.
  247. * It returns the next element from the ulist or %NULL when the
  248. * end is reached. No guarantee is made with respect to the order in which
  249. * the elements are returned. They might neither be returned in order of
  250. * addition nor in ascending order.
  251. * It is allowed to call ulist_add during an enumeration. Newly added items
  252. * are guaranteed to show up in the running enumeration.
  253. */
  254. struct ulist_node *ulist_next(const struct ulist *ulist, struct ulist_iterator *uiter)
  255. {
  256. struct ulist_node *node;
  257. if (list_empty(&ulist->nodes))
  258. return NULL;
  259. if (uiter->cur_list && uiter->cur_list->next == &ulist->nodes)
  260. return NULL;
  261. if (uiter->cur_list) {
  262. uiter->cur_list = uiter->cur_list->next;
  263. } else {
  264. uiter->cur_list = ulist->nodes.next;
  265. }
  266. node = list_entry(uiter->cur_list, struct ulist_node, list);
  267. return node;
  268. }