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