subr_devsw.c revision 1.37.2.2 1 1.37.2.2 pgoyette /* $NetBSD: subr_devsw.c,v 1.37.2.2 2017/04/28 02:36:10 pgoyette Exp $ */
2 1.11 ad
3 1.2 gehenna /*-
4 1.20 ad * Copyright (c) 2001, 2002, 2007, 2008 The NetBSD Foundation, Inc.
5 1.2 gehenna * All rights reserved.
6 1.2 gehenna *
7 1.2 gehenna * This code is derived from software contributed to The NetBSD Foundation
8 1.11 ad * by MAEKAWA Masahide <gehenna (at) NetBSD.org>, and by Andrew Doran.
9 1.2 gehenna *
10 1.2 gehenna * Redistribution and use in source and binary forms, with or without
11 1.2 gehenna * modification, are permitted provided that the following conditions
12 1.2 gehenna * are met:
13 1.2 gehenna * 1. Redistributions of source code must retain the above copyright
14 1.2 gehenna * notice, this list of conditions and the following disclaimer.
15 1.2 gehenna * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 gehenna * notice, this list of conditions and the following disclaimer in the
17 1.2 gehenna * documentation and/or other materials provided with the distribution.
18 1.2 gehenna *
19 1.2 gehenna * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 gehenna * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 gehenna * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 gehenna * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 gehenna * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 gehenna * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 gehenna * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 gehenna * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 gehenna * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 gehenna * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 gehenna * POSSIBILITY OF SUCH DAMAGE.
30 1.2 gehenna */
31 1.11 ad
32 1.11 ad /*
33 1.11 ad * Overview
34 1.11 ad *
35 1.11 ad * subr_devsw.c: registers device drivers by name and by major
36 1.11 ad * number, and provides wrapper methods for performing I/O and
37 1.11 ad * other tasks on device drivers, keying on the device number
38 1.11 ad * (dev_t).
39 1.11 ad *
40 1.11 ad * When the system is built, the config(8) command generates
41 1.11 ad * static tables of device drivers built into the kernel image
42 1.11 ad * along with their associated methods. These are recorded in
43 1.11 ad * the cdevsw0 and bdevsw0 tables. Drivers can also be added to
44 1.11 ad * and removed from the system dynamically.
45 1.11 ad *
46 1.11 ad * Allocation
47 1.11 ad *
48 1.11 ad * When the system initially boots only the statically allocated
49 1.11 ad * indexes (bdevsw0, cdevsw0) are used. If these overflow due to
50 1.11 ad * allocation, we allocate a fixed block of memory to hold the new,
51 1.11 ad * expanded index. This "fork" of the table is only ever performed
52 1.11 ad * once in order to guarantee that other threads may safely access
53 1.11 ad * the device tables:
54 1.11 ad *
55 1.11 ad * o Once a thread has a "reference" to the table via an earlier
56 1.11 ad * open() call, we know that the entry in the table must exist
57 1.11 ad * and so it is safe to access it.
58 1.11 ad *
59 1.11 ad * o Regardless of whether other threads see the old or new
60 1.11 ad * pointers, they will point to a correct device switch
61 1.11 ad * structure for the operation being performed.
62 1.11 ad *
63 1.11 ad * XXX Currently, the wrapper methods such as cdev_read() verify
64 1.11 ad * that a device driver does in fact exist before calling the
65 1.11 ad * associated driver method. This should be changed so that
66 1.11 ad * once the device is has been referenced by a vnode (opened),
67 1.11 ad * calling the other methods should be valid until that reference
68 1.11 ad * is dropped.
69 1.11 ad */
70 1.7 lukem
71 1.7 lukem #include <sys/cdefs.h>
72 1.37.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.37.2.2 2017/04/28 02:36:10 pgoyette Exp $");
73 1.34 riz
74 1.34 riz #ifdef _KERNEL_OPT
75 1.34 riz #include "opt_dtrace.h"
76 1.34 riz #endif
77 1.2 gehenna
78 1.2 gehenna #include <sys/param.h>
79 1.2 gehenna #include <sys/conf.h>
80 1.11 ad #include <sys/kmem.h>
81 1.2 gehenna #include <sys/systm.h>
82 1.11 ad #include <sys/poll.h>
83 1.11 ad #include <sys/tty.h>
84 1.15 matt #include <sys/cpu.h>
85 1.11 ad #include <sys/buf.h>
86 1.29 mrg #include <sys/reboot.h>
87 1.34 riz #include <sys/sdt.h>
88 1.37.2.1 pgoyette #include <sys/atomic.h>
89 1.37.2.1 pgoyette #include <sys/condvar.h>
90 1.37.2.1 pgoyette #include <sys/localcount.h>
91 1.37.2.1 pgoyette #include <sys/pserialize.h>
92 1.2 gehenna
93 1.2 gehenna #ifdef DEVSW_DEBUG
94 1.2 gehenna #define DPRINTF(x) printf x
95 1.2 gehenna #else /* DEVSW_DEBUG */
96 1.2 gehenna #define DPRINTF(x)
97 1.2 gehenna #endif /* DEVSW_DEBUG */
98 1.2 gehenna
99 1.11 ad #define MAXDEVSW 512 /* the maximum of major device number */
100 1.2 gehenna #define BDEVSW_SIZE (sizeof(struct bdevsw *))
101 1.2 gehenna #define CDEVSW_SIZE (sizeof(struct cdevsw *))
102 1.2 gehenna #define DEVSWCONV_SIZE (sizeof(struct devsw_conv))
103 1.2 gehenna
104 1.2 gehenna extern const struct bdevsw **bdevsw, *bdevsw0[];
105 1.2 gehenna extern const struct cdevsw **cdevsw, *cdevsw0[];
106 1.2 gehenna extern struct devsw_conv *devsw_conv, devsw_conv0[];
107 1.2 gehenna extern const int sys_bdevsws, sys_cdevsws;
108 1.2 gehenna extern int max_bdevsws, max_cdevsws, max_devsw_convs;
109 1.2 gehenna
110 1.24 drochner static int bdevsw_attach(const struct bdevsw *, devmajor_t *);
111 1.24 drochner static int cdevsw_attach(const struct cdevsw *, devmajor_t *);
112 1.11 ad static void devsw_detach_locked(const struct bdevsw *, const struct cdevsw *);
113 1.11 ad
114 1.37.2.1 pgoyette kmutex_t device_lock;
115 1.37.2.1 pgoyette kcondvar_t device_cv;
116 1.37.2.1 pgoyette pserialize_t device_psz = NULL;
117 1.23 pooka
118 1.31 pooka void (*biodone_vfs)(buf_t *) = (void *)nullop;
119 1.31 pooka
120 1.11 ad void
121 1.11 ad devsw_init(void)
122 1.11 ad {
123 1.11 ad
124 1.11 ad KASSERT(sys_bdevsws < MAXDEVSW - 1);
125 1.11 ad KASSERT(sys_cdevsws < MAXDEVSW - 1);
126 1.23 pooka mutex_init(&device_lock, MUTEX_DEFAULT, IPL_NONE);
127 1.37.2.1 pgoyette cv_init(&device_cv, "devsw");
128 1.37.2.1 pgoyette }
129 1.37.2.1 pgoyette
130 1.37.2.1 pgoyette void
131 1.37.2.1 pgoyette devsw_detach_init(void)
132 1.37.2.1 pgoyette {
133 1.37.2.1 pgoyette
134 1.37.2.1 pgoyette device_psz = pserialize_create();
135 1.11 ad }
136 1.2 gehenna
137 1.2 gehenna int
138 1.24 drochner devsw_attach(const char *devname,
139 1.24 drochner const struct bdevsw *bdev, devmajor_t *bmajor,
140 1.24 drochner const struct cdevsw *cdev, devmajor_t *cmajor)
141 1.2 gehenna {
142 1.2 gehenna struct devsw_conv *conv;
143 1.2 gehenna char *name;
144 1.2 gehenna int error, i;
145 1.25 enami size_t len;
146 1.2 gehenna
147 1.2 gehenna if (devname == NULL || cdev == NULL)
148 1.2 gehenna return (EINVAL);
149 1.2 gehenna
150 1.23 pooka mutex_enter(&device_lock);
151 1.11 ad
152 1.37.2.1 pgoyette if (bdev != NULL) {
153 1.37.2.2 pgoyette if (bdev->d_localcount == NULL) {
154 1.37.2.2 pgoyette aprint_normal("%s: %s's bdevsw has no localcount",
155 1.37.2.2 pgoyette __func__, devname);
156 1.37.2.2 pgoyette return EINVAL;
157 1.37.2.2 pgoyette }
158 1.37.2.2 pgoyette if (bdev->d_localcount == cdev->d_localcount) {
159 1.37.2.2 pgoyette aprint_normal("%s: %s uses same localcount for both "
160 1.37.2.2 pgoyette cdevsw and bdevsw", __func__, devname);
161 1.37.2.2 pgoyette return EINVAL;
162 1.37.2.2 pgoyette }
163 1.37.2.2 pgoyette }
164 1.37.2.2 pgoyette if (cdev != NULL) {
165 1.37.2.2 pgoyette aprint_normal("%s: %s's cdevsw has no localcount",
166 1.37.2.1 pgoyette __func__, devname);
167 1.37.2.2 pgoyette return EINVAL;
168 1.37.2.1 pgoyette }
169 1.37.2.1 pgoyette
170 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
171 1.2 gehenna conv = &devsw_conv[i];
172 1.2 gehenna if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
173 1.2 gehenna continue;
174 1.2 gehenna
175 1.2 gehenna if (*bmajor < 0)
176 1.2 gehenna *bmajor = conv->d_bmajor;
177 1.2 gehenna if (*cmajor < 0)
178 1.2 gehenna *cmajor = conv->d_cmajor;
179 1.2 gehenna
180 1.11 ad if (*bmajor != conv->d_bmajor || *cmajor != conv->d_cmajor) {
181 1.11 ad error = EINVAL;
182 1.11 ad goto fail;
183 1.11 ad }
184 1.11 ad if ((*bmajor >= 0 && bdev == NULL) || *cmajor < 0) {
185 1.11 ad error = EINVAL;
186 1.11 ad goto fail;
187 1.11 ad }
188 1.2 gehenna
189 1.2 gehenna if ((*bmajor >= 0 && bdevsw[*bmajor] != NULL) ||
190 1.11 ad cdevsw[*cmajor] != NULL) {
191 1.11 ad error = EEXIST;
192 1.11 ad goto fail;
193 1.11 ad }
194 1.2 gehenna
195 1.37.2.1 pgoyette /* use membar_producer() to ensure visibility of the xdevsw */
196 1.37.2.1 pgoyette if (bdev != NULL) {
197 1.37.2.1 pgoyette localcount_init(bdev->d_localcount);
198 1.37.2.1 pgoyette membar_producer();
199 1.2 gehenna bdevsw[*bmajor] = bdev;
200 1.37.2.1 pgoyette }
201 1.37.2.1 pgoyette localcount_init(cdev->d_localcount);
202 1.37.2.1 pgoyette membar_producer();
203 1.2 gehenna cdevsw[*cmajor] = cdev;
204 1.2 gehenna
205 1.23 pooka mutex_exit(&device_lock);
206 1.2 gehenna return (0);
207 1.2 gehenna }
208 1.2 gehenna
209 1.14 pooka error = bdevsw_attach(bdev, bmajor);
210 1.11 ad if (error != 0)
211 1.11 ad goto fail;
212 1.14 pooka error = cdevsw_attach(cdev, cmajor);
213 1.2 gehenna if (error != 0) {
214 1.11 ad devsw_detach_locked(bdev, NULL);
215 1.11 ad goto fail;
216 1.2 gehenna }
217 1.2 gehenna
218 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
219 1.2 gehenna if (devsw_conv[i].d_name == NULL)
220 1.2 gehenna break;
221 1.2 gehenna }
222 1.2 gehenna if (i == max_devsw_convs) {
223 1.2 gehenna struct devsw_conv *newptr;
224 1.33 matt int old_convs, new_convs;
225 1.2 gehenna
226 1.33 matt old_convs = max_devsw_convs;
227 1.33 matt new_convs = old_convs + 1;
228 1.2 gehenna
229 1.33 matt newptr = kmem_zalloc(new_convs * DEVSWCONV_SIZE, KM_NOSLEEP);
230 1.2 gehenna if (newptr == NULL) {
231 1.11 ad devsw_detach_locked(bdev, cdev);
232 1.11 ad error = ENOMEM;
233 1.11 ad goto fail;
234 1.2 gehenna }
235 1.33 matt newptr[old_convs].d_name = NULL;
236 1.33 matt newptr[old_convs].d_bmajor = -1;
237 1.33 matt newptr[old_convs].d_cmajor = -1;
238 1.33 matt memcpy(newptr, devsw_conv, old_convs * DEVSWCONV_SIZE);
239 1.2 gehenna if (devsw_conv != devsw_conv0)
240 1.33 matt kmem_free(devsw_conv, old_convs * DEVSWCONV_SIZE);
241 1.2 gehenna devsw_conv = newptr;
242 1.33 matt max_devsw_convs = new_convs;
243 1.2 gehenna }
244 1.2 gehenna
245 1.25 enami len = strlen(devname) + 1;
246 1.25 enami name = kmem_alloc(len, KM_NOSLEEP);
247 1.2 gehenna if (name == NULL) {
248 1.11 ad devsw_detach_locked(bdev, cdev);
249 1.25 enami error = ENOMEM;
250 1.11 ad goto fail;
251 1.2 gehenna }
252 1.25 enami strlcpy(name, devname, len);
253 1.2 gehenna
254 1.2 gehenna devsw_conv[i].d_name = name;
255 1.2 gehenna devsw_conv[i].d_bmajor = *bmajor;
256 1.2 gehenna devsw_conv[i].d_cmajor = *cmajor;
257 1.2 gehenna
258 1.23 pooka mutex_exit(&device_lock);
259 1.2 gehenna return (0);
260 1.11 ad fail:
261 1.23 pooka mutex_exit(&device_lock);
262 1.11 ad return (error);
263 1.2 gehenna }
264 1.2 gehenna
265 1.2 gehenna static int
266 1.24 drochner bdevsw_attach(const struct bdevsw *devsw, devmajor_t *devmajor)
267 1.2 gehenna {
268 1.11 ad const struct bdevsw **newptr;
269 1.24 drochner devmajor_t bmajor;
270 1.24 drochner int i;
271 1.2 gehenna
272 1.23 pooka KASSERT(mutex_owned(&device_lock));
273 1.11 ad
274 1.2 gehenna if (devsw == NULL)
275 1.2 gehenna return (0);
276 1.2 gehenna
277 1.2 gehenna if (*devmajor < 0) {
278 1.2 gehenna for (bmajor = sys_bdevsws ; bmajor < max_bdevsws ; bmajor++) {
279 1.2 gehenna if (bdevsw[bmajor] != NULL)
280 1.2 gehenna continue;
281 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
282 1.2 gehenna if (devsw_conv[i].d_bmajor == bmajor)
283 1.2 gehenna break;
284 1.2 gehenna }
285 1.2 gehenna if (i != max_devsw_convs)
286 1.2 gehenna continue;
287 1.2 gehenna break;
288 1.2 gehenna }
289 1.3 gehenna *devmajor = bmajor;
290 1.2 gehenna }
291 1.11 ad
292 1.2 gehenna if (*devmajor >= MAXDEVSW) {
293 1.37 pgoyette printf("%s: block majors exhausted", __func__);
294 1.2 gehenna return (ENOMEM);
295 1.2 gehenna }
296 1.2 gehenna
297 1.2 gehenna if (*devmajor >= max_bdevsws) {
298 1.11 ad KASSERT(bdevsw == bdevsw0);
299 1.11 ad newptr = kmem_zalloc(MAXDEVSW * BDEVSW_SIZE, KM_NOSLEEP);
300 1.2 gehenna if (newptr == NULL)
301 1.2 gehenna return (ENOMEM);
302 1.11 ad memcpy(newptr, bdevsw, max_bdevsws * BDEVSW_SIZE);
303 1.2 gehenna bdevsw = newptr;
304 1.11 ad max_bdevsws = MAXDEVSW;
305 1.2 gehenna }
306 1.2 gehenna
307 1.2 gehenna if (bdevsw[*devmajor] != NULL)
308 1.2 gehenna return (EEXIST);
309 1.2 gehenna
310 1.37.2.2 pgoyette if (devsw->d_localcount == NULL) {
311 1.37.2.2 pgoyette aprint_normal("%s: %s's bdevsw has no localcount",
312 1.37.2.2 pgoyette __func__, devname);
313 1.37.2.2 pgoyette return EINVAL;
314 1.37.2.2 pgoyette }
315 1.37.2.1 pgoyette localcount_init(devsw->d_localcount);
316 1.37.2.1 pgoyette
317 1.37.2.1 pgoyette /* ensure visibility of the bdevsw */
318 1.37.2.1 pgoyette membar_producer();
319 1.37.2.1 pgoyette
320 1.2 gehenna bdevsw[*devmajor] = devsw;
321 1.2 gehenna
322 1.2 gehenna return (0);
323 1.2 gehenna }
324 1.2 gehenna
325 1.2 gehenna static int
326 1.24 drochner cdevsw_attach(const struct cdevsw *devsw, devmajor_t *devmajor)
327 1.2 gehenna {
328 1.11 ad const struct cdevsw **newptr;
329 1.24 drochner devmajor_t cmajor;
330 1.24 drochner int i;
331 1.2 gehenna
332 1.23 pooka KASSERT(mutex_owned(&device_lock));
333 1.11 ad
334 1.2 gehenna if (*devmajor < 0) {
335 1.2 gehenna for (cmajor = sys_cdevsws ; cmajor < max_cdevsws ; cmajor++) {
336 1.2 gehenna if (cdevsw[cmajor] != NULL)
337 1.2 gehenna continue;
338 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
339 1.2 gehenna if (devsw_conv[i].d_cmajor == cmajor)
340 1.2 gehenna break;
341 1.2 gehenna }
342 1.2 gehenna if (i != max_devsw_convs)
343 1.2 gehenna continue;
344 1.2 gehenna break;
345 1.2 gehenna }
346 1.3 gehenna *devmajor = cmajor;
347 1.2 gehenna }
348 1.11 ad
349 1.2 gehenna if (*devmajor >= MAXDEVSW) {
350 1.37 pgoyette printf("%s: character majors exhausted", __func__);
351 1.2 gehenna return (ENOMEM);
352 1.2 gehenna }
353 1.2 gehenna
354 1.2 gehenna if (*devmajor >= max_cdevsws) {
355 1.11 ad KASSERT(cdevsw == cdevsw0);
356 1.11 ad newptr = kmem_zalloc(MAXDEVSW * CDEVSW_SIZE, KM_NOSLEEP);
357 1.2 gehenna if (newptr == NULL)
358 1.2 gehenna return (ENOMEM);
359 1.11 ad memcpy(newptr, cdevsw, max_cdevsws * CDEVSW_SIZE);
360 1.2 gehenna cdevsw = newptr;
361 1.11 ad max_cdevsws = MAXDEVSW;
362 1.2 gehenna }
363 1.2 gehenna
364 1.2 gehenna if (cdevsw[*devmajor] != NULL)
365 1.2 gehenna return (EEXIST);
366 1.2 gehenna
367 1.37.2.2 pgoyette if (devsw->d_localcount == NULL) {
368 1.37.2.2 pgoyette aprint_normal("%s: %s's cdevsw has no localcount",
369 1.37.2.2 pgoyette __func__, devname);
370 1.37.2.2 pgoyette return EINVAL;
371 1.37.2.2 pgoyette }
372 1.37.2.1 pgoyette localcount_init(devsw->d_localcount);
373 1.37.2.1 pgoyette
374 1.37.2.1 pgoyette /* ensure visibility of the cdevsw */
375 1.37.2.1 pgoyette membar_producer();
376 1.37.2.1 pgoyette
377 1.2 gehenna cdevsw[*devmajor] = devsw;
378 1.2 gehenna
379 1.2 gehenna return (0);
380 1.2 gehenna }
381 1.2 gehenna
382 1.37.2.1 pgoyette /*
383 1.37.2.1 pgoyette * First, look up both bdev and cdev indices, and remove the
384 1.37.2.1 pgoyette * {b,c]devsw[] entries so no new references can be taken. Then
385 1.37.2.1 pgoyette * drain any existing references.
386 1.37.2.1 pgoyette */
387 1.37.2.1 pgoyette
388 1.11 ad static void
389 1.11 ad devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
390 1.2 gehenna {
391 1.37.2.1 pgoyette int i, j;
392 1.2 gehenna
393 1.23 pooka KASSERT(mutex_owned(&device_lock));
394 1.11 ad
395 1.37.2.1 pgoyette i = max_bdevsws;
396 1.2 gehenna if (bdev != NULL) {
397 1.2 gehenna for (i = 0 ; i < max_bdevsws ; i++) {
398 1.2 gehenna if (bdevsw[i] != bdev)
399 1.2 gehenna continue;
400 1.37.2.1 pgoyette
401 1.37.2.1 pgoyette KASSERTMSG(bdev->d_localcount != NULL,
402 1.37.2.1 pgoyette "%s: no bdev localcount for major %d", __func__, i);
403 1.2 gehenna break;
404 1.2 gehenna }
405 1.2 gehenna }
406 1.37.2.1 pgoyette j = max_cdevsws;
407 1.2 gehenna if (cdev != NULL) {
408 1.37.2.1 pgoyette for (j = 0 ; j < max_cdevsws ; j++) {
409 1.37.2.1 pgoyette if (cdevsw[j] != cdev)
410 1.2 gehenna continue;
411 1.37.2.1 pgoyette
412 1.37.2.1 pgoyette KASSERTMSG(cdev->d_localcount != NULL,
413 1.37.2.1 pgoyette "%s: no cdev localcount for major %d", __func__, j);
414 1.2 gehenna break;
415 1.2 gehenna }
416 1.2 gehenna }
417 1.37.2.1 pgoyette if (i < max_bdevsws)
418 1.37.2.1 pgoyette bdevsw[i] = NULL;
419 1.37.2.1 pgoyette if (j < max_cdevsws )
420 1.37.2.1 pgoyette cdevsw[j] = NULL;
421 1.37.2.1 pgoyette
422 1.37.2.1 pgoyette /* Wait for all current readers to finish with the devsw's */
423 1.37.2.1 pgoyette pserialize_perform(device_psz);
424 1.37.2.1 pgoyette
425 1.37.2.1 pgoyette /*
426 1.37.2.1 pgoyette * No new accessors can reach the bdev and cdev via the
427 1.37.2.1 pgoyette * {b,c}devsw[] arrays, so no new references can be
428 1.37.2.1 pgoyette * acquired. Wait for all existing references to drain,
429 1.37.2.1 pgoyette * and then destroy.
430 1.37.2.1 pgoyette */
431 1.37.2.1 pgoyette
432 1.37.2.1 pgoyette if (i < max_bdevsws && bdev->d_localcount != NULL) {
433 1.37.2.1 pgoyette localcount_drain(bdev->d_localcount, &device_cv, &device_lock);
434 1.37.2.1 pgoyette localcount_fini(bdev->d_localcount);
435 1.37.2.1 pgoyette }
436 1.37.2.1 pgoyette if (j < max_cdevsws && cdev->d_localcount != NULL ) {
437 1.37.2.1 pgoyette localcount_drain(cdev->d_localcount, &device_cv, &device_lock);
438 1.37.2.1 pgoyette localcount_fini(cdev->d_localcount);
439 1.37.2.1 pgoyette }
440 1.2 gehenna }
441 1.2 gehenna
442 1.19 ad int
443 1.11 ad devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
444 1.11 ad {
445 1.11 ad
446 1.23 pooka mutex_enter(&device_lock);
447 1.11 ad devsw_detach_locked(bdev, cdev);
448 1.23 pooka mutex_exit(&device_lock);
449 1.19 ad return 0;
450 1.11 ad }
451 1.11 ad
452 1.11 ad /*
453 1.11 ad * Look up a block device by number.
454 1.11 ad *
455 1.11 ad * => Caller must ensure that the device is attached.
456 1.11 ad */
457 1.2 gehenna const struct bdevsw *
458 1.2 gehenna bdevsw_lookup(dev_t dev)
459 1.2 gehenna {
460 1.24 drochner devmajor_t bmajor;
461 1.2 gehenna
462 1.2 gehenna if (dev == NODEV)
463 1.2 gehenna return (NULL);
464 1.2 gehenna bmajor = major(dev);
465 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws)
466 1.2 gehenna return (NULL);
467 1.2 gehenna
468 1.37.2.1 pgoyette /* Wait for the content of the struct bdevsw to become visible */
469 1.37.2.1 pgoyette membar_datadep_consumer();
470 1.37.2.1 pgoyette
471 1.2 gehenna return (bdevsw[bmajor]);
472 1.2 gehenna }
473 1.2 gehenna
474 1.37.2.1 pgoyette const struct bdevsw *
475 1.37.2.1 pgoyette bdevsw_lookup_acquire(dev_t dev)
476 1.37.2.1 pgoyette {
477 1.37.2.1 pgoyette devmajor_t bmajor;
478 1.37.2.1 pgoyette const struct bdevsw *bdev = NULL;
479 1.37.2.1 pgoyette int s;
480 1.37.2.1 pgoyette
481 1.37.2.1 pgoyette if (dev == NODEV)
482 1.37.2.1 pgoyette return (NULL);
483 1.37.2.1 pgoyette bmajor = major(dev);
484 1.37.2.1 pgoyette if (bmajor < 0 || bmajor >= max_bdevsws)
485 1.37.2.1 pgoyette return (NULL);
486 1.37.2.1 pgoyette
487 1.37.2.1 pgoyette /* Start a read transaction to block localcount_drain() */
488 1.37.2.1 pgoyette s = pserialize_read_enter();
489 1.37.2.1 pgoyette
490 1.37.2.1 pgoyette /* Get the struct bdevsw pointer */
491 1.37.2.1 pgoyette bdev = bdevsw[bmajor];
492 1.37.2.1 pgoyette if (bdev == NULL)
493 1.37.2.1 pgoyette goto out;
494 1.37.2.1 pgoyette
495 1.37.2.1 pgoyette /* Wait for the content of the struct bdevsw to become visible */
496 1.37.2.1 pgoyette membar_datadep_consumer();
497 1.37.2.1 pgoyette
498 1.37.2.1 pgoyette /* If the devsw is not statically linked, acquire a reference */
499 1.37.2.1 pgoyette if (bdev->d_localcount != NULL)
500 1.37.2.1 pgoyette localcount_acquire(bdev->d_localcount);
501 1.37.2.1 pgoyette
502 1.37.2.1 pgoyette out: pserialize_read_exit(s);
503 1.37.2.1 pgoyette
504 1.37.2.1 pgoyette return bdev;
505 1.37.2.1 pgoyette }
506 1.37.2.1 pgoyette
507 1.37.2.1 pgoyette void
508 1.37.2.1 pgoyette bdevsw_release(const struct bdevsw *bd)
509 1.37.2.1 pgoyette {
510 1.37.2.1 pgoyette
511 1.37.2.1 pgoyette KASSERT(bd != NULL);
512 1.37.2.1 pgoyette if (bd->d_localcount != NULL)
513 1.37.2.1 pgoyette localcount_release(bd->d_localcount, &device_cv, &device_lock);
514 1.37.2.1 pgoyette }
515 1.37.2.1 pgoyette
516 1.11 ad /*
517 1.11 ad * Look up a character device by number.
518 1.11 ad *
519 1.11 ad * => Caller must ensure that the device is attached.
520 1.11 ad */
521 1.2 gehenna const struct cdevsw *
522 1.2 gehenna cdevsw_lookup(dev_t dev)
523 1.2 gehenna {
524 1.24 drochner devmajor_t cmajor;
525 1.2 gehenna
526 1.2 gehenna if (dev == NODEV)
527 1.2 gehenna return (NULL);
528 1.2 gehenna cmajor = major(dev);
529 1.2 gehenna if (cmajor < 0 || cmajor >= max_cdevsws)
530 1.2 gehenna return (NULL);
531 1.2 gehenna
532 1.37.2.1 pgoyette /* Wait for the content of the struct bdevsw to become visible */
533 1.37.2.1 pgoyette membar_datadep_consumer();
534 1.37.2.1 pgoyette
535 1.2 gehenna return (cdevsw[cmajor]);
536 1.2 gehenna }
537 1.2 gehenna
538 1.37.2.1 pgoyette const struct cdevsw *
539 1.37.2.1 pgoyette cdevsw_lookup_acquire(dev_t dev)
540 1.37.2.1 pgoyette {
541 1.37.2.1 pgoyette devmajor_t cmajor;
542 1.37.2.1 pgoyette const struct cdevsw *cdev = NULL;
543 1.37.2.1 pgoyette int s;
544 1.37.2.1 pgoyette
545 1.37.2.1 pgoyette if (dev == NODEV)
546 1.37.2.1 pgoyette return (NULL);
547 1.37.2.1 pgoyette cmajor = major(dev);
548 1.37.2.1 pgoyette if (cmajor < 0 || cmajor >= max_cdevsws)
549 1.37.2.1 pgoyette return (NULL);
550 1.37.2.1 pgoyette
551 1.37.2.1 pgoyette /* Start a read transaction to block localcount_drain() */
552 1.37.2.1 pgoyette s = pserialize_read_enter();
553 1.37.2.1 pgoyette
554 1.37.2.1 pgoyette /* Get the struct bdevsw pointer */
555 1.37.2.1 pgoyette cdev = cdevsw[cmajor];
556 1.37.2.1 pgoyette if (cdev == NULL)
557 1.37.2.1 pgoyette goto out;
558 1.37.2.1 pgoyette
559 1.37.2.1 pgoyette /* Wait for the content of the struct cdevsw to become visible */
560 1.37.2.1 pgoyette membar_datadep_consumer();
561 1.37.2.1 pgoyette
562 1.37.2.1 pgoyette /* If the devsw is not statically linked, acquire a reference */
563 1.37.2.1 pgoyette if (cdev->d_localcount != NULL)
564 1.37.2.1 pgoyette localcount_acquire(cdev->d_localcount);
565 1.37.2.1 pgoyette
566 1.37.2.1 pgoyette out: pserialize_read_exit(s);
567 1.37.2.1 pgoyette
568 1.37.2.1 pgoyette return cdev;
569 1.37.2.1 pgoyette }
570 1.37.2.1 pgoyette
571 1.37.2.1 pgoyette void
572 1.37.2.1 pgoyette cdevsw_release(const struct cdevsw *cd)
573 1.37.2.1 pgoyette {
574 1.37.2.1 pgoyette
575 1.37.2.1 pgoyette KASSERT(cd != NULL);
576 1.37.2.1 pgoyette if (cd->d_localcount != NULL)
577 1.37.2.1 pgoyette localcount_release(cd->d_localcount, &device_cv, &device_lock);
578 1.37.2.1 pgoyette }
579 1.37.2.1 pgoyette
580 1.11 ad /*
581 1.11 ad * Look up a block device by reference to its operations set.
582 1.11 ad *
583 1.11 ad * => Caller must ensure that the device is not detached, and therefore
584 1.11 ad * that the returned major is still valid when dereferenced.
585 1.11 ad */
586 1.24 drochner devmajor_t
587 1.2 gehenna bdevsw_lookup_major(const struct bdevsw *bdev)
588 1.2 gehenna {
589 1.24 drochner devmajor_t bmajor;
590 1.2 gehenna
591 1.2 gehenna for (bmajor = 0 ; bmajor < max_bdevsws ; bmajor++) {
592 1.2 gehenna if (bdevsw[bmajor] == bdev)
593 1.2 gehenna return (bmajor);
594 1.2 gehenna }
595 1.2 gehenna
596 1.24 drochner return (NODEVMAJOR);
597 1.2 gehenna }
598 1.2 gehenna
599 1.11 ad /*
600 1.11 ad * Look up a character device by reference to its operations set.
601 1.11 ad *
602 1.11 ad * => Caller must ensure that the device is not detached, and therefore
603 1.11 ad * that the returned major is still valid when dereferenced.
604 1.11 ad */
605 1.24 drochner devmajor_t
606 1.2 gehenna cdevsw_lookup_major(const struct cdevsw *cdev)
607 1.2 gehenna {
608 1.24 drochner devmajor_t cmajor;
609 1.2 gehenna
610 1.2 gehenna for (cmajor = 0 ; cmajor < max_cdevsws ; cmajor++) {
611 1.2 gehenna if (cdevsw[cmajor] == cdev)
612 1.2 gehenna return (cmajor);
613 1.2 gehenna }
614 1.2 gehenna
615 1.24 drochner return (NODEVMAJOR);
616 1.2 gehenna }
617 1.2 gehenna
618 1.2 gehenna /*
619 1.2 gehenna * Convert from block major number to name.
620 1.11 ad *
621 1.11 ad * => Caller must ensure that the device is not detached, and therefore
622 1.11 ad * that the name pointer is still valid when dereferenced.
623 1.2 gehenna */
624 1.2 gehenna const char *
625 1.24 drochner devsw_blk2name(devmajor_t bmajor)
626 1.2 gehenna {
627 1.11 ad const char *name;
628 1.24 drochner devmajor_t cmajor;
629 1.24 drochner int i;
630 1.2 gehenna
631 1.11 ad name = NULL;
632 1.11 ad cmajor = -1;
633 1.11 ad
634 1.23 pooka mutex_enter(&device_lock);
635 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
636 1.23 pooka mutex_exit(&device_lock);
637 1.2 gehenna return (NULL);
638 1.2 gehenna }
639 1.11 ad for (i = 0 ; i < max_devsw_convs; i++) {
640 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
641 1.11 ad cmajor = devsw_conv[i].d_cmajor;
642 1.11 ad break;
643 1.11 ad }
644 1.11 ad }
645 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
646 1.11 ad name = devsw_conv[i].d_name;
647 1.23 pooka mutex_exit(&device_lock);
648 1.2 gehenna
649 1.11 ad return (name);
650 1.2 gehenna }
651 1.2 gehenna
652 1.2 gehenna /*
653 1.26 haad * Convert char major number to device driver name.
654 1.26 haad */
655 1.27 yamt const char *
656 1.26 haad cdevsw_getname(devmajor_t major)
657 1.26 haad {
658 1.26 haad const char *name;
659 1.26 haad int i;
660 1.26 haad
661 1.26 haad name = NULL;
662 1.26 haad
663 1.26 haad if (major < 0)
664 1.26 haad return (NULL);
665 1.26 haad
666 1.26 haad mutex_enter(&device_lock);
667 1.26 haad for (i = 0 ; i < max_devsw_convs; i++) {
668 1.26 haad if (devsw_conv[i].d_cmajor == major) {
669 1.26 haad name = devsw_conv[i].d_name;
670 1.26 haad break;
671 1.26 haad }
672 1.26 haad }
673 1.26 haad mutex_exit(&device_lock);
674 1.26 haad return (name);
675 1.26 haad }
676 1.26 haad
677 1.26 haad /*
678 1.26 haad * Convert block major number to device driver name.
679 1.26 haad */
680 1.27 yamt const char *
681 1.26 haad bdevsw_getname(devmajor_t major)
682 1.26 haad {
683 1.26 haad const char *name;
684 1.26 haad int i;
685 1.26 haad
686 1.26 haad name = NULL;
687 1.26 haad
688 1.26 haad if (major < 0)
689 1.26 haad return (NULL);
690 1.26 haad
691 1.26 haad mutex_enter(&device_lock);
692 1.26 haad for (i = 0 ; i < max_devsw_convs; i++) {
693 1.26 haad if (devsw_conv[i].d_bmajor == major) {
694 1.26 haad name = devsw_conv[i].d_name;
695 1.26 haad break;
696 1.26 haad }
697 1.26 haad }
698 1.26 haad mutex_exit(&device_lock);
699 1.26 haad return (name);
700 1.26 haad }
701 1.26 haad
702 1.26 haad /*
703 1.2 gehenna * Convert from device name to block major number.
704 1.11 ad *
705 1.11 ad * => Caller must ensure that the device is not detached, and therefore
706 1.11 ad * that the major number is still valid when dereferenced.
707 1.2 gehenna */
708 1.24 drochner devmajor_t
709 1.2 gehenna devsw_name2blk(const char *name, char *devname, size_t devnamelen)
710 1.2 gehenna {
711 1.2 gehenna struct devsw_conv *conv;
712 1.24 drochner devmajor_t bmajor;
713 1.24 drochner int i;
714 1.2 gehenna
715 1.2 gehenna if (name == NULL)
716 1.24 drochner return (NODEVMAJOR);
717 1.2 gehenna
718 1.23 pooka mutex_enter(&device_lock);
719 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
720 1.5 mrg size_t len;
721 1.5 mrg
722 1.2 gehenna conv = &devsw_conv[i];
723 1.2 gehenna if (conv->d_name == NULL)
724 1.2 gehenna continue;
725 1.5 mrg len = strlen(conv->d_name);
726 1.5 mrg if (strncmp(conv->d_name, name, len) != 0)
727 1.5 mrg continue;
728 1.5 mrg if (*(name +len) && !isdigit(*(name + len)))
729 1.2 gehenna continue;
730 1.2 gehenna bmajor = conv->d_bmajor;
731 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws ||
732 1.2 gehenna bdevsw[bmajor] == NULL)
733 1.5 mrg break;
734 1.2 gehenna if (devname != NULL) {
735 1.2 gehenna #ifdef DEVSW_DEBUG
736 1.2 gehenna if (strlen(conv->d_name) >= devnamelen)
737 1.37 pgoyette printf("%s: too short buffer", __func__);
738 1.2 gehenna #endif /* DEVSW_DEBUG */
739 1.4 tsutsui strncpy(devname, conv->d_name, devnamelen);
740 1.2 gehenna devname[devnamelen - 1] = '\0';
741 1.2 gehenna }
742 1.23 pooka mutex_exit(&device_lock);
743 1.2 gehenna return (bmajor);
744 1.2 gehenna }
745 1.2 gehenna
746 1.23 pooka mutex_exit(&device_lock);
747 1.24 drochner return (NODEVMAJOR);
748 1.2 gehenna }
749 1.2 gehenna
750 1.2 gehenna /*
751 1.16 plunky * Convert from device name to char major number.
752 1.16 plunky *
753 1.16 plunky * => Caller must ensure that the device is not detached, and therefore
754 1.16 plunky * that the major number is still valid when dereferenced.
755 1.16 plunky */
756 1.24 drochner devmajor_t
757 1.16 plunky devsw_name2chr(const char *name, char *devname, size_t devnamelen)
758 1.16 plunky {
759 1.16 plunky struct devsw_conv *conv;
760 1.24 drochner devmajor_t cmajor;
761 1.24 drochner int i;
762 1.16 plunky
763 1.16 plunky if (name == NULL)
764 1.24 drochner return (NODEVMAJOR);
765 1.16 plunky
766 1.23 pooka mutex_enter(&device_lock);
767 1.16 plunky for (i = 0 ; i < max_devsw_convs ; i++) {
768 1.16 plunky size_t len;
769 1.16 plunky
770 1.16 plunky conv = &devsw_conv[i];
771 1.16 plunky if (conv->d_name == NULL)
772 1.16 plunky continue;
773 1.16 plunky len = strlen(conv->d_name);
774 1.16 plunky if (strncmp(conv->d_name, name, len) != 0)
775 1.16 plunky continue;
776 1.16 plunky if (*(name +len) && !isdigit(*(name + len)))
777 1.16 plunky continue;
778 1.16 plunky cmajor = conv->d_cmajor;
779 1.16 plunky if (cmajor < 0 || cmajor >= max_cdevsws ||
780 1.16 plunky cdevsw[cmajor] == NULL)
781 1.16 plunky break;
782 1.16 plunky if (devname != NULL) {
783 1.16 plunky #ifdef DEVSW_DEBUG
784 1.16 plunky if (strlen(conv->d_name) >= devnamelen)
785 1.37 pgoyette printf("%s: too short buffer", __func__);
786 1.16 plunky #endif /* DEVSW_DEBUG */
787 1.16 plunky strncpy(devname, conv->d_name, devnamelen);
788 1.16 plunky devname[devnamelen - 1] = '\0';
789 1.16 plunky }
790 1.23 pooka mutex_exit(&device_lock);
791 1.16 plunky return (cmajor);
792 1.16 plunky }
793 1.16 plunky
794 1.23 pooka mutex_exit(&device_lock);
795 1.24 drochner return (NODEVMAJOR);
796 1.16 plunky }
797 1.16 plunky
798 1.16 plunky /*
799 1.2 gehenna * Convert from character dev_t to block dev_t.
800 1.11 ad *
801 1.11 ad * => Caller must ensure that the device is not detached, and therefore
802 1.11 ad * that the major number is still valid when dereferenced.
803 1.2 gehenna */
804 1.2 gehenna dev_t
805 1.2 gehenna devsw_chr2blk(dev_t cdev)
806 1.2 gehenna {
807 1.24 drochner devmajor_t bmajor, cmajor;
808 1.24 drochner int i;
809 1.11 ad dev_t rv;
810 1.2 gehenna
811 1.2 gehenna cmajor = major(cdev);
812 1.24 drochner bmajor = NODEVMAJOR;
813 1.11 ad rv = NODEV;
814 1.2 gehenna
815 1.23 pooka mutex_enter(&device_lock);
816 1.11 ad if (cmajor < 0 || cmajor >= max_cdevsws || cdevsw[cmajor] == NULL) {
817 1.23 pooka mutex_exit(&device_lock);
818 1.11 ad return (NODEV);
819 1.11 ad }
820 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
821 1.11 ad if (devsw_conv[i].d_cmajor == cmajor) {
822 1.11 ad bmajor = devsw_conv[i].d_bmajor;
823 1.11 ad break;
824 1.11 ad }
825 1.2 gehenna }
826 1.11 ad if (bmajor >= 0 && bmajor < max_bdevsws && bdevsw[bmajor] != NULL)
827 1.11 ad rv = makedev(bmajor, minor(cdev));
828 1.23 pooka mutex_exit(&device_lock);
829 1.2 gehenna
830 1.11 ad return (rv);
831 1.2 gehenna }
832 1.2 gehenna
833 1.2 gehenna /*
834 1.2 gehenna * Convert from block dev_t to character dev_t.
835 1.11 ad *
836 1.11 ad * => Caller must ensure that the device is not detached, and therefore
837 1.11 ad * that the major number is still valid when dereferenced.
838 1.2 gehenna */
839 1.2 gehenna dev_t
840 1.2 gehenna devsw_blk2chr(dev_t bdev)
841 1.2 gehenna {
842 1.24 drochner devmajor_t bmajor, cmajor;
843 1.24 drochner int i;
844 1.11 ad dev_t rv;
845 1.2 gehenna
846 1.11 ad bmajor = major(bdev);
847 1.24 drochner cmajor = NODEVMAJOR;
848 1.11 ad rv = NODEV;
849 1.11 ad
850 1.23 pooka mutex_enter(&device_lock);
851 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
852 1.23 pooka mutex_exit(&device_lock);
853 1.2 gehenna return (NODEV);
854 1.11 ad }
855 1.11 ad for (i = 0 ; i < max_devsw_convs ; i++) {
856 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
857 1.11 ad cmajor = devsw_conv[i].d_cmajor;
858 1.11 ad break;
859 1.11 ad }
860 1.11 ad }
861 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
862 1.11 ad rv = makedev(cmajor, minor(bdev));
863 1.23 pooka mutex_exit(&device_lock);
864 1.2 gehenna
865 1.11 ad return (rv);
866 1.11 ad }
867 1.11 ad
868 1.11 ad /*
869 1.11 ad * Device access methods.
870 1.11 ad */
871 1.11 ad
872 1.11 ad #define DEV_LOCK(d) \
873 1.17 ad if ((mpflag = (d->d_flag & D_MPSAFE)) == 0) { \
874 1.17 ad KERNEL_LOCK(1, NULL); \
875 1.11 ad }
876 1.2 gehenna
877 1.11 ad #define DEV_UNLOCK(d) \
878 1.17 ad if (mpflag == 0) { \
879 1.17 ad KERNEL_UNLOCK_ONE(NULL); \
880 1.2 gehenna }
881 1.2 gehenna
882 1.11 ad int
883 1.11 ad bdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
884 1.11 ad {
885 1.11 ad const struct bdevsw *d;
886 1.17 ad int rv, mpflag;
887 1.11 ad
888 1.11 ad /*
889 1.11 ad * For open we need to lock, in order to synchronize
890 1.11 ad * with attach/detach.
891 1.11 ad */
892 1.23 pooka mutex_enter(&device_lock);
893 1.37.2.1 pgoyette d = bdevsw_lookup_acquire(dev);
894 1.23 pooka mutex_exit(&device_lock);
895 1.11 ad if (d == NULL)
896 1.11 ad return ENXIO;
897 1.11 ad
898 1.11 ad DEV_LOCK(d);
899 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
900 1.11 ad DEV_UNLOCK(d);
901 1.37.2.1 pgoyette bdevsw_release(d);
902 1.11 ad
903 1.11 ad return rv;
904 1.11 ad }
905 1.11 ad
906 1.11 ad int
907 1.11 ad bdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
908 1.11 ad {
909 1.11 ad const struct bdevsw *d;
910 1.17 ad int rv, mpflag;
911 1.11 ad
912 1.37.2.1 pgoyette if ((d = bdevsw_lookup_acquire(dev)) == NULL)
913 1.11 ad return ENXIO;
914 1.11 ad
915 1.11 ad DEV_LOCK(d);
916 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
917 1.11 ad DEV_UNLOCK(d);
918 1.37.2.1 pgoyette bdevsw_release(d);
919 1.11 ad
920 1.11 ad return rv;
921 1.11 ad }
922 1.11 ad
923 1.34 riz SDT_PROVIDER_DECLARE(io);
924 1.34 riz SDT_PROBE_DEFINE1(io, kernel, , start, "struct buf *"/*bp*/);
925 1.34 riz
926 1.11 ad void
927 1.11 ad bdev_strategy(struct buf *bp)
928 1.11 ad {
929 1.11 ad const struct bdevsw *d;
930 1.17 ad int mpflag;
931 1.11 ad
932 1.34 riz SDT_PROBE1(io, kernel, , start, bp);
933 1.34 riz
934 1.37.2.1 pgoyette if ((d = bdevsw_lookup_acquire(bp->b_dev)) == NULL) {
935 1.28 jmcneill bp->b_error = ENXIO;
936 1.28 jmcneill bp->b_resid = bp->b_bcount;
937 1.31 pooka biodone_vfs(bp); /* biodone() iff vfs present */
938 1.28 jmcneill return;
939 1.28 jmcneill }
940 1.11 ad
941 1.11 ad DEV_LOCK(d);
942 1.11 ad (*d->d_strategy)(bp);
943 1.11 ad DEV_UNLOCK(d);
944 1.37.2.1 pgoyette bdevsw_release(d);
945 1.11 ad }
946 1.11 ad
947 1.11 ad int
948 1.11 ad bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
949 1.11 ad {
950 1.11 ad const struct bdevsw *d;
951 1.17 ad int rv, mpflag;
952 1.11 ad
953 1.37.2.1 pgoyette if ((d = bdevsw_lookup_acquire(dev)) == NULL)
954 1.11 ad return ENXIO;
955 1.11 ad
956 1.11 ad DEV_LOCK(d);
957 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
958 1.11 ad DEV_UNLOCK(d);
959 1.37.2.1 pgoyette bdevsw_release(d);
960 1.11 ad
961 1.11 ad return rv;
962 1.11 ad }
963 1.11 ad
964 1.11 ad int
965 1.11 ad bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
966 1.11 ad {
967 1.11 ad const struct bdevsw *d;
968 1.11 ad int rv;
969 1.11 ad
970 1.11 ad /*
971 1.11 ad * Dump can be called without the device open. Since it can
972 1.11 ad * currently only be called with the system paused (and in a
973 1.11 ad * potentially unstable state), we don't perform any locking.
974 1.11 ad */
975 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
976 1.11 ad return ENXIO;
977 1.11 ad
978 1.11 ad /* DEV_LOCK(d); */
979 1.11 ad rv = (*d->d_dump)(dev, addr, data, sz);
980 1.11 ad /* DEV_UNLOCK(d); */
981 1.11 ad
982 1.11 ad return rv;
983 1.11 ad }
984 1.11 ad
985 1.11 ad int
986 1.35 nat bdev_flags(dev_t dev)
987 1.35 nat {
988 1.35 nat const struct bdevsw *d;
989 1.37.2.1 pgoyette int rv;
990 1.35 nat
991 1.37.2.1 pgoyette if ((d = bdevsw_lookup_acquire(dev)) == NULL)
992 1.35 nat return 0;
993 1.37.2.1 pgoyette
994 1.37.2.1 pgoyette rv = d->d_flag & ~D_TYPEMASK;
995 1.37.2.1 pgoyette bdevsw_release(d);
996 1.37.2.1 pgoyette
997 1.37.2.1 pgoyette return rv;
998 1.35 nat }
999 1.35 nat
1000 1.35 nat int
1001 1.11 ad bdev_type(dev_t dev)
1002 1.11 ad {
1003 1.11 ad const struct bdevsw *d;
1004 1.37.2.1 pgoyette int rv;
1005 1.11 ad
1006 1.37.2.1 pgoyette if ((d = bdevsw_lookup_acquire(dev)) == NULL)
1007 1.11 ad return D_OTHER;
1008 1.37.2.1 pgoyette
1009 1.37.2.1 pgoyette rv = d->d_flag & D_TYPEMASK;
1010 1.37.2.1 pgoyette bdevsw_release(d);
1011 1.37.2.1 pgoyette
1012 1.37.2.1 pgoyette return rv;
1013 1.11 ad }
1014 1.11 ad
1015 1.11 ad int
1016 1.29 mrg bdev_size(dev_t dev)
1017 1.29 mrg {
1018 1.29 mrg const struct bdevsw *d;
1019 1.29 mrg int rv, mpflag = 0;
1020 1.29 mrg
1021 1.37.2.1 pgoyette if ((d = bdevsw_lookup_acquire(dev)) == NULL)
1022 1.29 mrg return -1;
1023 1.29 mrg
1024 1.37.2.1 pgoyette if (d->d_psize == NULL) {
1025 1.37.2.1 pgoyette bdevsw_release(d);
1026 1.37.2.1 pgoyette return -1;
1027 1.37.2.1 pgoyette }
1028 1.37.2.1 pgoyette
1029 1.29 mrg /*
1030 1.29 mrg * Don't to try lock the device if we're dumping.
1031 1.30 mrg * XXX: is there a better way to test this?
1032 1.29 mrg */
1033 1.29 mrg if ((boothowto & RB_DUMP) == 0)
1034 1.29 mrg DEV_LOCK(d);
1035 1.29 mrg rv = (*d->d_psize)(dev);
1036 1.29 mrg if ((boothowto & RB_DUMP) == 0)
1037 1.29 mrg DEV_UNLOCK(d);
1038 1.37.2.1 pgoyette bdevsw_release(d);
1039 1.29 mrg return rv;
1040 1.29 mrg }
1041 1.29 mrg
1042 1.29 mrg int
1043 1.32 dholland bdev_discard(dev_t dev, off_t pos, off_t len)
1044 1.32 dholland {
1045 1.32 dholland const struct bdevsw *d;
1046 1.32 dholland int rv, mpflag;
1047 1.32 dholland
1048 1.37.2.1 pgoyette if ((d = bdevsw_lookup_acquire(dev)) == NULL)
1049 1.32 dholland return ENXIO;
1050 1.32 dholland
1051 1.32 dholland DEV_LOCK(d);
1052 1.32 dholland rv = (*d->d_discard)(dev, pos, len);
1053 1.32 dholland DEV_UNLOCK(d);
1054 1.37.2.1 pgoyette bdevsw_release(d);
1055 1.32 dholland
1056 1.32 dholland return rv;
1057 1.32 dholland }
1058 1.32 dholland
1059 1.32 dholland int
1060 1.11 ad cdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
1061 1.11 ad {
1062 1.11 ad const struct cdevsw *d;
1063 1.17 ad int rv, mpflag;
1064 1.11 ad
1065 1.11 ad /*
1066 1.11 ad * For open we need to lock, in order to synchronize
1067 1.11 ad * with attach/detach.
1068 1.11 ad */
1069 1.23 pooka mutex_enter(&device_lock);
1070 1.37.2.1 pgoyette d = cdevsw_lookup_acquire(dev);
1071 1.23 pooka mutex_exit(&device_lock);
1072 1.11 ad if (d == NULL)
1073 1.11 ad return ENXIO;
1074 1.11 ad
1075 1.11 ad DEV_LOCK(d);
1076 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
1077 1.11 ad DEV_UNLOCK(d);
1078 1.37.2.1 pgoyette cdevsw_release(d);
1079 1.11 ad
1080 1.11 ad return rv;
1081 1.11 ad }
1082 1.11 ad
1083 1.11 ad int
1084 1.11 ad cdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
1085 1.11 ad {
1086 1.11 ad const struct cdevsw *d;
1087 1.17 ad int rv, mpflag;
1088 1.11 ad
1089 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1090 1.11 ad return ENXIO;
1091 1.11 ad
1092 1.11 ad DEV_LOCK(d);
1093 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
1094 1.11 ad DEV_UNLOCK(d);
1095 1.37.2.1 pgoyette cdevsw_release(d);
1096 1.11 ad
1097 1.11 ad return rv;
1098 1.11 ad }
1099 1.11 ad
1100 1.11 ad int
1101 1.11 ad cdev_read(dev_t dev, struct uio *uio, int flag)
1102 1.11 ad {
1103 1.11 ad const struct cdevsw *d;
1104 1.17 ad int rv, mpflag;
1105 1.11 ad
1106 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1107 1.11 ad return ENXIO;
1108 1.11 ad
1109 1.11 ad DEV_LOCK(d);
1110 1.11 ad rv = (*d->d_read)(dev, uio, flag);
1111 1.11 ad DEV_UNLOCK(d);
1112 1.37.2.1 pgoyette cdevsw_release(d);
1113 1.11 ad
1114 1.11 ad return rv;
1115 1.11 ad }
1116 1.11 ad
1117 1.11 ad int
1118 1.11 ad cdev_write(dev_t dev, struct uio *uio, int flag)
1119 1.11 ad {
1120 1.11 ad const struct cdevsw *d;
1121 1.17 ad int rv, mpflag;
1122 1.11 ad
1123 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1124 1.11 ad return ENXIO;
1125 1.11 ad
1126 1.11 ad DEV_LOCK(d);
1127 1.11 ad rv = (*d->d_write)(dev, uio, flag);
1128 1.11 ad DEV_UNLOCK(d);
1129 1.37.2.1 pgoyette cdevsw_release(d);
1130 1.11 ad
1131 1.11 ad return rv;
1132 1.11 ad }
1133 1.11 ad
1134 1.11 ad int
1135 1.11 ad cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
1136 1.11 ad {
1137 1.11 ad const struct cdevsw *d;
1138 1.17 ad int rv, mpflag;
1139 1.11 ad
1140 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1141 1.11 ad return ENXIO;
1142 1.11 ad
1143 1.11 ad DEV_LOCK(d);
1144 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
1145 1.11 ad DEV_UNLOCK(d);
1146 1.37.2.1 pgoyette cdevsw_release(d);
1147 1.11 ad
1148 1.11 ad return rv;
1149 1.11 ad }
1150 1.11 ad
1151 1.11 ad void
1152 1.11 ad cdev_stop(struct tty *tp, int flag)
1153 1.11 ad {
1154 1.11 ad const struct cdevsw *d;
1155 1.17 ad int mpflag;
1156 1.11 ad
1157 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(tp->t_dev)) == NULL)
1158 1.11 ad return;
1159 1.11 ad
1160 1.11 ad DEV_LOCK(d);
1161 1.11 ad (*d->d_stop)(tp, flag);
1162 1.11 ad DEV_UNLOCK(d);
1163 1.37.2.1 pgoyette cdevsw_release(d);
1164 1.11 ad }
1165 1.11 ad
1166 1.11 ad struct tty *
1167 1.11 ad cdev_tty(dev_t dev)
1168 1.11 ad {
1169 1.11 ad const struct cdevsw *d;
1170 1.37.2.1 pgoyette struct tty *rv;
1171 1.11 ad
1172 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1173 1.11 ad return NULL;
1174 1.11 ad
1175 1.12 ad /* XXX Check if necessary. */
1176 1.12 ad if (d->d_tty == NULL)
1177 1.37.2.1 pgoyette rv = NULL;
1178 1.37.2.1 pgoyette else
1179 1.37.2.1 pgoyette rv= (*d->d_tty)(dev);
1180 1.37.2.1 pgoyette cdevsw_release(d);
1181 1.12 ad
1182 1.37.2.1 pgoyette return rv;
1183 1.11 ad }
1184 1.11 ad
1185 1.11 ad int
1186 1.11 ad cdev_poll(dev_t dev, int flag, lwp_t *l)
1187 1.11 ad {
1188 1.11 ad const struct cdevsw *d;
1189 1.17 ad int rv, mpflag;
1190 1.11 ad
1191 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1192 1.11 ad return POLLERR;
1193 1.11 ad
1194 1.11 ad DEV_LOCK(d);
1195 1.11 ad rv = (*d->d_poll)(dev, flag, l);
1196 1.11 ad DEV_UNLOCK(d);
1197 1.37.2.1 pgoyette cdevsw_release(d);
1198 1.11 ad
1199 1.11 ad return rv;
1200 1.11 ad }
1201 1.11 ad
1202 1.11 ad paddr_t
1203 1.11 ad cdev_mmap(dev_t dev, off_t off, int flag)
1204 1.11 ad {
1205 1.11 ad const struct cdevsw *d;
1206 1.11 ad paddr_t rv;
1207 1.17 ad int mpflag;
1208 1.11 ad
1209 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1210 1.11 ad return (paddr_t)-1LL;
1211 1.11 ad
1212 1.11 ad DEV_LOCK(d);
1213 1.11 ad rv = (*d->d_mmap)(dev, off, flag);
1214 1.11 ad DEV_UNLOCK(d);
1215 1.37.2.1 pgoyette cdevsw_release(d);
1216 1.11 ad
1217 1.11 ad return rv;
1218 1.11 ad }
1219 1.11 ad
1220 1.11 ad int
1221 1.11 ad cdev_kqfilter(dev_t dev, struct knote *kn)
1222 1.11 ad {
1223 1.11 ad const struct cdevsw *d;
1224 1.17 ad int rv, mpflag;
1225 1.11 ad
1226 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1227 1.11 ad return ENXIO;
1228 1.11 ad
1229 1.11 ad DEV_LOCK(d);
1230 1.11 ad rv = (*d->d_kqfilter)(dev, kn);
1231 1.11 ad DEV_UNLOCK(d);
1232 1.37.2.1 pgoyette cdevsw_release(d);
1233 1.11 ad
1234 1.11 ad return rv;
1235 1.11 ad }
1236 1.11 ad
1237 1.11 ad int
1238 1.32 dholland cdev_discard(dev_t dev, off_t pos, off_t len)
1239 1.32 dholland {
1240 1.32 dholland const struct cdevsw *d;
1241 1.32 dholland int rv, mpflag;
1242 1.32 dholland
1243 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1244 1.32 dholland return ENXIO;
1245 1.32 dholland
1246 1.32 dholland DEV_LOCK(d);
1247 1.32 dholland rv = (*d->d_discard)(dev, pos, len);
1248 1.32 dholland DEV_UNLOCK(d);
1249 1.37.2.1 pgoyette cdevsw_release(d);
1250 1.32 dholland
1251 1.32 dholland return rv;
1252 1.32 dholland }
1253 1.32 dholland
1254 1.32 dholland int
1255 1.35 nat cdev_flags(dev_t dev)
1256 1.35 nat {
1257 1.35 nat const struct cdevsw *d;
1258 1.37.2.1 pgoyette int rv;
1259 1.35 nat
1260 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1261 1.35 nat return 0;
1262 1.37.2.1 pgoyette
1263 1.37.2.1 pgoyette rv = d->d_flag & ~D_TYPEMASK;
1264 1.37.2.1 pgoyette cdevsw_release(d);
1265 1.37.2.1 pgoyette
1266 1.37.2.1 pgoyette return rv;
1267 1.35 nat }
1268 1.35 nat
1269 1.35 nat int
1270 1.11 ad cdev_type(dev_t dev)
1271 1.11 ad {
1272 1.11 ad const struct cdevsw *d;
1273 1.37.2.1 pgoyette int rv;
1274 1.11 ad
1275 1.37.2.1 pgoyette if ((d = cdevsw_lookup_acquire(dev)) == NULL)
1276 1.11 ad return D_OTHER;
1277 1.37.2.1 pgoyette
1278 1.37.2.1 pgoyette rv = d->d_flag & D_TYPEMASK;
1279 1.37.2.1 pgoyette cdevsw_release(d);
1280 1.37.2.1 pgoyette
1281 1.37.2.1 pgoyette return rv;
1282 1.2 gehenna }
1283 1.36 riastrad
1284 1.36 riastrad /*
1285 1.36 riastrad * nommap(dev, off, prot)
1286 1.36 riastrad *
1287 1.36 riastrad * mmap routine that always fails, for non-mmappable devices.
1288 1.36 riastrad */
1289 1.36 riastrad paddr_t
1290 1.36 riastrad nommap(dev_t dev, off_t off, int prot)
1291 1.36 riastrad {
1292 1.36 riastrad
1293 1.36 riastrad return (paddr_t)-1;
1294 1.36 riastrad }
1295