blob: e9ace32d7bae1841b2ca86276eb23316cb3d6dd4 [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 }
29 set_bit(bit_slot, bits + 1);
30 return 0;
31}
32
33int test_radix_bit(struct radix_tree_root *radix, unsigned long bit)
34{
35 unsigned long *bits;
36 unsigned long slot;
37 int bit_slot;
38
39 slot = bit / BIT_RADIX_BITS_PER_ARRAY;
40 bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY;
41
42 bits = radix_tree_lookup(radix, slot);
43 if (!bits)
44 return 0;
45 return test_bit(bit_slot, bits + 1);
46}
47
48int clear_radix_bit(struct radix_tree_root *radix, unsigned long bit)
49{
50 unsigned long *bits;
51 unsigned long slot;
52 int bit_slot;
53 int i;
54 int empty = 1;
55
56 slot = bit / BIT_RADIX_BITS_PER_ARRAY;
57 bit_slot = bit % BIT_RADIX_BITS_PER_ARRAY;
58
59 bits = radix_tree_lookup(radix, slot);
60 if (!bits)
61 return 0;
62 clear_bit(bit_slot, bits + 1);
Chris Mason8ef97622007-03-26 10:15:30 -040063 for (i = 1; i < BIT_ARRAY_BYTES / sizeof(unsigned long); i++) {
64 if (bits[i]) {
65 empty = 0;
66 break;
67 }
68 }
Chris Mason8ef97622007-03-26 10:15:30 -040069 if (empty) {
70 bits = radix_tree_delete(radix, slot);
71 BUG_ON(!bits);
Chris Mason2c90e5d2007-04-02 10:50:19 -040072 kmem_cache_free(btrfs_bit_radix_cachep, bits);
Chris Mason8ef97622007-03-26 10:15:30 -040073 }
Chris Mason8ef97622007-03-26 10:15:30 -040074 return 0;
75}
76
77int find_first_radix_bit(struct radix_tree_root *radix, unsigned long *retbits,
78 int nr)
79{
80 unsigned long *bits;
81 unsigned long *gang[4];
82 int found;
83 int ret;
84 int i;
85 int total_found = 0;
86
Chris Mason0f7d52f2007-04-09 10:42:37 -040087 ret = radix_tree_gang_lookup(radix, (void **)gang, 0, ARRAY_SIZE(gang));
Chris Mason8ef97622007-03-26 10:15:30 -040088 for (i = 0; i < ret && nr > 0; i++) {
89 found = 0;
90 bits = gang[i];
91 while(nr > 0) {
92 found = find_next_bit(bits + 1,
93 BIT_RADIX_BITS_PER_ARRAY,
94 found);
95 if (found < BIT_RADIX_BITS_PER_ARRAY) {
96 *retbits = bits[0] *
97 BIT_RADIX_BITS_PER_ARRAY + found;
98 retbits++;
99 nr--;
100 total_found++;
101 found++;
102 } else
103 break;
104 }
105 }
106 return total_found;
107}