2 * Implementation of the extensible bitmap type.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/errno.h>
12 int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2)
14 struct ebitmap_node *n1, *n2;
16 if (e1->highbit != e2->highbit)
22 (n1->startbit == n2->startbit) &&
23 (n1->map == n2->map)) {
34 int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src)
36 struct ebitmap_node *n, *new, *prev;
42 new = kmalloc(sizeof(*new), GFP_ATOMIC);
47 memset(new, 0, sizeof(*new));
48 new->startbit = n->startbit;
59 dst->highbit = src->highbit;
63 int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2)
65 struct ebitmap_node *n1, *n2;
67 if (e1->highbit < e2->highbit)
72 while (n1 && n2 && (n1->startbit <= n2->startbit)) {
73 if (n1->startbit < n2->startbit) {
77 if ((n1->map & n2->map) != n2->map)
90 int ebitmap_get_bit(struct ebitmap *e, unsigned long bit)
92 struct ebitmap_node *n;
98 while (n && (n->startbit <= bit)) {
99 if ((n->startbit + MAPSIZE) > bit) {
100 if (n->map & (MAPBIT << (bit - n->startbit)))
111 int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value)
113 struct ebitmap_node *n, *prev, *new;
117 while (n && n->startbit <= bit) {
118 if ((n->startbit + MAPSIZE) > bit) {
120 n->map |= (MAPBIT << (bit - n->startbit));
122 n->map &= ~(MAPBIT << (bit - n->startbit));
124 /* drop this node from the bitmap */
128 * this was the highest map
132 e->highbit = prev->startbit + MAPSIZE;
137 prev->next = n->next;
153 new = kmalloc(sizeof(*new), GFP_ATOMIC);
156 memset(new, 0, sizeof(*new));
158 new->startbit = bit & ~(MAPSIZE - 1);
159 new->map = (MAPBIT << (bit - new->startbit));
162 /* this node will be the highest map within the bitmap */
163 e->highbit = new->startbit + MAPSIZE;
166 new->next = prev->next;
176 void ebitmap_destroy(struct ebitmap *e)
178 struct ebitmap_node *n, *temp;
195 int ebitmap_read(struct ebitmap *e, void *fp)
198 struct ebitmap_node *n, *l;
200 u32 mapsize, count, i;
205 rc = next_entry(buf, fp, sizeof buf);
209 mapsize = le32_to_cpu(buf[0]);
210 e->highbit = le32_to_cpu(buf[1]);
211 count = le32_to_cpu(buf[2]);
213 if (mapsize != MAPSIZE) {
214 printk(KERN_ERR "security: ebitmap: map size %u does not "
215 "match my size %Zd (high bit was %d)\n", mapsize,
216 MAPSIZE, e->highbit);
223 if (e->highbit & (MAPSIZE - 1)) {
224 printk(KERN_ERR "security: ebitmap: high bit (%d) is not a "
225 "multiple of the map size (%Zd)\n", e->highbit, MAPSIZE);
229 for (i = 0; i < count; i++) {
230 rc = next_entry(buf, fp, sizeof(u32));
232 printk(KERN_ERR "security: ebitmap: truncated map\n");
235 n = kmalloc(sizeof(*n), GFP_KERNEL);
237 printk(KERN_ERR "security: ebitmap: out of memory\n");
241 memset(n, 0, sizeof(*n));
243 n->startbit = le32_to_cpu(buf[0]);
245 if (n->startbit & (MAPSIZE - 1)) {
246 printk(KERN_ERR "security: ebitmap start bit (%d) is "
247 "not a multiple of the map size (%Zd)\n",
248 n->startbit, MAPSIZE);
251 if (n->startbit > (e->highbit - MAPSIZE)) {
252 printk(KERN_ERR "security: ebitmap start bit (%d) is "
253 "beyond the end of the bitmap (%Zd)\n",
254 n->startbit, (e->highbit - MAPSIZE));
257 rc = next_entry(&map, fp, sizeof(u64));
259 printk(KERN_ERR "security: ebitmap: truncated map\n");
262 n->map = le64_to_cpu(map);
265 printk(KERN_ERR "security: ebitmap: null map in "
266 "ebitmap (startbit %d)\n", n->startbit);
270 if (n->startbit <= l->startbit) {
271 printk(KERN_ERR "security: ebitmap: start "
272 "bit %d comes after start bit %d\n",
273 n->startbit, l->startbit);