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>
| 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>
|
38 39#include "opt_geom.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/sysctl.h> 45#include <sys/bio.h> 46#include <sys/conf.h> 47#include <sys/fcntl.h> 48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/devicestat.h> 51#include <machine/md_var.h> 52 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <geom/geom.h> 56#include <geom/geom_disk.h> 57#include <geom/geom_int.h> 58 59static struct mtx g_disk_done_mtx; 60 61static g_access_t g_disk_access; 62static g_init_t g_disk_init; 63static g_fini_t g_disk_fini; 64static g_start_t g_disk_start; 65static g_ioctl_t g_disk_ioctl; 66static g_dumpconf_t g_disk_dumpconf; 67 68static struct g_class g_disk_class = { 69 .name = "DISK", 70 .version = G_VERSION, 71 .init = g_disk_init, 72 .fini = g_disk_fini, 73 .start = g_disk_start, 74 .access = g_disk_access, 75 .ioctl = g_disk_ioctl, 76 .dumpconf = g_disk_dumpconf, 77}; 78 79static void 80g_disk_init(struct g_class *mp __unused) 81{ 82 83 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 84} 85 86static void 87g_disk_fini(struct g_class *mp __unused) 88{ 89 90 mtx_destroy(&g_disk_done_mtx); 91} 92 93DECLARE_GEOM_CLASS(g_disk_class, g_disk); 94 95static void __inline 96g_disk_lock_giant(struct disk *dp) 97{ 98 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 99 mtx_lock(&Giant); 100} 101 102static void __inline 103g_disk_unlock_giant(struct disk *dp) 104{ 105 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 106 mtx_unlock(&Giant); 107} 108 109static int 110g_disk_access(struct g_provider *pp, int r, int w, int e) 111{ 112 struct disk *dp; 113 int error; 114 115 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 116 pp->name, r, w, e); 117 g_topology_assert(); 118 dp = pp->geom->softc; 119 if (dp == NULL || dp->d_destroyed) { 120 /* 121 * Allow decreasing access count even if disk is not 122 * avaliable anymore. 123 */ 124 if (r <= 0 && w <= 0 && e <= 0) 125 return (0); 126 return (ENXIO); 127 } 128 r += pp->acr; 129 w += pp->acw; 130 e += pp->ace; 131 error = 0; 132 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 133 if (dp->d_open != NULL) { 134 g_disk_lock_giant(dp); 135 error = dp->d_open(dp);
| 38 39#include "opt_geom.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/sysctl.h> 45#include <sys/bio.h> 46#include <sys/conf.h> 47#include <sys/fcntl.h> 48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/devicestat.h> 51#include <machine/md_var.h> 52 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <geom/geom.h> 56#include <geom/geom_disk.h> 57#include <geom/geom_int.h> 58 59static struct mtx g_disk_done_mtx; 60 61static g_access_t g_disk_access; 62static g_init_t g_disk_init; 63static g_fini_t g_disk_fini; 64static g_start_t g_disk_start; 65static g_ioctl_t g_disk_ioctl; 66static g_dumpconf_t g_disk_dumpconf; 67 68static struct g_class g_disk_class = { 69 .name = "DISK", 70 .version = G_VERSION, 71 .init = g_disk_init, 72 .fini = g_disk_fini, 73 .start = g_disk_start, 74 .access = g_disk_access, 75 .ioctl = g_disk_ioctl, 76 .dumpconf = g_disk_dumpconf, 77}; 78 79static void 80g_disk_init(struct g_class *mp __unused) 81{ 82 83 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 84} 85 86static void 87g_disk_fini(struct g_class *mp __unused) 88{ 89 90 mtx_destroy(&g_disk_done_mtx); 91} 92 93DECLARE_GEOM_CLASS(g_disk_class, g_disk); 94 95static void __inline 96g_disk_lock_giant(struct disk *dp) 97{ 98 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 99 mtx_lock(&Giant); 100} 101 102static void __inline 103g_disk_unlock_giant(struct disk *dp) 104{ 105 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 106 mtx_unlock(&Giant); 107} 108 109static int 110g_disk_access(struct g_provider *pp, int r, int w, int e) 111{ 112 struct disk *dp; 113 int error; 114 115 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 116 pp->name, r, w, e); 117 g_topology_assert(); 118 dp = pp->geom->softc; 119 if (dp == NULL || dp->d_destroyed) { 120 /* 121 * Allow decreasing access count even if disk is not 122 * avaliable anymore. 123 */ 124 if (r <= 0 && w <= 0 && e <= 0) 125 return (0); 126 return (ENXIO); 127 } 128 r += pp->acr; 129 w += pp->acw; 130 e += pp->ace; 131 error = 0; 132 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 133 if (dp->d_open != NULL) { 134 g_disk_lock_giant(dp); 135 error = dp->d_open(dp);
|
137 printf("Opened disk %s -> %d\n", 138 pp->name, error); 139 g_disk_unlock_giant(dp); 140 } 141 pp->mediasize = dp->d_mediasize; 142 pp->sectorsize = dp->d_sectorsize; 143 dp->d_flags |= DISKFLAG_OPEN; 144 if (dp->d_maxsize == 0) { 145 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 146 dp->d_name, dp->d_unit); 147 dp->d_maxsize = DFLTPHYS; 148 } 149 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 150 if (dp->d_close != NULL) { 151 g_disk_lock_giant(dp); 152 error = dp->d_close(dp); 153 if (error != 0) 154 printf("Closed disk %s -> %d\n", 155 pp->name, error); 156 g_disk_unlock_giant(dp); 157 } 158 dp->d_flags &= ~DISKFLAG_OPEN; 159 } 160 return (error); 161} 162 163static void 164g_disk_kerneldump(struct bio *bp, struct disk *dp) 165{ 166 int error; 167 struct g_kerneldump *gkd; 168 struct dumperinfo di; 169 struct g_geom *gp; 170 171 gkd = (struct g_kerneldump*)bp->bio_data; 172 gp = bp->bio_to->geom; 173 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 174 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 175 if (dp->d_dump == NULL) { 176 g_io_deliver(bp, ENODEV); 177 return; 178 } 179 di.dumper = dp->d_dump; 180 di.priv = dp; 181 di.blocksize = dp->d_sectorsize; 182 di.mediaoffset = gkd->offset; 183 if ((gkd->offset + gkd->length) > dp->d_mediasize) 184 gkd->length = dp->d_mediasize - gkd->offset; 185 di.mediasize = gkd->length; 186 error = set_dumper(&di); 187 g_io_deliver(bp, error); 188} 189 190static void 191g_disk_done(struct bio *bp) 192{ 193 struct bio *bp2; 194 struct disk *dp; 195 196 /* See "notes" for why we need a mutex here */ 197 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 198 mtx_lock(&g_disk_done_mtx); 199 bp->bio_completed = bp->bio_length - bp->bio_resid; 200 201 bp2 = bp->bio_parent; 202 if (bp2->bio_error == 0) 203 bp2->bio_error = bp->bio_error; 204 bp2->bio_completed += bp->bio_completed; 205 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) && 206 (dp = bp2->bio_to->geom->softc)) { 207 devstat_end_transaction_bio(dp->d_devstat, bp); 208 } 209 g_destroy_bio(bp); 210 bp2->bio_inbed++; 211 if (bp2->bio_children == bp2->bio_inbed) { 212 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 213 g_io_deliver(bp2, bp2->bio_error); 214 } 215 mtx_unlock(&g_disk_done_mtx); 216} 217 218static int 219g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 220{ 221 struct g_geom *gp; 222 struct disk *dp; 223 int error; 224 225 gp = pp->geom; 226 dp = gp->softc; 227 228 if (dp->d_ioctl == NULL) 229 return (ENOIOCTL); 230 g_disk_lock_giant(dp); 231 error = dp->d_ioctl(dp, cmd, data, fflag, td); 232 g_disk_unlock_giant(dp); 233 return(error); 234} 235 236static void 237g_disk_start(struct bio *bp) 238{ 239 struct bio *bp2, *bp3; 240 struct disk *dp; 241 int error; 242 off_t off; 243 244 dp = bp->bio_to->geom->softc; 245 if (dp == NULL || dp->d_destroyed) { 246 g_io_deliver(bp, ENXIO); 247 return; 248 } 249 error = EJUSTRETURN; 250 switch(bp->bio_cmd) { 251 case BIO_DELETE: 252 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 253 error = 0; 254 break; 255 } 256 /* fall-through */ 257 case BIO_READ: 258 case BIO_WRITE: 259 off = 0; 260 bp3 = NULL; 261 bp2 = g_clone_bio(bp); 262 if (bp2 == NULL) { 263 error = ENOMEM; 264 break; 265 } 266 do { 267 bp2->bio_offset += off; 268 bp2->bio_length -= off; 269 bp2->bio_data += off; 270 if (bp2->bio_length > dp->d_maxsize) { 271 /* 272 * XXX: If we have a stripesize we should really 273 * use it here. 274 */ 275 bp2->bio_length = dp->d_maxsize; 276 off += dp->d_maxsize; 277 /* 278 * To avoid a race, we need to grab the next bio 279 * before we schedule this one. See "notes". 280 */ 281 bp3 = g_clone_bio(bp); 282 if (bp3 == NULL) 283 bp->bio_error = ENOMEM; 284 } 285 bp2->bio_done = g_disk_done; 286 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 287 bp2->bio_bcount = bp2->bio_length; 288 bp2->bio_disk = dp; 289 devstat_start_transaction_bio(dp->d_devstat, bp2); 290 g_disk_lock_giant(dp); 291 dp->d_strategy(bp2); 292 g_disk_unlock_giant(dp); 293 bp2 = bp3; 294 bp3 = NULL; 295 } while (bp2 != NULL); 296 break; 297 case BIO_GETATTR: 298 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 299 break; 300 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 301 break; 302 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 303 break; 304 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 305 g_disk_kerneldump(bp, dp); 306 else 307 error = ENOIOCTL; 308 break; 309 case BIO_FLUSH: 310 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)", 311 bp->bio_to->name); 312 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 313 g_io_deliver(bp, ENODEV); 314 return; 315 } 316 bp2 = g_clone_bio(bp); 317 if (bp2 == NULL) { 318 g_io_deliver(bp, ENOMEM); 319 return; 320 } 321 bp2->bio_done = g_disk_done; 322 bp2->bio_disk = dp; 323 g_disk_lock_giant(dp); 324 dp->d_strategy(bp2); 325 g_disk_unlock_giant(dp); 326 break; 327 default: 328 error = EOPNOTSUPP; 329 break; 330 } 331 if (error != EJUSTRETURN) 332 g_io_deliver(bp, error); 333 return; 334} 335 336static void 337g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 338{ 339 struct disk *dp; 340 341 dp = gp->softc; 342 if (dp == NULL) 343 return; 344 if (indent == NULL) { 345 sbuf_printf(sb, " hd %u", dp->d_fwheads); 346 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 347 return; 348 } 349 if (pp != NULL) { 350 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 351 indent, dp->d_fwheads); 352 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 353 indent, dp->d_fwsectors); 354 } 355} 356 357static void 358g_disk_create(void *arg, int flag) 359{ 360 struct g_geom *gp; 361 struct g_provider *pp; 362 struct disk *dp; 363 364 if (flag == EV_CANCEL) 365 return; 366 g_topology_assert(); 367 dp = arg; 368 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 369 gp->softc = dp; 370 pp = g_new_providerf(gp, "%s", gp->name); 371 pp->mediasize = dp->d_mediasize; 372 pp->sectorsize = dp->d_sectorsize; 373 if (dp->d_flags & DISKFLAG_CANDELETE) 374 pp->flags |= G_PF_CANDELETE; 375 pp->stripeoffset = dp->d_stripeoffset; 376 pp->stripesize = dp->d_stripesize; 377 if (bootverbose) 378 printf("GEOM: new disk %s\n", gp->name); 379 dp->d_geom = gp; 380 g_error_provider(pp, 0); 381} 382 383static void 384g_disk_destroy(void *ptr, int flag) 385{ 386 struct disk *dp; 387 struct g_geom *gp; 388 389 g_topology_assert(); 390 dp = ptr; 391 gp = dp->d_geom; 392 if (gp != NULL) { 393 gp->softc = NULL; 394 g_wither_geom(gp, ENXIO); 395 } 396 g_free(dp); 397} 398 399struct disk * 400disk_alloc() 401{ 402 struct disk *dp; 403 404 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO); 405 return (dp); 406} 407 408void 409disk_create(struct disk *dp, int version) 410{ 411 if (version != DISK_VERSION_00) { 412 printf("WARNING: Attempt to add disk %s%d %s", 413 dp->d_name, dp->d_unit, 414 " using incompatible ABI version of disk(9)\n"); 415 printf("WARNING: Ignoring disk %s%d\n", 416 dp->d_name, dp->d_unit); 417 return; 418 } 419 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 420 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 421 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 422 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 423 if (dp->d_devstat == NULL) 424 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 425 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 426 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 427 dp->d_geom = NULL; 428 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 429} 430 431void 432disk_destroy(struct disk *dp) 433{ 434 435 g_cancel_event(dp); 436 dp->d_destroyed = 1; 437 if (dp->d_devstat != NULL) 438 devstat_remove_entry(dp->d_devstat); 439 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 440} 441 442void 443disk_gone(struct disk *dp) 444{ 445 struct g_geom *gp; 446 struct g_provider *pp; 447 448 gp = dp->d_geom; 449 if (gp != NULL) 450 LIST_FOREACH(pp, &gp->provider, provider) 451 g_wither_provider(pp, ENXIO); 452} 453 454static void 455g_kern_disks(void *p, int flag __unused) 456{ 457 struct sbuf *sb; 458 struct g_geom *gp; 459 char *sp; 460 461 sb = p; 462 sp = ""; 463 g_topology_assert(); 464 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 465 sbuf_printf(sb, "%s%s", sp, gp->name); 466 sp = " "; 467 } 468 sbuf_finish(sb); 469} 470 471static int 472sysctl_disks(SYSCTL_HANDLER_ARGS) 473{ 474 int error; 475 struct sbuf *sb; 476 477 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 478 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 479 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 480 sbuf_delete(sb); 481 return error; 482} 483 484SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 485 sysctl_disks, "A", "names of available disks"); 486
| 137 printf("Opened disk %s -> %d\n", 138 pp->name, error); 139 g_disk_unlock_giant(dp); 140 } 141 pp->mediasize = dp->d_mediasize; 142 pp->sectorsize = dp->d_sectorsize; 143 dp->d_flags |= DISKFLAG_OPEN; 144 if (dp->d_maxsize == 0) { 145 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 146 dp->d_name, dp->d_unit); 147 dp->d_maxsize = DFLTPHYS; 148 } 149 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 150 if (dp->d_close != NULL) { 151 g_disk_lock_giant(dp); 152 error = dp->d_close(dp); 153 if (error != 0) 154 printf("Closed disk %s -> %d\n", 155 pp->name, error); 156 g_disk_unlock_giant(dp); 157 } 158 dp->d_flags &= ~DISKFLAG_OPEN; 159 } 160 return (error); 161} 162 163static void 164g_disk_kerneldump(struct bio *bp, struct disk *dp) 165{ 166 int error; 167 struct g_kerneldump *gkd; 168 struct dumperinfo di; 169 struct g_geom *gp; 170 171 gkd = (struct g_kerneldump*)bp->bio_data; 172 gp = bp->bio_to->geom; 173 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 174 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 175 if (dp->d_dump == NULL) { 176 g_io_deliver(bp, ENODEV); 177 return; 178 } 179 di.dumper = dp->d_dump; 180 di.priv = dp; 181 di.blocksize = dp->d_sectorsize; 182 di.mediaoffset = gkd->offset; 183 if ((gkd->offset + gkd->length) > dp->d_mediasize) 184 gkd->length = dp->d_mediasize - gkd->offset; 185 di.mediasize = gkd->length; 186 error = set_dumper(&di); 187 g_io_deliver(bp, error); 188} 189 190static void 191g_disk_done(struct bio *bp) 192{ 193 struct bio *bp2; 194 struct disk *dp; 195 196 /* See "notes" for why we need a mutex here */ 197 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 198 mtx_lock(&g_disk_done_mtx); 199 bp->bio_completed = bp->bio_length - bp->bio_resid; 200 201 bp2 = bp->bio_parent; 202 if (bp2->bio_error == 0) 203 bp2->bio_error = bp->bio_error; 204 bp2->bio_completed += bp->bio_completed; 205 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) && 206 (dp = bp2->bio_to->geom->softc)) { 207 devstat_end_transaction_bio(dp->d_devstat, bp); 208 } 209 g_destroy_bio(bp); 210 bp2->bio_inbed++; 211 if (bp2->bio_children == bp2->bio_inbed) { 212 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 213 g_io_deliver(bp2, bp2->bio_error); 214 } 215 mtx_unlock(&g_disk_done_mtx); 216} 217 218static int 219g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 220{ 221 struct g_geom *gp; 222 struct disk *dp; 223 int error; 224 225 gp = pp->geom; 226 dp = gp->softc; 227 228 if (dp->d_ioctl == NULL) 229 return (ENOIOCTL); 230 g_disk_lock_giant(dp); 231 error = dp->d_ioctl(dp, cmd, data, fflag, td); 232 g_disk_unlock_giant(dp); 233 return(error); 234} 235 236static void 237g_disk_start(struct bio *bp) 238{ 239 struct bio *bp2, *bp3; 240 struct disk *dp; 241 int error; 242 off_t off; 243 244 dp = bp->bio_to->geom->softc; 245 if (dp == NULL || dp->d_destroyed) { 246 g_io_deliver(bp, ENXIO); 247 return; 248 } 249 error = EJUSTRETURN; 250 switch(bp->bio_cmd) { 251 case BIO_DELETE: 252 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 253 error = 0; 254 break; 255 } 256 /* fall-through */ 257 case BIO_READ: 258 case BIO_WRITE: 259 off = 0; 260 bp3 = NULL; 261 bp2 = g_clone_bio(bp); 262 if (bp2 == NULL) { 263 error = ENOMEM; 264 break; 265 } 266 do { 267 bp2->bio_offset += off; 268 bp2->bio_length -= off; 269 bp2->bio_data += off; 270 if (bp2->bio_length > dp->d_maxsize) { 271 /* 272 * XXX: If we have a stripesize we should really 273 * use it here. 274 */ 275 bp2->bio_length = dp->d_maxsize; 276 off += dp->d_maxsize; 277 /* 278 * To avoid a race, we need to grab the next bio 279 * before we schedule this one. See "notes". 280 */ 281 bp3 = g_clone_bio(bp); 282 if (bp3 == NULL) 283 bp->bio_error = ENOMEM; 284 } 285 bp2->bio_done = g_disk_done; 286 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 287 bp2->bio_bcount = bp2->bio_length; 288 bp2->bio_disk = dp; 289 devstat_start_transaction_bio(dp->d_devstat, bp2); 290 g_disk_lock_giant(dp); 291 dp->d_strategy(bp2); 292 g_disk_unlock_giant(dp); 293 bp2 = bp3; 294 bp3 = NULL; 295 } while (bp2 != NULL); 296 break; 297 case BIO_GETATTR: 298 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 299 break; 300 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 301 break; 302 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 303 break; 304 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 305 g_disk_kerneldump(bp, dp); 306 else 307 error = ENOIOCTL; 308 break; 309 case BIO_FLUSH: 310 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)", 311 bp->bio_to->name); 312 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 313 g_io_deliver(bp, ENODEV); 314 return; 315 } 316 bp2 = g_clone_bio(bp); 317 if (bp2 == NULL) { 318 g_io_deliver(bp, ENOMEM); 319 return; 320 } 321 bp2->bio_done = g_disk_done; 322 bp2->bio_disk = dp; 323 g_disk_lock_giant(dp); 324 dp->d_strategy(bp2); 325 g_disk_unlock_giant(dp); 326 break; 327 default: 328 error = EOPNOTSUPP; 329 break; 330 } 331 if (error != EJUSTRETURN) 332 g_io_deliver(bp, error); 333 return; 334} 335 336static void 337g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 338{ 339 struct disk *dp; 340 341 dp = gp->softc; 342 if (dp == NULL) 343 return; 344 if (indent == NULL) { 345 sbuf_printf(sb, " hd %u", dp->d_fwheads); 346 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 347 return; 348 } 349 if (pp != NULL) { 350 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 351 indent, dp->d_fwheads); 352 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 353 indent, dp->d_fwsectors); 354 } 355} 356 357static void 358g_disk_create(void *arg, int flag) 359{ 360 struct g_geom *gp; 361 struct g_provider *pp; 362 struct disk *dp; 363 364 if (flag == EV_CANCEL) 365 return; 366 g_topology_assert(); 367 dp = arg; 368 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 369 gp->softc = dp; 370 pp = g_new_providerf(gp, "%s", gp->name); 371 pp->mediasize = dp->d_mediasize; 372 pp->sectorsize = dp->d_sectorsize; 373 if (dp->d_flags & DISKFLAG_CANDELETE) 374 pp->flags |= G_PF_CANDELETE; 375 pp->stripeoffset = dp->d_stripeoffset; 376 pp->stripesize = dp->d_stripesize; 377 if (bootverbose) 378 printf("GEOM: new disk %s\n", gp->name); 379 dp->d_geom = gp; 380 g_error_provider(pp, 0); 381} 382 383static void 384g_disk_destroy(void *ptr, int flag) 385{ 386 struct disk *dp; 387 struct g_geom *gp; 388 389 g_topology_assert(); 390 dp = ptr; 391 gp = dp->d_geom; 392 if (gp != NULL) { 393 gp->softc = NULL; 394 g_wither_geom(gp, ENXIO); 395 } 396 g_free(dp); 397} 398 399struct disk * 400disk_alloc() 401{ 402 struct disk *dp; 403 404 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO); 405 return (dp); 406} 407 408void 409disk_create(struct disk *dp, int version) 410{ 411 if (version != DISK_VERSION_00) { 412 printf("WARNING: Attempt to add disk %s%d %s", 413 dp->d_name, dp->d_unit, 414 " using incompatible ABI version of disk(9)\n"); 415 printf("WARNING: Ignoring disk %s%d\n", 416 dp->d_name, dp->d_unit); 417 return; 418 } 419 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 420 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 421 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 422 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 423 if (dp->d_devstat == NULL) 424 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 425 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 426 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 427 dp->d_geom = NULL; 428 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 429} 430 431void 432disk_destroy(struct disk *dp) 433{ 434 435 g_cancel_event(dp); 436 dp->d_destroyed = 1; 437 if (dp->d_devstat != NULL) 438 devstat_remove_entry(dp->d_devstat); 439 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 440} 441 442void 443disk_gone(struct disk *dp) 444{ 445 struct g_geom *gp; 446 struct g_provider *pp; 447 448 gp = dp->d_geom; 449 if (gp != NULL) 450 LIST_FOREACH(pp, &gp->provider, provider) 451 g_wither_provider(pp, ENXIO); 452} 453 454static void 455g_kern_disks(void *p, int flag __unused) 456{ 457 struct sbuf *sb; 458 struct g_geom *gp; 459 char *sp; 460 461 sb = p; 462 sp = ""; 463 g_topology_assert(); 464 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 465 sbuf_printf(sb, "%s%s", sp, gp->name); 466 sp = " "; 467 } 468 sbuf_finish(sb); 469} 470 471static int 472sysctl_disks(SYSCTL_HANDLER_ARGS) 473{ 474 int error; 475 struct sbuf *sb; 476 477 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 478 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 479 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 480 sbuf_delete(sb); 481 return error; 482} 483 484SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 485 sysctl_disks, "A", "names of available disks"); 486
|