blob: 8f9cd42772313520954b3864eefaad3eff6c6023 [file] [log] [blame]
Chris Mason8ef97622007-03-26 10:15:30 -04001#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 Mason2c90e5d2007-04-02 10:50:19 -04007extern struct kmem_cache *btrfs_bit_radix_cachep;
Chris Mason8ef97622007-03-26 10:15:30 -04008int 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 Mason2c90e5d2007-04-02 10:50:19 -040020 bits = kmem_cache_alloc(btrfs_bit_radix_cachep, GFP_NOFS);
Chris Mason8ef97622007-03-26 10:15:30 -040021 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 Masonbe744172007-05-06 10:15:01 -040029 ret = test_and_set_bit(bit_slot, bits + 1);
30 if (ret < 0)
31 ret = 1;
32 return ret;
Chris Mason8ef97622007-03-26 10:15:30 -040033}
34
35int 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
50int 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 Mason8ef97622007-03-26 10:15:30 -040065 for (i = 1; i < BIT_ARRAY_BYTES / sizeof(unsigned long); i++) {
66 if (bits[i]) {
67 empty = 0;
68 break;
69 }
70 }
Chris Mason8ef97622007-03-26 10:15:30 -040071 if (empty) {
72 bits = radix_tree_delete(radix, slot);
73 BUG_ON(!bits);
Chris Mason2c90e5d2007-04-02 10:50:19 -040074 kmem_cache_free(btrfs_bit_radix_cachep, bits);
Chris Mason8ef97622007-03-26 10:15:30 -040075 }
Chris Mason8ef97622007-03-26 10:15:30 -040076 return 0;
77}
78
79int find_first_radix_bit(struct radix_tree_root *radix, unsigned long *retbits,
Chris Masone37c9e62007-05-09 20:13:14 -040080 unsigned long start, int nr)
Chris Mason8ef97622007-03-26 10:15:30 -040081{
82 unsigned long *bits;
83 unsigned long *gang[4];
84 int found;
85 int ret;
86 int i;
87 int total_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -040088 unsigned long slot;
Chris Mason8ef97622007-03-26 10:15:30 -040089
Chris Masone37c9e62007-05-09 20:13:14 -040090 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 Mason8ef97622007-03-26 10:15:30 -040094 for (i = 0; i < ret && nr > 0; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -040095 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 Masone37c9e62007-05-09 20:13:14 -0400110 found = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400111 }
112 return total_found;
113}