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);
251 error = g_unload_class(mp);
254 KASSERT(LIST_EMPTY(&mp->geom),
255 ("Unloaded class (%s) still has geom", mp->name));
263 g_retaste_event(void *arg, int flag)
265 struct g_class *mp, *mp2;
268 struct g_provider *pp;
269 struct g_consumer *cp;
272 if (flag == EV_CANCEL) /* XXX: can't happen ? */
284 g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
286 LIST_FOREACH(mp2, &g_classes, class) {
287 LIST_FOREACH(gp, &mp2->geom, geom) {
288 LIST_FOREACH(pp, &gp->provider, provider) {
289 if (pp->acr || pp->acw || pp->ace)
291 LIST_FOREACH(cp, &pp->consumers, consumers) {
292 if (cp->geom->class == mp &&
293 (cp->flags & G_CF_ORPHAN) == 0)
297 cp->flags |= G_CF_ORPHAN;
298 g_wither_geom(cp->geom, ENXIO);
300 mp->taste(mp, pp, 0);
308 g_retaste(struct g_class *mp)
313 if (mp->taste == NULL)
316 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
321 error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
323 error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
333 g_new_geomf(struct g_class *mp, const char *fmt, ...)
341 sb = sbuf_new_auto();
343 sbuf_vprintf(sb, fmt, ap);
346 gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
347 gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
350 LIST_INIT(&gp->consumer);
351 LIST_INIT(&gp->provider);
352 LIST_INSERT_HEAD(&mp->geom, gp, geom);
353 TAILQ_INSERT_HEAD(&geoms, gp, geoms);
354 strcpy(gp->name, sbuf_data(sb));
356 /* Fill in defaults from class */
357 gp->start = mp->start;
358 gp->spoiled = mp->spoiled;
359 gp->attrchanged = mp->attrchanged;
360 gp->providergone = mp->providergone;
361 gp->dumpconf = mp->dumpconf;
362 gp->access = mp->access;
363 gp->orphan = mp->orphan;
364 gp->ioctl = mp->ioctl;
365 gp->resize = mp->resize;
370 g_destroy_geom(struct g_geom *gp)
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);
390 * This function is called (repeatedly) until the geom has withered away.
393 g_wither_geom(struct g_geom *gp, int error)
395 struct g_provider *pp;
399 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
400 if (!(gp->flags & G_GEOM_WITHER)) {
401 gp->flags |= G_GEOM_WITHER;
402 LIST_FOREACH(pp, &gp->provider, provider)
403 if (!(pp->flags & G_PF_ORPHAN))
404 g_orphan_provider(pp, error);
410 * Convenience function to destroy a particular provider.
413 g_wither_provider(struct g_provider *pp, int error)
416 pp->flags |= G_PF_WITHER;
417 if (!(pp->flags & G_PF_ORPHAN))
418 g_orphan_provider(pp, error);
422 * This function is called (repeatedly) until the has withered away.
425 g_wither_geom_close(struct g_geom *gp, int error)
427 struct g_consumer *cp;
431 g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
432 LIST_FOREACH(cp, &gp->consumer, consumer)
433 if (cp->acr || cp->acw || cp->ace)
434 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
435 g_wither_geom(gp, error);
439 * This function is called (repeatedly) until we cant wash away more
440 * withered bits at present.
446 struct g_geom *gp, *gp2;
447 struct g_provider *pp, *pp2;
448 struct g_consumer *cp, *cp2;
451 LIST_FOREACH(mp, &g_classes, class) {
452 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
453 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
454 if (!(pp->flags & G_PF_WITHER))
456 if (LIST_EMPTY(&pp->consumers))
457 g_destroy_provider(pp);
459 if (!(gp->flags & G_GEOM_WITHER))
461 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
462 if (LIST_EMPTY(&pp->consumers))
463 g_destroy_provider(pp);
465 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
466 if (cp->acr || cp->acw || cp->ace)
468 if (cp->provider != NULL)
470 g_destroy_consumer(cp);
472 if (LIST_EMPTY(&gp->provider) &&
473 LIST_EMPTY(&gp->consumer))
480 g_new_consumer(struct g_geom *gp)
482 struct g_consumer *cp;
486 KASSERT(!(gp->flags & G_GEOM_WITHER),
487 ("g_new_consumer on WITHERing geom(%s) (class %s)",
488 gp->name, gp->class->name));
489 KASSERT(gp->orphan != NULL,
490 ("g_new_consumer on geom(%s) (class %s) without orphan",
491 gp->name, gp->class->name));
493 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
495 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
496 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
497 LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
502 g_destroy_consumer(struct g_consumer *cp)
507 G_VALID_CONSUMER(cp);
508 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
509 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
510 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
511 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
512 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
515 LIST_REMOVE(cp, consumer);
516 devstat_remove_entry(cp->stat);
518 if (gp->flags & G_GEOM_WITHER)
523 g_new_provider_event(void *arg, int flag)
526 struct g_provider *pp;
527 struct g_consumer *cp, *next_cp;
530 if (flag == EV_CANCEL)
535 G_VALID_PROVIDER(pp);
536 KASSERT(!(pp->flags & G_PF_WITHER),
537 ("g_new_provider_event but withered"));
538 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
539 if ((cp->flags & G_CF_ORPHAN) == 0 &&
540 cp->geom->attrchanged != NULL)
541 cp->geom->attrchanged(cp, "GEOM::media");
543 LIST_FOREACH(mp, &g_classes, class) {
544 if (mp->taste == NULL)
546 LIST_FOREACH(cp, &pp->consumers, consumers)
547 if (cp->geom->class == mp &&
548 (cp->flags & G_CF_ORPHAN) == 0)
552 mp->taste(mp, pp, 0);
559 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
561 struct g_provider *pp;
567 KASSERT(gp->access != NULL,
568 ("new provider on geom(%s) without ->access (class %s)",
569 gp->name, gp->class->name));
570 KASSERT(gp->start != NULL,
571 ("new provider on geom(%s) without ->start (class %s)",
572 gp->name, gp->class->name));
573 KASSERT(!(gp->flags & G_GEOM_WITHER),
574 ("new provider on WITHERing geom(%s) (class %s)",
575 gp->name, gp->class->name));
576 sb = sbuf_new_auto();
578 sbuf_vprintf(sb, fmt, ap);
581 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
582 pp->name = (char *)(pp + 1);
583 strcpy(pp->name, sbuf_data(sb));
585 LIST_INIT(&pp->consumers);
588 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
589 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
590 LIST_INSERT_HEAD(&gp->provider, pp, provider);
591 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
596 g_error_provider(struct g_provider *pp, int error)
599 /* G_VALID_PROVIDER(pp); We may not have g_topology */
604 g_resize_provider_event(void *arg, int flag)
609 struct g_provider *pp;
610 struct g_consumer *cp, *cp2;
622 G_VALID_PROVIDER(pp);
623 g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
625 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
627 if (gp->resize == NULL && size < pp->mediasize) {
628 cp->flags |= G_CF_ORPHAN;
629 cp->geom->orphan(cp);
633 pp->mediasize = size;
635 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
637 if (gp->resize != NULL)
642 * After resizing, the previously invalid GEOM class metadata
643 * might become valid. This means we should retaste.
645 LIST_FOREACH(mp, &g_classes, class) {
646 if (mp->taste == NULL)
648 LIST_FOREACH(cp, &pp->consumers, consumers)
649 if (cp->geom->class == mp &&
650 (cp->flags & G_CF_ORPHAN) == 0)
654 mp->taste(mp, pp, 0);
660 g_resize_provider(struct g_provider *pp, off_t size)
664 G_VALID_PROVIDER(pp);
666 if (size == pp->mediasize)
669 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
672 g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
676 #define _PATH_DEV "/dev/"
680 g_provider_by_name(char const *arg)
684 struct g_provider *pp;
686 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
687 arg += sizeof(_PATH_DEV) - 1;
689 LIST_FOREACH(cp, &g_classes, class) {
690 LIST_FOREACH(gp, &cp->geom, geom) {
691 LIST_FOREACH(pp, &gp->provider, provider) {
692 if (!strcmp(arg, pp->name))
702 g_destroy_provider(struct g_provider *pp)
707 G_VALID_PROVIDER(pp);
708 KASSERT(LIST_EMPTY(&pp->consumers),
709 ("g_destroy_provider but attached"));
710 KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
711 KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
712 KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
714 LIST_REMOVE(pp, provider);
716 devstat_remove_entry(pp->stat);
718 * If a callback was provided, send notification that the provider
721 if (gp->providergone != NULL)
722 gp->providergone(pp);
725 if ((gp->flags & G_GEOM_WITHER))
730 * We keep the "geoms" list sorted by topological order (== increasing
731 * numerical rank) at all times.
732 * When an attach is done, the attaching geoms rank is invalidated
733 * and it is moved to the tail of the list.
734 * All geoms later in the sequence has their ranks reevaluated in
735 * sequence. If we cannot assign rank to a geom because it's
736 * prerequisites do not have rank, we move that element to the tail
737 * of the sequence with invalid rank as well.
738 * At some point we encounter our original geom and if we stil fail
739 * to assign it a rank, there must be a loop and we fail back to
740 * g_attach() which detach again and calls redo_rank again
741 * to fix up the damage.
742 * It would be much simpler code wise to do it recursively, but we
743 * can't risk that on the kernel stack.
747 redo_rank(struct g_geom *gp)
749 struct g_consumer *cp;
750 struct g_geom *gp1, *gp2;
756 /* Invalidate this geoms rank and move it to the tail */
757 gp1 = TAILQ_NEXT(gp, geoms);
760 TAILQ_REMOVE(&geoms, gp, geoms);
761 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
766 /* re-rank the rest of the sequence */
767 for (; gp1 != NULL; gp1 = gp2) {
770 LIST_FOREACH(cp, &gp1->consumer, consumer) {
771 if (cp->provider == NULL)
773 n = cp->provider->geom->rank;
781 gp2 = TAILQ_NEXT(gp1, geoms);
783 /* got a rank, moving on */
787 /* no rank to original geom means loop */
791 /* no rank, put it at the end move on */
792 TAILQ_REMOVE(&geoms, gp1, geoms);
793 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
799 g_attach(struct g_consumer *cp, struct g_provider *pp)
804 G_VALID_CONSUMER(cp);
805 G_VALID_PROVIDER(pp);
806 g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
807 KASSERT(cp->provider == NULL, ("attach but attached"));
809 LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
810 error = redo_rank(cp->geom);
812 LIST_REMOVE(cp, consumers);
820 g_detach(struct g_consumer *cp)
822 struct g_provider *pp;
825 G_VALID_CONSUMER(cp);
826 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
827 KASSERT(cp->provider != NULL, ("detach but not attached"));
828 KASSERT(cp->acr == 0, ("detach but nonzero acr"));
829 KASSERT(cp->acw == 0, ("detach but nonzero acw"));
830 KASSERT(cp->ace == 0, ("detach but nonzero ace"));
831 KASSERT(cp->nstart == cp->nend,
832 ("detach with active requests"));
834 LIST_REMOVE(cp, consumers);
836 if ((cp->geom->flags & G_GEOM_WITHER) ||
837 (pp->geom->flags & G_GEOM_WITHER) ||
838 (pp->flags & G_PF_WITHER))
846 * Access-check with delta values. The question asked is "can provider
847 * "cp" change the access counters by the relative amounts dc[rwe] ?"
851 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
853 struct g_provider *pp;
858 G_VALID_CONSUMER(cp);
860 KASSERT(pp != NULL, ("access but not attached"));
861 G_VALID_PROVIDER(pp);
863 g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
864 cp, pp->name, dcr, dcw, dce);
866 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
867 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
868 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
869 KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
870 KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
873 * If our class cares about being spoiled, and we have been, we
874 * are probably just ahead of the event telling us that. Fail
875 * now rather than having to unravel this later.
877 if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
878 (dcr > 0 || dcw > 0 || dce > 0))
882 * Figure out what counts the provider would have had, if this
883 * consumer had (r0w0e0) at this time.
885 pr = pp->acr - cp->acr;
886 pw = pp->acw - cp->acw;
887 pe = pp->ace - cp->ace;
890 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
892 cp->acr, cp->acw, cp->ace,
893 pp->acr, pp->acw, pp->ace,
896 /* If foot-shooting is enabled, any open on rank#1 is OK */
897 if ((g_debugflags & 16) && pp->geom->rank == 1)
899 /* If we try exclusive but already write: fail */
900 else if (dce > 0 && pw > 0)
902 /* If we try write but already exclusive: fail */
903 else if (dcw > 0 && pe > 0)
905 /* If we try to open more but provider is error'ed: fail */
906 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
911 error = pp->geom->access(pp, dcr, dcw, dce);
912 KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
913 ("Geom provider %s::%s failed closing ->access()",
914 pp->geom->class->name, pp->name));
917 * If we open first write, spoil any partner consumers.
918 * If we close last write and provider is not errored,
921 if (pp->acw == 0 && dcw != 0)
923 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
924 !(pp->geom->flags & G_GEOM_WITHER))
925 g_post_event(g_new_provider_event, pp, M_WAITOK,
934 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
935 KASSERT(pp->sectorsize > 0,
936 ("Provider %s lacks sectorsize", pp->name));
937 if ((cp->geom->flags & G_GEOM_WITHER) &&
938 cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
945 g_handleattr_int(struct bio *bp, const char *attribute, int val)
948 return (g_handleattr(bp, attribute, &val, sizeof val));
952 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
955 return (g_handleattr(bp, attribute, &val, sizeof val));
959 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
962 return (g_handleattr(bp, attribute, str, 0));
966 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
970 if (strcmp(bp->bio_attribute, attribute))
973 bzero(bp->bio_data, bp->bio_length);
974 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
976 printf("%s: %s bio_length %jd len %zu -> EFAULT\n",
977 __func__, bp->bio_to->name,
978 (intmax_t)bp->bio_length, strlen(val));
981 } else if (bp->bio_length == len) {
982 bcopy(val, bp->bio_data, len);
984 printf("%s: %s bio_length %jd len %d -> EFAULT\n", __func__,
985 bp->bio_to->name, (intmax_t)bp->bio_length, len);
989 bp->bio_completed = bp->bio_length;
990 g_io_deliver(bp, error);
995 g_std_access(struct g_provider *pp,
996 int dr __unused, int dw __unused, int de __unused)
1000 G_VALID_PROVIDER(pp);
1005 g_std_done(struct bio *bp)
1009 bp2 = bp->bio_parent;
1010 if (bp2->bio_error == 0)
1011 bp2->bio_error = bp->bio_error;
1012 bp2->bio_completed += bp->bio_completed;
1015 if (bp2->bio_children == bp2->bio_inbed)
1016 g_io_deliver(bp2, bp2->bio_error);
1019 /* XXX: maybe this is only g_slice_spoiled */
1022 g_std_spoiled(struct g_consumer *cp)
1025 struct g_provider *pp;
1027 g_topology_assert();
1028 G_VALID_CONSUMER(cp);
1029 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1030 cp->flags |= G_CF_ORPHAN;
1033 LIST_FOREACH(pp, &gp->provider, provider)
1034 g_orphan_provider(pp, ENXIO);
1035 g_destroy_consumer(cp);
1036 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1039 gp->flags |= G_GEOM_WITHER;
1043 * Spoiling happens when a provider is opened for writing, but consumers
1044 * which are configured by in-band data are attached (slicers for instance).
1045 * Since the write might potentially change the in-band data, such consumers
1046 * need to re-evaluate their existence after the writing session closes.
1047 * We do this by (offering to) tear them down when the open for write happens
1048 * in return for a re-taste when it closes again.
1049 * Together with the fact that such consumers grab an 'e' bit whenever they
1050 * are open, regardless of mode, this ends up DTRT.
1054 g_spoil_event(void *arg, int flag)
1056 struct g_provider *pp;
1057 struct g_consumer *cp, *cp2;
1059 g_topology_assert();
1060 if (flag == EV_CANCEL)
1063 G_VALID_PROVIDER(pp);
1064 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1065 cp2 = LIST_NEXT(cp, consumers);
1066 if ((cp->flags & G_CF_SPOILED) == 0)
1068 cp->flags &= ~G_CF_SPOILED;
1069 if (cp->geom->spoiled == NULL)
1071 cp->geom->spoiled(cp);
1072 g_topology_assert();
1077 g_spoil(struct g_provider *pp, struct g_consumer *cp)
1079 struct g_consumer *cp2;
1081 g_topology_assert();
1082 G_VALID_PROVIDER(pp);
1083 G_VALID_CONSUMER(cp);
1085 LIST_FOREACH(cp2, &pp->consumers, consumers) {
1089 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1090 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1092 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1093 cp2->flags |= G_CF_SPOILED;
1095 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1099 g_media_changed_event(void *arg, int flag)
1101 struct g_provider *pp;
1104 g_topology_assert();
1105 if (flag == EV_CANCEL)
1108 G_VALID_PROVIDER(pp);
1111 * If provider was not open for writing, queue retaste after spoiling.
1112 * If it was, retaste will happen automatically on close.
1114 retaste = (pp->acw == 0 && pp->error == 0 &&
1115 !(pp->geom->flags & G_GEOM_WITHER));
1116 g_spoil_event(arg, flag);
1118 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1122 g_media_changed(struct g_provider *pp, int flag)
1124 struct g_consumer *cp;
1126 LIST_FOREACH(cp, &pp->consumers, consumers)
1127 cp->flags |= G_CF_SPOILED;
1128 return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1132 g_media_gone(struct g_provider *pp, int flag)
1134 struct g_consumer *cp;
1136 LIST_FOREACH(cp, &pp->consumers, consumers)
1137 cp->flags |= G_CF_SPOILED;
1138 return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1142 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1147 error = g_io_getattr(attr, cp, &i, var);
1156 g_get_device_prefix_len(const char *name)
1160 if (strncmp(name, "ada", 3) == 0)
1162 else if (strncmp(name, "ad", 2) == 0)
1166 if (name[len] < '0' || name[len] > '9')
1170 } while (name[len] >= '0' && name[len] <= '9');
1175 g_compare_names(const char *namea, const char *nameb)
1179 if (strcmp(namea, nameb) == 0)
1181 deva = g_get_device_prefix_len(namea);
1184 devb = g_get_device_prefix_len(nameb);
1187 if (strcmp(namea + deva, nameb + devb) == 0)
1192 #if defined(DIAGNOSTIC) || defined(DDB)
1194 * This function walks the mesh and returns a non-zero integer if it
1195 * finds the argument pointer is an object. The return value indicates
1196 * which type of object it is believed to be. If topology is not locked,
1197 * this function is potentially dangerous, but we don't assert that the
1198 * topology lock is held when called from debugger.
1201 g_valid_obj(void const *ptr)
1205 struct g_consumer *cp;
1206 struct g_provider *pp;
1209 if (kdb_active == 0)
1211 g_topology_assert();
1213 LIST_FOREACH(mp, &g_classes, class) {
1216 LIST_FOREACH(gp, &mp->geom, geom) {
1219 LIST_FOREACH(cp, &gp->consumer, consumer)
1222 LIST_FOREACH(pp, &gp->provider, provider)
1233 #define gprintf(...) do { \
1234 db_printf("%*s", indent, ""); \
1235 db_printf(__VA_ARGS__); \
1237 #define gprintln(...) do { \
1238 gprintf(__VA_ARGS__); \
1242 #define ADDFLAG(obj, flag, sflag) do { \
1243 if ((obj)->flags & (flag)) { \
1245 strlcat(str, ",", size); \
1246 strlcat(str, (sflag), size); \
1252 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1257 if (pp->flags == 0) {
1258 strlcpy(str, "NONE", size);
1261 ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1262 ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1267 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1272 if (gp->flags == 0) {
1273 strlcpy(str, "NONE", size);
1276 ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1280 db_show_geom_consumer(int indent, struct g_consumer *cp)
1284 gprintln("consumer: %p", cp);
1285 gprintln(" class: %s (%p)", cp->geom->class->name,
1287 gprintln(" geom: %s (%p)", cp->geom->name, cp->geom);
1288 if (cp->provider == NULL)
1289 gprintln(" provider: none");
1291 gprintln(" provider: %s (%p)", cp->provider->name,
1294 gprintln(" access: r%dw%de%d", cp->acr, cp->acw, cp->ace);
1295 gprintln(" flags: 0x%04x", cp->flags);
1296 gprintln(" nstart: %u", cp->nstart);
1297 gprintln(" nend: %u", cp->nend);
1299 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1300 cp->provider != NULL ? cp->provider->name : "none",
1301 cp->acr, cp->acw, cp->ace);
1303 db_printf(", flags=0x%04x", cp->flags);
1309 db_show_geom_provider(int indent, struct g_provider *pp)
1311 struct g_consumer *cp;
1315 gprintln("provider: %s (%p)", pp->name, pp);
1316 gprintln(" class: %s (%p)", pp->geom->class->name,
1318 gprintln(" geom: %s (%p)", pp->geom->name, pp->geom);
1319 gprintln(" mediasize: %jd", (intmax_t)pp->mediasize);
1320 gprintln(" sectorsize: %u", pp->sectorsize);
1321 gprintln(" stripesize: %u", pp->stripesize);
1322 gprintln(" stripeoffset: %u", pp->stripeoffset);
1323 gprintln(" access: r%dw%de%d", pp->acr, pp->acw,
1325 gprintln(" flags: %s (0x%04x)",
1326 provider_flags_to_string(pp, flags, sizeof(flags)),
1328 gprintln(" error: %d", pp->error);
1329 gprintln(" nstart: %u", pp->nstart);
1330 gprintln(" nend: %u", pp->nend);
1331 if (LIST_EMPTY(&pp->consumers))
1332 gprintln(" consumers: none");
1334 gprintf("provider: %s (%p), access=r%dw%de%d",
1335 pp->name, pp, pp->acr, pp->acw, pp->ace);
1336 if (pp->flags != 0) {
1337 db_printf(", flags=%s (0x%04x)",
1338 provider_flags_to_string(pp, flags, sizeof(flags)),
1343 if (!LIST_EMPTY(&pp->consumers)) {
1344 LIST_FOREACH(cp, &pp->consumers, consumers) {
1345 db_show_geom_consumer(indent + 2, cp);
1353 db_show_geom_geom(int indent, struct g_geom *gp)
1355 struct g_provider *pp;
1356 struct g_consumer *cp;
1360 gprintln("geom: %s (%p)", gp->name, gp);
1361 gprintln(" class: %s (%p)", gp->class->name, gp->class);
1362 gprintln(" flags: %s (0x%04x)",
1363 geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1364 gprintln(" rank: %d", gp->rank);
1365 if (LIST_EMPTY(&gp->provider))
1366 gprintln(" providers: none");
1367 if (LIST_EMPTY(&gp->consumer))
1368 gprintln(" consumers: none");
1370 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1371 if (gp->flags != 0) {
1372 db_printf(", flags=%s (0x%04x)",
1373 geom_flags_to_string(gp, flags, sizeof(flags)),
1378 if (!LIST_EMPTY(&gp->provider)) {
1379 LIST_FOREACH(pp, &gp->provider, provider) {
1380 db_show_geom_provider(indent + 2, pp);
1385 if (!LIST_EMPTY(&gp->consumer)) {
1386 LIST_FOREACH(cp, &gp->consumer, consumer) {
1387 db_show_geom_consumer(indent + 2, cp);
1395 db_show_geom_class(struct g_class *mp)
1399 db_printf("class: %s (%p)\n", mp->name, mp);
1400 LIST_FOREACH(gp, &mp->geom, geom) {
1401 db_show_geom_geom(2, gp);
1408 * Print the GEOM topology or the given object.
1410 DB_SHOW_COMMAND(geom, db_show_geom)
1415 /* No address given, print the entire topology. */
1416 LIST_FOREACH(mp, &g_classes, class) {
1417 db_show_geom_class(mp);
1423 switch (g_valid_obj((void *)addr)) {
1425 db_show_geom_class((struct g_class *)addr);
1428 db_show_geom_geom(0, (struct g_geom *)addr);
1431 db_show_geom_consumer(0, (struct g_consumer *)addr);
1434 db_show_geom_provider(0, (struct g_provider *)addr);
1437 db_printf("Not a GEOM object.\n");
1444 db_print_bio_cmd(struct bio *bp)
1446 db_printf(" cmd: ");
1447 switch (bp->bio_cmd) {
1448 case BIO_READ: db_printf("BIO_READ"); break;
1449 case BIO_WRITE: db_printf("BIO_WRITE"); break;
1450 case BIO_DELETE: db_printf("BIO_DELETE"); break;
1451 case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1452 case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1453 case BIO_CMD0: db_printf("BIO_CMD0"); break;
1454 case BIO_CMD1: db_printf("BIO_CMD1"); break;
1455 case BIO_CMD2: db_printf("BIO_CMD2"); break;
1456 default: db_printf("UNKNOWN"); break;
1462 db_print_bio_flags(struct bio *bp)
1467 db_printf(" flags: ");
1468 if (bp->bio_flags & BIO_ERROR) {
1469 db_printf("BIO_ERROR");
1472 if (bp->bio_flags & BIO_DONE) {
1473 db_printf("%sBIO_DONE", (comma ? ", " : ""));
1476 if (bp->bio_flags & BIO_ONQUEUE)
1477 db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1482 * Print useful information in a BIO
1484 DB_SHOW_COMMAND(bio, db_show_bio)
1489 bp = (struct bio *)addr;
1490 db_printf("BIO %p\n", bp);
1491 db_print_bio_cmd(bp);
1492 db_print_bio_flags(bp);
1493 db_printf(" cflags: 0x%hhx\n", bp->bio_cflags);
1494 db_printf(" pflags: 0x%hhx\n", bp->bio_pflags);
1495 db_printf(" offset: %jd\n", (intmax_t)bp->bio_offset);
1496 db_printf(" length: %jd\n", (intmax_t)bp->bio_length);
1497 db_printf(" bcount: %ld\n", bp->bio_bcount);
1498 db_printf(" resid: %ld\n", bp->bio_resid);
1499 db_printf(" completed: %jd\n", (intmax_t)bp->bio_completed);
1500 db_printf(" children: %u\n", bp->bio_children);
1501 db_printf(" inbed: %u\n", bp->bio_inbed);
1502 db_printf(" error: %d\n", bp->bio_error);
1503 db_printf(" parent: %p\n", bp->bio_parent);
1504 db_printf(" driver1: %p\n", bp->bio_driver1);
1505 db_printf(" driver2: %p\n", bp->bio_driver2);
1506 db_printf(" caller1: %p\n", bp->bio_caller1);
1507 db_printf(" caller2: %p\n", bp->bio_caller2);
1508 db_printf(" bio_from: %p\n", bp->bio_from);
1509 db_printf(" bio_to: %p\n", bp->bio_to);