subr_devsw.c revision 1.4.2.2 1 1.4.2.2 nathanw /* $NetBSD: subr_devsw.c,v 1.4.2.2 2002/09/17 21:22:16 nathanw Exp $ */
2 1.4.2.2 nathanw /*-
3 1.4.2.2 nathanw * Copyright (c) 2001,2002 The NetBSD Foundation, Inc.
4 1.4.2.2 nathanw * All rights reserved.
5 1.4.2.2 nathanw *
6 1.4.2.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
7 1.4.2.2 nathanw * by MAEKAWA Masahide <gehenna (at) NetBSD.org>.
8 1.4.2.2 nathanw *
9 1.4.2.2 nathanw * Redistribution and use in source and binary forms, with or without
10 1.4.2.2 nathanw * modification, are permitted provided that the following conditions
11 1.4.2.2 nathanw * are met:
12 1.4.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
13 1.4.2.2 nathanw * notice, this list of conditions and the following disclaimer.
14 1.4.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
15 1.4.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
16 1.4.2.2 nathanw * documentation and/or other materials provided with the distribution.
17 1.4.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
18 1.4.2.2 nathanw * must display the following acknowledgement:
19 1.4.2.2 nathanw * This product includes software developed by the NetBSD
20 1.4.2.2 nathanw * Foundation, Inc. and its contributors.
21 1.4.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.4.2.2 nathanw * contributors may be used to endorse or promote products derived
23 1.4.2.2 nathanw * from this software without specific prior written permission.
24 1.4.2.2 nathanw *
25 1.4.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.4.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.4.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.4.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.4.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.4.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.4.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.4.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.4.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.4.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.4.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
36 1.4.2.2 nathanw */
37 1.4.2.2 nathanw
38 1.4.2.2 nathanw /*
39 1.4.2.2 nathanw * New device switch framework is developing.
40 1.4.2.2 nathanw * So debug options are always turned on.
41 1.4.2.2 nathanw */
42 1.4.2.2 nathanw #ifndef DEVSW_DEBUG
43 1.4.2.2 nathanw #define DEVSW_DEBUG
44 1.4.2.2 nathanw #endif /* DEVSW_DEBUG */
45 1.4.2.2 nathanw
46 1.4.2.2 nathanw #include <sys/param.h>
47 1.4.2.2 nathanw #include <sys/conf.h>
48 1.4.2.2 nathanw #include <sys/malloc.h>
49 1.4.2.2 nathanw #include <sys/systm.h>
50 1.4.2.2 nathanw
51 1.4.2.2 nathanw #ifdef DEVSW_DEBUG
52 1.4.2.2 nathanw #define DPRINTF(x) printf x
53 1.4.2.2 nathanw #else /* DEVSW_DEBUG */
54 1.4.2.2 nathanw #define DPRINTF(x)
55 1.4.2.2 nathanw #endif /* DEVSW_DEBUG */
56 1.4.2.2 nathanw
57 1.4.2.2 nathanw #define MAXDEVSW 4096 /* the maximum of major device number */
58 1.4.2.2 nathanw #define BDEVSW_SIZE (sizeof(struct bdevsw *))
59 1.4.2.2 nathanw #define CDEVSW_SIZE (sizeof(struct cdevsw *))
60 1.4.2.2 nathanw #define DEVSWCONV_SIZE (sizeof(struct devsw_conv))
61 1.4.2.2 nathanw
62 1.4.2.2 nathanw extern const struct bdevsw **bdevsw, *bdevsw0[];
63 1.4.2.2 nathanw extern const struct cdevsw **cdevsw, *cdevsw0[];
64 1.4.2.2 nathanw extern struct devsw_conv *devsw_conv, devsw_conv0[];
65 1.4.2.2 nathanw extern const int sys_bdevsws, sys_cdevsws;
66 1.4.2.2 nathanw extern int max_bdevsws, max_cdevsws, max_devsw_convs;
67 1.4.2.2 nathanw
68 1.4.2.2 nathanw static int bdevsw_attach(const char *, const struct bdevsw *, int *);
69 1.4.2.2 nathanw static int cdevsw_attach(const char *, const struct cdevsw *, int *);
70 1.4.2.2 nathanw
71 1.4.2.2 nathanw int
72 1.4.2.2 nathanw devsw_attach(const char *devname, const struct bdevsw *bdev, int *bmajor,
73 1.4.2.2 nathanw const struct cdevsw *cdev, int *cmajor)
74 1.4.2.2 nathanw {
75 1.4.2.2 nathanw struct devsw_conv *conv;
76 1.4.2.2 nathanw char *name;
77 1.4.2.2 nathanw int error, i;
78 1.4.2.2 nathanw
79 1.4.2.2 nathanw if (devname == NULL || cdev == NULL)
80 1.4.2.2 nathanw return (EINVAL);
81 1.4.2.2 nathanw
82 1.4.2.2 nathanw for (i = 0 ; i < max_devsw_convs ; i++) {
83 1.4.2.2 nathanw conv = &devsw_conv[i];
84 1.4.2.2 nathanw if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
85 1.4.2.2 nathanw continue;
86 1.4.2.2 nathanw
87 1.4.2.2 nathanw if (*bmajor < 0)
88 1.4.2.2 nathanw *bmajor = conv->d_bmajor;
89 1.4.2.2 nathanw if (*cmajor < 0)
90 1.4.2.2 nathanw *cmajor = conv->d_cmajor;
91 1.4.2.2 nathanw
92 1.4.2.2 nathanw if (*bmajor != conv->d_bmajor || *cmajor != conv->d_cmajor)
93 1.4.2.2 nathanw return (EINVAL);
94 1.4.2.2 nathanw if ((*bmajor >= 0 && bdev == NULL) || *cmajor < 0)
95 1.4.2.2 nathanw return (EINVAL);
96 1.4.2.2 nathanw
97 1.4.2.2 nathanw if ((*bmajor >= 0 && bdevsw[*bmajor] != NULL) ||
98 1.4.2.2 nathanw cdevsw[*cmajor] != NULL)
99 1.4.2.2 nathanw return (EEXIST);
100 1.4.2.2 nathanw
101 1.4.2.2 nathanw if (bdev != NULL)
102 1.4.2.2 nathanw bdevsw[*bmajor] = bdev;
103 1.4.2.2 nathanw cdevsw[*cmajor] = cdev;
104 1.4.2.2 nathanw
105 1.4.2.2 nathanw return (0);
106 1.4.2.2 nathanw }
107 1.4.2.2 nathanw
108 1.4.2.2 nathanw error = bdevsw_attach(devname, bdev, bmajor);
109 1.4.2.2 nathanw if (error != 0)
110 1.4.2.2 nathanw return (error);
111 1.4.2.2 nathanw error = cdevsw_attach(devname, cdev, cmajor);
112 1.4.2.2 nathanw if (error != 0) {
113 1.4.2.2 nathanw devsw_detach(bdev, NULL);
114 1.4.2.2 nathanw return (error);
115 1.4.2.2 nathanw }
116 1.4.2.2 nathanw
117 1.4.2.2 nathanw for (i = 0 ; i < max_devsw_convs ; i++) {
118 1.4.2.2 nathanw if (devsw_conv[i].d_name == NULL)
119 1.4.2.2 nathanw break;
120 1.4.2.2 nathanw }
121 1.4.2.2 nathanw if (i == max_devsw_convs) {
122 1.4.2.2 nathanw struct devsw_conv *newptr;
123 1.4.2.2 nathanw int old, new;
124 1.4.2.2 nathanw
125 1.4.2.2 nathanw old = max_devsw_convs;
126 1.4.2.2 nathanw new = old + 1;
127 1.4.2.2 nathanw
128 1.4.2.2 nathanw newptr = malloc(new * DEVSWCONV_SIZE, M_DEVBUF, M_NOWAIT);
129 1.4.2.2 nathanw if (newptr == NULL) {
130 1.4.2.2 nathanw devsw_detach(bdev, cdev);
131 1.4.2.2 nathanw return (ENOMEM);
132 1.4.2.2 nathanw }
133 1.4.2.2 nathanw newptr[old].d_name = NULL;
134 1.4.2.2 nathanw newptr[old].d_bmajor = -1;
135 1.4.2.2 nathanw newptr[old].d_cmajor = -1;
136 1.4.2.2 nathanw memcpy(newptr, devsw_conv, old * DEVSWCONV_SIZE);
137 1.4.2.2 nathanw if (devsw_conv != devsw_conv0)
138 1.4.2.2 nathanw free(devsw_conv, M_DEVBUF);
139 1.4.2.2 nathanw devsw_conv = newptr;
140 1.4.2.2 nathanw max_devsw_convs = new;
141 1.4.2.2 nathanw }
142 1.4.2.2 nathanw
143 1.4.2.2 nathanw name = malloc(strlen(devname) + 1, M_DEVBUF, M_NOWAIT);
144 1.4.2.2 nathanw if (name == NULL) {
145 1.4.2.2 nathanw devsw_detach(bdev, cdev);
146 1.4.2.2 nathanw return (ENOMEM);
147 1.4.2.2 nathanw }
148 1.4.2.2 nathanw strcpy(name, devname);
149 1.4.2.2 nathanw
150 1.4.2.2 nathanw devsw_conv[i].d_name = name;
151 1.4.2.2 nathanw devsw_conv[i].d_bmajor = *bmajor;
152 1.4.2.2 nathanw devsw_conv[i].d_cmajor = *cmajor;
153 1.4.2.2 nathanw
154 1.4.2.2 nathanw return (0);
155 1.4.2.2 nathanw }
156 1.4.2.2 nathanw
157 1.4.2.2 nathanw static int
158 1.4.2.2 nathanw bdevsw_attach(const char *devname, const struct bdevsw *devsw, int *devmajor)
159 1.4.2.2 nathanw {
160 1.4.2.2 nathanw int bmajor, i;
161 1.4.2.2 nathanw
162 1.4.2.2 nathanw if (devsw == NULL)
163 1.4.2.2 nathanw return (0);
164 1.4.2.2 nathanw
165 1.4.2.2 nathanw if (*devmajor < 0) {
166 1.4.2.2 nathanw for (bmajor = sys_bdevsws ; bmajor < max_bdevsws ; bmajor++) {
167 1.4.2.2 nathanw if (bdevsw[bmajor] != NULL)
168 1.4.2.2 nathanw continue;
169 1.4.2.2 nathanw for (i = 0 ; i < max_devsw_convs ; i++) {
170 1.4.2.2 nathanw if (devsw_conv[i].d_bmajor == bmajor)
171 1.4.2.2 nathanw break;
172 1.4.2.2 nathanw }
173 1.4.2.2 nathanw if (i != max_devsw_convs)
174 1.4.2.2 nathanw continue;
175 1.4.2.2 nathanw break;
176 1.4.2.2 nathanw }
177 1.4.2.2 nathanw *devmajor = bmajor;
178 1.4.2.2 nathanw }
179 1.4.2.2 nathanw if (*devmajor >= MAXDEVSW) {
180 1.4.2.2 nathanw #ifdef DEVSW_DEBUG
181 1.4.2.2 nathanw panic("bdevsw_attach: block majors exhausted");
182 1.4.2.2 nathanw #endif /* DEVSW_DEBUG */
183 1.4.2.2 nathanw return (ENOMEM);
184 1.4.2.2 nathanw }
185 1.4.2.2 nathanw
186 1.4.2.2 nathanw if (*devmajor >= max_bdevsws) {
187 1.4.2.2 nathanw const struct bdevsw **newptr;
188 1.4.2.2 nathanw int old, new;
189 1.4.2.2 nathanw
190 1.4.2.2 nathanw old = max_bdevsws;
191 1.4.2.2 nathanw new = *devmajor + 1;
192 1.4.2.2 nathanw
193 1.4.2.2 nathanw newptr = malloc(new * BDEVSW_SIZE, M_DEVBUF, M_NOWAIT);
194 1.4.2.2 nathanw if (newptr == NULL)
195 1.4.2.2 nathanw return (ENOMEM);
196 1.4.2.2 nathanw memset(newptr + old, 0, (new - old) * BDEVSW_SIZE);
197 1.4.2.2 nathanw if (old != 0) {
198 1.4.2.2 nathanw memcpy(newptr, bdevsw, old * BDEVSW_SIZE);
199 1.4.2.2 nathanw if (bdevsw != bdevsw0)
200 1.4.2.2 nathanw free(bdevsw, M_DEVBUF);
201 1.4.2.2 nathanw }
202 1.4.2.2 nathanw bdevsw = newptr;
203 1.4.2.2 nathanw max_bdevsws = new;
204 1.4.2.2 nathanw }
205 1.4.2.2 nathanw
206 1.4.2.2 nathanw if (bdevsw[*devmajor] != NULL)
207 1.4.2.2 nathanw return (EEXIST);
208 1.4.2.2 nathanw
209 1.4.2.2 nathanw bdevsw[*devmajor] = devsw;
210 1.4.2.2 nathanw
211 1.4.2.2 nathanw return (0);
212 1.4.2.2 nathanw }
213 1.4.2.2 nathanw
214 1.4.2.2 nathanw static int
215 1.4.2.2 nathanw cdevsw_attach(const char *devname, const struct cdevsw *devsw, int *devmajor)
216 1.4.2.2 nathanw {
217 1.4.2.2 nathanw int cmajor, i;
218 1.4.2.2 nathanw
219 1.4.2.2 nathanw if (*devmajor < 0) {
220 1.4.2.2 nathanw for (cmajor = sys_cdevsws ; cmajor < max_cdevsws ; cmajor++) {
221 1.4.2.2 nathanw if (cdevsw[cmajor] != NULL)
222 1.4.2.2 nathanw continue;
223 1.4.2.2 nathanw for (i = 0 ; i < max_devsw_convs ; i++) {
224 1.4.2.2 nathanw if (devsw_conv[i].d_cmajor == cmajor)
225 1.4.2.2 nathanw break;
226 1.4.2.2 nathanw }
227 1.4.2.2 nathanw if (i != max_devsw_convs)
228 1.4.2.2 nathanw continue;
229 1.4.2.2 nathanw break;
230 1.4.2.2 nathanw }
231 1.4.2.2 nathanw *devmajor = cmajor;
232 1.4.2.2 nathanw }
233 1.4.2.2 nathanw if (*devmajor >= MAXDEVSW) {
234 1.4.2.2 nathanw #ifdef DEVSW_DEBUG
235 1.4.2.2 nathanw panic("cdevsw_attach: character majors exhausted");
236 1.4.2.2 nathanw #endif /* DEVSW_DEBUG */
237 1.4.2.2 nathanw return (ENOMEM);
238 1.4.2.2 nathanw }
239 1.4.2.2 nathanw
240 1.4.2.2 nathanw if (*devmajor >= max_cdevsws) {
241 1.4.2.2 nathanw const struct cdevsw **newptr;
242 1.4.2.2 nathanw int old, new;
243 1.4.2.2 nathanw
244 1.4.2.2 nathanw old = max_cdevsws;
245 1.4.2.2 nathanw new = *devmajor + 1;
246 1.4.2.2 nathanw
247 1.4.2.2 nathanw newptr = malloc(new * CDEVSW_SIZE, M_DEVBUF, M_NOWAIT);
248 1.4.2.2 nathanw if (newptr == NULL)
249 1.4.2.2 nathanw return (ENOMEM);
250 1.4.2.2 nathanw memset(newptr + old, 0, (new - old) * CDEVSW_SIZE);
251 1.4.2.2 nathanw if (old != 0) {
252 1.4.2.2 nathanw memcpy(newptr, cdevsw, old * CDEVSW_SIZE);
253 1.4.2.2 nathanw if (cdevsw != cdevsw0)
254 1.4.2.2 nathanw free(cdevsw, M_DEVBUF);
255 1.4.2.2 nathanw }
256 1.4.2.2 nathanw cdevsw = newptr;
257 1.4.2.2 nathanw max_cdevsws = new;
258 1.4.2.2 nathanw }
259 1.4.2.2 nathanw
260 1.4.2.2 nathanw if (cdevsw[*devmajor] != NULL)
261 1.4.2.2 nathanw return (EEXIST);
262 1.4.2.2 nathanw
263 1.4.2.2 nathanw cdevsw[*devmajor] = devsw;
264 1.4.2.2 nathanw
265 1.4.2.2 nathanw return (0);
266 1.4.2.2 nathanw }
267 1.4.2.2 nathanw
268 1.4.2.2 nathanw void
269 1.4.2.2 nathanw devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
270 1.4.2.2 nathanw {
271 1.4.2.2 nathanw int i;
272 1.4.2.2 nathanw
273 1.4.2.2 nathanw if (bdev != NULL) {
274 1.4.2.2 nathanw for (i = 0 ; i < max_bdevsws ; i++) {
275 1.4.2.2 nathanw if (bdevsw[i] != bdev)
276 1.4.2.2 nathanw continue;
277 1.4.2.2 nathanw bdevsw[i] = NULL;
278 1.4.2.2 nathanw break;
279 1.4.2.2 nathanw }
280 1.4.2.2 nathanw }
281 1.4.2.2 nathanw if (cdev != NULL) {
282 1.4.2.2 nathanw for (i = 0 ; i < max_cdevsws ; i++) {
283 1.4.2.2 nathanw if (cdevsw[i] != cdev)
284 1.4.2.2 nathanw continue;
285 1.4.2.2 nathanw cdevsw[i] = NULL;
286 1.4.2.2 nathanw break;
287 1.4.2.2 nathanw }
288 1.4.2.2 nathanw }
289 1.4.2.2 nathanw }
290 1.4.2.2 nathanw
291 1.4.2.2 nathanw const struct bdevsw *
292 1.4.2.2 nathanw bdevsw_lookup(dev_t dev)
293 1.4.2.2 nathanw {
294 1.4.2.2 nathanw int bmajor;
295 1.4.2.2 nathanw
296 1.4.2.2 nathanw if (dev == NODEV)
297 1.4.2.2 nathanw return (NULL);
298 1.4.2.2 nathanw bmajor = major(dev);
299 1.4.2.2 nathanw if (bmajor < 0 || bmajor >= max_bdevsws)
300 1.4.2.2 nathanw return (NULL);
301 1.4.2.2 nathanw
302 1.4.2.2 nathanw return (bdevsw[bmajor]);
303 1.4.2.2 nathanw }
304 1.4.2.2 nathanw
305 1.4.2.2 nathanw const struct cdevsw *
306 1.4.2.2 nathanw cdevsw_lookup(dev_t dev)
307 1.4.2.2 nathanw {
308 1.4.2.2 nathanw int cmajor;
309 1.4.2.2 nathanw
310 1.4.2.2 nathanw if (dev == NODEV)
311 1.4.2.2 nathanw return (NULL);
312 1.4.2.2 nathanw cmajor = major(dev);
313 1.4.2.2 nathanw if (cmajor < 0 || cmajor >= max_cdevsws)
314 1.4.2.2 nathanw return (NULL);
315 1.4.2.2 nathanw
316 1.4.2.2 nathanw return (cdevsw[cmajor]);
317 1.4.2.2 nathanw }
318 1.4.2.2 nathanw
319 1.4.2.2 nathanw int
320 1.4.2.2 nathanw bdevsw_lookup_major(const struct bdevsw *bdev)
321 1.4.2.2 nathanw {
322 1.4.2.2 nathanw int bmajor;
323 1.4.2.2 nathanw
324 1.4.2.2 nathanw for (bmajor = 0 ; bmajor < max_bdevsws ; bmajor++) {
325 1.4.2.2 nathanw if (bdevsw[bmajor] == bdev)
326 1.4.2.2 nathanw return (bmajor);
327 1.4.2.2 nathanw }
328 1.4.2.2 nathanw
329 1.4.2.2 nathanw return (-1);
330 1.4.2.2 nathanw }
331 1.4.2.2 nathanw
332 1.4.2.2 nathanw int
333 1.4.2.2 nathanw cdevsw_lookup_major(const struct cdevsw *cdev)
334 1.4.2.2 nathanw {
335 1.4.2.2 nathanw int cmajor;
336 1.4.2.2 nathanw
337 1.4.2.2 nathanw for (cmajor = 0 ; cmajor < max_cdevsws ; cmajor++) {
338 1.4.2.2 nathanw if (cdevsw[cmajor] == cdev)
339 1.4.2.2 nathanw return (cmajor);
340 1.4.2.2 nathanw }
341 1.4.2.2 nathanw
342 1.4.2.2 nathanw return (-1);
343 1.4.2.2 nathanw }
344 1.4.2.2 nathanw
345 1.4.2.2 nathanw /*
346 1.4.2.2 nathanw * Convert from block major number to name.
347 1.4.2.2 nathanw */
348 1.4.2.2 nathanw const char *
349 1.4.2.2 nathanw devsw_blk2name(int bmajor)
350 1.4.2.2 nathanw {
351 1.4.2.2 nathanw int cmajor, i;
352 1.4.2.2 nathanw
353 1.4.2.2 nathanw if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL)
354 1.4.2.2 nathanw return (NULL);
355 1.4.2.2 nathanw
356 1.4.2.2 nathanw for (i = 0 ; i < max_devsw_convs ; i++) {
357 1.4.2.2 nathanw if (devsw_conv[i].d_bmajor != bmajor)
358 1.4.2.2 nathanw continue;
359 1.4.2.2 nathanw cmajor = devsw_conv[i].d_cmajor;
360 1.4.2.2 nathanw if (cmajor < 0 || cmajor >= max_cdevsws ||
361 1.4.2.2 nathanw cdevsw[cmajor] == NULL)
362 1.4.2.2 nathanw return (NULL);
363 1.4.2.2 nathanw return (devsw_conv[i].d_name);
364 1.4.2.2 nathanw }
365 1.4.2.2 nathanw
366 1.4.2.2 nathanw return (NULL);
367 1.4.2.2 nathanw }
368 1.4.2.2 nathanw
369 1.4.2.2 nathanw /*
370 1.4.2.2 nathanw * Convert from device name to block major number.
371 1.4.2.2 nathanw */
372 1.4.2.2 nathanw int
373 1.4.2.2 nathanw devsw_name2blk(const char *name, char *devname, size_t devnamelen)
374 1.4.2.2 nathanw {
375 1.4.2.2 nathanw struct devsw_conv *conv;
376 1.4.2.2 nathanw int bmajor, i;
377 1.4.2.2 nathanw
378 1.4.2.2 nathanw if (name == NULL)
379 1.4.2.2 nathanw return (-1);
380 1.4.2.2 nathanw
381 1.4.2.2 nathanw for (i = 0 ; i < max_devsw_convs ; i++) {
382 1.4.2.2 nathanw conv = &devsw_conv[i];
383 1.4.2.2 nathanw if (conv->d_name == NULL)
384 1.4.2.2 nathanw continue;
385 1.4.2.2 nathanw if (strncmp(conv->d_name, name, strlen(conv->d_name)) != 0)
386 1.4.2.2 nathanw continue;
387 1.4.2.2 nathanw bmajor = conv->d_bmajor;
388 1.4.2.2 nathanw if (bmajor < 0 || bmajor >= max_bdevsws ||
389 1.4.2.2 nathanw bdevsw[bmajor] == NULL)
390 1.4.2.2 nathanw return (-1);
391 1.4.2.2 nathanw if (devname != NULL) {
392 1.4.2.2 nathanw #ifdef DEVSW_DEBUG
393 1.4.2.2 nathanw if (strlen(conv->d_name) >= devnamelen)
394 1.4.2.2 nathanw printf("devsw_name2blk: too short buffer");
395 1.4.2.2 nathanw #endif /* DEVSW_DEBUG */
396 1.4.2.2 nathanw strncpy(devname, conv->d_name, devnamelen);
397 1.4.2.2 nathanw devname[devnamelen - 1] = '\0';
398 1.4.2.2 nathanw }
399 1.4.2.2 nathanw return (bmajor);
400 1.4.2.2 nathanw }
401 1.4.2.2 nathanw
402 1.4.2.2 nathanw return (-1);
403 1.4.2.2 nathanw }
404 1.4.2.2 nathanw
405 1.4.2.2 nathanw /*
406 1.4.2.2 nathanw * Convert from character dev_t to block dev_t.
407 1.4.2.2 nathanw */
408 1.4.2.2 nathanw dev_t
409 1.4.2.2 nathanw devsw_chr2blk(dev_t cdev)
410 1.4.2.2 nathanw {
411 1.4.2.2 nathanw int bmajor, cmajor, i;
412 1.4.2.2 nathanw
413 1.4.2.2 nathanw if (cdevsw_lookup(cdev) == NULL)
414 1.4.2.2 nathanw return (NODEV);
415 1.4.2.2 nathanw
416 1.4.2.2 nathanw cmajor = major(cdev);
417 1.4.2.2 nathanw
418 1.4.2.2 nathanw for (i = 0 ; i < max_devsw_convs ; i++) {
419 1.4.2.2 nathanw if (devsw_conv[i].d_cmajor != cmajor)
420 1.4.2.2 nathanw continue;
421 1.4.2.2 nathanw bmajor = devsw_conv[i].d_bmajor;
422 1.4.2.2 nathanw if (bmajor < 0 || bmajor >= max_bdevsws ||
423 1.4.2.2 nathanw bdevsw[bmajor] == NULL)
424 1.4.2.2 nathanw return (NODEV);
425 1.4.2.2 nathanw return (makedev(bmajor, minor(cdev)));
426 1.4.2.2 nathanw }
427 1.4.2.2 nathanw
428 1.4.2.2 nathanw return (NODEV);
429 1.4.2.2 nathanw }
430 1.4.2.2 nathanw
431 1.4.2.2 nathanw /*
432 1.4.2.2 nathanw * Convert from block dev_t to character dev_t.
433 1.4.2.2 nathanw */
434 1.4.2.2 nathanw dev_t
435 1.4.2.2 nathanw devsw_blk2chr(dev_t bdev)
436 1.4.2.2 nathanw {
437 1.4.2.2 nathanw int bmajor, cmajor, i;
438 1.4.2.2 nathanw
439 1.4.2.2 nathanw if (bdevsw_lookup(bdev) == NULL)
440 1.4.2.2 nathanw return (NODEV);
441 1.4.2.2 nathanw
442 1.4.2.2 nathanw bmajor = major(bdev);
443 1.4.2.2 nathanw
444 1.4.2.2 nathanw for (i = 0 ; i < max_devsw_convs ; i++) {
445 1.4.2.2 nathanw if (devsw_conv[i].d_bmajor != bmajor)
446 1.4.2.2 nathanw continue;
447 1.4.2.2 nathanw cmajor = devsw_conv[i].d_cmajor;
448 1.4.2.2 nathanw if (cmajor < 0 || cmajor >= max_cdevsws ||
449 1.4.2.2 nathanw cdevsw[cmajor] == NULL)
450 1.4.2.2 nathanw return (NODEV);
451 1.4.2.2 nathanw return (makedev(cmajor, minor(bdev)));
452 1.4.2.2 nathanw }
453 1.4.2.2 nathanw
454 1.4.2.2 nathanw return (NODEV);
455 1.4.2.2 nathanw }
456