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 *
|
77static void __inline 78g_disk_lock_giant(struct disk *dp) 79{ 80 if (dp->d_flags & DISKFLAG_NOGIANT) 81 return; 82 mtx_lock(&Giant); 83} 84 85static void __inline 86g_disk_unlock_giant(struct disk *dp) 87{ 88 if (dp->d_flags & DISKFLAG_NOGIANT) 89 return; 90 mtx_unlock(&Giant); 91} 92 93static int 94g_disk_access(struct g_provider *pp, int r, int w, int e) 95{ 96 struct disk *dp; 97 int error; 98 99 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 100 pp->name, r, w, e); 101 g_topology_assert(); 102 r += pp->acr; 103 w += pp->acw; 104 e += pp->ace; 105 dp = pp->geom->softc; 106 if (dp == NULL) 107 return (ENXIO); 108 error = 0; 109 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 110 if (dp->d_open != NULL) { 111 g_disk_lock_giant(dp); 112 error = dp->d_open(dp); 113 if (error != 0) 114 printf("Opened disk %s -> %d\n", 115 pp->name, error); 116 g_disk_unlock_giant(dp); 117 } 118 pp->mediasize = dp->d_mediasize; 119 pp->sectorsize = dp->d_sectorsize; 120 dp->d_flags |= DISKFLAG_OPEN; 121 if (dp->d_maxsize == 0) { 122 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 123 dp->d_name, dp->d_unit); 124 dp->d_maxsize = DFLTPHYS; 125 } 126 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 127 if (dp->d_close != NULL) { 128 g_disk_lock_giant(dp); 129 error = dp->d_close(dp); 130 if (error != 0) 131 printf("Closed disk %s -> %d\n", 132 pp->name, error); 133 g_disk_unlock_giant(dp); 134 } 135 dp->d_flags &= ~DISKFLAG_OPEN; 136 } 137 return (error); 138} 139 140static void 141g_disk_kerneldump(struct bio *bp, struct disk *dp) 142{ 143 int error; 144 struct g_kerneldump *gkd; 145 struct dumperinfo di; 146 struct g_geom *gp; 147 148 gkd = (struct g_kerneldump*)bp->bio_data; 149 gp = bp->bio_to->geom; 150 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 151 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 152 di.dumper = dp->d_dump; 153 di.priv = dp; 154 di.blocksize = dp->d_sectorsize; 155 di.mediaoffset = gkd->offset; 156 di.mediasize = gkd->length; 157 error = set_dumper(&di); 158 g_io_deliver(bp, error); 159} 160 161static void 162g_disk_done(struct bio *bp) 163{ 164 struct bio *bp2; 165 struct disk *dp; 166 167 /* See "notes" for why we need a mutex here */ 168 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 169 mtx_lock(&g_disk_done_mtx); 170 bp->bio_completed = bp->bio_length - bp->bio_resid; 171 172 bp2 = bp->bio_parent; 173 dp = bp2->bio_to->geom->softc; 174 if (bp2->bio_error == 0) 175 bp2->bio_error = bp->bio_error; 176 bp2->bio_completed += bp->bio_completed; 177 g_destroy_bio(bp); 178 bp2->bio_inbed++; 179 if (bp2->bio_children == bp2->bio_inbed) { 180 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 181 devstat_end_transaction_bio(dp->d_devstat, bp2); 182 g_io_deliver(bp2, bp2->bio_error); 183 } 184 mtx_unlock(&g_disk_done_mtx); 185} 186 187static void 188g_disk_start(struct bio *bp) 189{ 190 struct bio *bp2, *bp3; 191 struct disk *dp; 192 struct g_ioctl *gio; 193 int error; 194 off_t off; 195 196 dp = bp->bio_to->geom->softc; 197 if (dp == NULL) 198 g_io_deliver(bp, ENXIO); 199 error = EJUSTRETURN; 200 switch(bp->bio_cmd) { 201 case BIO_DELETE: 202 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 203 error = 0; 204 break; 205 } 206 /* fall-through */ 207 case BIO_READ: 208 case BIO_WRITE: 209 off = 0; 210 bp3 = NULL; 211 bp2 = g_clone_bio(bp); 212 if (bp2 == NULL) { 213 error = ENOMEM; 214 break; 215 } 216 devstat_start_transaction_bio(dp->d_devstat, bp); 217 do { 218 bp2->bio_offset += off; 219 bp2->bio_length -= off; 220 bp2->bio_data += off; 221 if (bp2->bio_length > dp->d_maxsize) { 222 /* 223 * XXX: If we have a stripesize we should really 224 * use it here. 225 */ 226 bp2->bio_length = dp->d_maxsize; 227 off += dp->d_maxsize; 228 /* 229 * To avoid a race, we need to grab the next bio 230 * before we schedule this one. See "notes". 231 */ 232 bp3 = g_clone_bio(bp); 233 if (bp3 == NULL) 234 bp->bio_error = ENOMEM; 235 } 236 bp2->bio_done = g_disk_done; 237 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 238 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 239 bp2->bio_bcount = bp2->bio_length; 240 bp2->bio_disk = dp; 241 g_disk_lock_giant(dp); 242 dp->d_strategy(bp2); 243 g_disk_unlock_giant(dp); 244 bp2 = bp3; 245 bp3 = NULL; 246 } while (bp2 != NULL); 247 break; 248 case BIO_GETATTR: 249 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 250 break; 251 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 252 break; 253 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 254 break; 255 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 256 g_disk_kerneldump(bp, dp); 257 else if ((g_debugflags & G_F_DISKIOCTL) && 258 (dp->d_ioctl != NULL) && 259 !strcmp(bp->bio_attribute, "GEOM::ioctl") && 260 bp->bio_length == sizeof *gio) { 261 gio = (struct g_ioctl *)bp->bio_data; 262 gio->dev = dp; 263 gio->func = (d_ioctl_t *)(dp->d_ioctl); 264 error = EDIRIOCTL; 265 } else 266 error = ENOIOCTL; 267 break; 268 default: 269 error = EOPNOTSUPP; 270 break; 271 } 272 if (error != EJUSTRETURN) 273 g_io_deliver(bp, error); 274 return; 275} 276 277static void 278g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 279{ 280 struct disk *dp; 281 282 dp = gp->softc; 283 if (indent == NULL) { 284 sbuf_printf(sb, " hd %u", dp->d_fwheads); 285 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 286 return; 287 } 288 if (pp != NULL) { 289 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 290 indent, dp->d_fwheads); 291 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 292 indent, dp->d_fwsectors); 293 } 294} 295 296static void 297g_disk_create(void *arg, int flag) 298{ 299 struct g_geom *gp; 300 struct g_provider *pp; 301 struct disk *dp; 302 303 if (flag == EV_CANCEL) 304 return; 305 g_topology_assert(); 306 dp = arg; 307 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 308 gp->start = g_disk_start; 309 gp->access = g_disk_access; 310 gp->softc = dp; 311 gp->dumpconf = g_disk_dumpconf; 312 pp = g_new_providerf(gp, "%s", gp->name); 313 pp->mediasize = dp->d_mediasize; 314 pp->sectorsize = dp->d_sectorsize; 315 if (dp->d_flags & DISKFLAG_CANDELETE) 316 pp->flags |= G_PF_CANDELETE; 317 pp->stripeoffset = dp->d_stripeoffset; 318 pp->stripesize = dp->d_stripesize; 319 if (bootverbose) 320 printf("GEOM: new disk %s\n", gp->name); 321 dp->d_geom = gp; 322 g_error_provider(pp, 0); 323} 324 325 326 327void 328disk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused) 329{ 330 331 dp->d_unit = unit; 332 dp->d_flags = flags; 333 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 334 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 335 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 336 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 337 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 338 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 339 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 340 dp->d_geom = NULL; 341 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 342} 343 344/* 345 * XXX: There is a race if disk_destroy() is called while the g_disk_create() 346 * XXX: event is running. I belive the current result is that disk_destroy() 347 * XXX: actually doesn't do anything. Considering that the driver owns the 348 * XXX: struct disk and is likely to free it in a few moments, this can 349 * XXX: hardly be said to be optimal. To what extent we can sleep in 350 * XXX: disk_create() and disk_destroy() is currently undefined (but generally 351 * XXX: undesirable) so any solution seems to involve an intrusive decision. 352 */ 353 354static void 355disk_destroy_event(void *ptr, int flag) 356{ 357 358 g_topology_assert(); 359 g_wither_geom(ptr, ENXIO); 360} 361 362void 363disk_destroy(struct disk *dp) 364{ 365 struct g_geom *gp; 366 367 g_cancel_event(dp); 368 gp = dp->d_geom; 369 if (gp == NULL) 370 return; 371 gp->softc = NULL; 372 devstat_remove_entry(dp->d_devstat); 373 g_post_event(disk_destroy_event, gp, M_WAITOK, NULL, NULL); 374} 375 376static void 377g_kern_disks(void *p, int flag __unused) 378{ 379 struct sbuf *sb; 380 struct g_geom *gp; 381 char *sp; 382 383 sb = p; 384 sp = ""; 385 g_topology_assert(); 386 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 387 sbuf_printf(sb, "%s%s", sp, gp->name); 388 sp = " "; 389 } 390 sbuf_finish(sb); 391} 392 393static int 394sysctl_disks(SYSCTL_HANDLER_ARGS) 395{ 396 int error; 397 struct sbuf *sb; 398 399 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 400 sbuf_clear(sb); 401 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 402 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 403 sbuf_delete(sb); 404 return error; 405} 406 407SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 408 sysctl_disks, "A", "names of available disks"); 409
| 86static void __inline 87g_disk_lock_giant(struct disk *dp) 88{ 89 if (dp->d_flags & DISKFLAG_NOGIANT) 90 return; 91 mtx_lock(&Giant); 92} 93 94static void __inline 95g_disk_unlock_giant(struct disk *dp) 96{ 97 if (dp->d_flags & DISKFLAG_NOGIANT) 98 return; 99 mtx_unlock(&Giant); 100} 101 102static int 103g_disk_access(struct g_provider *pp, int r, int w, int e) 104{ 105 struct disk *dp; 106 int error; 107 108 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 109 pp->name, r, w, e); 110 g_topology_assert(); 111 r += pp->acr; 112 w += pp->acw; 113 e += pp->ace; 114 dp = pp->geom->softc; 115 if (dp == NULL) 116 return (ENXIO); 117 error = 0; 118 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 119 if (dp->d_open != NULL) { 120 g_disk_lock_giant(dp); 121 error = dp->d_open(dp); 122 if (error != 0) 123 printf("Opened disk %s -> %d\n", 124 pp->name, error); 125 g_disk_unlock_giant(dp); 126 } 127 pp->mediasize = dp->d_mediasize; 128 pp->sectorsize = dp->d_sectorsize; 129 dp->d_flags |= DISKFLAG_OPEN; 130 if (dp->d_maxsize == 0) { 131 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 132 dp->d_name, dp->d_unit); 133 dp->d_maxsize = DFLTPHYS; 134 } 135 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 136 if (dp->d_close != NULL) { 137 g_disk_lock_giant(dp); 138 error = dp->d_close(dp); 139 if (error != 0) 140 printf("Closed disk %s -> %d\n", 141 pp->name, error); 142 g_disk_unlock_giant(dp); 143 } 144 dp->d_flags &= ~DISKFLAG_OPEN; 145 } 146 return (error); 147} 148 149static void 150g_disk_kerneldump(struct bio *bp, struct disk *dp) 151{ 152 int error; 153 struct g_kerneldump *gkd; 154 struct dumperinfo di; 155 struct g_geom *gp; 156 157 gkd = (struct g_kerneldump*)bp->bio_data; 158 gp = bp->bio_to->geom; 159 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 160 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 161 di.dumper = dp->d_dump; 162 di.priv = dp; 163 di.blocksize = dp->d_sectorsize; 164 di.mediaoffset = gkd->offset; 165 di.mediasize = gkd->length; 166 error = set_dumper(&di); 167 g_io_deliver(bp, error); 168} 169 170static void 171g_disk_done(struct bio *bp) 172{ 173 struct bio *bp2; 174 struct disk *dp; 175 176 /* See "notes" for why we need a mutex here */ 177 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 178 mtx_lock(&g_disk_done_mtx); 179 bp->bio_completed = bp->bio_length - bp->bio_resid; 180 181 bp2 = bp->bio_parent; 182 dp = bp2->bio_to->geom->softc; 183 if (bp2->bio_error == 0) 184 bp2->bio_error = bp->bio_error; 185 bp2->bio_completed += bp->bio_completed; 186 g_destroy_bio(bp); 187 bp2->bio_inbed++; 188 if (bp2->bio_children == bp2->bio_inbed) { 189 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 190 devstat_end_transaction_bio(dp->d_devstat, bp2); 191 g_io_deliver(bp2, bp2->bio_error); 192 } 193 mtx_unlock(&g_disk_done_mtx); 194} 195 196static void 197g_disk_start(struct bio *bp) 198{ 199 struct bio *bp2, *bp3; 200 struct disk *dp; 201 struct g_ioctl *gio; 202 int error; 203 off_t off; 204 205 dp = bp->bio_to->geom->softc; 206 if (dp == NULL) 207 g_io_deliver(bp, ENXIO); 208 error = EJUSTRETURN; 209 switch(bp->bio_cmd) { 210 case BIO_DELETE: 211 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 212 error = 0; 213 break; 214 } 215 /* fall-through */ 216 case BIO_READ: 217 case BIO_WRITE: 218 off = 0; 219 bp3 = NULL; 220 bp2 = g_clone_bio(bp); 221 if (bp2 == NULL) { 222 error = ENOMEM; 223 break; 224 } 225 devstat_start_transaction_bio(dp->d_devstat, bp); 226 do { 227 bp2->bio_offset += off; 228 bp2->bio_length -= off; 229 bp2->bio_data += off; 230 if (bp2->bio_length > dp->d_maxsize) { 231 /* 232 * XXX: If we have a stripesize we should really 233 * use it here. 234 */ 235 bp2->bio_length = dp->d_maxsize; 236 off += dp->d_maxsize; 237 /* 238 * To avoid a race, we need to grab the next bio 239 * before we schedule this one. See "notes". 240 */ 241 bp3 = g_clone_bio(bp); 242 if (bp3 == NULL) 243 bp->bio_error = ENOMEM; 244 } 245 bp2->bio_done = g_disk_done; 246 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 247 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 248 bp2->bio_bcount = bp2->bio_length; 249 bp2->bio_disk = dp; 250 g_disk_lock_giant(dp); 251 dp->d_strategy(bp2); 252 g_disk_unlock_giant(dp); 253 bp2 = bp3; 254 bp3 = NULL; 255 } while (bp2 != NULL); 256 break; 257 case BIO_GETATTR: 258 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 259 break; 260 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 261 break; 262 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 263 break; 264 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 265 g_disk_kerneldump(bp, dp); 266 else if ((g_debugflags & G_F_DISKIOCTL) && 267 (dp->d_ioctl != NULL) && 268 !strcmp(bp->bio_attribute, "GEOM::ioctl") && 269 bp->bio_length == sizeof *gio) { 270 gio = (struct g_ioctl *)bp->bio_data; 271 gio->dev = dp; 272 gio->func = (d_ioctl_t *)(dp->d_ioctl); 273 error = EDIRIOCTL; 274 } else 275 error = ENOIOCTL; 276 break; 277 default: 278 error = EOPNOTSUPP; 279 break; 280 } 281 if (error != EJUSTRETURN) 282 g_io_deliver(bp, error); 283 return; 284} 285 286static void 287g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 288{ 289 struct disk *dp; 290 291 dp = gp->softc; 292 if (indent == NULL) { 293 sbuf_printf(sb, " hd %u", dp->d_fwheads); 294 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 295 return; 296 } 297 if (pp != NULL) { 298 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 299 indent, dp->d_fwheads); 300 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 301 indent, dp->d_fwsectors); 302 } 303} 304 305static void 306g_disk_create(void *arg, int flag) 307{ 308 struct g_geom *gp; 309 struct g_provider *pp; 310 struct disk *dp; 311 312 if (flag == EV_CANCEL) 313 return; 314 g_topology_assert(); 315 dp = arg; 316 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 317 gp->start = g_disk_start; 318 gp->access = g_disk_access; 319 gp->softc = dp; 320 gp->dumpconf = g_disk_dumpconf; 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 if (bootverbose) 329 printf("GEOM: new disk %s\n", gp->name); 330 dp->d_geom = gp; 331 g_error_provider(pp, 0); 332} 333 334 335 336void 337disk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused) 338{ 339 340 dp->d_unit = unit; 341 dp->d_flags = flags; 342 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 343 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 344 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 345 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 346 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 347 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 348 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 349 dp->d_geom = NULL; 350 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 351} 352 353/* 354 * XXX: There is a race if disk_destroy() is called while the g_disk_create() 355 * XXX: event is running. I belive the current result is that disk_destroy() 356 * XXX: actually doesn't do anything. Considering that the driver owns the 357 * XXX: struct disk and is likely to free it in a few moments, this can 358 * XXX: hardly be said to be optimal. To what extent we can sleep in 359 * XXX: disk_create() and disk_destroy() is currently undefined (but generally 360 * XXX: undesirable) so any solution seems to involve an intrusive decision. 361 */ 362 363static void 364disk_destroy_event(void *ptr, int flag) 365{ 366 367 g_topology_assert(); 368 g_wither_geom(ptr, ENXIO); 369} 370 371void 372disk_destroy(struct disk *dp) 373{ 374 struct g_geom *gp; 375 376 g_cancel_event(dp); 377 gp = dp->d_geom; 378 if (gp == NULL) 379 return; 380 gp->softc = NULL; 381 devstat_remove_entry(dp->d_devstat); 382 g_post_event(disk_destroy_event, gp, M_WAITOK, NULL, NULL); 383} 384 385static void 386g_kern_disks(void *p, int flag __unused) 387{ 388 struct sbuf *sb; 389 struct g_geom *gp; 390 char *sp; 391 392 sb = p; 393 sp = ""; 394 g_topology_assert(); 395 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 396 sbuf_printf(sb, "%s%s", sp, gp->name); 397 sp = " "; 398 } 399 sbuf_finish(sb); 400} 401 402static int 403sysctl_disks(SYSCTL_HANDLER_ARGS) 404{ 405 int error; 406 struct sbuf *sb; 407 408 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 409 sbuf_clear(sb); 410 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 411 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 412 sbuf_delete(sb); 413 return error; 414} 415 416SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 417 sysctl_disks, "A", "names of available disks"); 418
|