ulist.c 7.0 KB

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