Home | History | Annotate | Line # | Download | only in dev
bio.c revision 1.2.2.2
      1  1.2.2.2      mjf /*	$NetBSD: bio.c,v 1.2.2.2 2008/02/18 21:05:31 mjf Exp $ */
      2      1.1   bouyer /*	$OpenBSD: bio.c,v 1.9 2007/03/20 02:35:55 marco Exp $	*/
      3      1.1   bouyer 
      4      1.1   bouyer /*
      5      1.1   bouyer  * Copyright (c) 2002 Niklas Hallqvist.  All rights reserved.
      6      1.1   bouyer  *
      7      1.1   bouyer  * Redistribution and use in source and binary forms, with or without
      8      1.1   bouyer  * modification, are permitted provided that the following conditions
      9      1.1   bouyer  * are met:
     10      1.1   bouyer  * 1. Redistributions of source code must retain the above copyright
     11      1.1   bouyer  *    notice, this list of conditions and the following disclaimer.
     12      1.1   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1   bouyer  *    notice, this list of conditions and the following disclaimer in the
     14      1.1   bouyer  *    documentation and/or other materials provided with the distribution.
     15      1.1   bouyer  *
     16      1.1   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21      1.1   bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22      1.1   bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23      1.1   bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24      1.1   bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25      1.1   bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26      1.1   bouyer  */
     27      1.1   bouyer 
     28      1.1   bouyer /* A device controller ioctl tunnelling device.  */
     29      1.1   bouyer 
     30      1.1   bouyer #include <sys/cdefs.h>
     31  1.2.2.2      mjf __KERNEL_RCSID(0, "$NetBSD: bio.c,v 1.2.2.2 2008/02/18 21:05:31 mjf Exp $");
     32  1.2.2.2      mjf 
     33  1.2.2.2      mjf #include "opt_compat_netbsd.h"
     34      1.1   bouyer 
     35      1.1   bouyer #include <sys/param.h>
     36      1.1   bouyer #include <sys/conf.h>
     37      1.1   bouyer #include <sys/device.h>
     38      1.1   bouyer #include <sys/event.h>
     39      1.1   bouyer #include <sys/ioctl.h>
     40      1.1   bouyer #include <sys/malloc.h>
     41      1.1   bouyer #include <sys/queue.h>
     42      1.1   bouyer #include <sys/systm.h>
     43      1.1   bouyer #include <sys/mutex.h>
     44      1.1   bouyer #include <sys/proc.h>
     45      1.1   bouyer #include <sys/kauth.h>
     46      1.1   bouyer 
     47      1.1   bouyer #include <dev/biovar.h>
     48      1.1   bouyer 
     49      1.1   bouyer struct bio_mapping {
     50      1.1   bouyer 	LIST_ENTRY(bio_mapping) bm_link;
     51      1.1   bouyer 	struct device *bm_dev;
     52      1.1   bouyer 	int (*bm_ioctl)(struct device *, u_long, void *);
     53      1.1   bouyer };
     54      1.1   bouyer 
     55  1.2.2.2      mjf static LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
     56      1.1   bouyer static kmutex_t bio_lock;
     57  1.2.2.1      mjf static bool bio_lock_initialized = false;
     58      1.1   bouyer 
     59  1.2.2.2      mjf static void	bio_initialize(void);
     60  1.2.2.2      mjf static int	bioclose(dev_t, int, int, struct lwp *);
     61  1.2.2.2      mjf static int	bioioctl(dev_t, u_long, void *, int, struct lwp *);
     62  1.2.2.2      mjf static int	bioopen(dev_t, int, int, struct lwp *);
     63  1.2.2.2      mjf 
     64  1.2.2.2      mjf static int	bio_delegate_ioctl(void *, u_long, void *);
     65  1.2.2.2      mjf static struct	bio_mapping *bio_lookup(char *);
     66  1.2.2.2      mjf static int	bio_validate(void *);
     67  1.2.2.2      mjf 
     68      1.1   bouyer void	bioattach(int);
     69      1.1   bouyer 
     70      1.1   bouyer const struct cdevsw bio_cdevsw = {
     71      1.1   bouyer         bioopen, bioclose, noread, nowrite, bioioctl,
     72      1.1   bouyer         nostop, notty, nopoll, nommap, nokqfilter, 0
     73      1.1   bouyer };
     74      1.1   bouyer 
     75      1.1   bouyer 
     76  1.2.2.2      mjf static void
     77  1.2.2.1      mjf bio_initialize(void)
     78  1.2.2.1      mjf {
     79  1.2.2.1      mjf 	if (bio_lock_initialized)
     80  1.2.2.1      mjf 		return;
     81  1.2.2.1      mjf 
     82  1.2.2.1      mjf 	mutex_init(&bio_lock, MUTEX_DEFAULT, IPL_VM);
     83  1.2.2.1      mjf 	bio_lock_initialized = true;
     84  1.2.2.1      mjf }
     85  1.2.2.1      mjf 
     86  1.2.2.1      mjf void
     87      1.1   bouyer bioattach(int nunits)
     88      1.1   bouyer {
     89  1.2.2.1      mjf 	if (!bio_lock_initialized)
     90  1.2.2.1      mjf 		bio_initialize();
     91      1.1   bouyer }
     92      1.1   bouyer 
     93  1.2.2.2      mjf static int
     94      1.1   bouyer bioopen(dev_t dev, int flags, int mode, struct lwp *l)
     95      1.1   bouyer {
     96      1.2  xtraeme 	return 0;
     97      1.1   bouyer }
     98      1.1   bouyer 
     99  1.2.2.2      mjf static int
    100      1.1   bouyer bioclose(dev_t dev, int flags, int mode, struct lwp *l)
    101      1.1   bouyer {
    102      1.2  xtraeme 	return 0;
    103      1.1   bouyer }
    104      1.1   bouyer 
    105  1.2.2.2      mjf static int
    106      1.1   bouyer bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct  lwp *l)
    107      1.1   bouyer {
    108      1.1   bouyer 	struct bio_locate *locate;
    109      1.1   bouyer 	struct bio_common *common;
    110      1.1   bouyer 	char name[16];
    111      1.1   bouyer 	int error;
    112      1.1   bouyer 
    113      1.1   bouyer 	switch(cmd) {
    114      1.1   bouyer 	case BIOCLOCATE:
    115      1.1   bouyer 	case BIOCINQ:
    116      1.1   bouyer 	case BIOCDISK:
    117  1.2.2.2      mjf 	case BIOCDISK_NOVOL:
    118      1.1   bouyer 	case BIOCVOL:
    119  1.2.2.2      mjf #ifdef COMPAT_30
    120  1.2.2.2      mjf 	case OBIOCDISK:
    121  1.2.2.2      mjf 	case OBIOCVOL:
    122  1.2.2.2      mjf #endif
    123      1.1   bouyer 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    124      1.1   bouyer 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    125      1.1   bouyer 		if (error)
    126      1.2  xtraeme 			return error;
    127      1.1   bouyer 		break;
    128      1.1   bouyer 	case BIOCBLINK:
    129      1.1   bouyer 	case BIOCSETSTATE:
    130  1.2.2.2      mjf 	case BIOCVOLOPS:
    131      1.1   bouyer 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    132      1.1   bouyer 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    133      1.1   bouyer 		if (error)
    134      1.2  xtraeme 			return error;
    135      1.1   bouyer 		break;
    136      1.1   bouyer 	case BIOCALARM: {
    137      1.1   bouyer 		struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
    138      1.1   bouyer 		switch (alarm->ba_opcode) {
    139      1.1   bouyer 		case BIOC_SADISABLE:
    140      1.1   bouyer 		case BIOC_SAENABLE:
    141      1.1   bouyer 		case BIOC_SASILENCE:
    142      1.1   bouyer 		case BIOC_SATEST:
    143      1.1   bouyer 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    144      1.1   bouyer 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    145      1.1   bouyer 			if (error)
    146      1.2  xtraeme 				return error;
    147      1.1   bouyer 			break;
    148      1.1   bouyer 		case BIOC_GASTATUS:
    149      1.1   bouyer 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    150      1.1   bouyer 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    151      1.1   bouyer 			if (error)
    152      1.2  xtraeme 				return error;
    153      1.1   bouyer 			break;
    154      1.1   bouyer 		default:
    155      1.1   bouyer 			return EINVAL;
    156      1.1   bouyer 		}
    157      1.1   bouyer 		break;
    158      1.1   bouyer 	}
    159      1.1   bouyer 	default:
    160      1.1   bouyer 		return ENOTTY;
    161      1.1   bouyer 	}
    162      1.1   bouyer 
    163      1.1   bouyer 	switch (cmd) {
    164      1.1   bouyer 	case BIOCLOCATE:
    165  1.2.2.2      mjf 		locate = addr;
    166  1.2.2.2      mjf 		error = copyinstr(locate->bl_name, name, sizeof(name), NULL);
    167      1.1   bouyer 		if (error != 0)
    168      1.2  xtraeme 			return error;
    169      1.1   bouyer 		locate->bl_cookie = bio_lookup(name);
    170      1.1   bouyer 		if (locate->bl_cookie == NULL)
    171      1.2  xtraeme 			return ENOENT;
    172      1.1   bouyer 		break;
    173      1.1   bouyer 
    174      1.1   bouyer 	default:
    175  1.2.2.2      mjf 		common = addr;
    176      1.1   bouyer 		mutex_enter(&bio_lock);
    177      1.1   bouyer 		if (!bio_validate(common->bc_cookie)) {
    178      1.1   bouyer 			mutex_exit(&bio_lock);
    179      1.2  xtraeme 			return ENOENT;
    180      1.1   bouyer 		}
    181      1.1   bouyer 		mutex_exit(&bio_lock);
    182  1.2.2.2      mjf #ifdef COMPAT_30
    183  1.2.2.2      mjf 		switch (cmd) {
    184  1.2.2.2      mjf 		case OBIOCDISK: {
    185  1.2.2.2      mjf 			struct bioc_disk *bd =
    186  1.2.2.2      mjf 			    malloc(sizeof(*bd), M_DEVBUF, M_WAITOK|M_ZERO);
    187  1.2.2.2      mjf 
    188  1.2.2.2      mjf 			(void)memcpy(bd, addr, sizeof(struct obioc_disk));
    189  1.2.2.2      mjf 			error = bio_delegate_ioctl(common->bc_cookie,
    190  1.2.2.2      mjf 			    BIOCDISK, bd);
    191  1.2.2.2      mjf 			if (error) {
    192  1.2.2.2      mjf 				free(bd, M_DEVBUF);
    193  1.2.2.2      mjf 				return error;
    194  1.2.2.2      mjf 			}
    195  1.2.2.2      mjf 
    196  1.2.2.2      mjf 			(void)memcpy(addr, bd, sizeof(struct obioc_disk));
    197  1.2.2.2      mjf 			free(bd, M_DEVBUF);
    198  1.2.2.2      mjf 			return 0;
    199  1.2.2.2      mjf 		}
    200  1.2.2.2      mjf 		case OBIOCVOL: {
    201  1.2.2.2      mjf 			struct bioc_vol *bv =
    202  1.2.2.2      mjf 			    malloc(sizeof(*bv), M_DEVBUF, M_WAITOK|M_ZERO);
    203  1.2.2.2      mjf 
    204  1.2.2.2      mjf 			(void)memcpy(bv, addr, sizeof(struct obioc_vol));
    205  1.2.2.2      mjf 			error = bio_delegate_ioctl(common->bc_cookie,
    206  1.2.2.2      mjf 			    BIOCVOL, bv);
    207  1.2.2.2      mjf 			if (error) {
    208  1.2.2.2      mjf 				free(bv, M_DEVBUF);
    209  1.2.2.2      mjf 				return error;
    210  1.2.2.2      mjf 			}
    211  1.2.2.2      mjf 
    212  1.2.2.2      mjf 			(void)memcpy(addr, bv, sizeof(struct obioc_vol));
    213  1.2.2.2      mjf 			free(bv, M_DEVBUF);
    214  1.2.2.2      mjf 			return 0;
    215  1.2.2.2      mjf 		}
    216  1.2.2.2      mjf 		}
    217  1.2.2.2      mjf #endif
    218  1.2.2.2      mjf 		error = bio_delegate_ioctl(common->bc_cookie, cmd, addr);
    219      1.2  xtraeme 		return error;
    220      1.1   bouyer 	}
    221      1.2  xtraeme 	return 0;
    222      1.1   bouyer }
    223      1.1   bouyer 
    224      1.1   bouyer int
    225      1.1   bouyer bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, void *))
    226      1.1   bouyer {
    227      1.1   bouyer 	struct bio_mapping *bm;
    228      1.1   bouyer 
    229  1.2.2.1      mjf 	if (!bio_lock_initialized)
    230  1.2.2.1      mjf 		bio_initialize();
    231  1.2.2.1      mjf 
    232  1.2.2.2      mjf 	bm = malloc(sizeof(*bm), M_DEVBUF, M_NOWAIT|M_ZERO);
    233      1.1   bouyer 	if (bm == NULL)
    234      1.2  xtraeme 		return ENOMEM;
    235      1.1   bouyer 	bm->bm_dev = dev;
    236      1.1   bouyer 	bm->bm_ioctl = ioctl;
    237      1.1   bouyer 	mutex_enter(&bio_lock);
    238      1.1   bouyer 	LIST_INSERT_HEAD(&bios, bm, bm_link);
    239      1.1   bouyer 	mutex_exit(&bio_lock);
    240      1.2  xtraeme 	return 0;
    241      1.1   bouyer }
    242      1.1   bouyer 
    243      1.1   bouyer void
    244      1.1   bouyer bio_unregister(struct device *dev)
    245      1.1   bouyer {
    246      1.1   bouyer 	struct bio_mapping *bm, *next;
    247      1.1   bouyer 
    248      1.1   bouyer 	mutex_enter(&bio_lock);
    249      1.1   bouyer 	for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
    250      1.1   bouyer 		next = LIST_NEXT(bm, bm_link);
    251      1.1   bouyer 
    252      1.1   bouyer 		if (dev == bm->bm_dev) {
    253      1.1   bouyer 			LIST_REMOVE(bm, bm_link);
    254      1.1   bouyer 			free(bm, M_DEVBUF);
    255      1.1   bouyer 		}
    256      1.1   bouyer 	}
    257      1.1   bouyer 	mutex_exit(&bio_lock);
    258      1.1   bouyer }
    259      1.1   bouyer 
    260  1.2.2.2      mjf static struct bio_mapping *
    261      1.1   bouyer bio_lookup(char *name)
    262      1.1   bouyer {
    263      1.1   bouyer 	struct bio_mapping *bm;
    264      1.1   bouyer 
    265      1.1   bouyer 	mutex_enter(&bio_lock);
    266      1.1   bouyer 	LIST_FOREACH(bm, &bios, bm_link) {
    267      1.1   bouyer 		if (strcmp(name, bm->bm_dev->dv_xname) == 0) {
    268      1.1   bouyer 			mutex_exit(&bio_lock);
    269      1.2  xtraeme 			return bm;
    270      1.1   bouyer 		}
    271      1.1   bouyer 	}
    272      1.1   bouyer 	mutex_exit(&bio_lock);
    273      1.2  xtraeme 	return NULL;
    274      1.1   bouyer }
    275      1.1   bouyer 
    276  1.2.2.2      mjf static int
    277      1.1   bouyer bio_validate(void *cookie)
    278      1.1   bouyer {
    279      1.1   bouyer 	struct bio_mapping *bm;
    280      1.1   bouyer 
    281      1.2  xtraeme 	LIST_FOREACH(bm, &bios, bm_link)
    282      1.2  xtraeme 		if (bm == cookie)
    283      1.2  xtraeme 			return 1;
    284      1.2  xtraeme 
    285      1.2  xtraeme 	return 0;
    286      1.1   bouyer }
    287      1.1   bouyer 
    288  1.2.2.2      mjf static int
    289  1.2.2.2      mjf bio_delegate_ioctl(void *cookie, u_long cmd, void *addr)
    290      1.1   bouyer {
    291  1.2.2.2      mjf 	struct bio_mapping *bm = cookie;
    292      1.1   bouyer 
    293      1.2  xtraeme 	return bm->bm_ioctl(bm->bm_dev, cmd, addr);
    294      1.1   bouyer }
    295