1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/modctl.h>
#include <sys/modhash.h>
#include <sys/atomic.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/t_lock.h>
/*
* This module provides the framework that manage STREAMS modules.
* fmodsw_alloc() is called from modconf.c as a result of a module calling
* mod_install() and fmodsw_free() is called as the result of the module
* calling mod_remove().
* fmodsw_find() will find the fmodsw_impl_t structure relating to a named
* module. There is no equivalent of driver major numbers for modules; the
* the database of fmodsw_impl_t structures is purely keyed by name and
* is hence a hash table to keep lookup cost to a minimum.
*/
/*
* fmodsw_hash is the hash table that will be used to map module names to
* their fmodsw_impl_t structures. The hash function requires that the value is
* a power of 2 so this definition specifies the log of the hash table size.
*/
#define FMODSW_LOG_HASHSZ 8
/*
* Hash table and associated reader-writer lock
*
* NOTE: Because the lock is global data, it is initialized to zero and hence
* a call to rw_init() is not required. Similarly all the pointers in
* the hash table will be implicitly initialized to NULL.
*/
#define FMODSW_HASHSZ (1 << FMODSW_LOG_HASHSZ)
static fmodsw_impl_t *fmodsw_hash[FMODSW_HASHSZ];
static krwlock_t fmodsw_lock;
/*
* Debug code:
*
* This is not conditionally compiled since it may be useful to third
* parties when developing new modules.
*/
#define BUFSZ 512
#define FMODSW_INIT 0x00000001
#define FMODSW_REGISTER 0x00000002
#define FMODSW_UNREGISTER 0x00000004
#define FMODSW_FIND 0x00000008
uint32_t fmodsw_debug_flags = 0x00000000;
static void fmodsw_dprintf(uint_t flag, const char *fmt, ...) __KPRINTFLIKE(2);
/* PRINTFLIKE2 */
static void
i_fmodsw_dprintf(uint_t flag, const char *fmt, ...)
{
va_list alist;
char buf[BUFSZ + 1];
char *ptr;
if (fmodsw_debug_flags & flag) {
va_start(alist, fmt);
ptr = buf;
(void) sprintf(ptr, "strmod debug: ");
ptr += strlen(buf);
(void) vsnprintf(ptr, buf + BUFSZ - ptr, fmt, alist);
printf(buf);
va_end(alist);
}
}
/*
* Local functions:
*/
#define FMODSW_HASH(_key) \
(uint_t)(((_key[0] << 4) | (_key[1] & 0x0f)) & (FMODSW_HASHSZ - 1))
#define FMODSW_KEYCMP(_k1, _k2, _match) \
{ \
char *p1 = (char *)(_k1); \
char *p2 = (char *)(_k2); \
\
while (*p1 == *p2++) { \
if (*p1++ == '\0') { \
goto _match; \
} \
} \
}
static int
i_fmodsw_hash_insert(fmodsw_impl_t *fp)
{
uint_t bucket;
fmodsw_impl_t **pp;
fmodsw_impl_t *p;
ASSERT(rw_write_held(&fmodsw_lock));
bucket = FMODSW_HASH(fp->f_name);
for (pp = &(fmodsw_hash[bucket]); (p = *pp) != NULL;
pp = &(p->f_next))
FMODSW_KEYCMP(p->f_name, fp->f_name, found);
fp->f_next = p;
*pp = fp;
return (0);
found:
return (EEXIST);
}
static int
i_fmodsw_hash_remove(const char *name, fmodsw_impl_t **fpp)
{
uint_t bucket;
fmodsw_impl_t **pp;
fmodsw_impl_t *p;
ASSERT(rw_write_held(&fmodsw_lock));
bucket = FMODSW_HASH(name);
for (pp = &(fmodsw_hash[bucket]); (p = *pp) != NULL;
pp = &(p->f_next))
FMODSW_KEYCMP(p->f_name, name, found);
return (ENOENT);
found:
if (p->f_ref > 0)
return (EBUSY);
*pp = p->f_next;
*fpp = p;
return (0);
}
static int
i_fmodsw_hash_find(const char *name, fmodsw_impl_t **fpp)
{
uint_t bucket;
fmodsw_impl_t *p;
int rc = 0;
ASSERT(rw_read_held(&fmodsw_lock));
bucket = FMODSW_HASH(name);
for (p = fmodsw_hash[bucket]; p != NULL; p = p->f_next)
FMODSW_KEYCMP(p->f_name, name, found);
rc = ENOENT;
found:
*fpp = p;
#ifdef DEBUG
if (p != NULL)
p->f_hits++;
#endif /* DEBUG */
return (rc);
}
/*
* Exported functions:
*/
int
fmodsw_register(const char *name, struct streamtab *str, int flag)
{
fmodsw_impl_t *fp;
int len;
int err;
uint_t qflag;
uint_t sqtype;
if ((len = strlen(name)) > FMNAMESZ)
return (EINVAL);
if ((fp = kmem_zalloc(sizeof (fmodsw_impl_t), KM_NOSLEEP)) == NULL)
return (ENOMEM);
(void) strncpy(fp->f_name, name, len);
fp->f_name[len] = '\0';
if ((err = devflg_to_qflag(str, flag, &qflag, &sqtype)) != 0)
goto failed;
fp->f_str = str;
fp->f_qflag = qflag;
fp->f_sqtype = sqtype;
if (qflag & (QPERMOD | QMTOUTPERIM))
fp->f_dmp = hold_dm(str, qflag, sqtype);
rw_enter(&fmodsw_lock, RW_WRITER);
if ((err = i_fmodsw_hash_insert(fp)) != 0) {
rw_exit(&fmodsw_lock);
goto failed;
}
rw_exit(&fmodsw_lock);
i_fmodsw_dprintf(FMODSW_REGISTER, "registered module '%s'\n", name);
return (0);
failed:
i_fmodsw_dprintf(FMODSW_REGISTER, "failed to register module '%s'\n",
name);
if (fp->f_dmp != NULL)
rele_dm(fp->f_dmp);
kmem_free(fp, sizeof (fmodsw_impl_t));
return (err);
}
int
fmodsw_unregister(const char *name)
{
fmodsw_impl_t *fp;
int err;
rw_enter(&fmodsw_lock, RW_WRITER);
if ((err = i_fmodsw_hash_remove(name, &fp)) != 0) {
rw_exit(&fmodsw_lock);
goto failed;
}
rw_exit(&fmodsw_lock);
if (fp->f_dmp != NULL)
rele_dm(fp->f_dmp);
kmem_free(fp, sizeof (fmodsw_impl_t));
i_fmodsw_dprintf(FMODSW_UNREGISTER, "unregistered module '%s'\n",
name);
return (0);
failed:
i_fmodsw_dprintf(FMODSW_UNREGISTER, "failed to unregister module "
"'%s'\n", name);
return (err);
}
fmodsw_impl_t *
fmodsw_find(const char *name, fmodsw_flags_t flags)
{
fmodsw_impl_t *fp;
int id;
try_again:
rw_enter(&fmodsw_lock, RW_READER);
if (i_fmodsw_hash_find(name, &fp) == 0) {
if (flags & FMODSW_HOLD) {
atomic_inc_32(&(fp->f_ref)); /* lock must be held */
ASSERT(fp->f_ref > 0);
}
rw_exit(&fmodsw_lock);
return (fp);
}
rw_exit(&fmodsw_lock);
if (flags & FMODSW_LOAD) {
if ((id = modload("strmod", (char *)name)) != -1) {
i_fmodsw_dprintf(FMODSW_FIND,
"module '%s' loaded: id = %d\n", name, id);
goto try_again;
}
}
return (NULL);
}
void
fmodsw_rele(fmodsw_impl_t *fp)
{
ASSERT(fp->f_ref > 0);
atomic_dec_32(&(fp->f_ref));
}
|