2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
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>
55 #include <geom/geom.h>
56 #include <geom/geom_dbg.h>
57 #include <geom/geom_int.h>
58 #include <machine/stdarg.h>
68 SDT_PROVIDER_DEFINE(geom);
70 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
71 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
72 char *g_wait_event, *g_wait_up, *g_wait_down;
76 struct g_provider *pp;
83 g_dbg_printf(const char *classname, int lvl, struct bio *bp,
87 #ifndef PRINTF_BUFR_SIZE
88 #define PRINTF_BUFR_SIZE 64
90 char bufr[PRINTF_BUFR_SIZE];
91 struct sbuf sb, *sbp __unused;
94 sbp = sbuf_new(&sb, bufr, sizeof(bufr), SBUF_FIXEDLEN);
95 KASSERT(sbp != NULL, ("sbuf_new misused?"));
97 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
99 sbuf_cat(&sb, classname);
101 sbuf_printf(&sb, "[%d]", lvl);
103 va_start(ap, format);
104 sbuf_vprintf(&sb, format, ap);
109 g_format_bio(&sb, bp);
112 /* Terminate the debug line with a single '\n'. */
113 sbuf_nl_terminate(&sb);
115 /* Flush line to printf. */
121 * This event offers a new class a chance to taste all preexisting providers.
124 g_load_class(void *arg, int flag)
127 struct g_class *mp2, *mp;
129 struct g_provider *pp;
132 if (flag == EV_CANCEL) /* XXX: can't happen ? */
144 g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
145 KASSERT(mp->name != NULL && *mp->name != '\0',
146 ("GEOM class has no name"));
147 LIST_FOREACH(mp2, &g_classes, class) {
149 printf("The GEOM class %s is already loaded.\n",
154 } else if (strcmp(mp2->name, mp->name) == 0) {
155 printf("A GEOM class %s is already loaded.\n",
163 LIST_INIT(&mp->geom);
164 LIST_INSERT_HEAD(&g_classes, mp, class);
165 if (mp->init != NULL)
167 if (mp->taste == NULL)
169 LIST_FOREACH(mp2, &g_classes, class) {
172 LIST_FOREACH(gp, &mp2->geom, geom) {
173 LIST_FOREACH(pp, &gp->provider, provider) {
174 mp->taste(mp, pp, 0);
182 g_unload_class(struct g_class *mp)
185 struct g_provider *pp;
186 struct g_consumer *cp;
190 g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
193 LIST_FOREACH(gp, &mp->geom, geom) {
194 /* We refuse to unload if anything is open */
195 LIST_FOREACH(pp, &gp->provider, provider)
196 if (pp->acr || pp->acw || pp->ace) {
200 LIST_FOREACH(cp, &gp->consumer, consumer)
201 if (cp->acr || cp->acw || cp->ace) {
205 /* If the geom is withering, wait for it to finish. */
206 if (gp->flags & G_GEOM_WITHER) {
207 g_topology_sleep(mp, 1);
213 * We allow unloading if we have no geoms, or a class
214 * method we can use to get rid of them.
216 if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
221 /* Bar new entries */
224 LIST_FOREACH(gp, &mp->geom, geom) {
225 error = mp->destroy_geom(NULL, mp, gp);
231 /* Wait for withering to finish. */
233 gp = LIST_FIRST(&mp->geom);
236 KASSERT(gp->flags & G_GEOM_WITHER,
237 ("Non-withering geom in class %s", mp->name));
238 g_topology_sleep(mp, 1);
241 if (mp->fini != NULL)
243 LIST_REMOVE(mp, class);
250 g_modevent(module_t mod, int type, void *data)
254 static int g_ignition;
258 if (mp->version != G_VERSION) {
259 printf("GEOM class %s has Wrong version %x\n",
260 mp->name, mp->version);
270 g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", mp->name);
271 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
274 * Once the system is not cold, MOD_LOAD calls will be
275 * from the userland and the g_event thread will be able
276 * to acknowledge their completion.
280 error = g_post_event(g_load_class, hh, M_WAITOK, NULL);
282 error = g_waitfor_event(g_load_class, hh, M_WAITOK,
290 g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", mp->name);
291 error = g_unload_class(mp);
293 KASSERT(LIST_EMPTY(&mp->geom),
294 ("Unloaded class (%s) still has geom", mp->name));
302 g_retaste_event(void *arg, int flag)
304 struct g_class *mp, *mp2;
307 struct g_provider *pp;
308 struct g_consumer *cp;
311 if (flag == EV_CANCEL) /* XXX: can't happen ? */
313 if (g_shutdown || g_notaste)
323 g_trace(G_T_TOPOLOGY, "g_retaste(%s)", mp->name);
325 LIST_FOREACH(mp2, &g_classes, class) {
326 LIST_FOREACH(gp, &mp2->geom, geom) {
327 LIST_FOREACH(pp, &gp->provider, provider) {
328 if (pp->acr || pp->acw || pp->ace)
330 LIST_FOREACH(cp, &pp->consumers, consumers) {
331 if (cp->geom->class == mp &&
332 (cp->flags & G_CF_ORPHAN) == 0)
336 cp->flags |= G_CF_ORPHAN;
337 g_wither_geom(cp->geom, ENXIO);
339 mp->taste(mp, pp, 0);
347 g_retaste(struct g_class *mp)
352 if (mp->taste == NULL)
355 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
360 error = g_post_event(g_retaste_event, hh, M_WAITOK, NULL);
362 error = g_waitfor_event(g_retaste_event, hh, M_WAITOK, NULL);
372 g_new_geomf(struct g_class *mp, const char *fmt, ...)
380 sb = sbuf_new_auto();
382 sbuf_vprintf(sb, fmt, ap);
385 gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
386 gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
389 LIST_INIT(&gp->consumer);
390 LIST_INIT(&gp->provider);
391 LIST_INSERT_HEAD(&mp->geom, gp, geom);
392 TAILQ_INSERT_HEAD(&geoms, gp, geoms);
393 strcpy(gp->name, sbuf_data(sb));
395 /* Fill in defaults from class */
396 gp->start = mp->start;
397 gp->spoiled = mp->spoiled;
398 gp->attrchanged = mp->attrchanged;
399 gp->providergone = mp->providergone;
400 gp->dumpconf = mp->dumpconf;
401 gp->access = mp->access;
402 gp->orphan = mp->orphan;
403 gp->ioctl = mp->ioctl;
404 gp->resize = mp->resize;
409 g_destroy_geom(struct g_geom *gp)
414 g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
415 KASSERT(LIST_EMPTY(&gp->consumer),
416 ("g_destroy_geom(%s) with consumer(s) [%p]",
417 gp->name, LIST_FIRST(&gp->consumer)));
418 KASSERT(LIST_EMPTY(&gp->provider),
419 ("g_destroy_geom(%s) with provider(s) [%p]",
420 gp->name, LIST_FIRST(&gp->provider)));
422 LIST_REMOVE(gp, geom);
423 TAILQ_REMOVE(&geoms, gp, geoms);
429 * This function is called (repeatedly) until the geom has withered away.
432 g_wither_geom(struct g_geom *gp, int error)
434 struct g_provider *pp;
438 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
439 if (!(gp->flags & G_GEOM_WITHER)) {
440 gp->flags |= G_GEOM_WITHER;
441 LIST_FOREACH(pp, &gp->provider, provider)
442 if (!(pp->flags & G_PF_ORPHAN))
443 g_orphan_provider(pp, error);
449 * Convenience function to destroy a particular provider.
452 g_wither_provider(struct g_provider *pp, int error)
455 pp->flags |= G_PF_WITHER;
456 if (!(pp->flags & G_PF_ORPHAN))
457 g_orphan_provider(pp, error);
461 * This function is called (repeatedly) until the has withered away.
464 g_wither_geom_close(struct g_geom *gp, int error)
466 struct g_consumer *cp;
470 g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
471 LIST_FOREACH(cp, &gp->consumer, consumer)
472 if (cp->acr || cp->acw || cp->ace)
473 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
474 g_wither_geom(gp, error);
478 * This function is called (repeatedly) until we can't wash away more
479 * withered bits at present.
482 g_wither_washer(void)
485 struct g_geom *gp, *gp2;
486 struct g_provider *pp, *pp2;
487 struct g_consumer *cp, *cp2;
490 LIST_FOREACH(mp, &g_classes, class) {
491 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
492 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
493 if (!(pp->flags & G_PF_WITHER))
495 if (LIST_EMPTY(&pp->consumers))
496 g_destroy_provider(pp);
498 if (!(gp->flags & G_GEOM_WITHER))
500 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
501 if (LIST_EMPTY(&pp->consumers))
502 g_destroy_provider(pp);
504 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
505 if (cp->acr || cp->acw || cp->ace)
507 if (cp->provider != NULL)
509 g_destroy_consumer(cp);
511 if (LIST_EMPTY(&gp->provider) &&
512 LIST_EMPTY(&gp->consumer))
519 g_new_consumer(struct g_geom *gp)
521 struct g_consumer *cp;
525 KASSERT(!(gp->flags & G_GEOM_WITHER),
526 ("g_new_consumer on WITHERing geom(%s) (class %s)",
527 gp->name, gp->class->name));
528 KASSERT(gp->orphan != NULL,
529 ("g_new_consumer on geom(%s) (class %s) without orphan",
530 gp->name, gp->class->name));
532 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
534 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
535 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
536 LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
541 g_destroy_consumer(struct g_consumer *cp)
546 G_VALID_CONSUMER(cp);
547 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
548 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
549 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
550 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
551 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
554 LIST_REMOVE(cp, consumer);
555 devstat_remove_entry(cp->stat);
557 if (gp->flags & G_GEOM_WITHER)
562 g_new_provider_event(void *arg, int flag)
565 struct g_provider *pp;
566 struct g_consumer *cp, *next_cp;
569 if (flag == EV_CANCEL)
574 G_VALID_PROVIDER(pp);
575 if ((pp->flags & G_PF_WITHER) != 0)
577 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
578 if ((cp->flags & G_CF_ORPHAN) == 0 &&
579 cp->geom->attrchanged != NULL)
580 cp->geom->attrchanged(cp, "GEOM::media");
584 LIST_FOREACH(mp, &g_classes, class) {
585 if (mp->taste == NULL)
587 LIST_FOREACH(cp, &pp->consumers, consumers)
588 if (cp->geom->class == mp &&
589 (cp->flags & G_CF_ORPHAN) == 0)
593 mp->taste(mp, pp, 0);
599 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
601 struct g_provider *pp;
607 KASSERT(gp->access != NULL,
608 ("new provider on geom(%s) without ->access (class %s)",
609 gp->name, gp->class->name));
610 KASSERT(gp->start != NULL,
611 ("new provider on geom(%s) without ->start (class %s)",
612 gp->name, gp->class->name));
613 KASSERT(!(gp->flags & G_GEOM_WITHER),
614 ("new provider on WITHERing geom(%s) (class %s)",
615 gp->name, gp->class->name));
616 sb = sbuf_new_auto();
618 sbuf_vprintf(sb, fmt, ap);
621 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
622 pp->name = (char *)(pp + 1);
623 strcpy(pp->name, sbuf_data(sb));
625 LIST_INIT(&pp->consumers);
626 LIST_INIT(&pp->aliases);
629 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
630 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
631 LIST_INSERT_HEAD(&gp->provider, pp, provider);
632 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
637 g_provider_add_alias(struct g_provider *pp, const char *fmt, ...)
640 struct g_geom_alias *gap;
644 * Generate the alias string and save it in the list.
646 sb = sbuf_new_auto();
648 sbuf_vprintf(sb, fmt, ap);
652 LIST_FOREACH(gap, &pp->aliases, ga_next) {
653 if (strcmp(gap->ga_alias, sbuf_data(sb)) != 0)
655 /* Don't re-add the same alias. */
660 gap = g_malloc(sizeof(*gap) + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
661 memcpy((char *)(gap + 1), sbuf_data(sb), sbuf_len(sb));
663 gap->ga_alias = (const char *)(gap + 1);
664 LIST_INSERT_HEAD(&pp->aliases, gap, ga_next);
668 g_error_provider(struct g_provider *pp, int error)
671 /* G_VALID_PROVIDER(pp); We may not have g_topology */
676 g_resize_provider_event(void *arg, int flag)
681 struct g_provider *pp;
682 struct g_consumer *cp, *cp2;
694 G_VALID_PROVIDER(pp);
695 KASSERT(!(pp->flags & G_PF_WITHER),
696 ("g_resize_provider_event but withered"));
697 g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
699 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
701 if (gp->resize == NULL && size < pp->mediasize) {
703 * XXX: g_dev_orphan method does deferred destroying
704 * and it is possible, that other event could already
705 * call the orphan method. Check consumer's flags to
706 * do not schedule it twice.
708 if (cp->flags & G_CF_ORPHAN)
710 cp->flags |= G_CF_ORPHAN;
711 cp->geom->orphan(cp);
715 pp->mediasize = size;
717 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
719 if ((gp->flags & G_GEOM_WITHER) == 0 && gp->resize != NULL)
724 * After resizing, the previously invalid GEOM class metadata
725 * might become valid. This means we should retaste.
727 LIST_FOREACH(mp, &g_classes, class) {
728 if (mp->taste == NULL)
730 LIST_FOREACH(cp, &pp->consumers, consumers)
731 if (cp->geom->class == mp &&
732 (cp->flags & G_CF_ORPHAN) == 0)
736 mp->taste(mp, pp, 0);
742 g_resize_provider(struct g_provider *pp, off_t size)
746 G_VALID_PROVIDER(pp);
747 if (pp->flags & G_PF_WITHER)
750 if (size == pp->mediasize)
753 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
756 g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
760 g_provider_by_name(char const *arg)
764 struct g_provider *pp, *wpp;
766 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
767 arg += sizeof(_PATH_DEV) - 1;
770 LIST_FOREACH(cp, &g_classes, class) {
771 LIST_FOREACH(gp, &cp->geom, geom) {
772 LIST_FOREACH(pp, &gp->provider, provider) {
773 if (strcmp(arg, pp->name) != 0)
775 if ((gp->flags & G_GEOM_WITHER) == 0 &&
776 (pp->flags & G_PF_WITHER) == 0)
788 g_destroy_provider(struct g_provider *pp)
791 struct g_geom_alias *gap, *gaptmp;
794 G_VALID_PROVIDER(pp);
795 KASSERT(LIST_EMPTY(&pp->consumers),
796 ("g_destroy_provider but attached"));
797 KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
798 KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
799 KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
801 LIST_REMOVE(pp, provider);
803 devstat_remove_entry(pp->stat);
805 * If a callback was provided, send notification that the provider
808 if (gp->providergone != NULL)
809 gp->providergone(pp);
810 LIST_FOREACH_SAFE(gap, &pp->aliases, ga_next, gaptmp)
813 if ((gp->flags & G_GEOM_WITHER))
818 * We keep the "geoms" list sorted by topological order (== increasing
819 * numerical rank) at all times.
820 * When an attach is done, the attaching geoms rank is invalidated
821 * and it is moved to the tail of the list.
822 * All geoms later in the sequence has their ranks reevaluated in
823 * sequence. If we cannot assign rank to a geom because it's
824 * prerequisites do not have rank, we move that element to the tail
825 * of the sequence with invalid rank as well.
826 * At some point we encounter our original geom and if we stil fail
827 * to assign it a rank, there must be a loop and we fail back to
828 * g_attach() which detach again and calls redo_rank again
829 * to fix up the damage.
830 * It would be much simpler code wise to do it recursively, but we
831 * can't risk that on the kernel stack.
835 redo_rank(struct g_geom *gp)
837 struct g_consumer *cp;
838 struct g_geom *gp1, *gp2;
844 /* Invalidate this geoms rank and move it to the tail */
845 gp1 = TAILQ_NEXT(gp, geoms);
848 TAILQ_REMOVE(&geoms, gp, geoms);
849 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
854 /* re-rank the rest of the sequence */
855 for (; gp1 != NULL; gp1 = gp2) {
858 LIST_FOREACH(cp, &gp1->consumer, consumer) {
859 if (cp->provider == NULL)
861 n = cp->provider->geom->rank;
869 gp2 = TAILQ_NEXT(gp1, geoms);
871 /* got a rank, moving on */
875 /* no rank to original geom means loop */
879 /* no rank, put it at the end move on */
880 TAILQ_REMOVE(&geoms, gp1, geoms);
881 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
887 g_attach(struct g_consumer *cp, struct g_provider *pp)
892 G_VALID_CONSUMER(cp);
893 G_VALID_PROVIDER(pp);
894 g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
895 KASSERT(cp->provider == NULL, ("attach but attached"));
896 if ((pp->flags & (G_PF_ORPHAN | G_PF_WITHER)) != 0)
899 cp->flags &= ~G_CF_ORPHAN;
900 LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
901 error = redo_rank(cp->geom);
903 LIST_REMOVE(cp, consumers);
911 g_detach(struct g_consumer *cp)
913 struct g_provider *pp;
916 G_VALID_CONSUMER(cp);
917 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
918 KASSERT(cp->provider != NULL, ("detach but not attached"));
919 KASSERT(cp->acr == 0, ("detach but nonzero acr"));
920 KASSERT(cp->acw == 0, ("detach but nonzero acw"));
921 KASSERT(cp->ace == 0, ("detach but nonzero ace"));
922 KASSERT(cp->nstart == cp->nend,
923 ("detach with active requests"));
925 LIST_REMOVE(cp, consumers);
927 if ((cp->geom->flags & G_GEOM_WITHER) ||
928 (pp->geom->flags & G_GEOM_WITHER) ||
929 (pp->flags & G_PF_WITHER))
937 * Access-check with delta values. The question asked is "can provider
938 * "cp" change the access counters by the relative amounts dc[rwe] ?"
942 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
944 struct g_provider *pp;
953 G_VALID_CONSUMER(cp);
955 KASSERT(pp != NULL, ("access but not attached"));
956 G_VALID_PROVIDER(pp);
959 g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
960 cp, pp->name, dcr, dcw, dce);
962 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
963 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
964 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
965 KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
966 KASSERT(cp->acr + dcr != 0 || cp->acw + dcw != 0 ||
967 cp->ace + dce != 0 || cp->nstart == cp->nend,
968 ("Last close with active requests"));
969 KASSERT(gp->access != NULL, ("NULL geom->access"));
972 * If our class cares about being spoiled, and we have been, we
973 * are probably just ahead of the event telling us that. Fail
974 * now rather than having to unravel this later.
976 if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
977 (dcr > 0 || dcw > 0 || dce > 0))
981 * A number of GEOM classes either need to perform an I/O on the first
982 * open or to acquire a different subsystem's lock. To do that they
983 * may have to drop the topology lock.
984 * Other GEOM classes perform special actions when opening a lower rank
985 * geom for the first time. As a result, more than one thread may
986 * end up performing the special actions.
987 * So, we prevent concurrent "first" opens by marking the consumer with
990 * Note that if the geom's access method never drops the topology lock,
991 * then we will never see G_GEOM_IN_ACCESS here.
993 while ((gp->flags & G_GEOM_IN_ACCESS) != 0) {
995 "%s: race on geom %s via provider %s and consumer of %s",
996 __func__, gp->name, pp->name, cp->geom->name);
997 gp->flags |= G_GEOM_ACCESS_WAIT;
998 g_topology_sleep(gp, 0);
1002 * Figure out what counts the provider would have had, if this
1003 * consumer had (r0w0e0) at this time.
1005 pw = pp->acw - cp->acw;
1006 pe = pp->ace - cp->ace;
1009 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
1011 cp->acr, cp->acw, cp->ace,
1012 pp->acr, pp->acw, pp->ace,
1015 /* If foot-shooting is enabled, any open on rank#1 is OK */
1016 if ((g_debugflags & G_F_FOOTSHOOTING) && gp->rank == 1)
1018 /* If we try exclusive but already write: fail */
1019 else if (dce > 0 && pw > 0)
1021 /* If we try write but already exclusive: fail */
1022 else if (dcw > 0 && pe > 0)
1024 /* If we try to open more but provider is error'ed: fail */
1025 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0) {
1026 printf("%s(%d): provider %s has error %d set\n",
1027 __func__, __LINE__, pp->name, pp->error);
1038 gp->flags |= G_GEOM_IN_ACCESS;
1039 error = gp->access(pp, dcr, dcw, dce);
1040 KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
1041 ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed "
1042 "closing ->access()", gp->class->name, pp->name, dcr, dcw,
1045 g_topology_assert();
1046 gp->flags &= ~G_GEOM_IN_ACCESS;
1047 KASSERT(cp->acr == sr && cp->acw == sw && cp->ace == se,
1048 ("Access counts changed during geom->access"));
1049 if ((gp->flags & G_GEOM_ACCESS_WAIT) != 0) {
1050 gp->flags &= ~G_GEOM_ACCESS_WAIT;
1056 * If we open first write, spoil any partner consumers.
1057 * If we close last write and provider is not errored,
1060 if (pp->acw == 0 && dcw != 0)
1062 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
1063 !(gp->flags & G_GEOM_WITHER))
1064 g_post_event(g_new_provider_event, pp, M_WAITOK,
1073 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
1074 KASSERT(pp->sectorsize > 0,
1075 ("Provider %s lacks sectorsize", pp->name));
1076 if ((cp->geom->flags & G_GEOM_WITHER) &&
1077 cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
1084 g_handleattr_int(struct bio *bp, const char *attribute, int val)
1087 return (g_handleattr(bp, attribute, &val, sizeof val));
1091 g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val)
1094 return (g_handleattr(bp, attribute, &val, sizeof val));
1098 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
1101 return (g_handleattr(bp, attribute, &val, sizeof val));
1105 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
1108 return (g_handleattr(bp, attribute, str, 0));
1112 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
1116 if (strcmp(bp->bio_attribute, attribute))
1119 bzero(bp->bio_data, bp->bio_length);
1120 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
1122 printf("%s: %s %s bio_length %jd strlen %zu -> EFAULT\n",
1123 __func__, bp->bio_to->name, attribute,
1124 (intmax_t)bp->bio_length, strlen(val));
1127 } else if (bp->bio_length == len) {
1128 bcopy(val, bp->bio_data, len);
1130 printf("%s: %s %s bio_length %jd len %d -> EFAULT\n", __func__,
1131 bp->bio_to->name, attribute, (intmax_t)bp->bio_length, len);
1135 bp->bio_completed = bp->bio_length;
1136 g_io_deliver(bp, error);
1141 g_std_access(struct g_provider *pp,
1142 int dr __unused, int dw __unused, int de __unused)
1145 g_topology_assert();
1146 G_VALID_PROVIDER(pp);
1151 g_std_done(struct bio *bp)
1155 bp2 = bp->bio_parent;
1156 if (bp2->bio_error == 0)
1157 bp2->bio_error = bp->bio_error;
1158 bp2->bio_completed += bp->bio_completed;
1161 if (bp2->bio_children == bp2->bio_inbed) {
1162 if (bp2->bio_cmd == BIO_SPEEDUP)
1163 bp2->bio_completed = bp2->bio_length;
1164 g_io_deliver(bp2, bp2->bio_error);
1168 /* XXX: maybe this is only g_slice_spoiled */
1171 g_std_spoiled(struct g_consumer *cp)
1174 struct g_provider *pp;
1176 g_topology_assert();
1177 G_VALID_CONSUMER(cp);
1178 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1179 cp->flags |= G_CF_ORPHAN;
1182 LIST_FOREACH(pp, &gp->provider, provider)
1183 g_orphan_provider(pp, ENXIO);
1184 g_destroy_consumer(cp);
1185 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1188 gp->flags |= G_GEOM_WITHER;
1192 * Spoiling happens when a provider is opened for writing, but consumers
1193 * which are configured by in-band data are attached (slicers for instance).
1194 * Since the write might potentially change the in-band data, such consumers
1195 * need to re-evaluate their existence after the writing session closes.
1196 * We do this by (offering to) tear them down when the open for write happens
1197 * in return for a re-taste when it closes again.
1198 * Together with the fact that such consumers grab an 'e' bit whenever they
1199 * are open, regardless of mode, this ends up DTRT.
1203 g_spoil_event(void *arg, int flag)
1205 struct g_provider *pp;
1206 struct g_consumer *cp, *cp2;
1208 g_topology_assert();
1209 if (flag == EV_CANCEL)
1212 G_VALID_PROVIDER(pp);
1213 g_trace(G_T_TOPOLOGY, "%s %p(%s:%s:%s)", __func__, pp,
1214 pp->geom->class->name, pp->geom->name, pp->name);
1215 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1216 cp2 = LIST_NEXT(cp, consumers);
1217 if ((cp->flags & G_CF_SPOILED) == 0)
1219 cp->flags &= ~G_CF_SPOILED;
1220 if (cp->geom->spoiled == NULL)
1222 cp->geom->spoiled(cp);
1223 g_topology_assert();
1228 g_spoil(struct g_provider *pp, struct g_consumer *cp)
1230 struct g_consumer *cp2;
1232 g_topology_assert();
1233 G_VALID_PROVIDER(pp);
1234 G_VALID_CONSUMER(cp);
1236 LIST_FOREACH(cp2, &pp->consumers, consumers) {
1240 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1241 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1243 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1244 cp2->flags |= G_CF_SPOILED;
1246 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1250 g_media_changed_event(void *arg, int flag)
1252 struct g_provider *pp;
1255 g_topology_assert();
1256 if (flag == EV_CANCEL)
1259 G_VALID_PROVIDER(pp);
1262 * If provider was not open for writing, queue retaste after spoiling.
1263 * If it was, retaste will happen automatically on close.
1265 retaste = (pp->acw == 0 && pp->error == 0 &&
1266 !(pp->geom->flags & G_GEOM_WITHER));
1267 g_spoil_event(arg, flag);
1269 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1273 g_media_changed(struct g_provider *pp, int flag)
1275 struct g_consumer *cp;
1277 LIST_FOREACH(cp, &pp->consumers, consumers)
1278 cp->flags |= G_CF_SPOILED;
1279 return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1283 g_media_gone(struct g_provider *pp, int flag)
1285 struct g_consumer *cp;
1287 LIST_FOREACH(cp, &pp->consumers, consumers)
1288 cp->flags |= G_CF_SPOILED;
1289 return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1293 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1298 error = g_io_getattr(attr, cp, &i, var);
1307 g_get_device_prefix_len(const char *name)
1311 if (strncmp(name, "ada", 3) == 0)
1313 else if (strncmp(name, "ad", 2) == 0)
1317 if (name[len] < '0' || name[len] > '9')
1321 } while (name[len] >= '0' && name[len] <= '9');
1326 g_compare_names(const char *namea, const char *nameb)
1330 if (strcmp(namea, nameb) == 0)
1332 deva = g_get_device_prefix_len(namea);
1335 devb = g_get_device_prefix_len(nameb);
1338 if (strcmp(namea + deva, nameb + devb) == 0)
1343 #if defined(DIAGNOSTIC) || defined(DDB)
1345 * This function walks the mesh and returns a non-zero integer if it
1346 * finds the argument pointer is an object. The return value indicates
1347 * which type of object it is believed to be. If topology is not locked,
1348 * this function is potentially dangerous, but we don't assert that the
1349 * topology lock is held when called from debugger.
1352 g_valid_obj(void const *ptr)
1356 struct g_consumer *cp;
1357 struct g_provider *pp;
1360 if (kdb_active == 0)
1362 g_topology_assert();
1364 LIST_FOREACH(mp, &g_classes, class) {
1367 LIST_FOREACH(gp, &mp->geom, geom) {
1370 LIST_FOREACH(cp, &gp->consumer, consumer)
1373 LIST_FOREACH(pp, &gp->provider, provider)
1384 #define gprintf(...) do { \
1385 db_printf("%*s", indent, ""); \
1386 db_printf(__VA_ARGS__); \
1388 #define gprintln(...) do { \
1389 gprintf(__VA_ARGS__); \
1393 #define ADDFLAG(obj, flag, sflag) do { \
1394 if ((obj)->flags & (flag)) { \
1396 strlcat(str, ",", size); \
1397 strlcat(str, (sflag), size); \
1403 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1408 if (pp->flags == 0) {
1409 strlcpy(str, "NONE", size);
1412 ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1413 ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1418 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1423 if (gp->flags == 0) {
1424 strlcpy(str, "NONE", size);
1427 ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1431 db_show_geom_consumer(int indent, struct g_consumer *cp)
1435 gprintln("consumer: %p", cp);
1436 gprintln(" class: %s (%p)", cp->geom->class->name,
1438 gprintln(" geom: %s (%p)", cp->geom->name, cp->geom);
1439 if (cp->provider == NULL)
1440 gprintln(" provider: none");
1442 gprintln(" provider: %s (%p)", cp->provider->name,
1445 gprintln(" access: r%dw%de%d", cp->acr, cp->acw, cp->ace);
1446 gprintln(" flags: 0x%04x", cp->flags);
1448 gprintln(" nstart: %u", cp->nstart);
1449 gprintln(" nend: %u", cp->nend);
1452 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1453 cp->provider != NULL ? cp->provider->name : "none",
1454 cp->acr, cp->acw, cp->ace);
1456 db_printf(", flags=0x%04x", cp->flags);
1462 db_show_geom_provider(int indent, struct g_provider *pp)
1464 struct g_consumer *cp;
1468 gprintln("provider: %s (%p)", pp->name, pp);
1469 gprintln(" class: %s (%p)", pp->geom->class->name,
1471 gprintln(" geom: %s (%p)", pp->geom->name, pp->geom);
1472 gprintln(" mediasize: %jd", (intmax_t)pp->mediasize);
1473 gprintln(" sectorsize: %u", pp->sectorsize);
1474 gprintln(" stripesize: %ju", (uintmax_t)pp->stripesize);
1475 gprintln(" stripeoffset: %ju", (uintmax_t)pp->stripeoffset);
1476 gprintln(" access: r%dw%de%d", pp->acr, pp->acw,
1478 gprintln(" flags: %s (0x%04x)",
1479 provider_flags_to_string(pp, flags, sizeof(flags)),
1481 gprintln(" error: %d", pp->error);
1482 if (LIST_EMPTY(&pp->consumers))
1483 gprintln(" consumers: none");
1485 gprintf("provider: %s (%p), access=r%dw%de%d",
1486 pp->name, pp, pp->acr, pp->acw, pp->ace);
1487 if (pp->flags != 0) {
1488 db_printf(", flags=%s (0x%04x)",
1489 provider_flags_to_string(pp, flags, sizeof(flags)),
1494 if (!LIST_EMPTY(&pp->consumers)) {
1495 LIST_FOREACH(cp, &pp->consumers, consumers) {
1496 db_show_geom_consumer(indent + 2, cp);
1504 db_show_geom_geom(int indent, struct g_geom *gp)
1506 struct g_provider *pp;
1507 struct g_consumer *cp;
1511 gprintln("geom: %s (%p)", gp->name, gp);
1512 gprintln(" class: %s (%p)", gp->class->name, gp->class);
1513 gprintln(" flags: %s (0x%04x)",
1514 geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1515 gprintln(" rank: %d", gp->rank);
1516 if (LIST_EMPTY(&gp->provider))
1517 gprintln(" providers: none");
1518 if (LIST_EMPTY(&gp->consumer))
1519 gprintln(" consumers: none");
1521 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1522 if (gp->flags != 0) {
1523 db_printf(", flags=%s (0x%04x)",
1524 geom_flags_to_string(gp, flags, sizeof(flags)),
1529 if (!LIST_EMPTY(&gp->provider)) {
1530 LIST_FOREACH(pp, &gp->provider, provider) {
1531 db_show_geom_provider(indent + 2, pp);
1536 if (!LIST_EMPTY(&gp->consumer)) {
1537 LIST_FOREACH(cp, &gp->consumer, consumer) {
1538 db_show_geom_consumer(indent + 2, cp);
1546 db_show_geom_class(struct g_class *mp)
1550 db_printf("class: %s (%p)\n", mp->name, mp);
1551 LIST_FOREACH(gp, &mp->geom, geom) {
1552 db_show_geom_geom(2, gp);
1559 * Print the GEOM topology or the given object.
1561 DB_SHOW_COMMAND(geom, db_show_geom)
1566 /* No address given, print the entire topology. */
1567 LIST_FOREACH(mp, &g_classes, class) {
1568 db_show_geom_class(mp);
1574 switch (g_valid_obj((void *)addr)) {
1576 db_show_geom_class((struct g_class *)addr);
1579 db_show_geom_geom(0, (struct g_geom *)addr);
1582 db_show_geom_consumer(0, (struct g_consumer *)addr);
1585 db_show_geom_provider(0, (struct g_provider *)addr);
1588 db_printf("Not a GEOM object.\n");
1595 db_print_bio_cmd(struct bio *bp)
1597 db_printf(" cmd: ");
1598 switch (bp->bio_cmd) {
1599 case BIO_READ: db_printf("BIO_READ"); break;
1600 case BIO_WRITE: db_printf("BIO_WRITE"); break;
1601 case BIO_DELETE: db_printf("BIO_DELETE"); break;
1602 case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1603 case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1604 case BIO_CMD0: db_printf("BIO_CMD0"); break;
1605 case BIO_CMD1: db_printf("BIO_CMD1"); break;
1606 case BIO_CMD2: db_printf("BIO_CMD2"); break;
1607 case BIO_ZONE: db_printf("BIO_ZONE"); break;
1608 default: db_printf("UNKNOWN"); break;
1614 db_print_bio_flags(struct bio *bp)
1619 db_printf(" flags: ");
1620 if (bp->bio_flags & BIO_ERROR) {
1621 db_printf("BIO_ERROR");
1624 if (bp->bio_flags & BIO_DONE) {
1625 db_printf("%sBIO_DONE", (comma ? ", " : ""));
1628 if (bp->bio_flags & BIO_ONQUEUE)
1629 db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1634 * Print useful information in a BIO
1636 DB_SHOW_COMMAND(bio, db_show_bio)
1641 bp = (struct bio *)addr;
1642 db_printf("BIO %p\n", bp);
1643 db_print_bio_cmd(bp);
1644 db_print_bio_flags(bp);
1645 db_printf(" cflags: 0x%hx\n", bp->bio_cflags);
1646 db_printf(" pflags: 0x%hx\n", bp->bio_pflags);
1647 db_printf(" offset: %jd\n", (intmax_t)bp->bio_offset);
1648 db_printf(" length: %jd\n", (intmax_t)bp->bio_length);
1649 db_printf(" bcount: %ld\n", bp->bio_bcount);
1650 db_printf(" resid: %ld\n", bp->bio_resid);
1651 db_printf(" completed: %jd\n", (intmax_t)bp->bio_completed);
1652 db_printf(" children: %u\n", bp->bio_children);
1653 db_printf(" inbed: %u\n", bp->bio_inbed);
1654 db_printf(" error: %d\n", bp->bio_error);
1655 db_printf(" parent: %p\n", bp->bio_parent);
1656 db_printf(" driver1: %p\n", bp->bio_driver1);
1657 db_printf(" driver2: %p\n", bp->bio_driver2);
1658 db_printf(" caller1: %p\n", bp->bio_caller1);
1659 db_printf(" caller2: %p\n", bp->bio_caller2);
1660 db_printf(" bio_from: %p\n", bp->bio_from);
1661 db_printf(" bio_to: %p\n", bp->bio_to);
1663 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1664 db_printf(" bio_track_bp: %p\n", bp->bio_track_bp);