xref: /netbsd-src/sys/dev/bio.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: bio.c,v 1.2 2007/11/02 08:38:37 xtraeme 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.2 2007/11/02 08:38:37 xtraeme 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/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/kauth.h>
44 
45 #include <dev/biovar.h>
46 
47 struct bio_mapping {
48 	LIST_ENTRY(bio_mapping) bm_link;
49 	struct device *bm_dev;
50 	int (*bm_ioctl)(struct device *, u_long, void *);
51 };
52 
53 LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
54 static kmutex_t bio_lock;
55 
56 void	bioattach(int);
57 int	bioclose(dev_t, int, int, struct lwp *);
58 int	bioioctl(dev_t, u_long, void *, int, struct lwp *);
59 int	bioopen(dev_t, int, int, struct lwp *);
60 
61 int	bio_delegate_ioctl(struct bio_mapping *, u_long, void *);
62 struct	bio_mapping *bio_lookup(char *);
63 int	bio_validate(void *);
64 
65 const struct cdevsw bio_cdevsw = {
66         bioopen, bioclose, noread, nowrite, bioioctl,
67         nostop, notty, nopoll, nommap, nokqfilter, 0
68 };
69 
70 
71 void
72 bioattach(int nunits)
73 {
74 	mutex_init(&bio_lock, MUTEX_DRIVER, IPL_BIO);
75 }
76 
77 int
78 bioopen(dev_t dev, int flags, int mode, struct lwp *l)
79 {
80 	return 0;
81 }
82 
83 int
84 bioclose(dev_t dev, int flags, int mode, struct lwp *l)
85 {
86 	return 0;
87 }
88 
89 int
90 bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct  lwp *l)
91 {
92 	struct bio_locate *locate;
93 	struct bio_common *common;
94 	char name[16];
95 	int error;
96 
97 	switch(cmd) {
98 	case BIOCLOCATE:
99 	case BIOCINQ:
100 	case BIOCDISK:
101 	case BIOCVOL:
102 		error = kauth_authorize_device_passthru(l->l_cred, dev,
103 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
104 		if (error)
105 			return error;
106 		break;
107 	case BIOCBLINK:
108 	case BIOCSETSTATE:
109 		error = kauth_authorize_device_passthru(l->l_cred, dev,
110 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
111 		if (error)
112 			return error;
113 		break;
114 	case BIOCALARM: {
115 		struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
116 		switch (alarm->ba_opcode) {
117 		case BIOC_SADISABLE:
118 		case BIOC_SAENABLE:
119 		case BIOC_SASILENCE:
120 		case BIOC_SATEST:
121 			error = kauth_authorize_device_passthru(l->l_cred, dev,
122 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
123 			if (error)
124 				return error;
125 			break;
126 		case BIOC_GASTATUS:
127 			error = kauth_authorize_device_passthru(l->l_cred, dev,
128 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
129 			if (error)
130 				return error;
131 			break;
132 		default:
133 			return EINVAL;
134 		}
135 		break;
136 	}
137 	default:
138 		return ENOTTY;
139 	}
140 
141 	switch (cmd) {
142 	case BIOCLOCATE:
143 		locate = (struct bio_locate *)addr;
144 		error = copyinstr(locate->bl_name, name, 16, NULL);
145 		if (error != 0)
146 			return error;
147 		locate->bl_cookie = bio_lookup(name);
148 		if (locate->bl_cookie == NULL)
149 			return ENOENT;
150 		break;
151 
152 	default:
153 		common = (struct bio_common *)addr;
154 		mutex_enter(&bio_lock);
155 		if (!bio_validate(common->bc_cookie)) {
156 			mutex_exit(&bio_lock);
157 			return ENOENT;
158 		}
159 		mutex_exit(&bio_lock);
160 		error = bio_delegate_ioctl(
161 		    (struct bio_mapping *)common->bc_cookie, cmd, addr);
162 		return error;
163 	}
164 	return 0;
165 }
166 
167 int
168 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, void *))
169 {
170 	struct bio_mapping *bm;
171 
172 	bm = (struct bio_mapping *)malloc(sizeof(*bm), M_DEVBUF,
173 	    M_NOWAIT|M_ZERO);
174 	if (bm == NULL)
175 		return ENOMEM;
176 	bm->bm_dev = dev;
177 	bm->bm_ioctl = ioctl;
178 	mutex_enter(&bio_lock);
179 	LIST_INSERT_HEAD(&bios, bm, bm_link);
180 	mutex_exit(&bio_lock);
181 	return 0;
182 }
183 
184 void
185 bio_unregister(struct device *dev)
186 {
187 	struct bio_mapping *bm, *next;
188 
189 	mutex_enter(&bio_lock);
190 	for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
191 		next = LIST_NEXT(bm, bm_link);
192 
193 		if (dev == bm->bm_dev) {
194 			LIST_REMOVE(bm, bm_link);
195 			free(bm, M_DEVBUF);
196 		}
197 	}
198 	mutex_exit(&bio_lock);
199 }
200 
201 struct bio_mapping *
202 bio_lookup(char *name)
203 {
204 	struct bio_mapping *bm;
205 
206 	mutex_enter(&bio_lock);
207 	LIST_FOREACH(bm, &bios, bm_link) {
208 		if (strcmp(name, bm->bm_dev->dv_xname) == 0) {
209 			mutex_exit(&bio_lock);
210 			return bm;
211 		}
212 	}
213 	mutex_exit(&bio_lock);
214 	return NULL;
215 }
216 
217 int
218 bio_validate(void *cookie)
219 {
220 	struct bio_mapping *bm;
221 
222 	LIST_FOREACH(bm, &bios, bm_link)
223 		if (bm == cookie)
224 			return 1;
225 
226 	return 0;
227 }
228 
229 int
230 bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, void *addr)
231 {
232 
233 	return bm->bm_ioctl(bm->bm_dev, cmd, addr);
234 }
235