Home | History | Annotate | Line # | Download | only in dev
bio.c revision 1.13.16.4
      1 /*	$NetBSD: bio.c,v 1.13.16.4 2018/09/18 23:03:54 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.4 2018/09/18 23:03:54 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 MODULE_CALL_HOOK_DECL(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 MODULE_CALL_HOOK(compat_bio_30_hook, f,
    122     (void * cookie, u_long cmd, void *addr, int(*ff)(void *, u_long, void *)),
    123     (cookie, cmd, addr, ff), enosys());
    124 
    125 static int
    126 bioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct  lwp *l)
    127 {
    128 	struct bio_locate *locate;
    129 	struct bio_common *common;
    130 	char name[16];
    131 	int error;
    132 
    133 	switch(cmd) {
    134 	case BIOCLOCATE:
    135 	case BIOCINQ:
    136 	case BIOCDISK:
    137 	case BIOCDISK_NOVOL:
    138 	case BIOCVOL:
    139 	case OBIOCDISK:
    140 	case OBIOCVOL:
    141 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    142 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    143 		if (error)
    144 			return error;
    145 		break;
    146 	case BIOCBLINK:
    147 	case BIOCSETSTATE:
    148 	case BIOCVOLOPS:
    149 		error = kauth_authorize_device_passthru(l->l_cred, dev,
    150 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    151 		if (error)
    152 			return error;
    153 		break;
    154 	case BIOCALARM: {
    155 		struct bioc_alarm *alarm = (struct bioc_alarm *)addr;
    156 		switch (alarm->ba_opcode) {
    157 		case BIOC_SADISABLE:
    158 		case BIOC_SAENABLE:
    159 		case BIOC_SASILENCE:
    160 		case BIOC_SATEST:
    161 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    162 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF, addr);
    163 			if (error)
    164 				return error;
    165 			break;
    166 		case BIOC_GASTATUS:
    167 			error = kauth_authorize_device_passthru(l->l_cred, dev,
    168 			    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF, addr);
    169 			if (error)
    170 				return error;
    171 			break;
    172 		default:
    173 			return EINVAL;
    174 		}
    175 		break;
    176 	}
    177 	default:
    178 		return ENOTTY;
    179 	}
    180 
    181 	switch (cmd) {
    182 	case BIOCLOCATE:
    183 		locate = addr;
    184 		error = copyinstr(locate->bl_name, name, sizeof(name), NULL);
    185 		if (error != 0)
    186 			return error;
    187 		locate->bl_cookie = bio_lookup(name);
    188 		if (locate->bl_cookie == NULL)
    189 			return ENOENT;
    190 		break;
    191 
    192 	default:
    193 		common = addr;
    194 		mutex_enter(&bio_lock);
    195 		if (!bio_validate(common->bc_cookie)) {
    196 			mutex_exit(&bio_lock);
    197 			return ENOENT;
    198 		}
    199 		mutex_exit(&bio_lock);
    200 		error = compat_bio_30_hook_f_call(common->bc_cookie, cmd, addr,
    201 		    bio_delegate_ioctl);
    202 		if (error == ENOSYS)
    203 			error = bio_delegate_ioctl(common->bc_cookie, cmd,
    204 			    addr);
    205 		return error;
    206 	}
    207 	return 0;
    208 }
    209 
    210 int
    211 bio_register(device_t dev, int (*ioctl)(device_t, u_long, void *))
    212 {
    213 	struct bio_mapping *bm;
    214 
    215 	if (!bio_lock_initialized)
    216 		bio_initialize();
    217 
    218 	bm = malloc(sizeof(*bm), M_DEVBUF, M_NOWAIT|M_ZERO);
    219 	if (bm == NULL)
    220 		return ENOMEM;
    221 	bm->bm_dev = dev;
    222 	bm->bm_ioctl = ioctl;
    223 	mutex_enter(&bio_lock);
    224 	LIST_INSERT_HEAD(&bios, bm, bm_link);
    225 	mutex_exit(&bio_lock);
    226 	return 0;
    227 }
    228 
    229 void
    230 bio_unregister(device_t dev)
    231 {
    232 	struct bio_mapping *bm, *next;
    233 
    234 	mutex_enter(&bio_lock);
    235 	for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) {
    236 		next = LIST_NEXT(bm, bm_link);
    237 
    238 		if (dev == bm->bm_dev) {
    239 			LIST_REMOVE(bm, bm_link);
    240 			free(bm, M_DEVBUF);
    241 		}
    242 	}
    243 	mutex_exit(&bio_lock);
    244 }
    245 
    246 static struct bio_mapping *
    247 bio_lookup(char *name)
    248 {
    249 	struct bio_mapping *bm;
    250 
    251 	mutex_enter(&bio_lock);
    252 	LIST_FOREACH(bm, &bios, bm_link) {
    253 		if (strcmp(name, device_xname(bm->bm_dev)) == 0) {
    254 			mutex_exit(&bio_lock);
    255 			return bm;
    256 		}
    257 	}
    258 	mutex_exit(&bio_lock);
    259 	return NULL;
    260 }
    261 
    262 static int
    263 bio_validate(void *cookie)
    264 {
    265 	struct bio_mapping *bm;
    266 
    267 	LIST_FOREACH(bm, &bios, bm_link)
    268 		if (bm == cookie)
    269 			return 1;
    270 
    271 	return 0;
    272 }
    273 
    274 static int
    275 bio_delegate_ioctl(void *cookie, u_long cmd, void *addr)
    276 {
    277 	struct bio_mapping *bm = cookie;
    278 
    279 	return bm->bm_ioctl(bm->bm_dev, cmd, addr);
    280 }
    281 
    282 void
    283 bio_disk_to_envsys(envsys_data_t *edata, const struct bioc_disk *bd)
    284 {
    285 	switch (bd->bd_status) {
    286 	case BIOC_SDONLINE:
    287 		edata->value_cur = ENVSYS_DRIVE_ONLINE;
    288 		edata->state = ENVSYS_SVALID;
    289 		break;
    290 	case BIOC_SDOFFLINE:
    291 		edata->value_cur = ENVSYS_DRIVE_OFFLINE;
    292 		edata->state = ENVSYS_SCRITICAL;
    293 		break;
    294 	default:
    295 		edata->value_cur = ENVSYS_DRIVE_FAIL;
    296 		edata->state = ENVSYS_SCRITICAL;
    297 		break;
    298 	}
    299 }
    300 
    301 void
    302 bio_vol_to_envsys(envsys_data_t *edata, const struct bioc_vol *bv)
    303 {
    304 	switch (bv->bv_status) {
    305 	case BIOC_SVOFFLINE:
    306 		edata->value_cur = ENVSYS_DRIVE_OFFLINE;
    307 		edata->state = ENVSYS_SCRITICAL;
    308 		break;
    309 	case BIOC_SVDEGRADED:
    310 		edata->value_cur = ENVSYS_DRIVE_PFAIL;
    311 		edata->state = ENVSYS_SCRITICAL;
    312 		break;
    313 	case BIOC_SVBUILDING:
    314 		edata->value_cur = ENVSYS_DRIVE_BUILD;
    315 		edata->state = ENVSYS_SVALID;
    316 		break;
    317 	case BIOC_SVMIGRATING:
    318 		edata->value_cur = ENVSYS_DRIVE_MIGRATING;
    319 		edata->state = ENVSYS_SVALID;
    320 		break;
    321 	case BIOC_SVCHECKING:
    322 		edata->value_cur = ENVSYS_DRIVE_CHECK;
    323 		edata->state = ENVSYS_SVALID;
    324 		break;
    325 	case BIOC_SVREBUILD:
    326 		edata->value_cur = ENVSYS_DRIVE_REBUILD;
    327 		edata->state = ENVSYS_SCRITICAL;
    328 		break;
    329 	case BIOC_SVSCRUB:
    330 	case BIOC_SVONLINE:
    331 		edata->value_cur = ENVSYS_DRIVE_ONLINE;
    332 		edata->state = ENVSYS_SVALID;
    333 		break;
    334 	case BIOC_SVINVALID:
    335 		/* FALLTHROUGH */
    336 	default:
    337 		edata->value_cur = ENVSYS_DRIVE_EMPTY; /* unknown state */
    338 		edata->state = ENVSYS_SINVALID;
    339 		break;
    340 	}
    341 }
    342