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