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