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/proc.h> 48 #include <sys/errno.h> 49 #include <sys/time.h> 50 #include <sys/disk.h> 51 #include <sys/fcntl.h> 52 #include <sys/limits.h> 53 #include <geom/geom.h> 54 #include <geom/geom_int.h> 55 56 static d_open_t g_dev_open; 57 static d_close_t g_dev_close; 58 static d_strategy_t g_dev_strategy; 59 static d_ioctl_t g_dev_ioctl; 60 61 static struct cdevsw g_dev_cdevsw = { 62 .d_version = D_VERSION, 63 .d_open = g_dev_open, 64 .d_close = g_dev_close, 65 .d_read = physread, 66 .d_write = physwrite, 67 .d_ioctl = g_dev_ioctl, 68 .d_strategy = g_dev_strategy, 69 .d_name = "g_dev", 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 .version = G_VERSION, 79 .taste = g_dev_taste, 80 .orphan = g_dev_orphan, 81 }; 82 83 void 84 g_dev_print(void) 85 { 86 struct g_geom *gp; 87 char const *p = ""; 88 89 LIST_FOREACH(gp, &g_dev_class.geom, geom) { 90 printf("%s%s", p, gp->name); 91 p = " "; 92 } 93 printf("\n"); 94 } 95 96 struct g_provider * 97 g_dev_getprovider(struct cdev *dev) 98 { 99 struct g_consumer *cp; 100 101 g_topology_assert(); 102 if (dev == NULL) 103 return (NULL); 104 if (dev->si_devsw != &g_dev_cdevsw) 105 return (NULL); 106 cp = dev->si_drv2; 107 return (cp->provider); 108 } 109 110 111 static struct g_geom * 112 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 113 { 114 struct g_geom *gp; 115 struct g_consumer *cp; 116 int error, len; 117 struct cdev *dev, *adev; 118 char buf[64], *val; 119 120 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 121 g_topology_assert(); 122 LIST_FOREACH(cp, &pp->consumers, consumers) 123 if (cp->geom->class == mp) 124 return (NULL); 125 gp = g_new_geomf(mp, pp->name); 126 cp = g_new_consumer(gp); 127 error = g_attach(cp, pp); 128 KASSERT(error == 0, 129 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 130 error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &dev, 131 &g_dev_cdevsw, NULL, UID_ROOT, GID_OPERATOR, 0640, "%s", gp->name); 132 if (error != 0) { 133 printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n", 134 __func__, gp->name, error); 135 g_detach(cp); 136 g_destroy_consumer(cp); 137 g_destroy_geom(gp); 138 return (NULL); 139 } 140 141 /* Search for device alias name and create it if found. */ 142 adev = NULL; 143 for (len = MIN(strlen(gp->name), sizeof(buf) - 15); len > 0; len--) { 144 snprintf(buf, sizeof(buf), "kern.devalias.%s", gp->name); 145 buf[14 + len] = 0; 146 val = getenv(buf); 147 if (val != NULL) { 148 snprintf(buf, sizeof(buf), "%s%s", 149 val, gp->name + len); 150 freeenv(val); 151 adev = make_dev_alias(dev, buf); 152 break; 153 } 154 } 155 156 if (pp->flags & G_PF_CANDELETE) 157 dev->si_flags |= SI_CANDELETE; 158 dev->si_iosize_max = MAXPHYS; 159 gp->softc = dev; 160 dev->si_drv1 = gp; 161 dev->si_drv2 = cp; 162 if (adev != NULL) { 163 if (pp->flags & G_PF_CANDELETE) 164 adev->si_flags |= SI_CANDELETE; 165 adev->si_iosize_max = MAXPHYS; 166 adev->si_drv1 = gp; 167 adev->si_drv2 = cp; 168 } 169 return (gp); 170 } 171 172 static int 173 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td) 174 { 175 struct g_geom *gp; 176 struct g_consumer *cp; 177 int error, r, w, e; 178 179 gp = dev->si_drv1; 180 cp = dev->si_drv2; 181 if (gp == NULL || cp == NULL || gp->softc != dev) 182 return(ENXIO); /* g_dev_taste() not done yet */ 183 184 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 185 gp->name, flags, fmt, td); 186 187 r = flags & FREAD ? 1 : 0; 188 w = flags & FWRITE ? 1 : 0; 189 #ifdef notyet 190 e = flags & O_EXCL ? 1 : 0; 191 #else 192 e = 0; 193 #endif 194 if (w) { 195 /* 196 * When running in very secure mode, do not allow 197 * opens for writing of any disks. 198 */ 199 error = securelevel_ge(td->td_ucred, 2); 200 if (error) 201 return (error); 202 } 203 g_topology_lock(); 204 if (dev->si_devsw == NULL) 205 error = ENXIO; /* We were orphaned */ 206 else 207 error = g_access(cp, r, w, e); 208 g_topology_unlock(); 209 return(error); 210 } 211 212 static int 213 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td) 214 { 215 struct g_geom *gp; 216 struct g_consumer *cp; 217 int error, r, w, e, i; 218 219 gp = dev->si_drv1; 220 cp = dev->si_drv2; 221 if (gp == NULL || cp == NULL) 222 return(ENXIO); 223 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 224 gp->name, flags, fmt, td); 225 r = flags & FREAD ? -1 : 0; 226 w = flags & FWRITE ? -1 : 0; 227 #ifdef notyet 228 e = flags & O_EXCL ? -1 : 0; 229 #else 230 e = 0; 231 #endif 232 g_topology_lock(); 233 if (dev->si_devsw == NULL) 234 error = ENXIO; /* We were orphaned */ 235 else 236 error = g_access(cp, r, w, e); 237 for (i = 0; i < 10 * hz;) { 238 if (cp->acr != 0 || cp->acw != 0) 239 break; 240 if (cp->nstart == cp->nend) 241 break; 242 pause("gdevwclose", hz / 10); 243 i += hz / 10; 244 } 245 if (cp->acr == 0 && cp->acw == 0 && cp->nstart != cp->nend) { 246 printf("WARNING: Final close of geom_dev(%s) %s %s\n", 247 gp->name, 248 "still has outstanding I/O after 10 seconds.", 249 "Completing close anyway, panic may happen later."); 250 } 251 g_topology_unlock(); 252 return (error); 253 } 254 255 /* 256 * XXX: Until we have unmessed the ioctl situation, there is a race against 257 * XXX: a concurrent orphanization. We cannot close it by holding topology 258 * XXX: since that would prevent us from doing our job, and stalling events 259 * XXX: will break (actually: stall) the BSD disklabel hacks. 260 */ 261 static int 262 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 263 { 264 struct g_geom *gp; 265 struct g_consumer *cp; 266 struct g_provider *pp; 267 struct g_kerneldump kd; 268 off_t offset, length, chunk; 269 int i, error; 270 u_int u; 271 272 gp = dev->si_drv1; 273 cp = dev->si_drv2; 274 pp = cp->provider; 275 276 error = 0; 277 KASSERT(cp->acr || cp->acw, 278 ("Consumer with zero access count in g_dev_ioctl")); 279 280 i = IOCPARM_LEN(cmd); 281 switch (cmd) { 282 case DIOCGSECTORSIZE: 283 *(u_int *)data = cp->provider->sectorsize; 284 if (*(u_int *)data == 0) 285 error = ENOENT; 286 break; 287 case DIOCGMEDIASIZE: 288 *(off_t *)data = cp->provider->mediasize; 289 if (*(off_t *)data == 0) 290 error = ENOENT; 291 break; 292 case DIOCGFWSECTORS: 293 error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 294 if (error == 0 && *(u_int *)data == 0) 295 error = ENOENT; 296 break; 297 case DIOCGFWHEADS: 298 error = g_io_getattr("GEOM::fwheads", cp, &i, data); 299 if (error == 0 && *(u_int *)data == 0) 300 error = ENOENT; 301 break; 302 case DIOCGFRONTSTUFF: 303 error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 304 break; 305 case DIOCSKERNELDUMP: 306 u = *((u_int *)data); 307 if (!u) { 308 set_dumper(NULL); 309 error = 0; 310 break; 311 } 312 kd.offset = 0; 313 kd.length = OFF_MAX; 314 i = sizeof kd; 315 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 316 if (!error) { 317 error = set_dumper(&kd.di); 318 if (!error) 319 dev->si_flags |= SI_DUMPDEV; 320 } 321 break; 322 case DIOCGFLUSH: 323 error = g_io_flush(cp); 324 break; 325 case DIOCGDELETE: 326 offset = ((off_t *)data)[0]; 327 length = ((off_t *)data)[1]; 328 if ((offset % cp->provider->sectorsize) != 0 || 329 (length % cp->provider->sectorsize) != 0 || length <= 0) { 330 printf("%s: offset=%jd length=%jd\n", __func__, offset, 331 length); 332 error = EINVAL; 333 break; 334 } 335 while (length > 0) { 336 chunk = length; 337 if (chunk > 65536 * cp->provider->sectorsize) 338 chunk = 65536 * cp->provider->sectorsize; 339 error = g_delete_data(cp, offset, chunk); 340 length -= chunk; 341 offset += chunk; 342 if (error) 343 break; 344 /* 345 * Since the request size is unbounded, the service 346 * time is likewise. We make this ioctl interruptible 347 * by checking for signals for each bio. 348 */ 349 if (SIGPENDING(td)) 350 break; 351 } 352 break; 353 case DIOCGIDENT: 354 error = g_io_getattr("GEOM::ident", cp, &i, data); 355 break; 356 case DIOCGPROVIDERNAME: 357 if (pp == NULL) 358 return (ENOENT); 359 strlcpy(data, pp->name, i); 360 break; 361 case DIOCGSTRIPESIZE: 362 *(off_t *)data = cp->provider->stripesize; 363 break; 364 case DIOCGSTRIPEOFFSET: 365 *(off_t *)data = cp->provider->stripeoffset; 366 break; 367 default: 368 if (cp->provider->geom->ioctl != NULL) { 369 error = cp->provider->geom->ioctl(cp->provider, cmd, data, fflag, td); 370 } else { 371 error = ENOIOCTL; 372 } 373 } 374 375 return (error); 376 } 377 378 static void 379 g_dev_done(struct bio *bp2) 380 { 381 struct bio *bp; 382 383 bp = bp2->bio_parent; 384 bp->bio_error = bp2->bio_error; 385 if (bp->bio_error != 0) { 386 g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 387 bp2, bp->bio_error); 388 bp->bio_flags |= BIO_ERROR; 389 } else { 390 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 391 bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 392 } 393 bp->bio_resid = bp->bio_length - bp2->bio_completed; 394 bp->bio_completed = bp2->bio_completed; 395 g_destroy_bio(bp2); 396 biodone(bp); 397 } 398 399 static void 400 g_dev_strategy(struct bio *bp) 401 { 402 struct g_consumer *cp; 403 struct bio *bp2; 404 struct cdev *dev; 405 406 KASSERT(bp->bio_cmd == BIO_READ || 407 bp->bio_cmd == BIO_WRITE || 408 bp->bio_cmd == BIO_DELETE, 409 ("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd)); 410 dev = bp->bio_dev; 411 cp = dev->si_drv2; 412 KASSERT(cp->acr || cp->acw, 413 ("Consumer with zero access count in g_dev_strategy")); 414 #ifdef INVARIANTS 415 if ((bp->bio_offset % cp->provider->sectorsize) != 0 || 416 (bp->bio_bcount % cp->provider->sectorsize) != 0) { 417 bp->bio_resid = bp->bio_bcount; 418 biofinish(bp, NULL, EINVAL); 419 return; 420 } 421 #endif 422 for (;;) { 423 /* 424 * XXX: This is not an ideal solution, but I belive it to 425 * XXX: deadlock safe, all things considered. 426 */ 427 bp2 = g_clone_bio(bp); 428 if (bp2 != NULL) 429 break; 430 pause("gdstrat", hz / 10); 431 } 432 KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place")); 433 bp2->bio_done = g_dev_done; 434 g_trace(G_T_BIO, 435 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 436 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 437 bp2->bio_data, bp2->bio_cmd); 438 g_io_request(bp2, cp); 439 KASSERT(cp->acr || cp->acw, 440 ("g_dev_strategy raced with g_dev_close and lost")); 441 442 } 443 444 /* 445 * g_dev_orphan() 446 * 447 * Called from below when the provider orphaned us. 448 * - Clear any dump settings. 449 * - Destroy the struct cdev *to prevent any more request from coming in. The 450 * provider is already marked with an error, so anything which comes in 451 * in the interrim will be returned immediately. 452 * - Wait for any outstanding I/O to finish. 453 * - Set our access counts to zero, whatever they were. 454 * - Detach and self-destruct. 455 */ 456 457 static void 458 g_dev_orphan(struct g_consumer *cp) 459 { 460 struct g_geom *gp; 461 struct cdev *dev; 462 463 g_topology_assert(); 464 gp = cp->geom; 465 dev = gp->softc; 466 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name); 467 468 /* Reset any dump-area set on this device */ 469 if (dev->si_flags & SI_DUMPDEV) 470 set_dumper(NULL); 471 472 /* Destroy the struct cdev *so we get no more requests */ 473 destroy_dev(dev); 474 475 /* Wait for the cows to come home */ 476 while (cp->nstart != cp->nend) 477 pause("gdevorphan", hz / 10); 478 479 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 480 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 481 482 g_detach(cp); 483 g_destroy_consumer(cp); 484 g_destroy_geom(gp); 485 } 486 487 DECLARE_GEOM_CLASS(g_dev_class, g_dev); 488