usnic_uiom_interval_tree.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2014, Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. *
  17. */
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/slab.h>
  21. #include <linux/list_sort.h>
  22. #include <linux/interval_tree_generic.h>
  23. #include "usnic_uiom_interval_tree.h"
  24. #define START(node) ((node)->start)
  25. #define LAST(node) ((node)->last)
  26. #define MAKE_NODE(node, start, end, ref_cnt, flags, err, err_out) \
  27. do { \
  28. node = usnic_uiom_interval_node_alloc(start, \
  29. end, ref_cnt, flags); \
  30. if (!node) { \
  31. err = -ENOMEM; \
  32. goto err_out; \
  33. } \
  34. } while (0)
  35. #define MARK_FOR_ADD(node, list) (list_add_tail(&node->link, list))
  36. #define MAKE_NODE_AND_APPEND(node, start, end, ref_cnt, flags, err, \
  37. err_out, list) \
  38. do { \
  39. MAKE_NODE(node, start, end, \
  40. ref_cnt, flags, err, \
  41. err_out); \
  42. MARK_FOR_ADD(node, list); \
  43. } while (0)
  44. #define FLAGS_EQUAL(flags1, flags2, mask) \
  45. (((flags1) & (mask)) == ((flags2) & (mask)))
  46. static struct usnic_uiom_interval_node*
  47. usnic_uiom_interval_node_alloc(long int start, long int last, int ref_cnt,
  48. int flags)
  49. {
  50. struct usnic_uiom_interval_node *interval = kzalloc(sizeof(*interval),
  51. GFP_ATOMIC);
  52. if (!interval)
  53. return NULL;
  54. interval->start = start;
  55. interval->last = last;
  56. interval->flags = flags;
  57. interval->ref_cnt = ref_cnt;
  58. return interval;
  59. }
  60. static int interval_cmp(void *priv, struct list_head *a, struct list_head *b)
  61. {
  62. struct usnic_uiom_interval_node *node_a, *node_b;
  63. node_a = list_entry(a, struct usnic_uiom_interval_node, link);
  64. node_b = list_entry(b, struct usnic_uiom_interval_node, link);
  65. /* long to int */
  66. if (node_a->start < node_b->start)
  67. return -1;
  68. else if (node_a->start > node_b->start)
  69. return 1;
  70. return 0;
  71. }
  72. static void
  73. find_intervals_intersection_sorted(struct rb_root *root, unsigned long start,
  74. unsigned long last,
  75. struct list_head *list)
  76. {
  77. struct usnic_uiom_interval_node *node;
  78. INIT_LIST_HEAD(list);
  79. for (node = usnic_uiom_interval_tree_iter_first(root, start, last);
  80. node;
  81. node = usnic_uiom_interval_tree_iter_next(node, start, last))
  82. list_add_tail(&node->link, list);
  83. list_sort(NULL, list, interval_cmp);
  84. }
  85. int usnic_uiom_get_intervals_diff(unsigned long start, unsigned long last,
  86. int flags, int flag_mask,
  87. struct rb_root *root,
  88. struct list_head *diff_set)
  89. {
  90. struct usnic_uiom_interval_node *interval, *tmp;
  91. int err = 0;
  92. long int pivot = start;
  93. LIST_HEAD(intersection_set);
  94. INIT_LIST_HEAD(diff_set);
  95. find_intervals_intersection_sorted(root, start, last,
  96. &intersection_set);
  97. list_for_each_entry(interval, &intersection_set, link) {
  98. if (pivot < interval->start) {
  99. MAKE_NODE_AND_APPEND(tmp, pivot, interval->start - 1,
  100. 1, flags, err, err_out,
  101. diff_set);
  102. pivot = interval->start;
  103. }
  104. /*
  105. * Invariant: Set [start, pivot] is either in diff_set or root,
  106. * but not in both.
  107. */
  108. if (pivot > interval->last) {
  109. continue;
  110. } else if (pivot <= interval->last &&
  111. FLAGS_EQUAL(interval->flags, flags,
  112. flag_mask)) {
  113. pivot = interval->last + 1;
  114. }
  115. }
  116. if (pivot <= last)
  117. MAKE_NODE_AND_APPEND(tmp, pivot, last, 1, flags, err, err_out,
  118. diff_set);
  119. return 0;
  120. err_out:
  121. list_for_each_entry_safe(interval, tmp, diff_set, link) {
  122. list_del(&interval->link);
  123. kfree(interval);
  124. }
  125. return err;
  126. }
  127. void usnic_uiom_put_interval_set(struct list_head *intervals)
  128. {
  129. struct usnic_uiom_interval_node *interval, *tmp;
  130. list_for_each_entry_safe(interval, tmp, intervals, link)
  131. kfree(interval);
  132. }
  133. int usnic_uiom_insert_interval(struct rb_root *root, unsigned long start,
  134. unsigned long last, int flags)
  135. {
  136. struct usnic_uiom_interval_node *interval, *tmp;
  137. unsigned long istart, ilast;
  138. int iref_cnt, iflags;
  139. unsigned long lpivot = start;
  140. int err = 0;
  141. LIST_HEAD(to_add);
  142. LIST_HEAD(intersection_set);
  143. find_intervals_intersection_sorted(root, start, last,
  144. &intersection_set);
  145. list_for_each_entry(interval, &intersection_set, link) {
  146. /*
  147. * Invariant - lpivot is the left edge of next interval to be
  148. * inserted
  149. */
  150. istart = interval->start;
  151. ilast = interval->last;
  152. iref_cnt = interval->ref_cnt;
  153. iflags = interval->flags;
  154. if (istart < lpivot) {
  155. MAKE_NODE_AND_APPEND(tmp, istart, lpivot - 1, iref_cnt,
  156. iflags, err, err_out, &to_add);
  157. } else if (istart > lpivot) {
  158. MAKE_NODE_AND_APPEND(tmp, lpivot, istart - 1, 1, flags,
  159. err, err_out, &to_add);
  160. lpivot = istart;
  161. } else {
  162. lpivot = istart;
  163. }
  164. if (ilast > last) {
  165. MAKE_NODE_AND_APPEND(tmp, lpivot, last, iref_cnt + 1,
  166. iflags | flags, err, err_out,
  167. &to_add);
  168. MAKE_NODE_AND_APPEND(tmp, last + 1, ilast, iref_cnt,
  169. iflags, err, err_out, &to_add);
  170. } else {
  171. MAKE_NODE_AND_APPEND(tmp, lpivot, ilast, iref_cnt + 1,
  172. iflags | flags, err, err_out,
  173. &to_add);
  174. }
  175. lpivot = ilast + 1;
  176. }
  177. if (lpivot <= last)
  178. MAKE_NODE_AND_APPEND(tmp, lpivot, last, 1, flags, err, err_out,
  179. &to_add);
  180. list_for_each_entry_safe(interval, tmp, &intersection_set, link) {
  181. usnic_uiom_interval_tree_remove(interval, root);
  182. kfree(interval);
  183. }
  184. list_for_each_entry(interval, &to_add, link)
  185. usnic_uiom_interval_tree_insert(interval, root);
  186. return 0;
  187. err_out:
  188. list_for_each_entry_safe(interval, tmp, &to_add, link)
  189. kfree(interval);
  190. return err;
  191. }
  192. void usnic_uiom_remove_interval(struct rb_root *root, unsigned long start,
  193. unsigned long last, struct list_head *removed)
  194. {
  195. struct usnic_uiom_interval_node *interval;
  196. for (interval = usnic_uiom_interval_tree_iter_first(root, start, last);
  197. interval;
  198. interval = usnic_uiom_interval_tree_iter_next(interval,
  199. start,
  200. last)) {
  201. if (--interval->ref_cnt == 0)
  202. list_add_tail(&interval->link, removed);
  203. }
  204. list_for_each_entry(interval, removed, link)
  205. usnic_uiom_interval_tree_remove(interval, root);
  206. }
  207. INTERVAL_TREE_DEFINE(struct usnic_uiom_interval_node, rb,
  208. unsigned long, __subtree_last,
  209. START, LAST, , usnic_uiom_interval_tree)