xref: /freebsd/sys/compat/linuxkpi/common/src/linux_radix.c (revision ead15282ae00f2639a9b747d3acf62ed391258e5)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2018 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38 
39 #include <linux/slab.h>
40 #include <linux/kernel.h>
41 #include <linux/radix-tree.h>
42 #include <linux/err.h>
43 
44 static MALLOC_DEFINE(M_RADIX, "radix", "Linux radix compat");
45 
46 static inline unsigned long
47 radix_max(struct radix_tree_root *root)
48 {
49 	return ((1UL << (root->height * RADIX_TREE_MAP_SHIFT)) - 1UL);
50 }
51 
52 static inline int
53 radix_pos(long id, int height)
54 {
55 	return (id >> (RADIX_TREE_MAP_SHIFT * height)) & RADIX_TREE_MAP_MASK;
56 }
57 
58 void *
59 radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
60 {
61 	struct radix_tree_node *node;
62 	void *item;
63 	int height;
64 
65 	item = NULL;
66 	node = root->rnode;
67 	height = root->height - 1;
68 	if (index > radix_max(root))
69 		goto out;
70 	while (height && node)
71 		node = node->slots[radix_pos(index, height--)];
72 	if (node)
73 		item = node->slots[radix_pos(index, 0)];
74 
75 out:
76 	return (item);
77 }
78 
79 bool
80 radix_tree_iter_find(struct radix_tree_root *root, struct radix_tree_iter *iter,
81     void ***pppslot)
82 {
83 	struct radix_tree_node *node;
84 	unsigned long index = iter->index;
85 	int height;
86 
87 restart:
88 	node = root->rnode;
89 	if (node == NULL)
90 		return (false);
91 	height = root->height - 1;
92 	if (height == -1 || index > radix_max(root))
93 		return (false);
94 	do {
95 		unsigned long mask = RADIX_TREE_MAP_MASK << (RADIX_TREE_MAP_SHIFT * height);
96 		unsigned long step = 1UL << (RADIX_TREE_MAP_SHIFT * height);
97 		int pos = radix_pos(index, height);
98 		struct radix_tree_node *next;
99 
100 		/* track last slot */
101 		*pppslot = node->slots + pos;
102 
103 		next = node->slots[pos];
104 		if (next == NULL) {
105 			index += step;
106 			if ((index & mask) == 0)
107 				goto restart;
108 		} else {
109 			node = next;
110 			height--;
111 		}
112 	} while (height != -1);
113 	iter->index = index;
114 	return (true);
115 }
116 
117 void *
118 radix_tree_delete(struct radix_tree_root *root, unsigned long index)
119 {
120 	struct radix_tree_node *stack[RADIX_TREE_MAX_HEIGHT];
121 	struct radix_tree_node *node;
122 	void *item;
123 	int height;
124 	int idx;
125 
126 	item = NULL;
127 	node = root->rnode;
128 	height = root->height - 1;
129 	if (index > radix_max(root))
130 		goto out;
131 	/*
132 	 * Find the node and record the path in stack.
133 	 */
134 	while (height && node) {
135 		stack[height] = node;
136 		node = node->slots[radix_pos(index, height--)];
137 	}
138 	idx = radix_pos(index, 0);
139 	if (node)
140 		item = node->slots[idx];
141 	/*
142 	 * If we removed something reduce the height of the tree.
143 	 */
144 	if (item)
145 		for (;;) {
146 			node->slots[idx] = NULL;
147 			node->count--;
148 			if (node->count > 0)
149 				break;
150 			free(node, M_RADIX);
151 			if (node == root->rnode) {
152 				root->rnode = NULL;
153 				root->height = 0;
154 				break;
155 			}
156 			height++;
157 			node = stack[height];
158 			idx = radix_pos(index, height);
159 		}
160 out:
161 	return (item);
162 }
163 
164 int
165 radix_tree_insert(struct radix_tree_root *root, unsigned long index, void *item)
166 {
167 	struct radix_tree_node *node;
168 	struct radix_tree_node *temp[RADIX_TREE_MAX_HEIGHT - 1];
169 	int height;
170 	int idx;
171 
172 	/* bail out upon insertion of a NULL item */
173 	if (item == NULL)
174 		return (-EINVAL);
175 
176 	/* get root node, if any */
177 	node = root->rnode;
178 
179 	/* allocate root node, if any */
180 	if (node == NULL) {
181 		node = malloc(sizeof(*node), M_RADIX, root->gfp_mask | M_ZERO);
182 		if (node == NULL)
183 			return (-ENOMEM);
184 		root->rnode = node;
185 		root->height++;
186 	}
187 
188 	/* expand radix tree as needed */
189 	while (radix_max(root) < index) {
190 
191 		/* check if the radix tree is getting too big */
192 		if (root->height == RADIX_TREE_MAX_HEIGHT)
193 			return (-E2BIG);
194 
195 		/*
196 		 * If the root radix level is not empty, we need to
197 		 * allocate a new radix level:
198 		 */
199 		if (node->count != 0) {
200 			node = malloc(sizeof(*node), M_RADIX, root->gfp_mask | M_ZERO);
201 			if (node == NULL)
202 				return (-ENOMEM);
203 			node->slots[0] = root->rnode;
204 			node->count++;
205 			root->rnode = node;
206 		}
207 		root->height++;
208 	}
209 
210 	/* get radix tree height index */
211 	height = root->height - 1;
212 
213 	/* walk down the tree until the first missing node, if any */
214 	for ( ; height != 0; height--) {
215 		idx = radix_pos(index, height);
216 		if (node->slots[idx] == NULL)
217 			break;
218 		node = node->slots[idx];
219 	}
220 
221 	/* allocate the missing radix levels, if any */
222 	for (idx = 0; idx != height; idx++) {
223 		temp[idx] = malloc(sizeof(*node), M_RADIX,
224 		    root->gfp_mask | M_ZERO);
225 		if (temp[idx] == NULL) {
226 			while(idx--)
227 				free(temp[idx], M_RADIX);
228 			/* Check if we should free the root node as well. */
229 			if (root->rnode->count == 0) {
230 				free(root->rnode, M_RADIX);
231 				root->rnode = NULL;
232 				root->height = 0;
233 			}
234 			return (-ENOMEM);
235 		}
236 	}
237 
238 	/* setup new radix levels, if any */
239 	for ( ; height != 0; height--) {
240 		idx = radix_pos(index, height);
241 		node->slots[idx] = temp[height - 1];
242 		node->count++;
243 		node = node->slots[idx];
244 	}
245 
246 	/*
247 	 * Insert and adjust count if the item does not already exist.
248 	 */
249 	idx = radix_pos(index, 0);
250 	if (node->slots[idx])
251 		return (-EEXIST);
252 	node->slots[idx] = item;
253 	node->count++;
254 
255 	return (0);
256 }
257