geom_dev.c revision 119593
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 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: head/sys/geom/geom_dev.c 119593 2003-08-30 18:33:55Z phk $"); 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/malloc.h> 42#include <sys/kernel.h> 43#include <sys/conf.h> 44#include <sys/bio.h> 45#include <sys/lock.h> 46#include <sys/mutex.h> 47#include <sys/errno.h> 48#include <sys/time.h> 49#include <sys/disk.h> 50#include <sys/fcntl.h> 51#include <sys/limits.h> 52#include <geom/geom.h> 53#include <geom/geom_int.h> 54 55static d_open_t g_dev_open; 56static d_close_t g_dev_close; 57static d_strategy_t g_dev_strategy; 58static d_ioctl_t g_dev_ioctl; 59 60static struct cdevsw g_dev_cdevsw = { 61 .d_open = g_dev_open, 62 .d_close = g_dev_close, 63 .d_read = physread, 64 .d_write = physwrite, 65 .d_ioctl = g_dev_ioctl, 66 .d_strategy = g_dev_strategy, 67 .d_name = "g_dev", 68 .d_maj = GEOM_MAJOR, 69 .d_flags = D_DISK | D_TRACKCLOSE | D_NOGIANT, 70}; 71 72static g_taste_t g_dev_taste; 73static g_orphan_t g_dev_orphan; 74 75static struct g_class g_dev_class = { 76 .name = "DEV", 77 .taste = g_dev_taste, 78}; 79 80void 81g_dev_print(void) 82{ 83 struct g_geom *gp; 84 char const *p = ""; 85 86 LIST_FOREACH(gp, &g_dev_class.geom, geom) { 87 printf("%s%s", p, gp->name); 88 p = " "; 89 } 90 printf("\n"); 91} 92 93/* 94 * XXX: This is disgusting and wrong in every way imaginable: The only reason 95 * XXX: we have a clone function is because of the root-mount hack we currently 96 * XXX: employ. An improvment would be to unregister this cloner once we know 97 * XXX: we no longer need it. Ideally, root-fs would be mounted through DEVFS 98 * XXX: eliminating the need for this hack. 99 */ 100static void 101g_dev_clone(void *arg __unused, char *name, int namelen __unused, dev_t *dev) 102{ 103 struct g_geom *gp; 104 105 if (*dev != NODEV) 106 return; 107 108 g_waitidle(); 109 110 /* g_topology_lock(); */ 111 LIST_FOREACH(gp, &g_dev_class.geom, geom) { 112 if (strcmp(gp->name, name)) 113 continue; 114 *dev = gp->softc; 115 g_trace(G_T_TOPOLOGY, "g_dev_clone(%s) = %p", name, *dev); 116 return; 117 } 118 /* g_topology_unlock(); */ 119 return; 120} 121 122static void 123g_dev_register_cloner(void *foo __unused) 124{ 125 static int once; 126 127 /* XXX: why would this happen more than once ?? */ 128 if (!once) { 129 EVENTHANDLER_REGISTER(dev_clone, g_dev_clone, 0, 1000); 130 once++; 131 } 132} 133 134SYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL); 135 136struct g_provider * 137g_dev_getprovider(dev_t dev) 138{ 139 struct g_consumer *cp; 140 141 if (dev == NULL) 142 return (NULL); 143 if (devsw(dev) != &g_dev_cdevsw) 144 return (NULL); 145 cp = dev->si_drv2; 146 return (cp->provider); 147} 148 149 150static struct g_geom * 151g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 152{ 153 struct g_geom *gp; 154 struct g_consumer *cp; 155 static int unit = GEOM_MINOR_PROVIDERS; 156 int error; 157 dev_t dev; 158 159 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 160 g_topology_assert(); 161 LIST_FOREACH(cp, &pp->consumers, consumers) 162 if (cp->geom->class == mp) 163 return (NULL); 164 gp = g_new_geomf(mp, pp->name); 165 gp->orphan = g_dev_orphan; 166 cp = g_new_consumer(gp); 167 error = g_attach(cp, pp); 168 KASSERT(error == 0, 169 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 170 /* 171 * XXX: I'm not 100% sure we can call make_dev(9) without Giant 172 * yet. Once we can, we don't need to drop topology here either. 173 */ 174 g_topology_unlock(); 175 mtx_lock(&Giant); 176 dev = make_dev(&g_dev_cdevsw, unit2minor(unit++), 177 UID_ROOT, GID_OPERATOR, 0640, gp->name); 178 if (pp->flags & G_PF_CANDELETE) 179 dev->si_flags |= SI_CANDELETE; 180 mtx_unlock(&Giant); 181 g_topology_lock(); 182 dev->si_iosize_max = MAXPHYS; 183 dev->si_stripesize = pp->stripesize; 184 dev->si_stripeoffset = pp->stripeoffset; 185 gp->softc = dev; 186 dev->si_drv1 = gp; 187 dev->si_drv2 = cp; 188 return (gp); 189} 190 191static int 192g_dev_open(dev_t dev, int flags, int fmt, struct thread *td) 193{ 194 struct g_geom *gp; 195 struct g_consumer *cp; 196 int error, r, w, e; 197 198 gp = dev->si_drv1; 199 cp = dev->si_drv2; 200 if (gp == NULL || cp == NULL || gp->softc != dev) 201 return(ENXIO); /* g_dev_taste() not done yet */ 202 203 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 204 gp->name, flags, fmt, td); 205 r = flags & FREAD ? 1 : 0; 206 w = flags & FWRITE ? 1 : 0; 207#ifdef notyet 208 e = flags & O_EXCL ? 1 : 0; 209#else 210 e = 0; 211#endif 212 g_topology_lock(); 213 if (dev->si_devsw == NULL) 214 error = ENXIO; /* We were orphaned */ 215 else 216 error = g_access_rel(cp, r, w, e); 217 g_topology_unlock(); 218 g_waitidle(); 219 if (!error) 220 dev->si_bsize_phys = cp->provider->sectorsize; 221 return(error); 222} 223 224static int 225g_dev_close(dev_t dev, int flags, int fmt, struct thread *td) 226{ 227 struct g_geom *gp; 228 struct g_consumer *cp; 229 int error, r, w, e, i; 230 231 gp = dev->si_drv1; 232 cp = dev->si_drv2; 233 if (gp == NULL || cp == NULL) 234 return(ENXIO); 235 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 236 gp->name, flags, fmt, td); 237 r = flags & FREAD ? -1 : 0; 238 w = flags & FWRITE ? -1 : 0; 239#ifdef notyet 240 e = flags & O_EXCL ? -1 : 0; 241#else 242 e = 0; 243#endif 244 g_topology_lock(); 245 if (dev->si_devsw == NULL) 246 error = ENXIO; /* We were orphaned */ 247 else 248 error = g_access_rel(cp, r, w, e); 249 for (i = 0; i < 10 * hz;) { 250 if (cp->acr != 0 || cp->acw != 0) 251 break; 252 if (cp->nstart == cp->nend) 253 break; 254 tsleep(&i, PRIBIO, "gdevwclose", hz / 10); 255 i += hz / 10; 256 } 257 if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) { 258 printf("WARNING: Final close of geom_dev(%s) %s %s", 259 gp->name, 260 "still has outstanding I/O after 10 seconds.", 261 "Completing close anyway, panic may happen later."); 262 } 263 g_topology_unlock(); 264 g_waitidle(); 265 return (error); 266} 267 268/* 269 * XXX: Until we have unmessed the ioctl situation, there is a race against 270 * XXX: a concurrent orphanization. We cannot close it by holding topology 271 * XXX: since that would prevent us from doing our job, and stalling events 272 * XXX: will break (actually: stall) the BSD disklabel hacks. 273 */ 274static int 275g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 276{ 277 struct g_geom *gp; 278 struct g_consumer *cp; 279 struct g_kerneldump kd; 280 int i, error; 281 u_int u; 282 struct g_ioctl *gio; 283 284 gp = dev->si_drv1; 285 cp = dev->si_drv2; 286 gio = NULL; 287 288 error = 0; 289 KASSERT(cp->acr || cp->acw, 290 ("Consumer with zero access count in g_dev_ioctl")); 291 292 gio = NULL; 293 i = IOCPARM_LEN(cmd); 294 switch (cmd) { 295 case DIOCGSECTORSIZE: 296 *(u_int *)data = cp->provider->sectorsize; 297 if (*(u_int *)data == 0) 298 error = ENOENT; 299 break; 300 case DIOCGMEDIASIZE: 301 *(off_t *)data = cp->provider->mediasize; 302 if (*(off_t *)data == 0) 303 error = ENOENT; 304 break; 305 case DIOCGFWSECTORS: 306 error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 307 if (error == 0 && *(u_int *)data == 0) 308 error = ENOENT; 309 break; 310 case DIOCGFWHEADS: 311 error = g_io_getattr("GEOM::fwheads", cp, &i, data); 312 if (error == 0 && *(u_int *)data == 0) 313 error = ENOENT; 314 break; 315 case DIOCGFRONTSTUFF: 316 error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 317 break; 318 case DIOCSKERNELDUMP: 319 u = *((u_int *)data); 320 if (!u) { 321 set_dumper(NULL); 322 error = 0; 323 break; 324 } 325 kd.offset = 0; 326 kd.length = OFF_MAX; 327 i = sizeof kd; 328 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 329 if (!error) 330 dev->si_flags |= SI_DUMPDEV; 331 break; 332 333 default: 334 gio = g_malloc(sizeof *gio, M_WAITOK | M_ZERO); 335 gio->cmd = cmd; 336 gio->data = data; 337 gio->fflag = fflag; 338 gio->td = td; 339 i = sizeof *gio; 340 /* 341 * We always issue ioctls as getattr since the direction of data 342 * movement in ioctl is no indication of the ioctl being a "set" 343 * or "get" type ioctl or if such simplistic terms even apply 344 */ 345 error = g_io_getattr("GEOM::ioctl", cp, &i, gio); 346 break; 347 } 348 349 if (error == EDIRIOCTL) { 350 KASSERT(gio != NULL, ("NULL gio but EDIRIOCTL")); 351 KASSERT(gio->func != NULL, ("NULL function but EDIRIOCTL")); 352 error = (gio->func)(gio->dev, cmd, data, fflag, td); 353 } 354 g_waitidle(); 355 if (gio != NULL && (error == EOPNOTSUPP || error == ENOIOCTL)) { 356 if (g_debugflags & G_T_TOPOLOGY) { 357 i = IOCGROUP(cmd); 358 printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name); 359 if (i > ' ' && i <= '~') 360 printf(" '%c'", (int)IOCGROUP(cmd)); 361 else 362 printf(" 0x%lx", IOCGROUP(cmd)); 363 printf("/%ld ", cmd & 0xff); 364 if (cmd & IOC_IN) 365 printf("I"); 366 if (cmd & IOC_OUT) 367 printf("O"); 368 printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd)); 369 } 370 error = ENOTTY; 371 } 372 if (gio != NULL) 373 g_free(gio); 374 return (error); 375} 376 377static void 378g_dev_done(struct bio *bp2) 379{ 380 struct bio *bp; 381 382 bp = bp2->bio_parent; 383 bp->bio_error = bp2->bio_error; 384 if (bp->bio_error != 0) { 385 g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 386 bp2, bp->bio_error); 387 bp->bio_flags |= BIO_ERROR; 388 } else { 389 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 390 bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 391 } 392 bp->bio_resid = bp->bio_bcount - bp2->bio_completed; 393 g_destroy_bio(bp2); 394 biodone(bp); 395} 396 397static void 398g_dev_strategy(struct bio *bp) 399{ 400 struct g_consumer *cp; 401 struct bio *bp2; 402 dev_t dev; 403 404 KASSERT(bp->bio_cmd == BIO_READ || 405 bp->bio_cmd == BIO_WRITE || 406 bp->bio_cmd == BIO_DELETE, 407 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 408 dev = bp->bio_dev; 409 cp = dev->si_drv2; 410 KASSERT(cp->acr || cp->acw, 411 ("Consumer with zero access count in g_dev_strategy")); 412 413 for (;;) { 414 /* 415 * XXX: This is not an ideal solution, but I belive it to 416 * XXX: deadlock safe, all things considered. 417 */ 418 bp2 = g_clone_bio(bp); 419 if (bp2 != NULL) 420 break; 421 tsleep(&bp, PRIBIO, "gdstrat", hz / 10); 422 } 423 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 424 bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT; 425 KASSERT(bp2->bio_offset >= 0, 426 ("Negative bio_offset (%jd) on bio %p", 427 (intmax_t)bp2->bio_offset, bp)); 428 bp2->bio_length = (off_t)bp->bio_bcount; 429 bp2->bio_done = g_dev_done; 430 g_trace(G_T_BIO, 431 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 432 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 433 bp2->bio_data, bp2->bio_cmd); 434 g_io_request(bp2, cp); 435 KASSERT(cp->acr || cp->acw, 436 ("g_dev_strategy raced with g_dev_close and lost")); 437 438} 439 440/* 441 * g_dev_orphan() 442 * 443 * Called from below when the provider orphaned us. 444 * - Clear any dump settings. 445 * - Destroy the dev_t to prevent any more request from coming in. The 446 * provider is already marked with an error, so anything which comes in 447 * in the interrim will be returned immediately. 448 * - Wait for any outstanding I/O to finish. 449 * - Set our access counts to zero, whatever they were. 450 * - Detach and self-destruct. 451 */ 452 453static void 454g_dev_orphan(struct g_consumer *cp) 455{ 456 struct g_geom *gp; 457 dev_t dev; 458 459 g_topology_assert(); 460 gp = cp->geom; 461 dev = gp->softc; 462 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name); 463 464 /* Reset any dump-area set on this device */ 465 if (dev->si_flags & SI_DUMPDEV) 466 set_dumper(NULL); 467 468 /* Destroy the dev_t so we get no more requests */ 469 destroy_dev(dev); 470 471 /* Wait for the cows to come home */ 472 while (cp->nstart != cp->nend) 473 msleep(&dev, NULL, PRIBIO, "gdevorphan", hz / 10); 474 475 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 476 g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace); 477 478 g_detach(cp); 479 g_destroy_consumer(cp); 480 g_destroy_geom(gp); 481} 482 483DECLARE_GEOM_CLASS(g_dev_class, g_dev); 484