bio.c revision 1.13.16.7 1 /* $NetBSD: bio.c,v 1.13.16.7 2019/01/14 13:34:27 pgoyette 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.13.16.7 2019/01/14 13:34:27 pgoyette 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 #include <sys/compat_stub.h>
47
48 #include <dev/biovar.h>
49 #include <dev/sysmon/sysmonvar.h>
50
51 #include "ioconf.h"
52
53 struct bio_mapping {
54 LIST_ENTRY(bio_mapping) bm_link;
55 device_t bm_dev;
56 int (*bm_ioctl)(device_t, u_long, void *);
57 };
58
59 static LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
60 static kmutex_t bio_lock;
61 static bool bio_lock_initialized = false;
62
63 static void bio_initialize(void);
64 static int bioclose(dev_t, int, int, struct lwp *);
65 static int bioioctl(dev_t, u_long, void *, int, struct lwp *);
66 static int bioopen(dev_t, int, int, struct lwp *);
67
68 static int bio_delegate_ioctl(void *, u_long, void *);
69 static struct bio_mapping *bio_lookup(char *);
70 static int bio_validate(void *);
71
72 const struct cdevsw bio_cdevsw = {
73 .d_open = bioopen,
74 .d_close = bioclose,
75 .d_read = noread,
76 .d_write = nowrite,
77 .d_ioctl = bioioctl,
78 .d_stop = nostop,
79 .d_tty = notty,
80 .d_poll = nopoll,
81 .d_mmap = nommap,
82 .d_kqfilter = nokqfilter,
83 .d_discard = nodiscard,
84 .d_flag = D_OTHER | D_MPSAFE
85 };
86
87
88 static void
89 bio_initialize(void)
90 {
91 if (bio_lock_initialized)
92 return;
93
94 mutex_init(&bio_lock, MUTEX_DEFAULT, IPL_VM);
95 bio_lock_initialized = true;
96 }
97
98 void
99 bioattach(int nunits)
100 {
101 if (!bio_lock_initialized)
102 bio_initialize();
103 }
104
105 static int
106 bioopen(dev_t dev, int flags, int mode, struct lwp *l)
107 {
108 return 0;
109 }
110
111 static int
112 bioclose(dev_t dev, int flags, int mode, struct lwp *l)
113 {
114 return 0;
115 }
116
117 /* Hook up the compat_bio_30 routine */
118 MODULE_CALL_INT_HOOK_DECL(compat_bio_30_hook,
119 (void * cookie, u_long cmd, void *addr, int(*ff)(void *, u_long, void *)));
120 MODULE_CALL_INT_HOOK(compat_bio_30_hook,
121 (void * cookie, u_long cmd, void *addr, int(*ff)(void *, u_long, void *)),
122 (cookie, cmd, addr, ff), enosys());
123
124 static int
125 bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
126 {
127 struct bio_locate *locate;
128 struct bio_common *common;
129 char name[16];
130 int error;
131
132 switch(cmd) {
133 case BIOCLOCATE:
134 case BIOCINQ:
135 case BIOCDISK:
136 case BIOCDISK_NOVOL:
137 case BIOCVOL:
138 case OBIOCDISK:
139 case OBIOCVOL:
140 error = kauth_authorize_device_passthru(l->l_cred, dev,
141 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
142 if (error)
143 return error;
144 break;
145 case BIOCBLINK:
146 case BIOCSETSTATE:
147 case BIOCVOLOPS:
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 BIOCALARM: {
154 struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
155 switch (alarm->ba_opcode) {
156 case BIOC_SADISABLE:
157 case BIOC_SAENABLE:
158 case BIOC_SASILENCE:
159 case BIOC_SATEST:
160 error = kauth_authorize_device_passthru(l->l_cred, dev,
161 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
162 if (error)
163 return error;
164 break;
165 case BIOC_GASTATUS:
166 error = kauth_authorize_device_passthru(l->l_cred, dev,
167 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
168 if (error)
169 return error;
170 break;
171 default:
172 return EINVAL;
173 }
174 break;
175 }
176 default:
177 return ENOTTY;
178 }
179
180 switch (cmd) {
181 case BIOCLOCATE:
182 locate = addr;
183 error = copyinstr(locate->bl_name, name, sizeof(name), NULL);
184 if (error != 0)
185 return error;
186 locate->bl_cookie = bio_lookup(name);
187 if (locate->bl_cookie == NULL)
188 return ENOENT;
189 break;
190
191 default:
192 common = addr;
193 mutex_enter(&bio_lock);
194 if (!bio_validate(common->bc_cookie)) {
195 mutex_exit(&bio_lock);
196 return ENOENT;
197 }
198 mutex_exit(&bio_lock);
199 error = compat_bio_30_hook_call(common->bc_cookie, cmd, addr,
200 bio_delegate_ioctl);
201 if (error == ENOSYS)
202 error = bio_delegate_ioctl(common->bc_cookie, cmd,
203 addr);
204 return error;
205 }
206 return 0;
207 }
208
209 int
210 bio_register(device_t dev, int (*ioctl)(device_t, u_long, void *))
211 {
212 struct bio_mapping *bm;
213
214 if (!bio_lock_initialized)
215 bio_initialize();
216
217 bm = malloc(sizeof(*bm), M_DEVBUF, M_NOWAIT|M_ZERO);
218 if (bm == NULL)
219 return ENOMEM;
220 bm->bm_dev = dev;
221 bm->bm_ioctl = ioctl;
222 mutex_enter(&bio_lock);
223 LIST_INSERT_HEAD(&bios, bm, bm_link);
224 mutex_exit(&bio_lock);
225 return 0;
226 }
227
228 void
229 bio_unregister(device_t dev)
230 {
231 struct bio_mapping *bm, *next;
232
233 mutex_enter(&bio_lock);
234 for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
235 next = LIST_NEXT(bm, bm_link);
236
237 if (dev == bm->bm_dev) {
238 LIST_REMOVE(bm, bm_link);
239 free(bm, M_DEVBUF);
240 }
241 }
242 mutex_exit(&bio_lock);
243 }
244
245 static struct bio_mapping *
246 bio_lookup(char *name)
247 {
248 struct bio_mapping *bm;
249
250 mutex_enter(&bio_lock);
251 LIST_FOREACH(bm, &bios, bm_link) {
252 if (strcmp(name, device_xname(bm->bm_dev)) == 0) {
253 mutex_exit(&bio_lock);
254 return bm;
255 }
256 }
257 mutex_exit(&bio_lock);
258 return NULL;
259 }
260
261 static int
262 bio_validate(void *cookie)
263 {
264 struct bio_mapping *bm;
265
266 LIST_FOREACH(bm, &bios, bm_link)
267 if (bm == cookie)
268 return 1;
269
270 return 0;
271 }
272
273 static int
274 bio_delegate_ioctl(void *cookie, u_long cmd, void *addr)
275 {
276 struct bio_mapping *bm = cookie;
277
278 return bm->bm_ioctl(bm->bm_dev, cmd, addr);
279 }
280
281 void
282 bio_disk_to_envsys(envsys_data_t *edata, const struct bioc_disk *bd)
283 {
284 switch (bd->bd_status) {
285 case BIOC_SDONLINE:
286 edata->value_cur = ENVSYS_DRIVE_ONLINE;
287 edata->state = ENVSYS_SVALID;
288 break;
289 case BIOC_SDOFFLINE:
290 edata->value_cur = ENVSYS_DRIVE_OFFLINE;
291 edata->state = ENVSYS_SCRITICAL;
292 break;
293 default:
294 edata->value_cur = ENVSYS_DRIVE_FAIL;
295 edata->state = ENVSYS_SCRITICAL;
296 break;
297 }
298 }
299
300 void
301 bio_vol_to_envsys(envsys_data_t *edata, const struct bioc_vol *bv)
302 {
303 switch (bv->bv_status) {
304 case BIOC_SVOFFLINE:
305 edata->value_cur = ENVSYS_DRIVE_OFFLINE;
306 edata->state = ENVSYS_SCRITICAL;
307 break;
308 case BIOC_SVDEGRADED:
309 edata->value_cur = ENVSYS_DRIVE_PFAIL;
310 edata->state = ENVSYS_SCRITICAL;
311 break;
312 case BIOC_SVBUILDING:
313 edata->value_cur = ENVSYS_DRIVE_BUILD;
314 edata->state = ENVSYS_SVALID;
315 break;
316 case BIOC_SVMIGRATING:
317 edata->value_cur = ENVSYS_DRIVE_MIGRATING;
318 edata->state = ENVSYS_SVALID;
319 break;
320 case BIOC_SVCHECKING:
321 edata->value_cur = ENVSYS_DRIVE_CHECK;
322 edata->state = ENVSYS_SVALID;
323 break;
324 case BIOC_SVREBUILD:
325 edata->value_cur = ENVSYS_DRIVE_REBUILD;
326 edata->state = ENVSYS_SCRITICAL;
327 break;
328 case BIOC_SVSCRUB:
329 case BIOC_SVONLINE:
330 edata->value_cur = ENVSYS_DRIVE_ONLINE;
331 edata->state = ENVSYS_SVALID;
332 break;
333 case BIOC_SVINVALID:
334 /* FALLTHROUGH */
335 default:
336 edata->value_cur = ENVSYS_DRIVE_EMPTY; /* unknown state */
337 edata->state = ENVSYS_SINVALID;
338 break;
339 }
340 }
341