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