Line data Source code
1 : /* $OpenBSD: bio.c,v 1.17 2015/08/26 22:28:57 deraadt Exp $ */
2 :
3 : /*
4 : * Copyright (c) 2002 Niklas Hallqvist. All rights reserved.
5 : * Copyright (c) 2012 Joel Sing <jsing@openbsd.org>. 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/param.h>
31 : #include <sys/device.h>
32 : #include <sys/ioctl.h>
33 : #include <sys/malloc.h>
34 : #include <sys/queue.h>
35 : #include <sys/systm.h>
36 :
37 : #include <dev/biovar.h>
38 :
39 : struct bio_mapping {
40 : LIST_ENTRY(bio_mapping) bm_link;
41 : struct device *bm_dev;
42 : int (*bm_ioctl)(struct device *, u_long, caddr_t);
43 : };
44 :
45 : LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
46 :
47 : void bioattach(int);
48 : int bioclose(dev_t, int, int, struct proc *);
49 : int bioioctl(dev_t, u_long, caddr_t, int, struct proc *);
50 : int bioopen(dev_t, int, int, struct proc *);
51 :
52 : int bio_delegate_ioctl(struct bio_mapping *, u_long, caddr_t);
53 : struct bio_mapping *bio_lookup(char *);
54 : int bio_validate(void *);
55 :
56 : void
57 0 : bioattach(int nunits)
58 : {
59 0 : }
60 :
61 : int
62 0 : bioopen(dev_t dev, int flags, int mode, struct proc *p)
63 : {
64 0 : return (0);
65 : }
66 :
67 : int
68 0 : bioclose(dev_t dev, int flags, int mode, struct proc *p)
69 : {
70 0 : return (0);
71 : }
72 :
73 : int
74 0 : bioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
75 : {
76 : struct bio_locate *locate;
77 : struct bio *bio;
78 0 : char name[16];
79 : int error;
80 :
81 0 : switch (cmd) {
82 : case BIOCLOCATE:
83 0 : locate = (struct bio_locate *)addr;
84 0 : error = copyinstr(locate->bl_name, name, sizeof name, NULL);
85 0 : if (error != 0)
86 0 : return (error);
87 0 : locate->bl_bio.bio_cookie = bio_lookup(name);
88 0 : if (locate->bl_bio.bio_cookie == NULL)
89 0 : return (ENOENT);
90 : break;
91 :
92 : case BIOCINQ:
93 : case BIOCDISK:
94 : case BIOCVOL:
95 : case BIOCALARM:
96 : case BIOCBLINK:
97 : case BIOCSETSTATE:
98 : case BIOCCREATERAID:
99 : case BIOCDELETERAID:
100 : case BIOCDISCIPLINE:
101 : case BIOCPATROL:
102 0 : bio = (struct bio *)addr;
103 0 : if (!bio_validate(bio->bio_cookie))
104 0 : return (ENOENT);
105 0 : return (bio_delegate_ioctl(
106 0 : (struct bio_mapping *)bio->bio_cookie, cmd, addr));
107 :
108 : default:
109 0 : return (ENXIO);
110 : }
111 0 : return (0);
112 0 : }
113 :
114 : int
115 0 : bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, caddr_t))
116 : {
117 : struct bio_mapping *bm;
118 :
119 0 : bm = malloc(sizeof *bm, M_DEVBUF, M_NOWAIT);
120 0 : if (bm == NULL)
121 0 : return (ENOMEM);
122 0 : bm->bm_dev = dev;
123 0 : bm->bm_ioctl = ioctl;
124 0 : LIST_INSERT_HEAD(&bios, bm, bm_link);
125 0 : return (0);
126 0 : }
127 :
128 : void
129 0 : bio_unregister(struct device *dev)
130 : {
131 : struct bio_mapping *bm, *next;
132 :
133 0 : for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
134 0 : next = LIST_NEXT(bm, bm_link);
135 :
136 0 : if (dev == bm->bm_dev) {
137 0 : LIST_REMOVE(bm, bm_link);
138 0 : free(bm, M_DEVBUF, sizeof(*bm));
139 0 : }
140 : }
141 0 : }
142 :
143 : struct bio_mapping *
144 0 : bio_lookup(char *name)
145 : {
146 : struct bio_mapping *bm;
147 :
148 0 : LIST_FOREACH(bm, &bios, bm_link)
149 0 : if (strcmp(name, bm->bm_dev->dv_xname) == 0)
150 0 : return (bm);
151 0 : return (NULL);
152 0 : }
153 :
154 : int
155 0 : bio_validate(void *cookie)
156 : {
157 : struct bio_mapping *bm;
158 :
159 0 : LIST_FOREACH(bm, &bios, bm_link)
160 0 : if (bm == cookie)
161 0 : return (1);
162 0 : return (0);
163 0 : }
164 :
165 : int
166 0 : bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, caddr_t addr)
167 : {
168 0 : return (bm->bm_ioctl(bm->bm_dev, cmd, addr));
169 : }
170 :
171 : void
172 0 : bio_info(struct bio_status *bs, int print, const char *fmt, ...)
173 : {
174 0 : va_list ap;
175 :
176 0 : va_start(ap, fmt);
177 0 : bio_status(bs, print, BIO_MSG_INFO, fmt, &ap);
178 0 : va_end(ap);
179 0 : }
180 :
181 : void
182 0 : bio_warn(struct bio_status *bs, int print, const char *fmt, ...)
183 : {
184 0 : va_list ap;
185 :
186 0 : va_start(ap, fmt);
187 0 : bio_status(bs, print, BIO_MSG_WARN, fmt, &ap);
188 0 : va_end(ap);
189 0 : }
190 :
191 : void
192 0 : bio_error(struct bio_status *bs, int print, const char *fmt, ...)
193 : {
194 0 : va_list ap;
195 :
196 0 : va_start(ap, fmt);
197 0 : bio_status(bs, print, BIO_MSG_ERROR, fmt, &ap);
198 0 : va_end(ap);
199 0 : }
200 :
201 : void
202 0 : bio_status_init(struct bio_status *bs, struct device *dv)
203 : {
204 0 : bzero(bs, sizeof(struct bio_status));
205 :
206 0 : bs->bs_status = BIO_STATUS_UNKNOWN;
207 :
208 0 : strlcpy(bs->bs_controller, dv->dv_xname, sizeof(bs->bs_controller));
209 0 : }
210 :
211 : void
212 0 : bio_status(struct bio_status *bs, int print, int msg_type, const char *fmt,
213 : va_list *ap)
214 : {
215 : int idx;
216 :
217 0 : if (bs->bs_msg_count >= BIO_MSG_COUNT) {
218 0 : printf("%s: insufficient message buffers\n", bs->bs_controller);
219 0 : return;
220 : }
221 :
222 0 : idx = bs->bs_msg_count++;
223 :
224 0 : bs->bs_msgs[idx].bm_type = msg_type;
225 0 : vsnprintf(bs->bs_msgs[idx].bm_msg, BIO_MSG_LEN, fmt, *ap);
226 :
227 0 : if (print)
228 0 : printf("%s: %s\n", bs->bs_controller, bs->bs_msgs[idx].bm_msg);
229 0 : }
|