blob: 6aa7cf6a21979a4fa751d917e3656f2fdd28e41d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * An extensible bitmap is a bitmap that supports an
4 * arbitrary number of bits. Extensible bitmaps are
5 * used to represent sets of values, such as types,
6 * roles, categories, and classes.
7 *
8 * Each extensible bitmap is implemented as a linked
9 * list of bitmap nodes, where each bitmap node has
10 * an explicitly specified starting bit position within
11 * the total bitmap.
12 *
Stephen Smalley7efbb602017-08-17 13:32:36 -040013 * Author : Stephen Smalley, <sds@tycho.nsa.gov>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15#ifndef _SS_EBITMAP_H_
16#define _SS_EBITMAP_H_
17
Paul Moore02752762006-11-29 13:18:18 -050018#include <net/netlabel.h>
19
Waiman Longa767f682013-07-23 17:38:41 -040020#ifdef CONFIG_64BIT
21#define EBITMAP_NODE_SIZE 64
22#else
23#define EBITMAP_NODE_SIZE 32
24#endif
25
26#define EBITMAP_UNIT_NUMS ((EBITMAP_NODE_SIZE-sizeof(void *)-sizeof(u32))\
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090027 / sizeof(unsigned long))
28#define EBITMAP_UNIT_SIZE BITS_PER_LONG
29#define EBITMAP_SIZE (EBITMAP_UNIT_NUMS * EBITMAP_UNIT_SIZE)
30#define EBITMAP_BIT 1ULL
KaiGai Kohei087feb92007-10-03 23:42:56 +090031#define EBITMAP_SHIFT_UNIT_SIZE(x) \
32 (((x) >> EBITMAP_UNIT_SIZE / 2) >> EBITMAP_UNIT_SIZE / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34struct ebitmap_node {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 struct ebitmap_node *next;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090036 unsigned long maps[EBITMAP_UNIT_NUMS];
37 u32 startbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40struct ebitmap {
41 struct ebitmap_node *node; /* first node in the bitmap */
42 u32 highbit; /* highest position in the total bitmap */
43};
44
45#define ebitmap_length(e) ((e)->highbit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090047static inline unsigned int ebitmap_start_positive(struct ebitmap *e,
48 struct ebitmap_node **n)
Stephen Smalley782ebb92005-09-03 15:55:16 -070049{
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090050 unsigned int ofs;
51
52 for (*n = e->node; *n; *n = (*n)->next) {
53 ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
54 if (ofs < EBITMAP_SIZE)
55 return (*n)->startbit + ofs;
56 }
57 return ebitmap_length(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -070058}
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static inline void ebitmap_init(struct ebitmap *e)
61{
62 memset(e, 0, sizeof(*e));
63}
64
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090065static inline unsigned int ebitmap_next_positive(struct ebitmap *e,
66 struct ebitmap_node **n,
67 unsigned int bit)
Stephen Smalley782ebb92005-09-03 15:55:16 -070068{
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090069 unsigned int ofs;
Stephen Smalley782ebb92005-09-03 15:55:16 -070070
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090071 ofs = find_next_bit((*n)->maps, EBITMAP_SIZE, bit - (*n)->startbit + 1);
72 if (ofs < EBITMAP_SIZE)
73 return ofs + (*n)->startbit;
74
75 for (*n = (*n)->next; *n; *n = (*n)->next) {
76 ofs = find_first_bit((*n)->maps, EBITMAP_SIZE);
77 if (ofs < EBITMAP_SIZE)
78 return ofs + (*n)->startbit;
79 }
80 return ebitmap_length(e);
Stephen Smalley782ebb92005-09-03 15:55:16 -070081}
82
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090083#define EBITMAP_NODE_INDEX(node, bit) \
84 (((bit) - (node)->startbit) / EBITMAP_UNIT_SIZE)
85#define EBITMAP_NODE_OFFSET(node, bit) \
86 (((bit) - (node)->startbit) % EBITMAP_UNIT_SIZE)
87
88static inline int ebitmap_node_get_bit(struct ebitmap_node *n,
Stephen Smalley782ebb92005-09-03 15:55:16 -070089 unsigned int bit)
90{
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +090091 unsigned int index = EBITMAP_NODE_INDEX(n, bit);
92 unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
93
94 BUG_ON(index >= EBITMAP_UNIT_NUMS);
95 if ((n->maps[index] & (EBITMAP_BIT << ofs)))
Stephen Smalley782ebb92005-09-03 15:55:16 -070096 return 1;
97 return 0;
98}
99
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900100static inline void ebitmap_node_set_bit(struct ebitmap_node *n,
101 unsigned int bit)
102{
103 unsigned int index = EBITMAP_NODE_INDEX(n, bit);
104 unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
105
106 BUG_ON(index >= EBITMAP_UNIT_NUMS);
107 n->maps[index] |= (EBITMAP_BIT << ofs);
108}
109
110static inline void ebitmap_node_clr_bit(struct ebitmap_node *n,
111 unsigned int bit)
112{
113 unsigned int index = EBITMAP_NODE_INDEX(n, bit);
114 unsigned int ofs = EBITMAP_NODE_OFFSET(n, bit);
115
116 BUG_ON(index >= EBITMAP_UNIT_NUMS);
117 n->maps[index] &= ~(EBITMAP_BIT << ofs);
118}
119
120#define ebitmap_for_each_positive_bit(e, n, bit) \
121 for (bit = ebitmap_start_positive(e, &n); \
122 bit < ebitmap_length(e); \
123 bit = ebitmap_next_positive(e, &n, bit)) \
Stephen Smalley782ebb92005-09-03 15:55:16 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125int ebitmap_cmp(struct ebitmap *e1, struct ebitmap *e2);
126int ebitmap_cpy(struct ebitmap *dst, struct ebitmap *src);
Waiman Longfee71142013-07-23 17:38:41 -0400127int ebitmap_contains(struct ebitmap *e1, struct ebitmap *e2, u32 last_e2bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128int ebitmap_get_bit(struct ebitmap *e, unsigned long bit);
129int ebitmap_set_bit(struct ebitmap *e, unsigned long bit, int value);
130void ebitmap_destroy(struct ebitmap *e);
131int ebitmap_read(struct ebitmap *e, void *fp);
Eric Pariscee74f42010-10-13 17:50:25 -0400132int ebitmap_write(struct ebitmap *e, void *fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Paul Moore02752762006-11-29 13:18:18 -0500134#ifdef CONFIG_NETLABEL
135int ebitmap_netlbl_export(struct ebitmap *ebmap,
Paul Moore4fbe63d2014-08-01 11:17:37 -0400136 struct netlbl_lsm_catmap **catmap);
Paul Moore02752762006-11-29 13:18:18 -0500137int ebitmap_netlbl_import(struct ebitmap *ebmap,
Paul Moore4fbe63d2014-08-01 11:17:37 -0400138 struct netlbl_lsm_catmap *catmap);
Paul Moore02752762006-11-29 13:18:18 -0500139#else
140static inline int ebitmap_netlbl_export(struct ebitmap *ebmap,
Paul Moore4fbe63d2014-08-01 11:17:37 -0400141 struct netlbl_lsm_catmap **catmap)
Paul Moore02752762006-11-29 13:18:18 -0500142{
143 return -ENOMEM;
144}
145static inline int ebitmap_netlbl_import(struct ebitmap *ebmap,
Paul Moore4fbe63d2014-08-01 11:17:37 -0400146 struct netlbl_lsm_catmap *catmap)
Paul Moore02752762006-11-29 13:18:18 -0500147{
148 return -ENOMEM;
149}
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif /* _SS_EBITMAP_H_ */