Home | History | Annotate | Line # | Download | only in kern
subr_devsw.c revision 1.45
      1  1.45  riastrad /*	$NetBSD: subr_devsw.c,v 1.45 2022/03/28 12:41:17 riastradh 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.45  riastrad 
     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.45  riastrad __KERNEL_RCSID(0, "$NetBSD: subr_devsw.c,v 1.45 2022/03/28 12:41:17 riastradh 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.40  riastrad #include <sys/atomic.h>
     89  1.40  riastrad #include <sys/localcount.h>
     90  1.40  riastrad #include <sys/pserialize.h>
     91  1.40  riastrad #include <sys/xcall.h>
     92  1.41  riastrad #include <sys/device.h>
     93   1.2   gehenna 
     94   1.2   gehenna #ifdef DEVSW_DEBUG
     95   1.2   gehenna #define	DPRINTF(x)	printf x
     96   1.2   gehenna #else /* DEVSW_DEBUG */
     97   1.2   gehenna #define	DPRINTF(x)
     98   1.2   gehenna #endif /* DEVSW_DEBUG */
     99   1.2   gehenna 
    100  1.11        ad #define	MAXDEVSW	512	/* the maximum of major device number */
    101   1.2   gehenna #define	BDEVSW_SIZE	(sizeof(struct bdevsw *))
    102   1.2   gehenna #define	CDEVSW_SIZE	(sizeof(struct cdevsw *))
    103   1.2   gehenna #define	DEVSWCONV_SIZE	(sizeof(struct devsw_conv))
    104   1.2   gehenna 
    105  1.40  riastrad struct devswref {
    106  1.40  riastrad 	struct localcount	*dr_lc;
    107  1.40  riastrad };
    108  1.40  riastrad 
    109  1.40  riastrad /* XXX bdevsw, cdevsw, max_bdevsws, and max_cdevsws should be volatile */
    110   1.2   gehenna extern const struct bdevsw **bdevsw, *bdevsw0[];
    111   1.2   gehenna extern const struct cdevsw **cdevsw, *cdevsw0[];
    112   1.2   gehenna extern struct devsw_conv *devsw_conv, devsw_conv0[];
    113   1.2   gehenna extern const int sys_bdevsws, sys_cdevsws;
    114   1.2   gehenna extern int max_bdevsws, max_cdevsws, max_devsw_convs;
    115   1.2   gehenna 
    116  1.40  riastrad static struct devswref *cdevswref;
    117  1.40  riastrad static struct devswref *bdevswref;
    118  1.40  riastrad static kcondvar_t devsw_cv;
    119  1.40  riastrad 
    120  1.24  drochner static int bdevsw_attach(const struct bdevsw *, devmajor_t *);
    121  1.24  drochner static int cdevsw_attach(const struct cdevsw *, devmajor_t *);
    122  1.11        ad static void devsw_detach_locked(const struct bdevsw *, const struct cdevsw *);
    123  1.11        ad 
    124  1.23     pooka kmutex_t device_lock;
    125  1.23     pooka 
    126  1.31     pooka void (*biodone_vfs)(buf_t *) = (void *)nullop;
    127  1.31     pooka 
    128  1.11        ad void
    129  1.11        ad devsw_init(void)
    130  1.11        ad {
    131  1.11        ad 
    132  1.11        ad 	KASSERT(sys_bdevsws < MAXDEVSW - 1);
    133  1.11        ad 	KASSERT(sys_cdevsws < MAXDEVSW - 1);
    134  1.23     pooka 	mutex_init(&device_lock, MUTEX_DEFAULT, IPL_NONE);
    135  1.40  riastrad 
    136  1.40  riastrad 	cv_init(&devsw_cv, "devsw");
    137  1.11        ad }
    138   1.2   gehenna 
    139   1.2   gehenna int
    140  1.24  drochner devsw_attach(const char *devname,
    141  1.24  drochner 	     const struct bdevsw *bdev, devmajor_t *bmajor,
    142  1.24  drochner 	     const struct cdevsw *cdev, devmajor_t *cmajor)
    143   1.2   gehenna {
    144   1.2   gehenna 	struct devsw_conv *conv;
    145   1.2   gehenna 	char *name;
    146   1.2   gehenna 	int error, i;
    147   1.2   gehenna 
    148   1.2   gehenna 	if (devname == NULL || cdev == NULL)
    149  1.45  riastrad 		return EINVAL;
    150   1.2   gehenna 
    151  1.23     pooka 	mutex_enter(&device_lock);
    152  1.11        ad 
    153  1.45  riastrad 	for (i = 0; i < max_devsw_convs; i++) {
    154   1.2   gehenna 		conv = &devsw_conv[i];
    155   1.2   gehenna 		if (conv->d_name == NULL || strcmp(devname, conv->d_name) != 0)
    156   1.2   gehenna 			continue;
    157   1.2   gehenna 
    158   1.2   gehenna 		if (*bmajor < 0)
    159   1.2   gehenna 			*bmajor = conv->d_bmajor;
    160   1.2   gehenna 		if (*cmajor < 0)
    161   1.2   gehenna 			*cmajor = conv->d_cmajor;
    162   1.2   gehenna 
    163  1.11        ad 		if (*bmajor != conv->d_bmajor || *cmajor != conv->d_cmajor) {
    164  1.11        ad 			error = EINVAL;
    165  1.45  riastrad 			goto out;
    166  1.11        ad 		}
    167  1.11        ad 		if ((*bmajor >= 0 && bdev == NULL) || *cmajor < 0) {
    168  1.11        ad 			error = EINVAL;
    169  1.45  riastrad 			goto out;
    170  1.11        ad 		}
    171   1.2   gehenna 
    172   1.2   gehenna 		if ((*bmajor >= 0 && bdevsw[*bmajor] != NULL) ||
    173  1.11        ad 		    cdevsw[*cmajor] != NULL) {
    174  1.11        ad 			error = EEXIST;
    175  1.45  riastrad 			goto out;
    176  1.11        ad 		}
    177  1.40  riastrad 		break;
    178   1.2   gehenna 	}
    179   1.2   gehenna 
    180  1.40  riastrad 	/*
    181  1.40  riastrad 	 * XXX This should allocate what it needs up front so we never
    182  1.40  riastrad 	 * need to flail around trying to unwind.
    183  1.40  riastrad 	 */
    184  1.14     pooka 	error = bdevsw_attach(bdev, bmajor);
    185  1.45  riastrad 	if (error != 0)
    186  1.45  riastrad 		goto out;
    187  1.14     pooka 	error = cdevsw_attach(cdev, cmajor);
    188   1.2   gehenna 	if (error != 0) {
    189  1.11        ad 		devsw_detach_locked(bdev, NULL);
    190  1.45  riastrad 		goto out;
    191   1.2   gehenna 	}
    192   1.2   gehenna 
    193  1.40  riastrad 	/*
    194  1.40  riastrad 	 * If we already found a conv, we're done.  Otherwise, find an
    195  1.40  riastrad 	 * empty slot or extend the table.
    196  1.40  riastrad 	 */
    197  1.40  riastrad 	if (i == max_devsw_convs)
    198  1.45  riastrad 		goto out;
    199  1.40  riastrad 
    200  1.45  riastrad 	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.45  riastrad 			goto out;
    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.38  christos 	name = kmem_strdupsize(devname, NULL, KM_NOSLEEP);
    228   1.2   gehenna 	if (name == NULL) {
    229  1.11        ad 		devsw_detach_locked(bdev, cdev);
    230  1.25     enami 		error = ENOMEM;
    231  1.45  riastrad 		goto out;
    232   1.2   gehenna 	}
    233   1.2   gehenna 
    234   1.2   gehenna 	devsw_conv[i].d_name = name;
    235   1.2   gehenna 	devsw_conv[i].d_bmajor = *bmajor;
    236   1.2   gehenna 	devsw_conv[i].d_cmajor = *cmajor;
    237  1.45  riastrad 	error = 0;
    238  1.45  riastrad out:
    239  1.23     pooka 	mutex_exit(&device_lock);
    240  1.45  riastrad 	return error;
    241   1.2   gehenna }
    242   1.2   gehenna 
    243   1.2   gehenna static int
    244  1.24  drochner bdevsw_attach(const struct bdevsw *devsw, devmajor_t *devmajor)
    245   1.2   gehenna {
    246  1.40  riastrad 	const struct bdevsw **newbdevsw = NULL;
    247  1.40  riastrad 	struct devswref *newbdevswref = NULL;
    248  1.40  riastrad 	struct localcount *lc;
    249  1.24  drochner 	devmajor_t bmajor;
    250  1.24  drochner 	int i;
    251   1.2   gehenna 
    252  1.23     pooka 	KASSERT(mutex_owned(&device_lock));
    253  1.11        ad 
    254   1.2   gehenna 	if (devsw == NULL)
    255  1.45  riastrad 		return 0;
    256   1.2   gehenna 
    257   1.2   gehenna 	if (*devmajor < 0) {
    258  1.45  riastrad 		for (bmajor = sys_bdevsws; bmajor < max_bdevsws; bmajor++) {
    259   1.2   gehenna 			if (bdevsw[bmajor] != NULL)
    260   1.2   gehenna 				continue;
    261  1.45  riastrad 			for (i = 0; i < max_devsw_convs; i++) {
    262   1.2   gehenna 				if (devsw_conv[i].d_bmajor == bmajor)
    263   1.2   gehenna 					break;
    264   1.2   gehenna 			}
    265   1.2   gehenna 			if (i != max_devsw_convs)
    266   1.2   gehenna 				continue;
    267   1.2   gehenna 			break;
    268   1.2   gehenna 		}
    269   1.3   gehenna 		*devmajor = bmajor;
    270   1.2   gehenna 	}
    271  1.11        ad 
    272   1.2   gehenna 	if (*devmajor >= MAXDEVSW) {
    273  1.45  riastrad 		printf("%s: block majors exhausted\n", __func__);
    274  1.45  riastrad 		return ENOMEM;
    275   1.2   gehenna 	}
    276   1.2   gehenna 
    277  1.40  riastrad 	if (bdevswref == NULL) {
    278  1.40  riastrad 		newbdevswref = kmem_zalloc(MAXDEVSW * sizeof(newbdevswref[0]),
    279  1.40  riastrad 		    KM_NOSLEEP);
    280  1.40  riastrad 		if (newbdevswref == NULL)
    281  1.40  riastrad 			return ENOMEM;
    282  1.40  riastrad 		atomic_store_release(&bdevswref, newbdevswref);
    283  1.40  riastrad 	}
    284  1.40  riastrad 
    285   1.2   gehenna 	if (*devmajor >= max_bdevsws) {
    286  1.11        ad 		KASSERT(bdevsw == bdevsw0);
    287  1.40  riastrad 		newbdevsw = kmem_zalloc(MAXDEVSW * sizeof(newbdevsw[0]),
    288  1.40  riastrad 		    KM_NOSLEEP);
    289  1.40  riastrad 		if (newbdevsw == NULL)
    290  1.40  riastrad 			return ENOMEM;
    291  1.40  riastrad 		memcpy(newbdevsw, bdevsw, max_bdevsws * sizeof(bdevsw[0]));
    292  1.40  riastrad 		atomic_store_release(&bdevsw, newbdevsw);
    293  1.40  riastrad 		atomic_store_release(&max_bdevsws, MAXDEVSW);
    294   1.2   gehenna 	}
    295   1.2   gehenna 
    296   1.2   gehenna 	if (bdevsw[*devmajor] != NULL)
    297  1.45  riastrad 		return EEXIST;
    298   1.2   gehenna 
    299  1.40  riastrad 	KASSERT(bdevswref[*devmajor].dr_lc == NULL);
    300  1.40  riastrad 	lc = kmem_zalloc(sizeof(*lc), KM_SLEEP);
    301  1.40  riastrad 	localcount_init(lc);
    302  1.40  riastrad 	bdevswref[*devmajor].dr_lc = lc;
    303  1.40  riastrad 
    304  1.40  riastrad 	atomic_store_release(&bdevsw[*devmajor], devsw);
    305   1.2   gehenna 
    306  1.45  riastrad 	return 0;
    307   1.2   gehenna }
    308   1.2   gehenna 
    309   1.2   gehenna static int
    310  1.24  drochner cdevsw_attach(const struct cdevsw *devsw, devmajor_t *devmajor)
    311   1.2   gehenna {
    312  1.40  riastrad 	const struct cdevsw **newcdevsw = NULL;
    313  1.40  riastrad 	struct devswref *newcdevswref = NULL;
    314  1.40  riastrad 	struct localcount *lc;
    315  1.24  drochner 	devmajor_t cmajor;
    316  1.24  drochner 	int i;
    317   1.2   gehenna 
    318  1.23     pooka 	KASSERT(mutex_owned(&device_lock));
    319  1.11        ad 
    320   1.2   gehenna 	if (*devmajor < 0) {
    321  1.45  riastrad 		for (cmajor = sys_cdevsws; cmajor < max_cdevsws; cmajor++) {
    322   1.2   gehenna 			if (cdevsw[cmajor] != NULL)
    323   1.2   gehenna 				continue;
    324  1.45  riastrad 			for (i = 0; i < max_devsw_convs; i++) {
    325   1.2   gehenna 				if (devsw_conv[i].d_cmajor == cmajor)
    326   1.2   gehenna 					break;
    327   1.2   gehenna 			}
    328   1.2   gehenna 			if (i != max_devsw_convs)
    329   1.2   gehenna 				continue;
    330   1.2   gehenna 			break;
    331   1.2   gehenna 		}
    332   1.3   gehenna 		*devmajor = cmajor;
    333   1.2   gehenna 	}
    334  1.11        ad 
    335   1.2   gehenna 	if (*devmajor >= MAXDEVSW) {
    336  1.45  riastrad 		printf("%s: character majors exhausted\n", __func__);
    337  1.45  riastrad 		return ENOMEM;
    338   1.2   gehenna 	}
    339   1.2   gehenna 
    340  1.40  riastrad 	if (cdevswref == NULL) {
    341  1.40  riastrad 		newcdevswref = kmem_zalloc(MAXDEVSW * sizeof(newcdevswref[0]),
    342  1.40  riastrad 		    KM_NOSLEEP);
    343  1.40  riastrad 		if (newcdevswref == NULL)
    344  1.40  riastrad 			return ENOMEM;
    345  1.40  riastrad 		atomic_store_release(&cdevswref, newcdevswref);
    346  1.40  riastrad 	}
    347  1.40  riastrad 
    348   1.2   gehenna 	if (*devmajor >= max_cdevsws) {
    349  1.11        ad 		KASSERT(cdevsw == cdevsw0);
    350  1.40  riastrad 		newcdevsw = kmem_zalloc(MAXDEVSW * sizeof(newcdevsw[0]),
    351  1.40  riastrad 		    KM_NOSLEEP);
    352  1.40  riastrad 		if (newcdevsw == NULL)
    353  1.40  riastrad 			return ENOMEM;
    354  1.40  riastrad 		memcpy(newcdevsw, cdevsw, max_cdevsws * sizeof(cdevsw[0]));
    355  1.40  riastrad 		atomic_store_release(&cdevsw, newcdevsw);
    356  1.40  riastrad 		atomic_store_release(&max_cdevsws, MAXDEVSW);
    357   1.2   gehenna 	}
    358   1.2   gehenna 
    359   1.2   gehenna 	if (cdevsw[*devmajor] != NULL)
    360  1.45  riastrad 		return EEXIST;
    361   1.2   gehenna 
    362  1.40  riastrad 	KASSERT(cdevswref[*devmajor].dr_lc == NULL);
    363  1.40  riastrad 	lc = kmem_zalloc(sizeof(*lc), KM_SLEEP);
    364  1.40  riastrad 	localcount_init(lc);
    365  1.40  riastrad 	cdevswref[*devmajor].dr_lc = lc;
    366  1.40  riastrad 
    367  1.40  riastrad 	atomic_store_release(&cdevsw[*devmajor], devsw);
    368   1.2   gehenna 
    369  1.45  riastrad 	return 0;
    370   1.2   gehenna }
    371   1.2   gehenna 
    372  1.11        ad static void
    373  1.11        ad devsw_detach_locked(const struct bdevsw *bdev, const struct cdevsw *cdev)
    374   1.2   gehenna {
    375  1.40  riastrad 	int bi, ci = -1/*XXXGCC*/;
    376   1.2   gehenna 
    377  1.23     pooka 	KASSERT(mutex_owned(&device_lock));
    378  1.11        ad 
    379  1.40  riastrad 	/* Prevent new references.  */
    380   1.2   gehenna 	if (bdev != NULL) {
    381  1.40  riastrad 		for (bi = 0; bi < max_bdevsws; bi++) {
    382  1.40  riastrad 			if (bdevsw[bi] != bdev)
    383   1.2   gehenna 				continue;
    384  1.40  riastrad 			atomic_store_relaxed(&bdevsw[bi], NULL);
    385   1.2   gehenna 			break;
    386   1.2   gehenna 		}
    387  1.40  riastrad 		KASSERT(bi < max_bdevsws);
    388   1.2   gehenna 	}
    389   1.2   gehenna 	if (cdev != NULL) {
    390  1.40  riastrad 		for (ci = 0; ci < max_cdevsws; ci++) {
    391  1.40  riastrad 			if (cdevsw[ci] != cdev)
    392   1.2   gehenna 				continue;
    393  1.40  riastrad 			atomic_store_relaxed(&cdevsw[ci], NULL);
    394   1.2   gehenna 			break;
    395   1.2   gehenna 		}
    396  1.40  riastrad 		KASSERT(ci < max_cdevsws);
    397  1.40  riastrad 	}
    398  1.40  riastrad 
    399  1.40  riastrad 	if (bdev == NULL && cdev == NULL) /* XXX possible? */
    400  1.40  riastrad 		return;
    401  1.40  riastrad 
    402  1.40  riastrad 	/*
    403  1.40  riastrad 	 * Wait for all bdevsw_lookup_acquire, cdevsw_lookup_acquire
    404  1.40  riastrad 	 * calls to notice that the devsw is gone.
    405  1.40  riastrad 	 *
    406  1.40  riastrad 	 * XXX Despite the use of the pserialize_read_enter/exit API
    407  1.40  riastrad 	 * elsewhere in this file, we use xc_barrier here instead of
    408  1.40  riastrad 	 * pserialize_perform -- because devsw_init is too early for
    409  1.40  riastrad 	 * pserialize_create.  Either pserialize_create should be made
    410  1.40  riastrad 	 * to work earlier, or it should be nixed altogether.  Until
    411  1.40  riastrad 	 * that is fixed, xc_barrier will serve the same purpose.
    412  1.40  riastrad 	 */
    413  1.40  riastrad 	xc_barrier(0);
    414  1.40  riastrad 
    415  1.40  riastrad 	/*
    416  1.40  riastrad 	 * Wait for all references to drain.  It is the caller's
    417  1.40  riastrad 	 * responsibility to ensure that at this point, there are no
    418  1.40  riastrad 	 * extant open instances and all new d_open calls will fail.
    419  1.40  riastrad 	 *
    420  1.40  riastrad 	 * Note that localcount_drain may release and reacquire
    421  1.40  riastrad 	 * device_lock.
    422  1.40  riastrad 	 */
    423  1.40  riastrad 	if (bdev != NULL) {
    424  1.40  riastrad 		localcount_drain(bdevswref[bi].dr_lc,
    425  1.40  riastrad 		    &devsw_cv, &device_lock);
    426  1.40  riastrad 		localcount_fini(bdevswref[bi].dr_lc);
    427  1.40  riastrad 		kmem_free(bdevswref[bi].dr_lc, sizeof(*bdevswref[bi].dr_lc));
    428  1.40  riastrad 		bdevswref[bi].dr_lc = NULL;
    429  1.40  riastrad 	}
    430  1.40  riastrad 	if (cdev != NULL) {
    431  1.40  riastrad 		localcount_drain(cdevswref[ci].dr_lc,
    432  1.40  riastrad 		    &devsw_cv, &device_lock);
    433  1.40  riastrad 		localcount_fini(cdevswref[ci].dr_lc);
    434  1.40  riastrad 		kmem_free(cdevswref[ci].dr_lc, sizeof(*cdevswref[ci].dr_lc));
    435  1.40  riastrad 		cdevswref[ci].dr_lc = NULL;
    436   1.2   gehenna 	}
    437   1.2   gehenna }
    438   1.2   gehenna 
    439  1.39  riastrad void
    440  1.11        ad devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
    441  1.11        ad {
    442  1.11        ad 
    443  1.23     pooka 	mutex_enter(&device_lock);
    444  1.11        ad 	devsw_detach_locked(bdev, cdev);
    445  1.23     pooka 	mutex_exit(&device_lock);
    446  1.11        ad }
    447  1.11        ad 
    448  1.11        ad /*
    449  1.11        ad  * Look up a block device by number.
    450  1.11        ad  *
    451  1.11        ad  * => Caller must ensure that the device is attached.
    452  1.11        ad  */
    453   1.2   gehenna const struct bdevsw *
    454   1.2   gehenna bdevsw_lookup(dev_t dev)
    455   1.2   gehenna {
    456  1.24  drochner 	devmajor_t bmajor;
    457   1.2   gehenna 
    458   1.2   gehenna 	if (dev == NODEV)
    459  1.45  riastrad 		return NULL;
    460   1.2   gehenna 	bmajor = major(dev);
    461  1.40  riastrad 	if (bmajor < 0 || bmajor >= atomic_load_relaxed(&max_bdevsws))
    462  1.45  riastrad 		return NULL;
    463   1.2   gehenna 
    464  1.40  riastrad 	return atomic_load_consume(&bdevsw)[bmajor];
    465  1.40  riastrad }
    466  1.40  riastrad 
    467  1.40  riastrad static const struct bdevsw *
    468  1.40  riastrad bdevsw_lookup_acquire(dev_t dev, struct localcount **lcp)
    469  1.40  riastrad {
    470  1.40  riastrad 	devmajor_t bmajor;
    471  1.40  riastrad 	const struct bdevsw *bdev = NULL, *const *curbdevsw;
    472  1.40  riastrad 	struct devswref *curbdevswref;
    473  1.40  riastrad 	int s;
    474  1.40  riastrad 
    475  1.40  riastrad 	if (dev == NODEV)
    476  1.40  riastrad 		return NULL;
    477  1.40  riastrad 	bmajor = major(dev);
    478  1.40  riastrad 	if (bmajor < 0)
    479  1.40  riastrad 		return NULL;
    480  1.40  riastrad 
    481  1.40  riastrad 	s = pserialize_read_enter();
    482  1.40  riastrad 
    483  1.40  riastrad 	/*
    484  1.40  riastrad 	 * max_bdevsws never goes down, so it is safe to rely on this
    485  1.40  riastrad 	 * condition without any locking for the array access below.
    486  1.40  riastrad 	 * Test sys_bdevsws first so we can avoid the memory barrier in
    487  1.40  riastrad 	 * that case.
    488  1.40  riastrad 	 */
    489  1.40  riastrad 	if (bmajor >= sys_bdevsws &&
    490  1.40  riastrad 	    bmajor >= atomic_load_acquire(&max_bdevsws))
    491  1.40  riastrad 		goto out;
    492  1.40  riastrad 	curbdevsw = atomic_load_consume(&bdevsw);
    493  1.40  riastrad 	if ((bdev = atomic_load_consume(&curbdevsw[bmajor])) == NULL)
    494  1.40  riastrad 		goto out;
    495  1.40  riastrad 
    496  1.40  riastrad 	curbdevswref = atomic_load_consume(&bdevswref);
    497  1.40  riastrad 	if (curbdevswref == NULL) {
    498  1.40  riastrad 		*lcp = NULL;
    499  1.40  riastrad 	} else if ((*lcp = curbdevswref[bmajor].dr_lc) != NULL) {
    500  1.40  riastrad 		localcount_acquire(*lcp);
    501  1.40  riastrad 	}
    502  1.40  riastrad out:
    503  1.40  riastrad 	pserialize_read_exit(s);
    504  1.40  riastrad 	return bdev;
    505  1.40  riastrad }
    506  1.40  riastrad 
    507  1.40  riastrad static void
    508  1.40  riastrad bdevsw_release(const struct bdevsw *bdev, struct localcount *lc)
    509  1.40  riastrad {
    510  1.40  riastrad 
    511  1.40  riastrad 	if (lc == NULL)
    512  1.40  riastrad 		return;
    513  1.40  riastrad 	localcount_release(lc, &devsw_cv, &device_lock);
    514   1.2   gehenna }
    515   1.2   gehenna 
    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.45  riastrad 		return NULL;
    528   1.2   gehenna 	cmajor = major(dev);
    529  1.40  riastrad 	if (cmajor < 0 || cmajor >= atomic_load_relaxed(&max_cdevsws))
    530  1.45  riastrad 		return NULL;
    531   1.2   gehenna 
    532  1.40  riastrad 	return atomic_load_consume(&cdevsw)[cmajor];
    533  1.40  riastrad }
    534  1.40  riastrad 
    535  1.40  riastrad static const struct cdevsw *
    536  1.40  riastrad cdevsw_lookup_acquire(dev_t dev, struct localcount **lcp)
    537  1.40  riastrad {
    538  1.40  riastrad 	devmajor_t cmajor;
    539  1.40  riastrad 	const struct cdevsw *cdev = NULL, *const *curcdevsw;
    540  1.40  riastrad 	struct devswref *curcdevswref;
    541  1.40  riastrad 	int s;
    542  1.40  riastrad 
    543  1.40  riastrad 	if (dev == NODEV)
    544  1.40  riastrad 		return NULL;
    545  1.40  riastrad 	cmajor = major(dev);
    546  1.40  riastrad 	if (cmajor < 0)
    547  1.40  riastrad 		return NULL;
    548  1.40  riastrad 
    549  1.40  riastrad 	s = pserialize_read_enter();
    550  1.40  riastrad 
    551  1.40  riastrad 	/*
    552  1.40  riastrad 	 * max_cdevsws never goes down, so it is safe to rely on this
    553  1.40  riastrad 	 * condition without any locking for the array access below.
    554  1.40  riastrad 	 * Test sys_cdevsws first so we can avoid the memory barrier in
    555  1.40  riastrad 	 * that case.
    556  1.40  riastrad 	 */
    557  1.40  riastrad 	if (cmajor >= sys_cdevsws &&
    558  1.40  riastrad 	    cmajor >= atomic_load_acquire(&max_cdevsws))
    559  1.40  riastrad 		goto out;
    560  1.40  riastrad 	curcdevsw = atomic_load_consume(&cdevsw);
    561  1.40  riastrad 	if ((cdev = atomic_load_consume(&curcdevsw[cmajor])) == NULL)
    562  1.40  riastrad 		goto out;
    563  1.40  riastrad 
    564  1.40  riastrad 	curcdevswref = atomic_load_consume(&cdevswref);
    565  1.40  riastrad 	if (curcdevswref == NULL) {
    566  1.40  riastrad 		*lcp = NULL;
    567  1.40  riastrad 	} else if ((*lcp = curcdevswref[cmajor].dr_lc) != NULL) {
    568  1.40  riastrad 		localcount_acquire(*lcp);
    569  1.40  riastrad 	}
    570  1.40  riastrad out:
    571  1.40  riastrad 	pserialize_read_exit(s);
    572  1.40  riastrad 	return cdev;
    573  1.40  riastrad }
    574  1.40  riastrad 
    575  1.40  riastrad static void
    576  1.40  riastrad cdevsw_release(const struct cdevsw *cdev, struct localcount *lc)
    577  1.40  riastrad {
    578  1.40  riastrad 
    579  1.40  riastrad 	if (lc == NULL)
    580  1.40  riastrad 		return;
    581  1.40  riastrad 	localcount_release(lc, &devsw_cv, &device_lock);
    582   1.2   gehenna }
    583   1.2   gehenna 
    584  1.11        ad /*
    585  1.11        ad  * Look up a block device by reference to its operations set.
    586  1.11        ad  *
    587  1.11        ad  * => Caller must ensure that the device is not detached, and therefore
    588  1.11        ad  *    that the returned major is still valid when dereferenced.
    589  1.11        ad  */
    590  1.24  drochner devmajor_t
    591   1.2   gehenna bdevsw_lookup_major(const struct bdevsw *bdev)
    592   1.2   gehenna {
    593  1.40  riastrad 	const struct bdevsw *const *curbdevsw;
    594  1.40  riastrad 	devmajor_t bmajor, bmax;
    595   1.2   gehenna 
    596  1.40  riastrad 	bmax = atomic_load_acquire(&max_bdevsws);
    597  1.40  riastrad 	curbdevsw = atomic_load_consume(&bdevsw);
    598  1.40  riastrad 	for (bmajor = 0; bmajor < bmax; bmajor++) {
    599  1.40  riastrad 		if (atomic_load_relaxed(&curbdevsw[bmajor]) == bdev)
    600  1.45  riastrad 			return bmajor;
    601   1.2   gehenna 	}
    602   1.2   gehenna 
    603  1.45  riastrad 	return NODEVMAJOR;
    604   1.2   gehenna }
    605   1.2   gehenna 
    606  1.11        ad /*
    607  1.11        ad  * Look up a character device by reference to its operations set.
    608  1.11        ad  *
    609  1.11        ad  * => Caller must ensure that the device is not detached, and therefore
    610  1.11        ad  *    that the returned major is still valid when dereferenced.
    611  1.11        ad  */
    612  1.24  drochner devmajor_t
    613   1.2   gehenna cdevsw_lookup_major(const struct cdevsw *cdev)
    614   1.2   gehenna {
    615  1.40  riastrad 	const struct cdevsw *const *curcdevsw;
    616  1.40  riastrad 	devmajor_t cmajor, cmax;
    617   1.2   gehenna 
    618  1.40  riastrad 	cmax = atomic_load_acquire(&max_cdevsws);
    619  1.40  riastrad 	curcdevsw = atomic_load_consume(&cdevsw);
    620  1.40  riastrad 	for (cmajor = 0; cmajor < cmax; cmajor++) {
    621  1.40  riastrad 		if (atomic_load_relaxed(&curcdevsw[cmajor]) == cdev)
    622  1.45  riastrad 			return cmajor;
    623   1.2   gehenna 	}
    624   1.2   gehenna 
    625  1.45  riastrad 	return NODEVMAJOR;
    626   1.2   gehenna }
    627   1.2   gehenna 
    628   1.2   gehenna /*
    629   1.2   gehenna  * Convert from block major number to name.
    630  1.11        ad  *
    631  1.11        ad  * => Caller must ensure that the device is not detached, and therefore
    632  1.11        ad  *    that the name pointer is still valid when dereferenced.
    633   1.2   gehenna  */
    634   1.2   gehenna const char *
    635  1.24  drochner devsw_blk2name(devmajor_t bmajor)
    636   1.2   gehenna {
    637  1.11        ad 	const char *name;
    638  1.24  drochner 	devmajor_t cmajor;
    639  1.24  drochner 	int i;
    640   1.2   gehenna 
    641  1.11        ad 	name = NULL;
    642  1.11        ad 	cmajor = -1;
    643  1.11        ad 
    644  1.23     pooka 	mutex_enter(&device_lock);
    645  1.11        ad 	if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
    646  1.23     pooka 		mutex_exit(&device_lock);
    647  1.45  riastrad 		return NULL;
    648   1.2   gehenna 	}
    649  1.45  riastrad 	for (i = 0; i < max_devsw_convs; i++) {
    650  1.11        ad 		if (devsw_conv[i].d_bmajor == bmajor) {
    651  1.11        ad 			cmajor = devsw_conv[i].d_cmajor;
    652  1.11        ad 			break;
    653  1.11        ad 		}
    654  1.11        ad 	}
    655  1.11        ad 	if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
    656  1.11        ad 		name = devsw_conv[i].d_name;
    657  1.23     pooka 	mutex_exit(&device_lock);
    658   1.2   gehenna 
    659  1.45  riastrad 	return name;
    660   1.2   gehenna }
    661   1.2   gehenna 
    662   1.2   gehenna /*
    663  1.26      haad  * Convert char major number to device driver name.
    664  1.26      haad  */
    665  1.27      yamt const char *
    666  1.26      haad cdevsw_getname(devmajor_t major)
    667  1.26      haad {
    668  1.26      haad 	const char *name;
    669  1.26      haad 	int i;
    670  1.26      haad 
    671  1.26      haad 	name = NULL;
    672  1.26      haad 
    673  1.26      haad 	if (major < 0)
    674  1.45  riastrad 		return NULL;
    675  1.45  riastrad 
    676  1.26      haad 	mutex_enter(&device_lock);
    677  1.45  riastrad 	for (i = 0; i < max_devsw_convs; i++) {
    678  1.26      haad 		if (devsw_conv[i].d_cmajor == major) {
    679  1.26      haad 			name = devsw_conv[i].d_name;
    680  1.26      haad 			break;
    681  1.26      haad 		}
    682  1.26      haad 	}
    683  1.26      haad 	mutex_exit(&device_lock);
    684  1.45  riastrad 	return name;
    685  1.26      haad }
    686  1.26      haad 
    687  1.26      haad /*
    688  1.26      haad  * Convert block major number to device driver name.
    689  1.26      haad  */
    690  1.27      yamt const char *
    691  1.26      haad bdevsw_getname(devmajor_t major)
    692  1.26      haad {
    693  1.26      haad 	const char *name;
    694  1.26      haad 	int i;
    695  1.26      haad 
    696  1.26      haad 	name = NULL;
    697  1.26      haad 
    698  1.26      haad 	if (major < 0)
    699  1.45  riastrad 		return NULL;
    700  1.45  riastrad 
    701  1.26      haad 	mutex_enter(&device_lock);
    702  1.45  riastrad 	for (i = 0; i < max_devsw_convs; i++) {
    703  1.26      haad 		if (devsw_conv[i].d_bmajor == major) {
    704  1.26      haad 			name = devsw_conv[i].d_name;
    705  1.26      haad 			break;
    706  1.26      haad 		}
    707  1.26      haad 	}
    708  1.26      haad 	mutex_exit(&device_lock);
    709  1.45  riastrad 	return name;
    710  1.26      haad }
    711  1.26      haad 
    712  1.26      haad /*
    713   1.2   gehenna  * Convert from device name to block major number.
    714  1.11        ad  *
    715  1.11        ad  * => Caller must ensure that the device is not detached, and therefore
    716  1.11        ad  *    that the major number is still valid when dereferenced.
    717   1.2   gehenna  */
    718  1.24  drochner devmajor_t
    719   1.2   gehenna devsw_name2blk(const char *name, char *devname, size_t devnamelen)
    720   1.2   gehenna {
    721   1.2   gehenna 	struct devsw_conv *conv;
    722  1.24  drochner 	devmajor_t bmajor;
    723  1.24  drochner 	int i;
    724   1.2   gehenna 
    725   1.2   gehenna 	if (name == NULL)
    726  1.45  riastrad 		return NODEVMAJOR;
    727   1.2   gehenna 
    728  1.23     pooka 	mutex_enter(&device_lock);
    729  1.45  riastrad 	for (i = 0; i < max_devsw_convs; i++) {
    730   1.5       mrg 		size_t len;
    731   1.5       mrg 
    732   1.2   gehenna 		conv = &devsw_conv[i];
    733   1.2   gehenna 		if (conv->d_name == NULL)
    734   1.2   gehenna 			continue;
    735   1.5       mrg 		len = strlen(conv->d_name);
    736   1.5       mrg 		if (strncmp(conv->d_name, name, len) != 0)
    737   1.5       mrg 			continue;
    738  1.45  riastrad 		if (name[len] != '\0' && !isdigit((unsigned char)name[len]))
    739   1.2   gehenna 			continue;
    740   1.2   gehenna 		bmajor = conv->d_bmajor;
    741   1.2   gehenna 		if (bmajor < 0 || bmajor >= max_bdevsws ||
    742   1.2   gehenna 		    bdevsw[bmajor] == NULL)
    743   1.5       mrg 			break;
    744   1.2   gehenna 		if (devname != NULL) {
    745   1.2   gehenna #ifdef DEVSW_DEBUG
    746   1.2   gehenna 			if (strlen(conv->d_name) >= devnamelen)
    747  1.45  riastrad 				printf("%s: too short buffer\n", __func__);
    748   1.2   gehenna #endif /* DEVSW_DEBUG */
    749   1.4   tsutsui 			strncpy(devname, conv->d_name, devnamelen);
    750   1.2   gehenna 			devname[devnamelen - 1] = '\0';
    751   1.2   gehenna 		}
    752  1.23     pooka 		mutex_exit(&device_lock);
    753  1.45  riastrad 		return bmajor;
    754   1.2   gehenna 	}
    755   1.2   gehenna 
    756  1.23     pooka 	mutex_exit(&device_lock);
    757  1.45  riastrad 	return NODEVMAJOR;
    758   1.2   gehenna }
    759   1.2   gehenna 
    760   1.2   gehenna /*
    761  1.16    plunky  * Convert from device name to char major number.
    762  1.16    plunky  *
    763  1.16    plunky  * => Caller must ensure that the device is not detached, and therefore
    764  1.16    plunky  *    that the major number is still valid when dereferenced.
    765  1.16    plunky  */
    766  1.24  drochner devmajor_t
    767  1.16    plunky devsw_name2chr(const char *name, char *devname, size_t devnamelen)
    768  1.16    plunky {
    769  1.16    plunky 	struct devsw_conv *conv;
    770  1.24  drochner 	devmajor_t cmajor;
    771  1.24  drochner 	int i;
    772  1.16    plunky 
    773  1.16    plunky 	if (name == NULL)
    774  1.45  riastrad 		return NODEVMAJOR;
    775  1.16    plunky 
    776  1.23     pooka 	mutex_enter(&device_lock);
    777  1.45  riastrad 	for (i = 0; i < max_devsw_convs; i++) {
    778  1.16    plunky 		size_t len;
    779  1.16    plunky 
    780  1.16    plunky 		conv = &devsw_conv[i];
    781  1.16    plunky 		if (conv->d_name == NULL)
    782  1.16    plunky 			continue;
    783  1.16    plunky 		len = strlen(conv->d_name);
    784  1.16    plunky 		if (strncmp(conv->d_name, name, len) != 0)
    785  1.16    plunky 			continue;
    786  1.45  riastrad 		if (name[len] != '\0' && !isdigit((unsigned char)name[len]))
    787  1.16    plunky 			continue;
    788  1.16    plunky 		cmajor = conv->d_cmajor;
    789  1.16    plunky 		if (cmajor < 0 || cmajor >= max_cdevsws ||
    790  1.16    plunky 		    cdevsw[cmajor] == NULL)
    791  1.16    plunky 			break;
    792  1.16    plunky 		if (devname != NULL) {
    793  1.16    plunky #ifdef DEVSW_DEBUG
    794  1.16    plunky 			if (strlen(conv->d_name) >= devnamelen)
    795  1.37  pgoyette 				printf("%s: too short buffer", __func__);
    796  1.16    plunky #endif /* DEVSW_DEBUG */
    797  1.16    plunky 			strncpy(devname, conv->d_name, devnamelen);
    798  1.16    plunky 			devname[devnamelen - 1] = '\0';
    799  1.16    plunky 		}
    800  1.23     pooka 		mutex_exit(&device_lock);
    801  1.45  riastrad 		return cmajor;
    802  1.16    plunky 	}
    803  1.16    plunky 
    804  1.23     pooka 	mutex_exit(&device_lock);
    805  1.45  riastrad 	return NODEVMAJOR;
    806  1.16    plunky }
    807  1.16    plunky 
    808  1.16    plunky /*
    809   1.2   gehenna  * Convert from character dev_t to block dev_t.
    810  1.11        ad  *
    811  1.11        ad  * => Caller must ensure that the device is not detached, and therefore
    812  1.11        ad  *    that the major number is still valid when dereferenced.
    813   1.2   gehenna  */
    814   1.2   gehenna dev_t
    815   1.2   gehenna devsw_chr2blk(dev_t cdev)
    816   1.2   gehenna {
    817  1.24  drochner 	devmajor_t bmajor, cmajor;
    818  1.24  drochner 	int i;
    819  1.11        ad 	dev_t rv;
    820   1.2   gehenna 
    821   1.2   gehenna 	cmajor = major(cdev);
    822  1.24  drochner 	bmajor = NODEVMAJOR;
    823  1.11        ad 	rv = NODEV;
    824   1.2   gehenna 
    825  1.23     pooka 	mutex_enter(&device_lock);
    826  1.11        ad 	if (cmajor < 0 || cmajor >= max_cdevsws || cdevsw[cmajor] == NULL) {
    827  1.23     pooka 		mutex_exit(&device_lock);
    828  1.45  riastrad 		return NODEV;
    829  1.11        ad 	}
    830  1.45  riastrad 	for (i = 0; i < max_devsw_convs; i++) {
    831  1.11        ad 		if (devsw_conv[i].d_cmajor == cmajor) {
    832  1.11        ad 			bmajor = devsw_conv[i].d_bmajor;
    833  1.11        ad 			break;
    834  1.11        ad 		}
    835   1.2   gehenna 	}
    836  1.11        ad 	if (bmajor >= 0 && bmajor < max_bdevsws && bdevsw[bmajor] != NULL)
    837  1.11        ad 		rv = makedev(bmajor, minor(cdev));
    838  1.23     pooka 	mutex_exit(&device_lock);
    839   1.2   gehenna 
    840  1.45  riastrad 	return rv;
    841   1.2   gehenna }
    842   1.2   gehenna 
    843   1.2   gehenna /*
    844   1.2   gehenna  * Convert from block dev_t to character dev_t.
    845  1.11        ad  *
    846  1.11        ad  * => Caller must ensure that the device is not detached, and therefore
    847  1.11        ad  *    that the major number is still valid when dereferenced.
    848   1.2   gehenna  */
    849   1.2   gehenna dev_t
    850   1.2   gehenna devsw_blk2chr(dev_t bdev)
    851   1.2   gehenna {
    852  1.24  drochner 	devmajor_t bmajor, cmajor;
    853  1.24  drochner 	int i;
    854  1.11        ad 	dev_t rv;
    855   1.2   gehenna 
    856  1.11        ad 	bmajor = major(bdev);
    857  1.24  drochner 	cmajor = NODEVMAJOR;
    858  1.11        ad 	rv = NODEV;
    859  1.11        ad 
    860  1.23     pooka 	mutex_enter(&device_lock);
    861  1.11        ad 	if (bmajor < 0 || bmajor >= max_bdevsws || bdevsw[bmajor] == NULL) {
    862  1.23     pooka 		mutex_exit(&device_lock);
    863  1.45  riastrad 		return NODEV;
    864  1.11        ad 	}
    865  1.45  riastrad 	for (i = 0; i < max_devsw_convs; i++) {
    866  1.11        ad 		if (devsw_conv[i].d_bmajor == bmajor) {
    867  1.11        ad 			cmajor = devsw_conv[i].d_cmajor;
    868  1.11        ad 			break;
    869  1.11        ad 		}
    870  1.11        ad 	}
    871  1.11        ad 	if (cmajor >= 0 && cmajor < max_cdevsws && cdevsw[cmajor] != NULL)
    872  1.11        ad 		rv = makedev(cmajor, minor(bdev));
    873  1.23     pooka 	mutex_exit(&device_lock);
    874   1.2   gehenna 
    875  1.45  riastrad 	return rv;
    876  1.11        ad }
    877  1.11        ad 
    878  1.11        ad /*
    879  1.11        ad  * Device access methods.
    880  1.11        ad  */
    881  1.11        ad 
    882  1.11        ad #define	DEV_LOCK(d)						\
    883  1.17        ad 	if ((mpflag = (d->d_flag & D_MPSAFE)) == 0) {		\
    884  1.17        ad 		KERNEL_LOCK(1, NULL);				\
    885  1.11        ad 	}
    886   1.2   gehenna 
    887  1.11        ad #define	DEV_UNLOCK(d)						\
    888  1.17        ad 	if (mpflag == 0) {					\
    889  1.17        ad 		KERNEL_UNLOCK_ONE(NULL);			\
    890   1.2   gehenna 	}
    891   1.2   gehenna 
    892  1.11        ad int
    893  1.11        ad bdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
    894  1.11        ad {
    895  1.11        ad 	const struct bdevsw *d;
    896  1.40  riastrad 	struct localcount *lc;
    897  1.41  riastrad 	device_t dv = NULL/*XXXGCC*/;
    898  1.41  riastrad 	int unit, rv, mpflag;
    899  1.11        ad 
    900  1.40  riastrad 	d = bdevsw_lookup_acquire(dev, &lc);
    901  1.11        ad 	if (d == NULL)
    902  1.11        ad 		return ENXIO;
    903  1.11        ad 
    904  1.41  riastrad 	if (d->d_devtounit) {
    905  1.41  riastrad 		/*
    906  1.41  riastrad 		 * If the device node corresponds to an autoconf device
    907  1.41  riastrad 		 * instance, acquire a reference to it so that during
    908  1.41  riastrad 		 * d_open, device_lookup is stable.
    909  1.41  riastrad 		 *
    910  1.41  riastrad 		 * XXX This should also arrange to instantiate cloning
    911  1.41  riastrad 		 * pseudo-devices if appropriate, but that requires
    912  1.41  riastrad 		 * reviewing them all to find and verify a common
    913  1.41  riastrad 		 * pattern.
    914  1.41  riastrad 		 */
    915  1.41  riastrad 		if ((unit = (*d->d_devtounit)(dev)) == -1)
    916  1.41  riastrad 			return ENXIO;
    917  1.41  riastrad 		if ((dv = device_lookup_acquire(d->d_cfdriver, unit)) == NULL)
    918  1.41  riastrad 			return ENXIO;
    919  1.41  riastrad 	}
    920  1.41  riastrad 
    921  1.11        ad 	DEV_LOCK(d);
    922  1.11        ad 	rv = (*d->d_open)(dev, flag, devtype, l);
    923  1.11        ad 	DEV_UNLOCK(d);
    924  1.11        ad 
    925  1.41  riastrad 	if (d->d_devtounit) {
    926  1.41  riastrad 		device_release(dv);
    927  1.41  riastrad 	}
    928  1.41  riastrad 
    929  1.40  riastrad 	bdevsw_release(d, lc);
    930  1.40  riastrad 
    931  1.11        ad 	return rv;
    932  1.11        ad }
    933  1.11        ad 
    934  1.11        ad int
    935  1.44  riastrad bdev_cancel(dev_t dev, int flag, int devtype, struct lwp *l)
    936  1.44  riastrad {
    937  1.44  riastrad 	const struct bdevsw *d;
    938  1.44  riastrad 	int rv, mpflag;
    939  1.44  riastrad 
    940  1.44  riastrad 	if ((d = bdevsw_lookup(dev)) == NULL)
    941  1.44  riastrad 		return ENXIO;
    942  1.44  riastrad 	if (d->d_cancel == NULL)
    943  1.44  riastrad 		return ENODEV;
    944  1.44  riastrad 
    945  1.44  riastrad 	DEV_LOCK(d);
    946  1.44  riastrad 	rv = (*d->d_cancel)(dev, flag, devtype, l);
    947  1.44  riastrad 	DEV_UNLOCK(d);
    948  1.44  riastrad 
    949  1.44  riastrad 	return rv;
    950  1.44  riastrad }
    951  1.44  riastrad 
    952  1.44  riastrad int
    953  1.11        ad bdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
    954  1.11        ad {
    955  1.11        ad 	const struct bdevsw *d;
    956  1.17        ad 	int rv, mpflag;
    957  1.11        ad 
    958  1.11        ad 	if ((d = bdevsw_lookup(dev)) == NULL)
    959  1.11        ad 		return ENXIO;
    960  1.11        ad 
    961  1.11        ad 	DEV_LOCK(d);
    962  1.11        ad 	rv = (*d->d_close)(dev, flag, devtype, l);
    963  1.11        ad 	DEV_UNLOCK(d);
    964  1.11        ad 
    965  1.11        ad 	return rv;
    966  1.11        ad }
    967  1.11        ad 
    968  1.34       riz SDT_PROVIDER_DECLARE(io);
    969  1.34       riz SDT_PROBE_DEFINE1(io, kernel, , start, "struct buf *"/*bp*/);
    970  1.34       riz 
    971  1.11        ad void
    972  1.11        ad bdev_strategy(struct buf *bp)
    973  1.11        ad {
    974  1.11        ad 	const struct bdevsw *d;
    975  1.17        ad 	int mpflag;
    976  1.11        ad 
    977  1.34       riz 	SDT_PROBE1(io, kernel, , start, bp);
    978  1.34       riz 
    979  1.28  jmcneill 	if ((d = bdevsw_lookup(bp->b_dev)) == NULL) {
    980  1.28  jmcneill 		bp->b_error = ENXIO;
    981  1.28  jmcneill 		bp->b_resid = bp->b_bcount;
    982  1.31     pooka 		biodone_vfs(bp); /* biodone() iff vfs present */
    983  1.28  jmcneill 		return;
    984  1.28  jmcneill 	}
    985  1.11        ad 
    986  1.11        ad 	DEV_LOCK(d);
    987  1.11        ad 	(*d->d_strategy)(bp);
    988  1.11        ad 	DEV_UNLOCK(d);
    989  1.11        ad }
    990  1.11        ad 
    991  1.11        ad int
    992  1.11        ad bdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    993  1.11        ad {
    994  1.11        ad 	const struct bdevsw *d;
    995  1.17        ad 	int rv, mpflag;
    996  1.11        ad 
    997  1.11        ad 	if ((d = bdevsw_lookup(dev)) == NULL)
    998  1.11        ad 		return ENXIO;
    999  1.11        ad 
   1000  1.11        ad 	DEV_LOCK(d);
   1001  1.11        ad 	rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
   1002  1.11        ad 	DEV_UNLOCK(d);
   1003  1.11        ad 
   1004  1.11        ad 	return rv;
   1005  1.11        ad }
   1006  1.11        ad 
   1007  1.11        ad int
   1008  1.11        ad bdev_dump(dev_t dev, daddr_t addr, void *data, size_t sz)
   1009  1.11        ad {
   1010  1.11        ad 	const struct bdevsw *d;
   1011  1.11        ad 	int rv;
   1012  1.11        ad 
   1013  1.11        ad 	/*
   1014  1.11        ad 	 * Dump can be called without the device open.  Since it can
   1015  1.11        ad 	 * currently only be called with the system paused (and in a
   1016  1.11        ad 	 * potentially unstable state), we don't perform any locking.
   1017  1.11        ad 	 */
   1018  1.11        ad 	if ((d = bdevsw_lookup(dev)) == NULL)
   1019  1.11        ad 		return ENXIO;
   1020  1.11        ad 
   1021  1.11        ad 	/* DEV_LOCK(d); */
   1022  1.11        ad 	rv = (*d->d_dump)(dev, addr, data, sz);
   1023  1.11        ad 	/* DEV_UNLOCK(d); */
   1024  1.11        ad 
   1025  1.11        ad 	return rv;
   1026  1.11        ad }
   1027  1.11        ad 
   1028  1.11        ad int
   1029  1.35       nat bdev_flags(dev_t dev)
   1030  1.35       nat {
   1031  1.35       nat 	const struct bdevsw *d;
   1032  1.35       nat 
   1033  1.35       nat 	if ((d = bdevsw_lookup(dev)) == NULL)
   1034  1.35       nat 		return 0;
   1035  1.35       nat 	return d->d_flag & ~D_TYPEMASK;
   1036  1.35       nat }
   1037  1.35       nat 
   1038  1.35       nat int
   1039  1.11        ad bdev_type(dev_t dev)
   1040  1.11        ad {
   1041  1.11        ad 	const struct bdevsw *d;
   1042  1.11        ad 
   1043  1.11        ad 	if ((d = bdevsw_lookup(dev)) == NULL)
   1044  1.11        ad 		return D_OTHER;
   1045  1.11        ad 	return d->d_flag & D_TYPEMASK;
   1046  1.11        ad }
   1047  1.11        ad 
   1048  1.11        ad int
   1049  1.29       mrg bdev_size(dev_t dev)
   1050  1.29       mrg {
   1051  1.29       mrg 	const struct bdevsw *d;
   1052  1.29       mrg 	int rv, mpflag = 0;
   1053  1.29       mrg 
   1054  1.29       mrg 	if ((d = bdevsw_lookup(dev)) == NULL ||
   1055  1.29       mrg 	    d->d_psize == NULL)
   1056  1.29       mrg 		return -1;
   1057  1.29       mrg 
   1058  1.29       mrg 	/*
   1059  1.29       mrg 	 * Don't to try lock the device if we're dumping.
   1060  1.30       mrg 	 * XXX: is there a better way to test this?
   1061  1.29       mrg 	 */
   1062  1.29       mrg 	if ((boothowto & RB_DUMP) == 0)
   1063  1.29       mrg 		DEV_LOCK(d);
   1064  1.29       mrg 	rv = (*d->d_psize)(dev);
   1065  1.29       mrg 	if ((boothowto & RB_DUMP) == 0)
   1066  1.29       mrg 		DEV_UNLOCK(d);
   1067  1.29       mrg 
   1068  1.29       mrg 	return rv;
   1069  1.29       mrg }
   1070  1.29       mrg 
   1071  1.29       mrg int
   1072  1.32  dholland bdev_discard(dev_t dev, off_t pos, off_t len)
   1073  1.32  dholland {
   1074  1.32  dholland 	const struct bdevsw *d;
   1075  1.32  dholland 	int rv, mpflag;
   1076  1.32  dholland 
   1077  1.32  dholland 	if ((d = bdevsw_lookup(dev)) == NULL)
   1078  1.32  dholland 		return ENXIO;
   1079  1.32  dholland 
   1080  1.32  dholland 	DEV_LOCK(d);
   1081  1.32  dholland 	rv = (*d->d_discard)(dev, pos, len);
   1082  1.32  dholland 	DEV_UNLOCK(d);
   1083  1.32  dholland 
   1084  1.32  dholland 	return rv;
   1085  1.32  dholland }
   1086  1.32  dholland 
   1087  1.43  riastrad void
   1088  1.43  riastrad bdev_detached(dev_t dev)
   1089  1.43  riastrad {
   1090  1.43  riastrad 	const struct bdevsw *d;
   1091  1.43  riastrad 	device_t dv;
   1092  1.43  riastrad 	int unit;
   1093  1.43  riastrad 
   1094  1.43  riastrad 	if ((d = bdevsw_lookup(dev)) == NULL)
   1095  1.43  riastrad 		return;
   1096  1.43  riastrad 	if (d->d_devtounit == NULL)
   1097  1.43  riastrad 		return;
   1098  1.43  riastrad 	if ((unit = (*d->d_devtounit)(dev)) == -1)
   1099  1.43  riastrad 		return;
   1100  1.43  riastrad 	if ((dv = device_lookup(d->d_cfdriver, unit)) == NULL)
   1101  1.43  riastrad 		return;
   1102  1.43  riastrad 	config_detach_commit(dv);
   1103  1.43  riastrad }
   1104  1.43  riastrad 
   1105  1.32  dholland int
   1106  1.11        ad cdev_open(dev_t dev, int flag, int devtype, lwp_t *l)
   1107  1.11        ad {
   1108  1.11        ad 	const struct cdevsw *d;
   1109  1.40  riastrad 	struct localcount *lc;
   1110  1.41  riastrad 	device_t dv = NULL/*XXXGCC*/;
   1111  1.41  riastrad 	int unit, rv, mpflag;
   1112  1.11        ad 
   1113  1.40  riastrad 	d = cdevsw_lookup_acquire(dev, &lc);
   1114  1.11        ad 	if (d == NULL)
   1115  1.11        ad 		return ENXIO;
   1116  1.11        ad 
   1117  1.41  riastrad 	if (d->d_devtounit) {
   1118  1.41  riastrad 		/*
   1119  1.41  riastrad 		 * If the device node corresponds to an autoconf device
   1120  1.41  riastrad 		 * instance, acquire a reference to it so that during
   1121  1.41  riastrad 		 * d_open, device_lookup is stable.
   1122  1.41  riastrad 		 *
   1123  1.41  riastrad 		 * XXX This should also arrange to instantiate cloning
   1124  1.41  riastrad 		 * pseudo-devices if appropriate, but that requires
   1125  1.41  riastrad 		 * reviewing them all to find and verify a common
   1126  1.41  riastrad 		 * pattern.
   1127  1.41  riastrad 		 */
   1128  1.41  riastrad 		if ((unit = (*d->d_devtounit)(dev)) == -1)
   1129  1.41  riastrad 			return ENXIO;
   1130  1.41  riastrad 		if ((dv = device_lookup_acquire(d->d_cfdriver, unit)) == NULL)
   1131  1.41  riastrad 			return ENXIO;
   1132  1.41  riastrad 	}
   1133  1.41  riastrad 
   1134  1.11        ad 	DEV_LOCK(d);
   1135  1.11        ad 	rv = (*d->d_open)(dev, flag, devtype, l);
   1136  1.11        ad 	DEV_UNLOCK(d);
   1137  1.11        ad 
   1138  1.41  riastrad 	if (d->d_devtounit) {
   1139  1.41  riastrad 		device_release(dv);
   1140  1.41  riastrad 	}
   1141  1.41  riastrad 
   1142  1.40  riastrad 	cdevsw_release(d, lc);
   1143  1.40  riastrad 
   1144  1.11        ad 	return rv;
   1145  1.11        ad }
   1146  1.11        ad 
   1147  1.11        ad int
   1148  1.44  riastrad cdev_cancel(dev_t dev, int flag, int devtype, struct lwp *l)
   1149  1.44  riastrad {
   1150  1.44  riastrad 	const struct cdevsw *d;
   1151  1.44  riastrad 	int rv, mpflag;
   1152  1.44  riastrad 
   1153  1.44  riastrad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1154  1.44  riastrad 		return ENXIO;
   1155  1.44  riastrad 	if (d->d_cancel == NULL)
   1156  1.44  riastrad 		return ENODEV;
   1157  1.44  riastrad 
   1158  1.44  riastrad 	DEV_LOCK(d);
   1159  1.44  riastrad 	rv = (*d->d_cancel)(dev, flag, devtype, l);
   1160  1.44  riastrad 	DEV_UNLOCK(d);
   1161  1.44  riastrad 
   1162  1.44  riastrad 	return rv;
   1163  1.44  riastrad }
   1164  1.44  riastrad 
   1165  1.44  riastrad int
   1166  1.11        ad cdev_close(dev_t dev, int flag, int devtype, lwp_t *l)
   1167  1.11        ad {
   1168  1.11        ad 	const struct cdevsw *d;
   1169  1.17        ad 	int rv, mpflag;
   1170  1.11        ad 
   1171  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1172  1.11        ad 		return ENXIO;
   1173  1.11        ad 
   1174  1.11        ad 	DEV_LOCK(d);
   1175  1.11        ad 	rv = (*d->d_close)(dev, flag, devtype, l);
   1176  1.11        ad 	DEV_UNLOCK(d);
   1177  1.11        ad 
   1178  1.11        ad 	return rv;
   1179  1.11        ad }
   1180  1.11        ad 
   1181  1.11        ad int
   1182  1.11        ad cdev_read(dev_t dev, struct uio *uio, int flag)
   1183  1.11        ad {
   1184  1.11        ad 	const struct cdevsw *d;
   1185  1.17        ad 	int rv, mpflag;
   1186  1.11        ad 
   1187  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1188  1.11        ad 		return ENXIO;
   1189  1.11        ad 
   1190  1.11        ad 	DEV_LOCK(d);
   1191  1.11        ad 	rv = (*d->d_read)(dev, uio, flag);
   1192  1.11        ad 	DEV_UNLOCK(d);
   1193  1.11        ad 
   1194  1.11        ad 	return rv;
   1195  1.11        ad }
   1196  1.11        ad 
   1197  1.11        ad int
   1198  1.11        ad cdev_write(dev_t dev, struct uio *uio, int flag)
   1199  1.11        ad {
   1200  1.11        ad 	const struct cdevsw *d;
   1201  1.17        ad 	int rv, mpflag;
   1202  1.11        ad 
   1203  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1204  1.11        ad 		return ENXIO;
   1205  1.11        ad 
   1206  1.11        ad 	DEV_LOCK(d);
   1207  1.11        ad 	rv = (*d->d_write)(dev, uio, flag);
   1208  1.11        ad 	DEV_UNLOCK(d);
   1209  1.11        ad 
   1210  1.11        ad 	return rv;
   1211  1.11        ad }
   1212  1.11        ad 
   1213  1.11        ad int
   1214  1.11        ad cdev_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
   1215  1.11        ad {
   1216  1.11        ad 	const struct cdevsw *d;
   1217  1.17        ad 	int rv, mpflag;
   1218  1.11        ad 
   1219  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1220  1.11        ad 		return ENXIO;
   1221  1.11        ad 
   1222  1.11        ad 	DEV_LOCK(d);
   1223  1.11        ad 	rv = (*d->d_ioctl)(dev, cmd, data, flag, l);
   1224  1.11        ad 	DEV_UNLOCK(d);
   1225  1.11        ad 
   1226  1.11        ad 	return rv;
   1227  1.11        ad }
   1228  1.11        ad 
   1229  1.11        ad void
   1230  1.11        ad cdev_stop(struct tty *tp, int flag)
   1231  1.11        ad {
   1232  1.11        ad 	const struct cdevsw *d;
   1233  1.17        ad 	int mpflag;
   1234  1.11        ad 
   1235  1.11        ad 	if ((d = cdevsw_lookup(tp->t_dev)) == NULL)
   1236  1.11        ad 		return;
   1237  1.11        ad 
   1238  1.11        ad 	DEV_LOCK(d);
   1239  1.11        ad 	(*d->d_stop)(tp, flag);
   1240  1.11        ad 	DEV_UNLOCK(d);
   1241  1.11        ad }
   1242  1.11        ad 
   1243  1.11        ad struct tty *
   1244  1.11        ad cdev_tty(dev_t dev)
   1245  1.11        ad {
   1246  1.11        ad 	const struct cdevsw *d;
   1247  1.11        ad 
   1248  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1249  1.11        ad 		return NULL;
   1250  1.11        ad 
   1251  1.12        ad 	/* XXX Check if necessary. */
   1252  1.12        ad 	if (d->d_tty == NULL)
   1253  1.12        ad 		return NULL;
   1254  1.12        ad 
   1255  1.21        ad 	return (*d->d_tty)(dev);
   1256  1.11        ad }
   1257  1.11        ad 
   1258  1.11        ad int
   1259  1.11        ad cdev_poll(dev_t dev, int flag, lwp_t *l)
   1260  1.11        ad {
   1261  1.11        ad 	const struct cdevsw *d;
   1262  1.17        ad 	int rv, mpflag;
   1263  1.11        ad 
   1264  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1265  1.11        ad 		return POLLERR;
   1266  1.11        ad 
   1267  1.11        ad 	DEV_LOCK(d);
   1268  1.11        ad 	rv = (*d->d_poll)(dev, flag, l);
   1269  1.11        ad 	DEV_UNLOCK(d);
   1270  1.11        ad 
   1271  1.11        ad 	return rv;
   1272  1.11        ad }
   1273  1.11        ad 
   1274  1.11        ad paddr_t
   1275  1.11        ad cdev_mmap(dev_t dev, off_t off, int flag)
   1276  1.11        ad {
   1277  1.11        ad 	const struct cdevsw *d;
   1278  1.11        ad 	paddr_t rv;
   1279  1.17        ad 	int mpflag;
   1280  1.11        ad 
   1281  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1282  1.11        ad 		return (paddr_t)-1LL;
   1283  1.11        ad 
   1284  1.11        ad 	DEV_LOCK(d);
   1285  1.11        ad 	rv = (*d->d_mmap)(dev, off, flag);
   1286  1.11        ad 	DEV_UNLOCK(d);
   1287  1.11        ad 
   1288  1.11        ad 	return rv;
   1289  1.11        ad }
   1290  1.11        ad 
   1291  1.11        ad int
   1292  1.11        ad cdev_kqfilter(dev_t dev, struct knote *kn)
   1293  1.11        ad {
   1294  1.11        ad 	const struct cdevsw *d;
   1295  1.17        ad 	int rv, mpflag;
   1296  1.11        ad 
   1297  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1298  1.11        ad 		return ENXIO;
   1299  1.11        ad 
   1300  1.11        ad 	DEV_LOCK(d);
   1301  1.11        ad 	rv = (*d->d_kqfilter)(dev, kn);
   1302  1.11        ad 	DEV_UNLOCK(d);
   1303  1.11        ad 
   1304  1.11        ad 	return rv;
   1305  1.11        ad }
   1306  1.11        ad 
   1307  1.11        ad int
   1308  1.32  dholland cdev_discard(dev_t dev, off_t pos, off_t len)
   1309  1.32  dholland {
   1310  1.32  dholland 	const struct cdevsw *d;
   1311  1.32  dholland 	int rv, mpflag;
   1312  1.32  dholland 
   1313  1.32  dholland 	if ((d = cdevsw_lookup(dev)) == NULL)
   1314  1.32  dholland 		return ENXIO;
   1315  1.32  dholland 
   1316  1.32  dholland 	DEV_LOCK(d);
   1317  1.32  dholland 	rv = (*d->d_discard)(dev, pos, len);
   1318  1.32  dholland 	DEV_UNLOCK(d);
   1319  1.32  dholland 
   1320  1.32  dholland 	return rv;
   1321  1.32  dholland }
   1322  1.32  dholland 
   1323  1.32  dholland int
   1324  1.35       nat cdev_flags(dev_t dev)
   1325  1.35       nat {
   1326  1.35       nat 	const struct cdevsw *d;
   1327  1.35       nat 
   1328  1.35       nat 	if ((d = cdevsw_lookup(dev)) == NULL)
   1329  1.35       nat 		return 0;
   1330  1.35       nat 	return d->d_flag & ~D_TYPEMASK;
   1331  1.35       nat }
   1332  1.35       nat 
   1333  1.35       nat int
   1334  1.11        ad cdev_type(dev_t dev)
   1335  1.11        ad {
   1336  1.11        ad 	const struct cdevsw *d;
   1337  1.11        ad 
   1338  1.11        ad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1339  1.11        ad 		return D_OTHER;
   1340  1.11        ad 	return d->d_flag & D_TYPEMASK;
   1341   1.2   gehenna }
   1342  1.36  riastrad 
   1343  1.43  riastrad void
   1344  1.43  riastrad cdev_detached(dev_t dev)
   1345  1.43  riastrad {
   1346  1.43  riastrad 	const struct cdevsw *d;
   1347  1.43  riastrad 	device_t dv;
   1348  1.43  riastrad 	int unit;
   1349  1.43  riastrad 
   1350  1.43  riastrad 	if ((d = cdevsw_lookup(dev)) == NULL)
   1351  1.43  riastrad 		return;
   1352  1.43  riastrad 	if (d->d_devtounit == NULL)
   1353  1.43  riastrad 		return;
   1354  1.43  riastrad 	if ((unit = (*d->d_devtounit)(dev)) == -1)
   1355  1.43  riastrad 		return;
   1356  1.43  riastrad 	if ((dv = device_lookup(d->d_cfdriver, unit)) == NULL)
   1357  1.43  riastrad 		return;
   1358  1.43  riastrad 	config_detach_commit(dv);
   1359  1.43  riastrad }
   1360  1.43  riastrad 
   1361  1.36  riastrad /*
   1362  1.36  riastrad  * nommap(dev, off, prot)
   1363  1.36  riastrad  *
   1364  1.36  riastrad  *	mmap routine that always fails, for non-mmappable devices.
   1365  1.36  riastrad  */
   1366  1.36  riastrad paddr_t
   1367  1.36  riastrad nommap(dev_t dev, off_t off, int prot)
   1368  1.36  riastrad {
   1369  1.36  riastrad 
   1370  1.36  riastrad 	return (paddr_t)-1;
   1371  1.36  riastrad }
   1372  1.42  riastrad 
   1373  1.42  riastrad /*
   1374  1.42  riastrad  * dev_minor_unit(dev)
   1375  1.42  riastrad  *
   1376  1.42  riastrad  *	Returns minor(dev) as an int.  Intended for use with struct
   1377  1.42  riastrad  *	bdevsw, cdevsw::d_devtounit for drivers whose /dev nodes are
   1378  1.42  riastrad  *	implemented by reference to an autoconf instance with the minor
   1379  1.42  riastrad  *	number.
   1380  1.42  riastrad  */
   1381  1.42  riastrad int
   1382  1.42  riastrad dev_minor_unit(dev_t dev)
   1383  1.42  riastrad {
   1384  1.42  riastrad 
   1385  1.42  riastrad 	return minor(dev);
   1386  1.42  riastrad }
   1387