geom_subr.c revision 122762
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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
21 *    permission.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/geom/geom_subr.c 122762 2003-11-15 18:44:43Z phk $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/devicestat.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/bio.h>
45#include <sys/sysctl.h>
46#include <sys/proc.h>
47#include <sys/kthread.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/errno.h>
51#include <sys/sbuf.h>
52#include <geom/geom.h>
53#include <geom/geom_int.h>
54#include <machine/stdarg.h>
55
56struct class_list_head g_classes = LIST_HEAD_INITIALIZER(g_classes);
57static struct g_tailq_head geoms = TAILQ_HEAD_INITIALIZER(geoms);
58char *g_wait_event, *g_wait_up, *g_wait_down, *g_wait_sim;
59
60static int g_valid_obj(void const *ptr);
61
62struct g_hh00 {
63	struct g_class	*mp;
64	int		error;
65};
66
67/*
68 * This event offers a new class a chance to taste all preexisting providers.
69 */
70static void
71g_load_class(void *arg, int flag)
72{
73	struct g_hh00 *hh;
74	struct g_class *mp2, *mp;
75	struct g_geom *gp;
76	struct g_provider *pp;
77
78	g_topology_assert();
79	if (flag == EV_CANCEL)	/* XXX: can't happen ? */
80		return;
81	if (g_shutdown)
82		return;
83
84	hh = arg;
85	mp = hh->mp;
86	g_free(hh);
87	g_trace(G_T_TOPOLOGY, "g_load_class(%s)", mp->name);
88	LIST_FOREACH(mp2, &g_classes, class) {
89		KASSERT(mp2 != mp,
90		    ("The GEOM class %s already loaded", mp2->name));
91		KASSERT(strcmp(mp2->name, mp->name) != 0,
92		    ("A GEOM class named %s is already loaded", mp2->name));
93	}
94
95	if (mp->init != NULL)
96		mp->init(mp);
97	LIST_INIT(&mp->geom);
98	LIST_INSERT_HEAD(&g_classes, mp, class);
99	if (mp->taste == NULL)
100		return;
101	LIST_FOREACH(mp2, &g_classes, class) {
102		if (mp == mp2)
103			continue;
104		LIST_FOREACH(gp, &mp2->geom, geom) {
105			LIST_FOREACH(pp, &gp->provider, provider) {
106				mp->taste(mp, pp, 0);
107				g_topology_assert();
108			}
109		}
110	}
111}
112
113static void
114g_unload_class(void *arg, int flag)
115{
116	struct g_hh00 *hh;
117	struct g_class *mp;
118	struct g_geom *gp;
119	struct g_provider *pp;
120	struct g_consumer *cp;
121	int error;
122
123	g_topology_assert();
124	hh = arg;
125	mp = hh->mp;
126	g_trace(G_T_TOPOLOGY, "g_unload_class(%s)", mp->name);
127	if (mp->destroy_geom == NULL) {
128		hh->error = EOPNOTSUPP;
129		return;
130	}
131
132	/* We refuse to unload if anything is open */
133	LIST_FOREACH(gp, &mp->geom, geom) {
134		LIST_FOREACH(pp, &gp->provider, provider)
135			if (pp->acr || pp->acw || pp->ace) {
136				hh->error = EBUSY;
137				return;
138			}
139		LIST_FOREACH(cp, &gp->consumer, consumer)
140			if (cp->acr || cp->acw || cp->ace) {
141				hh->error = EBUSY;
142				return;
143			}
144	}
145
146	/* Bar new entries */
147	mp->taste = NULL;
148	mp->config = NULL;
149
150	error = 0;
151	LIST_FOREACH(gp, &mp->geom, geom) {
152		error = mp->destroy_geom(NULL, mp, gp);
153		if (error != 0)
154			break;
155	}
156	if (error == 0) {
157		LIST_REMOVE(mp, class);
158		if (mp->fini != NULL)
159			mp->fini(mp);
160	}
161	hh->error = error;
162	return;
163}
164
165int
166g_modevent(module_t mod, int type, void *data)
167{
168	struct g_hh00 *hh;
169	int error;
170	static int g_ignition;
171
172	if (!g_ignition) {
173		g_ignition++;
174		g_init();
175	}
176	hh = g_malloc(sizeof *hh, M_WAITOK | M_ZERO);
177	hh->mp = data;
178	error = EOPNOTSUPP;
179	switch (type) {
180	case MOD_LOAD:
181		g_trace(G_T_TOPOLOGY, "g_modevent(%s, LOAD)", hh->mp->name);
182		g_post_event(g_load_class, hh, M_WAITOK, NULL);
183		error = 0;
184		break;
185	case MOD_UNLOAD:
186		g_trace(G_T_TOPOLOGY, "g_modevent(%s, UNLOAD)", hh->mp->name);
187		error = g_waitfor_event(g_unload_class, hh, M_WAITOK, NULL);
188		if (error == 0)
189			error = hh->error;
190		if (error == 0) {
191			g_waitidle();
192			KASSERT(LIST_EMPTY(&hh->mp->geom),
193			    ("Unloaded class (%s) still has geom", hh->mp->name));
194		}
195		g_free(hh);
196		break;
197	}
198	return (error);
199}
200
201struct g_geom *
202g_new_geomf(struct g_class *mp, const char *fmt, ...)
203{
204	struct g_geom *gp;
205	va_list ap;
206	struct sbuf *sb;
207
208	g_topology_assert();
209	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
210	va_start(ap, fmt);
211	sbuf_vprintf(sb, fmt, ap);
212	va_end(ap);
213	sbuf_finish(sb);
214	gp = g_malloc(sizeof *gp, M_WAITOK | M_ZERO);
215	gp->name = g_malloc(sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
216	gp->class = mp;
217	gp->rank = 1;
218	LIST_INIT(&gp->consumer);
219	LIST_INIT(&gp->provider);
220	LIST_INSERT_HEAD(&mp->geom, gp, geom);
221	TAILQ_INSERT_HEAD(&geoms, gp, geoms);
222	strcpy(gp->name, sbuf_data(sb));
223	sbuf_delete(sb);
224	return (gp);
225}
226
227void
228g_destroy_geom(struct g_geom *gp)
229{
230
231	g_trace(G_T_TOPOLOGY, "g_destroy_geom(%p(%s))", gp, gp->name);
232	g_topology_assert();
233	KASSERT(LIST_EMPTY(&gp->consumer),
234	    ("g_destroy_geom(%s) with consumer(s) [%p]",
235	    gp->name, LIST_FIRST(&gp->consumer)));
236	KASSERT(LIST_EMPTY(&gp->provider),
237	    ("g_destroy_geom(%s) with provider(s) [%p]",
238	    gp->name, LIST_FIRST(&gp->consumer)));
239	g_cancel_event(gp);
240	LIST_REMOVE(gp, geom);
241	TAILQ_REMOVE(&geoms, gp, geoms);
242	g_free(gp->name);
243	g_free(gp);
244}
245
246/*
247 * This function is called (repeatedly) until has withered away.
248 */
249void
250g_wither_geom(struct g_geom *gp, int error)
251{
252	struct g_provider *pp, *pp2;
253	struct g_consumer *cp, *cp2;
254	static int once_is_enough;
255
256	if (once_is_enough)
257		return;
258	once_is_enough = 1;
259	g_trace(G_T_TOPOLOGY, "g_wither_geom(%p(%s))", gp, gp->name);
260	g_topology_assert();
261	if (!(gp->flags & G_GEOM_WITHER)) {
262		gp->flags |= G_GEOM_WITHER;
263		LIST_FOREACH(pp, &gp->provider, provider)
264			g_orphan_provider(pp, error);
265	}
266	for (pp = LIST_FIRST(&gp->provider); pp != NULL; pp = pp2) {
267		pp2 = LIST_NEXT(pp, provider);
268		if (!LIST_EMPTY(&pp->consumers))
269			continue;
270		g_destroy_provider(pp);
271	}
272	for (cp = LIST_FIRST(&gp->consumer); cp != NULL; cp = cp2) {
273		cp2 = LIST_NEXT(cp, consumer);
274		if (cp->acr || cp->acw || cp->ace)
275			continue;
276		g_detach(cp);
277		g_destroy_consumer(cp);
278	}
279	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
280		g_destroy_geom(gp);
281	once_is_enough = 0;
282}
283
284struct g_consumer *
285g_new_consumer(struct g_geom *gp)
286{
287	struct g_consumer *cp;
288
289	g_topology_assert();
290	KASSERT(gp->orphan != NULL,
291	    ("g_new_consumer on geom(%s) (class %s) without orphan",
292	    gp->name, gp->class->name));
293
294	cp = g_malloc(sizeof *cp, M_WAITOK | M_ZERO);
295	cp->geom = gp;
296	cp->stat = devstat_new_entry(cp, -1, 0, DEVSTAT_ALL_SUPPORTED,
297	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
298	LIST_INSERT_HEAD(&gp->consumer, cp, consumer);
299	return(cp);
300}
301
302void
303g_destroy_consumer(struct g_consumer *cp)
304{
305	struct g_geom *gp;
306
307	g_trace(G_T_TOPOLOGY, "g_destroy_consumer(%p)", cp);
308	g_topology_assert();
309	KASSERT (cp->provider == NULL, ("g_destroy_consumer but attached"));
310	KASSERT (cp->acr == 0, ("g_destroy_consumer with acr"));
311	KASSERT (cp->acw == 0, ("g_destroy_consumer with acw"));
312	KASSERT (cp->ace == 0, ("g_destroy_consumer with ace"));
313	g_cancel_event(cp);
314	gp = cp->geom;
315	LIST_REMOVE(cp, consumer);
316	devstat_remove_entry(cp->stat);
317	g_free(cp);
318	if (gp->flags & G_GEOM_WITHER)
319		g_wither_geom(gp, 0);
320}
321
322static void
323g_new_provider_event(void *arg, int flag)
324{
325	struct g_class *mp;
326	struct g_provider *pp;
327	struct g_consumer *cp;
328	int i;
329
330	g_topology_assert();
331	if (flag == EV_CANCEL)
332		return;
333	if (g_shutdown)
334		return;
335	pp = arg;
336	LIST_FOREACH(mp, &g_classes, class) {
337		if (mp->taste == NULL)
338			continue;
339		i = 1;
340		LIST_FOREACH(cp, &pp->consumers, consumers)
341			if (cp->geom->class == mp)
342				i = 0;
343		if (!i)
344			continue;
345		mp->taste(mp, pp, 0);
346		g_topology_assert();
347		/*
348		 * XXX: Bandaid for 5.2-RELEASE
349		 * XXX: DO NOT REPLICATE THIS CODE!
350		 */
351		if (!g_valid_obj(pp)) {
352			printf("g_provider %p disappeared while tasting\n", pp);
353			return;
354		}
355	}
356}
357
358
359struct g_provider *
360g_new_providerf(struct g_geom *gp, const char *fmt, ...)
361{
362	struct g_provider *pp;
363	struct sbuf *sb;
364	va_list ap;
365
366	g_topology_assert();
367	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
368	va_start(ap, fmt);
369	sbuf_vprintf(sb, fmt, ap);
370	va_end(ap);
371	sbuf_finish(sb);
372	pp = g_malloc(sizeof *pp + sbuf_len(sb) + 1, M_WAITOK | M_ZERO);
373	pp->name = (char *)(pp + 1);
374	strcpy(pp->name, sbuf_data(sb));
375	sbuf_delete(sb);
376	LIST_INIT(&pp->consumers);
377	pp->error = ENXIO;
378	pp->geom = gp;
379	pp->stat = devstat_new_entry(pp, -1, 0, DEVSTAT_ALL_SUPPORTED,
380	    DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
381	LIST_INSERT_HEAD(&gp->provider, pp, provider);
382	g_post_event(g_new_provider_event, pp, M_WAITOK, pp, NULL);
383	return (pp);
384}
385
386void
387g_error_provider(struct g_provider *pp, int error)
388{
389
390	pp->error = error;
391}
392
393struct g_provider *
394g_provider_by_name(char const *arg)
395{
396	struct g_class *cp;
397	struct g_geom *gp;
398	struct g_provider *pp;
399
400	LIST_FOREACH(cp, &g_classes, class) {
401		LIST_FOREACH(gp, &cp->geom, geom) {
402			LIST_FOREACH(pp, &gp->provider, provider) {
403				if (!strcmp(arg, pp->name))
404					return (pp);
405			}
406		}
407	}
408	return (NULL);
409}
410
411void
412g_destroy_provider(struct g_provider *pp)
413{
414	struct g_geom *gp;
415
416	g_topology_assert();
417	KASSERT(LIST_EMPTY(&pp->consumers),
418	    ("g_destroy_provider but attached"));
419	KASSERT (pp->acr == 0, ("g_destroy_provider with acr"));
420	KASSERT (pp->acw == 0, ("g_destroy_provider with acw"));
421	KASSERT (pp->acw == 0, ("g_destroy_provider with ace"));
422	g_cancel_event(pp);
423	LIST_REMOVE(pp, provider);
424	gp = pp->geom;
425	devstat_remove_entry(pp->stat);
426	g_free(pp);
427	if ((gp->flags & G_GEOM_WITHER))
428		g_wither_geom(gp, 0);
429}
430
431/*
432 * We keep the "geoms" list sorted by topological order (== increasing
433 * numerical rank) at all times.
434 * When an attach is done, the attaching geoms rank is invalidated
435 * and it is moved to the tail of the list.
436 * All geoms later in the sequence has their ranks reevaluated in
437 * sequence.  If we cannot assign rank to a geom because it's
438 * prerequisites do not have rank, we move that element to the tail
439 * of the sequence with invalid rank as well.
440 * At some point we encounter our original geom and if we stil fail
441 * to assign it a rank, there must be a loop and we fail back to
442 * g_attach() which detach again and calls redo_rank again
443 * to fix up the damage.
444 * It would be much simpler code wise to do it recursively, but we
445 * can't risk that on the kernel stack.
446 */
447
448static int
449redo_rank(struct g_geom *gp)
450{
451	struct g_consumer *cp;
452	struct g_geom *gp1, *gp2;
453	int n, m;
454
455	g_topology_assert();
456
457	/* Invalidate this geoms rank and move it to the tail */
458	gp1 = TAILQ_NEXT(gp, geoms);
459	if (gp1 != NULL) {
460		gp->rank = 0;
461		TAILQ_REMOVE(&geoms, gp, geoms);
462		TAILQ_INSERT_TAIL(&geoms, gp, geoms);
463	} else {
464		gp1 = gp;
465	}
466
467	/* re-rank the rest of the sequence */
468	for (; gp1 != NULL; gp1 = gp2) {
469		gp1->rank = 0;
470		m = 1;
471		LIST_FOREACH(cp, &gp1->consumer, consumer) {
472			if (cp->provider == NULL)
473				continue;
474			n = cp->provider->geom->rank;
475			if (n == 0) {
476				m = 0;
477				break;
478			} else if (n >= m)
479				m = n + 1;
480		}
481		gp1->rank = m;
482		gp2 = TAILQ_NEXT(gp1, geoms);
483
484		/* got a rank, moving on */
485		if (m != 0)
486			continue;
487
488		/* no rank to original geom means loop */
489		if (gp == gp1)
490			return (ELOOP);
491
492		/* no rank, put it at the end move on */
493		TAILQ_REMOVE(&geoms, gp1, geoms);
494		TAILQ_INSERT_TAIL(&geoms, gp1, geoms);
495	}
496	return (0);
497}
498
499int
500g_attach(struct g_consumer *cp, struct g_provider *pp)
501{
502	int error;
503
504	g_topology_assert();
505	KASSERT(cp->provider == NULL, ("attach but attached"));
506	cp->provider = pp;
507	LIST_INSERT_HEAD(&pp->consumers, cp, consumers);
508	error = redo_rank(cp->geom);
509	if (error) {
510		LIST_REMOVE(cp, consumers);
511		cp->provider = NULL;
512		redo_rank(cp->geom);
513	}
514	return (error);
515}
516
517void
518g_detach(struct g_consumer *cp)
519{
520	struct g_provider *pp;
521
522	g_trace(G_T_TOPOLOGY, "g_detach(%p)", cp);
523	KASSERT(cp != (void*)0xd0d0d0d0, ("ARGH!"));
524	g_topology_assert();
525	KASSERT(cp->provider != NULL, ("detach but not attached"));
526	KASSERT(cp->acr == 0, ("detach but nonzero acr"));
527	KASSERT(cp->acw == 0, ("detach but nonzero acw"));
528	KASSERT(cp->ace == 0, ("detach but nonzero ace"));
529	KASSERT(cp->nstart == cp->nend,
530	    ("detach with active requests"));
531	pp = cp->provider;
532	LIST_REMOVE(cp, consumers);
533	cp->provider = NULL;
534	if (pp->geom->flags & G_GEOM_WITHER)
535		g_wither_geom(pp->geom, 0);
536	else if (pp->flags & G_PF_WITHER)
537		g_destroy_provider(pp);
538	redo_rank(cp->geom);
539}
540
541
542/*
543 * g_access_abs()
544 *
545 * Access-check with absolute new values:  Just fall through
546 * and use the relative version.
547 */
548int
549g_access_abs(struct g_consumer *cp, int acr, int acw, int ace)
550{
551
552	g_topology_assert();
553	return(g_access_rel(cp,
554		acr - cp->acr,
555		acw - cp->acw,
556		ace - cp->ace));
557}
558
559/*
560 * g_access_rel()
561 *
562 * Access-check with delta values.  The question asked is "can provider
563 * "cp" change the access counters by the relative amounts dc[rwe] ?"
564 */
565
566int
567g_access_rel(struct g_consumer *cp, int dcr, int dcw, int dce)
568{
569	struct g_provider *pp;
570	int pr,pw,pe;
571	int error;
572
573	pp = cp->provider;
574
575	g_trace(G_T_ACCESS, "g_access_rel(%p(%s), %d, %d, %d)",
576	    cp, pp->name, dcr, dcw, dce);
577
578	g_topology_assert();
579	KASSERT(cp->provider != NULL, ("access but not attached"));
580	KASSERT(cp->acr + dcr >= 0, ("access resulting in negative acr"));
581	KASSERT(cp->acw + dcw >= 0, ("access resulting in negative acw"));
582	KASSERT(cp->ace + dce >= 0, ("access resulting in negative ace"));
583	KASSERT(pp->geom->access != NULL, ("NULL geom->access"));
584
585	/*
586	 * If our class cares about being spoiled, and we have been, we
587	 * are probably just ahead of the event telling us that.  Fail
588	 * now rather than having to unravel this later.
589	 */
590	if (cp->geom->spoiled != NULL && cp->spoiled) {
591		KASSERT(dcr <= 0, ("spoiled but dcr = %d", dcr));
592		KASSERT(dcw <= 0, ("spoiled but dce = %d", dcw));
593		KASSERT(dce <= 0, ("spoiled but dcw = %d", dce));
594	}
595
596	/*
597	 * Figure out what counts the provider would have had, if this
598	 * consumer had (r0w0e0) at this time.
599	 */
600	pr = pp->acr - cp->acr;
601	pw = pp->acw - cp->acw;
602	pe = pp->ace - cp->ace;
603
604	g_trace(G_T_ACCESS,
605    "open delta:[r%dw%de%d] old:[r%dw%de%d] provider:[r%dw%de%d] %p(%s)",
606	    dcr, dcw, dce,
607	    cp->acr, cp->acw, cp->ace,
608	    pp->acr, pp->acw, pp->ace,
609	    pp, pp->name);
610
611	/* If foot-shooting is enabled, any open on rank#1 is OK */
612	if ((g_debugflags & 16) && pp->geom->rank == 1)
613		;
614	/* If we try exclusive but already write: fail */
615	else if (dce > 0 && pw > 0)
616		return (EPERM);
617	/* If we try write but already exclusive: fail */
618	else if (dcw > 0 && pe > 0)
619		return (EPERM);
620	/* If we try to open more but provider is error'ed: fail */
621	else if ((dcr > 0 || dcw > 0 || dce > 0) && pp->error != 0)
622		return (pp->error);
623
624	/* Ok then... */
625
626	error = pp->geom->access(pp, dcr, dcw, dce);
627	if (!error) {
628		/*
629		 * If we open first write, spoil any partner consumers.
630		 * If we close last write, trigger re-taste.
631		 */
632		if (pp->acw == 0 && dcw != 0)
633			g_spoil(pp, cp);
634		else if (pp->acw != 0 && pp->acw == -dcw &&
635		    !(pp->geom->flags & G_GEOM_WITHER))
636			g_post_event(g_new_provider_event, pp, M_WAITOK,
637			    pp, NULL);
638
639		pp->acr += dcr;
640		pp->acw += dcw;
641		pp->ace += dce;
642		cp->acr += dcr;
643		cp->acw += dcw;
644		cp->ace += dce;
645	}
646	return (error);
647}
648
649int
650g_handleattr_int(struct bio *bp, const char *attribute, int val)
651{
652
653	return (g_handleattr(bp, attribute, &val, sizeof val));
654}
655
656int
657g_handleattr_off_t(struct bio *bp, const char *attribute, off_t val)
658{
659
660	return (g_handleattr(bp, attribute, &val, sizeof val));
661}
662
663int
664g_handleattr(struct bio *bp, const char *attribute, void *val, int len)
665{
666	int error;
667
668	if (strcmp(bp->bio_attribute, attribute))
669		return (0);
670	if (bp->bio_length != len) {
671		printf("bio_length %jd len %d -> EFAULT\n",
672		    (intmax_t)bp->bio_length, len);
673		error = EFAULT;
674	} else {
675		error = 0;
676		bcopy(val, bp->bio_data, len);
677		bp->bio_completed = len;
678	}
679	g_io_deliver(bp, error);
680	return (1);
681}
682
683int
684g_std_access(struct g_provider *pp __unused,
685	int dr __unused, int dw __unused, int de __unused)
686{
687
688        return (0);
689}
690
691void
692g_std_done(struct bio *bp)
693{
694	struct bio *bp2;
695
696	bp2 = bp->bio_parent;
697	if (bp2->bio_error == 0)
698		bp2->bio_error = bp->bio_error;
699	bp2->bio_completed += bp->bio_completed;
700	g_destroy_bio(bp);
701	bp2->bio_inbed++;
702	if (bp2->bio_children == bp2->bio_inbed)
703		g_io_deliver(bp2, bp2->bio_error);
704}
705
706/* XXX: maybe this is only g_slice_spoiled */
707
708void
709g_std_spoiled(struct g_consumer *cp)
710{
711	struct g_geom *gp;
712	struct g_provider *pp;
713
714	g_trace(G_T_TOPOLOGY, "g_std_spoiled(%p)", cp);
715	g_topology_assert();
716	g_detach(cp);
717	gp = cp->geom;
718	LIST_FOREACH(pp, &gp->provider, provider)
719		g_orphan_provider(pp, ENXIO);
720	g_destroy_consumer(cp);
721	if (LIST_EMPTY(&gp->provider) && LIST_EMPTY(&gp->consumer))
722		g_destroy_geom(gp);
723	else
724		gp->flags |= G_GEOM_WITHER;
725}
726
727/*
728 * Spoiling happens when a provider is opened for writing, but consumers
729 * which are configured by in-band data are attached (slicers for instance).
730 * Since the write might potentially change the in-band data, such consumers
731 * need to re-evaluate their existence after the writing session closes.
732 * We do this by (offering to) tear them down when the open for write happens
733 * in return for a re-taste when it closes again.
734 * Together with the fact that such consumers grab an 'e' bit whenever they
735 * are open, regardless of mode, this ends up DTRT.
736 */
737
738static void
739g_spoil_event(void *arg, int flag)
740{
741	struct g_provider *pp;
742	struct g_consumer *cp, *cp2;
743
744	g_topology_assert();
745	if (flag == EV_CANCEL)
746		return;
747	pp = arg;
748	for (cp = LIST_FIRST(&pp->consumers); cp != NULL; cp = cp2) {
749		cp2 = LIST_NEXT(cp, consumers);
750		if (!cp->spoiled)
751			continue;
752		cp->spoiled = 0;
753		if (cp->geom->spoiled == NULL)
754			continue;
755		cp->geom->spoiled(cp);
756		g_topology_assert();
757	}
758}
759
760void
761g_spoil(struct g_provider *pp, struct g_consumer *cp)
762{
763	struct g_consumer *cp2;
764
765	g_topology_assert();
766
767	LIST_FOREACH(cp2, &pp->consumers, consumers) {
768		if (cp2 == cp)
769			continue;
770/*
771		KASSERT(cp2->acr == 0, ("spoiling cp->acr = %d", cp2->acr));
772		KASSERT(cp2->acw == 0, ("spoiling cp->acw = %d", cp2->acw));
773*/
774		KASSERT(cp2->ace == 0, ("spoiling cp->ace = %d", cp2->ace));
775		cp2->spoiled++;
776	}
777	g_post_event(g_spoil_event, pp, M_WAITOK, pp, NULL);
778}
779
780int
781g_getattr__(const char *attr, struct g_consumer *cp, void *var, int len)
782{
783	int error, i;
784
785	i = len;
786	error = g_io_getattr(attr, cp, &i, var);
787	if (error)
788		return (error);
789	if (i != len)
790		return (EINVAL);
791	return (0);
792}
793
794/*
795 * XXX: Bandaid for 5.2.
796 * XXX: DO NOT EVEN THINK ABOUT CALLING THIS FUNCTION!
797 */
798static int
799g_valid_obj(void const *ptr)
800{
801	struct g_class *mp;
802	struct g_geom *gp;
803	struct g_consumer *cp;
804	struct g_provider *pp;
805
806	LIST_FOREACH(mp, &g_classes, class) {
807		if (ptr == mp)
808			return (1);
809		LIST_FOREACH(gp, &mp->geom, geom) {
810			if (ptr == gp)
811				return (1);
812			LIST_FOREACH(cp, &gp->consumer, consumer)
813				if (ptr == cp)
814					return (1);
815			LIST_FOREACH(pp, &gp->provider, provider)
816				if (ptr == pp)
817					return (1);
818		}
819	}
820	return(0);
821}
822
823/*
824 * Check if the given pointer is a live object
825 */
826
827void
828g_sanity(void const *ptr)
829{
830	struct g_class *mp;
831	struct g_geom *gp;
832	struct g_consumer *cp;
833	struct g_provider *pp;
834
835	if (!(g_debugflags & 0x8))
836		return;
837	LIST_FOREACH(mp, &g_classes, class) {
838		KASSERT(mp != ptr, ("Ptr is live class"));
839		LIST_FOREACH(gp, &mp->geom, geom) {
840			KASSERT(gp != ptr, ("Ptr is live geom"));
841			KASSERT(gp->name != ptr, ("Ptr is live geom's name"));
842			LIST_FOREACH(cp, &gp->consumer, consumer) {
843				KASSERT(cp != ptr, ("Ptr is live consumer"));
844			}
845			LIST_FOREACH(pp, &gp->provider, provider) {
846				KASSERT(pp != ptr, ("Ptr is live provider"));
847			}
848		}
849	}
850}
851
852