Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | /* * Copyright (c) 2014, Cisco Systems, Inc. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * BSD license below: * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ #include <linux/init.h> #include <linux/list.h> #include <linux/slab.h> #include <linux/list_sort.h> #include <linux/interval_tree_generic.h> #include "usnic_uiom_interval_tree.h" #define START(node) ((node)->start) #define LAST(node) ((node)->last) #define MAKE_NODE(node, start, end, ref_cnt, flags, err, err_out) \ do { \ node = usnic_uiom_interval_node_alloc(start, \ end, ref_cnt, flags); \ if (!node) { \ err = -ENOMEM; \ goto err_out; \ } \ } while (0) #define MARK_FOR_ADD(node, list) (list_add_tail(&node->link, list)) #define MAKE_NODE_AND_APPEND(node, start, end, ref_cnt, flags, err, \ err_out, list) \ do { \ MAKE_NODE(node, start, end, \ ref_cnt, flags, err, \ err_out); \ MARK_FOR_ADD(node, list); \ } while (0) #define FLAGS_EQUAL(flags1, flags2, mask) \ (((flags1) & (mask)) == ((flags2) & (mask))) static struct usnic_uiom_interval_node* usnic_uiom_interval_node_alloc(long int start, long int last, int ref_cnt, int flags) { struct usnic_uiom_interval_node *interval = kzalloc(sizeof(*interval), GFP_ATOMIC); if (!interval) return NULL; interval->start = start; interval->last = last; interval->flags = flags; interval->ref_cnt = ref_cnt; return interval; } static int interval_cmp(void *priv, const struct list_head *a, const struct list_head *b) { struct usnic_uiom_interval_node *node_a, *node_b; node_a = list_entry(a, struct usnic_uiom_interval_node, link); node_b = list_entry(b, struct usnic_uiom_interval_node, link); /* long to int */ if (node_a->start < node_b->start) return -1; else if (node_a->start > node_b->start) return 1; return 0; } static void find_intervals_intersection_sorted(struct rb_root_cached *root, unsigned long start, unsigned long last, struct list_head *list) { struct usnic_uiom_interval_node *node; INIT_LIST_HEAD(list); for (node = usnic_uiom_interval_tree_iter_first(root, start, last); node; node = usnic_uiom_interval_tree_iter_next(node, start, last)) list_add_tail(&node->link, list); list_sort(NULL, list, interval_cmp); } int usnic_uiom_get_intervals_diff(unsigned long start, unsigned long last, int flags, int flag_mask, struct rb_root_cached *root, struct list_head *diff_set) { struct usnic_uiom_interval_node *interval, *tmp; int err = 0; long int pivot = start; LIST_HEAD(intersection_set); INIT_LIST_HEAD(diff_set); find_intervals_intersection_sorted(root, start, last, &intersection_set); list_for_each_entry(interval, &intersection_set, link) { if (pivot < interval->start) { MAKE_NODE_AND_APPEND(tmp, pivot, interval->start - 1, 1, flags, err, err_out, diff_set); pivot = interval->start; } /* * Invariant: Set [start, pivot] is either in diff_set or root, * but not in both. */ if (pivot > interval->last) { continue; } else if (pivot <= interval->last && FLAGS_EQUAL(interval->flags, flags, flag_mask)) { pivot = interval->last + 1; } } if (pivot <= last) MAKE_NODE_AND_APPEND(tmp, pivot, last, 1, flags, err, err_out, diff_set); return 0; err_out: list_for_each_entry_safe(interval, tmp, diff_set, link) { list_del(&interval->link); kfree(interval); } return err; } void usnic_uiom_put_interval_set(struct list_head *intervals) { struct usnic_uiom_interval_node *interval, *tmp; list_for_each_entry_safe(interval, tmp, intervals, link) kfree(interval); } int usnic_uiom_insert_interval(struct rb_root_cached *root, unsigned long start, unsigned long last, int flags) { struct usnic_uiom_interval_node *interval, *tmp; unsigned long istart, ilast; int iref_cnt, iflags; unsigned long lpivot = start; int err = 0; LIST_HEAD(to_add); LIST_HEAD(intersection_set); find_intervals_intersection_sorted(root, start, last, &intersection_set); list_for_each_entry(interval, &intersection_set, link) { /* * Invariant - lpivot is the left edge of next interval to be * inserted */ istart = interval->start; ilast = interval->last; iref_cnt = interval->ref_cnt; iflags = interval->flags; if (istart < lpivot) { MAKE_NODE_AND_APPEND(tmp, istart, lpivot - 1, iref_cnt, iflags, err, err_out, &to_add); } else if (istart > lpivot) { MAKE_NODE_AND_APPEND(tmp, lpivot, istart - 1, 1, flags, err, err_out, &to_add); lpivot = istart; } else { lpivot = istart; } if (ilast > last) { MAKE_NODE_AND_APPEND(tmp, lpivot, last, iref_cnt + 1, iflags | flags, err, err_out, &to_add); MAKE_NODE_AND_APPEND(tmp, last + 1, ilast, iref_cnt, iflags, err, err_out, &to_add); } else { MAKE_NODE_AND_APPEND(tmp, lpivot, ilast, iref_cnt + 1, iflags | flags, err, err_out, &to_add); } lpivot = ilast + 1; } if (lpivot <= last) MAKE_NODE_AND_APPEND(tmp, lpivot, last, 1, flags, err, err_out, &to_add); list_for_each_entry_safe(interval, tmp, &intersection_set, link) { usnic_uiom_interval_tree_remove(interval, root); kfree(interval); } list_for_each_entry(interval, &to_add, link) usnic_uiom_interval_tree_insert(interval, root); return 0; err_out: list_for_each_entry_safe(interval, tmp, &to_add, link) kfree(interval); return err; } void usnic_uiom_remove_interval(struct rb_root_cached *root, unsigned long start, unsigned long last, struct list_head *removed) { struct usnic_uiom_interval_node *interval; for (interval = usnic_uiom_interval_tree_iter_first(root, start, last); interval; interval = usnic_uiom_interval_tree_iter_next(interval, start, last)) { if (--interval->ref_cnt == 0) list_add_tail(&interval->link, removed); } list_for_each_entry(interval, removed, link) usnic_uiom_interval_tree_remove(interval, root); } INTERVAL_TREE_DEFINE(struct usnic_uiom_interval_node, rb, unsigned long, __subtree_last, START, LAST, , usnic_uiom_interval_tree) |