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