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