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