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>
37__FBSDID("$FreeBSD: head/sys/geom/geom_disk.c 240629 2012-09-18 07:57:34Z avg $");
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/ctype.h>
47#include <sys/fcntl.h>
48#include <sys/malloc.h>
49#include <sys/sbuf.h>
50#include <sys/sysctl.h>
51#include <sys/taskqueue.h>
52#include <sys/devicestat.h>
53#include <machine/md_var.h>
54
55#include <sys/lock.h>
56#include <sys/mutex.h>
57#include <geom/geom.h>
58#include <geom/geom_disk.h>
59#include <geom/geom_int.h>
60
61#include <dev/led/led.h>
62
63struct g_disk_softc {
64 struct disk *dp;
65 struct sysctl_ctx_list sysctl_ctx;
66 struct sysctl_oid *sysctl_tree;
67 char led[64];
68 uint32_t state;
69 struct task resize_task;
70};
71
72static struct mtx g_disk_done_mtx;
73
74static g_access_t g_disk_access;
75static g_init_t g_disk_init;
76static g_fini_t g_disk_fini;
77static g_start_t g_disk_start;
78static g_ioctl_t g_disk_ioctl;
79static g_dumpconf_t g_disk_dumpconf;
80static g_provgone_t g_disk_providergone;
81
82static struct g_class g_disk_class = {
83 .name = "DISK",
84 .version = G_VERSION,
85 .init = g_disk_init,
86 .fini = g_disk_fini,
87 .start = g_disk_start,
88 .access = g_disk_access,
89 .ioctl = g_disk_ioctl,
90 .providergone = g_disk_providergone,
91 .dumpconf = g_disk_dumpconf,
92};
93
94SYSCTL_DECL(_kern_geom);
95static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
96 "GEOM_DISK stuff");
97
98static void
99g_disk_init(struct g_class *mp __unused)
100{
101
102 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
103}
104
105static void
106g_disk_fini(struct g_class *mp __unused)
107{
108
109 mtx_destroy(&g_disk_done_mtx);
110}
111
112DECLARE_GEOM_CLASS(g_disk_class, g_disk);
113
114static void __inline
115g_disk_lock_giant(struct disk *dp)
116{
117
118 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
119 mtx_lock(&Giant);
120}
121
122static void __inline
123g_disk_unlock_giant(struct disk *dp)
124{
125
126 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
127 mtx_unlock(&Giant);
128}
129
130static int
131g_disk_access(struct g_provider *pp, int r, int w, int e)
132{
133 struct disk *dp;
134 struct g_disk_softc *sc;
135 int error;
136
137 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
138 pp->name, r, w, e);
139 g_topology_assert();
140 sc = pp->geom->softc;
141 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
142 /*
143 * Allow decreasing access count even if disk is not
144 * avaliable anymore.
145 */
146 if (r <= 0 && w <= 0 && e <= 0)
147 return (0);
148 return (ENXIO);
149 }
150 r += pp->acr;
151 w += pp->acw;
152 e += pp->ace;
153 error = 0;
154 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
155 if (dp->d_open != NULL) {
156 g_disk_lock_giant(dp);
157 error = dp->d_open(dp);
158 if (bootverbose && error != 0)
159 printf("Opened disk %s -> %d\n",
160 pp->name, error);
161 g_disk_unlock_giant(dp);
162 }
163 pp->mediasize = dp->d_mediasize;
164 pp->sectorsize = dp->d_sectorsize;
165 pp->stripeoffset = dp->d_stripeoffset;
166 pp->stripesize = dp->d_stripesize;
167 dp->d_flags |= DISKFLAG_OPEN;
168 if (dp->d_maxsize == 0) {
169 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
170 dp->d_name, dp->d_unit);
171 dp->d_maxsize = DFLTPHYS;
172 }
173 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
174 if (dp->d_close != NULL) {
175 g_disk_lock_giant(dp);
176 error = dp->d_close(dp);
177 if (error != 0)
178 printf("Closed disk %s -> %d\n",
179 pp->name, error);
180 g_disk_unlock_giant(dp);
181 }
182 sc->state = G_STATE_ACTIVE;
183 if (sc->led[0] != 0)
184 led_set(sc->led, "0");
185 dp->d_flags &= ~DISKFLAG_OPEN;
186 }
187 return (error);
188}
189
190static void
191g_disk_kerneldump(struct bio *bp, struct disk *dp)
192{
193 struct g_kerneldump *gkd;
194 struct g_geom *gp;
195
196 gkd = (struct g_kerneldump*)bp->bio_data;
197 gp = bp->bio_to->geom;
198 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
199 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
200 if (dp->d_dump == NULL) {
201 g_io_deliver(bp, ENODEV);
202 return;
203 }
204 gkd->di.dumper = dp->d_dump;
205 gkd->di.priv = dp;
206 gkd->di.blocksize = dp->d_sectorsize;
207 gkd->di.maxiosize = dp->d_maxsize;
208 gkd->di.mediaoffset = gkd->offset;
209 if ((gkd->offset + gkd->length) > dp->d_mediasize)
210 gkd->length = dp->d_mediasize - gkd->offset;
211 gkd->di.mediasize = gkd->length;
212 g_io_deliver(bp, 0);
213}
214
215static void
216g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
217{
218 const char *cmd;
219
220 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
221 if (sc->led[0] != 0) {
222 switch (sc->state) {
223 case G_STATE_FAILED:
224 cmd = "1";
225 break;
226 case G_STATE_REBUILD:
227 cmd = "f5";
228 break;
229 case G_STATE_RESYNC:
230 cmd = "f1";
231 break;
232 default:
233 cmd = "0";
234 break;
235 }
236 led_set(sc->led, cmd);
237 }
238 g_io_deliver(bp, 0);
239}
240
241static void
242g_disk_done(struct bio *bp)
243{
244 struct bio *bp2;
245 struct disk *dp;
246 struct g_disk_softc *sc;
247
248 /* See "notes" for why we need a mutex here */
249 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
250 mtx_lock(&g_disk_done_mtx);
251 bp->bio_completed = bp->bio_length - bp->bio_resid;
252
253 bp2 = bp->bio_parent;
254 if (bp2->bio_error == 0)
255 bp2->bio_error = bp->bio_error;
256 bp2->bio_completed += bp->bio_completed;
257 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0 &&
258 (sc = bp2->bio_to->geom->softc) != NULL &&
259 (dp = sc->dp) != NULL) {
260 devstat_end_transaction_bio(dp->d_devstat, bp);
261 }
262 g_destroy_bio(bp);
263 bp2->bio_inbed++;
264 if (bp2->bio_children == bp2->bio_inbed) {
265 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
266 g_io_deliver(bp2, bp2->bio_error);
267 }
268 mtx_unlock(&g_disk_done_mtx);
269}
270
271static int
272g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
273{
274 struct g_geom *gp;
275 struct disk *dp;
276 struct g_disk_softc *sc;
277 int error;
278
279 gp = pp->geom;
280 sc = gp->softc;
281 dp = sc->dp;
282
283 if (dp->d_ioctl == NULL)
284 return (ENOIOCTL);
285 g_disk_lock_giant(dp);
286 error = dp->d_ioctl(dp, cmd, data, fflag, td);
287 g_disk_unlock_giant(dp);
288 return (error);
289}
290
291static void
292g_disk_start(struct bio *bp)
293{
294 struct bio *bp2, *bp3;
295 struct disk *dp;
296 struct g_disk_softc *sc;
297 int error;
298 off_t off;
299
300 sc = bp->bio_to->geom->softc;
301 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
302 g_io_deliver(bp, ENXIO);
303 return;
304 }
305 error = EJUSTRETURN;
306 switch(bp->bio_cmd) {
307 case BIO_DELETE:
308 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
309 error = EOPNOTSUPP;
310 break;
311 }
312 /* fall-through */
313 case BIO_READ:
314 case BIO_WRITE:
315 off = 0;
316 bp3 = NULL;
317 bp2 = g_clone_bio(bp);
318 if (bp2 == NULL) {
319 error = ENOMEM;
320 break;
321 }
322 do {
323 bp2->bio_offset += off;
324 bp2->bio_length -= off;
325 bp2->bio_data += off;
326 if (bp2->bio_length > dp->d_maxsize) {
327 /*
328 * XXX: If we have a stripesize we should really
329 * use it here.
330 */
331 bp2->bio_length = dp->d_maxsize;
332 off += dp->d_maxsize;
333 /*
334 * To avoid a race, we need to grab the next bio
335 * before we schedule this one. See "notes".
336 */
337 bp3 = g_clone_bio(bp);
338 if (bp3 == NULL)
339 bp->bio_error = ENOMEM;
340 }
341 bp2->bio_done = g_disk_done;
342 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
343 bp2->bio_bcount = bp2->bio_length;
344 bp2->bio_disk = dp;
345 devstat_start_transaction_bio(dp->d_devstat, bp2);
346 g_disk_lock_giant(dp);
347 dp->d_strategy(bp2);
348 g_disk_unlock_giant(dp);
349 bp2 = bp3;
350 bp3 = NULL;
351 } while (bp2 != NULL);
352 break;
353 case BIO_GETATTR:
354 /* Give the driver a chance to override */
355 if (dp->d_getattr != NULL) {
356 if (bp->bio_disk == NULL)
357 bp->bio_disk = dp;
358 error = dp->d_getattr(bp);
359 if (error != -1)
360 break;
361 error = EJUSTRETURN;
362 }
363 if (g_handleattr_int(bp, "GEOM::candelete",
364 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
365 break;
366 else if (g_handleattr_int(bp, "GEOM::fwsectors",
367 dp->d_fwsectors))
368 break;
369 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
370 break;
371 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
372 break;
373 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
374 break;
375 else if (g_handleattr(bp, "GEOM::hba_vendor",
376 &dp->d_hba_vendor, 2))
377 break;
378 else if (g_handleattr(bp, "GEOM::hba_device",
379 &dp->d_hba_device, 2))
380 break;
381 else if (g_handleattr(bp, "GEOM::hba_subvendor",
382 &dp->d_hba_subvendor, 2))
383 break;
384 else if (g_handleattr(bp, "GEOM::hba_subdevice",
385 &dp->d_hba_subdevice, 2))
386 break;
387 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
388 g_disk_kerneldump(bp, dp);
389 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
390 g_disk_setstate(bp, sc);
391 else
392 error = ENOIOCTL;
393 break;
394 case BIO_FLUSH:
395 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
396 bp->bio_to->name);
397 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
398 error = EOPNOTSUPP;
399 break;
400 }
401 bp2 = g_clone_bio(bp);
402 if (bp2 == NULL) {
403 g_io_deliver(bp, ENOMEM);
404 return;
405 }
406 bp2->bio_done = g_disk_done;
407 bp2->bio_disk = dp;
408 g_disk_lock_giant(dp);
409 dp->d_strategy(bp2);
410 g_disk_unlock_giant(dp);
411 break;
412 default:
413 error = EOPNOTSUPP;
414 break;
415 }
416 if (error != EJUSTRETURN)
417 g_io_deliver(bp, error);
418 return;
419}
420
421static void
422g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
423{
424 struct disk *dp;
425 struct g_disk_softc *sc;
426
427 sc = gp->softc;
428 if (sc == NULL || (dp = sc->dp) == NULL)
429 return;
430 if (indent == NULL) {
431 sbuf_printf(sb, " hd %u", dp->d_fwheads);
432 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
433 return;
434 }
435 if (pp != NULL) {
436 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
437 indent, dp->d_fwheads);
438 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
439 indent, dp->d_fwsectors);
440 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident);
441 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr);
442 }
443}
444
445static void
446g_disk_resize_task(void *context, int pending)
447{
448 struct g_geom *gp;
449 struct g_provider *pp;
450 struct disk *dp;
451 struct g_disk_softc *sc;
452
453 sc = (struct g_disk_softc *)context;
454 dp = sc->dp;
455 gp = dp->d_geom;
456
457 LIST_FOREACH(pp, &gp->provider, provider) {
458 if (pp->sectorsize != 0 &&
459 pp->sectorsize != dp->d_sectorsize)
460 g_wither_provider(pp, ENXIO);
461 else
462 g_resize_provider(pp, dp->d_mediasize);
463 }
464}
465
466static void
467g_disk_create(void *arg, int flag)
468{
469 struct g_geom *gp;
470 struct g_provider *pp;
471 struct disk *dp;
472 struct g_disk_softc *sc;
473 char tmpstr[80];
474
475 if (flag == EV_CANCEL)
476 return;
477 g_topology_assert();
478 dp = arg;
479 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
480 sc->dp = dp;
481 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
482 gp->softc = sc;
483 pp = g_new_providerf(gp, "%s", gp->name);
484 pp->mediasize = dp->d_mediasize;
485 pp->sectorsize = dp->d_sectorsize;
486 pp->stripeoffset = dp->d_stripeoffset;
487 pp->stripesize = dp->d_stripesize;
488 if (bootverbose)
489 printf("GEOM: new disk %s\n", gp->name);
490 sysctl_ctx_init(&sc->sysctl_ctx);
491 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
492 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
493 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
494 CTLFLAG_RD, 0, tmpstr);
495 if (sc->sysctl_tree != NULL) {
496 snprintf(tmpstr, sizeof(tmpstr),
497 "kern.geom.disk.%s.led", gp->name);
498 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
499 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
500 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
501 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
502 "LED name");
503 }
504 TASK_INIT(&sc->resize_task, 0, g_disk_resize_task, sc);
505 pp->private = sc;
506 dp->d_geom = gp;
507 g_error_provider(pp, 0);
508}
509
510/*
511 * We get this callback after all of the consumers have gone away, and just
512 * before the provider is freed. If the disk driver provided a d_gone
513 * callback, let them know that it is okay to free resources -- they won't
514 * be getting any more accesses from GEOM.
515 */
516static void
517g_disk_providergone(struct g_provider *pp)
518{
519 struct disk *dp;
520 struct g_disk_softc *sc;
521
522 sc = (struct g_disk_softc *)pp->geom->softc;
523
524 /*
525 * If the softc is already NULL, then we've probably been through
526 * g_disk_destroy already; there is nothing for us to do anyway.
527 */
528 if (sc == NULL)
529 return;
530
531 dp = sc->dp;
532
533 if (dp->d_gone != NULL)
534 dp->d_gone(dp);
535}
536
537static void
538g_disk_destroy(void *ptr, int flag)
539{
540 struct disk *dp;
541 struct g_geom *gp;
542 struct g_disk_softc *sc;
543
544 g_topology_assert();
545 dp = ptr;
546 gp = dp->d_geom;
547 if (gp != NULL) {
548 sc = gp->softc;
549 if (sc->sysctl_tree != NULL) {
550 sysctl_ctx_free(&sc->sysctl_ctx);
551 sc->sysctl_tree = NULL;
552 }
553 if (sc->led[0] != 0) {
554 led_set(sc->led, "0");
555 sc->led[0] = 0;
556 }
557 g_free(sc);
558 gp->softc = NULL;
559 g_wither_geom(gp, ENXIO);
560 }
561 g_free(dp);
562}
563
564/*
565 * We only allow printable characters in disk ident,
566 * the rest is converted to 'x<HH>'.
567 */
568static void
569g_disk_ident_adjust(char *ident, size_t size)
570{
571 char *p, tmp[4], newid[DISK_IDENT_SIZE];
572
573 newid[0] = '\0';
574 for (p = ident; *p != '\0'; p++) {
575 if (isprint(*p)) {
576 tmp[0] = *p;
577 tmp[1] = '\0';
578 } else {
579 snprintf(tmp, sizeof(tmp), "x%02hhx",
580 *(unsigned char *)p);
581 }
582 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
583 break;
584 }
585 bzero(ident, size);
586 strlcpy(ident, newid, size);
587}
588
589struct disk *
590disk_alloc(void)
591{
592
593 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
594}
595
596void
597disk_create(struct disk *dp, int version)
598{
599
600 if (version != DISK_VERSION_02) {
601 printf("WARNING: Attempt to add disk %s%d %s",
602 dp->d_name, dp->d_unit,
603 " using incompatible ABI version of disk(9)\n");
604 printf("WARNING: Ignoring disk %s%d\n",
605 dp->d_name, dp->d_unit);
606 return;
607 }
608 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
609 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
610 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
611 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
612 if (dp->d_devstat == NULL)
613 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
614 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
615 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
616 dp->d_geom = NULL;
617 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
618 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
619}
620
621void
622disk_destroy(struct disk *dp)
623{
624
625 g_cancel_event(dp);
626 dp->d_destroyed = 1;
627 if (dp->d_devstat != NULL)
628 devstat_remove_entry(dp->d_devstat);
629 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
630}
631
632void
633disk_gone(struct disk *dp)
634{
635 struct g_geom *gp;
636 struct g_provider *pp;
637
638 gp = dp->d_geom;
639 if (gp != NULL)
640 LIST_FOREACH(pp, &gp->provider, provider)
641 g_wither_provider(pp, ENXIO);
642}
643
644void
645disk_attr_changed(struct disk *dp, const char *attr, int flag)
646{
647 struct g_geom *gp;
648 struct g_provider *pp;
649
650 gp = dp->d_geom;
651 if (gp != NULL)
652 LIST_FOREACH(pp, &gp->provider, provider)
653 (void)g_attr_changed(pp, attr, flag);
654}
655
656void
657disk_media_changed(struct disk *dp, int flag)
658{
659 struct g_geom *gp;
660 struct g_provider *pp;
661
662 gp = dp->d_geom;
663 if (gp != NULL) {
664 LIST_FOREACH(pp, &gp->provider, provider)
665 g_media_changed(pp, flag);
666 }
667}
668
669void
670disk_media_gone(struct disk *dp, int flag)
671{
672 struct g_geom *gp;
673 struct g_provider *pp;
674
675 gp = dp->d_geom;
676 if (gp != NULL) {
677 LIST_FOREACH(pp, &gp->provider, provider)
678 g_media_gone(pp, flag);
679 }
680}
681
682void
683disk_resize(struct disk *dp)
684{
685 struct g_geom *gp;
686 struct g_disk_softc *sc;
687 int error;
688
689 gp = dp->d_geom;
690
691 if (gp == NULL)
692 return;
693
694 sc = gp->softc;
695
696 error = taskqueue_enqueue(taskqueue_thread, &sc->resize_task);
697 KASSERT(error == 0, ("taskqueue_enqueue(9) failed."));
698}
699
700static void
701g_kern_disks(void *p, int flag __unused)
702{
703 struct sbuf *sb;
704 struct g_geom *gp;
705 char *sp;
706
707 sb = p;
708 sp = "";
709 g_topology_assert();
710 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
711 sbuf_printf(sb, "%s%s", sp, gp->name);
712 sp = " ";
713 }
714 sbuf_finish(sb);
715}
716
717static int
718sysctl_disks(SYSCTL_HANDLER_ARGS)
719{
720 int error;
721 struct sbuf *sb;
722
723 sb = sbuf_new_auto();
724 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
725 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
726 sbuf_delete(sb);
727 return error;
728}
729
730SYSCTL_PROC(_kern, OID_AUTO, disks,
731 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
732 sysctl_disks, "A", "names of available disks");
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>
37__FBSDID("$FreeBSD: head/sys/geom/geom_disk.c 240629 2012-09-18 07:57:34Z avg $");
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/ctype.h>
47#include <sys/fcntl.h>
48#include <sys/malloc.h>
49#include <sys/sbuf.h>
50#include <sys/sysctl.h>
51#include <sys/taskqueue.h>
52#include <sys/devicestat.h>
53#include <machine/md_var.h>
54
55#include <sys/lock.h>
56#include <sys/mutex.h>
57#include <geom/geom.h>
58#include <geom/geom_disk.h>
59#include <geom/geom_int.h>
60
61#include <dev/led/led.h>
62
63struct g_disk_softc {
64 struct disk *dp;
65 struct sysctl_ctx_list sysctl_ctx;
66 struct sysctl_oid *sysctl_tree;
67 char led[64];
68 uint32_t state;
69 struct task resize_task;
70};
71
72static struct mtx g_disk_done_mtx;
73
74static g_access_t g_disk_access;
75static g_init_t g_disk_init;
76static g_fini_t g_disk_fini;
77static g_start_t g_disk_start;
78static g_ioctl_t g_disk_ioctl;
79static g_dumpconf_t g_disk_dumpconf;
80static g_provgone_t g_disk_providergone;
81
82static struct g_class g_disk_class = {
83 .name = "DISK",
84 .version = G_VERSION,
85 .init = g_disk_init,
86 .fini = g_disk_fini,
87 .start = g_disk_start,
88 .access = g_disk_access,
89 .ioctl = g_disk_ioctl,
90 .providergone = g_disk_providergone,
91 .dumpconf = g_disk_dumpconf,
92};
93
94SYSCTL_DECL(_kern_geom);
95static SYSCTL_NODE(_kern_geom, OID_AUTO, disk, CTLFLAG_RW, 0,
96 "GEOM_DISK stuff");
97
98static void
99g_disk_init(struct g_class *mp __unused)
100{
101
102 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
103}
104
105static void
106g_disk_fini(struct g_class *mp __unused)
107{
108
109 mtx_destroy(&g_disk_done_mtx);
110}
111
112DECLARE_GEOM_CLASS(g_disk_class, g_disk);
113
114static void __inline
115g_disk_lock_giant(struct disk *dp)
116{
117
118 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
119 mtx_lock(&Giant);
120}
121
122static void __inline
123g_disk_unlock_giant(struct disk *dp)
124{
125
126 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
127 mtx_unlock(&Giant);
128}
129
130static int
131g_disk_access(struct g_provider *pp, int r, int w, int e)
132{
133 struct disk *dp;
134 struct g_disk_softc *sc;
135 int error;
136
137 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
138 pp->name, r, w, e);
139 g_topology_assert();
140 sc = pp->geom->softc;
141 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
142 /*
143 * Allow decreasing access count even if disk is not
144 * avaliable anymore.
145 */
146 if (r <= 0 && w <= 0 && e <= 0)
147 return (0);
148 return (ENXIO);
149 }
150 r += pp->acr;
151 w += pp->acw;
152 e += pp->ace;
153 error = 0;
154 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
155 if (dp->d_open != NULL) {
156 g_disk_lock_giant(dp);
157 error = dp->d_open(dp);
158 if (bootverbose && error != 0)
159 printf("Opened disk %s -> %d\n",
160 pp->name, error);
161 g_disk_unlock_giant(dp);
162 }
163 pp->mediasize = dp->d_mediasize;
164 pp->sectorsize = dp->d_sectorsize;
165 pp->stripeoffset = dp->d_stripeoffset;
166 pp->stripesize = dp->d_stripesize;
167 dp->d_flags |= DISKFLAG_OPEN;
168 if (dp->d_maxsize == 0) {
169 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
170 dp->d_name, dp->d_unit);
171 dp->d_maxsize = DFLTPHYS;
172 }
173 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
174 if (dp->d_close != NULL) {
175 g_disk_lock_giant(dp);
176 error = dp->d_close(dp);
177 if (error != 0)
178 printf("Closed disk %s -> %d\n",
179 pp->name, error);
180 g_disk_unlock_giant(dp);
181 }
182 sc->state = G_STATE_ACTIVE;
183 if (sc->led[0] != 0)
184 led_set(sc->led, "0");
185 dp->d_flags &= ~DISKFLAG_OPEN;
186 }
187 return (error);
188}
189
190static void
191g_disk_kerneldump(struct bio *bp, struct disk *dp)
192{
193 struct g_kerneldump *gkd;
194 struct g_geom *gp;
195
196 gkd = (struct g_kerneldump*)bp->bio_data;
197 gp = bp->bio_to->geom;
198 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
199 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
200 if (dp->d_dump == NULL) {
201 g_io_deliver(bp, ENODEV);
202 return;
203 }
204 gkd->di.dumper = dp->d_dump;
205 gkd->di.priv = dp;
206 gkd->di.blocksize = dp->d_sectorsize;
207 gkd->di.maxiosize = dp->d_maxsize;
208 gkd->di.mediaoffset = gkd->offset;
209 if ((gkd->offset + gkd->length) > dp->d_mediasize)
210 gkd->length = dp->d_mediasize - gkd->offset;
211 gkd->di.mediasize = gkd->length;
212 g_io_deliver(bp, 0);
213}
214
215static void
216g_disk_setstate(struct bio *bp, struct g_disk_softc *sc)
217{
218 const char *cmd;
219
220 memcpy(&sc->state, bp->bio_data, sizeof(sc->state));
221 if (sc->led[0] != 0) {
222 switch (sc->state) {
223 case G_STATE_FAILED:
224 cmd = "1";
225 break;
226 case G_STATE_REBUILD:
227 cmd = "f5";
228 break;
229 case G_STATE_RESYNC:
230 cmd = "f1";
231 break;
232 default:
233 cmd = "0";
234 break;
235 }
236 led_set(sc->led, cmd);
237 }
238 g_io_deliver(bp, 0);
239}
240
241static void
242g_disk_done(struct bio *bp)
243{
244 struct bio *bp2;
245 struct disk *dp;
246 struct g_disk_softc *sc;
247
248 /* See "notes" for why we need a mutex here */
249 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
250 mtx_lock(&g_disk_done_mtx);
251 bp->bio_completed = bp->bio_length - bp->bio_resid;
252
253 bp2 = bp->bio_parent;
254 if (bp2->bio_error == 0)
255 bp2->bio_error = bp->bio_error;
256 bp2->bio_completed += bp->bio_completed;
257 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) != 0 &&
258 (sc = bp2->bio_to->geom->softc) != NULL &&
259 (dp = sc->dp) != NULL) {
260 devstat_end_transaction_bio(dp->d_devstat, bp);
261 }
262 g_destroy_bio(bp);
263 bp2->bio_inbed++;
264 if (bp2->bio_children == bp2->bio_inbed) {
265 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
266 g_io_deliver(bp2, bp2->bio_error);
267 }
268 mtx_unlock(&g_disk_done_mtx);
269}
270
271static int
272g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
273{
274 struct g_geom *gp;
275 struct disk *dp;
276 struct g_disk_softc *sc;
277 int error;
278
279 gp = pp->geom;
280 sc = gp->softc;
281 dp = sc->dp;
282
283 if (dp->d_ioctl == NULL)
284 return (ENOIOCTL);
285 g_disk_lock_giant(dp);
286 error = dp->d_ioctl(dp, cmd, data, fflag, td);
287 g_disk_unlock_giant(dp);
288 return (error);
289}
290
291static void
292g_disk_start(struct bio *bp)
293{
294 struct bio *bp2, *bp3;
295 struct disk *dp;
296 struct g_disk_softc *sc;
297 int error;
298 off_t off;
299
300 sc = bp->bio_to->geom->softc;
301 if (sc == NULL || (dp = sc->dp) == NULL || dp->d_destroyed) {
302 g_io_deliver(bp, ENXIO);
303 return;
304 }
305 error = EJUSTRETURN;
306 switch(bp->bio_cmd) {
307 case BIO_DELETE:
308 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
309 error = EOPNOTSUPP;
310 break;
311 }
312 /* fall-through */
313 case BIO_READ:
314 case BIO_WRITE:
315 off = 0;
316 bp3 = NULL;
317 bp2 = g_clone_bio(bp);
318 if (bp2 == NULL) {
319 error = ENOMEM;
320 break;
321 }
322 do {
323 bp2->bio_offset += off;
324 bp2->bio_length -= off;
325 bp2->bio_data += off;
326 if (bp2->bio_length > dp->d_maxsize) {
327 /*
328 * XXX: If we have a stripesize we should really
329 * use it here.
330 */
331 bp2->bio_length = dp->d_maxsize;
332 off += dp->d_maxsize;
333 /*
334 * To avoid a race, we need to grab the next bio
335 * before we schedule this one. See "notes".
336 */
337 bp3 = g_clone_bio(bp);
338 if (bp3 == NULL)
339 bp->bio_error = ENOMEM;
340 }
341 bp2->bio_done = g_disk_done;
342 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
343 bp2->bio_bcount = bp2->bio_length;
344 bp2->bio_disk = dp;
345 devstat_start_transaction_bio(dp->d_devstat, bp2);
346 g_disk_lock_giant(dp);
347 dp->d_strategy(bp2);
348 g_disk_unlock_giant(dp);
349 bp2 = bp3;
350 bp3 = NULL;
351 } while (bp2 != NULL);
352 break;
353 case BIO_GETATTR:
354 /* Give the driver a chance to override */
355 if (dp->d_getattr != NULL) {
356 if (bp->bio_disk == NULL)
357 bp->bio_disk = dp;
358 error = dp->d_getattr(bp);
359 if (error != -1)
360 break;
361 error = EJUSTRETURN;
362 }
363 if (g_handleattr_int(bp, "GEOM::candelete",
364 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
365 break;
366 else if (g_handleattr_int(bp, "GEOM::fwsectors",
367 dp->d_fwsectors))
368 break;
369 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
370 break;
371 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
372 break;
373 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
374 break;
375 else if (g_handleattr(bp, "GEOM::hba_vendor",
376 &dp->d_hba_vendor, 2))
377 break;
378 else if (g_handleattr(bp, "GEOM::hba_device",
379 &dp->d_hba_device, 2))
380 break;
381 else if (g_handleattr(bp, "GEOM::hba_subvendor",
382 &dp->d_hba_subvendor, 2))
383 break;
384 else if (g_handleattr(bp, "GEOM::hba_subdevice",
385 &dp->d_hba_subdevice, 2))
386 break;
387 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
388 g_disk_kerneldump(bp, dp);
389 else if (!strcmp(bp->bio_attribute, "GEOM::setstate"))
390 g_disk_setstate(bp, sc);
391 else
392 error = ENOIOCTL;
393 break;
394 case BIO_FLUSH:
395 g_trace(G_T_BIO, "g_disk_flushcache(%s)",
396 bp->bio_to->name);
397 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
398 error = EOPNOTSUPP;
399 break;
400 }
401 bp2 = g_clone_bio(bp);
402 if (bp2 == NULL) {
403 g_io_deliver(bp, ENOMEM);
404 return;
405 }
406 bp2->bio_done = g_disk_done;
407 bp2->bio_disk = dp;
408 g_disk_lock_giant(dp);
409 dp->d_strategy(bp2);
410 g_disk_unlock_giant(dp);
411 break;
412 default:
413 error = EOPNOTSUPP;
414 break;
415 }
416 if (error != EJUSTRETURN)
417 g_io_deliver(bp, error);
418 return;
419}
420
421static void
422g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
423{
424 struct disk *dp;
425 struct g_disk_softc *sc;
426
427 sc = gp->softc;
428 if (sc == NULL || (dp = sc->dp) == NULL)
429 return;
430 if (indent == NULL) {
431 sbuf_printf(sb, " hd %u", dp->d_fwheads);
432 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
433 return;
434 }
435 if (pp != NULL) {
436 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
437 indent, dp->d_fwheads);
438 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
439 indent, dp->d_fwsectors);
440 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident);
441 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr);
442 }
443}
444
445static void
446g_disk_resize_task(void *context, int pending)
447{
448 struct g_geom *gp;
449 struct g_provider *pp;
450 struct disk *dp;
451 struct g_disk_softc *sc;
452
453 sc = (struct g_disk_softc *)context;
454 dp = sc->dp;
455 gp = dp->d_geom;
456
457 LIST_FOREACH(pp, &gp->provider, provider) {
458 if (pp->sectorsize != 0 &&
459 pp->sectorsize != dp->d_sectorsize)
460 g_wither_provider(pp, ENXIO);
461 else
462 g_resize_provider(pp, dp->d_mediasize);
463 }
464}
465
466static void
467g_disk_create(void *arg, int flag)
468{
469 struct g_geom *gp;
470 struct g_provider *pp;
471 struct disk *dp;
472 struct g_disk_softc *sc;
473 char tmpstr[80];
474
475 if (flag == EV_CANCEL)
476 return;
477 g_topology_assert();
478 dp = arg;
479 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
480 sc->dp = dp;
481 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
482 gp->softc = sc;
483 pp = g_new_providerf(gp, "%s", gp->name);
484 pp->mediasize = dp->d_mediasize;
485 pp->sectorsize = dp->d_sectorsize;
486 pp->stripeoffset = dp->d_stripeoffset;
487 pp->stripesize = dp->d_stripesize;
488 if (bootverbose)
489 printf("GEOM: new disk %s\n", gp->name);
490 sysctl_ctx_init(&sc->sysctl_ctx);
491 snprintf(tmpstr, sizeof(tmpstr), "GEOM disk %s", gp->name);
492 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
493 SYSCTL_STATIC_CHILDREN(_kern_geom_disk), OID_AUTO, gp->name,
494 CTLFLAG_RD, 0, tmpstr);
495 if (sc->sysctl_tree != NULL) {
496 snprintf(tmpstr, sizeof(tmpstr),
497 "kern.geom.disk.%s.led", gp->name);
498 TUNABLE_STR_FETCH(tmpstr, sc->led, sizeof(sc->led));
499 SYSCTL_ADD_STRING(&sc->sysctl_ctx,
500 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "led",
501 CTLFLAG_RW | CTLFLAG_TUN, sc->led, sizeof(sc->led),
502 "LED name");
503 }
504 TASK_INIT(&sc->resize_task, 0, g_disk_resize_task, sc);
505 pp->private = sc;
506 dp->d_geom = gp;
507 g_error_provider(pp, 0);
508}
509
510/*
511 * We get this callback after all of the consumers have gone away, and just
512 * before the provider is freed. If the disk driver provided a d_gone
513 * callback, let them know that it is okay to free resources -- they won't
514 * be getting any more accesses from GEOM.
515 */
516static void
517g_disk_providergone(struct g_provider *pp)
518{
519 struct disk *dp;
520 struct g_disk_softc *sc;
521
522 sc = (struct g_disk_softc *)pp->geom->softc;
523
524 /*
525 * If the softc is already NULL, then we've probably been through
526 * g_disk_destroy already; there is nothing for us to do anyway.
527 */
528 if (sc == NULL)
529 return;
530
531 dp = sc->dp;
532
533 if (dp->d_gone != NULL)
534 dp->d_gone(dp);
535}
536
537static void
538g_disk_destroy(void *ptr, int flag)
539{
540 struct disk *dp;
541 struct g_geom *gp;
542 struct g_disk_softc *sc;
543
544 g_topology_assert();
545 dp = ptr;
546 gp = dp->d_geom;
547 if (gp != NULL) {
548 sc = gp->softc;
549 if (sc->sysctl_tree != NULL) {
550 sysctl_ctx_free(&sc->sysctl_ctx);
551 sc->sysctl_tree = NULL;
552 }
553 if (sc->led[0] != 0) {
554 led_set(sc->led, "0");
555 sc->led[0] = 0;
556 }
557 g_free(sc);
558 gp->softc = NULL;
559 g_wither_geom(gp, ENXIO);
560 }
561 g_free(dp);
562}
563
564/*
565 * We only allow printable characters in disk ident,
566 * the rest is converted to 'x<HH>'.
567 */
568static void
569g_disk_ident_adjust(char *ident, size_t size)
570{
571 char *p, tmp[4], newid[DISK_IDENT_SIZE];
572
573 newid[0] = '\0';
574 for (p = ident; *p != '\0'; p++) {
575 if (isprint(*p)) {
576 tmp[0] = *p;
577 tmp[1] = '\0';
578 } else {
579 snprintf(tmp, sizeof(tmp), "x%02hhx",
580 *(unsigned char *)p);
581 }
582 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
583 break;
584 }
585 bzero(ident, size);
586 strlcpy(ident, newid, size);
587}
588
589struct disk *
590disk_alloc(void)
591{
592
593 return (g_malloc(sizeof(struct disk), M_WAITOK | M_ZERO));
594}
595
596void
597disk_create(struct disk *dp, int version)
598{
599
600 if (version != DISK_VERSION_02) {
601 printf("WARNING: Attempt to add disk %s%d %s",
602 dp->d_name, dp->d_unit,
603 " using incompatible ABI version of disk(9)\n");
604 printf("WARNING: Ignoring disk %s%d\n",
605 dp->d_name, dp->d_unit);
606 return;
607 }
608 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
609 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
610 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
611 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
612 if (dp->d_devstat == NULL)
613 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
614 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
615 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
616 dp->d_geom = NULL;
617 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
618 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
619}
620
621void
622disk_destroy(struct disk *dp)
623{
624
625 g_cancel_event(dp);
626 dp->d_destroyed = 1;
627 if (dp->d_devstat != NULL)
628 devstat_remove_entry(dp->d_devstat);
629 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
630}
631
632void
633disk_gone(struct disk *dp)
634{
635 struct g_geom *gp;
636 struct g_provider *pp;
637
638 gp = dp->d_geom;
639 if (gp != NULL)
640 LIST_FOREACH(pp, &gp->provider, provider)
641 g_wither_provider(pp, ENXIO);
642}
643
644void
645disk_attr_changed(struct disk *dp, const char *attr, int flag)
646{
647 struct g_geom *gp;
648 struct g_provider *pp;
649
650 gp = dp->d_geom;
651 if (gp != NULL)
652 LIST_FOREACH(pp, &gp->provider, provider)
653 (void)g_attr_changed(pp, attr, flag);
654}
655
656void
657disk_media_changed(struct disk *dp, int flag)
658{
659 struct g_geom *gp;
660 struct g_provider *pp;
661
662 gp = dp->d_geom;
663 if (gp != NULL) {
664 LIST_FOREACH(pp, &gp->provider, provider)
665 g_media_changed(pp, flag);
666 }
667}
668
669void
670disk_media_gone(struct disk *dp, int flag)
671{
672 struct g_geom *gp;
673 struct g_provider *pp;
674
675 gp = dp->d_geom;
676 if (gp != NULL) {
677 LIST_FOREACH(pp, &gp->provider, provider)
678 g_media_gone(pp, flag);
679 }
680}
681
682void
683disk_resize(struct disk *dp)
684{
685 struct g_geom *gp;
686 struct g_disk_softc *sc;
687 int error;
688
689 gp = dp->d_geom;
690
691 if (gp == NULL)
692 return;
693
694 sc = gp->softc;
695
696 error = taskqueue_enqueue(taskqueue_thread, &sc->resize_task);
697 KASSERT(error == 0, ("taskqueue_enqueue(9) failed."));
698}
699
700static void
701g_kern_disks(void *p, int flag __unused)
702{
703 struct sbuf *sb;
704 struct g_geom *gp;
705 char *sp;
706
707 sb = p;
708 sp = "";
709 g_topology_assert();
710 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
711 sbuf_printf(sb, "%s%s", sp, gp->name);
712 sp = " ";
713 }
714 sbuf_finish(sb);
715}
716
717static int
718sysctl_disks(SYSCTL_HANDLER_ARGS)
719{
720 int error;
721 struct sbuf *sb;
722
723 sb = sbuf_new_auto();
724 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
725 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
726 sbuf_delete(sb);
727 return error;
728}
729
730SYSCTL_PROC(_kern, OID_AUTO, disks,
731 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
732 sysctl_disks, "A", "names of available disks");