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>
39 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/devicestat.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
51 #include <sys/kthread.h>
53 #include <sys/mutex.h>
54 #include <sys/errno.h>
56 #include <geom/geom.h>
57 #include <geom/geom_dbg.h>
58 #include <geom/geom_int.h>
59 #include <machine/stdarg.h>
69 struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
70 static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
71 char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
75 struct g_provider *pp;
82 g_dbg_printf(const char *classname, int lvl, struct bio *bp,
86 #ifndef PRINTF_BUFR_SIZE
87 #define PRINTF_BUFR_SIZE 64
89 char bufr[PRINTF_BUFR_SIZE];
90 struct sbuf sb, *sbp __unused;
93 sbp = sbuf_new(&sb, bufr, sizeof(bufr), SBUF_FIXEDLEN);
94 KASSERT(sbp != NULL, ("sbuf_new misused?"));
96 sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
98 sbuf_cat(&sb, classname);
100 sbuf_printf(&sb, "[%d]", lvl);
102 va_start(ap, format);
103 sbuf_vprintf(&sb, format, ap);
108 g_format_bio(&sb, bp);
111 /* Terminate the debug line with a single '\n'. */
112 sbuf_nl_terminate(&sb);
114 /* Flush line to printf. */
120 * This event offers a new class a chance to taste all preexisting providers.
123 g_load_class(void *arg, int flag)
126 struct g_class *mp2, *mp;
128 struct g_provider *pp;
131 if (flag == EV_CANCEL) /* XXX: can't happen ? */
143 g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
144 KASSERT(mp->name != NULL && *mp->name != '\0',
145 ("GEOM class has no name"));
146 LIST_FOREACH(mp2, &g_classes, class) {
148 printf("The GEOM class %s is already loaded.\n",
153 } else if (strcmp(mp2->name, mp->name) == 0) {
154 printf("A GEOM class %s is already loaded.\n",
162 LIST_INIT(&mp->geom);
163 LIST_INSERT_HEAD(&g_classes, mp, class);
164 if (mp->init != NULL)
166 if (mp->taste == NULL)
168 LIST_FOREACH(mp2, &g_classes, class) {
171 LIST_FOREACH(gp, &mp2->geom, geom) {
172 LIST_FOREACH(pp, &gp->provider, provider) {
173 mp->taste(mp, pp, 0);
181 g_unload_class(struct g_class *mp)
184 struct g_provider *pp;
185 struct g_consumer *cp;
189 g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
192 LIST_FOREACH(gp, &mp->geom, geom) {
193 /* We refuse to unload if anything is open */
194 LIST_FOREACH(pp, &gp->provider, provider)
195 if (pp->acr || pp->acw || pp->ace) {
199 LIST_FOREACH(cp, &gp->consumer, consumer)
200 if (cp->acr || cp->acw || cp->ace) {
204 /* If the geom is withering, wait for it to finish. */
205 if (gp->flags & G_GEOM_WITHER) {
206 g_topology_sleep(mp, 1);
212 * We allow unloading if we have no geoms, or a class
213 * method we can use to get rid of them.
215 if (!LIST_EMPTY(&mp->geom) && mp->destroy_geom == NULL) {
220 /* 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_INIT(&gp->aliases);
392 LIST_INSERT_HEAD(&mp->geom, gp, geom);
393 TAILQ_INSERT_HEAD(&geoms, gp, geoms);
394 strcpy(gp->name, sbuf_data(sb));
396 /* Fill in defaults from class */
397 gp->start = mp->start;
398 gp->spoiled = mp->spoiled;
399 gp->attrchanged = mp->attrchanged;
400 gp->providergone = mp->providergone;
401 gp->dumpconf = mp->dumpconf;
402 gp->access = mp->access;
403 gp->orphan = mp->orphan;
404 gp->ioctl = mp->ioctl;
405 gp->resize = mp->resize;
410 g_destroy_geom(struct g_geom *gp)
412 struct g_geom_alias *gap, *gaptmp;
416 g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
417 KASSERT(LIST_EMPTY(&gp->consumer),
418 ("g_destroy_geom(%s) with consumer(s) [%p]",
419 gp->name, LIST_FIRST(&gp->consumer)));
420 KASSERT(LIST_EMPTY(&gp->provider),
421 ("g_destroy_geom(%s) with provider(s) [%p]",
422 gp->name, LIST_FIRST(&gp->provider)));
424 LIST_REMOVE(gp, geom);
425 TAILQ_REMOVE(&geoms, gp, geoms);
426 LIST_FOREACH_SAFE(gap, &gp->aliases, ga_next, gaptmp)
433 * This function is called (repeatedly) until the geom has withered away.
436 g_wither_geom(struct g_geom *gp, int error)
438 struct g_provider *pp;
442 g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
443 if (!(gp->flags & G_GEOM_WITHER)) {
444 gp->flags |= G_GEOM_WITHER;
445 LIST_FOREACH(pp, &gp->provider, provider)
446 if (!(pp->flags & G_PF_ORPHAN))
447 g_orphan_provider(pp, error);
453 * Convenience function to destroy a particular provider.
456 g_wither_provider(struct g_provider *pp, int error)
459 pp->flags |= G_PF_WITHER;
460 if (!(pp->flags & G_PF_ORPHAN))
461 g_orphan_provider(pp, error);
465 * This function is called (repeatedly) until the has withered away.
468 g_wither_geom_close(struct g_geom *gp, int error)
470 struct g_consumer *cp;
474 g_trace(G_T_TOPOLOGY, "g_wither_geom_close(%p(%s))", gp, gp->name);
475 LIST_FOREACH(cp, &gp->consumer, consumer)
476 if (cp->acr || cp->acw || cp->ace)
477 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
478 g_wither_geom(gp, error);
482 * This function is called (repeatedly) until we cant wash away more
483 * withered bits at present.
489 struct g_geom *gp, *gp2;
490 struct g_provider *pp, *pp2;
491 struct g_consumer *cp, *cp2;
494 LIST_FOREACH(mp, &g_classes, class) {
495 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
496 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
497 if (!(pp->flags & G_PF_WITHER))
499 if (LIST_EMPTY(&pp->consumers))
500 g_destroy_provider(pp);
502 if (!(gp->flags & G_GEOM_WITHER))
504 LIST_FOREACH_SAFE(pp, &gp->provider, provider, pp2) {
505 if (LIST_EMPTY(&pp->consumers))
506 g_destroy_provider(pp);
508 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp2) {
509 if (cp->acr || cp->acw || cp->ace)
511 if (cp->provider != NULL)
513 g_destroy_consumer(cp);
515 if (LIST_EMPTY(&gp->provider) &&
516 LIST_EMPTY(&gp->consumer))
523 g_new_consumer(struct g_geom *gp)
525 struct g_consumer *cp;
529 KASSERT(!(gp->flags & G_GEOM_WITHER),
530 ("g_new_consumer on WITHERing geom(%s) (class %s)",
531 gp->name, gp->class->name));
532 KASSERT(gp->orphan != NULL,
533 ("g_new_consumer on geom(%s) (class %s) without orphan",
534 gp->name, gp->class->name));
536 cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
538 cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
539 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
540 LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
545 g_destroy_consumer(struct g_consumer *cp)
550 G_VALID_CONSUMER(cp);
551 g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
552 KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
553 KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
554 KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
555 KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
558 LIST_REMOVE(cp, consumer);
559 devstat_remove_entry(cp->stat);
561 if (gp->flags & G_GEOM_WITHER)
566 g_new_provider_event(void *arg, int flag)
569 struct g_provider *pp;
570 struct g_consumer *cp, *next_cp;
573 if (flag == EV_CANCEL)
578 G_VALID_PROVIDER(pp);
579 KASSERT(!(pp->flags & G_PF_WITHER),
580 ("g_new_provider_event but withered"));
581 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, next_cp) {
582 if ((cp->flags & G_CF_ORPHAN) == 0 &&
583 cp->geom->attrchanged != NULL)
584 cp->geom->attrchanged(cp, "GEOM::media");
588 LIST_FOREACH(mp, &g_classes, class) {
589 if (mp->taste == NULL)
591 LIST_FOREACH(cp, &pp->consumers, consumers)
592 if (cp->geom->class == mp &&
593 (cp->flags & G_CF_ORPHAN) == 0)
597 mp->taste(mp, pp, 0);
604 g_new_providerf(struct g_geom *gp, const char *fmt, ...)
606 struct g_provider *pp;
612 KASSERT(gp->access != NULL,
613 ("new provider on geom(%s) without ->access (class %s)",
614 gp->name, gp->class->name));
615 KASSERT(gp->start != NULL,
616 ("new provider on geom(%s) without ->start (class %s)",
617 gp->name, gp->class->name));
618 KASSERT(!(gp->flags & G_GEOM_WITHER),
619 ("new provider on WITHERing geom(%s) (class %s)",
620 gp->name, gp->class->name));
621 sb = sbuf_new_auto();
623 sbuf_vprintf(sb, fmt, ap);
626 pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
627 pp->name = (char *)(pp + 1);
628 strcpy(pp->name, sbuf_data(sb));
630 LIST_INIT(&pp->consumers);
633 pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
634 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
635 LIST_INSERT_HEAD(&gp->provider, pp, provider);
636 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, gp, NULL);
641 g_error_provider(struct g_provider *pp, int error)
644 /* G_VALID_PROVIDER(pp); We may not have g_topology */
649 g_resize_provider_event(void *arg, int flag)
654 struct g_provider *pp;
655 struct g_consumer *cp, *cp2;
667 G_VALID_PROVIDER(pp);
668 KASSERT(!(pp->flags & G_PF_WITHER),
669 ("g_resize_provider_event but withered"));
670 g_trace(G_T_TOPOLOGY, "g_resize_provider_event(%p)", pp);
672 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
674 if (gp->resize == NULL && size < pp->mediasize) {
676 * XXX: g_dev_orphan method does deferred destroying
677 * and it is possible, that other event could already
678 * call the orphan method. Check consumer's flags to
679 * do not schedule it twice.
681 if (cp->flags & G_CF_ORPHAN)
683 cp->flags |= G_CF_ORPHAN;
684 cp->geom->orphan(cp);
688 pp->mediasize = size;
690 LIST_FOREACH_SAFE(cp, &pp->consumers, consumers, cp2) {
692 if ((gp->flags & G_GEOM_WITHER) == 0 && gp->resize != NULL)
697 * After resizing, the previously invalid GEOM class metadata
698 * might become valid. This means we should retaste.
700 LIST_FOREACH(mp, &g_classes, class) {
701 if (mp->taste == NULL)
703 LIST_FOREACH(cp, &pp->consumers, consumers)
704 if (cp->geom->class == mp &&
705 (cp->flags & G_CF_ORPHAN) == 0)
709 mp->taste(mp, pp, 0);
715 g_resize_provider(struct g_provider *pp, off_t size)
719 G_VALID_PROVIDER(pp);
720 if (pp->flags & G_PF_WITHER)
723 if (size == pp->mediasize)
726 hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
729 g_post_event(g_resize_provider_event, hh, M_WAITOK, NULL);
733 #define _PATH_DEV "/dev/"
737 g_provider_by_name(char const *arg)
741 struct g_provider *pp, *wpp;
743 if (strncmp(arg, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
744 arg += sizeof(_PATH_DEV) - 1;
747 LIST_FOREACH(cp, &g_classes, class) {
748 LIST_FOREACH(gp, &cp->geom, geom) {
749 LIST_FOREACH(pp, &gp->provider, provider) {
750 if (strcmp(arg, pp->name) != 0)
752 if ((gp->flags & G_GEOM_WITHER) == 0 &&
753 (pp->flags & G_PF_WITHER) == 0)
765 g_destroy_provider(struct g_provider *pp)
770 G_VALID_PROVIDER(pp);
771 KASSERT(LIST_EMPTY(&pp->consumers),
772 ("g_destroy_provider but attached"));
773 KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
774 KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
775 KASSERT (pp->ace == 0, ("g_destroy_provider with ace"));
777 LIST_REMOVE(pp, provider);
779 devstat_remove_entry(pp->stat);
781 * If a callback was provided, send notification that the provider
784 if (gp->providergone != NULL)
785 gp->providergone(pp);
788 if ((gp->flags & G_GEOM_WITHER))
793 * We keep the "geoms" list sorted by topological order (== increasing
794 * numerical rank) at all times.
795 * When an attach is done, the attaching geoms rank is invalidated
796 * and it is moved to the tail of the list.
797 * All geoms later in the sequence has their ranks reevaluated in
798 * sequence. If we cannot assign rank to a geom because it's
799 * prerequisites do not have rank, we move that element to the tail
800 * of the sequence with invalid rank as well.
801 * At some point we encounter our original geom and if we stil fail
802 * to assign it a rank, there must be a loop and we fail back to
803 * g_attach() which detach again and calls redo_rank again
804 * to fix up the damage.
805 * It would be much simpler code wise to do it recursively, but we
806 * can't risk that on the kernel stack.
810 redo_rank(struct g_geom *gp)
812 struct g_consumer *cp;
813 struct g_geom *gp1, *gp2;
819 /* Invalidate this geoms rank and move it to the tail */
820 gp1 = TAILQ_NEXT(gp, geoms);
823 TAILQ_REMOVE(&geoms, gp, geoms);
824 TAILQ_INSERT_TAIL(&geoms, gp, geoms);
829 /* re-rank the rest of the sequence */
830 for (; gp1 != NULL; gp1 = gp2) {
833 LIST_FOREACH(cp, &gp1->consumer, consumer) {
834 if (cp->provider == NULL)
836 n = cp->provider->geom->rank;
844 gp2 = TAILQ_NEXT(gp1, geoms);
846 /* got a rank, moving on */
850 /* no rank to original geom means loop */
854 /* no rank, put it at the end move on */
855 TAILQ_REMOVE(&geoms, gp1, geoms);
856 TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
862 g_attach(struct g_consumer *cp, struct g_provider *pp)
867 G_VALID_CONSUMER(cp);
868 G_VALID_PROVIDER(pp);
869 g_trace(G_T_TOPOLOGY, "g_attach(%p, %p)", cp, pp);
870 KASSERT(cp->provider == NULL, ("attach but attached"));
872 cp->flags &= ~G_CF_ORPHAN;
873 LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
874 error = redo_rank(cp->geom);
876 LIST_REMOVE(cp, consumers);
884 g_detach(struct g_consumer *cp)
886 struct g_provider *pp;
889 G_VALID_CONSUMER(cp);
890 g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
891 KASSERT(cp->provider != NULL, ("detach but not attached"));
892 KASSERT(cp->acr == 0, ("detach but nonzero acr"));
893 KASSERT(cp->acw == 0, ("detach but nonzero acw"));
894 KASSERT(cp->ace == 0, ("detach but nonzero ace"));
895 KASSERT(cp->nstart == cp->nend,
896 ("detach with active requests"));
898 LIST_REMOVE(cp, consumers);
900 if ((cp->geom->flags & G_GEOM_WITHER) ||
901 (pp->geom->flags & G_GEOM_WITHER) ||
902 (pp->flags & G_PF_WITHER))
910 * Access-check with delta values. The question asked is "can provider
911 * "cp" change the access counters by the relative amounts dc[rwe] ?"
915 g_access(struct g_consumer *cp, int dcr, int dcw, int dce)
917 struct g_provider *pp;
926 G_VALID_CONSUMER(cp);
928 KASSERT(pp != NULL, ("access but not attached"));
929 G_VALID_PROVIDER(pp);
932 g_trace(G_T_ACCESS, "g_access(%p(%s), %d, %d, %d)",
933 cp, pp->name, dcr, dcw, dce);
935 KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
936 KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
937 KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
938 KASSERT(dcr != 0 || dcw != 0 || dce != 0, ("NOP access request"));
939 KASSERT(gp->access != NULL, ("NULL geom->access"));
942 * If our class cares about being spoiled, and we have been, we
943 * are probably just ahead of the event telling us that. Fail
944 * now rather than having to unravel this later.
946 if (cp->geom->spoiled != NULL && (cp->flags & G_CF_SPOILED) &&
947 (dcr > 0 || dcw > 0 || dce > 0))
951 * A number of GEOM classes either need to perform an I/O on the first
952 * open or to acquire a different subsystem's lock. To do that they
953 * may have to drop the topology lock.
954 * Other GEOM classes perform special actions when opening a lower rank
955 * geom for the first time. As a result, more than one thread may
956 * end up performing the special actions.
957 * So, we prevent concurrent "first" opens by marking the consumer with
960 * Note that if the geom's access method never drops the topology lock,
961 * then we will never see G_GEOM_IN_ACCESS here.
963 while ((gp->flags & G_GEOM_IN_ACCESS) != 0) {
965 "%s: race on geom %s via provider %s and consumer of %s",
966 __func__, gp->name, pp->name, cp->geom->name);
967 gp->flags |= G_GEOM_ACCESS_WAIT;
968 g_topology_sleep(gp, 0);
972 * Figure out what counts the provider would have had, if this
973 * consumer had (r0w0e0) at this time.
975 pw = pp->acw - cp->acw;
976 pe = pp->ace - cp->ace;
979 "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
981 cp->acr, cp->acw, cp->ace,
982 pp->acr, pp->acw, pp->ace,
985 /* If foot-shooting is enabled, any open on rank#1 is OK */
986 if ((g_debugflags & 16) && gp->rank == 1)
988 /* If we try exclusive but already write: fail */
989 else if (dce > 0 && pw > 0)
991 /* If we try write but already exclusive: fail */
992 else if (dcw > 0 && pe > 0)
994 /* If we try to open more but provider is error'ed: fail */
995 else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0) {
996 printf("%s(%d): provider %s has error %d set\n",
997 __func__, __LINE__, pp->name, pp->error);
1008 gp->flags |= G_GEOM_IN_ACCESS;
1009 error = gp->access(pp, dcr, dcw, dce);
1010 KASSERT(dcr > 0 || dcw > 0 || dce > 0 || error == 0,
1011 ("Geom provider %s::%s dcr=%d dcw=%d dce=%d error=%d failed "
1012 "closing ->access()", gp->class->name, pp->name, dcr, dcw,
1015 g_topology_assert();
1016 gp->flags &= ~G_GEOM_IN_ACCESS;
1017 KASSERT(cp->acr == sr && cp->acw == sw && cp->ace == se,
1018 ("Access counts changed during geom->access"));
1019 if ((gp->flags & G_GEOM_ACCESS_WAIT) != 0) {
1020 gp->flags &= ~G_GEOM_ACCESS_WAIT;
1026 * If we open first write, spoil any partner consumers.
1027 * If we close last write and provider is not errored,
1030 if (pp->acw == 0 && dcw != 0)
1032 else if (pp->acw != 0 && pp->acw == -dcw && pp->error == 0 &&
1033 !(gp->flags & G_GEOM_WITHER))
1034 g_post_event(g_new_provider_event, pp, M_WAITOK,
1043 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)
1044 KASSERT(pp->sectorsize > 0,
1045 ("Provider %s lacks sectorsize", pp->name));
1046 if ((cp->geom->flags & G_GEOM_WITHER) &&
1047 cp->acr == 0 && cp->acw == 0 && cp->ace == 0)
1054 g_handleattr_int(struct bio *bp, const char *attribute, int val)
1057 return (g_handleattr(bp, attribute, &val, sizeof val));
1061 g_handleattr_uint16_t(struct bio *bp, const char *attribute, uint16_t val)
1064 return (g_handleattr(bp, attribute, &val, sizeof val));
1068 g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
1071 return (g_handleattr(bp, attribute, &val, sizeof val));
1075 g_handleattr_str(struct bio *bp, const char *attribute, const char *str)
1078 return (g_handleattr(bp, attribute, str, 0));
1082 g_handleattr(struct bio *bp, const char *attribute, const void *val, int len)
1086 if (strcmp(bp->bio_attribute, attribute))
1089 bzero(bp->bio_data, bp->bio_length);
1090 if (strlcpy(bp->bio_data, val, bp->bio_length) >=
1092 printf("%s: %s %s bio_length %jd strlen %zu -> EFAULT\n",
1093 __func__, bp->bio_to->name, attribute,
1094 (intmax_t)bp->bio_length, strlen(val));
1097 } else if (bp->bio_length == len) {
1098 bcopy(val, bp->bio_data, len);
1100 printf("%s: %s %s bio_length %jd len %d -> EFAULT\n", __func__,
1101 bp->bio_to->name, attribute, (intmax_t)bp->bio_length, len);
1105 bp->bio_completed = bp->bio_length;
1106 g_io_deliver(bp, error);
1111 g_std_access(struct g_provider *pp,
1112 int dr __unused, int dw __unused, int de __unused)
1115 g_topology_assert();
1116 G_VALID_PROVIDER(pp);
1121 g_std_done(struct bio *bp)
1125 bp2 = bp->bio_parent;
1126 if (bp2->bio_error == 0)
1127 bp2->bio_error = bp->bio_error;
1128 bp2->bio_completed += bp->bio_completed;
1131 if (bp2->bio_children == bp2->bio_inbed)
1132 g_io_deliver(bp2, bp2->bio_error);
1135 /* XXX: maybe this is only g_slice_spoiled */
1138 g_std_spoiled(struct g_consumer *cp)
1141 struct g_provider *pp;
1143 g_topology_assert();
1144 G_VALID_CONSUMER(cp);
1145 g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
1146 cp->flags |= G_CF_ORPHAN;
1149 LIST_FOREACH(pp, &gp->provider, provider)
1150 g_orphan_provider(pp, ENXIO);
1151 g_destroy_consumer(cp);
1152 if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
1155 gp->flags |= G_GEOM_WITHER;
1159 * Spoiling happens when a provider is opened for writing, but consumers
1160 * which are configured by in-band data are attached (slicers for instance).
1161 * Since the write might potentially change the in-band data, such consumers
1162 * need to re-evaluate their existence after the writing session closes.
1163 * We do this by (offering to) tear them down when the open for write happens
1164 * in return for a re-taste when it closes again.
1165 * Together with the fact that such consumers grab an 'e' bit whenever they
1166 * are open, regardless of mode, this ends up DTRT.
1170 g_spoil_event(void *arg, int flag)
1172 struct g_provider *pp;
1173 struct g_consumer *cp, *cp2;
1175 g_topology_assert();
1176 if (flag == EV_CANCEL)
1179 G_VALID_PROVIDER(pp);
1180 g_trace(G_T_TOPOLOGY, "%s %p(%s:%s:%s)", __func__, pp,
1181 pp->geom->class->name, pp->geom->name, pp->name);
1182 for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
1183 cp2 = LIST_NEXT(cp, consumers);
1184 if ((cp->flags & G_CF_SPOILED) == 0)
1186 cp->flags &= ~G_CF_SPOILED;
1187 if (cp->geom->spoiled == NULL)
1189 cp->geom->spoiled(cp);
1190 g_topology_assert();
1195 g_spoil(struct g_provider *pp, struct g_consumer *cp)
1197 struct g_consumer *cp2;
1199 g_topology_assert();
1200 G_VALID_PROVIDER(pp);
1201 G_VALID_CONSUMER(cp);
1203 LIST_FOREACH(cp2, &pp->consumers, consumers) {
1207 KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
1208 KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
1210 KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
1211 cp2->flags |= G_CF_SPOILED;
1213 g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
1217 g_media_changed_event(void *arg, int flag)
1219 struct g_provider *pp;
1222 g_topology_assert();
1223 if (flag == EV_CANCEL)
1226 G_VALID_PROVIDER(pp);
1229 * If provider was not open for writing, queue retaste after spoiling.
1230 * If it was, retaste will happen automatically on close.
1232 retaste = (pp->acw == 0 && pp->error == 0 &&
1233 !(pp->geom->flags & G_GEOM_WITHER));
1234 g_spoil_event(arg, flag);
1236 g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
1240 g_media_changed(struct g_provider *pp, int flag)
1242 struct g_consumer *cp;
1244 LIST_FOREACH(cp, &pp->consumers, consumers)
1245 cp->flags |= G_CF_SPOILED;
1246 return (g_post_event(g_media_changed_event, pp, flag, pp, NULL));
1250 g_media_gone(struct g_provider *pp, int flag)
1252 struct g_consumer *cp;
1254 LIST_FOREACH(cp, &pp->consumers, consumers)
1255 cp->flags |= G_CF_SPOILED;
1256 return (g_post_event(g_spoil_event, pp, flag, pp, NULL));
1260 g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
1265 error = g_io_getattr(attr, cp, &i, var);
1274 g_get_device_prefix_len(const char *name)
1278 if (strncmp(name, "ada", 3) == 0)
1280 else if (strncmp(name, "ad", 2) == 0)
1284 if (name[len] < '0' || name[len] > '9')
1288 } while (name[len] >= '0' && name[len] <= '9');
1293 g_compare_names(const char *namea, const char *nameb)
1297 if (strcmp(namea, nameb) == 0)
1299 deva = g_get_device_prefix_len(namea);
1302 devb = g_get_device_prefix_len(nameb);
1305 if (strcmp(namea + deva, nameb + devb) == 0)
1311 g_geom_add_alias(struct g_geom *gp, const char *alias)
1313 struct g_geom_alias *gap;
1315 gap = (struct g_geom_alias *)g_malloc(
1316 sizeof(struct g_geom_alias) + strlen(alias) + 1, M_WAITOK);
1317 strcpy((char *)(gap + 1), alias);
1318 gap->ga_alias = (const char *)(gap + 1);
1319 LIST_INSERT_HEAD(&gp->aliases, gap, ga_next);
1322 #if defined(DIAGNOSTIC) || defined(DDB)
1324 * This function walks the mesh and returns a non-zero integer if it
1325 * finds the argument pointer is an object. The return value indicates
1326 * which type of object it is believed to be. If topology is not locked,
1327 * this function is potentially dangerous, but we don't assert that the
1328 * topology lock is held when called from debugger.
1331 g_valid_obj(void const *ptr)
1335 struct g_consumer *cp;
1336 struct g_provider *pp;
1339 if (kdb_active == 0)
1341 g_topology_assert();
1343 LIST_FOREACH(mp, &g_classes, class) {
1346 LIST_FOREACH(gp, &mp->geom, geom) {
1349 LIST_FOREACH(cp, &gp->consumer, consumer)
1352 LIST_FOREACH(pp, &gp->provider, provider)
1363 #define gprintf(...) do { \
1364 db_printf("%*s", indent, ""); \
1365 db_printf(__VA_ARGS__); \
1367 #define gprintln(...) do { \
1368 gprintf(__VA_ARGS__); \
1372 #define ADDFLAG(obj, flag, sflag) do { \
1373 if ((obj)->flags & (flag)) { \
1375 strlcat(str, ",", size); \
1376 strlcat(str, (sflag), size); \
1382 provider_flags_to_string(struct g_provider *pp, char *str, size_t size)
1387 if (pp->flags == 0) {
1388 strlcpy(str, "NONE", size);
1391 ADDFLAG(pp, G_PF_WITHER, "G_PF_WITHER");
1392 ADDFLAG(pp, G_PF_ORPHAN, "G_PF_ORPHAN");
1397 geom_flags_to_string(struct g_geom *gp, char *str, size_t size)
1402 if (gp->flags == 0) {
1403 strlcpy(str, "NONE", size);
1406 ADDFLAG(gp, G_GEOM_WITHER, "G_GEOM_WITHER");
1410 db_show_geom_consumer(int indent, struct g_consumer *cp)
1414 gprintln("consumer: %p", cp);
1415 gprintln(" class: %s (%p)", cp->geom->class->name,
1417 gprintln(" geom: %s (%p)", cp->geom->name, cp->geom);
1418 if (cp->provider == NULL)
1419 gprintln(" provider: none");
1421 gprintln(" provider: %s (%p)", cp->provider->name,
1424 gprintln(" access: r%dw%de%d", cp->acr, cp->acw, cp->ace);
1425 gprintln(" flags: 0x%04x", cp->flags);
1426 gprintln(" nstart: %u", cp->nstart);
1427 gprintln(" nend: %u", cp->nend);
1429 gprintf("consumer: %p (%s), access=r%dw%de%d", cp,
1430 cp->provider != NULL ? cp->provider->name : "none",
1431 cp->acr, cp->acw, cp->ace);
1433 db_printf(", flags=0x%04x", cp->flags);
1439 db_show_geom_provider(int indent, struct g_provider *pp)
1441 struct g_consumer *cp;
1445 gprintln("provider: %s (%p)", pp->name, pp);
1446 gprintln(" class: %s (%p)", pp->geom->class->name,
1448 gprintln(" geom: %s (%p)", pp->geom->name, pp->geom);
1449 gprintln(" mediasize: %jd", (intmax_t)pp->mediasize);
1450 gprintln(" sectorsize: %u", pp->sectorsize);
1451 gprintln(" stripesize: %ju", (uintmax_t)pp->stripesize);
1452 gprintln(" stripeoffset: %ju", (uintmax_t)pp->stripeoffset);
1453 gprintln(" access: r%dw%de%d", pp->acr, pp->acw,
1455 gprintln(" flags: %s (0x%04x)",
1456 provider_flags_to_string(pp, flags, sizeof(flags)),
1458 gprintln(" error: %d", pp->error);
1459 gprintln(" nstart: %u", pp->nstart);
1460 gprintln(" nend: %u", pp->nend);
1461 if (LIST_EMPTY(&pp->consumers))
1462 gprintln(" consumers: none");
1464 gprintf("provider: %s (%p), access=r%dw%de%d",
1465 pp->name, pp, pp->acr, pp->acw, pp->ace);
1466 if (pp->flags != 0) {
1467 db_printf(", flags=%s (0x%04x)",
1468 provider_flags_to_string(pp, flags, sizeof(flags)),
1473 if (!LIST_EMPTY(&pp->consumers)) {
1474 LIST_FOREACH(cp, &pp->consumers, consumers) {
1475 db_show_geom_consumer(indent + 2, cp);
1483 db_show_geom_geom(int indent, struct g_geom *gp)
1485 struct g_provider *pp;
1486 struct g_consumer *cp;
1490 gprintln("geom: %s (%p)", gp->name, gp);
1491 gprintln(" class: %s (%p)", gp->class->name, gp->class);
1492 gprintln(" flags: %s (0x%04x)",
1493 geom_flags_to_string(gp, flags, sizeof(flags)), gp->flags);
1494 gprintln(" rank: %d", gp->rank);
1495 if (LIST_EMPTY(&gp->provider))
1496 gprintln(" providers: none");
1497 if (LIST_EMPTY(&gp->consumer))
1498 gprintln(" consumers: none");
1500 gprintf("geom: %s (%p), rank=%d", gp->name, gp, gp->rank);
1501 if (gp->flags != 0) {
1502 db_printf(", flags=%s (0x%04x)",
1503 geom_flags_to_string(gp, flags, sizeof(flags)),
1508 if (!LIST_EMPTY(&gp->provider)) {
1509 LIST_FOREACH(pp, &gp->provider, provider) {
1510 db_show_geom_provider(indent + 2, pp);
1515 if (!LIST_EMPTY(&gp->consumer)) {
1516 LIST_FOREACH(cp, &gp->consumer, consumer) {
1517 db_show_geom_consumer(indent + 2, cp);
1525 db_show_geom_class(struct g_class *mp)
1529 db_printf("class: %s (%p)\n", mp->name, mp);
1530 LIST_FOREACH(gp, &mp->geom, geom) {
1531 db_show_geom_geom(2, gp);
1538 * Print the GEOM topology or the given object.
1540 DB_SHOW_COMMAND(geom, db_show_geom)
1545 /* No address given, print the entire topology. */
1546 LIST_FOREACH(mp, &g_classes, class) {
1547 db_show_geom_class(mp);
1553 switch (g_valid_obj((void *)addr)) {
1555 db_show_geom_class((struct g_class *)addr);
1558 db_show_geom_geom(0, (struct g_geom *)addr);
1561 db_show_geom_consumer(0, (struct g_consumer *)addr);
1564 db_show_geom_provider(0, (struct g_provider *)addr);
1567 db_printf("Not a GEOM object.\n");
1574 db_print_bio_cmd(struct bio *bp)
1576 db_printf(" cmd: ");
1577 switch (bp->bio_cmd) {
1578 case BIO_READ: db_printf("BIO_READ"); break;
1579 case BIO_WRITE: db_printf("BIO_WRITE"); break;
1580 case BIO_DELETE: db_printf("BIO_DELETE"); break;
1581 case BIO_GETATTR: db_printf("BIO_GETATTR"); break;
1582 case BIO_FLUSH: db_printf("BIO_FLUSH"); break;
1583 case BIO_CMD0: db_printf("BIO_CMD0"); break;
1584 case BIO_CMD1: db_printf("BIO_CMD1"); break;
1585 case BIO_CMD2: db_printf("BIO_CMD2"); break;
1586 case BIO_ZONE: db_printf("BIO_ZONE"); break;
1587 default: db_printf("UNKNOWN"); break;
1593 db_print_bio_flags(struct bio *bp)
1598 db_printf(" flags: ");
1599 if (bp->bio_flags & BIO_ERROR) {
1600 db_printf("BIO_ERROR");
1603 if (bp->bio_flags & BIO_DONE) {
1604 db_printf("%sBIO_DONE", (comma ? ", " : ""));
1607 if (bp->bio_flags & BIO_ONQUEUE)
1608 db_printf("%sBIO_ONQUEUE", (comma ? ", " : ""));
1613 * Print useful information in a BIO
1615 DB_SHOW_COMMAND(bio, db_show_bio)
1620 bp = (struct bio *)addr;
1621 db_printf("BIO %p\n", bp);
1622 db_print_bio_cmd(bp);
1623 db_print_bio_flags(bp);
1624 db_printf(" cflags: 0x%hx\n", bp->bio_cflags);
1625 db_printf(" pflags: 0x%hx\n", bp->bio_pflags);
1626 db_printf(" offset: %jd\n", (intmax_t)bp->bio_offset);
1627 db_printf(" length: %jd\n", (intmax_t)bp->bio_length);
1628 db_printf(" bcount: %ld\n", bp->bio_bcount);
1629 db_printf(" resid: %ld\n", bp->bio_resid);
1630 db_printf(" completed: %jd\n", (intmax_t)bp->bio_completed);
1631 db_printf(" children: %u\n", bp->bio_children);
1632 db_printf(" inbed: %u\n", bp->bio_inbed);
1633 db_printf(" error: %d\n", bp->bio_error);
1634 db_printf(" parent: %p\n", bp->bio_parent);
1635 db_printf(" driver1: %p\n", bp->bio_driver1);
1636 db_printf(" driver2: %p\n", bp->bio_driver2);
1637 db_printf(" caller1: %p\n", bp->bio_caller1);
1638 db_printf(" caller2: %p\n", bp->bio_caller2);
1639 db_printf(" bio_from: %p\n", bp->bio_from);
1640 db_printf(" bio_to: %p\n", bp->bio_to);
1642 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1643 db_printf(" bio_track_bp: %p\n", bp->bio_track_bp);