Home | History | Annotate | Line # | Download | only in dev
bio.c revision 1.1.14.1
      1  1.1.14.1  jmcneill /*	$NetBSD: bio.c,v 1.1.14.1 2007/11/04 21:03:21 jmcneill 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.1.14.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: bio.c,v 1.1.14.1 2007/11/04 21:03:21 jmcneill Exp $");
     32       1.1    bouyer 
     33       1.1    bouyer #include <sys/param.h>
     34       1.1    bouyer #include <sys/conf.h>
     35       1.1    bouyer #include <sys/device.h>
     36       1.1    bouyer #include <sys/event.h>
     37       1.1    bouyer #include <sys/ioctl.h>
     38       1.1    bouyer #include <sys/malloc.h>
     39       1.1    bouyer #include <sys/queue.h>
     40       1.1    bouyer #include <sys/systm.h>
     41       1.1    bouyer #include <sys/mutex.h>
     42       1.1    bouyer #include <sys/proc.h>
     43       1.1    bouyer #include <sys/kauth.h>
     44       1.1    bouyer 
     45       1.1    bouyer #include <dev/biovar.h>
     46       1.1    bouyer 
     47       1.1    bouyer struct bio_mapping {
     48       1.1    bouyer 	LIST_ENTRY(bio_mapping) bm_link;
     49       1.1    bouyer 	struct device *bm_dev;
     50       1.1    bouyer 	int (*bm_ioctl)(struct device *, u_long, void *);
     51       1.1    bouyer };
     52       1.1    bouyer 
     53       1.1    bouyer LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
     54       1.1    bouyer static kmutex_t bio_lock;
     55       1.1    bouyer 
     56       1.1    bouyer void	bioattach(int);
     57       1.1    bouyer int	bioclose(dev_t, int, int, struct lwp *);
     58       1.1    bouyer int	bioioctl(dev_t, u_long, void *, int, struct lwp *);
     59       1.1    bouyer int	bioopen(dev_t, int, int, struct lwp *);
     60       1.1    bouyer 
     61       1.1    bouyer int	bio_delegate_ioctl(struct bio_mapping *, u_long, void *);
     62       1.1    bouyer struct	bio_mapping *bio_lookup(char *);
     63       1.1    bouyer int	bio_validate(void *);
     64       1.1    bouyer 
     65       1.1    bouyer const struct cdevsw bio_cdevsw = {
     66       1.1    bouyer         bioopen, bioclose, noread, nowrite, bioioctl,
     67       1.1    bouyer         nostop, notty, nopoll, nommap, nokqfilter, 0
     68       1.1    bouyer };
     69       1.1    bouyer 
     70       1.1    bouyer 
     71       1.1    bouyer void
     72       1.1    bouyer bioattach(int nunits)
     73       1.1    bouyer {
     74       1.1    bouyer 	mutex_init(&bio_lock, MUTEX_DRIVER, IPL_BIO);
     75       1.1    bouyer }
     76       1.1    bouyer 
     77       1.1    bouyer int
     78       1.1    bouyer bioopen(dev_t dev, int flags, int mode, struct lwp *l)
     79       1.1    bouyer {
     80  1.1.14.1  jmcneill 	return 0;
     81       1.1    bouyer }
     82       1.1    bouyer 
     83       1.1    bouyer int
     84       1.1    bouyer bioclose(dev_t dev, int flags, int mode, struct lwp *l)
     85       1.1    bouyer {
     86  1.1.14.1  jmcneill 	return 0;
     87       1.1    bouyer }
     88       1.1    bouyer 
     89       1.1    bouyer int
     90       1.1    bouyer bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct  lwp *l)
     91       1.1    bouyer {
     92       1.1    bouyer 	struct bio_locate *locate;
     93       1.1    bouyer 	struct bio_common *common;
     94       1.1    bouyer 	char name[16];
     95       1.1    bouyer 	int error;
     96       1.1    bouyer 
     97       1.1    bouyer 	switch(cmd) {
     98       1.1    bouyer 	case BIOCLOCATE:
     99       1.1    bouyer 	case BIOCINQ:
    100       1.1    bouyer 	case BIOCDISK:
    101       1.1    bouyer 	case BIOCVOL:
    102       1.1    bouyer 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    103       1.1    bouyer 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    104       1.1    bouyer 		if (error)
    105  1.1.14.1  jmcneill 			return error;
    106       1.1    bouyer 		break;
    107       1.1    bouyer 	case BIOCBLINK:
    108       1.1    bouyer 	case BIOCSETSTATE:
    109       1.1    bouyer 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    110       1.1    bouyer 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    111       1.1    bouyer 		if (error)
    112  1.1.14.1  jmcneill 			return error;
    113       1.1    bouyer 		break;
    114       1.1    bouyer 	case BIOCALARM: {
    115       1.1    bouyer 		struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
    116       1.1    bouyer 		switch (alarm->ba_opcode) {
    117       1.1    bouyer 		case BIOC_SADISABLE:
    118       1.1    bouyer 		case BIOC_SAENABLE:
    119       1.1    bouyer 		case BIOC_SASILENCE:
    120       1.1    bouyer 		case BIOC_SATEST:
    121       1.1    bouyer 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    122       1.1    bouyer 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    123       1.1    bouyer 			if (error)
    124  1.1.14.1  jmcneill 				return error;
    125       1.1    bouyer 			break;
    126       1.1    bouyer 		case BIOC_GASTATUS:
    127       1.1    bouyer 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    128       1.1    bouyer 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    129       1.1    bouyer 			if (error)
    130  1.1.14.1  jmcneill 				return error;
    131       1.1    bouyer 			break;
    132       1.1    bouyer 		default:
    133       1.1    bouyer 			return EINVAL;
    134       1.1    bouyer 		}
    135       1.1    bouyer 		break;
    136       1.1    bouyer 	}
    137       1.1    bouyer 	default:
    138       1.1    bouyer 		return ENOTTY;
    139       1.1    bouyer 	}
    140       1.1    bouyer 
    141       1.1    bouyer 	switch (cmd) {
    142       1.1    bouyer 	case BIOCLOCATE:
    143       1.1    bouyer 		locate = (struct bio_locate *)addr;
    144       1.1    bouyer 		error = copyinstr(locate->bl_name, name, 16, NULL);
    145       1.1    bouyer 		if (error != 0)
    146  1.1.14.1  jmcneill 			return error;
    147       1.1    bouyer 		locate->bl_cookie = bio_lookup(name);
    148       1.1    bouyer 		if (locate->bl_cookie == NULL)
    149  1.1.14.1  jmcneill 			return ENOENT;
    150       1.1    bouyer 		break;
    151       1.1    bouyer 
    152       1.1    bouyer 	default:
    153       1.1    bouyer 		common = (struct bio_common *)addr;
    154       1.1    bouyer 		mutex_enter(&bio_lock);
    155       1.1    bouyer 		if (!bio_validate(common->bc_cookie)) {
    156       1.1    bouyer 			mutex_exit(&bio_lock);
    157  1.1.14.1  jmcneill 			return ENOENT;
    158       1.1    bouyer 		}
    159       1.1    bouyer 		mutex_exit(&bio_lock);
    160  1.1.14.1  jmcneill 		error = bio_delegate_ioctl(
    161       1.1    bouyer 		    (struct bio_mapping *)common->bc_cookie, cmd, addr);
    162  1.1.14.1  jmcneill 		return error;
    163       1.1    bouyer 	}
    164  1.1.14.1  jmcneill 	return 0;
    165       1.1    bouyer }
    166       1.1    bouyer 
    167       1.1    bouyer int
    168       1.1    bouyer bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, void *))
    169       1.1    bouyer {
    170       1.1    bouyer 	struct bio_mapping *bm;
    171       1.1    bouyer 
    172  1.1.14.1  jmcneill 	bm = (struct bio_mapping *)malloc(sizeof(*bm), M_DEVBUF,
    173  1.1.14.1  jmcneill 	    M_NOWAIT|M_ZERO);
    174       1.1    bouyer 	if (bm == NULL)
    175  1.1.14.1  jmcneill 		return ENOMEM;
    176       1.1    bouyer 	bm->bm_dev = dev;
    177       1.1    bouyer 	bm->bm_ioctl = ioctl;
    178       1.1    bouyer 	mutex_enter(&bio_lock);
    179       1.1    bouyer 	LIST_INSERT_HEAD(&bios, bm, bm_link);
    180       1.1    bouyer 	mutex_exit(&bio_lock);
    181  1.1.14.1  jmcneill 	return 0;
    182       1.1    bouyer }
    183       1.1    bouyer 
    184       1.1    bouyer void
    185       1.1    bouyer bio_unregister(struct device *dev)
    186       1.1    bouyer {
    187       1.1    bouyer 	struct bio_mapping *bm, *next;
    188       1.1    bouyer 
    189       1.1    bouyer 	mutex_enter(&bio_lock);
    190       1.1    bouyer 	for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
    191       1.1    bouyer 		next = LIST_NEXT(bm, bm_link);
    192       1.1    bouyer 
    193       1.1    bouyer 		if (dev == bm->bm_dev) {
    194       1.1    bouyer 			LIST_REMOVE(bm, bm_link);
    195       1.1    bouyer 			free(bm, M_DEVBUF);
    196       1.1    bouyer 		}
    197       1.1    bouyer 	}
    198       1.1    bouyer 	mutex_exit(&bio_lock);
    199       1.1    bouyer }
    200       1.1    bouyer 
    201       1.1    bouyer struct bio_mapping *
    202       1.1    bouyer bio_lookup(char *name)
    203       1.1    bouyer {
    204       1.1    bouyer 	struct bio_mapping *bm;
    205       1.1    bouyer 
    206       1.1    bouyer 	mutex_enter(&bio_lock);
    207       1.1    bouyer 	LIST_FOREACH(bm, &bios, bm_link) {
    208       1.1    bouyer 		if (strcmp(name, bm->bm_dev->dv_xname) == 0) {
    209       1.1    bouyer 			mutex_exit(&bio_lock);
    210  1.1.14.1  jmcneill 			return bm;
    211       1.1    bouyer 		}
    212       1.1    bouyer 	}
    213       1.1    bouyer 	mutex_exit(&bio_lock);
    214  1.1.14.1  jmcneill 	return NULL;
    215       1.1    bouyer }
    216       1.1    bouyer 
    217       1.1    bouyer int
    218       1.1    bouyer bio_validate(void *cookie)
    219       1.1    bouyer {
    220       1.1    bouyer 	struct bio_mapping *bm;
    221       1.1    bouyer 
    222  1.1.14.1  jmcneill 	LIST_FOREACH(bm, &bios, bm_link)
    223  1.1.14.1  jmcneill 		if (bm == cookie)
    224  1.1.14.1  jmcneill 			return 1;
    225  1.1.14.1  jmcneill 
    226  1.1.14.1  jmcneill 	return 0;
    227       1.1    bouyer }
    228       1.1    bouyer 
    229       1.1    bouyer int
    230       1.1    bouyer bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, void *addr)
    231       1.1    bouyer {
    232       1.1    bouyer 
    233  1.1.14.1  jmcneill 	return bm->bm_ioctl(bm->bm_dev, cmd, addr);
    234       1.1    bouyer }
    235