1 /* $OpenBSD: bio.c,v 1.17 2015/08/26 22:28:57 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Niklas Hallqvist. All rights reserved. 5 * Copyright (c) 2012 Joel Sing <jsing@openbsd.org>. 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/param.h> 31 #include <sys/device.h> 32 #include <sys/ioctl.h> 33 #include <sys/malloc.h> 34 #include <sys/queue.h> 35 #include <sys/systm.h> 36 37 #include <dev/biovar.h> 38 39 struct bio_mapping { 40 LIST_ENTRY(bio_mapping) bm_link; 41 struct device *bm_dev; 42 int (*bm_ioctl)(struct device *, u_long, caddr_t); 43 }; 44 45 LIST_HEAD(, bio_mapping) bios = LIST_HEAD_INITIALIZER(bios); 46 47 void bioattach(int); 48 int bioclose(dev_t, int, int, struct proc *); 49 int bioioctl(dev_t, u_long, caddr_t, int, struct proc *); 50 int bioopen(dev_t, int, int, struct proc *); 51 52 int bio_delegate_ioctl(struct bio_mapping *, u_long, caddr_t); 53 struct bio_mapping *bio_lookup(char *); 54 int bio_validate(void *); 55 56 void 57 bioattach(int nunits) 58 { 59 } 60 61 int 62 bioopen(dev_t dev, int flags, int mode, struct proc *p) 63 { 64 return (0); 65 } 66 67 int 68 bioclose(dev_t dev, int flags, int mode, struct proc *p) 69 { 70 return (0); 71 } 72 73 int 74 bioioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 75 { 76 struct bio_locate *locate; 77 struct bio *bio; 78 char name[16]; 79 int error; 80 81 switch (cmd) { 82 case BIOCLOCATE: 83 locate = (struct bio_locate *)addr; 84 error = copyinstr(locate->bl_name, name, sizeof name, NULL); 85 if (error != 0) 86 return (error); 87 locate->bl_bio.bio_cookie = bio_lookup(name); 88 if (locate->bl_bio.bio_cookie == NULL) 89 return (ENOENT); 90 break; 91 92 case BIOCINQ: 93 case BIOCDISK: 94 case BIOCVOL: 95 case BIOCALARM: 96 case BIOCBLINK: 97 case BIOCSETSTATE: 98 case BIOCCREATERAID: 99 case BIOCDELETERAID: 100 case BIOCDISCIPLINE: 101 case BIOCPATROL: 102 bio = (struct bio *)addr; 103 if (!bio_validate(bio->bio_cookie)) 104 return (ENOENT); 105 return (bio_delegate_ioctl( 106 (struct bio_mapping *)bio->bio_cookie, cmd, addr)); 107 108 default: 109 return (ENXIO); 110 } 111 return (0); 112 } 113 114 int 115 bio_register(struct device *dev, int (*ioctl)(struct device *, u_long, caddr_t)) 116 { 117 struct bio_mapping *bm; 118 119 bm = malloc(sizeof *bm, M_DEVBUF, M_NOWAIT); 120 if (bm == NULL) 121 return (ENOMEM); 122 bm->bm_dev = dev; 123 bm->bm_ioctl = ioctl; 124 LIST_INSERT_HEAD(&bios, bm, bm_link); 125 return (0); 126 } 127 128 void 129 bio_unregister(struct device *dev) 130 { 131 struct bio_mapping *bm, *next; 132 133 for (bm = LIST_FIRST(&bios); bm != NULL; bm = next) { 134 next = LIST_NEXT(bm, bm_link); 135 136 if (dev == bm->bm_dev) { 137 LIST_REMOVE(bm, bm_link); 138 free(bm, M_DEVBUF, sizeof(*bm)); 139 } 140 } 141 } 142 143 struct bio_mapping * 144 bio_lookup(char *name) 145 { 146 struct bio_mapping *bm; 147 148 LIST_FOREACH(bm, &bios, bm_link) 149 if (strcmp(name, bm->bm_dev->dv_xname) == 0) 150 return (bm); 151 return (NULL); 152 } 153 154 int 155 bio_validate(void *cookie) 156 { 157 struct bio_mapping *bm; 158 159 LIST_FOREACH(bm, &bios, bm_link) 160 if (bm == cookie) 161 return (1); 162 return (0); 163 } 164 165 int 166 bio_delegate_ioctl(struct bio_mapping *bm, u_long cmd, caddr_t addr) 167 { 168 return (bm->bm_ioctl(bm->bm_dev, cmd, addr)); 169 } 170 171 void 172 bio_info(struct bio_status *bs, int print, const char *fmt, ...) 173 { 174 va_list ap; 175 176 va_start(ap, fmt); 177 bio_status(bs, print, BIO_MSG_INFO, fmt, &ap); 178 va_end(ap); 179 } 180 181 void 182 bio_warn(struct bio_status *bs, int print, const char *fmt, ...) 183 { 184 va_list ap; 185 186 va_start(ap, fmt); 187 bio_status(bs, print, BIO_MSG_WARN, fmt, &ap); 188 va_end(ap); 189 } 190 191 void 192 bio_error(struct bio_status *bs, int print, const char *fmt, ...) 193 { 194 va_list ap; 195 196 va_start(ap, fmt); 197 bio_status(bs, print, BIO_MSG_ERROR, fmt, &ap); 198 va_end(ap); 199 } 200 201 void 202 bio_status_init(struct bio_status *bs, struct device *dv) 203 { 204 bzero(bs, sizeof(struct bio_status)); 205 206 bs->bs_status = BIO_STATUS_UNKNOWN; 207 208 strlcpy(bs->bs_controller, dv->dv_xname, sizeof(bs->bs_controller)); 209 } 210 211 void 212 bio_status(struct bio_status *bs, int print, int msg_type, const char *fmt, 213 va_list *ap) 214 { 215 int idx; 216 217 if (bs->bs_msg_count >= BIO_MSG_COUNT) { 218 printf("%s: insufficient message buffers\n", bs->bs_controller); 219 return; 220 } 221 222 idx = bs->bs_msg_count++; 223 224 bs->bs_msgs[idx].bm_type = msg_type; 225 vsnprintf(bs->bs_msgs[idx].bm_msg, BIO_MSG_LEN, fmt, *ap); 226 227 if (print) 228 printf("%s: %s\n", bs->bs_controller, bs->bs_msgs[idx].bm_msg); 229 } 230