2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/devicestat.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
51 #include <sys/mutex.h>
52 #include <sys/errno.h>
54 #include <geom/geom.h>
55 #include <geom/geom_int.h>
56 #include <machine/stdarg.h>
66 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
67 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
68 char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
77 * This event offers a new class a chance to taste all preexisting providers.
80 g_load_class(void *arg, int flag)
83 struct g_class *mp2, *mp;
85 struct g_provider *pp;
88 if (flag == EV_CANCEL) /* XXX: can't happen ? */
100 g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
101 KASSERT(mp->name != NULL && *mp->name != '\0',
102 ("GEOM class has no name"));
103 LIST_FOREACH(mp2, &g_classes, class) {
105 printf("The GEOM class %s is already loaded.\n",
110 } else if (strcmp(mp2->name, mp->name) == 0) {
111 printf("A GEOM class %s is already loaded.\n",
119 LIST_INIT(&mp->geom);
120 LIST_INSERT_HEAD(&g_classes, mp, class);
121 if (mp->init != NULL)
123 if (mp->taste == NULL)
125 LIST_FOREACH(mp2, &g_classes, class) {
128 LIST_FOREACH(gp, &mp2->geom, geom) {
129 LIST_FOREACH(pp, &gp->provider, provider) {
130 mp->taste(mp, pp, 0);
138 g_unload_class(struct g_class *mp)
141 struct g_provider *pp;
142 struct g_consumer *cp;
146 g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
149 LIST_FOREACH(gp, &mp->geom, geom) {
150 /* We refuse to unload if anything is open */
151 LIST_FOREACH(pp, &gp->provider, provider)
152 if (pp->acr || pp->acw || pp->ace) {
156 LIST_FOREACH(cp, &gp->consumer, consumer)
157 if (cp->acr || cp->acw || cp->ace) {
161 /* If the geom is withering, wait for it to finish. */
162 if (gp->flags & G_GEOM_WITHER) {
163 g_topology_sleep(mp, 1);
169 * We allow unloading if we have no geoms, or a class
170 * method we can use to get rid of them.
172 if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
177 /* Bar new entries */
181 LIST_FOREACH(gp, &mp->geom, geom) {
182 error = mp->destroy_geom(NULL, mp, gp);
188 /* Wait for withering to finish. */
190 gp = LIST_FIRST(&mp->geom);
193 KASSERT(gp->flags & G_GEOM_WITHER,
194 ("Non-withering geom in class %s", mp->name));
195 g_topology_sleep(mp, 1);
198 if (mp->fini != NULL)
200 LIST_REMOVE(mp, class);
207 g_modevent(module_t mod, int type, void *data)
211 static int g_ignition;
215 if (mp->version != G_VERSION) {
216 printf("GEOM class %s has Wrong version %x\n",
217 mp->name, mp->version);
227 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", mp->name);
228 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
231 * Once the system is not cold, MOD_LOAD calls will be
232 * from the userland and the g_event thread will be able
233 * to acknowledge their completion.
237 error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
239 error = g_waitfor_event(g_load_class, hh, M_WAITOK,
247 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", mp->name);
249 error = g_unload_class(mp);
252 KASSERT(LIST_EMPTY(&mp->geom),
253 ("Unloaded class (%s) still has geom", mp->name));
261 g_retaste_event(void *arg, int flag)
263 struct g_class *mp, *mp2;
266 struct g_provider *pp;
267 struct g_consumer *cp;
270 if (flag == EV_CANCEL) /* XXX: can't happen ? */
282 g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
284 LIST_FOREACH(mp2, &g_classes, class) {
285 LIST_FOREACH(gp, &mp2->geom, geom) {
286 LIST_FOREACH(pp, &gp->provider, provider) {
287 if (pp->acr || pp->acw || pp->ace)
289 LIST_FOREACH(cp, &pp->consumers, consumers) {
290 if (cp->geom->class == mp &&
291 (cp->flags & G_CF_ORPHAN) == 0)
295 cp->flags |= G_CF_ORPHAN;
296 g_wither_geom(cp->geom, ENXIO);
298 mp->taste(mp, pp, 0);
306 g_retaste(struct g_class *mp)
311 if (mp->taste == NULL)
314 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
319 error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
321 error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
331 g_new_geomf(struct g_class *mp, const char *fmt, ...)
339 sb = sbuf_new_auto();
341 sbuf_vprintf(sb, fmt, ap);
344 gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
345 gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
348 LIST_INIT(&gp->consumer);
349 LIST_INIT(&gp->provider);
350 LIST_INSERT_HEAD(&mp->geom, gp, geom);
351 TAILQ_INSERT_HEAD(&geoms, gp, geoms);
352 strcpy(gp->name, sbuf_data(sb));
354 /* Fill in defaults from class */
355 gp->start = mp->start;
356 gp->spoiled = mp->spoiled;
357 gp->attrchanged = mp->attrchanged;
358 gp->providergone = mp->providergone;
359 gp->dumpconf = mp->dumpconf;
360 gp->access = mp->access;
361 gp->orphan = mp->orphan;
362 gp->ioctl = mp->ioctl;
367 g_destroy_geom(struct g_geom *gp)
372 g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
373 KASSERT(LIST_EMPTY(&gp->consumer),
374 ("g_destroy_geom(%s) with consumer(s) [%p]",
375 gp->name, LIST_FIRST(&gp->consumer)));
376 KASSERT(LIST_EMPTY(&gp->provider),
377 ("g_destroy_geom(%s) with provider(s) [%p]",
378 gp->name, LIST_FIRST(&gp->provider)));
380 LIST_REMOVE(gp, geom);
381 TAILQ_REMOVE(&geoms, gp, geoms);
387 * This function is called (repeatedly) until the geom has withered away.
390 g_wither_geom(struct g_geom *gp, int error)
392 struct g_provider *pp;
396 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
397 if (!(gp->flags & G_GEOM_WITHER)) {
398 gp->flags |= G_GEOM_WITHER;
399 LIST_FOREACH(pp, &gp->provider, provider)
400 if (!(pp->flags & G_PF_ORPHAN))
401 g_orphan_provider(pp, error);
407 * Convenience function to destroy a particular provider.
410 g_wither_provider(struct g_provider *pp, int error)
413 pp->flags |= G_PF_WITHER;
414 if (!(pp->flags & G_PF_ORPHAN))
415 g_orphan_provider(pp, error);
419 * This function is called (repeatedly) until the has withered away.
422 g_wither_geom_close(struct g_geom *gp, int error)
424 struct g_consumer *cp;
428 g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
429 LIST_FOREACH(cp, &gp->consumer, consumer)
430 if (cp->acr || cp->acw || cp->ace)
431 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
432 g_wither_geom(gp, error);
436 * This function is called (repeatedly) until we cant wash away more
437 * withered bits at present.
443 struct g_geom *gp, *gp2;
444 struct g_provider *pp, *pp2;
445 struct g_consumer *cp, *cp2;
448 LIST_FOREACH(mp, &g_classes, class) {
449 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
450 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
451 if (!(pp->flags & G_PF_WITHER))
453 if (LIST_EMPTY(&pp->consumers))
454 g_destroy_provider(pp);
456 if (!(gp->flags & G_GEOM_WITHER))
458 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
459 if (LIST_EMPTY(&pp->consumers))
460 g_destroy_provider(pp);
462 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
463 if (cp->acr || cp->acw || cp->ace)
465 if (cp->provider != NULL)
467 g_destroy_consumer(cp);
469 if (LIST_EMPTY(&gp->provider) &&
470 LIST_EMPTY(&gp->consumer))
477 g_new_consumer(struct g_geom *gp)
479 struct g_consumer *cp;
483 KASSERT(!(gp->flags & G_GEOM_WITHER),
484 ("g_new_consumer on WITHERing geom(%s) (class %s)",
485 gp->name, gp->class->name));
486 KASSERT(gp->orphan != NULL,
487 ("g_new_consumer on geom(%s) (class %s) without orphan",
488 gp->name, gp->class->name));
490 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
492 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
493 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
494 LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
499 g_destroy_consumer(struct g_consumer *cp)
504 G_VALID_CONSUMER(cp);
505 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
506 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
507 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
508 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
509 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
512 LIST_REMOVE(cp, consumer);
513 devstat_remove_entry(cp->stat);
515 if (gp->flags & G_GEOM_WITHER)
520 g_new_provider_event(void *arg, int flag)
523 struct g_provider *pp;
524 struct g_consumer *cp, *next_cp;
527 if (flag == EV_CANCEL)
532 G_VALID_PROVIDER(pp);
533 KASSERT(!(pp->flags & G_PF_WITHER),
534 ("g_new_provider_event but withered"));
535 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
536 if ((cp->flags & G_CF_ORPHAN) == 0 &&
537 cp->geom->attrchanged != NULL)
538 cp->geom->attrchanged(cp, "GEOM::media");
540 LIST_FOREACH(mp, &g_classes, class) {
541 if (mp->taste == NULL)
543 LIST_FOREACH(cp, &pp->consumers, consumers)
544 if (cp->geom->class == mp &&
545 (cp->flags & G_CF_ORPHAN) == 0)
549 mp->taste(mp, pp, 0);
556 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
558 struct g_provider *pp;
564 KASSERT(gp->access != NULL,
565 ("new provider on geom(%s) without ->access (class %s)",
566 gp->name, gp->class->name));
567 KASSERT(gp->start != NULL,
568 ("new provider on geom(%s) without ->start (class %s)",
569 gp->name, gp->class->name));
570 KASSERT(!(gp->flags & G_GEOM_WITHER),
571 ("new provider on WITHERing geom(%s) (class %s)",
572 gp->name, gp->class->name));
573 sb = sbuf_new_auto();
575 sbuf_vprintf(sb, fmt, ap);
578 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
579 pp->name = (char *)(pp + 1);
580 strcpy(pp->name, sbuf_data(sb));
582 LIST_INIT(&pp->consumers);
585 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
586 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
587 LIST_INSERT_HEAD(&gp->provider, pp, provider);
588 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
593 g_error_provider(struct g_provider *pp, int error)
596 /* G_VALID_PROVIDER(pp); We may not have g_topology */
601 #define _PATH_DEV "/dev/"
605 g_provider_by_name(char const *arg)
609 struct g_provider *pp;
611 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
612 arg += sizeof(_PATH_DEV) - 1;
614 LIST_FOREACH(cp, &g_classes, class) {
615 LIST_FOREACH(gp, &cp->geom, geom) {
616 LIST_FOREACH(pp, &gp->provider, provider) {
617 if (!strcmp(arg, pp->name))
627 g_destroy_provider(struct g_provider *pp)
632 G_VALID_PROVIDER(pp);
633 KASSERT(LIST_EMPTY(&pp->consumers),
634 ("g_destroy_provider but attached"));
635 KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
636 KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
637 KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
639 LIST_REMOVE(pp, provider);
641 devstat_remove_entry(pp->stat);
643 * If a callback was provided, send notification that the provider
646 if (gp->providergone != NULL)
647 gp->providergone(pp);
650 if ((gp->flags & G_GEOM_WITHER))
655 * We keep the "geoms" list sorted by topological order (== increasing
656 * numerical rank) at all times.
657 * When an attach is done, the attaching geoms rank is invalidated
658 * and it is moved to the tail of the list.
659 * All geoms later in the sequence has their ranks reevaluated in
660 * sequence. If we cannot assign rank to a geom because it's
661 * prerequisites do not have rank, we move that element to the tail
662 * of the sequence with invalid rank as well.
663 * At some point we encounter our original geom and if we stil fail
664 * to assign it a rank, there must be a loop and we fail back to
665 * g_attach() which detach again and calls redo_rank again
666 * to fix up the damage.
667 * It would be much simpler code wise to do it recursively, but we
668 * can't risk that on the kernel stack.
672 redo_rank(struct g_geom *gp)
674 struct g_consumer *cp;
675 struct g_geom *gp1, *gp2;
681 /* Invalidate this geoms rank and move it to the tail */
682 gp1 = TAILQ_NEXT(gp, geoms);
685 TAILQ_REMOVE(&geoms, gp, geoms);
686 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
691 /* re-rank the rest of the sequence */
692 for (; gp1 != NULL; gp1 = gp2) {
695 LIST_FOREACH(cp, &gp1->consumer, consumer) {
696 if (cp->provider == NULL)
698 n = cp->provider->geom->rank;
706 gp2 = TAILQ_NEXT(gp1, geoms);
708 /* got a rank, moving on */
712 /* no rank to original geom means loop */
716 /* no rank, put it at the end move on */
717 TAILQ_REMOVE(&geoms, gp1, geoms);
718 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
724 g_attach(struct g_consumer *cp, struct g_provider *pp)
729 G_VALID_CONSUMER(cp);
730 G_VALID_PROVIDER(pp);
731 g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
732 KASSERT(cp->provider == NULL, ("attach but attached"));
734 LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
735 error = redo_rank(cp->geom);
737 LIST_REMOVE(cp, consumers);
745 g_detach(struct g_consumer *cp)
747 struct g_provider *pp;
750 G_VALID_CONSUMER(cp);
751 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
752 KASSERT(cp->provider != NULL, ("detach but not attached"));
753 KASSERT(cp->acr == 0, ("detach but nonzero acr"));
754 KASSERT(cp->acw == 0, ("detach but nonzero acw"));
755 KASSERT(cp->ace == 0, ("detach but nonzero ace"));
756 KASSERT(cp->nstart == cp->nend,
757 ("detach with active requests"));
759 LIST_REMOVE(cp, consumers);
761 if ((cp->geom->flags & G_GEOM_WITHER) ||
762 (pp->geom->flags & G_GEOM_WITHER) ||
763 (pp->flags & G_PF_WITHER))
771 * Access-check with delta values. The question asked is "can provider
772 * "cp" change the access counters by the relative amounts dc[rwe] ?"
776 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
778 struct g_provider *pp;
783 G_VALID_CONSUMER(cp);
785 KASSERT(pp != NULL, ("access but not attached"));
786 G_VALID_PROVIDER(pp);
788 g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
789 cp, pp->name, dcr, dcw, dce);
791 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
792 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
793 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
794 KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
795 KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
798 * If our class cares about being spoiled, and we have been, we
799 * are probably just ahead of the event telling us that. Fail
800 * now rather than having to unravel this later.
802 if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
803 (dcr > 0 || dcw > 0 || dce > 0))
807 * Figure out what counts the provider would have had, if this
808 * consumer had (r0w0e0) at this time.
810 pr = pp->acr - cp->acr;
811 pw = pp->acw - cp->acw;
812 pe = pp->ace - cp->ace;
815 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
817 cp->acr, cp->acw, cp->ace,
818 pp->acr, pp->acw, pp->ace,
821 /* If foot-shooting is enabled, any open on rank#1 is OK */
822 if ((g_debugflags & 16) && pp->geom->rank == 1)
824 /* If we try exclusive but already write: fail */
825 else if (dce > 0 && pw > 0)
827 /* If we try write but already exclusive: fail */
828 else if (dcw > 0 && pe > 0)
830 /* If we try to open more but provider is error'ed: fail */
831 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
836 error = pp->geom->access(pp, dcr, dcw, dce);
837 KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
838 ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed "
839 "closing ->access()", pp->geom->class->name, pp->name, dcr, dcw,
843 * If we open first write, spoil any partner consumers.
844 * If we close last write and provider is not errored,
847 if (pp->acw == 0 && dcw != 0)
849 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
850 !(pp->geom->flags & G_GEOM_WITHER))
851 g_post_event(g_new_provider_event, pp, M_WAITOK,
860 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
861 KASSERT(pp->sectorsize > 0,
862 ("Provider %s lacks sectorsize", pp->name));
863 if ((cp->geom->flags & G_GEOM_WITHER) &&
864 cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
871 g_handleattr_int(struct bio *bp, const char *attribute, int val)
874 return (g_handleattr(bp, attribute, &val, sizeof val));
878 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
881 return (g_handleattr(bp, attribute, &val, sizeof val));
885 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
888 return (g_handleattr(bp, attribute, str, 0));
892 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
896 if (strcmp(bp->bio_attribute, attribute))
899 bzero(bp->bio_data, bp->bio_length);
900 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
902 printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
903 __func__, bp->bio_to->name,
904 (intmax_t)bp->bio_length, strlen(val));
907 } else if (bp->bio_length == len) {
908 bcopy(val, bp->bio_data, len);
910 printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
911 bp->bio_to->name, (intmax_t)bp->bio_length, len);
915 bp->bio_completed = bp->bio_length;
916 g_io_deliver(bp, error);
921 g_std_access(struct g_provider *pp,
922 int dr __unused, int dw __unused, int de __unused)
926 G_VALID_PROVIDER(pp);
931 g_std_done(struct bio *bp)
935 bp2 = bp->bio_parent;
936 if (bp2->bio_error == 0)
937 bp2->bio_error = bp->bio_error;
938 bp2->bio_completed += bp->bio_completed;
941 if (bp2->bio_children == bp2->bio_inbed)
942 g_io_deliver(bp2, bp2->bio_error);
945 /* XXX: maybe this is only g_slice_spoiled */
948 g_std_spoiled(struct g_consumer *cp)
951 struct g_provider *pp;
954 G_VALID_CONSUMER(cp);
955 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
956 cp->flags |= G_CF_ORPHAN;
959 LIST_FOREACH(pp, &gp->provider, provider)
960 g_orphan_provider(pp, ENXIO);
961 g_destroy_consumer(cp);
962 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
965 gp->flags |= G_GEOM_WITHER;
969 * Spoiling happens when a provider is opened for writing, but consumers
970 * which are configured by in-band data are attached (slicers for instance).
971 * Since the write might potentially change the in-band data, such consumers
972 * need to re-evaluate their existence after the writing session closes.
973 * We do this by (offering to) tear them down when the open for write happens
974 * in return for a re-taste when it closes again.
975 * Together with the fact that such consumers grab an 'e' bit whenever they
976 * are open, regardless of mode, this ends up DTRT.
980 g_spoil_event(void *arg, int flag)
982 struct g_provider *pp;
983 struct g_consumer *cp, *cp2;
986 if (flag == EV_CANCEL)
989 G_VALID_PROVIDER(pp);
990 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
991 cp2 = LIST_NEXT(cp, consumers);
992 if ((cp->flags & G_CF_SPOILED) == 0)
994 cp->flags &= ~G_CF_SPOILED;
995 if (cp->geom->spoiled == NULL)
997 cp->geom->spoiled(cp);
1003 g_spoil(struct g_provider *pp, struct g_consumer *cp)
1005 struct g_consumer *cp2;
1007 g_topology_assert();
1008 G_VALID_PROVIDER(pp);
1009 G_VALID_CONSUMER(cp);
1011 LIST_FOREACH(cp2, &pp->consumers, consumers) {
1015 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1016 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1018 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1019 cp2->flags |= G_CF_SPOILED;
1021 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1025 g_media_changed_event(void *arg, int flag)
1027 struct g_provider *pp;
1030 g_topology_assert();
1031 if (flag == EV_CANCEL)
1034 G_VALID_PROVIDER(pp);
1037 * If provider was not open for writing, queue retaste after spoiling.
1038 * If it was, retaste will happen automatically on close.
1040 retaste = (pp->acw == 0 && pp->error == 0 &&
1041 !(pp->geom->flags & G_GEOM_WITHER));
1042 g_spoil_event(arg, flag);
1044 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1048 g_media_changed(struct g_provider *pp, int flag)
1050 struct g_consumer *cp;
1052 LIST_FOREACH(cp, &pp->consumers, consumers)
1053 cp->flags |= G_CF_SPOILED;
1054 return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1058 g_media_gone(struct g_provider *pp, int flag)
1060 struct g_consumer *cp;
1062 LIST_FOREACH(cp, &pp->consumers, consumers)
1063 cp->flags |= G_CF_SPOILED;
1064 return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1068 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1073 error = g_io_getattr(attr, cp, &i, var);
1082 g_get_device_prefix_len(const char *name)
1086 if (strncmp(name, "ada", 3) == 0)
1088 else if (strncmp(name, "ad", 2) == 0)
1092 if (name[len] < '0' || name[len] > '9')
1096 } while (name[len] >= '0' && name[len] <= '9');
1101 g_compare_names(const char *namea, const char *nameb)
1105 if (strcmp(namea, nameb) == 0)
1107 deva = g_get_device_prefix_len(namea);
1110 devb = g_get_device_prefix_len(nameb);
1113 if (strcmp(namea + deva, nameb + devb) == 0)
1118 #if defined(DIAGNOSTIC) || defined(DDB)
1120 * This function walks the mesh and returns a non-zero integer if it
1121 * finds the argument pointer is an object. The return value indicates
1122 * which type of object it is believed to be. If topology is not locked,
1123 * this function is potentially dangerous, but we don't assert that the
1124 * topology lock is held when called from debugger.
1127 g_valid_obj(void const *ptr)
1131 struct g_consumer *cp;
1132 struct g_provider *pp;
1135 if (kdb_active == 0)
1137 g_topology_assert();
1139 LIST_FOREACH(mp, &g_classes, class) {
1142 LIST_FOREACH(gp, &mp->geom, geom) {
1145 LIST_FOREACH(cp, &gp->consumer, consumer)
1148 LIST_FOREACH(pp, &gp->provider, provider)
1159 #define gprintf(...) do { \
1160 db_printf("%*s", indent, ""); \
1161 db_printf(__VA_ARGS__); \
1163 #define gprintln(...) do { \
1164 gprintf(__VA_ARGS__); \
1168 #define ADDFLAG(obj, flag, sflag) do { \
1169 if ((obj)->flags & (flag)) { \
1171 strlcat(str, ",", size); \
1172 strlcat(str, (sflag), size); \
1178 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1183 if (pp->flags == 0) {
1184 strlcpy(str, "NONE", size);
1187 ADDFLAG(pp, G_PF_CANDELETE, "G_PF_CANDELETE");
1188 ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1189 ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1194 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1199 if (gp->flags == 0) {
1200 strlcpy(str, "NONE", size);
1203 ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1207 db_show_geom_consumer(int indent, struct g_consumer *cp)
1211 gprintln("consumer: %p", cp);
1212 gprintln(" class: %s (%p)", cp->geom->class->name,
1214 gprintln(" geom: %s (%p)", cp->geom->name, cp->geom);
1215 if (cp->provider == NULL)
1216 gprintln(" provider: none");
1218 gprintln(" provider: %s (%p)", cp->provider->name,
1221 gprintln(" access: r%dw%de%d", cp->acr, cp->acw, cp->ace);
1222 gprintln(" flags: 0x%04x", cp->flags);
1223 gprintln(" nstart: %u", cp->nstart);
1224 gprintln(" nend: %u", cp->nend);
1226 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1227 cp->provider != NULL ? cp->provider->name : "none",
1228 cp->acr, cp->acw, cp->ace);
1230 db_printf(", flags=0x%04x", cp->flags);
1236 db_show_geom_provider(int indent, struct g_provider *pp)
1238 struct g_consumer *cp;
1242 gprintln("provider: %s (%p)", pp->name, pp);
1243 gprintln(" class: %s (%p)", pp->geom->class->name,
1245 gprintln(" geom: %s (%p)", pp->geom->name, pp->geom);
1246 gprintln(" mediasize: %jd", (intmax_t)pp->mediasize);
1247 gprintln(" sectorsize: %u", pp->sectorsize);
1248 gprintln(" stripesize: %u", pp->stripesize);
1249 gprintln(" stripeoffset: %u", pp->stripeoffset);
1250 gprintln(" access: r%dw%de%d", pp->acr, pp->acw,
1252 gprintln(" flags: %s (0x%04x)",
1253 provider_flags_to_string(pp, flags, sizeof(flags)),
1255 gprintln(" error: %d", pp->error);
1256 gprintln(" nstart: %u", pp->nstart);
1257 gprintln(" nend: %u", pp->nend);
1258 if (LIST_EMPTY(&pp->consumers))
1259 gprintln(" consumers: none");
1261 gprintf("provider: %s (%p), access=r%dw%de%d",
1262 pp->name, pp, pp->acr, pp->acw, pp->ace);
1263 if (pp->flags != 0) {
1264 db_printf(", flags=%s (0x%04x)",
1265 provider_flags_to_string(pp, flags, sizeof(flags)),
1270 if (!LIST_EMPTY(&pp->consumers)) {
1271 LIST_FOREACH(cp, &pp->consumers, consumers) {
1272 db_show_geom_consumer(indent + 2, cp);
1280 db_show_geom_geom(int indent, struct g_geom *gp)
1282 struct g_provider *pp;
1283 struct g_consumer *cp;
1287 gprintln("geom: %s (%p)", gp->name, gp);
1288 gprintln(" class: %s (%p)", gp->class->name, gp->class);
1289 gprintln(" flags: %s (0x%04x)",
1290 geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1291 gprintln(" rank: %d", gp->rank);
1292 if (LIST_EMPTY(&gp->provider))
1293 gprintln(" providers: none");
1294 if (LIST_EMPTY(&gp->consumer))
1295 gprintln(" consumers: none");
1297 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1298 if (gp->flags != 0) {
1299 db_printf(", flags=%s (0x%04x)",
1300 geom_flags_to_string(gp, flags, sizeof(flags)),
1305 if (!LIST_EMPTY(&gp->provider)) {
1306 LIST_FOREACH(pp, &gp->provider, provider) {
1307 db_show_geom_provider(indent + 2, pp);
1312 if (!LIST_EMPTY(&gp->consumer)) {
1313 LIST_FOREACH(cp, &gp->consumer, consumer) {
1314 db_show_geom_consumer(indent + 2, cp);
1322 db_show_geom_class(struct g_class *mp)
1326 db_printf("class: %s (%p)\n", mp->name, mp);
1327 LIST_FOREACH(gp, &mp->geom, geom) {
1328 db_show_geom_geom(2, gp);
1335 * Print the GEOM topology or the given object.
1337 DB_SHOW_COMMAND(geom, db_show_geom)
1342 /* No address given, print the entire topology. */
1343 LIST_FOREACH(mp, &g_classes, class) {
1344 db_show_geom_class(mp);
1350 switch (g_valid_obj((void *)addr)) {
1352 db_show_geom_class((struct g_class *)addr);
1355 db_show_geom_geom(0, (struct g_geom *)addr);
1358 db_show_geom_consumer(0, (struct g_consumer *)addr);
1361 db_show_geom_provider(0, (struct g_provider *)addr);
1364 db_printf("Not a GEOM object.\n");
1371 db_print_bio_cmd(struct bio *bp)
1373 db_printf(" cmd: ");
1374 switch (bp->bio_cmd) {
1375 case BIO_READ: db_printf("BIO_READ"); break;
1376 case BIO_WRITE: db_printf("BIO_WRITE"); break;
1377 case BIO_DELETE: db_printf("BIO_DELETE"); break;
1378 case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1379 case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1380 case BIO_CMD0: db_printf("BIO_CMD0"); break;
1381 case BIO_CMD1: db_printf("BIO_CMD1"); break;
1382 case BIO_CMD2: db_printf("BIO_CMD2"); break;
1383 default: db_printf("UNKNOWN"); break;
1389 db_print_bio_flags(struct bio *bp)
1394 db_printf(" flags: ");
1395 if (bp->bio_flags & BIO_ERROR) {
1396 db_printf("BIO_ERROR");
1399 if (bp->bio_flags & BIO_DONE) {
1400 db_printf("%sBIO_DONE", (comma ? ", " : ""));
1403 if (bp->bio_flags & BIO_ONQUEUE)
1404 db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1409 * Print useful information in a BIO
1411 DB_SHOW_COMMAND(bio, db_show_bio)
1416 bp = (struct bio *)addr;
1417 db_printf("BIO %p\n", bp);
1418 db_print_bio_cmd(bp);
1419 db_print_bio_flags(bp);
1420 db_printf(" cflags: 0x%hhx\n", bp->bio_cflags);
1421 db_printf(" pflags: 0x%hhx\n", bp->bio_pflags);
1422 db_printf(" offset: %jd\n", (intmax_t)bp->bio_offset);
1423 db_printf(" length: %jd\n", (intmax_t)bp->bio_length);
1424 db_printf(" bcount: %ld\n", bp->bio_bcount);
1425 db_printf(" resid: %ld\n", bp->bio_resid);
1426 db_printf(" completed: %jd\n", (intmax_t)bp->bio_completed);
1427 db_printf(" children: %u\n", bp->bio_children);
1428 db_printf(" inbed: %u\n", bp->bio_inbed);
1429 db_printf(" error: %d\n", bp->bio_error);
1430 db_printf(" parent: %p\n", bp->bio_parent);
1431 db_printf(" driver1: %p\n", bp->bio_driver1);
1432 db_printf(" driver2: %p\n", bp->bio_driver2);
1433 db_printf(" caller1: %p\n", bp->bio_caller1);
1434 db_printf(" caller2: %p\n", bp->bio_caller2);
1435 db_printf(" bio_from: %p\n", bp->bio_from);
1436 db_printf(" bio_to: %p\n", bp->bio_to);