subr_devsw.c revision 1.34.2.4 1 1.34.2.4 pgoyette /* $NetBSD: subr_devsw.c,v 1.34.2.4 2016/07/17 05:02:19 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.34.2.4 pgoyette __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.34.2.4 2016/07/17 05:02:19 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.34.2.2 pgoyette #include <sys/atomic.h>
89 1.34.2.1 pgoyette #include <sys/condvar.h>
90 1.34.2.1 pgoyette #include <sys/localcount.h>
91 1.34.2.2 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.34.2.1 pgoyette kmutex_t device_lock;
115 1.34.2.1 pgoyette kcondvar_t device_cv;
116 1.34.2.4 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.34.2.1 pgoyette cv_init(&device_cv, "devsw");
128 1.11 ad }
129 1.2 gehenna
130 1.2 gehenna int
131 1.24 drochner devsw_attach(const char *devname,
132 1.24 drochner const struct bdevsw *bdev, devmajor_t *bmajor,
133 1.24 drochner const struct cdevsw *cdev, devmajor_t *cmajor)
134 1.2 gehenna {
135 1.2 gehenna struct devsw_conv *conv;
136 1.2 gehenna char *name;
137 1.2 gehenna int error, i;
138 1.25 enami size_t len;
139 1.2 gehenna
140 1.2 gehenna if (devname == NULL || cdev == NULL)
141 1.2 gehenna return (EINVAL);
142 1.2 gehenna
143 1.23 pooka mutex_enter(&device_lock);
144 1.11 ad
145 1.34.2.2 pgoyette if (bdev != NULL) {
146 1.34.2.2 pgoyette KASSERT(bdev->d_localcount != NULL);
147 1.34.2.2 pgoyette KASSERT(bdev->d_localcount != cdev->d_localcount);
148 1.34.2.2 pgoyette }
149 1.34.2.2 pgoyette if (cdev != NULL)
150 1.34.2.2 pgoyette KASSERT(cdev->d_localcount != NULL);
151 1.34.2.2 pgoyette
152 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
153 1.2 gehenna conv = &devsw_conv[i];
154 1.2 gehenna if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
155 1.2 gehenna continue;
156 1.2 gehenna
157 1.2 gehenna if (*bmajor < 0)
158 1.2 gehenna *bmajor = conv->d_bmajor;
159 1.2 gehenna if (*cmajor < 0)
160 1.2 gehenna *cmajor = conv->d_cmajor;
161 1.2 gehenna
162 1.11 ad if (*bmajor != conv->d_bmajor || *cmajor != conv->d_cmajor) {
163 1.11 ad error = EINVAL;
164 1.11 ad goto fail;
165 1.11 ad }
166 1.11 ad if ((*bmajor >= 0 && bdev == NULL) || *cmajor < 0) {
167 1.11 ad error = EINVAL;
168 1.11 ad goto fail;
169 1.11 ad }
170 1.2 gehenna
171 1.2 gehenna if ((*bmajor >= 0 && bdevsw[*bmajor] != NULL) ||
172 1.11 ad cdevsw[*cmajor] != NULL) {
173 1.11 ad error = EEXIST;
174 1.11 ad goto fail;
175 1.11 ad }
176 1.2 gehenna
177 1.34.2.2 pgoyette /* use membar_producer() to ensure visibility of the xdevsw */
178 1.34.2.1 pgoyette if (bdev != NULL) {
179 1.34.2.1 pgoyette localcount_init(bdev->d_localcount);
180 1.34.2.2 pgoyette membar_producer();
181 1.2 gehenna bdevsw[*bmajor] = bdev;
182 1.34.2.1 pgoyette }
183 1.34.2.1 pgoyette localcount_init(cdev->d_localcount);
184 1.34.2.2 pgoyette membar_producer();
185 1.2 gehenna cdevsw[*cmajor] = cdev;
186 1.2 gehenna
187 1.23 pooka mutex_exit(&device_lock);
188 1.2 gehenna return (0);
189 1.2 gehenna }
190 1.2 gehenna
191 1.14 pooka error = bdevsw_attach(bdev, bmajor);
192 1.11 ad if (error != 0)
193 1.11 ad goto fail;
194 1.14 pooka error = cdevsw_attach(cdev, cmajor);
195 1.2 gehenna if (error != 0) {
196 1.11 ad devsw_detach_locked(bdev, NULL);
197 1.11 ad goto fail;
198 1.2 gehenna }
199 1.2 gehenna
200 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
201 1.2 gehenna if (devsw_conv[i].d_name == NULL)
202 1.2 gehenna break;
203 1.2 gehenna }
204 1.2 gehenna if (i == max_devsw_convs) {
205 1.2 gehenna struct devsw_conv *newptr;
206 1.33 matt int old_convs, new_convs;
207 1.2 gehenna
208 1.33 matt old_convs = max_devsw_convs;
209 1.33 matt new_convs = old_convs + 1;
210 1.2 gehenna
211 1.33 matt newptr = kmem_zalloc(new_convs * DEVSWCONV_SIZE, KM_NOSLEEP);
212 1.2 gehenna if (newptr == NULL) {
213 1.11 ad devsw_detach_locked(bdev, cdev);
214 1.11 ad error = ENOMEM;
215 1.11 ad goto fail;
216 1.2 gehenna }
217 1.33 matt newptr[old_convs].d_name = NULL;
218 1.33 matt newptr[old_convs].d_bmajor = -1;
219 1.33 matt newptr[old_convs].d_cmajor = -1;
220 1.33 matt memcpy(newptr, devsw_conv, old_convs * DEVSWCONV_SIZE);
221 1.2 gehenna if (devsw_conv != devsw_conv0)
222 1.33 matt kmem_free(devsw_conv, old_convs * DEVSWCONV_SIZE);
223 1.2 gehenna devsw_conv = newptr;
224 1.33 matt max_devsw_convs = new_convs;
225 1.2 gehenna }
226 1.2 gehenna
227 1.25 enami len = strlen(devname) + 1;
228 1.25 enami name = kmem_alloc(len, KM_NOSLEEP);
229 1.2 gehenna if (name == NULL) {
230 1.11 ad devsw_detach_locked(bdev, cdev);
231 1.25 enami error = ENOMEM;
232 1.11 ad goto fail;
233 1.2 gehenna }
234 1.25 enami strlcpy(name, devname, len);
235 1.2 gehenna
236 1.2 gehenna devsw_conv[i].d_name = name;
237 1.2 gehenna devsw_conv[i].d_bmajor = *bmajor;
238 1.2 gehenna devsw_conv[i].d_cmajor = *cmajor;
239 1.2 gehenna
240 1.23 pooka mutex_exit(&device_lock);
241 1.2 gehenna return (0);
242 1.11 ad fail:
243 1.23 pooka mutex_exit(&device_lock);
244 1.11 ad return (error);
245 1.2 gehenna }
246 1.2 gehenna
247 1.2 gehenna static int
248 1.24 drochner bdevsw_attach(const struct bdevsw *devsw, devmajor_t *devmajor)
249 1.2 gehenna {
250 1.11 ad const struct bdevsw **newptr;
251 1.24 drochner devmajor_t bmajor;
252 1.24 drochner int i;
253 1.2 gehenna
254 1.23 pooka KASSERT(mutex_owned(&device_lock));
255 1.11 ad
256 1.2 gehenna if (devsw == NULL)
257 1.2 gehenna return (0);
258 1.2 gehenna
259 1.2 gehenna if (*devmajor < 0) {
260 1.2 gehenna for (bmajor = sys_bdevsws ; bmajor < max_bdevsws ; bmajor++) {
261 1.2 gehenna if (bdevsw[bmajor] != NULL)
262 1.2 gehenna continue;
263 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
264 1.2 gehenna if (devsw_conv[i].d_bmajor == bmajor)
265 1.2 gehenna break;
266 1.2 gehenna }
267 1.2 gehenna if (i != max_devsw_convs)
268 1.2 gehenna continue;
269 1.2 gehenna break;
270 1.2 gehenna }
271 1.3 gehenna *devmajor = bmajor;
272 1.2 gehenna }
273 1.11 ad
274 1.2 gehenna if (*devmajor >= MAXDEVSW) {
275 1.11 ad printf("bdevsw_attach: block majors exhausted");
276 1.2 gehenna return (ENOMEM);
277 1.2 gehenna }
278 1.2 gehenna
279 1.2 gehenna if (*devmajor >= max_bdevsws) {
280 1.11 ad KASSERT(bdevsw == bdevsw0);
281 1.11 ad newptr = kmem_zalloc(MAXDEVSW * BDEVSW_SIZE, KM_NOSLEEP);
282 1.2 gehenna if (newptr == NULL)
283 1.2 gehenna return (ENOMEM);
284 1.11 ad memcpy(newptr, bdevsw, max_bdevsws * BDEVSW_SIZE);
285 1.2 gehenna bdevsw = newptr;
286 1.11 ad max_bdevsws = MAXDEVSW;
287 1.2 gehenna }
288 1.2 gehenna
289 1.2 gehenna if (bdevsw[*devmajor] != NULL)
290 1.2 gehenna return (EEXIST);
291 1.2 gehenna
292 1.34.2.2 pgoyette /* ensure visibility of the bdevsw */
293 1.34.2.2 pgoyette membar_producer();
294 1.34.2.2 pgoyette
295 1.2 gehenna bdevsw[*devmajor] = devsw;
296 1.34.2.1 pgoyette KASSERT(devsw->d_localcount != NULL);
297 1.34.2.1 pgoyette localcount_init(devsw->d_localcount);
298 1.2 gehenna
299 1.2 gehenna return (0);
300 1.2 gehenna }
301 1.2 gehenna
302 1.2 gehenna static int
303 1.24 drochner cdevsw_attach(const struct cdevsw *devsw, devmajor_t *devmajor)
304 1.2 gehenna {
305 1.11 ad const struct cdevsw **newptr;
306 1.24 drochner devmajor_t cmajor;
307 1.24 drochner int i;
308 1.2 gehenna
309 1.23 pooka KASSERT(mutex_owned(&device_lock));
310 1.11 ad
311 1.2 gehenna if (*devmajor < 0) {
312 1.2 gehenna for (cmajor = sys_cdevsws ; cmajor < max_cdevsws ; cmajor++) {
313 1.2 gehenna if (cdevsw[cmajor] != NULL)
314 1.2 gehenna continue;
315 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
316 1.2 gehenna if (devsw_conv[i].d_cmajor == cmajor)
317 1.2 gehenna break;
318 1.2 gehenna }
319 1.2 gehenna if (i != max_devsw_convs)
320 1.2 gehenna continue;
321 1.2 gehenna break;
322 1.2 gehenna }
323 1.3 gehenna *devmajor = cmajor;
324 1.2 gehenna }
325 1.11 ad
326 1.2 gehenna if (*devmajor >= MAXDEVSW) {
327 1.11 ad printf("cdevsw_attach: character majors exhausted");
328 1.2 gehenna return (ENOMEM);
329 1.2 gehenna }
330 1.2 gehenna
331 1.2 gehenna if (*devmajor >= max_cdevsws) {
332 1.11 ad KASSERT(cdevsw == cdevsw0);
333 1.11 ad newptr = kmem_zalloc(MAXDEVSW * CDEVSW_SIZE, KM_NOSLEEP);
334 1.2 gehenna if (newptr == NULL)
335 1.2 gehenna return (ENOMEM);
336 1.11 ad memcpy(newptr, cdevsw, max_cdevsws * CDEVSW_SIZE);
337 1.2 gehenna cdevsw = newptr;
338 1.11 ad max_cdevsws = MAXDEVSW;
339 1.2 gehenna }
340 1.2 gehenna
341 1.2 gehenna if (cdevsw[*devmajor] != NULL)
342 1.2 gehenna return (EEXIST);
343 1.2 gehenna
344 1.34.2.2 pgoyette /* ensure visibility of the bdevsw */
345 1.34.2.2 pgoyette membar_producer();
346 1.34.2.2 pgoyette
347 1.2 gehenna cdevsw[*devmajor] = devsw;
348 1.34.2.1 pgoyette KASSERT(devsw->d_localcount != NULL);
349 1.34.2.1 pgoyette localcount_init(devsw->d_localcount);
350 1.2 gehenna
351 1.2 gehenna return (0);
352 1.2 gehenna }
353 1.2 gehenna
354 1.34.2.1 pgoyette /*
355 1.34.2.2 pgoyette * First, look up both bdev and cdev indices, and remove the
356 1.34.2.2 pgoyette * {b,c]devsw[] entries so no new references can be taken. Then
357 1.34.2.2 pgoyette * drain any existing references.
358 1.34.2.1 pgoyette */
359 1.34.2.1 pgoyette
360 1.11 ad static void
361 1.11 ad devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
362 1.2 gehenna {
363 1.34.2.3 pgoyette int i, j;
364 1.2 gehenna
365 1.23 pooka KASSERT(mutex_owned(&device_lock));
366 1.11 ad
367 1.34.2.1 pgoyette i = max_bdevsws;
368 1.2 gehenna if (bdev != NULL) {
369 1.2 gehenna for (i = 0 ; i < max_bdevsws ; i++) {
370 1.2 gehenna if (bdevsw[i] != bdev)
371 1.2 gehenna continue;
372 1.34.2.1 pgoyette
373 1.34.2.1 pgoyette KASSERTMSG(bdev->d_localcount != NULL,
374 1.34.2.1 pgoyette "%s: no bdev localcount", __func__);
375 1.2 gehenna break;
376 1.2 gehenna }
377 1.2 gehenna }
378 1.34.2.1 pgoyette j = max_cdevsws;
379 1.2 gehenna if (cdev != NULL) {
380 1.34.2.1 pgoyette for (j = 0 ; j < max_cdevsws ; j++) {
381 1.34.2.1 pgoyette if (cdevsw[j] != cdev)
382 1.2 gehenna continue;
383 1.34.2.1 pgoyette
384 1.34.2.1 pgoyette KASSERTMSG(cdev->d_localcount != NULL,
385 1.34.2.1 pgoyette "%s: no cdev localcount", __func__);
386 1.2 gehenna break;
387 1.2 gehenna }
388 1.2 gehenna }
389 1.34.2.2 pgoyette if (i < max_bdevsws)
390 1.34.2.2 pgoyette bdevsw[i] = NULL;
391 1.34.2.2 pgoyette if (j < max_cdevsws )
392 1.34.2.2 pgoyette cdevsw[j] = NULL;
393 1.34.2.2 pgoyette
394 1.34.2.4 pgoyette /*
395 1.34.2.4 pgoyette * If we haven't already done so, create the serialization
396 1.34.2.4 pgoyette * stucture. Then wait for all current readers to finish.
397 1.34.2.4 pgoyette */
398 1.34.2.4 pgoyette if(__predict_false(device_psz == NULL))
399 1.34.2.4 pgoyette device_psz = pserialize_create();
400 1.34.2.3 pgoyette pserialize_perform(device_psz);
401 1.34.2.3 pgoyette
402 1.34.2.3 pgoyette /*
403 1.34.2.3 pgoyette * Here, no new readers can reach the bdev and cdev via the
404 1.34.2.3 pgoyette * {b,c}devsw[] arrays. Wait for existing references to
405 1.34.2.3 pgoyette * drain, and then destroy.
406 1.34.2.3 pgoyette */
407 1.34.2.3 pgoyette
408 1.34.2.2 pgoyette if (i < max_bdevsws && bdev->d_localcount != NULL) {
409 1.34.2.1 pgoyette localcount_drain(bdev->d_localcount, &device_cv, &device_lock);
410 1.34.2.1 pgoyette localcount_fini(bdev->d_localcount);
411 1.34.2.1 pgoyette }
412 1.34.2.2 pgoyette if (j < max_cdevsws && cdev->d_localcount != NULL ) {
413 1.34.2.2 pgoyette localcount_drain(cdev->d_localcount, &device_cv, &device_lock);
414 1.34.2.2 pgoyette localcount_fini(cdev->d_localcount);
415 1.34.2.1 pgoyette }
416 1.2 gehenna }
417 1.2 gehenna
418 1.19 ad int
419 1.11 ad devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
420 1.11 ad {
421 1.11 ad
422 1.23 pooka mutex_enter(&device_lock);
423 1.11 ad devsw_detach_locked(bdev, cdev);
424 1.23 pooka mutex_exit(&device_lock);
425 1.19 ad return 0;
426 1.11 ad }
427 1.11 ad
428 1.11 ad /*
429 1.11 ad * Look up a block device by number.
430 1.11 ad *
431 1.11 ad * => Caller must ensure that the device is attached.
432 1.11 ad */
433 1.2 gehenna const struct bdevsw *
434 1.2 gehenna bdevsw_lookup(dev_t dev)
435 1.2 gehenna {
436 1.24 drochner devmajor_t bmajor;
437 1.2 gehenna
438 1.2 gehenna if (dev == NODEV)
439 1.2 gehenna return (NULL);
440 1.2 gehenna bmajor = major(dev);
441 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws)
442 1.2 gehenna return (NULL);
443 1.2 gehenna
444 1.2 gehenna return (bdevsw[bmajor]);
445 1.2 gehenna }
446 1.2 gehenna
447 1.34.2.1 pgoyette const struct bdevsw *
448 1.34.2.1 pgoyette bdevsw_lookup_acquire(dev_t dev)
449 1.34.2.1 pgoyette {
450 1.34.2.1 pgoyette devmajor_t bmajor;
451 1.34.2.2 pgoyette const struct bdevsw *bdev = NULL;
452 1.34.2.2 pgoyette int s;
453 1.34.2.1 pgoyette
454 1.34.2.1 pgoyette if (dev == NODEV)
455 1.34.2.1 pgoyette return (NULL);
456 1.34.2.1 pgoyette bmajor = major(dev);
457 1.34.2.1 pgoyette if (bmajor < 0 || bmajor >= max_bdevsws)
458 1.34.2.1 pgoyette return (NULL);
459 1.34.2.1 pgoyette
460 1.34.2.2 pgoyette /* Start a read transaction to block localcount_drain() */
461 1.34.2.2 pgoyette s = pserialize_read_enter();
462 1.34.2.2 pgoyette
463 1.34.2.2 pgoyette /* Get the struct bdevsw pointer */
464 1.34.2.2 pgoyette bdev = bdevsw[bmajor];
465 1.34.2.2 pgoyette if (bdev == NULL)
466 1.34.2.2 pgoyette goto out;
467 1.34.2.2 pgoyette
468 1.34.2.2 pgoyette /* Wait for the content of the struct bdevsw to become visible */
469 1.34.2.2 pgoyette membar_datadep_consumer();
470 1.34.2.2 pgoyette
471 1.34.2.2 pgoyette /* If the devsw is not statically linked, acquire a reference */
472 1.34.2.1 pgoyette if (bdevsw[bmajor]->d_localcount != NULL)
473 1.34.2.1 pgoyette localcount_acquire(bdevsw[bmajor]->d_localcount);
474 1.34.2.1 pgoyette
475 1.34.2.2 pgoyette out: pserialize_read_exit(s);
476 1.34.2.4 pgoyette
477 1.34.2.4 pgoyette return bdev;
478 1.34.2.1 pgoyette }
479 1.34.2.1 pgoyette
480 1.34.2.1 pgoyette void
481 1.34.2.1 pgoyette bdevsw_release(const struct bdevsw *bd)
482 1.34.2.1 pgoyette {
483 1.34.2.1 pgoyette
484 1.34.2.2 pgoyette KASSERT(bd != NULL);
485 1.34.2.1 pgoyette if (bd->d_localcount != NULL)
486 1.34.2.1 pgoyette localcount_release(bd->d_localcount, &device_cv, &device_lock);
487 1.34.2.1 pgoyette }
488 1.34.2.1 pgoyette
489 1.11 ad /*
490 1.11 ad * Look up a character device by number.
491 1.11 ad *
492 1.11 ad * => Caller must ensure that the device is attached.
493 1.11 ad */
494 1.2 gehenna const struct cdevsw *
495 1.2 gehenna cdevsw_lookup(dev_t dev)
496 1.2 gehenna {
497 1.24 drochner devmajor_t cmajor;
498 1.2 gehenna
499 1.2 gehenna if (dev == NODEV)
500 1.2 gehenna return (NULL);
501 1.2 gehenna cmajor = major(dev);
502 1.2 gehenna if (cmajor < 0 || cmajor >= max_cdevsws)
503 1.2 gehenna return (NULL);
504 1.2 gehenna
505 1.2 gehenna return (cdevsw[cmajor]);
506 1.2 gehenna }
507 1.2 gehenna
508 1.34.2.1 pgoyette const struct cdevsw *
509 1.34.2.1 pgoyette cdevsw_lookup_acquire(dev_t dev)
510 1.34.2.1 pgoyette {
511 1.34.2.1 pgoyette devmajor_t cmajor;
512 1.34.2.2 pgoyette const struct cdevsw *cdev = NULL;
513 1.34.2.2 pgoyette int s;
514 1.34.2.1 pgoyette
515 1.34.2.1 pgoyette if (dev == NODEV)
516 1.34.2.1 pgoyette return (NULL);
517 1.34.2.1 pgoyette cmajor = major(dev);
518 1.34.2.1 pgoyette if (cmajor < 0 || cmajor >= max_cdevsws)
519 1.34.2.1 pgoyette return (NULL);
520 1.34.2.1 pgoyette
521 1.34.2.2 pgoyette /* Start a read transaction to block localcount_drain() */
522 1.34.2.2 pgoyette s = pserialize_read_enter();
523 1.34.2.2 pgoyette
524 1.34.2.2 pgoyette /* Get the struct bdevsw pointer */
525 1.34.2.2 pgoyette cdev = cdevsw[cmajor];
526 1.34.2.2 pgoyette if (cdev == NULL)
527 1.34.2.2 pgoyette goto out;
528 1.34.2.2 pgoyette
529 1.34.2.4 pgoyette /* Wait for the content of the struct cdevsw to become visible */
530 1.34.2.2 pgoyette membar_datadep_consumer();
531 1.34.2.2 pgoyette
532 1.34.2.2 pgoyette /* If the devsw is not statically linked, acquire a reference */
533 1.34.2.1 pgoyette if (cdevsw[cmajor]->d_localcount != NULL)
534 1.34.2.1 pgoyette localcount_acquire(cdevsw[cmajor]->d_localcount);
535 1.34.2.1 pgoyette
536 1.34.2.2 pgoyette out: pserialize_read_exit(s);
537 1.34.2.2 pgoyette mutex_exit(&device_lock);
538 1.34.2.2 pgoyette
539 1.34.2.2 pgoyette return cdev;
540 1.34.2.1 pgoyette }
541 1.34.2.1 pgoyette
542 1.34.2.1 pgoyette void
543 1.34.2.1 pgoyette cdevsw_release(const struct cdevsw *cd)
544 1.34.2.1 pgoyette {
545 1.34.2.1 pgoyette
546 1.34.2.2 pgoyette KASSERT(cd != NULL);
547 1.34.2.1 pgoyette if (cd->d_localcount != NULL)
548 1.34.2.1 pgoyette localcount_release(cd->d_localcount, &device_cv, &device_lock);
549 1.34.2.1 pgoyette }
550 1.34.2.1 pgoyette
551 1.11 ad /*
552 1.11 ad * Look up a block device by reference to its operations set.
553 1.11 ad *
554 1.11 ad * => Caller must ensure that the device is not detached, and therefore
555 1.11 ad * that the returned major is still valid when dereferenced.
556 1.11 ad */
557 1.24 drochner devmajor_t
558 1.2 gehenna bdevsw_lookup_major(const struct bdevsw *bdev)
559 1.2 gehenna {
560 1.24 drochner devmajor_t bmajor;
561 1.2 gehenna
562 1.2 gehenna for (bmajor = 0 ; bmajor < max_bdevsws ; bmajor++) {
563 1.2 gehenna if (bdevsw[bmajor] == bdev)
564 1.2 gehenna return (bmajor);
565 1.2 gehenna }
566 1.2 gehenna
567 1.24 drochner return (NODEVMAJOR);
568 1.2 gehenna }
569 1.2 gehenna
570 1.11 ad /*
571 1.11 ad * Look up a character device by reference to its operations set.
572 1.11 ad *
573 1.11 ad * => Caller must ensure that the device is not detached, and therefore
574 1.11 ad * that the returned major is still valid when dereferenced.
575 1.11 ad */
576 1.24 drochner devmajor_t
577 1.2 gehenna cdevsw_lookup_major(const struct cdevsw *cdev)
578 1.2 gehenna {
579 1.24 drochner devmajor_t cmajor;
580 1.2 gehenna
581 1.2 gehenna for (cmajor = 0 ; cmajor < max_cdevsws ; cmajor++) {
582 1.2 gehenna if (cdevsw[cmajor] == cdev)
583 1.2 gehenna return (cmajor);
584 1.2 gehenna }
585 1.2 gehenna
586 1.24 drochner return (NODEVMAJOR);
587 1.2 gehenna }
588 1.2 gehenna
589 1.2 gehenna /*
590 1.2 gehenna * Convert from block major number to name.
591 1.11 ad *
592 1.11 ad * => Caller must ensure that the device is not detached, and therefore
593 1.11 ad * that the name pointer is still valid when dereferenced.
594 1.2 gehenna */
595 1.2 gehenna const char *
596 1.24 drochner devsw_blk2name(devmajor_t bmajor)
597 1.2 gehenna {
598 1.11 ad const char *name;
599 1.24 drochner devmajor_t cmajor;
600 1.24 drochner int i;
601 1.2 gehenna
602 1.11 ad name = NULL;
603 1.11 ad cmajor = -1;
604 1.11 ad
605 1.23 pooka mutex_enter(&device_lock);
606 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
607 1.23 pooka mutex_exit(&device_lock);
608 1.2 gehenna return (NULL);
609 1.2 gehenna }
610 1.11 ad for (i = 0 ; i < max_devsw_convs; i++) {
611 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
612 1.11 ad cmajor = devsw_conv[i].d_cmajor;
613 1.11 ad break;
614 1.11 ad }
615 1.11 ad }
616 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
617 1.11 ad name = devsw_conv[i].d_name;
618 1.23 pooka mutex_exit(&device_lock);
619 1.2 gehenna
620 1.11 ad return (name);
621 1.2 gehenna }
622 1.2 gehenna
623 1.2 gehenna /*
624 1.26 haad * Convert char major number to device driver name.
625 1.26 haad */
626 1.27 yamt const char *
627 1.26 haad cdevsw_getname(devmajor_t major)
628 1.26 haad {
629 1.26 haad const char *name;
630 1.26 haad int i;
631 1.26 haad
632 1.26 haad name = NULL;
633 1.26 haad
634 1.26 haad if (major < 0)
635 1.26 haad return (NULL);
636 1.26 haad
637 1.26 haad mutex_enter(&device_lock);
638 1.26 haad for (i = 0 ; i < max_devsw_convs; i++) {
639 1.26 haad if (devsw_conv[i].d_cmajor == major) {
640 1.26 haad name = devsw_conv[i].d_name;
641 1.26 haad break;
642 1.26 haad }
643 1.26 haad }
644 1.26 haad mutex_exit(&device_lock);
645 1.26 haad return (name);
646 1.26 haad }
647 1.26 haad
648 1.26 haad /*
649 1.26 haad * Convert block major number to device driver name.
650 1.26 haad */
651 1.27 yamt const char *
652 1.26 haad bdevsw_getname(devmajor_t major)
653 1.26 haad {
654 1.26 haad const char *name;
655 1.26 haad int i;
656 1.26 haad
657 1.26 haad name = NULL;
658 1.26 haad
659 1.26 haad if (major < 0)
660 1.26 haad return (NULL);
661 1.26 haad
662 1.26 haad mutex_enter(&device_lock);
663 1.26 haad for (i = 0 ; i < max_devsw_convs; i++) {
664 1.26 haad if (devsw_conv[i].d_bmajor == major) {
665 1.26 haad name = devsw_conv[i].d_name;
666 1.26 haad break;
667 1.26 haad }
668 1.26 haad }
669 1.26 haad mutex_exit(&device_lock);
670 1.26 haad return (name);
671 1.26 haad }
672 1.26 haad
673 1.26 haad /*
674 1.2 gehenna * Convert from device name to block major number.
675 1.11 ad *
676 1.11 ad * => Caller must ensure that the device is not detached, and therefore
677 1.11 ad * that the major number is still valid when dereferenced.
678 1.2 gehenna */
679 1.24 drochner devmajor_t
680 1.2 gehenna devsw_name2blk(const char *name, char *devname, size_t devnamelen)
681 1.2 gehenna {
682 1.2 gehenna struct devsw_conv *conv;
683 1.24 drochner devmajor_t bmajor;
684 1.24 drochner int i;
685 1.2 gehenna
686 1.2 gehenna if (name == NULL)
687 1.24 drochner return (NODEVMAJOR);
688 1.2 gehenna
689 1.23 pooka mutex_enter(&device_lock);
690 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
691 1.5 mrg size_t len;
692 1.5 mrg
693 1.2 gehenna conv = &devsw_conv[i];
694 1.2 gehenna if (conv->d_name == NULL)
695 1.2 gehenna continue;
696 1.5 mrg len = strlen(conv->d_name);
697 1.5 mrg if (strncmp(conv->d_name, name, len) != 0)
698 1.5 mrg continue;
699 1.5 mrg if (*(name +len) && !isdigit(*(name + len)))
700 1.2 gehenna continue;
701 1.2 gehenna bmajor = conv->d_bmajor;
702 1.2 gehenna if (bmajor < 0 || bmajor >= max_bdevsws ||
703 1.2 gehenna bdevsw[bmajor] == NULL)
704 1.5 mrg break;
705 1.2 gehenna if (devname != NULL) {
706 1.2 gehenna #ifdef DEVSW_DEBUG
707 1.2 gehenna if (strlen(conv->d_name) >= devnamelen)
708 1.2 gehenna printf("devsw_name2blk: too short buffer");
709 1.2 gehenna #endif /* DEVSW_DEBUG */
710 1.4 tsutsui strncpy(devname, conv->d_name, devnamelen);
711 1.2 gehenna devname[devnamelen - 1] = '\0';
712 1.2 gehenna }
713 1.23 pooka mutex_exit(&device_lock);
714 1.2 gehenna return (bmajor);
715 1.2 gehenna }
716 1.2 gehenna
717 1.23 pooka mutex_exit(&device_lock);
718 1.24 drochner return (NODEVMAJOR);
719 1.2 gehenna }
720 1.2 gehenna
721 1.2 gehenna /*
722 1.16 plunky * Convert from device name to char major number.
723 1.16 plunky *
724 1.16 plunky * => Caller must ensure that the device is not detached, and therefore
725 1.16 plunky * that the major number is still valid when dereferenced.
726 1.16 plunky */
727 1.24 drochner devmajor_t
728 1.16 plunky devsw_name2chr(const char *name, char *devname, size_t devnamelen)
729 1.16 plunky {
730 1.16 plunky struct devsw_conv *conv;
731 1.24 drochner devmajor_t cmajor;
732 1.24 drochner int i;
733 1.16 plunky
734 1.16 plunky if (name == NULL)
735 1.24 drochner return (NODEVMAJOR);
736 1.16 plunky
737 1.23 pooka mutex_enter(&device_lock);
738 1.16 plunky for (i = 0 ; i < max_devsw_convs ; i++) {
739 1.16 plunky size_t len;
740 1.16 plunky
741 1.16 plunky conv = &devsw_conv[i];
742 1.16 plunky if (conv->d_name == NULL)
743 1.16 plunky continue;
744 1.16 plunky len = strlen(conv->d_name);
745 1.16 plunky if (strncmp(conv->d_name, name, len) != 0)
746 1.16 plunky continue;
747 1.16 plunky if (*(name +len) && !isdigit(*(name + len)))
748 1.16 plunky continue;
749 1.16 plunky cmajor = conv->d_cmajor;
750 1.16 plunky if (cmajor < 0 || cmajor >= max_cdevsws ||
751 1.16 plunky cdevsw[cmajor] == NULL)
752 1.16 plunky break;
753 1.16 plunky if (devname != NULL) {
754 1.16 plunky #ifdef DEVSW_DEBUG
755 1.16 plunky if (strlen(conv->d_name) >= devnamelen)
756 1.16 plunky printf("devsw_name2chr: too short buffer");
757 1.16 plunky #endif /* DEVSW_DEBUG */
758 1.16 plunky strncpy(devname, conv->d_name, devnamelen);
759 1.16 plunky devname[devnamelen - 1] = '\0';
760 1.16 plunky }
761 1.23 pooka mutex_exit(&device_lock);
762 1.16 plunky return (cmajor);
763 1.16 plunky }
764 1.16 plunky
765 1.23 pooka mutex_exit(&device_lock);
766 1.24 drochner return (NODEVMAJOR);
767 1.16 plunky }
768 1.16 plunky
769 1.16 plunky /*
770 1.2 gehenna * Convert from character dev_t to block dev_t.
771 1.11 ad *
772 1.11 ad * => Caller must ensure that the device is not detached, and therefore
773 1.11 ad * that the major number is still valid when dereferenced.
774 1.2 gehenna */
775 1.2 gehenna dev_t
776 1.2 gehenna devsw_chr2blk(dev_t cdev)
777 1.2 gehenna {
778 1.24 drochner devmajor_t bmajor, cmajor;
779 1.24 drochner int i;
780 1.11 ad dev_t rv;
781 1.2 gehenna
782 1.2 gehenna cmajor = major(cdev);
783 1.24 drochner bmajor = NODEVMAJOR;
784 1.11 ad rv = NODEV;
785 1.2 gehenna
786 1.23 pooka mutex_enter(&device_lock);
787 1.11 ad if (cmajor < 0 || cmajor >= max_cdevsws || cdevsw[cmajor] == NULL) {
788 1.23 pooka mutex_exit(&device_lock);
789 1.11 ad return (NODEV);
790 1.11 ad }
791 1.2 gehenna for (i = 0 ; i < max_devsw_convs ; i++) {
792 1.11 ad if (devsw_conv[i].d_cmajor == cmajor) {
793 1.11 ad bmajor = devsw_conv[i].d_bmajor;
794 1.11 ad break;
795 1.11 ad }
796 1.2 gehenna }
797 1.11 ad if (bmajor >= 0 && bmajor < max_bdevsws && bdevsw[bmajor] != NULL)
798 1.11 ad rv = makedev(bmajor, minor(cdev));
799 1.23 pooka mutex_exit(&device_lock);
800 1.2 gehenna
801 1.11 ad return (rv);
802 1.2 gehenna }
803 1.2 gehenna
804 1.2 gehenna /*
805 1.2 gehenna * Convert from block dev_t to character dev_t.
806 1.11 ad *
807 1.11 ad * => Caller must ensure that the device is not detached, and therefore
808 1.11 ad * that the major number is still valid when dereferenced.
809 1.2 gehenna */
810 1.2 gehenna dev_t
811 1.2 gehenna devsw_blk2chr(dev_t bdev)
812 1.2 gehenna {
813 1.24 drochner devmajor_t bmajor, cmajor;
814 1.24 drochner int i;
815 1.11 ad dev_t rv;
816 1.2 gehenna
817 1.11 ad bmajor = major(bdev);
818 1.24 drochner cmajor = NODEVMAJOR;
819 1.11 ad rv = NODEV;
820 1.11 ad
821 1.23 pooka mutex_enter(&device_lock);
822 1.11 ad if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
823 1.23 pooka mutex_exit(&device_lock);
824 1.2 gehenna return (NODEV);
825 1.11 ad }
826 1.11 ad for (i = 0 ; i < max_devsw_convs ; i++) {
827 1.11 ad if (devsw_conv[i].d_bmajor == bmajor) {
828 1.11 ad cmajor = devsw_conv[i].d_cmajor;
829 1.11 ad break;
830 1.11 ad }
831 1.11 ad }
832 1.11 ad if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
833 1.11 ad rv = makedev(cmajor, minor(bdev));
834 1.23 pooka mutex_exit(&device_lock);
835 1.2 gehenna
836 1.11 ad return (rv);
837 1.11 ad }
838 1.11 ad
839 1.11 ad /*
840 1.11 ad * Device access methods.
841 1.11 ad */
842 1.11 ad
843 1.11 ad #define DEV_LOCK(d) \
844 1.17 ad if ((mpflag = (d->d_flag & D_MPSAFE)) == 0) { \
845 1.17 ad KERNEL_LOCK(1, NULL); \
846 1.11 ad }
847 1.2 gehenna
848 1.11 ad #define DEV_UNLOCK(d) \
849 1.17 ad if (mpflag == 0) { \
850 1.17 ad KERNEL_UNLOCK_ONE(NULL); \
851 1.2 gehenna }
852 1.2 gehenna
853 1.11 ad int
854 1.11 ad bdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
855 1.11 ad {
856 1.11 ad const struct bdevsw *d;
857 1.17 ad int rv, mpflag;
858 1.11 ad
859 1.11 ad /*
860 1.11 ad * For open we need to lock, in order to synchronize
861 1.11 ad * with attach/detach.
862 1.11 ad */
863 1.23 pooka mutex_enter(&device_lock);
864 1.11 ad d = bdevsw_lookup(dev);
865 1.23 pooka mutex_exit(&device_lock);
866 1.11 ad if (d == NULL)
867 1.11 ad return ENXIO;
868 1.11 ad
869 1.11 ad DEV_LOCK(d);
870 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
871 1.11 ad DEV_UNLOCK(d);
872 1.11 ad
873 1.11 ad return rv;
874 1.11 ad }
875 1.11 ad
876 1.11 ad int
877 1.11 ad bdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
878 1.11 ad {
879 1.11 ad const struct bdevsw *d;
880 1.17 ad int rv, mpflag;
881 1.11 ad
882 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
883 1.11 ad return ENXIO;
884 1.11 ad
885 1.11 ad DEV_LOCK(d);
886 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
887 1.11 ad DEV_UNLOCK(d);
888 1.11 ad
889 1.11 ad return rv;
890 1.11 ad }
891 1.11 ad
892 1.34 riz SDT_PROVIDER_DECLARE(io);
893 1.34 riz SDT_PROBE_DEFINE1(io, kernel, , start, "struct buf *"/*bp*/);
894 1.34 riz
895 1.11 ad void
896 1.11 ad bdev_strategy(struct buf *bp)
897 1.11 ad {
898 1.11 ad const struct bdevsw *d;
899 1.17 ad int mpflag;
900 1.11 ad
901 1.34 riz SDT_PROBE1(io, kernel, , start, bp);
902 1.34 riz
903 1.28 jmcneill if ((d = bdevsw_lookup(bp->b_dev)) == NULL) {
904 1.28 jmcneill bp->b_error = ENXIO;
905 1.28 jmcneill bp->b_resid = bp->b_bcount;
906 1.31 pooka biodone_vfs(bp); /* biodone() iff vfs present */
907 1.28 jmcneill return;
908 1.28 jmcneill }
909 1.11 ad
910 1.11 ad DEV_LOCK(d);
911 1.11 ad (*d->d_strategy)(bp);
912 1.11 ad DEV_UNLOCK(d);
913 1.11 ad }
914 1.11 ad
915 1.11 ad int
916 1.11 ad bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
917 1.11 ad {
918 1.11 ad const struct bdevsw *d;
919 1.17 ad int rv, mpflag;
920 1.11 ad
921 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
922 1.11 ad return ENXIO;
923 1.11 ad
924 1.11 ad DEV_LOCK(d);
925 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
926 1.11 ad DEV_UNLOCK(d);
927 1.11 ad
928 1.11 ad return rv;
929 1.11 ad }
930 1.11 ad
931 1.11 ad int
932 1.11 ad bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
933 1.11 ad {
934 1.11 ad const struct bdevsw *d;
935 1.11 ad int rv;
936 1.11 ad
937 1.11 ad /*
938 1.11 ad * Dump can be called without the device open. Since it can
939 1.11 ad * currently only be called with the system paused (and in a
940 1.11 ad * potentially unstable state), we don't perform any locking.
941 1.11 ad */
942 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
943 1.11 ad return ENXIO;
944 1.11 ad
945 1.11 ad /* DEV_LOCK(d); */
946 1.11 ad rv = (*d->d_dump)(dev, addr, data, sz);
947 1.11 ad /* DEV_UNLOCK(d); */
948 1.11 ad
949 1.11 ad return rv;
950 1.11 ad }
951 1.11 ad
952 1.11 ad int
953 1.11 ad bdev_type(dev_t dev)
954 1.11 ad {
955 1.11 ad const struct bdevsw *d;
956 1.11 ad
957 1.11 ad if ((d = bdevsw_lookup(dev)) == NULL)
958 1.11 ad return D_OTHER;
959 1.11 ad return d->d_flag & D_TYPEMASK;
960 1.11 ad }
961 1.11 ad
962 1.11 ad int
963 1.29 mrg bdev_size(dev_t dev)
964 1.29 mrg {
965 1.29 mrg const struct bdevsw *d;
966 1.29 mrg int rv, mpflag = 0;
967 1.29 mrg
968 1.29 mrg if ((d = bdevsw_lookup(dev)) == NULL ||
969 1.29 mrg d->d_psize == NULL)
970 1.29 mrg return -1;
971 1.29 mrg
972 1.29 mrg /*
973 1.29 mrg * Don't to try lock the device if we're dumping.
974 1.30 mrg * XXX: is there a better way to test this?
975 1.29 mrg */
976 1.29 mrg if ((boothowto & RB_DUMP) == 0)
977 1.29 mrg DEV_LOCK(d);
978 1.29 mrg rv = (*d->d_psize)(dev);
979 1.29 mrg if ((boothowto & RB_DUMP) == 0)
980 1.29 mrg DEV_UNLOCK(d);
981 1.29 mrg
982 1.29 mrg return rv;
983 1.29 mrg }
984 1.29 mrg
985 1.29 mrg int
986 1.32 dholland bdev_discard(dev_t dev, off_t pos, off_t len)
987 1.32 dholland {
988 1.32 dholland const struct bdevsw *d;
989 1.32 dholland int rv, mpflag;
990 1.32 dholland
991 1.32 dholland if ((d = bdevsw_lookup(dev)) == NULL)
992 1.32 dholland return ENXIO;
993 1.32 dholland
994 1.32 dholland DEV_LOCK(d);
995 1.32 dholland rv = (*d->d_discard)(dev, pos, len);
996 1.32 dholland DEV_UNLOCK(d);
997 1.32 dholland
998 1.32 dholland return rv;
999 1.32 dholland }
1000 1.32 dholland
1001 1.32 dholland int
1002 1.11 ad cdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
1003 1.11 ad {
1004 1.11 ad const struct cdevsw *d;
1005 1.17 ad int rv, mpflag;
1006 1.11 ad
1007 1.11 ad /*
1008 1.11 ad * For open we need to lock, in order to synchronize
1009 1.11 ad * with attach/detach.
1010 1.11 ad */
1011 1.23 pooka mutex_enter(&device_lock);
1012 1.11 ad d = cdevsw_lookup(dev);
1013 1.23 pooka mutex_exit(&device_lock);
1014 1.11 ad if (d == NULL)
1015 1.11 ad return ENXIO;
1016 1.11 ad
1017 1.11 ad DEV_LOCK(d);
1018 1.11 ad rv = (*d->d_open)(dev, flag, devtype, l);
1019 1.11 ad DEV_UNLOCK(d);
1020 1.11 ad
1021 1.11 ad return rv;
1022 1.11 ad }
1023 1.11 ad
1024 1.11 ad int
1025 1.11 ad cdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
1026 1.11 ad {
1027 1.11 ad const struct cdevsw *d;
1028 1.17 ad int rv, mpflag;
1029 1.11 ad
1030 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1031 1.11 ad return ENXIO;
1032 1.11 ad
1033 1.11 ad DEV_LOCK(d);
1034 1.11 ad rv = (*d->d_close)(dev, flag, devtype, l);
1035 1.11 ad DEV_UNLOCK(d);
1036 1.11 ad
1037 1.11 ad return rv;
1038 1.11 ad }
1039 1.11 ad
1040 1.11 ad int
1041 1.11 ad cdev_read(dev_t dev, struct uio *uio, int flag)
1042 1.11 ad {
1043 1.11 ad const struct cdevsw *d;
1044 1.17 ad int rv, mpflag;
1045 1.11 ad
1046 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1047 1.11 ad return ENXIO;
1048 1.11 ad
1049 1.11 ad DEV_LOCK(d);
1050 1.11 ad rv = (*d->d_read)(dev, uio, flag);
1051 1.11 ad DEV_UNLOCK(d);
1052 1.11 ad
1053 1.11 ad return rv;
1054 1.11 ad }
1055 1.11 ad
1056 1.11 ad int
1057 1.11 ad cdev_write(dev_t dev, struct uio *uio, int flag)
1058 1.11 ad {
1059 1.11 ad const struct cdevsw *d;
1060 1.17 ad int rv, mpflag;
1061 1.11 ad
1062 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1063 1.11 ad return ENXIO;
1064 1.11 ad
1065 1.11 ad DEV_LOCK(d);
1066 1.11 ad rv = (*d->d_write)(dev, uio, flag);
1067 1.11 ad DEV_UNLOCK(d);
1068 1.11 ad
1069 1.11 ad return rv;
1070 1.11 ad }
1071 1.11 ad
1072 1.11 ad int
1073 1.11 ad cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
1074 1.11 ad {
1075 1.11 ad const struct cdevsw *d;
1076 1.17 ad int rv, mpflag;
1077 1.11 ad
1078 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1079 1.11 ad return ENXIO;
1080 1.11 ad
1081 1.11 ad DEV_LOCK(d);
1082 1.11 ad rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
1083 1.11 ad DEV_UNLOCK(d);
1084 1.11 ad
1085 1.11 ad return rv;
1086 1.11 ad }
1087 1.11 ad
1088 1.11 ad void
1089 1.11 ad cdev_stop(struct tty *tp, int flag)
1090 1.11 ad {
1091 1.11 ad const struct cdevsw *d;
1092 1.17 ad int mpflag;
1093 1.11 ad
1094 1.11 ad if ((d = cdevsw_lookup(tp->t_dev)) == NULL)
1095 1.11 ad return;
1096 1.11 ad
1097 1.11 ad DEV_LOCK(d);
1098 1.11 ad (*d->d_stop)(tp, flag);
1099 1.11 ad DEV_UNLOCK(d);
1100 1.11 ad }
1101 1.11 ad
1102 1.11 ad struct tty *
1103 1.11 ad cdev_tty(dev_t dev)
1104 1.11 ad {
1105 1.11 ad const struct cdevsw *d;
1106 1.11 ad
1107 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1108 1.11 ad return NULL;
1109 1.11 ad
1110 1.12 ad /* XXX Check if necessary. */
1111 1.12 ad if (d->d_tty == NULL)
1112 1.12 ad return NULL;
1113 1.12 ad
1114 1.21 ad return (*d->d_tty)(dev);
1115 1.11 ad }
1116 1.11 ad
1117 1.11 ad int
1118 1.11 ad cdev_poll(dev_t dev, int flag, lwp_t *l)
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.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1124 1.11 ad return POLLERR;
1125 1.11 ad
1126 1.11 ad DEV_LOCK(d);
1127 1.11 ad rv = (*d->d_poll)(dev, flag, l);
1128 1.11 ad DEV_UNLOCK(d);
1129 1.11 ad
1130 1.11 ad return rv;
1131 1.11 ad }
1132 1.11 ad
1133 1.11 ad paddr_t
1134 1.11 ad cdev_mmap(dev_t dev, off_t off, int flag)
1135 1.11 ad {
1136 1.11 ad const struct cdevsw *d;
1137 1.11 ad paddr_t rv;
1138 1.17 ad int mpflag;
1139 1.11 ad
1140 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1141 1.11 ad return (paddr_t)-1LL;
1142 1.11 ad
1143 1.11 ad DEV_LOCK(d);
1144 1.11 ad rv = (*d->d_mmap)(dev, off, flag);
1145 1.11 ad DEV_UNLOCK(d);
1146 1.11 ad
1147 1.11 ad return rv;
1148 1.11 ad }
1149 1.11 ad
1150 1.11 ad int
1151 1.11 ad cdev_kqfilter(dev_t dev, struct knote *kn)
1152 1.11 ad {
1153 1.11 ad const struct cdevsw *d;
1154 1.17 ad int rv, mpflag;
1155 1.11 ad
1156 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1157 1.11 ad return ENXIO;
1158 1.11 ad
1159 1.11 ad DEV_LOCK(d);
1160 1.11 ad rv = (*d->d_kqfilter)(dev, kn);
1161 1.11 ad DEV_UNLOCK(d);
1162 1.11 ad
1163 1.11 ad return rv;
1164 1.11 ad }
1165 1.11 ad
1166 1.11 ad int
1167 1.32 dholland cdev_discard(dev_t dev, off_t pos, off_t len)
1168 1.32 dholland {
1169 1.32 dholland const struct cdevsw *d;
1170 1.32 dholland int rv, mpflag;
1171 1.32 dholland
1172 1.32 dholland if ((d = cdevsw_lookup(dev)) == NULL)
1173 1.32 dholland return ENXIO;
1174 1.32 dholland
1175 1.32 dholland DEV_LOCK(d);
1176 1.32 dholland rv = (*d->d_discard)(dev, pos, len);
1177 1.32 dholland DEV_UNLOCK(d);
1178 1.32 dholland
1179 1.32 dholland return rv;
1180 1.32 dholland }
1181 1.32 dholland
1182 1.32 dholland int
1183 1.11 ad cdev_type(dev_t dev)
1184 1.11 ad {
1185 1.11 ad const struct cdevsw *d;
1186 1.11 ad
1187 1.11 ad if ((d = cdevsw_lookup(dev)) == NULL)
1188 1.11 ad return D_OTHER;
1189 1.11 ad return d->d_flag & D_TYPEMASK;
1190 1.2 gehenna }
1191