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;
72 struct g_provider *pp;
79 * This event offers a new class a chance to taste all preexisting providers.
82 g_load_class(void *arg, int flag)
85 struct g_class *mp2, *mp;
87 struct g_provider *pp;
90 if (flag == EV_CANCEL) /* XXX: can't happen ? */
102 g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
103 KASSERT(mp->name != NULL && *mp->name != '\0',
104 ("GEOM class has no name"));
105 LIST_FOREACH(mp2, &g_classes, class) {
107 printf("The GEOM class %s is already loaded.\n",
112 } else if (strcmp(mp2->name, mp->name) == 0) {
113 printf("A GEOM class %s is already loaded.\n",
121 LIST_INIT(&mp->geom);
122 LIST_INSERT_HEAD(&g_classes, mp, class);
123 if (mp->init != NULL)
125 if (mp->taste == NULL)
127 LIST_FOREACH(mp2, &g_classes, class) {
130 LIST_FOREACH(gp, &mp2->geom, geom) {
131 LIST_FOREACH(pp, &gp->provider, provider) {
132 mp->taste(mp, pp, 0);
140 g_unload_class(struct g_class *mp)
143 struct g_provider *pp;
144 struct g_consumer *cp;
148 g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
151 LIST_FOREACH(gp, &mp->geom, geom) {
152 /* We refuse to unload if anything is open */
153 LIST_FOREACH(pp, &gp->provider, provider)
154 if (pp->acr || pp->acw || pp->ace) {
158 LIST_FOREACH(cp, &gp->consumer, consumer)
159 if (cp->acr || cp->acw || cp->ace) {
163 /* If the geom is withering, wait for it to finish. */
164 if (gp->flags & G_GEOM_WITHER) {
165 g_topology_sleep(mp, 1);
171 * We allow unloading if we have no geoms, or a class
172 * method we can use to get rid of them.
174 if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
179 /* Bar new entries */
183 LIST_FOREACH(gp, &mp->geom, geom) {
184 error = mp->destroy_geom(NULL, mp, gp);
190 /* Wait for withering to finish. */
192 gp = LIST_FIRST(&mp->geom);
195 KASSERT(gp->flags & G_GEOM_WITHER,
196 ("Non-withering geom in class %s", mp->name));
197 g_topology_sleep(mp, 1);
200 if (mp->fini != NULL)
202 LIST_REMOVE(mp, class);
209 g_modevent(module_t mod, int type, void *data)
213 static int g_ignition;
217 if (mp->version != G_VERSION) {
218 printf("GEOM class %s has Wrong version %x\n",
219 mp->name, mp->version);
229 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", mp->name);
230 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
233 * Once the system is not cold, MOD_LOAD calls will be
234 * from the userland and the g_event thread will be able
235 * to acknowledge their completion.
239 error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
241 error = g_waitfor_event(g_load_class, hh, M_WAITOK,
249 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", mp->name);
250 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 ? */
272 if (g_shutdown || g_notaste)
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_INIT(&gp->aliases);
351 LIST_INSERT_HEAD(&mp->geom, gp, geom);
352 TAILQ_INSERT_HEAD(&geoms, gp, geoms);
353 strcpy(gp->name, sbuf_data(sb));
355 /* Fill in defaults from class */
356 gp->start = mp->start;
357 gp->spoiled = mp->spoiled;
358 gp->attrchanged = mp->attrchanged;
359 gp->providergone = mp->providergone;
360 gp->dumpconf = mp->dumpconf;
361 gp->access = mp->access;
362 gp->orphan = mp->orphan;
363 gp->ioctl = mp->ioctl;
364 gp->resize = mp->resize;
369 g_destroy_geom(struct g_geom *gp)
371 struct g_geom_alias *gap, *gaptmp;
375 g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
376 KASSERT(LIST_EMPTY(&gp->consumer),
377 ("g_destroy_geom(%s) with consumer(s) [%p]",
378 gp->name, LIST_FIRST(&gp->consumer)));
379 KASSERT(LIST_EMPTY(&gp->provider),
380 ("g_destroy_geom(%s) with provider(s) [%p]",
381 gp->name, LIST_FIRST(&gp->provider)));
383 LIST_REMOVE(gp, geom);
384 TAILQ_REMOVE(&geoms, gp, geoms);
385 LIST_FOREACH_SAFE(gap, &gp->aliases, ga_next, gaptmp)
392 * This function is called (repeatedly) until the geom has withered away.
395 g_wither_geom(struct g_geom *gp, int error)
397 struct g_provider *pp;
401 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
402 if (!(gp->flags & G_GEOM_WITHER)) {
403 gp->flags |= G_GEOM_WITHER;
404 LIST_FOREACH(pp, &gp->provider, provider)
405 if (!(pp->flags & G_PF_ORPHAN))
406 g_orphan_provider(pp, error);
412 * Convenience function to destroy a particular provider.
415 g_wither_provider(struct g_provider *pp, int error)
418 pp->flags |= G_PF_WITHER;
419 if (!(pp->flags & G_PF_ORPHAN))
420 g_orphan_provider(pp, error);
424 * This function is called (repeatedly) until the has withered away.
427 g_wither_geom_close(struct g_geom *gp, int error)
429 struct g_consumer *cp;
433 g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
434 LIST_FOREACH(cp, &gp->consumer, consumer)
435 if (cp->acr || cp->acw || cp->ace)
436 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
437 g_wither_geom(gp, error);
441 * This function is called (repeatedly) until we cant wash away more
442 * withered bits at present.
448 struct g_geom *gp, *gp2;
449 struct g_provider *pp, *pp2;
450 struct g_consumer *cp, *cp2;
453 LIST_FOREACH(mp, &g_classes, class) {
454 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
455 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
456 if (!(pp->flags & G_PF_WITHER))
458 if (LIST_EMPTY(&pp->consumers))
459 g_destroy_provider(pp);
461 if (!(gp->flags & G_GEOM_WITHER))
463 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
464 if (LIST_EMPTY(&pp->consumers))
465 g_destroy_provider(pp);
467 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
468 if (cp->acr || cp->acw || cp->ace)
470 if (cp->provider != NULL)
472 g_destroy_consumer(cp);
474 if (LIST_EMPTY(&gp->provider) &&
475 LIST_EMPTY(&gp->consumer))
482 g_new_consumer(struct g_geom *gp)
484 struct g_consumer *cp;
488 KASSERT(!(gp->flags & G_GEOM_WITHER),
489 ("g_new_consumer on WITHERing geom(%s) (class %s)",
490 gp->name, gp->class->name));
491 KASSERT(gp->orphan != NULL,
492 ("g_new_consumer on geom(%s) (class %s) without orphan",
493 gp->name, gp->class->name));
495 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
497 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
498 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
499 LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
504 g_destroy_consumer(struct g_consumer *cp)
509 G_VALID_CONSUMER(cp);
510 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
511 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
512 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
513 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
514 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
517 LIST_REMOVE(cp, consumer);
518 devstat_remove_entry(cp->stat);
520 if (gp->flags & G_GEOM_WITHER)
525 g_new_provider_event(void *arg, int flag)
528 struct g_provider *pp;
529 struct g_consumer *cp, *next_cp;
532 if (flag == EV_CANCEL)
537 G_VALID_PROVIDER(pp);
538 KASSERT(!(pp->flags & G_PF_WITHER),
539 ("g_new_provider_event but withered"));
540 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
541 if ((cp->flags & G_CF_ORPHAN) == 0 &&
542 cp->geom->attrchanged != NULL)
543 cp->geom->attrchanged(cp, "GEOM::media");
547 LIST_FOREACH(mp, &g_classes, class) {
548 if (mp->taste == NULL)
550 LIST_FOREACH(cp, &pp->consumers, consumers)
551 if (cp->geom->class == mp &&
552 (cp->flags & G_CF_ORPHAN) == 0)
556 mp->taste(mp, pp, 0);
563 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
565 struct g_provider *pp;
571 KASSERT(gp->access != NULL,
572 ("new provider on geom(%s) without ->access (class %s)",
573 gp->name, gp->class->name));
574 KASSERT(gp->start != NULL,
575 ("new provider on geom(%s) without ->start (class %s)",
576 gp->name, gp->class->name));
577 KASSERT(!(gp->flags & G_GEOM_WITHER),
578 ("new provider on WITHERing geom(%s) (class %s)",
579 gp->name, gp->class->name));
580 sb = sbuf_new_auto();
582 sbuf_vprintf(sb, fmt, ap);
585 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
586 pp->name = (char *)(pp + 1);
587 strcpy(pp->name, sbuf_data(sb));
589 LIST_INIT(&pp->consumers);
592 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
593 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
594 LIST_INSERT_HEAD(&gp->provider, pp, provider);
595 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
600 g_error_provider(struct g_provider *pp, int error)
603 /* G_VALID_PROVIDER(pp); We may not have g_topology */
608 g_resize_provider_event(void *arg, int flag)
613 struct g_provider *pp;
614 struct g_consumer *cp, *cp2;
626 G_VALID_PROVIDER(pp);
627 KASSERT(!(pp->flags & G_PF_WITHER),
628 ("g_resize_provider_event but withered"));
629 g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
631 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
633 if (gp->resize == NULL && size < pp->mediasize) {
634 cp->flags |= G_CF_ORPHAN;
635 cp->geom->orphan(cp);
639 pp->mediasize = size;
641 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
643 if ((gp->flags & G_GEOM_WITHER) == 0 && gp->resize != NULL)
648 * After resizing, the previously invalid GEOM class metadata
649 * might become valid. This means we should retaste.
651 LIST_FOREACH(mp, &g_classes, class) {
652 if (mp->taste == NULL)
654 LIST_FOREACH(cp, &pp->consumers, consumers)
655 if (cp->geom->class == mp &&
656 (cp->flags & G_CF_ORPHAN) == 0)
660 mp->taste(mp, pp, 0);
666 g_resize_provider(struct g_provider *pp, off_t size)
670 G_VALID_PROVIDER(pp);
671 if (pp->flags & G_PF_WITHER)
674 if (size == pp->mediasize)
677 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
680 g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
684 #define _PATH_DEV "/dev/"
688 g_provider_by_name(char const *arg)
692 struct g_provider *pp, *wpp;
694 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
695 arg += sizeof(_PATH_DEV) - 1;
698 LIST_FOREACH(cp, &g_classes, class) {
699 LIST_FOREACH(gp, &cp->geom, geom) {
700 LIST_FOREACH(pp, &gp->provider, provider) {
701 if (strcmp(arg, pp->name) != 0)
703 if ((gp->flags & G_GEOM_WITHER) == 0 &&
704 (pp->flags & G_PF_WITHER) == 0)
716 g_destroy_provider(struct g_provider *pp)
721 G_VALID_PROVIDER(pp);
722 KASSERT(LIST_EMPTY(&pp->consumers),
723 ("g_destroy_provider but attached"));
724 KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
725 KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
726 KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
728 LIST_REMOVE(pp, provider);
730 devstat_remove_entry(pp->stat);
732 * If a callback was provided, send notification that the provider
735 if (gp->providergone != NULL)
736 gp->providergone(pp);
739 if ((gp->flags & G_GEOM_WITHER))
744 * We keep the "geoms" list sorted by topological order (== increasing
745 * numerical rank) at all times.
746 * When an attach is done, the attaching geoms rank is invalidated
747 * and it is moved to the tail of the list.
748 * All geoms later in the sequence has their ranks reevaluated in
749 * sequence. If we cannot assign rank to a geom because it's
750 * prerequisites do not have rank, we move that element to the tail
751 * of the sequence with invalid rank as well.
752 * At some point we encounter our original geom and if we stil fail
753 * to assign it a rank, there must be a loop and we fail back to
754 * g_attach() which detach again and calls redo_rank again
755 * to fix up the damage.
756 * It would be much simpler code wise to do it recursively, but we
757 * can't risk that on the kernel stack.
761 redo_rank(struct g_geom *gp)
763 struct g_consumer *cp;
764 struct g_geom *gp1, *gp2;
770 /* Invalidate this geoms rank and move it to the tail */
771 gp1 = TAILQ_NEXT(gp, geoms);
774 TAILQ_REMOVE(&geoms, gp, geoms);
775 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
780 /* re-rank the rest of the sequence */
781 for (; gp1 != NULL; gp1 = gp2) {
784 LIST_FOREACH(cp, &gp1->consumer, consumer) {
785 if (cp->provider == NULL)
787 n = cp->provider->geom->rank;
795 gp2 = TAILQ_NEXT(gp1, geoms);
797 /* got a rank, moving on */
801 /* no rank to original geom means loop */
805 /* no rank, put it at the end move on */
806 TAILQ_REMOVE(&geoms, gp1, geoms);
807 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
813 g_attach(struct g_consumer *cp, struct g_provider *pp)
818 G_VALID_CONSUMER(cp);
819 G_VALID_PROVIDER(pp);
820 g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
821 KASSERT(cp->provider == NULL, ("attach but attached"));
823 LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
824 error = redo_rank(cp->geom);
826 LIST_REMOVE(cp, consumers);
834 g_detach(struct g_consumer *cp)
836 struct g_provider *pp;
839 G_VALID_CONSUMER(cp);
840 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
841 KASSERT(cp->provider != NULL, ("detach but not attached"));
842 KASSERT(cp->acr == 0, ("detach but nonzero acr"));
843 KASSERT(cp->acw == 0, ("detach but nonzero acw"));
844 KASSERT(cp->ace == 0, ("detach but nonzero ace"));
845 KASSERT(cp->nstart == cp->nend,
846 ("detach with active requests"));
848 LIST_REMOVE(cp, consumers);
850 if ((cp->geom->flags & G_GEOM_WITHER) ||
851 (pp->geom->flags & G_GEOM_WITHER) ||
852 (pp->flags & G_PF_WITHER))
860 * Access-check with delta values. The question asked is "can provider
861 * "cp" change the access counters by the relative amounts dc[rwe] ?"
865 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
867 struct g_provider *pp;
872 G_VALID_CONSUMER(cp);
874 KASSERT(pp != NULL, ("access but not attached"));
875 G_VALID_PROVIDER(pp);
877 g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
878 cp, pp->name, dcr, dcw, dce);
880 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
881 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
882 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
883 KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
884 KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
887 * If our class cares about being spoiled, and we have been, we
888 * are probably just ahead of the event telling us that. Fail
889 * now rather than having to unravel this later.
891 if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
892 (dcr > 0 || dcw > 0 || dce > 0))
896 * Figure out what counts the provider would have had, if this
897 * consumer had (r0w0e0) at this time.
899 pr = pp->acr - cp->acr;
900 pw = pp->acw - cp->acw;
901 pe = pp->ace - cp->ace;
904 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
906 cp->acr, cp->acw, cp->ace,
907 pp->acr, pp->acw, pp->ace,
910 /* If foot-shooting is enabled, any open on rank#1 is OK */
911 if ((g_debugflags & 16) && pp->geom->rank == 1)
913 /* If we try exclusive but already write: fail */
914 else if (dce > 0 && pw > 0)
916 /* If we try write but already exclusive: fail */
917 else if (dcw > 0 && pe > 0)
919 /* If we try to open more but provider is error'ed: fail */
920 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0) {
921 printf("%s(%d): provider %s has error %d set\n",
922 __func__, __LINE__, pp->name, pp->error);
928 error = pp->geom->access(pp, dcr, dcw, dce);
929 KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
930 ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed "
931 "closing ->access()", pp->geom->class->name, pp->name, dcr, dcw,
935 * If we open first write, spoil any partner consumers.
936 * If we close last write and provider is not errored,
939 if (pp->acw == 0 && dcw != 0)
941 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
942 !(pp->geom->flags & G_GEOM_WITHER))
943 g_post_event(g_new_provider_event, pp, M_WAITOK,
952 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
953 KASSERT(pp->sectorsize > 0,
954 ("Provider %s lacks sectorsize", pp->name));
955 if ((cp->geom->flags & G_GEOM_WITHER) &&
956 cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
963 g_handleattr_int(struct bio *bp, const char *attribute, int val)
966 return (g_handleattr(bp, attribute, &val, sizeof val));
970 g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val)
973 return (g_handleattr(bp, attribute, &val, sizeof val));
977 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
980 return (g_handleattr(bp, attribute, &val, sizeof val));
984 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
987 return (g_handleattr(bp, attribute, str, 0));
991 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
995 if (strcmp(bp->bio_attribute, attribute))
998 bzero(bp->bio_data, bp->bio_length);
999 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
1001 printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
1002 __func__, bp->bio_to->name,
1003 (intmax_t)bp->bio_length, strlen(val));
1006 } else if (bp->bio_length == len) {
1007 bcopy(val, bp->bio_data, len);
1009 printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
1010 bp->bio_to->name, (intmax_t)bp->bio_length, len);
1014 bp->bio_completed = bp->bio_length;
1015 g_io_deliver(bp, error);
1020 g_std_access(struct g_provider *pp,
1021 int dr __unused, int dw __unused, int de __unused)
1024 g_topology_assert();
1025 G_VALID_PROVIDER(pp);
1030 g_std_done(struct bio *bp)
1034 bp2 = bp->bio_parent;
1035 if (bp2->bio_error == 0)
1036 bp2->bio_error = bp->bio_error;
1037 bp2->bio_completed += bp->bio_completed;
1040 if (bp2->bio_children == bp2->bio_inbed)
1041 g_io_deliver(bp2, bp2->bio_error);
1044 /* XXX: maybe this is only g_slice_spoiled */
1047 g_std_spoiled(struct g_consumer *cp)
1050 struct g_provider *pp;
1052 g_topology_assert();
1053 G_VALID_CONSUMER(cp);
1054 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1055 cp->flags |= G_CF_ORPHAN;
1058 LIST_FOREACH(pp, &gp->provider, provider)
1059 g_orphan_provider(pp, ENXIO);
1060 g_destroy_consumer(cp);
1061 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1064 gp->flags |= G_GEOM_WITHER;
1068 * Spoiling happens when a provider is opened for writing, but consumers
1069 * which are configured by in-band data are attached (slicers for instance).
1070 * Since the write might potentially change the in-band data, such consumers
1071 * need to re-evaluate their existence after the writing session closes.
1072 * We do this by (offering to) tear them down when the open for write happens
1073 * in return for a re-taste when it closes again.
1074 * Together with the fact that such consumers grab an 'e' bit whenever they
1075 * are open, regardless of mode, this ends up DTRT.
1079 g_spoil_event(void *arg, int flag)
1081 struct g_provider *pp;
1082 struct g_consumer *cp, *cp2;
1084 g_topology_assert();
1085 if (flag == EV_CANCEL)
1088 G_VALID_PROVIDER(pp);
1089 g_trace(G_T_TOPOLOGY, "%s %p(%s:%s:%s)", __func__, pp,
1090 pp->geom->class->name, pp->geom->name, pp->name);
1091 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1092 cp2 = LIST_NEXT(cp, consumers);
1093 if ((cp->flags & G_CF_SPOILED) == 0)
1095 cp->flags &= ~G_CF_SPOILED;
1096 if (cp->geom->spoiled == NULL)
1098 cp->geom->spoiled(cp);
1099 g_topology_assert();
1104 g_spoil(struct g_provider *pp, struct g_consumer *cp)
1106 struct g_consumer *cp2;
1108 g_topology_assert();
1109 G_VALID_PROVIDER(pp);
1110 G_VALID_CONSUMER(cp);
1112 LIST_FOREACH(cp2, &pp->consumers, consumers) {
1116 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1117 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1119 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1120 cp2->flags |= G_CF_SPOILED;
1122 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1126 g_media_changed_event(void *arg, int flag)
1128 struct g_provider *pp;
1131 g_topology_assert();
1132 if (flag == EV_CANCEL)
1135 G_VALID_PROVIDER(pp);
1138 * If provider was not open for writing, queue retaste after spoiling.
1139 * If it was, retaste will happen automatically on close.
1141 retaste = (pp->acw == 0 && pp->error == 0 &&
1142 !(pp->geom->flags & G_GEOM_WITHER));
1143 g_spoil_event(arg, flag);
1145 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1149 g_media_changed(struct g_provider *pp, int flag)
1151 struct g_consumer *cp;
1153 LIST_FOREACH(cp, &pp->consumers, consumers)
1154 cp->flags |= G_CF_SPOILED;
1155 return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1159 g_media_gone(struct g_provider *pp, int flag)
1161 struct g_consumer *cp;
1163 LIST_FOREACH(cp, &pp->consumers, consumers)
1164 cp->flags |= G_CF_SPOILED;
1165 return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1169 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1174 error = g_io_getattr(attr, cp, &i, var);
1183 g_get_device_prefix_len(const char *name)
1187 if (strncmp(name, "ada", 3) == 0)
1189 else if (strncmp(name, "ad", 2) == 0)
1193 if (name[len] < '0' || name[len] > '9')
1197 } while (name[len] >= '0' && name[len] <= '9');
1202 g_compare_names(const char *namea, const char *nameb)
1206 if (strcmp(namea, nameb) == 0)
1208 deva = g_get_device_prefix_len(namea);
1211 devb = g_get_device_prefix_len(nameb);
1214 if (strcmp(namea + deva, nameb + devb) == 0)
1220 g_geom_add_alias(struct g_geom *gp, const char *alias)
1222 struct g_geom_alias *gap;
1224 gap = (struct g_geom_alias *)g_malloc(
1225 sizeof(struct g_geom_alias) + strlen(alias) + 1, M_WAITOK);
1226 strcpy((char *)(gap + 1), alias);
1227 gap->ga_alias = (const char *)(gap + 1);
1228 LIST_INSERT_HEAD(&gp->aliases, gap, ga_next);
1231 #if defined(DIAGNOSTIC) || defined(DDB)
1233 * This function walks the mesh and returns a non-zero integer if it
1234 * finds the argument pointer is an object. The return value indicates
1235 * which type of object it is believed to be. If topology is not locked,
1236 * this function is potentially dangerous, but we don't assert that the
1237 * topology lock is held when called from debugger.
1240 g_valid_obj(void const *ptr)
1244 struct g_consumer *cp;
1245 struct g_provider *pp;
1248 if (kdb_active == 0)
1250 g_topology_assert();
1252 LIST_FOREACH(mp, &g_classes, class) {
1255 LIST_FOREACH(gp, &mp->geom, geom) {
1258 LIST_FOREACH(cp, &gp->consumer, consumer)
1261 LIST_FOREACH(pp, &gp->provider, provider)
1272 #define gprintf(...) do { \
1273 db_printf("%*s", indent, ""); \
1274 db_printf(__VA_ARGS__); \
1276 #define gprintln(...) do { \
1277 gprintf(__VA_ARGS__); \
1281 #define ADDFLAG(obj, flag, sflag) do { \
1282 if ((obj)->flags & (flag)) { \
1284 strlcat(str, ",", size); \
1285 strlcat(str, (sflag), size); \
1291 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1296 if (pp->flags == 0) {
1297 strlcpy(str, "NONE", size);
1300 ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1301 ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1306 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1311 if (gp->flags == 0) {
1312 strlcpy(str, "NONE", size);
1315 ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1319 db_show_geom_consumer(int indent, struct g_consumer *cp)
1323 gprintln("consumer: %p", cp);
1324 gprintln(" class: %s (%p)", cp->geom->class->name,
1326 gprintln(" geom: %s (%p)", cp->geom->name, cp->geom);
1327 if (cp->provider == NULL)
1328 gprintln(" provider: none");
1330 gprintln(" provider: %s (%p)", cp->provider->name,
1333 gprintln(" access: r%dw%de%d", cp->acr, cp->acw, cp->ace);
1334 gprintln(" flags: 0x%04x", cp->flags);
1335 gprintln(" nstart: %u", cp->nstart);
1336 gprintln(" nend: %u", cp->nend);
1338 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1339 cp->provider != NULL ? cp->provider->name : "none",
1340 cp->acr, cp->acw, cp->ace);
1342 db_printf(", flags=0x%04x", cp->flags);
1348 db_show_geom_provider(int indent, struct g_provider *pp)
1350 struct g_consumer *cp;
1354 gprintln("provider: %s (%p)", pp->name, pp);
1355 gprintln(" class: %s (%p)", pp->geom->class->name,
1357 gprintln(" geom: %s (%p)", pp->geom->name, pp->geom);
1358 gprintln(" mediasize: %jd", (intmax_t)pp->mediasize);
1359 gprintln(" sectorsize: %u", pp->sectorsize);
1360 gprintln(" stripesize: %u", pp->stripesize);
1361 gprintln(" stripeoffset: %u", pp->stripeoffset);
1362 gprintln(" access: r%dw%de%d", pp->acr, pp->acw,
1364 gprintln(" flags: %s (0x%04x)",
1365 provider_flags_to_string(pp, flags, sizeof(flags)),
1367 gprintln(" error: %d", pp->error);
1368 gprintln(" nstart: %u", pp->nstart);
1369 gprintln(" nend: %u", pp->nend);
1370 if (LIST_EMPTY(&pp->consumers))
1371 gprintln(" consumers: none");
1373 gprintf("provider: %s (%p), access=r%dw%de%d",
1374 pp->name, pp, pp->acr, pp->acw, pp->ace);
1375 if (pp->flags != 0) {
1376 db_printf(", flags=%s (0x%04x)",
1377 provider_flags_to_string(pp, flags, sizeof(flags)),
1382 if (!LIST_EMPTY(&pp->consumers)) {
1383 LIST_FOREACH(cp, &pp->consumers, consumers) {
1384 db_show_geom_consumer(indent + 2, cp);
1392 db_show_geom_geom(int indent, struct g_geom *gp)
1394 struct g_provider *pp;
1395 struct g_consumer *cp;
1399 gprintln("geom: %s (%p)", gp->name, gp);
1400 gprintln(" class: %s (%p)", gp->class->name, gp->class);
1401 gprintln(" flags: %s (0x%04x)",
1402 geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1403 gprintln(" rank: %d", gp->rank);
1404 if (LIST_EMPTY(&gp->provider))
1405 gprintln(" providers: none");
1406 if (LIST_EMPTY(&gp->consumer))
1407 gprintln(" consumers: none");
1409 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1410 if (gp->flags != 0) {
1411 db_printf(", flags=%s (0x%04x)",
1412 geom_flags_to_string(gp, flags, sizeof(flags)),
1417 if (!LIST_EMPTY(&gp->provider)) {
1418 LIST_FOREACH(pp, &gp->provider, provider) {
1419 db_show_geom_provider(indent + 2, pp);
1424 if (!LIST_EMPTY(&gp->consumer)) {
1425 LIST_FOREACH(cp, &gp->consumer, consumer) {
1426 db_show_geom_consumer(indent + 2, cp);
1434 db_show_geom_class(struct g_class *mp)
1438 db_printf("class: %s (%p)\n", mp->name, mp);
1439 LIST_FOREACH(gp, &mp->geom, geom) {
1440 db_show_geom_geom(2, gp);
1447 * Print the GEOM topology or the given object.
1449 DB_SHOW_COMMAND(geom, db_show_geom)
1454 /* No address given, print the entire topology. */
1455 LIST_FOREACH(mp, &g_classes, class) {
1456 db_show_geom_class(mp);
1462 switch (g_valid_obj((void *)addr)) {
1464 db_show_geom_class((struct g_class *)addr);
1467 db_show_geom_geom(0, (struct g_geom *)addr);
1470 db_show_geom_consumer(0, (struct g_consumer *)addr);
1473 db_show_geom_provider(0, (struct g_provider *)addr);
1476 db_printf("Not a GEOM object.\n");
1483 db_print_bio_cmd(struct bio *bp)
1485 db_printf(" cmd: ");
1486 switch (bp->bio_cmd) {
1487 case BIO_READ: db_printf("BIO_READ"); break;
1488 case BIO_WRITE: db_printf("BIO_WRITE"); break;
1489 case BIO_DELETE: db_printf("BIO_DELETE"); break;
1490 case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1491 case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1492 case BIO_CMD0: db_printf("BIO_CMD0"); break;
1493 case BIO_CMD1: db_printf("BIO_CMD1"); break;
1494 case BIO_CMD2: db_printf("BIO_CMD2"); break;
1495 case BIO_ZONE: db_printf("BIO_ZONE"); break;
1496 default: db_printf("UNKNOWN"); break;
1502 db_print_bio_flags(struct bio *bp)
1507 db_printf(" flags: ");
1508 if (bp->bio_flags & BIO_ERROR) {
1509 db_printf("BIO_ERROR");
1512 if (bp->bio_flags & BIO_DONE) {
1513 db_printf("%sBIO_DONE", (comma ? ", " : ""));
1516 if (bp->bio_flags & BIO_ONQUEUE)
1517 db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1522 * Print useful information in a BIO
1524 DB_SHOW_COMMAND(bio, db_show_bio)
1529 bp = (struct bio *)addr;
1530 db_printf("BIO %p\n", bp);
1531 db_print_bio_cmd(bp);
1532 db_print_bio_flags(bp);
1533 db_printf(" cflags: 0x%hx\n", bp->bio_cflags);
1534 db_printf(" pflags: 0x%hx\n", bp->bio_pflags);
1535 db_printf(" offset: %jd\n", (intmax_t)bp->bio_offset);
1536 db_printf(" length: %jd\n", (intmax_t)bp->bio_length);
1537 db_printf(" bcount: %ld\n", bp->bio_bcount);
1538 db_printf(" resid: %ld\n", bp->bio_resid);
1539 db_printf(" completed: %jd\n", (intmax_t)bp->bio_completed);
1540 db_printf(" children: %u\n", bp->bio_children);
1541 db_printf(" inbed: %u\n", bp->bio_inbed);
1542 db_printf(" error: %d\n", bp->bio_error);
1543 db_printf(" parent: %p\n", bp->bio_parent);
1544 db_printf(" driver1: %p\n", bp->bio_driver1);
1545 db_printf(" driver2: %p\n", bp->bio_driver2);
1546 db_printf(" caller1: %p\n", bp->bio_caller1);
1547 db_printf(" caller2: %p\n", bp->bio_caller2);
1548 db_printf(" bio_from: %p\n", bp->bio_from);
1549 db_printf(" bio_to: %p\n", bp->bio_to);
1551 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1552 db_printf(" bio_track_bp: %p\n", bp->bio_track_bp);