xref: /titanic_51/usr/src/cmd/ndmpd/include/bitmap.h (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * BSD 3 Clause License
8  *
9  * Copyright (c) 2007, The Storage Networking Industry Association.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 	- Redistributions of source code must retain the above copyright
15  *	  notice, this list of conditions and the following disclaimer.
16  *
17  * 	- Redistributions in binary form must reproduce the above copyright
18  *	  notice, this list of conditions and the following disclaimer in
19  *	  the documentation and/or other materials provided with the
20  *	  distribution.
21  *
22  *	- Neither the name of The Storage Networking Industry Association (SNIA)
23  *	  nor the names of its contributors may be used to endorse or promote
24  *	  products derived from this software without specific prior written
25  *	  permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 #ifndef _BITMAP_H_
40 #define	_BITMAP_H_
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #include <sys/errno.h>
47 
48 /*
49  * This interface is designed to provide an abatract data type
50  * for manipulating in-core and on-disk bitmaps.
51  *
52  * When a bitmap is allocated, a descriptor to the bitmap is
53  * returned to the caller.  The descriptor is an integer.  All
54  * functions of the API use this descriptor to locate the
55  * bitmap.
56  *
57  * Each bitmap is divided into chunks (internally).  Each chunk
58  * is BMAP_CHUNK_WORDS words (4K now).  Chunks are kept in an
59  * LRU list for caching.
60  *
61  * There is also a hashing on the chunks for accessing them.
62  * Each hash is an MRU list.
63  *
64  * The interfaces are:
65  *  bm_alloc: To allocate a new bitmap.
66  *  bm_free: To release the bitmap.
67  *  bm_getlen: To get the length of the bitmap.
68  *  bm_getiov: To get the bits specified by the vectors.
69  *  bm_setiov: To set the bits specified by the vectors.
70  *  bm_apply_ifset: Calls a callback function on each set
71  *      bit in the bitmap.
72  *  bm_apply_ifunset: Calls a callback function on each
73  *  	clear bit in the bitmap.
74  *
75  * There are some other interface for simpilicty of programs:
76  *   bm_get To get a range of bits.
77  *   bm_set: To set a range of bits.
78  *   bm_getone: To get one bit only.
79  *   bm_setone: To set one bit only.
80  *   bm_unsetone: To unset one bit only.
81  *
82  * The on-disk bitmap functions are the same except they start
83  * with dbm_*
84  */
85 
86 typedef	u_longlong_t u_quad_t;
87 
88 /*
89  * A vector for setting bits in the bitmap.
90  *     - bmv_base: The starting bit number.
91  *     - bmv_len: Lenght of the vector.
92  *     - bmv_val: Pointer to the new value of bits.
93  */
94 typedef struct bm_iovec {
95 	u_quad_t bmv_base;
96 	u_quad_t bmv_len;
97 	uint_t *bmv_val;
98 } bm_iovec_t;
99 
100 
101 /*
102  * An array of vectors on which the set/get operations
103  * will take place.
104  *     - bmio_iovcnt: Number of entries in the array.
105  *     - bmio_iov: Array of vectors.
106  */
107 typedef struct bm_io {
108 	int bmio_iovcnt;
109 	bm_iovec_t *bmio_iov;
110 } bm_io_t;
111 
112 extern void bm_print(int);
113 
114 /*
115  * External Interface.
116  */
117 extern int bm_alloc(u_quad_t, int);
118 extern int dbm_alloc(char *, u_quad_t, int);
119 
120 extern int bm_free(int);
121 extern int dbm_free(int);
122 
123 extern int bm_realloc(int, u_quad_t);
124 extern int dbm_realloc(int, u_quad_t);
125 
126 extern int bm_setiov(int, bm_io_t *);
127 extern int dbm_setiov(int, bm_io_t *);
128 extern int bm_getiov(int, bm_io_t *);
129 extern int dbm_getiov(int, bm_io_t *);
130 
131 extern int bm_apply_ifset(int, int (*)(), void *);
132 extern int dbm_apply_ifset(int, int (*)(), void *);
133 extern int bm_apply_ifunset(int, int (*)(), void *);
134 extern int dbm_apply_ifunset(int, int (*)(), void *);
135 
136 extern char *dbm_getfname(int);
137 extern u_quad_t bm_getlen(int);
138 extern u_quad_t dbm_getlen(int);
139 
140 extern void dbm_print(int);
141 
142 
143 /*
144  * Statistical and debugging interface.
145  */
146 extern void dbitmap_stats_clear(void);
147 
148 
149 /*
150  * Macros for setting and unsetting only one bit.
151  */
152 #define	bm_setone(bmd, bn)	bm_set((bmd), (bn), 1, 1)
153 #define	dbm_setone(bmd, bn)	dbm_set((bmd), (bn), 1, 1)
154 #define	bm_unsetone(bmd, bn)	bm_set((bmd), (bn), 1, 0)
155 #define	dbm_unsetone(bmd, bn)	dbm_set((bmd), (bn), 1, 0)
156 
157 extern int bm_set(int, u_quad_t, u_quad_t, uint_t);
158 extern int dbm_set(int, u_quad_t, u_quad_t, uint_t);
159 extern int bm_get(int, u_quad_t, u_quad_t, uint_t *);
160 extern int dbm_get(int, u_quad_t, u_quad_t, uint_t *);
161 extern int bm_getone(int, u_quad_t);
162 extern int dbm_getone(int, u_quad_t);
163 
164 #ifdef __cplusplus
165 }
166 #endif
167 #endif /* _BITMAP_H_ */
168