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