2 * Implementation of the extensible bitmap type.
4 * Author : Stephen Smalley, <sds@epoch.ncsc.mil>
7 * Updated: Hewlett-Packard <paul.moore@hp.com>
9 * Added ebitmap_export() and ebitmap_import()
11 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
20 int ebitmap_cmp(struct ebitmap
*e1
, struct ebitmap
*e2
)
22 struct ebitmap_node
*n1
, *n2
;
24 if (e1
->highbit
!= e2
->highbit
)
30 (n1
->startbit
== n2
->startbit
) &&
31 (n1
->map
== n2
->map
)) {
42 int ebitmap_cpy(struct ebitmap
*dst
, struct ebitmap
*src
)
44 struct ebitmap_node
*n
, *new, *prev
;
50 new = kzalloc(sizeof(*new), GFP_ATOMIC
);
55 new->startbit
= n
->startbit
;
66 dst
->highbit
= src
->highbit
;
71 * ebitmap_export - Export an ebitmap to a unsigned char bitmap string
72 * @src: the ebitmap to export
73 * @dst: the resulting bitmap string
74 * @dst_len: length of dst in bytes
77 * Allocate a buffer at least src->highbit bits long and export the extensible
78 * bitmap into the buffer. The bitmap string will be in little endian format,
79 * i.e. LSB first. The value returned in dst_len may not the true size of the
80 * buffer as the length of the buffer is rounded up to a multiple of MAPTYPE.
81 * The caller must free the buffer when finished. Returns zero on success,
82 * negative values on failure.
85 int ebitmap_export(const struct ebitmap
*src
,
90 unsigned char *bitmap
;
91 struct ebitmap_node
*iter_node
;
94 unsigned char bitmask
;
96 bitmap_len
= src
->highbit
/ 8;
102 bitmap
= kzalloc((bitmap_len
& ~(sizeof(MAPTYPE
) - 1)) +
108 iter_node
= src
->node
;
110 bitmap_byte
= iter_node
->startbit
/ 8;
112 node_val
= iter_node
->map
;
118 if (node_val
& (MAPTYPE
)0x01)
119 bitmap
[bitmap_byte
] |= bitmask
;
122 } while (node_val
> 0);
123 iter_node
= iter_node
->next
;
127 *dst_len
= bitmap_len
;
132 * ebitmap_import - Import an unsigned char bitmap string into an ebitmap
133 * @src: the bitmap string
134 * @src_len: the bitmap length in bytes
135 * @dst: the empty ebitmap
138 * This function takes a little endian bitmap string in src and imports it into
139 * the ebitmap pointed to by dst. Returns zero on success, negative values on
143 int ebitmap_import(const unsigned char *src
,
149 struct ebitmap_node
*node_new
;
150 struct ebitmap_node
*node_last
= NULL
;
153 unsigned char src_byte
;
155 while (src_off
< src_len
) {
156 if (src_len
- src_off
>= sizeof(MAPTYPE
)) {
157 if (*(MAPTYPE
*)&src
[src_off
] == 0) {
158 src_off
+= sizeof(MAPTYPE
);
161 node_limit
= sizeof(MAPTYPE
);
163 for (src_byte
= 0, i_byte
= src_off
;
164 i_byte
< src_len
&& src_byte
== 0;
166 src_byte
|= src
[i_byte
];
169 node_limit
= src_len
- src_off
;
172 node_new
= kzalloc(sizeof(*node_new
), GFP_ATOMIC
);
173 if (unlikely(node_new
== NULL
)) {
174 ebitmap_destroy(dst
);
177 node_new
->startbit
= src_off
* 8;
178 for (i_byte
= 0; i_byte
< node_limit
; i_byte
++) {
179 src_byte
= src
[src_off
++];
180 for (i_bit
= i_byte
* 8; src_byte
!= 0; i_bit
++) {
182 node_new
->map
|= MAPBIT
<< i_bit
;
187 if (node_last
!= NULL
)
188 node_last
->next
= node_new
;
190 dst
->node
= node_new
;
191 node_last
= node_new
;
194 if (likely(node_last
!= NULL
))
195 dst
->highbit
= node_last
->startbit
+ MAPSIZE
;
202 int ebitmap_contains(struct ebitmap
*e1
, struct ebitmap
*e2
)
204 struct ebitmap_node
*n1
, *n2
;
206 if (e1
->highbit
< e2
->highbit
)
211 while (n1
&& n2
&& (n1
->startbit
<= n2
->startbit
)) {
212 if (n1
->startbit
< n2
->startbit
) {
216 if ((n1
->map
& n2
->map
) != n2
->map
)
229 int ebitmap_get_bit(struct ebitmap
*e
, unsigned long bit
)
231 struct ebitmap_node
*n
;
233 if (e
->highbit
< bit
)
237 while (n
&& (n
->startbit
<= bit
)) {
238 if ((n
->startbit
+ MAPSIZE
) > bit
) {
239 if (n
->map
& (MAPBIT
<< (bit
- n
->startbit
)))
250 int ebitmap_set_bit(struct ebitmap
*e
, unsigned long bit
, int value
)
252 struct ebitmap_node
*n
, *prev
, *new;
256 while (n
&& n
->startbit
<= bit
) {
257 if ((n
->startbit
+ MAPSIZE
) > bit
) {
259 n
->map
|= (MAPBIT
<< (bit
- n
->startbit
));
261 n
->map
&= ~(MAPBIT
<< (bit
- n
->startbit
));
263 /* drop this node from the bitmap */
267 * this was the highest map
271 e
->highbit
= prev
->startbit
+ MAPSIZE
;
276 prev
->next
= n
->next
;
292 new = kzalloc(sizeof(*new), GFP_ATOMIC
);
296 new->startbit
= bit
& ~(MAPSIZE
- 1);
297 new->map
= (MAPBIT
<< (bit
- new->startbit
));
300 /* this node will be the highest map within the bitmap */
301 e
->highbit
= new->startbit
+ MAPSIZE
;
304 new->next
= prev
->next
;
314 void ebitmap_destroy(struct ebitmap
*e
)
316 struct ebitmap_node
*n
, *temp
;
333 int ebitmap_read(struct ebitmap
*e
, void *fp
)
336 struct ebitmap_node
*n
, *l
;
338 u32 mapsize
, count
, i
;
343 rc
= next_entry(buf
, fp
, sizeof buf
);
347 mapsize
= le32_to_cpu(buf
[0]);
348 e
->highbit
= le32_to_cpu(buf
[1]);
349 count
= le32_to_cpu(buf
[2]);
351 if (mapsize
!= MAPSIZE
) {
352 printk(KERN_ERR
"security: ebitmap: map size %u does not "
353 "match my size %Zd (high bit was %d)\n", mapsize
,
354 MAPSIZE
, e
->highbit
);
361 if (e
->highbit
& (MAPSIZE
- 1)) {
362 printk(KERN_ERR
"security: ebitmap: high bit (%d) is not a "
363 "multiple of the map size (%Zd)\n", e
->highbit
, MAPSIZE
);
367 for (i
= 0; i
< count
; i
++) {
368 rc
= next_entry(buf
, fp
, sizeof(u32
));
370 printk(KERN_ERR
"security: ebitmap: truncated map\n");
373 n
= kzalloc(sizeof(*n
), GFP_KERNEL
);
375 printk(KERN_ERR
"security: ebitmap: out of memory\n");
380 n
->startbit
= le32_to_cpu(buf
[0]);
382 if (n
->startbit
& (MAPSIZE
- 1)) {
383 printk(KERN_ERR
"security: ebitmap start bit (%d) is "
384 "not a multiple of the map size (%Zd)\n",
385 n
->startbit
, MAPSIZE
);
388 if (n
->startbit
> (e
->highbit
- MAPSIZE
)) {
389 printk(KERN_ERR
"security: ebitmap start bit (%d) is "
390 "beyond the end of the bitmap (%Zd)\n",
391 n
->startbit
, (e
->highbit
- MAPSIZE
));
394 rc
= next_entry(&map
, fp
, sizeof(u64
));
396 printk(KERN_ERR
"security: ebitmap: truncated map\n");
399 n
->map
= le64_to_cpu(map
);
402 printk(KERN_ERR
"security: ebitmap: null map in "
403 "ebitmap (startbit %d)\n", n
->startbit
);
407 if (n
->startbit
<= l
->startbit
) {
408 printk(KERN_ERR
"security: ebitmap: start "
409 "bit %d comes after start bit %d\n",
410 n
->startbit
, l
->startbit
);