Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include "bit-radix.h" |
| 3 | |
| 4 | #define BIT_ARRAY_BYTES 256 |
| 5 | #define BIT_RADIX_BITS_PER_ARRAY ((BIT_ARRAY_BYTES - sizeof(unsigned long)) * 8) |
| 6 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 7 | extern struct kmem_cache *btrfs_bit_radix_cachep; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 8 | int set_radix_bit(struct radix_tree_root *radix, unsigned long bit) |
| 9 | { |
| 10 | unsigned long *bits; |
| 11 | unsigned long slot; |
| 12 | int bit_slot; |
| 13 | int ret; |
| 14 | |
| 15 | slot = bit / BIT_RADIX_BITS_PER_ARRAY; |
| 16 | bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY; |
| 17 | |
| 18 | bits = radix_tree_lookup(radix, slot); |
| 19 | if (!bits) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 20 | bits = kmem_cache_alloc(btrfs_bit_radix_cachep, GFP_NOFS); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 21 | if (!bits) |
| 22 | return -ENOMEM; |
| 23 | memset(bits + 1, 0, BIT_ARRAY_BYTES - sizeof(unsigned long)); |
| 24 | bits[0] = slot; |
| 25 | ret = radix_tree_insert(radix, slot, bits); |
| 26 | if (ret) |
| 27 | return ret; |
| 28 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 29 | ret = test_and_set_bit(bit_slot, bits + 1); |
| 30 | if (ret < 0) |
| 31 | ret = 1; |
| 32 | return ret; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | int test_radix_bit(struct radix_tree_root *radix, unsigned long bit) |
| 36 | { |
| 37 | unsigned long *bits; |
| 38 | unsigned long slot; |
| 39 | int bit_slot; |
| 40 | |
| 41 | slot = bit / BIT_RADIX_BITS_PER_ARRAY; |
| 42 | bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY; |
| 43 | |
| 44 | bits = radix_tree_lookup(radix, slot); |
| 45 | if (!bits) |
| 46 | return 0; |
| 47 | return test_bit(bit_slot, bits + 1); |
| 48 | } |
| 49 | |
| 50 | int clear_radix_bit(struct radix_tree_root *radix, unsigned long bit) |
| 51 | { |
| 52 | unsigned long *bits; |
| 53 | unsigned long slot; |
| 54 | int bit_slot; |
| 55 | int i; |
| 56 | int empty = 1; |
| 57 | |
| 58 | slot = bit / BIT_RADIX_BITS_PER_ARRAY; |
| 59 | bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY; |
| 60 | |
| 61 | bits = radix_tree_lookup(radix, slot); |
| 62 | if (!bits) |
| 63 | return 0; |
| 64 | clear_bit(bit_slot, bits + 1); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 65 | for (i = 1; i < BIT_ARRAY_BYTES / sizeof(unsigned long); i++) { |
| 66 | if (bits[i]) { |
| 67 | empty = 0; |
| 68 | break; |
| 69 | } |
| 70 | } |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 71 | if (empty) { |
| 72 | bits = radix_tree_delete(radix, slot); |
| 73 | BUG_ON(!bits); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 74 | kmem_cache_free(btrfs_bit_radix_cachep, bits); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 75 | } |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int find_first_radix_bit(struct radix_tree_root *radix, unsigned long *retbits, |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame^] | 80 | unsigned long start, int nr) |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 81 | { |
| 82 | unsigned long *bits; |
| 83 | unsigned long *gang[4]; |
| 84 | int found; |
| 85 | int ret; |
| 86 | int i; |
| 87 | int total_found = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame^] | 88 | unsigned long slot; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 89 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame^] | 90 | slot = start / BIT_RADIX_BITS_PER_ARRAY; |
| 91 | ret = radix_tree_gang_lookup(radix, (void **)gang, slot, |
| 92 | ARRAY_SIZE(gang)); |
| 93 | found = start % BIT_RADIX_BITS_PER_ARRAY; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 94 | for (i = 0; i < ret && nr > 0; i++) { |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 95 | bits = gang[i]; |
| 96 | while(nr > 0) { |
| 97 | found = find_next_bit(bits + 1, |
| 98 | BIT_RADIX_BITS_PER_ARRAY, |
| 99 | found); |
| 100 | if (found < BIT_RADIX_BITS_PER_ARRAY) { |
| 101 | *retbits = bits[0] * |
| 102 | BIT_RADIX_BITS_PER_ARRAY + found; |
| 103 | retbits++; |
| 104 | nr--; |
| 105 | total_found++; |
| 106 | found++; |
| 107 | } else |
| 108 | break; |
| 109 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame^] | 110 | found = 0; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 111 | } |
| 112 | return total_found; |
| 113 | } |