1 /*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD$ 36 */ 37 38 #include "opt_geom.h" 39 #ifndef NO_GEOM 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/sysctl.h> 45 #include <sys/bio.h> 46 #include <sys/conf.h> 47 #include <sys/disk.h> 48 #include <sys/disklabel.h> 49 #include <sys/malloc.h> 50 #include <sys/sysctl.h> 51 #include <sys/stdint.h> 52 #include <machine/md_var.h> 53 54 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <geom/geom.h> 58 59 static g_access_t g_disk_access; 60 61 struct g_class g_disk_class = { 62 "DISK", 63 NULL, 64 NULL, 65 G_CLASS_INITIALIZER 66 }; 67 68 DECLARE_GEOM_CLASS(g_disk_class, g_disk); 69 70 static int 71 g_disk_access(struct g_provider *pp, int r, int w, int e) 72 { 73 struct disk *dp; 74 dev_t dev; 75 int error; 76 77 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 78 pp->name, r, w, e); 79 g_topology_assert(); 80 r += pp->acr; 81 w += pp->acw; 82 e += pp->ace; 83 dp = pp->geom->softc; 84 dev = dp->d_dev; 85 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 86 mtx_lock(&Giant); 87 error = devsw(dev)->d_open(dev, 3, 0, NULL); 88 if (error != 0) 89 printf("Opened disk %s -> %d\n", pp->name, error); 90 mtx_unlock(&Giant); 91 pp->mediasize = dp->d_mediasize; 92 pp->sectorsize = dp->d_sectorsize; 93 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 94 mtx_lock(&Giant); 95 error = devsw(dev)->d_close(dev, 3, 0, NULL); 96 if (error != 0) 97 printf("Closed disk %s -> %d\n", pp->name, error); 98 mtx_unlock(&Giant); 99 } else { 100 error = 0; 101 } 102 return (error); 103 } 104 105 static void 106 g_disk_kerneldump(struct bio *bp, struct disk *dp) 107 { 108 int error; 109 struct g_kerneldump *gkd; 110 struct dumperinfo di; 111 struct g_geom *gp; 112 113 gkd = (struct g_kerneldump*)bp->bio_data; 114 gp = bp->bio_to->geom; 115 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 116 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 117 di.dumper = (dumper_t *)dp->d_devsw->d_dump; 118 di.priv = dp->d_dev; 119 di.blocksize = dp->d_sectorsize; 120 di.mediaoffset = gkd->offset; 121 di.mediasize = gkd->length; 122 error = set_dumper(&di); 123 g_io_deliver(bp, error); 124 } 125 126 static void 127 g_disk_done(struct bio *bp) 128 { 129 130 mtx_unlock(&Giant); 131 bp->bio_completed = bp->bio_length - bp->bio_resid; 132 g_std_done(bp); 133 mtx_lock(&Giant); 134 } 135 136 static void 137 g_disk_start(struct bio *bp) 138 { 139 struct bio *bp2; 140 dev_t dev; 141 struct disk *dp; 142 struct g_ioctl *gio; 143 int error; 144 145 dp = bp->bio_to->geom->softc; 146 dev = dp->d_dev; 147 error = EJUSTRETURN; 148 switch(bp->bio_cmd) { 149 case BIO_DELETE: 150 if (!(devsw(dev)->d_flags & D_CANFREE)) { 151 error = 0; 152 break; 153 } 154 /* fall-through */ 155 case BIO_READ: 156 case BIO_WRITE: 157 bp2 = g_clone_bio(bp); 158 bp2->bio_done = g_disk_done; 159 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 160 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 161 bp2->bio_bcount = bp2->bio_length; 162 bp2->bio_dev = dev; 163 mtx_lock(&Giant); 164 devsw(dev)->d_strategy(bp2); 165 mtx_unlock(&Giant); 166 break; 167 case BIO_GETATTR: 168 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 169 break; 170 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 171 break; 172 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 173 break; 174 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 175 g_disk_kerneldump(bp, dp); 176 else if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 177 bp->bio_length == sizeof *gio) { 178 gio = (struct g_ioctl *)bp->bio_data; 179 gio->func = devsw(dp->d_dev)->d_ioctl; 180 gio->dev = dp->d_dev; 181 error = EDIRIOCTL; 182 } else 183 error = ENOIOCTL; 184 break; 185 case BIO_SETATTR: 186 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 187 bp->bio_length == sizeof *gio) { 188 gio = (struct g_ioctl *)bp->bio_data; 189 gio->func = devsw(dp->d_dev)->d_ioctl; 190 gio->dev = dp->d_dev; 191 error = EDIRIOCTL; 192 } else 193 error = ENOIOCTL; 194 break; 195 default: 196 error = EOPNOTSUPP; 197 break; 198 } 199 if (error != EJUSTRETURN) 200 g_io_deliver(bp, error); 201 return; 202 } 203 204 static void 205 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 206 { 207 struct disk *dp; 208 209 dp = gp->softc; 210 if (indent == NULL) { 211 sbuf_printf(sb, " hd %u", dp->d_fwheads); 212 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 213 return; 214 } 215 if (pp != NULL) { 216 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 217 indent, dp->d_fwheads); 218 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 219 indent, dp->d_fwsectors); 220 } 221 } 222 223 static void 224 g_disk_create(void *arg) 225 { 226 struct g_geom *gp; 227 struct g_provider *pp; 228 dev_t dev; 229 230 g_topology_assert(); 231 dev = arg; 232 gp = g_new_geomf(&g_disk_class, dev->si_name); 233 gp->start = g_disk_start; 234 gp->access = g_disk_access; 235 gp->softc = dev->si_disk; 236 gp->dumpconf = g_disk_dumpconf; 237 dev->si_disk->d_softc = gp; 238 pp = g_new_providerf(gp, "%s", gp->name); 239 pp->mediasize = dev->si_disk->d_mediasize; 240 pp->sectorsize = dev->si_disk->d_sectorsize; 241 g_error_provider(pp, 0); 242 if (bootverbose) 243 printf("GEOM: new disk %s\n", gp->name); 244 } 245 246 247 248 dev_t 249 disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, struct cdevsw *proto) 250 { 251 dev_t dev; 252 253 dev = g_malloc(sizeof *dev, M_ZERO); 254 dp->d_dev = dev; 255 dp->d_devsw = cdevsw; 256 dev->si_devsw = cdevsw; 257 dev->si_disk = dp; 258 dev->si_udev = dkmakeminor(unit, 1, RAW_PART); 259 sprintf(dev->si_name, "%s%d", cdevsw->d_name, unit); 260 g_call_me(g_disk_create, dev); 261 return (dev); 262 } 263 264 void 265 disk_destroy(dev_t dev) 266 { 267 struct disk *dp; 268 struct g_geom *gp; 269 270 dp = dev->si_disk; 271 gp = dp->d_softc; 272 g_free(dev); 273 gp->flags |= G_GEOM_WITHER; 274 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO); 275 } 276 277 void 278 disk_invalidate (struct disk *disk) 279 { 280 } 281 282 static void 283 g_kern_disks(void *p) 284 { 285 struct sbuf *sb; 286 struct g_geom *gp; 287 char *sp; 288 289 sb = p; 290 sp = ""; 291 g_topology_assert(); 292 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 293 sbuf_printf(sb, "%s%s", sp, gp->name); 294 sp = " "; 295 } 296 sbuf_finish(sb); 297 wakeup(sb); 298 } 299 300 static int 301 sysctl_disks(SYSCTL_HANDLER_ARGS) 302 { 303 int error; 304 struct sbuf *sb; 305 306 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 307 sbuf_clear(sb); 308 g_call_me(g_kern_disks, sb); 309 do { 310 tsleep(sb, PZERO, "kern.disks", hz); 311 } while(!sbuf_done(sb)); 312 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 313 sbuf_delete(sb); 314 return error; 315 } 316 317 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 318 sysctl_disks, "A", "names of available disks"); 319 320 #endif 321