]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
e9416bd3 DC |
2 | /* |
3 | * Copyright (C) 2001 Momchil Velikov | |
4 | * Portions Copyright (C) 2001 Christoph Hellwig | |
5 | * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com> | |
e9416bd3 | 6 | */ |
b28b84ca DW |
7 | #include <stdlib.h> |
8 | #include <string.h> | |
9 | #include <errno.h> | |
10 | #include <stdint.h> | |
6b803e5a | 11 | #include "platform_defs.h" |
e9416bd3 DC |
12 | #include "radix-tree.h" |
13 | ||
14 | #ifndef ARRAY_SIZE | |
15 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
16 | #endif | |
17 | ||
18 | #define RADIX_TREE_MAP_SHIFT 6 | |
19 | #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT) | |
20 | #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1) | |
21 | ||
22 | #ifdef RADIX_TREE_TAGS | |
23 | #define RADIX_TREE_TAG_LONGS \ | |
24 | ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG) | |
25 | #endif | |
26 | ||
27 | struct radix_tree_node { | |
28 | unsigned int count; | |
29 | void *slots[RADIX_TREE_MAP_SIZE]; | |
30 | #ifdef RADIX_TREE_TAGS | |
31 | unsigned long tags[RADIX_TREE_MAX_TAGS][RADIX_TREE_TAG_LONGS]; | |
32 | #endif | |
33 | }; | |
34 | ||
35 | struct radix_tree_path { | |
36 | struct radix_tree_node *node; | |
37 | int offset; | |
38 | }; | |
39 | ||
40 | #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long)) | |
41 | #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2) | |
42 | ||
43 | static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH]; | |
44 | ||
45 | /* | |
46 | * Radix tree node cache. | |
47 | */ | |
48 | ||
b28b84ca | 49 | #define radix_tree_node_alloc(r) ((struct radix_tree_node *) \ |
e9416bd3 | 50 | calloc(1, sizeof(struct radix_tree_node))) |
b28b84ca | 51 | #define radix_tree_node_free(n) free(n) |
e9416bd3 DC |
52 | |
53 | #ifdef RADIX_TREE_TAGS | |
54 | ||
55 | static inline void tag_set(struct radix_tree_node *node, unsigned int tag, | |
56 | int offset) | |
57 | { | |
14f8b681 | 58 | *((uint32_t *)node->tags[tag] + (offset >> 5)) |= (1 << (offset & 31)); |
e9416bd3 DC |
59 | } |
60 | ||
61 | static inline void tag_clear(struct radix_tree_node *node, unsigned int tag, | |
62 | int offset) | |
63 | { | |
b28b84ca DW |
64 | uint32_t *p = (uint32_t*)node->tags[tag] + (offset >> 5); |
65 | uint32_t m = 1 << (offset & 31); | |
e9416bd3 DC |
66 | *p &= ~m; |
67 | } | |
68 | ||
69 | static inline int tag_get(struct radix_tree_node *node, unsigned int tag, | |
70 | int offset) | |
71 | { | |
14f8b681 | 72 | return 1 & (((const uint32_t *)node->tags[tag])[offset >> 5] >> (offset & 31)); |
e9416bd3 DC |
73 | } |
74 | ||
75 | /* | |
76 | * Returns 1 if any slot in the node has this tag set. | |
77 | * Otherwise returns 0. | |
78 | */ | |
79 | static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag) | |
80 | { | |
81 | int idx; | |
82 | for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) { | |
83 | if (node->tags[tag][idx]) | |
84 | return 1; | |
85 | } | |
86 | return 0; | |
87 | } | |
88 | ||
89 | #endif | |
90 | ||
91 | /* | |
92 | * Return the maximum key which can be store into a | |
93 | * radix tree with height HEIGHT. | |
94 | */ | |
95 | static inline unsigned long radix_tree_maxindex(unsigned int height) | |
96 | { | |
97 | return height_to_maxindex[height]; | |
98 | } | |
99 | ||
100 | /* | |
101 | * Extend a radix tree so it can store key @index. | |
102 | */ | |
103 | static int radix_tree_extend(struct radix_tree_root *root, unsigned long index) | |
104 | { | |
105 | struct radix_tree_node *node; | |
106 | unsigned int height; | |
107 | #ifdef RADIX_TREE_TAGS | |
108 | char tags[RADIX_TREE_MAX_TAGS]; | |
109 | int tag; | |
110 | #endif | |
111 | ||
112 | /* Figure out what the height should be. */ | |
113 | height = root->height + 1; | |
114 | while (index > radix_tree_maxindex(height)) | |
115 | height++; | |
116 | ||
117 | if (root->rnode == NULL) { | |
118 | root->height = height; | |
119 | goto out; | |
120 | } | |
121 | ||
122 | #ifdef RADIX_TREE_TAGS | |
123 | /* | |
124 | * Prepare the tag status of the top-level node for propagation | |
125 | * into the newly-pushed top-level node(s) | |
126 | */ | |
127 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { | |
128 | tags[tag] = 0; | |
129 | if (any_tag_set(root->rnode, tag)) | |
130 | tags[tag] = 1; | |
131 | } | |
132 | #endif | |
133 | do { | |
134 | if (!(node = radix_tree_node_alloc(root))) | |
135 | return -ENOMEM; | |
136 | ||
137 | /* Increase the height. */ | |
138 | node->slots[0] = root->rnode; | |
139 | ||
140 | #ifdef RADIX_TREE_TAGS | |
141 | /* Propagate the aggregated tag info into the new root */ | |
142 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { | |
143 | if (tags[tag]) | |
144 | tag_set(node, tag, 0); | |
145 | } | |
146 | #endif | |
147 | node->count = 1; | |
148 | root->rnode = node; | |
149 | root->height++; | |
150 | } while (height > root->height); | |
151 | out: | |
152 | return 0; | |
153 | } | |
154 | ||
155 | /** | |
156 | * radix_tree_insert - insert into a radix tree | |
157 | * @root: radix tree root | |
158 | * @index: index key | |
159 | * @item: item to insert | |
160 | * | |
161 | * Insert an item into the radix tree at position @index. | |
162 | */ | |
163 | int radix_tree_insert(struct radix_tree_root *root, | |
164 | unsigned long index, void *item) | |
165 | { | |
166 | struct radix_tree_node *node = NULL, *slot; | |
167 | unsigned int height, shift; | |
168 | int offset; | |
169 | int error; | |
170 | ||
171 | /* Make sure the tree is high enough. */ | |
172 | if ((!index && !root->rnode) || | |
173 | index > radix_tree_maxindex(root->height)) { | |
174 | error = radix_tree_extend(root, index); | |
175 | if (error) | |
176 | return error; | |
177 | } | |
178 | ||
179 | slot = root->rnode; | |
180 | height = root->height; | |
181 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; | |
182 | ||
183 | offset = 0; /* uninitialised var warning */ | |
184 | do { | |
185 | if (slot == NULL) { | |
186 | /* Have to add a child node. */ | |
187 | if (!(slot = radix_tree_node_alloc(root))) | |
188 | return -ENOMEM; | |
189 | if (node) { | |
190 | node->slots[offset] = slot; | |
191 | node->count++; | |
192 | } else | |
193 | root->rnode = slot; | |
194 | } | |
195 | ||
196 | /* Go a level down */ | |
197 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; | |
198 | node = slot; | |
199 | slot = node->slots[offset]; | |
200 | shift -= RADIX_TREE_MAP_SHIFT; | |
201 | height--; | |
202 | } while (height > 0); | |
203 | ||
204 | if (slot != NULL) | |
205 | return -EEXIST; | |
206 | ||
207 | ASSERT(node); | |
208 | node->count++; | |
209 | node->slots[offset] = item; | |
210 | #ifdef RADIX_TREE_TAGS | |
211 | ASSERT(!tag_get(node, 0, offset)); | |
212 | ASSERT(!tag_get(node, 1, offset)); | |
213 | #endif | |
214 | return 0; | |
215 | } | |
216 | ||
217 | static inline void **__lookup_slot(struct radix_tree_root *root, | |
218 | unsigned long index) | |
219 | { | |
220 | unsigned int height, shift; | |
221 | struct radix_tree_node **slot; | |
222 | ||
223 | height = root->height; | |
224 | if (index > radix_tree_maxindex(height)) | |
225 | return NULL; | |
226 | ||
227 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; | |
228 | slot = &root->rnode; | |
229 | ||
230 | while (height > 0) { | |
231 | if (*slot == NULL) | |
232 | return NULL; | |
233 | ||
234 | slot = (struct radix_tree_node **) | |
235 | ((*slot)->slots + | |
236 | ((index >> shift) & RADIX_TREE_MAP_MASK)); | |
237 | shift -= RADIX_TREE_MAP_SHIFT; | |
238 | height--; | |
239 | } | |
240 | ||
241 | return (void **)slot; | |
242 | } | |
243 | ||
244 | /** | |
245 | * radix_tree_lookup_slot - lookup a slot in a radix tree | |
246 | * @root: radix tree root | |
247 | * @index: index key | |
248 | * | |
249 | * Lookup the slot corresponding to the position @index in the radix tree | |
250 | * @root. This is useful for update-if-exists operations. | |
251 | */ | |
252 | void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index) | |
253 | { | |
254 | return __lookup_slot(root, index); | |
255 | } | |
256 | ||
257 | /** | |
258 | * radix_tree_lookup - perform lookup operation on a radix tree | |
259 | * @root: radix tree root | |
260 | * @index: index key | |
261 | * | |
262 | * Lookup the item at the position @index in the radix tree @root. | |
263 | */ | |
264 | void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index) | |
265 | { | |
266 | void **slot; | |
267 | ||
268 | slot = __lookup_slot(root, index); | |
269 | return slot != NULL ? *slot : NULL; | |
270 | } | |
271 | ||
272 | /** | |
273 | * raid_tree_first_key - find the first index key in the radix tree | |
274 | * @root: radix tree root | |
275 | * @index: where the first index will be placed | |
276 | * | |
277 | * Returns the first entry and index key in the radix tree @root. | |
278 | */ | |
279 | void *radix_tree_lookup_first(struct radix_tree_root *root, unsigned long *index) | |
280 | { | |
281 | unsigned int height, shift; | |
282 | struct radix_tree_node *slot; | |
283 | unsigned long i; | |
284 | ||
285 | height = root->height; | |
286 | *index = 0; | |
287 | if (height == 0) | |
288 | return NULL; | |
289 | ||
290 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; | |
291 | slot = root->rnode; | |
292 | ||
293 | for (; height > 1; height--) { | |
294 | for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) { | |
295 | if (slot->slots[i] != NULL) | |
296 | break; | |
297 | } | |
298 | ASSERT(i < RADIX_TREE_MAP_SIZE); | |
299 | ||
300 | *index |= (i << shift); | |
301 | shift -= RADIX_TREE_MAP_SHIFT; | |
302 | slot = slot->slots[i]; | |
303 | } | |
304 | for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) { | |
305 | if (slot->slots[i] != NULL) { | |
306 | *index |= i; | |
307 | return slot->slots[i]; | |
308 | } | |
309 | } | |
310 | return NULL; | |
311 | } | |
312 | ||
313 | #ifdef RADIX_TREE_TAGS | |
314 | ||
315 | /** | |
316 | * radix_tree_tag_set - set a tag on a radix tree node | |
317 | * @root: radix tree root | |
318 | * @index: index key | |
b28b84ca | 319 | * @tag: tag index |
e9416bd3 DC |
320 | * |
321 | * Set the search tag (which must be < RADIX_TREE_MAX_TAGS) | |
322 | * corresponding to @index in the radix tree. From | |
323 | * the root all the way down to the leaf node. | |
324 | * | |
325 | * Returns the address of the tagged item. Setting a tag on a not-present | |
326 | * item is a bug. | |
327 | */ | |
328 | void *radix_tree_tag_set(struct radix_tree_root *root, | |
329 | unsigned long index, unsigned int tag) | |
330 | { | |
331 | unsigned int height, shift; | |
332 | struct radix_tree_node *slot; | |
333 | ||
334 | height = root->height; | |
335 | if (index > radix_tree_maxindex(height)) | |
336 | return NULL; | |
337 | ||
338 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; | |
339 | slot = root->rnode; | |
340 | ||
341 | while (height > 0) { | |
342 | int offset; | |
343 | ||
344 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; | |
345 | if (!tag_get(slot, tag, offset)) | |
346 | tag_set(slot, tag, offset); | |
347 | slot = slot->slots[offset]; | |
348 | ASSERT(slot != NULL); | |
349 | shift -= RADIX_TREE_MAP_SHIFT; | |
350 | height--; | |
351 | } | |
352 | ||
353 | return slot; | |
354 | } | |
355 | ||
356 | /** | |
357 | * radix_tree_tag_clear - clear a tag on a radix tree node | |
358 | * @root: radix tree root | |
359 | * @index: index key | |
b28b84ca | 360 | * @tag: tag index |
e9416bd3 DC |
361 | * |
362 | * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS) | |
363 | * corresponding to @index in the radix tree. If | |
364 | * this causes the leaf node to have no tags set then clear the tag in the | |
365 | * next-to-leaf node, etc. | |
366 | * | |
367 | * Returns the address of the tagged item on success, else NULL. ie: | |
368 | * has the same return value and semantics as radix_tree_lookup(). | |
369 | */ | |
370 | void *radix_tree_tag_clear(struct radix_tree_root *root, | |
371 | unsigned long index, unsigned int tag) | |
372 | { | |
373 | struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path; | |
374 | struct radix_tree_node *slot; | |
375 | unsigned int height, shift; | |
376 | void *ret = NULL; | |
377 | ||
378 | height = root->height; | |
379 | if (index > radix_tree_maxindex(height)) | |
380 | goto out; | |
381 | ||
382 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; | |
383 | pathp->node = NULL; | |
384 | slot = root->rnode; | |
385 | ||
386 | while (height > 0) { | |
387 | int offset; | |
388 | ||
389 | if (slot == NULL) | |
390 | goto out; | |
391 | ||
392 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; | |
393 | pathp[1].offset = offset; | |
394 | pathp[1].node = slot; | |
395 | slot = slot->slots[offset]; | |
396 | pathp++; | |
397 | shift -= RADIX_TREE_MAP_SHIFT; | |
398 | height--; | |
399 | } | |
400 | ||
401 | ret = slot; | |
402 | if (ret == NULL) | |
403 | goto out; | |
404 | ||
405 | do { | |
406 | if (!tag_get(pathp->node, tag, pathp->offset)) | |
407 | goto out; | |
408 | tag_clear(pathp->node, tag, pathp->offset); | |
409 | if (any_tag_set(pathp->node, tag)) | |
410 | goto out; | |
411 | pathp--; | |
412 | } while (pathp->node); | |
413 | out: | |
414 | return ret; | |
415 | } | |
416 | ||
417 | #endif | |
418 | ||
419 | static unsigned int | |
420 | __lookup(struct radix_tree_root *root, void **results, unsigned long index, | |
421 | unsigned int max_items, unsigned long *next_index) | |
422 | { | |
423 | unsigned int nr_found = 0; | |
424 | unsigned int shift, height; | |
425 | struct radix_tree_node *slot; | |
426 | unsigned long i; | |
427 | ||
428 | height = root->height; | |
429 | if (height == 0) | |
430 | goto out; | |
431 | ||
432 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; | |
433 | slot = root->rnode; | |
434 | ||
435 | for ( ; height > 1; height--) { | |
436 | ||
437 | for (i = (index >> shift) & RADIX_TREE_MAP_MASK ; | |
438 | i < RADIX_TREE_MAP_SIZE; i++) { | |
439 | if (slot->slots[i] != NULL) | |
440 | break; | |
441 | index &= ~((1UL << shift) - 1); | |
442 | index += 1UL << shift; | |
443 | if (index == 0) | |
444 | goto out; /* 32-bit wraparound */ | |
445 | } | |
446 | if (i == RADIX_TREE_MAP_SIZE) | |
447 | goto out; | |
448 | ||
449 | shift -= RADIX_TREE_MAP_SHIFT; | |
450 | slot = slot->slots[i]; | |
451 | } | |
452 | ||
453 | /* Bottom level: grab some items */ | |
454 | for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) { | |
455 | index++; | |
456 | if (slot->slots[i]) { | |
457 | results[nr_found++] = slot->slots[i]; | |
458 | if (nr_found == max_items) | |
459 | goto out; | |
460 | } | |
461 | } | |
462 | out: | |
463 | *next_index = index; | |
464 | return nr_found; | |
465 | } | |
466 | ||
467 | /** | |
468 | * radix_tree_gang_lookup - perform multiple lookup on a radix tree | |
469 | * @root: radix tree root | |
470 | * @results: where the results of the lookup are placed | |
471 | * @first_index: start the lookup from this key | |
472 | * @max_items: place up to this many items at *results | |
473 | * | |
474 | * Performs an index-ascending scan of the tree for present items. Places | |
475 | * them at *@results and returns the number of items which were placed at | |
476 | * *@results. | |
477 | * | |
478 | * The implementation is naive. | |
479 | */ | |
480 | unsigned int | |
481 | radix_tree_gang_lookup(struct radix_tree_root *root, void **results, | |
482 | unsigned long first_index, unsigned int max_items) | |
483 | { | |
484 | const unsigned long max_index = radix_tree_maxindex(root->height); | |
485 | unsigned long cur_index = first_index; | |
486 | unsigned int ret = 0; | |
487 | ||
488 | while (ret < max_items) { | |
489 | unsigned int nr_found; | |
490 | unsigned long next_index; /* Index of next search */ | |
491 | ||
492 | if (cur_index > max_index) | |
493 | break; | |
494 | nr_found = __lookup(root, results + ret, cur_index, | |
495 | max_items - ret, &next_index); | |
496 | ret += nr_found; | |
497 | if (next_index == 0) | |
498 | break; | |
499 | cur_index = next_index; | |
500 | } | |
501 | return ret; | |
502 | } | |
503 | ||
504 | /** | |
505 | * radix_tree_gang_lookup_ex - perform multiple lookup on a radix tree | |
506 | * @root: radix tree root | |
507 | * @results: where the results of the lookup are placed | |
508 | * @first_index: start the lookup from this key | |
509 | * @last_index: don't lookup past this key | |
510 | * @max_items: place up to this many items at *results | |
511 | * | |
512 | * Performs an index-ascending scan of the tree for present items starting | |
513 | * @first_index until @last_index up to as many as @max_items. Places | |
514 | * them at *@results and returns the number of items which were placed | |
515 | * at *@results. | |
516 | * | |
517 | * The implementation is naive. | |
518 | */ | |
519 | unsigned int | |
520 | radix_tree_gang_lookup_ex(struct radix_tree_root *root, void **results, | |
521 | unsigned long first_index, unsigned long last_index, | |
522 | unsigned int max_items) | |
523 | { | |
524 | const unsigned long max_index = radix_tree_maxindex(root->height); | |
525 | unsigned long cur_index = first_index; | |
526 | unsigned int ret = 0; | |
527 | ||
528 | while (ret < max_items && cur_index < last_index) { | |
529 | unsigned int nr_found; | |
530 | unsigned long next_index; /* Index of next search */ | |
531 | ||
532 | if (cur_index > max_index) | |
533 | break; | |
534 | nr_found = __lookup(root, results + ret, cur_index, | |
535 | max_items - ret, &next_index); | |
536 | ret += nr_found; | |
537 | if (next_index == 0) | |
538 | break; | |
539 | cur_index = next_index; | |
540 | } | |
541 | return ret; | |
542 | } | |
543 | ||
544 | #ifdef RADIX_TREE_TAGS | |
545 | ||
546 | static unsigned int | |
547 | __lookup_tag(struct radix_tree_root *root, void **results, unsigned long index, | |
548 | unsigned int max_items, unsigned long *next_index, unsigned int tag) | |
549 | { | |
550 | unsigned int nr_found = 0; | |
551 | unsigned int shift; | |
552 | unsigned int height = root->height; | |
553 | struct radix_tree_node *slot; | |
554 | ||
555 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; | |
556 | slot = root->rnode; | |
557 | ||
558 | while (height > 0) { | |
559 | unsigned long i = (index >> shift) & RADIX_TREE_MAP_MASK; | |
560 | ||
561 | for ( ; i < RADIX_TREE_MAP_SIZE; i++) { | |
562 | if (tag_get(slot, tag, i)) { | |
563 | ASSERT(slot->slots[i] != NULL); | |
564 | break; | |
565 | } | |
566 | index &= ~((1UL << shift) - 1); | |
567 | index += 1UL << shift; | |
568 | if (index == 0) | |
569 | goto out; /* 32-bit wraparound */ | |
570 | } | |
571 | if (i == RADIX_TREE_MAP_SIZE) | |
572 | goto out; | |
573 | height--; | |
574 | if (height == 0) { /* Bottom level: grab some items */ | |
575 | unsigned long j = index & RADIX_TREE_MAP_MASK; | |
576 | ||
577 | for ( ; j < RADIX_TREE_MAP_SIZE; j++) { | |
578 | index++; | |
579 | if (tag_get(slot, tag, j)) { | |
580 | ASSERT(slot->slots[j] != NULL); | |
581 | results[nr_found++] = slot->slots[j]; | |
582 | if (nr_found == max_items) | |
583 | goto out; | |
584 | } | |
585 | } | |
586 | } | |
587 | shift -= RADIX_TREE_MAP_SHIFT; | |
588 | slot = slot->slots[i]; | |
589 | } | |
590 | out: | |
591 | *next_index = index; | |
592 | return nr_found; | |
593 | } | |
594 | ||
595 | /** | |
596 | * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree | |
597 | * based on a tag | |
598 | * @root: radix tree root | |
599 | * @results: where the results of the lookup are placed | |
600 | * @first_index: start the lookup from this key | |
601 | * @max_items: place up to this many items at *results | |
602 | * @tag: the tag index (< RADIX_TREE_MAX_TAGS) | |
603 | * | |
604 | * Performs an index-ascending scan of the tree for present items which | |
605 | * have the tag indexed by @tag set. Places the items at *@results and | |
606 | * returns the number of items which were placed at *@results. | |
607 | */ | |
608 | unsigned int | |
609 | radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results, | |
610 | unsigned long first_index, unsigned int max_items, | |
611 | unsigned int tag) | |
612 | { | |
613 | const unsigned long max_index = radix_tree_maxindex(root->height); | |
614 | unsigned long cur_index = first_index; | |
615 | unsigned int ret = 0; | |
616 | ||
617 | while (ret < max_items) { | |
618 | unsigned int nr_found; | |
619 | unsigned long next_index; /* Index of next search */ | |
620 | ||
621 | if (cur_index > max_index) | |
622 | break; | |
623 | nr_found = __lookup_tag(root, results + ret, cur_index, | |
624 | max_items - ret, &next_index, tag); | |
625 | ret += nr_found; | |
626 | if (next_index == 0) | |
627 | break; | |
628 | cur_index = next_index; | |
629 | } | |
630 | return ret; | |
631 | } | |
632 | ||
633 | #endif | |
634 | ||
635 | /** | |
636 | * radix_tree_shrink - shrink height of a radix tree to minimal | |
637 | * @root radix tree root | |
638 | */ | |
639 | static inline void radix_tree_shrink(struct radix_tree_root *root) | |
640 | { | |
641 | /* try to shrink tree height */ | |
642 | while (root->height > 1 && | |
643 | root->rnode->count == 1 && | |
644 | root->rnode->slots[0]) { | |
645 | struct radix_tree_node *to_free = root->rnode; | |
646 | ||
647 | root->rnode = to_free->slots[0]; | |
648 | root->height--; | |
649 | /* must only free zeroed nodes into the slab */ | |
650 | #ifdef RADIX_TREE_TAGS | |
651 | tag_clear(to_free, 0, 0); | |
652 | tag_clear(to_free, 1, 0); | |
653 | #endif | |
654 | to_free->slots[0] = NULL; | |
655 | to_free->count = 0; | |
656 | radix_tree_node_free(to_free); | |
657 | } | |
658 | } | |
659 | ||
660 | /** | |
661 | * radix_tree_delete - delete an item from a radix tree | |
662 | * @root: radix tree root | |
663 | * @index: index key | |
664 | * | |
665 | * Remove the item at @index from the radix tree rooted at @root. | |
666 | * | |
667 | * Returns the address of the deleted item, or NULL if it was not present. | |
668 | */ | |
669 | void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) | |
670 | { | |
671 | struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path; | |
672 | struct radix_tree_path *orig_pathp; | |
673 | struct radix_tree_node *slot; | |
674 | unsigned int height, shift; | |
675 | void *ret = NULL; | |
676 | #ifdef RADIX_TREE_TAGS | |
677 | char tags[RADIX_TREE_MAX_TAGS]; | |
678 | int nr_cleared_tags; | |
679 | int tag; | |
680 | #endif | |
681 | int offset; | |
682 | ||
683 | height = root->height; | |
684 | if (index > radix_tree_maxindex(height)) | |
685 | goto out; | |
686 | ||
687 | shift = (height - 1) * RADIX_TREE_MAP_SHIFT; | |
688 | pathp->node = NULL; | |
689 | slot = root->rnode; | |
690 | ||
691 | for ( ; height > 0; height--) { | |
692 | if (slot == NULL) | |
693 | goto out; | |
694 | ||
695 | pathp++; | |
696 | offset = (index >> shift) & RADIX_TREE_MAP_MASK; | |
697 | pathp->offset = offset; | |
698 | pathp->node = slot; | |
699 | slot = slot->slots[offset]; | |
700 | shift -= RADIX_TREE_MAP_SHIFT; | |
701 | } | |
702 | ||
703 | ret = slot; | |
704 | if (ret == NULL) | |
705 | goto out; | |
706 | ||
707 | orig_pathp = pathp; | |
708 | ||
709 | #ifdef RADIX_TREE_TAGS | |
710 | /* | |
711 | * Clear all tags associated with the just-deleted item | |
712 | */ | |
713 | nr_cleared_tags = 0; | |
714 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { | |
715 | tags[tag] = 1; | |
716 | if (tag_get(pathp->node, tag, pathp->offset)) { | |
717 | tag_clear(pathp->node, tag, pathp->offset); | |
718 | if (!any_tag_set(pathp->node, tag)) { | |
719 | tags[tag] = 0; | |
720 | nr_cleared_tags++; | |
721 | } | |
722 | } | |
723 | } | |
724 | ||
725 | for (pathp--; nr_cleared_tags && pathp->node; pathp--) { | |
726 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { | |
727 | if (tags[tag]) | |
728 | continue; | |
729 | ||
730 | tag_clear(pathp->node, tag, pathp->offset); | |
731 | if (any_tag_set(pathp->node, tag)) { | |
732 | tags[tag] = 1; | |
733 | nr_cleared_tags--; | |
734 | } | |
735 | } | |
736 | } | |
737 | #endif | |
738 | /* Now free the nodes we do not need anymore */ | |
739 | for (pathp = orig_pathp; pathp->node; pathp--) { | |
740 | pathp->node->slots[pathp->offset] = NULL; | |
741 | pathp->node->count--; | |
742 | ||
743 | if (pathp->node->count) { | |
744 | if (pathp->node == root->rnode) | |
745 | radix_tree_shrink(root); | |
746 | goto out; | |
747 | } | |
748 | ||
749 | /* Node with zero slots in use so free it */ | |
750 | radix_tree_node_free(pathp->node); | |
751 | } | |
752 | root->rnode = NULL; | |
753 | root->height = 0; | |
754 | out: | |
755 | return ret; | |
756 | } | |
757 | ||
758 | #ifdef RADIX_TREE_TAGS | |
759 | /** | |
760 | * radix_tree_tagged - test whether any items in the tree are tagged | |
761 | * @root: radix tree root | |
762 | * @tag: tag to test | |
763 | */ | |
764 | int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag) | |
765 | { | |
b28b84ca DW |
766 | struct radix_tree_node *rnode; |
767 | rnode = root->rnode; | |
768 | if (!rnode) | |
769 | return 0; | |
e9416bd3 DC |
770 | return any_tag_set(rnode, tag); |
771 | } | |
772 | #endif | |
773 | ||
774 | static unsigned long __maxindex(unsigned int height) | |
775 | { | |
d781a1bf ES |
776 | unsigned int width = height * RADIX_TREE_MAP_SHIFT; |
777 | int shift = RADIX_TREE_INDEX_BITS - width; | |
778 | ||
779 | if (shift < 0) | |
780 | return ~0UL; | |
781 | if (shift >= BITS_PER_LONG) | |
782 | return 0UL; | |
783 | return ~0UL >> shift; | |
e9416bd3 DC |
784 | } |
785 | ||
786 | static void radix_tree_init_maxindex(void) | |
787 | { | |
788 | unsigned int i; | |
789 | ||
790 | for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++) | |
791 | height_to_maxindex[i] = __maxindex(i); | |
792 | } | |
793 | ||
794 | void radix_tree_init(void) | |
795 | { | |
796 | radix_tree_init_maxindex(); | |
797 | } |