diff options
Diffstat (limited to 'lib/radix-tree.c')
-rw-r--r-- | lib/radix-tree.c | 110 |
1 files changed, 63 insertions, 47 deletions
diff --git a/lib/radix-tree.c b/lib/radix-tree.c index 4bb42a0344ec..23abbd93cae1 100644 --- a/lib/radix-tree.c +++ b/lib/radix-tree.c | |||
@@ -351,20 +351,12 @@ int radix_tree_insert(struct radix_tree_root *root, | |||
351 | } | 351 | } |
352 | EXPORT_SYMBOL(radix_tree_insert); | 352 | EXPORT_SYMBOL(radix_tree_insert); |
353 | 353 | ||
354 | /** | 354 | /* |
355 | * radix_tree_lookup_slot - lookup a slot in a radix tree | 355 | * is_slot == 1 : search for the slot. |
356 | * @root: radix tree root | 356 | * is_slot == 0 : search for the node. |
357 | * @index: index key | ||
358 | * | ||
359 | * Returns: the slot corresponding to the position @index in the | ||
360 | * radix tree @root. This is useful for update-if-exists operations. | ||
361 | * | ||
362 | * This function can be called under rcu_read_lock iff the slot is not | ||
363 | * modified by radix_tree_replace_slot, otherwise it must be called | ||
364 | * exclusive from other writers. Any dereference of the slot must be done | ||
365 | * using radix_tree_deref_slot. | ||
366 | */ | 357 | */ |
367 | void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index) | 358 | static void *radix_tree_lookup_element(struct radix_tree_root *root, |
359 | unsigned long index, int is_slot) | ||
368 | { | 360 | { |
369 | unsigned int height, shift; | 361 | unsigned int height, shift; |
370 | struct radix_tree_node *node, **slot; | 362 | struct radix_tree_node *node, **slot; |
@@ -376,7 +368,7 @@ void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index) | |||
376 | if (!radix_tree_is_indirect_ptr(node)) { | 368 | if (!radix_tree_is_indirect_ptr(node)) { |
377 | if (index > 0) | 369 | if (index > 0) |
378 | return NULL; | 370 | return NULL; |
379 | return (void **)&root->rnode; | 371 | return is_slot ? (void *)&root->rnode : node; |
380 | } | 372 | } |
381 | node = radix_tree_indirect_to_ptr(node); | 373 | node = radix_tree_indirect_to_ptr(node); |
382 | 374 | ||
@@ -397,7 +389,25 @@ void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index) | |||
397 | height--; | 389 | height--; |
398 | } while (height > 0); | 390 | } while (height > 0); |
399 | 391 | ||
400 | return (void **)slot; | 392 | return is_slot ? (void *)slot:node; |
393 | } | ||
394 | |||
395 | /** | ||
396 | * radix_tree_lookup_slot - lookup a slot in a radix tree | ||
397 | * @root: radix tree root | ||
398 | * @index: index key | ||
399 | * | ||
400 | * Returns: the slot corresponding to the position @index in the | ||
401 | * radix tree @root. This is useful for update-if-exists operations. | ||
402 | * | ||
403 | * This function can be called under rcu_read_lock iff the slot is not | ||
404 | * modified by radix_tree_replace_slot, otherwise it must be called | ||
405 | * exclusive from other writers. Any dereference of the slot must be done | ||
406 | * using radix_tree_deref_slot. | ||
407 | */ | ||
408 | void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index) | ||
409 | { | ||
410 | return (void **)radix_tree_lookup_element(root, index, 1); | ||
401 | } | 411 | } |
402 | EXPORT_SYMBOL(radix_tree_lookup_slot); | 412 | EXPORT_SYMBOL(radix_tree_lookup_slot); |
403 | 413 | ||
@@ -415,38 +425,7 @@ EXPORT_SYMBOL(radix_tree_lookup_slot); | |||
415 | */ | 425 | */ |
416 | void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index) | 426 | void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index) |
417 | { | 427 | { |
418 | unsigned int height, shift; | 428 | return radix_tree_lookup_element(root, index, 0); |
419 | struct radix_tree_node *node, **slot; | ||
420 | |||
421 | node = rcu_dereference(root->rnode); | ||
422 | if (node == NULL) | ||
423 | return NULL; | ||
424 | |||
425 | if (!radix_tree_is_indirect_ptr(node)) { | ||
426 | if (index > 0) | ||
427 | return NULL; | ||
428 | return node; | ||
429 | } | ||
430 | node = radix_tree_indirect_to_ptr(node); | ||
431 | |||
432 | height = node->height; | ||
433 | if (index > radix_tree_maxindex(height)) | ||
434 | return NULL; | ||
435 | |||
436 | shift = (height-1) * RADIX_TREE_MAP_SHIFT; | ||
437 | |||
438 | do { | ||
439 | slot = (struct radix_tree_node **) | ||
440 | (node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK)); | ||
441 | node = rcu_dereference(*slot); | ||
442 | if (node == NULL) | ||
443 | return NULL; | ||
444 | |||
445 | shift -= RADIX_TREE_MAP_SHIFT; | ||
446 | height--; | ||
447 | } while (height > 0); | ||
448 | |||
449 | return node; | ||
450 | } | 429 | } |
451 | EXPORT_SYMBOL(radix_tree_lookup); | 430 | EXPORT_SYMBOL(radix_tree_lookup); |
452 | 431 | ||
@@ -666,6 +645,43 @@ unsigned long radix_tree_next_hole(struct radix_tree_root *root, | |||
666 | } | 645 | } |
667 | EXPORT_SYMBOL(radix_tree_next_hole); | 646 | EXPORT_SYMBOL(radix_tree_next_hole); |
668 | 647 | ||
648 | /** | ||
649 | * radix_tree_prev_hole - find the prev hole (not-present entry) | ||
650 | * @root: tree root | ||
651 | * @index: index key | ||
652 | * @max_scan: maximum range to search | ||
653 | * | ||
654 | * Search backwards in the range [max(index-max_scan+1, 0), index] | ||
655 | * for the first hole. | ||
656 | * | ||
657 | * Returns: the index of the hole if found, otherwise returns an index | ||
658 | * outside of the set specified (in which case 'index - return >= max_scan' | ||
659 | * will be true). In rare cases of wrap-around, LONG_MAX will be returned. | ||
660 | * | ||
661 | * radix_tree_next_hole may be called under rcu_read_lock. However, like | ||
662 | * radix_tree_gang_lookup, this will not atomically search a snapshot of | ||
663 | * the tree at a single point in time. For example, if a hole is created | ||
664 | * at index 10, then subsequently a hole is created at index 5, | ||
665 | * radix_tree_prev_hole covering both indexes may return 5 if called under | ||
666 | * rcu_read_lock. | ||
667 | */ | ||
668 | unsigned long radix_tree_prev_hole(struct radix_tree_root *root, | ||
669 | unsigned long index, unsigned long max_scan) | ||
670 | { | ||
671 | unsigned long i; | ||
672 | |||
673 | for (i = 0; i < max_scan; i++) { | ||
674 | if (!radix_tree_lookup(root, index)) | ||
675 | break; | ||
676 | index--; | ||
677 | if (index == LONG_MAX) | ||
678 | break; | ||
679 | } | ||
680 | |||
681 | return index; | ||
682 | } | ||
683 | EXPORT_SYMBOL(radix_tree_prev_hole); | ||
684 | |||
669 | static unsigned int | 685 | static unsigned int |
670 | __lookup(struct radix_tree_node *slot, void ***results, unsigned long index, | 686 | __lookup(struct radix_tree_node *slot, void ***results, unsigned long index, |
671 | unsigned int max_items, unsigned long *next_index) | 687 | unsigned int max_items, unsigned long *next_index) |