| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * An extensible bitmap is a bitmap that supports an | 
 | 3 |  * arbitrary number of bits.  Extensible bitmaps are | 
 | 4 |  * used to represent sets of values, such as types, | 
 | 5 |  * roles, categories, and classes. | 
 | 6 |  * | 
 | 7 |  * Each extensible bitmap is implemented as a linked | 
 | 8 |  * list of bitmap nodes, where each bitmap node has | 
 | 9 |  * an explicitly specified starting bit position within | 
 | 10 |  * the total bitmap. | 
 | 11 |  * | 
 | 12 |  * Author : Stephen Smalley, <sds@epoch.ncsc.mil> | 
 | 13 |  */ | 
 | 14 | #ifndef _SS_EBITMAP_H_ | 
 | 15 | #define _SS_EBITMAP_H_ | 
 | 16 |  | 
| Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 17 | #include <net/netlabel.h> | 
 | 18 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 19 | #define EBITMAP_UNIT_NUMS	((32 - sizeof(void *) - sizeof(u32))	\ | 
 | 20 | 					/ sizeof(unsigned long)) | 
 | 21 | #define EBITMAP_UNIT_SIZE	BITS_PER_LONG | 
 | 22 | #define EBITMAP_SIZE		(EBITMAP_UNIT_NUMS * EBITMAP_UNIT_SIZE) | 
 | 23 | #define EBITMAP_BIT		1ULL | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 |  | 
 | 25 | struct ebitmap_node { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | 	struct ebitmap_node *next; | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 27 | 	unsigned long maps[EBITMAP_UNIT_NUMS]; | 
 | 28 | 	u32 startbit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | }; | 
 | 30 |  | 
 | 31 | struct ebitmap { | 
 | 32 | 	struct ebitmap_node *node;	/* first node in the bitmap */ | 
 | 33 | 	u32 highbit;	/* highest position in the total bitmap */ | 
 | 34 | }; | 
 | 35 |  | 
 | 36 | #define ebitmap_length(e) ((e)->highbit) | 
 | 37 | #define ebitmap_startbit(e) ((e)->node ? (e)->node->startbit : 0) | 
 | 38 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 39 | static inline unsigned int ebitmap_start_positive(struct ebitmap *e, | 
 | 40 | 						  struct ebitmap_node **n) | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 41 | { | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 42 | 	unsigned int ofs; | 
 | 43 |  | 
 | 44 | 	for (*n = e->node; *n; *n = (*n)->next) { | 
 | 45 | 		ofs = find_first_bit((*n)->maps, EBITMAP_SIZE); | 
 | 46 | 		if (ofs < EBITMAP_SIZE) | 
 | 47 | 			return (*n)->startbit + ofs; | 
 | 48 | 	} | 
 | 49 | 	return ebitmap_length(e); | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 50 | } | 
 | 51 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static inline void ebitmap_init(struct ebitmap *e) | 
 | 53 | { | 
 | 54 | 	memset(e, 0, sizeof(*e)); | 
 | 55 | } | 
 | 56 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 57 | static inline unsigned int ebitmap_next_positive(struct ebitmap *e, | 
 | 58 | 						 struct ebitmap_node **n, | 
 | 59 | 						 unsigned int bit) | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 60 | { | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 61 | 	unsigned int ofs; | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 62 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 63 | 	ofs = find_next_bit((*n)->maps, EBITMAP_SIZE, bit - (*n)->startbit + 1); | 
 | 64 | 	if (ofs < EBITMAP_SIZE) | 
 | 65 | 		return ofs + (*n)->startbit; | 
 | 66 |  | 
 | 67 | 	for (*n = (*n)->next; *n; *n = (*n)->next) { | 
 | 68 | 		ofs = find_first_bit((*n)->maps, EBITMAP_SIZE); | 
 | 69 | 		if (ofs < EBITMAP_SIZE) | 
 | 70 | 			return ofs + (*n)->startbit; | 
 | 71 | 	} | 
 | 72 | 	return ebitmap_length(e); | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 73 | } | 
 | 74 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 75 | #define EBITMAP_NODE_INDEX(node, bit)	\ | 
 | 76 | 	(((bit) - (node)->startbit) / EBITMAP_UNIT_SIZE) | 
 | 77 | #define EBITMAP_NODE_OFFSET(node, bit)	\ | 
 | 78 | 	(((bit) - (node)->startbit) % EBITMAP_UNIT_SIZE) | 
 | 79 |  | 
 | 80 | static inline int ebitmap_node_get_bit(struct ebitmap_node *n, | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 81 | 				       unsigned int bit) | 
 | 82 | { | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 83 | 	unsigned int index = EBITMAP_NODE_INDEX(n, bit); | 
 | 84 | 	unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit); | 
 | 85 |  | 
 | 86 | 	BUG_ON(index >= EBITMAP_UNIT_NUMS); | 
 | 87 | 	if ((n->maps[index] & (EBITMAP_BIT << ofs))) | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 88 | 		return 1; | 
 | 89 | 	return 0; | 
 | 90 | } | 
 | 91 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame^] | 92 | static inline void ebitmap_node_set_bit(struct ebitmap_node *n, | 
 | 93 | 					unsigned int bit) | 
 | 94 | { | 
 | 95 | 	unsigned int index = EBITMAP_NODE_INDEX(n, bit); | 
 | 96 | 	unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit); | 
 | 97 |  | 
 | 98 | 	BUG_ON(index >= EBITMAP_UNIT_NUMS); | 
 | 99 | 	n->maps[index] |= (EBITMAP_BIT << ofs); | 
 | 100 | } | 
 | 101 |  | 
 | 102 | static inline void ebitmap_node_clr_bit(struct ebitmap_node *n, | 
 | 103 | 					unsigned int bit) | 
 | 104 | { | 
 | 105 | 	unsigned int index = EBITMAP_NODE_INDEX(n, bit); | 
 | 106 | 	unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit); | 
 | 107 |  | 
 | 108 | 	BUG_ON(index >= EBITMAP_UNIT_NUMS); | 
 | 109 | 	n->maps[index] &= ~(EBITMAP_BIT << ofs); | 
 | 110 | } | 
 | 111 |  | 
 | 112 | #define ebitmap_for_each_positive_bit(e, n, bit)	\ | 
 | 113 | 	for (bit = ebitmap_start_positive(e, &n);	\ | 
 | 114 | 	     bit < ebitmap_length(e);			\ | 
 | 115 | 	     bit = ebitmap_next_positive(e, &n, bit))	\ | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 116 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2); | 
 | 118 | int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2); | 
 | 120 | int ebitmap_get_bit(struct ebitmap *e, unsigned long bit); | 
 | 121 | int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value); | 
 | 122 | void ebitmap_destroy(struct ebitmap *e); | 
 | 123 | int ebitmap_read(struct ebitmap *e, void *fp); | 
 | 124 |  | 
| Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 125 | #ifdef CONFIG_NETLABEL | 
 | 126 | int ebitmap_netlbl_export(struct ebitmap *ebmap, | 
 | 127 | 			  struct netlbl_lsm_secattr_catmap **catmap); | 
 | 128 | int ebitmap_netlbl_import(struct ebitmap *ebmap, | 
 | 129 | 			  struct netlbl_lsm_secattr_catmap *catmap); | 
 | 130 | #else | 
 | 131 | static inline int ebitmap_netlbl_export(struct ebitmap *ebmap, | 
 | 132 | 				struct netlbl_lsm_secattr_catmap **catmap) | 
 | 133 | { | 
 | 134 | 	return -ENOMEM; | 
 | 135 | } | 
 | 136 | static inline int ebitmap_netlbl_import(struct ebitmap *ebmap, | 
 | 137 | 				struct netlbl_lsm_secattr_catmap *catmap) | 
 | 138 | { | 
 | 139 | 	return -ENOMEM; | 
 | 140 | } | 
 | 141 | #endif | 
 | 142 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | #endif	/* _SS_EBITMAP_H_ */ |