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