]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/radix-tree.c
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
4 * Copyright (C) 2005 SGI, Christoph Lameter <clameter@sgi.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "platform_defs.h"
23 #include "radix-tree.h"
26 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
29 #define RADIX_TREE_MAP_SHIFT 6
30 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
31 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
33 #ifdef RADIX_TREE_TAGS
34 #define RADIX_TREE_TAG_LONGS \
35 ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG)
38 struct radix_tree_node
{
40 void *slots
[RADIX_TREE_MAP_SIZE
];
41 #ifdef RADIX_TREE_TAGS
42 unsigned long tags
[RADIX_TREE_MAX_TAGS
][RADIX_TREE_TAG_LONGS
];
46 struct radix_tree_path
{
47 struct radix_tree_node
*node
;
51 #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
52 #define RADIX_TREE_MAX_PATH (RADIX_TREE_INDEX_BITS/RADIX_TREE_MAP_SHIFT + 2)
54 static unsigned long height_to_maxindex
[RADIX_TREE_MAX_PATH
];
57 * Radix tree node cache.
60 #define radix_tree_node_alloc(r) ((struct radix_tree_node *) \
61 calloc(1, sizeof(struct radix_tree_node)))
62 #define radix_tree_node_free(n) free(n)
64 #ifdef RADIX_TREE_TAGS
66 static inline void tag_set(struct radix_tree_node
*node
, unsigned int tag
,
69 *((uint32_t *)node
->tags
[tag
] + (offset
>> 5)) |= (1 << (offset
& 31));
72 static inline void tag_clear(struct radix_tree_node
*node
, unsigned int tag
,
75 uint32_t *p
= (uint32_t*)node
->tags
[tag
] + (offset
>> 5);
76 uint32_t m
= 1 << (offset
& 31);
80 static inline int tag_get(struct radix_tree_node
*node
, unsigned int tag
,
83 return 1 & (((const uint32_t *)node
->tags
[tag
])[offset
>> 5] >> (offset
& 31));
87 * Returns 1 if any slot in the node has this tag set.
88 * Otherwise returns 0.
90 static inline int any_tag_set(struct radix_tree_node
*node
, unsigned int tag
)
93 for (idx
= 0; idx
< RADIX_TREE_TAG_LONGS
; idx
++) {
94 if (node
->tags
[tag
][idx
])
103 * Return the maximum key which can be store into a
104 * radix tree with height HEIGHT.
106 static inline unsigned long radix_tree_maxindex(unsigned int height
)
108 return height_to_maxindex
[height
];
112 * Extend a radix tree so it can store key @index.
114 static int radix_tree_extend(struct radix_tree_root
*root
, unsigned long index
)
116 struct radix_tree_node
*node
;
118 #ifdef RADIX_TREE_TAGS
119 char tags
[RADIX_TREE_MAX_TAGS
];
123 /* Figure out what the height should be. */
124 height
= root
->height
+ 1;
125 while (index
> radix_tree_maxindex(height
))
128 if (root
->rnode
== NULL
) {
129 root
->height
= height
;
133 #ifdef RADIX_TREE_TAGS
135 * Prepare the tag status of the top-level node for propagation
136 * into the newly-pushed top-level node(s)
138 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
140 if (any_tag_set(root
->rnode
, tag
))
145 if (!(node
= radix_tree_node_alloc(root
)))
148 /* Increase the height. */
149 node
->slots
[0] = root
->rnode
;
151 #ifdef RADIX_TREE_TAGS
152 /* Propagate the aggregated tag info into the new root */
153 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
155 tag_set(node
, tag
, 0);
161 } while (height
> root
->height
);
167 * radix_tree_insert - insert into a radix tree
168 * @root: radix tree root
170 * @item: item to insert
172 * Insert an item into the radix tree at position @index.
174 int radix_tree_insert(struct radix_tree_root
*root
,
175 unsigned long index
, void *item
)
177 struct radix_tree_node
*node
= NULL
, *slot
;
178 unsigned int height
, shift
;
182 /* Make sure the tree is high enough. */
183 if ((!index
&& !root
->rnode
) ||
184 index
> radix_tree_maxindex(root
->height
)) {
185 error
= radix_tree_extend(root
, index
);
191 height
= root
->height
;
192 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
194 offset
= 0; /* uninitialised var warning */
197 /* Have to add a child node. */
198 if (!(slot
= radix_tree_node_alloc(root
)))
201 node
->slots
[offset
] = slot
;
207 /* Go a level down */
208 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
210 slot
= node
->slots
[offset
];
211 shift
-= RADIX_TREE_MAP_SHIFT
;
213 } while (height
> 0);
220 node
->slots
[offset
] = item
;
221 #ifdef RADIX_TREE_TAGS
222 ASSERT(!tag_get(node
, 0, offset
));
223 ASSERT(!tag_get(node
, 1, offset
));
228 static inline void **__lookup_slot(struct radix_tree_root
*root
,
231 unsigned int height
, shift
;
232 struct radix_tree_node
**slot
;
234 height
= root
->height
;
235 if (index
> radix_tree_maxindex(height
))
238 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
245 slot
= (struct radix_tree_node
**)
247 ((index
>> shift
) & RADIX_TREE_MAP_MASK
));
248 shift
-= RADIX_TREE_MAP_SHIFT
;
252 return (void **)slot
;
256 * radix_tree_lookup_slot - lookup a slot in a radix tree
257 * @root: radix tree root
260 * Lookup the slot corresponding to the position @index in the radix tree
261 * @root. This is useful for update-if-exists operations.
263 void **radix_tree_lookup_slot(struct radix_tree_root
*root
, unsigned long index
)
265 return __lookup_slot(root
, index
);
269 * radix_tree_lookup - perform lookup operation on a radix tree
270 * @root: radix tree root
273 * Lookup the item at the position @index in the radix tree @root.
275 void *radix_tree_lookup(struct radix_tree_root
*root
, unsigned long index
)
279 slot
= __lookup_slot(root
, index
);
280 return slot
!= NULL
? *slot
: NULL
;
284 * raid_tree_first_key - find the first index key in the radix tree
285 * @root: radix tree root
286 * @index: where the first index will be placed
288 * Returns the first entry and index key in the radix tree @root.
290 void *radix_tree_lookup_first(struct radix_tree_root
*root
, unsigned long *index
)
292 unsigned int height
, shift
;
293 struct radix_tree_node
*slot
;
296 height
= root
->height
;
301 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
304 for (; height
> 1; height
--) {
305 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++) {
306 if (slot
->slots
[i
] != NULL
)
309 ASSERT(i
< RADIX_TREE_MAP_SIZE
);
311 *index
|= (i
<< shift
);
312 shift
-= RADIX_TREE_MAP_SHIFT
;
313 slot
= slot
->slots
[i
];
315 for (i
= 0; i
< RADIX_TREE_MAP_SIZE
; i
++) {
316 if (slot
->slots
[i
] != NULL
) {
318 return slot
->slots
[i
];
324 #ifdef RADIX_TREE_TAGS
327 * radix_tree_tag_set - set a tag on a radix tree node
328 * @root: radix tree root
332 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
333 * corresponding to @index in the radix tree. From
334 * the root all the way down to the leaf node.
336 * Returns the address of the tagged item. Setting a tag on a not-present
339 void *radix_tree_tag_set(struct radix_tree_root
*root
,
340 unsigned long index
, unsigned int tag
)
342 unsigned int height
, shift
;
343 struct radix_tree_node
*slot
;
345 height
= root
->height
;
346 if (index
> radix_tree_maxindex(height
))
349 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
355 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
356 if (!tag_get(slot
, tag
, offset
))
357 tag_set(slot
, tag
, offset
);
358 slot
= slot
->slots
[offset
];
359 ASSERT(slot
!= NULL
);
360 shift
-= RADIX_TREE_MAP_SHIFT
;
368 * radix_tree_tag_clear - clear a tag on a radix tree node
369 * @root: radix tree root
373 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
374 * corresponding to @index in the radix tree. If
375 * this causes the leaf node to have no tags set then clear the tag in the
376 * next-to-leaf node, etc.
378 * Returns the address of the tagged item on success, else NULL. ie:
379 * has the same return value and semantics as radix_tree_lookup().
381 void *radix_tree_tag_clear(struct radix_tree_root
*root
,
382 unsigned long index
, unsigned int tag
)
384 struct radix_tree_path path
[RADIX_TREE_MAX_PATH
+ 1], *pathp
= path
;
385 struct radix_tree_node
*slot
;
386 unsigned int height
, shift
;
389 height
= root
->height
;
390 if (index
> radix_tree_maxindex(height
))
393 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
403 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
404 pathp
[1].offset
= offset
;
405 pathp
[1].node
= slot
;
406 slot
= slot
->slots
[offset
];
408 shift
-= RADIX_TREE_MAP_SHIFT
;
417 if (!tag_get(pathp
->node
, tag
, pathp
->offset
))
419 tag_clear(pathp
->node
, tag
, pathp
->offset
);
420 if (any_tag_set(pathp
->node
, tag
))
423 } while (pathp
->node
);
431 __lookup(struct radix_tree_root
*root
, void **results
, unsigned long index
,
432 unsigned int max_items
, unsigned long *next_index
)
434 unsigned int nr_found
= 0;
435 unsigned int shift
, height
;
436 struct radix_tree_node
*slot
;
439 height
= root
->height
;
443 shift
= (height
-1) * RADIX_TREE_MAP_SHIFT
;
446 for ( ; height
> 1; height
--) {
448 for (i
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
449 i
< RADIX_TREE_MAP_SIZE
; i
++) {
450 if (slot
->slots
[i
] != NULL
)
452 index
&= ~((1UL << shift
) - 1);
453 index
+= 1UL << shift
;
455 goto out
; /* 32-bit wraparound */
457 if (i
== RADIX_TREE_MAP_SIZE
)
460 shift
-= RADIX_TREE_MAP_SHIFT
;
461 slot
= slot
->slots
[i
];
464 /* Bottom level: grab some items */
465 for (i
= index
& RADIX_TREE_MAP_MASK
; i
< RADIX_TREE_MAP_SIZE
; i
++) {
467 if (slot
->slots
[i
]) {
468 results
[nr_found
++] = slot
->slots
[i
];
469 if (nr_found
== max_items
)
479 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
480 * @root: radix tree root
481 * @results: where the results of the lookup are placed
482 * @first_index: start the lookup from this key
483 * @max_items: place up to this many items at *results
485 * Performs an index-ascending scan of the tree for present items. Places
486 * them at *@results and returns the number of items which were placed at
489 * The implementation is naive.
492 radix_tree_gang_lookup(struct radix_tree_root
*root
, void **results
,
493 unsigned long first_index
, unsigned int max_items
)
495 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
496 unsigned long cur_index
= first_index
;
497 unsigned int ret
= 0;
499 while (ret
< max_items
) {
500 unsigned int nr_found
;
501 unsigned long next_index
; /* Index of next search */
503 if (cur_index
> max_index
)
505 nr_found
= __lookup(root
, results
+ ret
, cur_index
,
506 max_items
- ret
, &next_index
);
510 cur_index
= next_index
;
516 * radix_tree_gang_lookup_ex - perform multiple lookup on a radix tree
517 * @root: radix tree root
518 * @results: where the results of the lookup are placed
519 * @first_index: start the lookup from this key
520 * @last_index: don't lookup past this key
521 * @max_items: place up to this many items at *results
523 * Performs an index-ascending scan of the tree for present items starting
524 * @first_index until @last_index up to as many as @max_items. Places
525 * them at *@results and returns the number of items which were placed
528 * The implementation is naive.
531 radix_tree_gang_lookup_ex(struct radix_tree_root
*root
, void **results
,
532 unsigned long first_index
, unsigned long last_index
,
533 unsigned int max_items
)
535 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
536 unsigned long cur_index
= first_index
;
537 unsigned int ret
= 0;
539 while (ret
< max_items
&& cur_index
< last_index
) {
540 unsigned int nr_found
;
541 unsigned long next_index
; /* Index of next search */
543 if (cur_index
> max_index
)
545 nr_found
= __lookup(root
, results
+ ret
, cur_index
,
546 max_items
- ret
, &next_index
);
550 cur_index
= next_index
;
555 #ifdef RADIX_TREE_TAGS
558 __lookup_tag(struct radix_tree_root
*root
, void **results
, unsigned long index
,
559 unsigned int max_items
, unsigned long *next_index
, unsigned int tag
)
561 unsigned int nr_found
= 0;
563 unsigned int height
= root
->height
;
564 struct radix_tree_node
*slot
;
566 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
570 unsigned long i
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
572 for ( ; i
< RADIX_TREE_MAP_SIZE
; i
++) {
573 if (tag_get(slot
, tag
, i
)) {
574 ASSERT(slot
->slots
[i
] != NULL
);
577 index
&= ~((1UL << shift
) - 1);
578 index
+= 1UL << shift
;
580 goto out
; /* 32-bit wraparound */
582 if (i
== RADIX_TREE_MAP_SIZE
)
585 if (height
== 0) { /* Bottom level: grab some items */
586 unsigned long j
= index
& RADIX_TREE_MAP_MASK
;
588 for ( ; j
< RADIX_TREE_MAP_SIZE
; j
++) {
590 if (tag_get(slot
, tag
, j
)) {
591 ASSERT(slot
->slots
[j
] != NULL
);
592 results
[nr_found
++] = slot
->slots
[j
];
593 if (nr_found
== max_items
)
598 shift
-= RADIX_TREE_MAP_SHIFT
;
599 slot
= slot
->slots
[i
];
607 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
609 * @root: radix tree root
610 * @results: where the results of the lookup are placed
611 * @first_index: start the lookup from this key
612 * @max_items: place up to this many items at *results
613 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
615 * Performs an index-ascending scan of the tree for present items which
616 * have the tag indexed by @tag set. Places the items at *@results and
617 * returns the number of items which were placed at *@results.
620 radix_tree_gang_lookup_tag(struct radix_tree_root
*root
, void **results
,
621 unsigned long first_index
, unsigned int max_items
,
624 const unsigned long max_index
= radix_tree_maxindex(root
->height
);
625 unsigned long cur_index
= first_index
;
626 unsigned int ret
= 0;
628 while (ret
< max_items
) {
629 unsigned int nr_found
;
630 unsigned long next_index
; /* Index of next search */
632 if (cur_index
> max_index
)
634 nr_found
= __lookup_tag(root
, results
+ ret
, cur_index
,
635 max_items
- ret
, &next_index
, tag
);
639 cur_index
= next_index
;
647 * radix_tree_shrink - shrink height of a radix tree to minimal
648 * @root radix tree root
650 static inline void radix_tree_shrink(struct radix_tree_root
*root
)
652 /* try to shrink tree height */
653 while (root
->height
> 1 &&
654 root
->rnode
->count
== 1 &&
655 root
->rnode
->slots
[0]) {
656 struct radix_tree_node
*to_free
= root
->rnode
;
658 root
->rnode
= to_free
->slots
[0];
660 /* must only free zeroed nodes into the slab */
661 #ifdef RADIX_TREE_TAGS
662 tag_clear(to_free
, 0, 0);
663 tag_clear(to_free
, 1, 0);
665 to_free
->slots
[0] = NULL
;
667 radix_tree_node_free(to_free
);
672 * radix_tree_delete - delete an item from a radix tree
673 * @root: radix tree root
676 * Remove the item at @index from the radix tree rooted at @root.
678 * Returns the address of the deleted item, or NULL if it was not present.
680 void *radix_tree_delete(struct radix_tree_root
*root
, unsigned long index
)
682 struct radix_tree_path path
[RADIX_TREE_MAX_PATH
+ 1], *pathp
= path
;
683 struct radix_tree_path
*orig_pathp
;
684 struct radix_tree_node
*slot
;
685 unsigned int height
, shift
;
687 #ifdef RADIX_TREE_TAGS
688 char tags
[RADIX_TREE_MAX_TAGS
];
694 height
= root
->height
;
695 if (index
> radix_tree_maxindex(height
))
698 shift
= (height
- 1) * RADIX_TREE_MAP_SHIFT
;
702 for ( ; height
> 0; height
--) {
707 offset
= (index
>> shift
) & RADIX_TREE_MAP_MASK
;
708 pathp
->offset
= offset
;
710 slot
= slot
->slots
[offset
];
711 shift
-= RADIX_TREE_MAP_SHIFT
;
720 #ifdef RADIX_TREE_TAGS
722 * Clear all tags associated with the just-deleted item
725 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
727 if (tag_get(pathp
->node
, tag
, pathp
->offset
)) {
728 tag_clear(pathp
->node
, tag
, pathp
->offset
);
729 if (!any_tag_set(pathp
->node
, tag
)) {
736 for (pathp
--; nr_cleared_tags
&& pathp
->node
; pathp
--) {
737 for (tag
= 0; tag
< RADIX_TREE_MAX_TAGS
; tag
++) {
741 tag_clear(pathp
->node
, tag
, pathp
->offset
);
742 if (any_tag_set(pathp
->node
, tag
)) {
749 /* Now free the nodes we do not need anymore */
750 for (pathp
= orig_pathp
; pathp
->node
; pathp
--) {
751 pathp
->node
->slots
[pathp
->offset
] = NULL
;
752 pathp
->node
->count
--;
754 if (pathp
->node
->count
) {
755 if (pathp
->node
== root
->rnode
)
756 radix_tree_shrink(root
);
760 /* Node with zero slots in use so free it */
761 radix_tree_node_free(pathp
->node
);
769 #ifdef RADIX_TREE_TAGS
771 * radix_tree_tagged - test whether any items in the tree are tagged
772 * @root: radix tree root
775 int radix_tree_tagged(struct radix_tree_root
*root
, unsigned int tag
)
777 struct radix_tree_node
*rnode
;
781 return any_tag_set(rnode
, tag
);
785 static unsigned long __maxindex(unsigned int height
)
787 unsigned int width
= height
* RADIX_TREE_MAP_SHIFT
;
788 int shift
= RADIX_TREE_INDEX_BITS
- width
;
792 if (shift
>= BITS_PER_LONG
)
794 return ~0UL >> shift
;
797 static void radix_tree_init_maxindex(void)
801 for (i
= 0; i
< ARRAY_SIZE(height_to_maxindex
); i
++)
802 height_to_maxindex
[i
] = __maxindex(i
);
805 void radix_tree_init(void)
807 radix_tree_init_maxindex();