bio.c revision 1.1.6.2 1 1.1.6.2 ad /* $NetBSD: bio.c,v 1.1.6.2 2007/06/09 21:37:10 ad Exp $ */
2 1.1.6.2 ad /* $OpenBSD: bio.c,v 1.9 2007/03/20 02:35:55 marco Exp $ */
3 1.1.6.2 ad
4 1.1.6.2 ad /*
5 1.1.6.2 ad * Copyright (c) 2002 Niklas Hallqvist. All rights reserved.
6 1.1.6.2 ad *
7 1.1.6.2 ad * Redistribution and use in source and binary forms, with or without
8 1.1.6.2 ad * modification, are permitted provided that the following conditions
9 1.1.6.2 ad * are met:
10 1.1.6.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.1.6.2 ad * notice, this list of conditions and the following disclaimer.
12 1.1.6.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.6.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.1.6.2 ad * documentation and/or other materials provided with the distribution.
15 1.1.6.2 ad *
16 1.1.6.2 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.6.2 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.6.2 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.6.2 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.6.2 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1.6.2 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1.6.2 ad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1.6.2 ad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1.6.2 ad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1.6.2 ad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1.6.2 ad */
27 1.1.6.2 ad
28 1.1.6.2 ad /* A device controller ioctl tunnelling device. */
29 1.1.6.2 ad
30 1.1.6.2 ad #include <sys/cdefs.h>
31 1.1.6.2 ad __KERNEL_RCSID(0, "$NetBSD: bio.c,v 1.1.6.2 2007/06/09 21:37:10 ad Exp $");
32 1.1.6.2 ad
33 1.1.6.2 ad #include <sys/param.h>
34 1.1.6.2 ad #include <sys/conf.h>
35 1.1.6.2 ad #include <sys/device.h>
36 1.1.6.2 ad #include <sys/event.h>
37 1.1.6.2 ad #include <sys/ioctl.h>
38 1.1.6.2 ad #include <sys/malloc.h>
39 1.1.6.2 ad #include <sys/queue.h>
40 1.1.6.2 ad #include <sys/systm.h>
41 1.1.6.2 ad #include <sys/mutex.h>
42 1.1.6.2 ad #include <sys/proc.h>
43 1.1.6.2 ad #include <sys/kauth.h>
44 1.1.6.2 ad
45 1.1.6.2 ad #include <dev/biovar.h>
46 1.1.6.2 ad
47 1.1.6.2 ad struct bio_mapping {
48 1.1.6.2 ad LIST_ENTRY(bio_mapping) bm_link;
49 1.1.6.2 ad struct device *bm_dev;
50 1.1.6.2 ad int (*bm_ioctl)(struct device *, u_long, void *);
51 1.1.6.2 ad };
52 1.1.6.2 ad
53 1.1.6.2 ad LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
54 1.1.6.2 ad static kmutex_t bio_lock;
55 1.1.6.2 ad
56 1.1.6.2 ad void bioattach(int);
57 1.1.6.2 ad int bioclose(dev_t, int, int, struct lwp *);
58 1.1.6.2 ad int bioioctl(dev_t, u_long, void *, int, struct lwp *);
59 1.1.6.2 ad int bioopen(dev_t, int, int, struct lwp *);
60 1.1.6.2 ad
61 1.1.6.2 ad int bio_delegate_ioctl(struct bio_mapping *, u_long, void *);
62 1.1.6.2 ad struct bio_mapping *bio_lookup(char *);
63 1.1.6.2 ad int bio_validate(void *);
64 1.1.6.2 ad
65 1.1.6.2 ad const struct cdevsw bio_cdevsw = {
66 1.1.6.2 ad bioopen, bioclose, noread, nowrite, bioioctl,
67 1.1.6.2 ad nostop, notty, nopoll, nommap, nokqfilter, 0
68 1.1.6.2 ad };
69 1.1.6.2 ad
70 1.1.6.2 ad
71 1.1.6.2 ad void
72 1.1.6.2 ad bioattach(int nunits)
73 1.1.6.2 ad {
74 1.1.6.2 ad mutex_init(&bio_lock, MUTEX_DRIVER, IPL_BIO);
75 1.1.6.2 ad }
76 1.1.6.2 ad
77 1.1.6.2 ad int
78 1.1.6.2 ad bioopen(dev_t dev, int flags, int mode, struct lwp *l)
79 1.1.6.2 ad {
80 1.1.6.2 ad return (0);
81 1.1.6.2 ad }
82 1.1.6.2 ad
83 1.1.6.2 ad int
84 1.1.6.2 ad bioclose(dev_t dev, int flags, int mode, struct lwp *l)
85 1.1.6.2 ad {
86 1.1.6.2 ad return (0);
87 1.1.6.2 ad }
88 1.1.6.2 ad
89 1.1.6.2 ad int
90 1.1.6.2 ad bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
91 1.1.6.2 ad {
92 1.1.6.2 ad struct bio_locate *locate;
93 1.1.6.2 ad struct bio_common *common;
94 1.1.6.2 ad char name[16];
95 1.1.6.2 ad int error;
96 1.1.6.2 ad
97 1.1.6.2 ad switch(cmd) {
98 1.1.6.2 ad case BIOCLOCATE:
99 1.1.6.2 ad case BIOCINQ:
100 1.1.6.2 ad case BIOCDISK:
101 1.1.6.2 ad case BIOCVOL:
102 1.1.6.2 ad error = kauth_authorize_device_passthru(l->l_cred, dev,
103 1.1.6.2 ad KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
104 1.1.6.2 ad if (error)
105 1.1.6.2 ad return (error);
106 1.1.6.2 ad break;
107 1.1.6.2 ad case BIOCBLINK:
108 1.1.6.2 ad case BIOCSETSTATE:
109 1.1.6.2 ad error = kauth_authorize_device_passthru(l->l_cred, dev,
110 1.1.6.2 ad KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
111 1.1.6.2 ad if (error)
112 1.1.6.2 ad return (error);
113 1.1.6.2 ad break;
114 1.1.6.2 ad case BIOCALARM: {
115 1.1.6.2 ad struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
116 1.1.6.2 ad switch (alarm->ba_opcode) {
117 1.1.6.2 ad case BIOC_SADISABLE:
118 1.1.6.2 ad case BIOC_SAENABLE:
119 1.1.6.2 ad case BIOC_SASILENCE:
120 1.1.6.2 ad case BIOC_SATEST:
121 1.1.6.2 ad error = kauth_authorize_device_passthru(l->l_cred, dev,
122 1.1.6.2 ad KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
123 1.1.6.2 ad if (error)
124 1.1.6.2 ad return (error);
125 1.1.6.2 ad break;
126 1.1.6.2 ad case BIOC_GASTATUS:
127 1.1.6.2 ad error = kauth_authorize_device_passthru(l->l_cred, dev,
128 1.1.6.2 ad KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
129 1.1.6.2 ad if (error)
130 1.1.6.2 ad return (error);
131 1.1.6.2 ad break;
132 1.1.6.2 ad default:
133 1.1.6.2 ad return EINVAL;
134 1.1.6.2 ad }
135 1.1.6.2 ad break;
136 1.1.6.2 ad }
137 1.1.6.2 ad default:
138 1.1.6.2 ad return ENOTTY;
139 1.1.6.2 ad }
140 1.1.6.2 ad
141 1.1.6.2 ad switch (cmd) {
142 1.1.6.2 ad case BIOCLOCATE:
143 1.1.6.2 ad locate = (struct bio_locate *)addr;
144 1.1.6.2 ad error = copyinstr(locate->bl_name, name, 16, NULL);
145 1.1.6.2 ad if (error != 0)
146 1.1.6.2 ad return (error);
147 1.1.6.2 ad locate->bl_cookie = bio_lookup(name);
148 1.1.6.2 ad if (locate->bl_cookie == NULL)
149 1.1.6.2 ad return (ENOENT);
150 1.1.6.2 ad break;
151 1.1.6.2 ad
152 1.1.6.2 ad default:
153 1.1.6.2 ad common = (struct bio_common *)addr;
154 1.1.6.2 ad mutex_enter(&bio_lock);
155 1.1.6.2 ad if (!bio_validate(common->bc_cookie)) {
156 1.1.6.2 ad mutex_exit(&bio_lock);
157 1.1.6.2 ad return (ENOENT);
158 1.1.6.2 ad }
159 1.1.6.2 ad mutex_exit(&bio_lock);
160 1.1.6.2 ad error = bio_delegate_ioctl(
161 1.1.6.2 ad (struct bio_mapping *)common->bc_cookie, cmd, addr);
162 1.1.6.2 ad return (error);
163 1.1.6.2 ad }
164 1.1.6.2 ad return (0);
165 1.1.6.2 ad }
166 1.1.6.2 ad
167 1.1.6.2 ad int
168 1.1.6.2 ad bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, void *))
169 1.1.6.2 ad {
170 1.1.6.2 ad struct bio_mapping *bm;
171 1.1.6.2 ad
172 1.1.6.2 ad MALLOC(bm, struct bio_mapping *, sizeof *bm, M_DEVBUF, M_NOWAIT);
173 1.1.6.2 ad if (bm == NULL)
174 1.1.6.2 ad return (ENOMEM);
175 1.1.6.2 ad bm->bm_dev = dev;
176 1.1.6.2 ad bm->bm_ioctl = ioctl;
177 1.1.6.2 ad mutex_enter(&bio_lock);
178 1.1.6.2 ad LIST_INSERT_HEAD(&bios, bm, bm_link);
179 1.1.6.2 ad mutex_exit(&bio_lock);
180 1.1.6.2 ad return (0);
181 1.1.6.2 ad }
182 1.1.6.2 ad
183 1.1.6.2 ad void
184 1.1.6.2 ad bio_unregister(struct device *dev)
185 1.1.6.2 ad {
186 1.1.6.2 ad struct bio_mapping *bm, *next;
187 1.1.6.2 ad
188 1.1.6.2 ad mutex_enter(&bio_lock);
189 1.1.6.2 ad for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
190 1.1.6.2 ad next = LIST_NEXT(bm, bm_link);
191 1.1.6.2 ad
192 1.1.6.2 ad if (dev == bm->bm_dev) {
193 1.1.6.2 ad LIST_REMOVE(bm, bm_link);
194 1.1.6.2 ad free(bm, M_DEVBUF);
195 1.1.6.2 ad }
196 1.1.6.2 ad }
197 1.1.6.2 ad mutex_exit(&bio_lock);
198 1.1.6.2 ad }
199 1.1.6.2 ad
200 1.1.6.2 ad struct bio_mapping *
201 1.1.6.2 ad bio_lookup(char *name)
202 1.1.6.2 ad {
203 1.1.6.2 ad struct bio_mapping *bm;
204 1.1.6.2 ad
205 1.1.6.2 ad mutex_enter(&bio_lock);
206 1.1.6.2 ad LIST_FOREACH(bm, &bios, bm_link) {
207 1.1.6.2 ad if (strcmp(name, bm->bm_dev->dv_xname) == 0) {
208 1.1.6.2 ad mutex_exit(&bio_lock);
209 1.1.6.2 ad return (bm);
210 1.1.6.2 ad }
211 1.1.6.2 ad }
212 1.1.6.2 ad mutex_exit(&bio_lock);
213 1.1.6.2 ad return (NULL);
214 1.1.6.2 ad }
215 1.1.6.2 ad
216 1.1.6.2 ad int
217 1.1.6.2 ad bio_validate(void *cookie)
218 1.1.6.2 ad {
219 1.1.6.2 ad struct bio_mapping *bm;
220 1.1.6.2 ad
221 1.1.6.2 ad LIST_FOREACH(bm, &bios, bm_link) {
222 1.1.6.2 ad if (bm == cookie) {
223 1.1.6.2 ad return (1);
224 1.1.6.2 ad }
225 1.1.6.2 ad }
226 1.1.6.2 ad return (0);
227 1.1.6.2 ad }
228 1.1.6.2 ad
229 1.1.6.2 ad int
230 1.1.6.2 ad bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, void *addr)
231 1.1.6.2 ad {
232 1.1.6.2 ad
233 1.1.6.2 ad return (bm->bm_ioctl(bm->bm_dev, cmd, addr));
234 1.1.6.2 ad }
235