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 *
| 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_disk.c 110720 2003-02-11 18:32:31Z phk $
| 35 * $FreeBSD: head/sys/geom/geom_disk.c 110727 2003-02-11 21:23:34Z phk $
|
36 */ 37 38#include "opt_geom.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/sysctl.h> 44#include <sys/bio.h> 45#include <sys/conf.h> 46#include <sys/disk.h> 47#include <sys/fcntl.h> 48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/stdint.h> 51#include <machine/md_var.h> 52 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <geom/geom.h> 56 57static struct mtx g_disk_done_mtx; 58 59static g_access_t g_disk_access; 60 61struct g_class g_disk_class = { 62 "DISK", 63 NULL, 64 NULL, 65 G_CLASS_INITIALIZER 66}; 67 68static void 69g_disk_init(void) 70{ 71 mtx_unlock(&Giant); 72 g_add_class(&g_disk_class); 73 mtx_init(&g_disk_done_mtx, "g_disk_done", MTX_DEF, 0); 74 mtx_lock(&Giant); 75} 76 77DECLARE_GEOM_CLASS_INIT(g_disk_class, g_disk, g_disk_init); 78 79static void __inline 80g_disk_lock_giant(struct disk *dp) 81{ 82 if (dp->d_flags & DISKFLAG_NOGIANT) 83 return; 84 mtx_lock(&Giant); 85} 86 87static void __inline 88g_disk_unlock_giant(struct disk *dp) 89{ 90 if (dp->d_flags & DISKFLAG_NOGIANT) 91 return; 92 mtx_unlock(&Giant); 93} 94 95static int 96g_disk_access(struct g_provider *pp, int r, int w, int e) 97{ 98 struct disk *dp; 99 dev_t dev; 100 int error; 101 102 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 103 pp->name, r, w, e); 104 g_topology_assert(); 105 r += pp->acr; 106 w += pp->acw; 107 e += pp->ace; 108 dp = pp->geom->softc; 109 dev = dp->d_dev; 110 error = 0; 111 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 112 if (dp->d_open != NULL || dp->d_copen != NULL) { 113 g_disk_lock_giant(dp); 114 if (dp->d_open != NULL) 115 error = dp->d_open(dp); 116 else 117 error = dp->d_copen(dev, FREAD|FWRITE, 0, NULL); 118 if (error != 0) 119 printf("Opened disk %s -> %d\n", 120 pp->name, error); 121 g_disk_unlock_giant(dp); 122 } 123 pp->mediasize = dp->d_mediasize; 124 pp->sectorsize = dp->d_sectorsize; 125 dp->d_flags |= DISKFLAG_OPEN;
| 36 */ 37 38#include "opt_geom.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/sysctl.h> 44#include <sys/bio.h> 45#include <sys/conf.h> 46#include <sys/disk.h> 47#include <sys/fcntl.h> 48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/stdint.h> 51#include <machine/md_var.h> 52 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <geom/geom.h> 56 57static struct mtx g_disk_done_mtx; 58 59static g_access_t g_disk_access; 60 61struct g_class g_disk_class = { 62 "DISK", 63 NULL, 64 NULL, 65 G_CLASS_INITIALIZER 66}; 67 68static void 69g_disk_init(void) 70{ 71 mtx_unlock(&Giant); 72 g_add_class(&g_disk_class); 73 mtx_init(&g_disk_done_mtx, "g_disk_done", MTX_DEF, 0); 74 mtx_lock(&Giant); 75} 76 77DECLARE_GEOM_CLASS_INIT(g_disk_class, g_disk, g_disk_init); 78 79static void __inline 80g_disk_lock_giant(struct disk *dp) 81{ 82 if (dp->d_flags & DISKFLAG_NOGIANT) 83 return; 84 mtx_lock(&Giant); 85} 86 87static void __inline 88g_disk_unlock_giant(struct disk *dp) 89{ 90 if (dp->d_flags & DISKFLAG_NOGIANT) 91 return; 92 mtx_unlock(&Giant); 93} 94 95static int 96g_disk_access(struct g_provider *pp, int r, int w, int e) 97{ 98 struct disk *dp; 99 dev_t dev; 100 int error; 101 102 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 103 pp->name, r, w, e); 104 g_topology_assert(); 105 r += pp->acr; 106 w += pp->acw; 107 e += pp->ace; 108 dp = pp->geom->softc; 109 dev = dp->d_dev; 110 error = 0; 111 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 112 if (dp->d_open != NULL || dp->d_copen != NULL) { 113 g_disk_lock_giant(dp); 114 if (dp->d_open != NULL) 115 error = dp->d_open(dp); 116 else 117 error = dp->d_copen(dev, FREAD|FWRITE, 0, NULL); 118 if (error != 0) 119 printf("Opened disk %s -> %d\n", 120 pp->name, error); 121 g_disk_unlock_giant(dp); 122 } 123 pp->mediasize = dp->d_mediasize; 124 pp->sectorsize = dp->d_sectorsize; 125 dp->d_flags |= DISKFLAG_OPEN;
|
| 126 if (dp->d_maxsize == 0 && dp->d_dev->si_iosize_max != 0) 127 dp->d_maxsize = dp->d_dev->si_iosize_max; 128 if (dp->d_maxsize == 0) { 129 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 130 dp->d_name, dp->d_unit); 131 dp->d_maxsize = DFLTPHYS; 132 }
|
126 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 127 if (dp->d_close != NULL || dp->d_cclose != NULL) { 128 g_disk_lock_giant(dp); 129 if (dp->d_close != NULL) 130 error = dp->d_close(dp); 131 else 132 error = dp->d_cclose(dev, FREAD|FWRITE, 0, NULL); 133 if (error != 0) 134 printf("Closed disk %s -> %d\n", 135 pp->name, error); 136 g_disk_unlock_giant(dp); 137 } 138 dp->d_flags &= ~DISKFLAG_OPEN; 139 } 140 return (error); 141} 142 143static void 144g_disk_kerneldump(struct bio *bp, struct disk *dp) 145{ 146 int error; 147 struct g_kerneldump *gkd; 148 struct dumperinfo di; 149 struct g_geom *gp; 150 151 gkd = (struct g_kerneldump*)bp->bio_data; 152 gp = bp->bio_to->geom; 153 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 154 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 155 di.dumper = dp->d_dump; 156 di.priv = dp->d_dev; 157 di.blocksize = dp->d_sectorsize; 158 di.mediaoffset = gkd->offset; 159 di.mediasize = gkd->length; 160 error = set_dumper(&di); 161 g_io_deliver(bp, error); 162} 163 164static void 165g_disk_done(struct bio *bp) 166{ 167 168 /* See "notes" for why we need a mutex here */ 169 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 170 mtx_lock(&g_disk_done_mtx); 171 bp->bio_completed = bp->bio_length - bp->bio_resid; 172 g_std_done(bp); 173 mtx_unlock(&g_disk_done_mtx); 174} 175 176static void 177g_disk_start(struct bio *bp) 178{ 179 struct bio *bp2, *bp3; 180 dev_t dev; 181 struct disk *dp; 182 struct g_ioctl *gio; 183 int error; 184 off_t off; 185 186 dp = bp->bio_to->geom->softc; 187 dev = dp->d_dev; 188 error = EJUSTRETURN; 189 switch(bp->bio_cmd) { 190 case BIO_DELETE: 191 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 192 error = 0; 193 break; 194 } 195 /* fall-through */ 196 case BIO_READ: 197 case BIO_WRITE:
| 133 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 134 if (dp->d_close != NULL || dp->d_cclose != NULL) { 135 g_disk_lock_giant(dp); 136 if (dp->d_close != NULL) 137 error = dp->d_close(dp); 138 else 139 error = dp->d_cclose(dev, FREAD|FWRITE, 0, NULL); 140 if (error != 0) 141 printf("Closed disk %s -> %d\n", 142 pp->name, error); 143 g_disk_unlock_giant(dp); 144 } 145 dp->d_flags &= ~DISKFLAG_OPEN; 146 } 147 return (error); 148} 149 150static void 151g_disk_kerneldump(struct bio *bp, struct disk *dp) 152{ 153 int error; 154 struct g_kerneldump *gkd; 155 struct dumperinfo di; 156 struct g_geom *gp; 157 158 gkd = (struct g_kerneldump*)bp->bio_data; 159 gp = bp->bio_to->geom; 160 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 161 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 162 di.dumper = dp->d_dump; 163 di.priv = dp->d_dev; 164 di.blocksize = dp->d_sectorsize; 165 di.mediaoffset = gkd->offset; 166 di.mediasize = gkd->length; 167 error = set_dumper(&di); 168 g_io_deliver(bp, error); 169} 170 171static void 172g_disk_done(struct bio *bp) 173{ 174 175 /* See "notes" for why we need a mutex here */ 176 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 177 mtx_lock(&g_disk_done_mtx); 178 bp->bio_completed = bp->bio_length - bp->bio_resid; 179 g_std_done(bp); 180 mtx_unlock(&g_disk_done_mtx); 181} 182 183static void 184g_disk_start(struct bio *bp) 185{ 186 struct bio *bp2, *bp3; 187 dev_t dev; 188 struct disk *dp; 189 struct g_ioctl *gio; 190 int error; 191 off_t off; 192 193 dp = bp->bio_to->geom->softc; 194 dev = dp->d_dev; 195 error = EJUSTRETURN; 196 switch(bp->bio_cmd) { 197 case BIO_DELETE: 198 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 199 error = 0; 200 break; 201 } 202 /* fall-through */ 203 case BIO_READ: 204 case BIO_WRITE:
|
198 if (dp->d_dev->si_iosize_max != 0) 199 dp->d_maxsize = dp->d_dev->si_iosize_max; 200#ifdef maybe 201 else 202 /* 203 * XXX: Who knows how many drivers have undeclared 204 * limitations ? 205 */ 206 dp->d_maxsize = DFLTPHYS; 207#endif
| |
208 off = 0; 209 bp3 = NULL; 210 bp2 = g_clone_bio(bp); 211 if (bp2 == NULL) { 212 error = ENOMEM; 213 break; 214 } 215 do { 216 bp2->bio_offset += off; 217 bp2->bio_length -= off; 218 bp2->bio_data -= off; 219 if (bp2->bio_length > dp->d_maxsize) { 220 /* 221 * XXX: If we have a stripesize we should really 222 * use it here. 223 */ 224 bp2->bio_length = dp->d_maxsize; 225 off += dp->d_maxsize; 226 /* 227 * To avoid a race, we need to grab the next bio 228 * before we schedule this one. See "notes". 229 */ 230 bp3 = g_clone_bio(bp); 231 if (bp3 == NULL) 232 bp->bio_error = ENOMEM; 233 } 234 bp2->bio_done = g_disk_done; 235 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 236 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 237 bp2->bio_bcount = bp2->bio_length; 238 bp2->bio_dev = dev; 239 bp2->bio_disk = dp; 240 g_disk_lock_giant(dp); 241 dp->d_strategy(bp2); 242 g_disk_unlock_giant(dp); 243 bp2 = bp3; 244 bp3 = NULL; 245 } while (bp2 != NULL); 246 break; 247 case BIO_GETATTR: 248 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 249 break; 250 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 251 break; 252 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 253 break; 254 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 255 g_disk_kerneldump(bp, dp); 256 else if ((dp->d_ioctl != NULL || dp->d_cioctl != NULL) && 257 !strcmp(bp->bio_attribute, "GEOM::ioctl") && 258 bp->bio_length == sizeof *gio) { 259 gio = (struct g_ioctl *)bp->bio_data; 260 if (dp->d_ioctl != NULL) { 261 gio->func = (d_ioctl_t *)(dp->d_ioctl); 262 gio->dev = dp; 263 } else { 264 gio->func = dp->d_cioctl; 265 gio->dev = dp->d_dev; 266 } 267 error = EDIRIOCTL; 268 } else 269 error = ENOIOCTL; 270 break; 271 case BIO_SETATTR: 272 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 273 bp->bio_length == sizeof *gio) { 274 gio = (struct g_ioctl *)bp->bio_data; 275 gio->func = devsw(dp->d_dev)->d_ioctl; 276 gio->dev = dp->d_dev; 277 error = EDIRIOCTL; 278 } else 279 error = ENOIOCTL; 280 break; 281 default: 282 error = EOPNOTSUPP; 283 break; 284 } 285 if (error != EJUSTRETURN) 286 g_io_deliver(bp, error); 287 return; 288} 289 290static void 291g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 292{ 293 struct disk *dp; 294 295 dp = gp->softc; 296 if (indent == NULL) { 297 sbuf_printf(sb, " hd %u", dp->d_fwheads); 298 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 299 return; 300 } 301 if (pp != NULL) { 302 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 303 indent, dp->d_fwheads); 304 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 305 indent, dp->d_fwsectors); 306 } 307} 308 309static void 310g_disk_create(void *arg) 311{ 312 struct g_geom *gp; 313 struct g_provider *pp; 314 struct disk *dp; 315 316 g_topology_assert(); 317 dp = arg; 318 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 319 gp->start = g_disk_start; 320 gp->access = g_disk_access; 321 gp->softc = dp; 322 gp->dumpconf = g_disk_dumpconf; 323 dp->d_geom = gp; 324 pp = g_new_providerf(gp, "%s", gp->name); 325 pp->mediasize = dp->d_mediasize; 326 pp->sectorsize = dp->d_sectorsize; 327 if (dp->d_flags & DISKFLAG_CANDELETE) 328 pp->flags |= G_PF_CANDELETE; 329 pp->stripeoffset = dp->d_stripeoffset; 330 pp->stripesize = dp->d_stripesize; 331 g_error_provider(pp, 0); 332 if (bootverbose) 333 printf("GEOM: new disk %s\n", gp->name); 334} 335 336 337 338dev_t 339disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void * unused __unused) 340{ 341 dev_t dev; 342 343 dev = g_malloc(sizeof *dev, M_ZERO); 344 dp->d_unit = unit; 345 dp->d_dev = dev; 346 dp->d_flags = flags; 347 dp->d_devsw = cdevsw; 348 dev->si_devsw = cdevsw; 349 if (cdevsw != NULL) { 350 dp->d_copen = cdevsw->d_open; 351 dp->d_cclose = cdevsw->d_close; 352 dp->d_cioctl = cdevsw->d_ioctl; 353 dp->d_strategy = cdevsw->d_strategy; 354 dp->d_dump = (dumper_t *)cdevsw->d_dump; 355 dp->d_name = cdevsw->d_name; 356 } 357 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 358 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 359 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 360 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 361 dev->si_disk = dp; 362 dev->si_udev = 0x10002; /* XXX: Needed ? */ 363 dev->si_name = dev->__si_namebuf; 364 sprintf(dev->si_name, "%s%d", dp->d_name, unit); 365 g_call_me(g_disk_create, dp); 366 return (dev); 367} 368 369void 370disk_destroy(dev_t dev) 371{ 372 struct disk *dp; 373 struct g_geom *gp; 374 375 dp = dev->si_disk; 376 gp = dp->d_geom; 377 g_free(dev); 378 gp->flags |= G_GEOM_WITHER; 379 gp->softc = NULL; 380 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO); 381} 382 383static void 384g_kern_disks(void *p) 385{ 386 struct sbuf *sb; 387 struct g_geom *gp; 388 char *sp; 389 390 sb = p; 391 sp = ""; 392 g_topology_assert(); 393 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 394 sbuf_printf(sb, "%s%s", sp, gp->name); 395 sp = " "; 396 } 397 sbuf_finish(sb); 398 wakeup(sb); 399} 400 401static int 402sysctl_disks(SYSCTL_HANDLER_ARGS) 403{ 404 int error; 405 struct sbuf *sb; 406 407 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 408 sbuf_clear(sb); 409 g_call_me(g_kern_disks, sb); 410 do { 411 tsleep(sb, PZERO, "kern.disks", hz); 412 } while(!sbuf_done(sb)); 413 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 414 sbuf_delete(sb); 415 return error; 416} 417 418SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 419 sysctl_disks, "A", "names of available disks"); 420
| 205 off = 0; 206 bp3 = NULL; 207 bp2 = g_clone_bio(bp); 208 if (bp2 == NULL) { 209 error = ENOMEM; 210 break; 211 } 212 do { 213 bp2->bio_offset += off; 214 bp2->bio_length -= off; 215 bp2->bio_data -= off; 216 if (bp2->bio_length > dp->d_maxsize) { 217 /* 218 * XXX: If we have a stripesize we should really 219 * use it here. 220 */ 221 bp2->bio_length = dp->d_maxsize; 222 off += dp->d_maxsize; 223 /* 224 * To avoid a race, we need to grab the next bio 225 * before we schedule this one. See "notes". 226 */ 227 bp3 = g_clone_bio(bp); 228 if (bp3 == NULL) 229 bp->bio_error = ENOMEM; 230 } 231 bp2->bio_done = g_disk_done; 232 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 233 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 234 bp2->bio_bcount = bp2->bio_length; 235 bp2->bio_dev = dev; 236 bp2->bio_disk = dp; 237 g_disk_lock_giant(dp); 238 dp->d_strategy(bp2); 239 g_disk_unlock_giant(dp); 240 bp2 = bp3; 241 bp3 = NULL; 242 } while (bp2 != NULL); 243 break; 244 case BIO_GETATTR: 245 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 246 break; 247 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 248 break; 249 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 250 break; 251 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 252 g_disk_kerneldump(bp, dp); 253 else if ((dp->d_ioctl != NULL || dp->d_cioctl != NULL) && 254 !strcmp(bp->bio_attribute, "GEOM::ioctl") && 255 bp->bio_length == sizeof *gio) { 256 gio = (struct g_ioctl *)bp->bio_data; 257 if (dp->d_ioctl != NULL) { 258 gio->func = (d_ioctl_t *)(dp->d_ioctl); 259 gio->dev = dp; 260 } else { 261 gio->func = dp->d_cioctl; 262 gio->dev = dp->d_dev; 263 } 264 error = EDIRIOCTL; 265 } else 266 error = ENOIOCTL; 267 break; 268 case BIO_SETATTR: 269 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 270 bp->bio_length == sizeof *gio) { 271 gio = (struct g_ioctl *)bp->bio_data; 272 gio->func = devsw(dp->d_dev)->d_ioctl; 273 gio->dev = dp->d_dev; 274 error = EDIRIOCTL; 275 } else 276 error = ENOIOCTL; 277 break; 278 default: 279 error = EOPNOTSUPP; 280 break; 281 } 282 if (error != EJUSTRETURN) 283 g_io_deliver(bp, error); 284 return; 285} 286 287static void 288g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 289{ 290 struct disk *dp; 291 292 dp = gp->softc; 293 if (indent == NULL) { 294 sbuf_printf(sb, " hd %u", dp->d_fwheads); 295 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 296 return; 297 } 298 if (pp != NULL) { 299 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 300 indent, dp->d_fwheads); 301 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 302 indent, dp->d_fwsectors); 303 } 304} 305 306static void 307g_disk_create(void *arg) 308{ 309 struct g_geom *gp; 310 struct g_provider *pp; 311 struct disk *dp; 312 313 g_topology_assert(); 314 dp = arg; 315 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 316 gp->start = g_disk_start; 317 gp->access = g_disk_access; 318 gp->softc = dp; 319 gp->dumpconf = g_disk_dumpconf; 320 dp->d_geom = gp; 321 pp = g_new_providerf(gp, "%s", gp->name); 322 pp->mediasize = dp->d_mediasize; 323 pp->sectorsize = dp->d_sectorsize; 324 if (dp->d_flags & DISKFLAG_CANDELETE) 325 pp->flags |= G_PF_CANDELETE; 326 pp->stripeoffset = dp->d_stripeoffset; 327 pp->stripesize = dp->d_stripesize; 328 g_error_provider(pp, 0); 329 if (bootverbose) 330 printf("GEOM: new disk %s\n", gp->name); 331} 332 333 334 335dev_t 336disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void * unused __unused) 337{ 338 dev_t dev; 339 340 dev = g_malloc(sizeof *dev, M_ZERO); 341 dp->d_unit = unit; 342 dp->d_dev = dev; 343 dp->d_flags = flags; 344 dp->d_devsw = cdevsw; 345 dev->si_devsw = cdevsw; 346 if (cdevsw != NULL) { 347 dp->d_copen = cdevsw->d_open; 348 dp->d_cclose = cdevsw->d_close; 349 dp->d_cioctl = cdevsw->d_ioctl; 350 dp->d_strategy = cdevsw->d_strategy; 351 dp->d_dump = (dumper_t *)cdevsw->d_dump; 352 dp->d_name = cdevsw->d_name; 353 } 354 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 355 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 356 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 357 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 358 dev->si_disk = dp; 359 dev->si_udev = 0x10002; /* XXX: Needed ? */ 360 dev->si_name = dev->__si_namebuf; 361 sprintf(dev->si_name, "%s%d", dp->d_name, unit); 362 g_call_me(g_disk_create, dp); 363 return (dev); 364} 365 366void 367disk_destroy(dev_t dev) 368{ 369 struct disk *dp; 370 struct g_geom *gp; 371 372 dp = dev->si_disk; 373 gp = dp->d_geom; 374 g_free(dev); 375 gp->flags |= G_GEOM_WITHER; 376 gp->softc = NULL; 377 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO); 378} 379 380static void 381g_kern_disks(void *p) 382{ 383 struct sbuf *sb; 384 struct g_geom *gp; 385 char *sp; 386 387 sb = p; 388 sp = ""; 389 g_topology_assert(); 390 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 391 sbuf_printf(sb, "%s%s", sp, gp->name); 392 sp = " "; 393 } 394 sbuf_finish(sb); 395 wakeup(sb); 396} 397 398static int 399sysctl_disks(SYSCTL_HANDLER_ARGS) 400{ 401 int error; 402 struct sbuf *sb; 403 404 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 405 sbuf_clear(sb); 406 g_call_me(g_kern_disks, sb); 407 do { 408 tsleep(sb, PZERO, "kern.disks", hz); 409 } while(!sbuf_done(sb)); 410 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 411 sbuf_delete(sb); 412 return error; 413} 414 415SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 416 sysctl_disks, "A", "names of available disks"); 417
|