Home | History | Annotate | Line # | Download | only in dev
bio.c revision 1.13.16.2
      1 /*	$NetBSD: bio.c,v 1.13.16.2 2018/09/17 11:04:30 pgoyette 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.13.16.2 2018/09/17 11:04:30 pgoyette Exp $");
     32 
     33 #include "opt_compat_netbsd.h"
     34 
     35 #include <sys/param.h>
     36 #include <sys/conf.h>
     37 #include <sys/device.h>
     38 #include <sys/event.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/malloc.h>
     41 #include <sys/queue.h>
     42 #include <sys/systm.h>
     43 #include <sys/mutex.h>
     44 #include <sys/proc.h>
     45 #include <sys/kauth.h>
     46 #include <sys/compat_stub.h>
     47 
     48 #include <dev/biovar.h>
     49 #include <dev/sysmon/sysmonvar.h>
     50 
     51 #include "ioconf.h"
     52 
     53 struct bio_mapping {
     54 	LIST_ENTRY(bio_mapping) bm_link;
     55 	device_t bm_dev;
     56 	int (*bm_ioctl)(device_t, u_long, void *);
     57 };
     58 
     59 static LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios);
     60 static kmutex_t bio_lock;
     61 static bool bio_lock_initialized = false;
     62 
     63 static void	bio_initialize(void);
     64 static int	bioclose(dev_t, int, int, struct lwp *);
     65 static int	bioioctl(dev_t, u_long, void *, int, struct lwp *);
     66 static int	bioopen(dev_t, int, int, struct lwp *);
     67 
     68 static int	bio_delegate_ioctl(void *, u_long, void *);
     69 static struct	bio_mapping *bio_lookup(char *);
     70 static int	bio_validate(void *);
     71 
     72 const struct cdevsw bio_cdevsw = {
     73         .d_open = bioopen,
     74 	.d_close = bioclose,
     75 	.d_read = noread,
     76 	.d_write = nowrite,
     77 	.d_ioctl = bioioctl,
     78         .d_stop = nostop,
     79 	.d_tty = notty,
     80 	.d_poll = nopoll,
     81 	.d_mmap = nommap,
     82 	.d_kqfilter = nokqfilter,
     83 	.d_discard = nodiscard,
     84 	.d_flag = D_OTHER | D_MPSAFE
     85 };
     86 
     87 
     88 static void
     89 bio_initialize(void)
     90 {
     91 	if (bio_lock_initialized)
     92 		return;
     93 
     94 	mutex_init(&bio_lock, MUTEX_DEFAULT, IPL_VM);
     95 	bio_lock_initialized = true;
     96 }
     97 
     98 void
     99 bioattach(int nunits)
    100 {
    101 	if (!bio_lock_initialized)
    102 		bio_initialize();
    103 }
    104 
    105 static int
    106 bioopen(dev_t dev, int flags, int mode, struct lwp *l)
    107 {
    108 	return 0;
    109 }
    110 
    111 static int
    112 bioclose(dev_t dev, int flags, int mode, struct lwp *l)
    113 {
    114 	return 0;
    115 }
    116 
    117 /* Hook up the compat_bio_30 routine */
    118 COMPAT_CALL_HOOK(compat_bio_30_hook, f,
    119     (void * cookie, u_long cmd, void *addr, int(*ff)(void *, u_long, void *)),
    120     (cookie, cmd, addr, ff), enosys());
    121 
    122 static int
    123 bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct  lwp *l)
    124 {
    125 	struct bio_locate *locate;
    126 	struct bio_common *common;
    127 	char name[16];
    128 	int error;
    129 
    130 	switch(cmd) {
    131 	case BIOCLOCATE:
    132 	case BIOCINQ:
    133 	case BIOCDISK:
    134 	case BIOCDISK_NOVOL:
    135 	case BIOCVOL:
    136 	case OBIOCDISK:
    137 	case OBIOCVOL:
    138 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    139 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    140 		if (error)
    141 			return error;
    142 		break;
    143 	case BIOCBLINK:
    144 	case BIOCSETSTATE:
    145 	case BIOCVOLOPS:
    146 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    147 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    148 		if (error)
    149 			return error;
    150 		break;
    151 	case BIOCALARM: {
    152 		struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
    153 		switch (alarm->ba_opcode) {
    154 		case BIOC_SADISABLE:
    155 		case BIOC_SAENABLE:
    156 		case BIOC_SASILENCE:
    157 		case BIOC_SATEST:
    158 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    159 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    160 			if (error)
    161 				return error;
    162 			break;
    163 		case BIOC_GASTATUS:
    164 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    165 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    166 			if (error)
    167 				return error;
    168 			break;
    169 		default:
    170 			return EINVAL;
    171 		}
    172 		break;
    173 	}
    174 	default:
    175 		return ENOTTY;
    176 	}
    177 
    178 	switch (cmd) {
    179 	case BIOCLOCATE:
    180 		locate = addr;
    181 		error = copyinstr(locate->bl_name, name, sizeof(name), NULL);
    182 		if (error != 0)
    183 			return error;
    184 		locate->bl_cookie = bio_lookup(name);
    185 		if (locate->bl_cookie == NULL)
    186 			return ENOENT;
    187 		break;
    188 
    189 	default:
    190 		common = addr;
    191 		mutex_enter(&bio_lock);
    192 		if (!bio_validate(common->bc_cookie)) {
    193 			mutex_exit(&bio_lock);
    194 			return ENOENT;
    195 		}
    196 		mutex_exit(&bio_lock);
    197 		error = compat_bio_30_hook_f_call(common->bc_cookie, cmd, addr,
    198 		    bio_delegate_ioctl);
    199 		if (error == ENOSYS)
    200 			error = bio_delegate_ioctl(common->bc_cookie, cmd,
    201 			    addr);
    202 		return error;
    203 	}
    204 	return 0;
    205 }
    206 
    207 int
    208 bio_register(device_t dev, int (*ioctl)(device_t, u_long, void *))
    209 {
    210 	struct bio_mapping *bm;
    211 
    212 	if (!bio_lock_initialized)
    213 		bio_initialize();
    214 
    215 	bm = malloc(sizeof(*bm), M_DEVBUF, M_NOWAIT|M_ZERO);
    216 	if (bm == NULL)
    217 		return ENOMEM;
    218 	bm->bm_dev = dev;
    219 	bm->bm_ioctl = ioctl;
    220 	mutex_enter(&bio_lock);
    221 	LIST_INSERT_HEAD(&bios, bm, bm_link);
    222 	mutex_exit(&bio_lock);
    223 	return 0;
    224 }
    225 
    226 void
    227 bio_unregister(device_t dev)
    228 {
    229 	struct bio_mapping *bm, *next;
    230 
    231 	mutex_enter(&bio_lock);
    232 	for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
    233 		next = LIST_NEXT(bm, bm_link);
    234 
    235 		if (dev == bm->bm_dev) {
    236 			LIST_REMOVE(bm, bm_link);
    237 			free(bm, M_DEVBUF);
    238 		}
    239 	}
    240 	mutex_exit(&bio_lock);
    241 }
    242 
    243 static struct bio_mapping *
    244 bio_lookup(char *name)
    245 {
    246 	struct bio_mapping *bm;
    247 
    248 	mutex_enter(&bio_lock);
    249 	LIST_FOREACH(bm, &bios, bm_link) {
    250 		if (strcmp(name, device_xname(bm->bm_dev)) == 0) {
    251 			mutex_exit(&bio_lock);
    252 			return bm;
    253 		}
    254 	}
    255 	mutex_exit(&bio_lock);
    256 	return NULL;
    257 }
    258 
    259 static int
    260 bio_validate(void *cookie)
    261 {
    262 	struct bio_mapping *bm;
    263 
    264 	LIST_FOREACH(bm, &bios, bm_link)
    265 		if (bm == cookie)
    266 			return 1;
    267 
    268 	return 0;
    269 }
    270 
    271 static int
    272 bio_delegate_ioctl(void *cookie, u_long cmd, void *addr)
    273 {
    274 	struct bio_mapping *bm = cookie;
    275 
    276 	return bm->bm_ioctl(bm->bm_dev, cmd, addr);
    277 }
    278 
    279 void
    280 bio_disk_to_envsys(envsys_data_t *edata, const struct bioc_disk *bd)
    281 {
    282 	switch (bd->bd_status) {
    283 	case BIOC_SDONLINE:
    284 		edata->value_cur = ENVSYS_DRIVE_ONLINE;
    285 		edata->state = ENVSYS_SVALID;
    286 		break;
    287 	case BIOC_SDOFFLINE:
    288 		edata->value_cur = ENVSYS_DRIVE_OFFLINE;
    289 		edata->state = ENVSYS_SCRITICAL;
    290 		break;
    291 	default:
    292 		edata->value_cur = ENVSYS_DRIVE_FAIL;
    293 		edata->state = ENVSYS_SCRITICAL;
    294 		break;
    295 	}
    296 }
    297 
    298 void
    299 bio_vol_to_envsys(envsys_data_t *edata, const struct bioc_vol *bv)
    300 {
    301 	switch (bv->bv_status) {
    302 	case BIOC_SVOFFLINE:
    303 		edata->value_cur = ENVSYS_DRIVE_OFFLINE;
    304 		edata->state = ENVSYS_SCRITICAL;
    305 		break;
    306 	case BIOC_SVDEGRADED:
    307 		edata->value_cur = ENVSYS_DRIVE_PFAIL;
    308 		edata->state = ENVSYS_SCRITICAL;
    309 		break;
    310 	case BIOC_SVBUILDING:
    311 		edata->value_cur = ENVSYS_DRIVE_BUILD;
    312 		edata->state = ENVSYS_SVALID;
    313 		break;
    314 	case BIOC_SVMIGRATING:
    315 		edata->value_cur = ENVSYS_DRIVE_MIGRATING;
    316 		edata->state = ENVSYS_SVALID;
    317 		break;
    318 	case BIOC_SVCHECKING:
    319 		edata->value_cur = ENVSYS_DRIVE_CHECK;
    320 		edata->state = ENVSYS_SVALID;
    321 		break;
    322 	case BIOC_SVREBUILD:
    323 		edata->value_cur = ENVSYS_DRIVE_REBUILD;
    324 		edata->state = ENVSYS_SCRITICAL;
    325 		break;
    326 	case BIOC_SVSCRUB:
    327 	case BIOC_SVONLINE:
    328 		edata->value_cur = ENVSYS_DRIVE_ONLINE;
    329 		edata->state = ENVSYS_SVALID;
    330 		break;
    331 	case BIOC_SVINVALID:
    332 		/* FALLTHROUGH */
    333 	default:
    334 		edata->value_cur = ENVSYS_DRIVE_EMPTY; /* unknown state */
    335 		edata->state = ENVSYS_SINVALID;
    336 		break;
    337 	}
    338 }
    339