blob: a50a1c44eabd7b099b55b846259fe6abaf66c8d6 [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;
Chris Masond6025572007-03-30 14:27:56 -040025 radix_tree_preload(GFP_NOFS);
Chris Mason8ef97622007-03-26 10:15:30 -040026 ret = radix_tree_insert(radix, slot, bits);
Chris Masond6025572007-03-30 14:27:56 -040027 radix_tree_preload_end();
Chris Mason8ef97622007-03-26 10:15:30 -040028 if (ret)
29 return ret;
30 }
31 set_bit(bit_slot, bits + 1);
32 return 0;
33}
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 Masond6025572007-03-30 14:27:56 -040065#if 0
Chris Mason8ef97622007-03-26 10:15:30 -040066 for (i = 1; i < BIT_ARRAY_BYTES / sizeof(unsigned long); i++) {
67 if (bits[i]) {
68 empty = 0;
69 break;
70 }
71 }
Chris Mason8ef97622007-03-26 10:15:30 -040072 if (empty) {
73 bits = radix_tree_delete(radix, slot);
74 BUG_ON(!bits);
Chris Mason2c90e5d2007-04-02 10:50:19 -040075 kmem_cache_free(btrfs_bit_radix_cachep, bits);
Chris Mason8ef97622007-03-26 10:15:30 -040076 }
Chris Masond6025572007-03-30 14:27:56 -040077#endif
Chris Mason8ef97622007-03-26 10:15:30 -040078 return 0;
79}
80
81int find_first_radix_bit(struct radix_tree_root *radix, unsigned long *retbits,
82 int nr)
83{
84 unsigned long *bits;
85 unsigned long *gang[4];
86 int found;
87 int ret;
88 int i;
89 int total_found = 0;
90
91 ret = radix_tree_gang_lookup(radix, (void *)&gang, 0, ARRAY_SIZE(gang));
92 for (i = 0; i < ret && nr > 0; i++) {
93 found = 0;
94 bits = gang[i];
95 while(nr > 0) {
96 found = find_next_bit(bits + 1,
97 BIT_RADIX_BITS_PER_ARRAY,
98 found);
99 if (found < BIT_RADIX_BITS_PER_ARRAY) {
100 *retbits = bits[0] *
101 BIT_RADIX_BITS_PER_ARRAY + found;
102 retbits++;
103 nr--;
104 total_found++;
105 found++;
106 } else
107 break;
108 }
109 }
110 return total_found;
111}