| 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 | 
| KaiGai Kohei | 087feb9 | 2007-10-03 23:42:56 +0900 | [diff] [blame] | 24 | #define EBITMAP_SHIFT_UNIT_SIZE(x)					\ | 
|  | 25 | (((x) >> EBITMAP_UNIT_SIZE / 2) >> EBITMAP_UNIT_SIZE / 2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | struct ebitmap_node { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct ebitmap_node *next; | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 29 | unsigned long maps[EBITMAP_UNIT_NUMS]; | 
|  | 30 | u32 startbit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | }; | 
|  | 32 |  | 
|  | 33 | struct ebitmap { | 
|  | 34 | struct ebitmap_node *node;	/* first node in the bitmap */ | 
|  | 35 | u32 highbit;	/* highest position in the total bitmap */ | 
|  | 36 | }; | 
|  | 37 |  | 
|  | 38 | #define ebitmap_length(e) ((e)->highbit) | 
|  | 39 | #define ebitmap_startbit(e) ((e)->node ? (e)->node->startbit : 0) | 
|  | 40 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 41 | static inline unsigned int ebitmap_start_positive(struct ebitmap *e, | 
|  | 42 | struct ebitmap_node **n) | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 43 | { | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 44 | unsigned int ofs; | 
|  | 45 |  | 
|  | 46 | for (*n = e->node; *n; *n = (*n)->next) { | 
|  | 47 | ofs = find_first_bit((*n)->maps, EBITMAP_SIZE); | 
|  | 48 | if (ofs < EBITMAP_SIZE) | 
|  | 49 | return (*n)->startbit + ofs; | 
|  | 50 | } | 
|  | 51 | return ebitmap_length(e); | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static inline void ebitmap_init(struct ebitmap *e) | 
|  | 55 | { | 
|  | 56 | memset(e, 0, sizeof(*e)); | 
|  | 57 | } | 
|  | 58 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 59 | static inline unsigned int ebitmap_next_positive(struct ebitmap *e, | 
|  | 60 | struct ebitmap_node **n, | 
|  | 61 | unsigned int bit) | 
| 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 | unsigned int ofs; | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 64 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 65 | ofs = find_next_bit((*n)->maps, EBITMAP_SIZE, bit - (*n)->startbit + 1); | 
|  | 66 | if (ofs < EBITMAP_SIZE) | 
|  | 67 | return ofs + (*n)->startbit; | 
|  | 68 |  | 
|  | 69 | for (*n = (*n)->next; *n; *n = (*n)->next) { | 
|  | 70 | ofs = find_first_bit((*n)->maps, EBITMAP_SIZE); | 
|  | 71 | if (ofs < EBITMAP_SIZE) | 
|  | 72 | return ofs + (*n)->startbit; | 
|  | 73 | } | 
|  | 74 | return ebitmap_length(e); | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 77 | #define EBITMAP_NODE_INDEX(node, bit)	\ | 
|  | 78 | (((bit) - (node)->startbit) / EBITMAP_UNIT_SIZE) | 
|  | 79 | #define EBITMAP_NODE_OFFSET(node, bit)	\ | 
|  | 80 | (((bit) - (node)->startbit) % EBITMAP_UNIT_SIZE) | 
|  | 81 |  | 
|  | 82 | static inline int ebitmap_node_get_bit(struct ebitmap_node *n, | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 83 | unsigned int bit) | 
|  | 84 | { | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 85 | unsigned int index = EBITMAP_NODE_INDEX(n, bit); | 
|  | 86 | unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit); | 
|  | 87 |  | 
|  | 88 | BUG_ON(index >= EBITMAP_UNIT_NUMS); | 
|  | 89 | if ((n->maps[index] & (EBITMAP_BIT << ofs))) | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 90 | return 1; | 
|  | 91 | return 0; | 
|  | 92 | } | 
|  | 93 |  | 
| KaiGai Kohei | 9fe79ad | 2007-09-29 02:20:55 +0900 | [diff] [blame] | 94 | static inline void ebitmap_node_set_bit(struct ebitmap_node *n, | 
|  | 95 | unsigned int bit) | 
|  | 96 | { | 
|  | 97 | unsigned int index = EBITMAP_NODE_INDEX(n, bit); | 
|  | 98 | unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit); | 
|  | 99 |  | 
|  | 100 | BUG_ON(index >= EBITMAP_UNIT_NUMS); | 
|  | 101 | n->maps[index] |= (EBITMAP_BIT << ofs); | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | static inline void ebitmap_node_clr_bit(struct ebitmap_node *n, | 
|  | 105 | unsigned int bit) | 
|  | 106 | { | 
|  | 107 | unsigned int index = EBITMAP_NODE_INDEX(n, bit); | 
|  | 108 | unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit); | 
|  | 109 |  | 
|  | 110 | BUG_ON(index >= EBITMAP_UNIT_NUMS); | 
|  | 111 | n->maps[index] &= ~(EBITMAP_BIT << ofs); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | #define ebitmap_for_each_positive_bit(e, n, bit)	\ | 
|  | 115 | for (bit = ebitmap_start_positive(e, &n);	\ | 
|  | 116 | bit < ebitmap_length(e);			\ | 
|  | 117 | bit = ebitmap_next_positive(e, &n, bit))	\ | 
| Stephen Smalley | 782ebb9 | 2005-09-03 15:55:16 -0700 | [diff] [blame] | 118 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2); | 
|  | 120 | int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2); | 
|  | 122 | int ebitmap_get_bit(struct ebitmap *e, unsigned long bit); | 
|  | 123 | int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value); | 
|  | 124 | void ebitmap_destroy(struct ebitmap *e); | 
|  | 125 | int ebitmap_read(struct ebitmap *e, void *fp); | 
|  | 126 |  | 
| Paul Moore | 0275276 | 2006-11-29 13:18:18 -0500 | [diff] [blame] | 127 | #ifdef CONFIG_NETLABEL | 
|  | 128 | int ebitmap_netlbl_export(struct ebitmap *ebmap, | 
|  | 129 | struct netlbl_lsm_secattr_catmap **catmap); | 
|  | 130 | int ebitmap_netlbl_import(struct ebitmap *ebmap, | 
|  | 131 | struct netlbl_lsm_secattr_catmap *catmap); | 
|  | 132 | #else | 
|  | 133 | static inline int ebitmap_netlbl_export(struct ebitmap *ebmap, | 
|  | 134 | struct netlbl_lsm_secattr_catmap **catmap) | 
|  | 135 | { | 
|  | 136 | return -ENOMEM; | 
|  | 137 | } | 
|  | 138 | static inline int ebitmap_netlbl_import(struct ebitmap *ebmap, | 
|  | 139 | struct netlbl_lsm_secattr_catmap *catmap) | 
|  | 140 | { | 
|  | 141 | return -ENOMEM; | 
|  | 142 | } | 
|  | 143 | #endif | 
|  | 144 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | #endif	/* _SS_EBITMAP_H_ */ |