bio.c revision 1.1.22.2 1 /* $NetBSD: bio.c,v 1.1.22.2 2007/10/15 05:09:53 riz Exp $ */
2 /* $OpenBSD: bio.c,v 1.9 2007/03/20 02:35:55 marco Exp $ */
3
4 /*
5 * Copyright (c) 2002 Niklas Hallqvist. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /* A device controller ioctl tunnelling device. */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: bio.c,v 1.1.22.2 2007/10/15 05:09:53 riz Exp $");
32
33 #include <sys/param.h>
34 #include <sys/conf.h>
35 #include <sys/device.h>
36 #include <sys/event.h>
37 #include <sys/ioctl.h>
38 #include <sys/malloc.h>
39 #include <sys/queue.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42
43 #include <dev/biovar.h>
44
45 struct bio_mapping {
46 LIST_ENTRY(bio_mapping) bm_link;
47 struct device *bm_dev;
48 int (*bm_ioctl)(struct device *, u_long, caddr_t);
49 };
50
51 LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
52
53 void bioattach(int);
54 int bioclose(dev_t, int, int, struct proc *);
55 int bioioctl(dev_t, u_long, caddr_t, int, struct proc *);
56 int bioopen(dev_t, int, int, struct proc *);
57
58 int bio_delegate_ioctl(struct bio_mapping *, u_long, void *);
59 struct bio_mapping *bio_lookup(char *);
60 int bio_validate(void *);
61
62 const struct cdevsw bio_cdevsw = {
63 bioopen, bioclose, noread, nowrite, bioioctl,
64 nostop, notty, nopoll, nommap, nokqfilter, 0
65 };
66
67
68 void
69 bioattach(int nunits)
70 {
71 }
72
73 int
74 bioopen(dev_t dev, int flags, int mode, struct proc *p)
75 {
76 return (0);
77 }
78
79 int
80 bioclose(dev_t dev, int flags, int mode, struct proc *p)
81 {
82 return (0);
83 }
84
85 int
86 bioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
87 {
88 struct bio_locate *locate;
89 struct bio_common *common;
90 char name[16];
91 int error;
92
93 switch(cmd) {
94 case BIOCLOCATE:
95 case BIOCINQ:
96 case BIOCDISK:
97 case BIOCVOL:
98 case BIOCBLINK:
99 case BIOCSETSTATE:
100 case BIOCALARM:
101 break;
102 default:
103 return ENOTTY;
104 }
105
106 switch (cmd) {
107 case BIOCLOCATE:
108 locate = (struct bio_locate *)addr;
109 error = copyinstr(locate->bl_name, name, 16, NULL);
110 if (error != 0)
111 return (error);
112 locate->bl_cookie = bio_lookup(name);
113 if (locate->bl_cookie == NULL)
114 return (ENOENT);
115 break;
116
117 default:
118 common = (struct bio_common *)addr;
119 if (!bio_validate(common->bc_cookie)) {
120 return (ENOENT);
121 }
122 error = bio_delegate_ioctl(
123 (struct bio_mapping *)common->bc_cookie, cmd, addr);
124 return (error);
125 }
126 return (0);
127 }
128
129 int
130 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, caddr_t))
131 {
132 struct bio_mapping *bm;
133
134 MALLOC(bm, struct bio_mapping *, sizeof *bm, M_DEVBUF, M_NOWAIT);
135 if (bm == NULL)
136 return (ENOMEM);
137 bm->bm_dev = dev;
138 bm->bm_ioctl = ioctl;
139 LIST_INSERT_HEAD(&bios, bm, bm_link);
140 return (0);
141 }
142
143 void
144 bio_unregister(struct device *dev)
145 {
146 struct bio_mapping *bm, *next;
147
148 for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
149 next = LIST_NEXT(bm, bm_link);
150
151 if (dev == bm->bm_dev) {
152 LIST_REMOVE(bm, bm_link);
153 free(bm, M_DEVBUF);
154 }
155 }
156 }
157
158 struct bio_mapping *
159 bio_lookup(char *name)
160 {
161 struct bio_mapping *bm;
162
163 LIST_FOREACH(bm, &bios, bm_link) {
164 if (strcmp(name, bm->bm_dev->dv_xname) == 0) {
165 return (bm);
166 }
167 }
168 return (NULL);
169 }
170
171 int
172 bio_validate(void *cookie)
173 {
174 struct bio_mapping *bm;
175
176 LIST_FOREACH(bm, &bios, bm_link) {
177 if (bm == cookie) {
178 return (1);
179 }
180 }
181 return (0);
182 }
183
184 int
185 bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, void *addr)
186 {
187
188 return (bm->bm_ioctl(bm->bm_dev, cmd, addr));
189 }
190 /* $NetBSD: bio.c,v 1.1.22.2 2007/10/15 05:09:53 riz Exp $ */
191 /* $OpenBSD: bio.c,v 1.9 2007/03/20 02:35:55 marco Exp $ */
192
193 /*
194 * Copyright (c) 2002 Niklas Hallqvist. All rights reserved.
195 *
196 * Redistribution and use in source and binary forms, with or without
197 * modification, are permitted provided that the following conditions
198 * are met:
199 * 1. Redistributions of source code must retain the above copyright
200 * notice, this list of conditions and the following disclaimer.
201 * 2. Redistributions in binary form must reproduce the above copyright
202 * notice, this list of conditions and the following disclaimer in the
203 * documentation and/or other materials provided with the distribution.
204 *
205 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
206 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
207 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
209 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
210 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
211 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
212 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
213 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
214 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
215 */
216
217 /* A device controller ioctl tunnelling device. */
218
219 #include <sys/cdefs.h>
220 __KERNEL_RCSID(0, "$NetBSD: bio.c,v 1.1.22.2 2007/10/15 05:09:53 riz Exp $");
221
222 #include <sys/param.h>
223 #include <sys/conf.h>
224 #include <sys/device.h>
225 #include <sys/event.h>
226 #include <sys/ioctl.h>
227 #include <sys/malloc.h>
228 #include <sys/queue.h>
229 #include <sys/systm.h>
230 #include <sys/proc.h>
231
232 #include <dev/biovar.h>
233
234 struct bio_mapping {
235 LIST_ENTRY(bio_mapping) bm_link;
236 struct device *bm_dev;
237 int (*bm_ioctl)(struct device *, u_long, caddr_t);
238 };
239
240 LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
241
242 void bioattach(int);
243 int bioclose(dev_t, int, int, struct proc *);
244 int bioioctl(dev_t, u_long, caddr_t, int, struct proc *);
245 int bioopen(dev_t, int, int, struct proc *);
246
247 int bio_delegate_ioctl(struct bio_mapping *, u_long, void *);
248 struct bio_mapping *bio_lookup(char *);
249 int bio_validate(void *);
250
251 const struct cdevsw bio_cdevsw = {
252 bioopen, bioclose, noread, nowrite, bioioctl,
253 nostop, notty, nopoll, nommap, nokqfilter, 0
254 };
255
256
257 void
258 bioattach(int nunits)
259 {
260 }
261
262 int
263 bioopen(dev_t dev, int flags, int mode, struct proc *p)
264 {
265 return (0);
266 }
267
268 int
269 bioclose(dev_t dev, int flags, int mode, struct proc *p)
270 {
271 return (0);
272 }
273
274 int
275 bioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
276 {
277 struct bio_locate *locate;
278 struct bio_common *common;
279 char name[16];
280 int error;
281
282 switch(cmd) {
283 case BIOCLOCATE:
284 case BIOCINQ:
285 case BIOCDISK:
286 case BIOCVOL:
287 case BIOCBLINK:
288 case BIOCSETSTATE:
289 case BIOCALARM:
290 break;
291 default:
292 return ENOTTY;
293 }
294
295 switch (cmd) {
296 case BIOCLOCATE:
297 locate = (struct bio_locate *)addr;
298 error = copyinstr(locate->bl_name, name, 16, NULL);
299 if (error != 0)
300 return (error);
301 locate->bl_cookie = bio_lookup(name);
302 if (locate->bl_cookie == NULL)
303 return (ENOENT);
304 break;
305
306 default:
307 common = (struct bio_common *)addr;
308 if (!bio_validate(common->bc_cookie)) {
309 return (ENOENT);
310 }
311 error = bio_delegate_ioctl(
312 (struct bio_mapping *)common->bc_cookie, cmd, addr);
313 return (error);
314 }
315 return (0);
316 }
317
318 int
319 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, caddr_t))
320 {
321 struct bio_mapping *bm;
322
323 MALLOC(bm, struct bio_mapping *, sizeof *bm, M_DEVBUF, M_NOWAIT);
324 if (bm == NULL)
325 return (ENOMEM);
326 bm->bm_dev = dev;
327 bm->bm_ioctl = ioctl;
328 LIST_INSERT_HEAD(&bios, bm, bm_link);
329 return (0);
330 }
331
332 void
333 bio_unregister(struct device *dev)
334 {
335 struct bio_mapping *bm, *next;
336
337 for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
338 next = LIST_NEXT(bm, bm_link);
339
340 if (dev == bm->bm_dev) {
341 LIST_REMOVE(bm, bm_link);
342 free(bm, M_DEVBUF);
343 }
344 }
345 }
346
347 struct bio_mapping *
348 bio_lookup(char *name)
349 {
350 struct bio_mapping *bm;
351
352 LIST_FOREACH(bm, &bios, bm_link) {
353 if (strcmp(name, bm->bm_dev->dv_xname) == 0) {
354 return (bm);
355 }
356 }
357 return (NULL);
358 }
359
360 int
361 bio_validate(void *cookie)
362 {
363 struct bio_mapping *bm;
364
365 LIST_FOREACH(bm, &bios, bm_link) {
366 if (bm == cookie) {
367 return (1);
368 }
369 }
370 return (0);
371 }
372
373 int
374 bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, void *addr)
375 {
376
377 return (bm->bm_ioctl(bm->bm_dev, cmd, addr));
378 }
379 /* $NetBSD: bio.c,v 1.1.22.2 2007/10/15 05:09:53 riz Exp $ */
380 /* $OpenBSD: bio.c,v 1.9 2007/03/20 02:35:55 marco Exp $ */
381
382 /*
383 * Copyright (c) 2002 Niklas Hallqvist. All rights reserved.
384 *
385 * Redistribution and use in source and binary forms, with or without
386 * modification, are permitted provided that the following conditions
387 * are met:
388 * 1. Redistributions of source code must retain the above copyright
389 * notice, this list of conditions and the following disclaimer.
390 * 2. Redistributions in binary form must reproduce the above copyright
391 * notice, this list of conditions and the following disclaimer in the
392 * documentation and/or other materials provided with the distribution.
393 *
394 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
395 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
396 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
397 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
398 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
399 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
400 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
401 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
402 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
403 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
404 */
405
406 /* A device controller ioctl tunnelling device. */
407
408 #include <sys/cdefs.h>
409 __KERNEL_RCSID(0, "$NetBSD: bio.c,v 1.1.22.2 2007/10/15 05:09:53 riz Exp $");
410
411 #include <sys/param.h>
412 #include <sys/conf.h>
413 #include <sys/device.h>
414 #include <sys/event.h>
415 #include <sys/ioctl.h>
416 #include <sys/malloc.h>
417 #include <sys/queue.h>
418 #include <sys/systm.h>
419 #include <sys/proc.h>
420
421 #include <dev/biovar.h>
422
423 struct bio_mapping {
424 LIST_ENTRY(bio_mapping) bm_link;
425 struct device *bm_dev;
426 int (*bm_ioctl)(struct device *, u_long, caddr_t);
427 };
428
429 LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
430
431 void bioattach(int);
432 int bioclose(dev_t, int, int, struct proc *);
433 int bioioctl(dev_t, u_long, caddr_t, int, struct proc *);
434 int bioopen(dev_t, int, int, struct proc *);
435
436 int bio_delegate_ioctl(struct bio_mapping *, u_long, void *);
437 struct bio_mapping *bio_lookup(char *);
438 int bio_validate(void *);
439
440 const struct cdevsw bio_cdevsw = {
441 bioopen, bioclose, noread, nowrite, bioioctl,
442 nostop, notty, nopoll, nommap, nokqfilter, 0
443 };
444
445
446 void
447 bioattach(int nunits)
448 {
449 }
450
451 int
452 bioopen(dev_t dev, int flags, int mode, struct proc *p)
453 {
454 return (0);
455 }
456
457 int
458 bioclose(dev_t dev, int flags, int mode, struct proc *p)
459 {
460 return (0);
461 }
462
463 int
464 bioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
465 {
466 struct bio_locate *locate;
467 struct bio_common *common;
468 char name[16];
469 int error;
470
471 switch(cmd) {
472 case BIOCLOCATE:
473 case BIOCINQ:
474 case BIOCDISK:
475 case BIOCVOL:
476 case BIOCBLINK:
477 case BIOCSETSTATE:
478 case BIOCALARM:
479 break;
480 default:
481 return ENOTTY;
482 }
483
484 switch (cmd) {
485 case BIOCLOCATE:
486 locate = (struct bio_locate *)addr;
487 error = copyinstr(locate->bl_name, name, 16, NULL);
488 if (error != 0)
489 return (error);
490 locate->bl_cookie = bio_lookup(name);
491 if (locate->bl_cookie == NULL)
492 return (ENOENT);
493 break;
494
495 default:
496 common = (struct bio_common *)addr;
497 if (!bio_validate(common->bc_cookie)) {
498 return (ENOENT);
499 }
500 error = bio_delegate_ioctl(
501 (struct bio_mapping *)common->bc_cookie, cmd, addr);
502 return (error);
503 }
504 return (0);
505 }
506
507 int
508 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, caddr_t))
509 {
510 struct bio_mapping *bm;
511
512 MALLOC(bm, struct bio_mapping *, sizeof *bm, M_DEVBUF, M_NOWAIT);
513 if (bm == NULL)
514 return (ENOMEM);
515 bm->bm_dev = dev;
516 bm->bm_ioctl = ioctl;
517 LIST_INSERT_HEAD(&bios, bm, bm_link);
518 return (0);
519 }
520
521 void
522 bio_unregister(struct device *dev)
523 {
524 struct bio_mapping *bm, *next;
525
526 for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
527 next = LIST_NEXT(bm, bm_link);
528
529 if (dev == bm->bm_dev) {
530 LIST_REMOVE(bm, bm_link);
531 free(bm, M_DEVBUF);
532 }
533 }
534 }
535
536 struct bio_mapping *
537 bio_lookup(char *name)
538 {
539 struct bio_mapping *bm;
540
541 LIST_FOREACH(bm, &bios, bm_link) {
542 if (strcmp(name, bm->bm_dev->dv_xname) == 0) {
543 return (bm);
544 }
545 }
546 return (NULL);
547 }
548
549 int
550 bio_validate(void *cookie)
551 {
552 struct bio_mapping *bm;
553
554 LIST_FOREACH(bm, &bios, bm_link) {
555 if (bm == cookie) {
556 return (1);
557 }
558 }
559 return (0);
560 }
561
562 int
563 bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, void *addr)
564 {
565
566 return (bm->bm_ioctl(bm->bm_dev, cmd, addr));
567 }
568