bio.c revision 1.6.6.3 1 /* $NetBSD: bio.c,v 1.6.6.3 2008/06/02 13:23:11 mjf 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.6.6.3 2008/06/02 13:23:11 mjf Exp $");
32
33 #include "opt_compat_netbsd.h"
34
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/device.h>
38 #include <sys/event.h>
39 #include <sys/ioctl.h>
40 #include <sys/malloc.h>
41 #include <sys/queue.h>
42 #include <sys/systm.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/kauth.h>
46
47 #include <dev/biovar.h>
48
49 struct bio_mapping {
50 LIST_ENTRY(bio_mapping) bm_link;
51 struct device *bm_dev;
52 int (*bm_ioctl)(struct device *, u_long, void *);
53 };
54
55 static LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
56 static kmutex_t bio_lock;
57 static bool bio_lock_initialized = false;
58
59 static void bio_initialize(void);
60 static int bioclose(dev_t, int, int, struct lwp *);
61 static int bioioctl(dev_t, u_long, void *, int, struct lwp *);
62 static int bioopen(dev_t, int, int, struct lwp *);
63
64 static int bio_delegate_ioctl(void *, u_long, void *);
65 static struct bio_mapping *bio_lookup(char *);
66 static int bio_validate(void *);
67
68 void bioattach(int);
69
70 const struct cdevsw bio_cdevsw = {
71 bioopen, bioclose, noread, nowrite, bioioctl,
72 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE
73 };
74
75
76 static void
77 bio_initialize(void)
78 {
79 if (bio_lock_initialized)
80 return;
81
82 mutex_init(&bio_lock, MUTEX_DEFAULT, IPL_VM);
83 bio_lock_initialized = true;
84 }
85
86 void
87 bioattach(int nunits)
88 {
89 int maj;
90
91 if (!bio_lock_initialized)
92 bio_initialize();
93
94 maj = cdevsw_lookup_major(&bio_cdevsw);
95 device_register_name(makedev(maj, 0), NULL, true, DEV_DISK, "bio");
96 }
97
98 static int
99 bioopen(dev_t dev, int flags, int mode, struct lwp *l)
100 {
101 return 0;
102 }
103
104 static int
105 bioclose(dev_t dev, int flags, int mode, struct lwp *l)
106 {
107 return 0;
108 }
109
110 static int
111 bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
112 {
113 struct bio_locate *locate;
114 struct bio_common *common;
115 char name[16];
116 int error;
117
118 switch(cmd) {
119 case BIOCLOCATE:
120 case BIOCINQ:
121 case BIOCDISK:
122 case BIOCDISK_NOVOL:
123 case BIOCVOL:
124 #ifdef COMPAT_30
125 case OBIOCDISK:
126 case OBIOCVOL:
127 #endif
128 error = kauth_authorize_device_passthru(l->l_cred, dev,
129 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
130 if (error)
131 return error;
132 break;
133 case BIOCBLINK:
134 case BIOCSETSTATE:
135 case BIOCVOLOPS:
136 error = kauth_authorize_device_passthru(l->l_cred, dev,
137 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
138 if (error)
139 return error;
140 break;
141 case BIOCALARM: {
142 struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
143 switch (alarm->ba_opcode) {
144 case BIOC_SADISABLE:
145 case BIOC_SAENABLE:
146 case BIOC_SASILENCE:
147 case BIOC_SATEST:
148 error = kauth_authorize_device_passthru(l->l_cred, dev,
149 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
150 if (error)
151 return error;
152 break;
153 case BIOC_GASTATUS:
154 error = kauth_authorize_device_passthru(l->l_cred, dev,
155 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
156 if (error)
157 return error;
158 break;
159 default:
160 return EINVAL;
161 }
162 break;
163 }
164 default:
165 return ENOTTY;
166 }
167
168 switch (cmd) {
169 case BIOCLOCATE:
170 locate = addr;
171 error = copyinstr(locate->bl_name, name, sizeof(name), NULL);
172 if (error != 0)
173 return error;
174 locate->bl_cookie = bio_lookup(name);
175 if (locate->bl_cookie == NULL)
176 return ENOENT;
177 break;
178
179 default:
180 common = addr;
181 mutex_enter(&bio_lock);
182 if (!bio_validate(common->bc_cookie)) {
183 mutex_exit(&bio_lock);
184 return ENOENT;
185 }
186 mutex_exit(&bio_lock);
187 #ifdef COMPAT_30
188 switch (cmd) {
189 case OBIOCDISK: {
190 struct bioc_disk *bd =
191 malloc(sizeof(*bd), M_DEVBUF, M_WAITOK|M_ZERO);
192
193 (void)memcpy(bd, addr, sizeof(struct obioc_disk));
194 error = bio_delegate_ioctl(common->bc_cookie,
195 BIOCDISK, bd);
196 if (error) {
197 free(bd, M_DEVBUF);
198 return error;
199 }
200
201 (void)memcpy(addr, bd, sizeof(struct obioc_disk));
202 free(bd, M_DEVBUF);
203 return 0;
204 }
205 case OBIOCVOL: {
206 struct bioc_vol *bv =
207 malloc(sizeof(*bv), M_DEVBUF, M_WAITOK|M_ZERO);
208
209 (void)memcpy(bv, addr, sizeof(struct obioc_vol));
210 error = bio_delegate_ioctl(common->bc_cookie,
211 BIOCVOL, bv);
212 if (error) {
213 free(bv, M_DEVBUF);
214 return error;
215 }
216
217 (void)memcpy(addr, bv, sizeof(struct obioc_vol));
218 free(bv, M_DEVBUF);
219 return 0;
220 }
221 }
222 #endif
223 error = bio_delegate_ioctl(common->bc_cookie, cmd, addr);
224 return error;
225 }
226 return 0;
227 }
228
229 int
230 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, void *))
231 {
232 struct bio_mapping *bm;
233
234 if (!bio_lock_initialized)
235 bio_initialize();
236
237 bm = malloc(sizeof(*bm), M_DEVBUF, M_NOWAIT|M_ZERO);
238 if (bm == NULL)
239 return ENOMEM;
240 bm->bm_dev = dev;
241 bm->bm_ioctl = ioctl;
242 mutex_enter(&bio_lock);
243 LIST_INSERT_HEAD(&bios, bm, bm_link);
244 mutex_exit(&bio_lock);
245 return 0;
246 }
247
248 void
249 bio_unregister(struct device *dev)
250 {
251 struct bio_mapping *bm, *next;
252
253 mutex_enter(&bio_lock);
254 for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
255 next = LIST_NEXT(bm, bm_link);
256
257 if (dev == bm->bm_dev) {
258 LIST_REMOVE(bm, bm_link);
259 free(bm, M_DEVBUF);
260 }
261 }
262 mutex_exit(&bio_lock);
263 }
264
265 static struct bio_mapping *
266 bio_lookup(char *name)
267 {
268 struct bio_mapping *bm;
269
270 mutex_enter(&bio_lock);
271 LIST_FOREACH(bm, &bios, bm_link) {
272 if (strcmp(name, device_xname(bm->bm_dev)) == 0) {
273 mutex_exit(&bio_lock);
274 return bm;
275 }
276 }
277 mutex_exit(&bio_lock);
278 return NULL;
279 }
280
281 static int
282 bio_validate(void *cookie)
283 {
284 struct bio_mapping *bm;
285
286 LIST_FOREACH(bm, &bios, bm_link)
287 if (bm == cookie)
288 return 1;
289
290 return 0;
291 }
292
293 static int
294 bio_delegate_ioctl(void *cookie, u_long cmd, void *addr)
295 {
296 struct bio_mapping *bm = cookie;
297
298 return bm->bm_ioctl(bm->bm_dev, cmd, addr);
299 }
300