geom_dev.c revision 105540
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: head/sys/geom/geom_dev.c 105540 2002-10-20 18:48:12Z phk $ 36 */ 37 38#include <sys/param.h> 39#include <sys/stdint.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 <geom/geom.h> 52#include <geom/geom_int.h> 53#include <machine/limits.h> 54 55#define CDEV_MAJOR 4 56 57static d_open_t g_dev_open; 58static d_close_t g_dev_close; 59static d_strategy_t g_dev_strategy; 60static d_ioctl_t g_dev_ioctl; 61static d_psize_t g_dev_psize; 62 63static struct cdevsw g_dev_cdevsw = { 64 /* open */ g_dev_open, 65 /* close */ g_dev_close, 66 /* read */ physread, 67 /* write */ physwrite, 68 /* ioctl */ g_dev_ioctl, 69 /* poll */ nopoll, 70 /* mmap */ nommap, 71 /* strategy */ g_dev_strategy, 72 /* name */ "g_dev", 73 /* maj */ CDEV_MAJOR, 74 /* dump */ nodump, 75 /* psize */ g_dev_psize, 76 /* flags */ D_DISK | D_CANFREE | D_TRACKCLOSE, 77 /* kqfilter */ nokqfilter 78}; 79 80static g_taste_t g_dev_taste; 81static g_orphan_t g_dev_orphan; 82 83static struct g_class g_dev_class = { 84 "DEV", 85 g_dev_taste, 86 NULL, 87 G_CLASS_INITIALIZER 88}; 89 90static void 91g_dev_clone(void *arg __unused, char *name, int namelen __unused, dev_t *dev) 92{ 93 struct g_geom *gp; 94 95 if (*dev != NODEV) 96 return; 97 98 g_trace(G_T_TOPOLOGY, "g_dev_clone(%s)", name); 99 g_waitidle(); 100 101 /* XXX: can I drop Giant here ??? */ 102 /* g_topology_lock(); */ 103 LIST_FOREACH(gp, &g_dev_class.geom, geom) { 104 if (strcmp(gp->name, name)) 105 continue; 106 *dev = gp->softc; 107 g_trace(G_T_TOPOLOGY, "g_dev_clone(%s) = %p", name, *dev); 108 return; 109 } 110 /* g_topology_unlock(); */ 111 return; 112} 113 114static void 115g_dev_register_cloner(void *foo __unused) 116{ 117 static int once; 118 119 /* XXX: why would this happen more than once ?? */ 120 if (!once) { 121 EVENTHANDLER_REGISTER(dev_clone, g_dev_clone, 0, 1000); 122 once++; 123 } 124} 125 126SYSINIT(geomdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,g_dev_register_cloner,NULL); 127 128static struct g_geom * 129g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused) 130{ 131 struct g_geom *gp; 132 struct g_consumer *cp; 133 static int unit; 134 int error; 135 dev_t dev; 136 137 g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name); 138 g_topology_assert(); 139 LIST_FOREACH(cp, &pp->consumers, consumers) 140 if (cp->geom->class == mp) 141 return (NULL); 142 gp = g_new_geomf(mp, pp->name); 143 gp->orphan = g_dev_orphan; 144 cp = g_new_consumer(gp); 145 error = g_attach(cp, pp); 146 KASSERT(error == 0, 147 ("g_dev_taste(%s) failed to g_attach, err=%d", pp->name, error)); 148 /* 149 * XXX: I'm not 100% sure we can call make_dev(9) without Giant 150 * yet. Once we can, we don't need to drop topology here either. 151 */ 152 g_topology_unlock(); 153 mtx_lock(&Giant); 154 dev = make_dev(&g_dev_cdevsw, unit2minor(unit++), 155 UID_ROOT, GID_OPERATOR, 0640, gp->name); 156 mtx_unlock(&Giant); 157 g_topology_lock(); 158 159 gp->softc = dev; 160 dev->si_drv1 = gp; 161 dev->si_drv2 = cp; 162 return (gp); 163} 164 165static int 166g_dev_open(dev_t dev, int flags, int fmt, struct thread *td) 167{ 168 struct g_geom *gp; 169 struct g_consumer *cp; 170 int error, r, w, e; 171 172 gp = dev->si_drv1; 173 cp = dev->si_drv2; 174 if (gp == NULL || cp == NULL) 175 return(ENXIO); 176 g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)", 177 gp->name, flags, fmt, td); 178 DROP_GIANT(); 179 g_topology_lock(); 180 g_silence(); 181 r = flags & FREAD ? 1 : 0; 182 w = flags & FWRITE ? 1 : 0; 183#ifdef notyet 184 e = flags & O_EXCL ? 1 : 0; 185#else 186 e = 0; 187#endif 188 error = g_access_rel(cp, r, w, e); 189 g_topology_unlock(); 190 PICKUP_GIANT(); 191 g_waitidle(); 192 return(error); 193} 194 195static int 196g_dev_close(dev_t dev, int flags, int fmt, struct thread *td) 197{ 198 struct g_geom *gp; 199 struct g_consumer *cp; 200 int error, r, w, e; 201 202 gp = dev->si_drv1; 203 cp = dev->si_drv2; 204 if (gp == NULL || cp == NULL) 205 return(ENXIO); 206 g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)", 207 gp->name, flags, fmt, td); 208 DROP_GIANT(); 209 g_topology_lock(); 210 g_silence(); 211 r = flags & FREAD ? -1 : 0; 212 w = flags & FWRITE ? -1 : 0; 213#ifdef notyet 214 e = flags & O_EXCL ? -1 : 0; 215#else 216 e = 0; 217#endif 218 error = g_access_rel(cp, r, w, e); 219 g_topology_unlock(); 220 PICKUP_GIANT(); 221 g_waitidle(); 222 return (error); 223} 224 225static int 226g_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 227{ 228 struct g_geom *gp, *gp2; 229 struct g_consumer *cp; 230 struct g_provider *pp2; 231 struct g_kerneldump kd; 232 int i, error; 233 u_int u; 234 struct g_ioctl *gio; 235 236 gp = dev->si_drv1; 237 cp = dev->si_drv2; 238 pp2 = cp->provider; 239 gp2 = pp2->geom; 240 gio = NULL; 241 242 error = 0; 243 DROP_GIANT(); 244 245 i = IOCPARM_LEN(cmd); 246 switch (cmd) { 247 case DIOCGSECTORSIZE: 248 error = g_io_getattr("GEOM::sectorsize", cp, &i, data); 249 if (error == 0 && *(u_int *)data == 0) 250 error = ENOENT; 251 break; 252 case DIOCGMEDIASIZE: 253 error = g_io_getattr("GEOM::mediasize", cp, &i, data); 254 if (error == 0 && *(off_t *)data == 0) 255 error = ENOENT; 256 break; 257 case DIOCGFWSECTORS: 258 error = g_io_getattr("GEOM::fwsectors", cp, &i, data); 259 if (error == 0 && *(u_int *)data == 0) 260 error = ENOENT; 261 break; 262 case DIOCGFWHEADS: 263 error = g_io_getattr("GEOM::fwheads", cp, &i, data); 264 if (error == 0 && *(u_int *)data == 0) 265 error = ENOENT; 266 break; 267 case DIOCGFRONTSTUFF: 268 error = g_io_getattr("GEOM::frontstuff", cp, &i, data); 269 break; 270 case DIOCSKERNELDUMP: 271 u = *((u_int *)data); 272 if (!u) { 273 set_dumper(NULL); 274 error = 0; 275 break; 276 } 277 kd.offset = 0; 278 kd.length = OFF_MAX; 279 i = sizeof kd; 280 error = g_io_getattr("GEOM::kerneldump", cp, &i, &kd); 281 if (!error) 282 dev->si_flags |= SI_DUMPDEV; 283 break; 284 285 default: 286 gio = g_malloc(sizeof *gio, M_WAITOK | M_ZERO); 287 gio->cmd = cmd; 288 gio->data = data; 289 gio->fflag = fflag; 290 gio->td = td; 291 i = sizeof *gio; 292 if (cmd & IOC_IN) 293 error = g_io_setattr("GEOM::ioctl", cp, i, gio); 294 else 295 error = g_io_getattr("GEOM::ioctl", cp, &i, gio); 296 break; 297 } 298 299 PICKUP_GIANT(); 300 if (error == EDIRIOCTL) { 301 KASSERT(gio != NULL, ("NULL gio but EDIRIOCTL")); 302 KASSERT(gio->func != NULL, ("NULL function but EDIRIOCTL")); 303 error = (gio->func)(gio->dev, cmd, data, fflag, td); 304 } 305 if (gio != NULL) 306 g_free(gio); 307 g_waitidle(); 308 if (error == ENOIOCTL) { 309 if (g_debugflags & G_T_TOPOLOGY) { 310 i = IOCGROUP(cmd); 311 printf("IOCTL(0x%lx) \"%s\"", cmd, gp->name); 312 if (i > ' ' && i <= '~') 313 printf(" '%c'", (int)IOCGROUP(cmd)); 314 else 315 printf(" 0x%lx", IOCGROUP(cmd)); 316 printf("/%ld ", cmd & 0xff); 317 if (cmd & IOC_IN) 318 printf("I"); 319 if (cmd & IOC_OUT) 320 printf("O"); 321 printf("(%ld) = ENOIOCTL\n", IOCPARM_LEN(cmd)); 322 } 323 error = ENOTTY; 324 } 325 return (error); 326} 327 328static int 329g_dev_psize(dev_t dev) 330{ 331 struct g_consumer *cp; 332 int i, error; 333 off_t mediasize; 334 335 cp = dev->si_drv2; 336 337 i = sizeof mediasize; 338 error = g_io_getattr("GEOM::mediasize", cp, &i, &mediasize); 339 if (error) 340 return (-1); 341 return (mediasize >> DEV_BSHIFT); 342} 343 344static void 345g_dev_done(struct bio *bp2) 346{ 347 struct bio *bp; 348 349 bp = bp2->bio_linkage; 350 bp->bio_error = bp2->bio_error; 351 if (bp->bio_error != 0) { 352 g_trace(G_T_BIO, "g_dev_done(%p) had error %d", 353 bp2, bp->bio_error); 354 bp->bio_flags |= BIO_ERROR; 355 } else { 356 g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd", 357 bp2, bp, bp->bio_resid, (intmax_t)bp2->bio_completed); 358 } 359 bp->bio_resid = bp->bio_bcount - bp2->bio_completed; 360 g_destroy_bio(bp2); 361 mtx_lock(&Giant); 362 biodone(bp); 363 mtx_unlock(&Giant); 364} 365 366static void 367g_dev_strategy(struct bio *bp) 368{ 369 struct g_geom *gp; 370 struct g_consumer *cp; 371 struct bio *bp2; 372 dev_t dev; 373 374 dev = bp->bio_dev; 375 gp = dev->si_drv1; 376 cp = dev->si_drv2; 377 bp2 = g_clone_bio(bp); 378 bp2->bio_offset = (off_t)bp->bio_blkno << DEV_BSHIFT; 379 bp2->bio_length = (off_t)bp->bio_bcount; 380 bp2->bio_done = g_dev_done; 381 g_trace(G_T_BIO, 382 "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d", 383 bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length, 384 bp2->bio_data, bp2->bio_cmd); 385 g_io_request(bp2, cp); 386} 387 388/* 389 * g_dev_orphan() 390 * 391 * Called from below when the provider orphaned us. It is our responsibility 392 * to get the access counts back to zero, until we do so the stack below will 393 * not unravel. We must clear the kernel-dump settings, if this is the 394 * current dumpdev. We call destroy_dev(9) to send our dev_t the way of 395 * punched cards and if we have non-zero access counts, we call down with 396 * them negated before we detattch and selfdestruct. 397 */ 398 399static void 400g_dev_orphan(struct g_consumer *cp) 401{ 402 struct g_geom *gp; 403 dev_t dev; 404 405 gp = cp->geom; 406 g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, gp->name); 407 g_topology_assert(); 408 if (cp->biocount > 0) 409 return; 410 dev = gp->softc; 411 if (dev->si_flags & SI_DUMPDEV) 412 set_dumper(NULL); 413 /* XXX: we may need Giant for now */ 414 destroy_dev(dev); 415 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 416 g_access_rel(cp, -cp->acr, -cp->acw, -cp->ace); 417 g_detach(cp); 418 g_destroy_consumer(cp); 419 g_destroy_geom(gp); 420} 421 422DECLARE_GEOM_CLASS(g_dev_class, g_dev); 423