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