]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/geom/concat/g_concat.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / geom / concat / g_concat.c
1 /*-
2  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/bio.h>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <geom/geom.h>
40 #include <geom/concat/g_concat.h>
41
42
43 static MALLOC_DEFINE(M_CONCAT, "concat_data", "GEOM_CONCAT Data");
44
45 SYSCTL_DECL(_kern_geom);
46 SYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW, 0, "GEOM_CONCAT stuff");
47 static u_int g_concat_debug = 0;
48 TUNABLE_INT("kern.geom.concat.debug", &g_concat_debug);
49 SYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RW, &g_concat_debug, 0,
50     "Debug level");
51
52 static int g_concat_destroy(struct g_concat_softc *sc, boolean_t force);
53 static int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp,
54     struct g_geom *gp);
55
56 static g_taste_t g_concat_taste;
57 static g_ctl_req_t g_concat_config;
58 static g_dumpconf_t g_concat_dumpconf;
59
60 struct g_class g_concat_class = {
61         .name = G_CONCAT_CLASS_NAME,
62         .version = G_VERSION,
63         .ctlreq = g_concat_config,
64         .taste = g_concat_taste,
65         .destroy_geom = g_concat_destroy_geom
66 };
67
68
69 /*
70  * Greatest Common Divisor.
71  */
72 static u_int
73 gcd(u_int a, u_int b)
74 {
75         u_int c;
76
77         while (b != 0) {
78                 c = a;
79                 a = b;
80                 b = (c % b);
81         }
82         return (a);
83 }
84
85 /*
86  * Least Common Multiple.
87  */
88 static u_int
89 lcm(u_int a, u_int b)
90 {
91
92         return ((a * b) / gcd(a, b));
93 }
94
95 /*
96  * Return the number of valid disks.
97  */
98 static u_int
99 g_concat_nvalid(struct g_concat_softc *sc)
100 {
101         u_int i, no;
102
103         no = 0;
104         for (i = 0; i < sc->sc_ndisks; i++) {
105                 if (sc->sc_disks[i].d_consumer != NULL)
106                         no++;
107         }
108
109         return (no);
110 }
111
112 static void
113 g_concat_remove_disk(struct g_concat_disk *disk)
114 {
115         struct g_consumer *cp;
116         struct g_concat_softc *sc;
117
118         KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__));
119         sc = disk->d_softc;
120         cp = disk->d_consumer;
121
122         G_CONCAT_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
123             sc->sc_name);
124
125         disk->d_consumer = NULL;
126         if (sc->sc_provider != NULL) {
127                 sc->sc_provider->flags |= G_PF_WITHER;
128                 g_orphan_provider(sc->sc_provider, ENXIO);
129                 sc->sc_provider = NULL;
130                 G_CONCAT_DEBUG(0, "Device %s removed.", sc->sc_name);
131         }
132
133         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
134                 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
135         g_detach(cp);
136         g_destroy_consumer(cp);
137 }
138
139 static void
140 g_concat_orphan(struct g_consumer *cp)
141 {
142         struct g_concat_softc *sc;
143         struct g_concat_disk *disk;
144         struct g_geom *gp;
145
146         g_topology_assert();
147         gp = cp->geom;
148         sc = gp->softc;
149         if (sc == NULL)
150                 return;
151
152         disk = cp->private;
153         if (disk == NULL)       /* Possible? */
154                 return;
155         g_concat_remove_disk(disk);
156
157         /* If there are no valid disks anymore, remove device. */
158         if (g_concat_nvalid(sc) == 0)
159                 g_concat_destroy(sc, 1);
160 }
161
162 static int
163 g_concat_access(struct g_provider *pp, int dr, int dw, int de)
164 {
165         struct g_consumer *cp1, *cp2;
166         struct g_concat_softc *sc;
167         struct g_geom *gp;
168         int error;
169
170         gp = pp->geom;
171         sc = gp->softc;
172
173         if (sc == NULL) {
174                 /*
175                  * It looks like geom is being withered.
176                  * In that case we allow only negative requests.
177                  */
178                 KASSERT(dr <= 0 && dw <= 0 && de <= 0,
179                     ("Positive access request (device=%s).", pp->name));
180                 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 &&
181                     (pp->ace + de) == 0) {
182                         G_CONCAT_DEBUG(0, "Device %s definitely destroyed.",
183                             gp->name);
184                 }
185                 return (0);
186         }
187
188         /* On first open, grab an extra "exclusive" bit */
189         if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
190                 de++;
191         /* ... and let go of it on last close */
192         if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
193                 de--;
194
195         error = ENXIO;
196         LIST_FOREACH(cp1, &gp->consumer, consumer) {
197                 error = g_access(cp1, dr, dw, de);
198                 if (error == 0)
199                         continue;
200                 /*
201                  * If we fail here, backout all previous changes.
202                  */
203                 LIST_FOREACH(cp2, &gp->consumer, consumer) {
204                         if (cp1 == cp2)
205                                 return (error);
206                         g_access(cp2, -dr, -dw, -de);
207                 }
208                 /* NOTREACHED */
209         }
210
211         return (error);
212 }
213
214 static void
215 g_concat_flush(struct g_concat_softc *sc, struct bio *bp)
216 {
217         struct bio_queue_head queue;
218         struct g_consumer *cp;
219         struct bio *cbp;
220         u_int no;
221
222         bioq_init(&queue);
223         for (no = 0; no < sc->sc_ndisks; no++) {
224                 cbp = g_clone_bio(bp);
225                 if (cbp == NULL) {
226                         for (cbp = bioq_first(&queue); cbp != NULL;
227                             cbp = bioq_first(&queue)) {
228                                 bioq_remove(&queue, cbp);
229                                 g_destroy_bio(cbp);
230                         }
231                         if (bp->bio_error == 0)
232                                 bp->bio_error = ENOMEM;
233                         g_io_deliver(bp, bp->bio_error);
234                         return;
235                 }
236                 bioq_insert_tail(&queue, cbp);
237                 cbp->bio_done = g_std_done;
238                 cbp->bio_caller1 = sc->sc_disks[no].d_consumer;
239                 cbp->bio_to = sc->sc_disks[no].d_consumer->provider;
240         }
241         for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
242                 bioq_remove(&queue, cbp);
243                 G_CONCAT_LOGREQ(cbp, "Sending request.");
244                 cp = cbp->bio_caller1;
245                 cbp->bio_caller1 = NULL;
246                 g_io_request(cbp, cp);
247         }
248 }
249
250 static void
251 g_concat_start(struct bio *bp)
252 {
253         struct bio_queue_head queue;
254         struct g_concat_softc *sc;
255         struct g_concat_disk *disk;
256         struct g_provider *pp;
257         off_t offset, end, length, off, len;
258         struct bio *cbp;
259         char *addr;
260         u_int no;
261
262         pp = bp->bio_to;
263         sc = pp->geom->softc;
264         /*
265          * If sc == NULL, provider's error should be set and g_concat_start()
266          * should not be called at all.
267          */
268         KASSERT(sc != NULL,
269             ("Provider's error should be set (error=%d)(device=%s).",
270             bp->bio_to->error, bp->bio_to->name));
271
272         G_CONCAT_LOGREQ(bp, "Request received.");
273
274         switch (bp->bio_cmd) {
275         case BIO_READ:
276         case BIO_WRITE:
277         case BIO_DELETE:
278                 break;
279         case BIO_FLUSH:
280                 g_concat_flush(sc, bp);
281                 return;
282         case BIO_GETATTR:
283                 /* To which provider it should be delivered? */
284         default:
285                 g_io_deliver(bp, EOPNOTSUPP);
286                 return;
287         }
288
289         offset = bp->bio_offset;
290         length = bp->bio_length;
291         addr = bp->bio_data;
292         end = offset + length;
293
294         bioq_init(&queue);
295         for (no = 0; no < sc->sc_ndisks; no++) {
296                 disk = &sc->sc_disks[no];
297                 if (disk->d_end <= offset)
298                         continue;
299                 if (disk->d_start >= end)
300                         break;
301
302                 off = offset - disk->d_start;
303                 len = MIN(length, disk->d_end - offset);
304                 length -= len;
305                 offset += len;
306
307                 cbp = g_clone_bio(bp);
308                 if (cbp == NULL) {
309                         for (cbp = bioq_first(&queue); cbp != NULL;
310                             cbp = bioq_first(&queue)) {
311                                 bioq_remove(&queue, cbp);
312                                 g_destroy_bio(cbp);
313                         }
314                         if (bp->bio_error == 0)
315                                 bp->bio_error = ENOMEM;
316                         g_io_deliver(bp, bp->bio_error);
317                         return;
318                 }
319                 bioq_insert_tail(&queue, cbp);
320                 /*
321                  * Fill in the component buf structure.
322                  */
323                 cbp->bio_done = g_std_done;
324                 cbp->bio_offset = off;
325                 cbp->bio_data = addr;
326                 addr += len;
327                 cbp->bio_length = len;
328                 cbp->bio_to = disk->d_consumer->provider;
329                 cbp->bio_caller1 = disk;
330
331                 if (length == 0)
332                         break;
333         }
334         KASSERT(length == 0,
335             ("Length is still greater than 0 (class=%s, name=%s).",
336             bp->bio_to->geom->class->name, bp->bio_to->geom->name));
337         for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
338                 bioq_remove(&queue, cbp);
339                 G_CONCAT_LOGREQ(cbp, "Sending request.");
340                 disk = cbp->bio_caller1;
341                 cbp->bio_caller1 = NULL;
342                 g_io_request(cbp, disk->d_consumer);
343         }
344 }
345
346 static void
347 g_concat_check_and_run(struct g_concat_softc *sc)
348 {
349         struct g_concat_disk *disk;
350         struct g_provider *pp;
351         u_int no, sectorsize = 0;
352         off_t start;
353
354         if (g_concat_nvalid(sc) != sc->sc_ndisks)
355                 return;
356
357         pp = g_new_providerf(sc->sc_geom, "concat/%s", sc->sc_name);
358         start = 0;
359         for (no = 0; no < sc->sc_ndisks; no++) {
360                 disk = &sc->sc_disks[no];
361                 disk->d_start = start;
362                 disk->d_end = disk->d_start +
363                     disk->d_consumer->provider->mediasize;
364                 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC)
365                         disk->d_end -= disk->d_consumer->provider->sectorsize;
366                 start = disk->d_end;
367                 if (no == 0)
368                         sectorsize = disk->d_consumer->provider->sectorsize;
369                 else {
370                         sectorsize = lcm(sectorsize,
371                             disk->d_consumer->provider->sectorsize);
372                 }
373         }
374         pp->sectorsize = sectorsize;
375         /* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
376         pp->mediasize = start;
377         pp->stripesize = sc->sc_disks[0].d_consumer->provider->stripesize;
378         pp->stripeoffset = sc->sc_disks[0].d_consumer->provider->stripeoffset;
379         sc->sc_provider = pp;
380         g_error_provider(pp, 0);
381
382         G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_name);
383 }
384
385 static int
386 g_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md)
387 {
388         struct g_provider *pp;
389         u_char *buf;
390         int error;
391
392         g_topology_assert();
393
394         error = g_access(cp, 1, 0, 0);
395         if (error != 0)
396                 return (error);
397         pp = cp->provider;
398         g_topology_unlock();
399         buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
400             &error);
401         g_topology_lock();
402         g_access(cp, -1, 0, 0);
403         if (buf == NULL)
404                 return (error);
405
406         /* Decode metadata. */
407         concat_metadata_decode(buf, md);
408         g_free(buf);
409
410         return (0);
411 }
412
413 /*
414  * Add disk to given device.
415  */
416 static int
417 g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no)
418 {
419         struct g_concat_disk *disk;
420         struct g_consumer *cp, *fcp;
421         struct g_geom *gp;
422         int error;
423
424         /* Metadata corrupted? */
425         if (no >= sc->sc_ndisks)
426                 return (EINVAL);
427
428         disk = &sc->sc_disks[no];
429         /* Check if disk is not already attached. */
430         if (disk->d_consumer != NULL)
431                 return (EEXIST);
432
433         gp = sc->sc_geom;
434         fcp = LIST_FIRST(&gp->consumer);
435
436         cp = g_new_consumer(gp);
437         error = g_attach(cp, pp);
438         if (error != 0) {
439                 g_destroy_consumer(cp);
440                 return (error);
441         }
442
443         if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
444                 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
445                 if (error != 0) {
446                         g_detach(cp);
447                         g_destroy_consumer(cp);
448                         return (error);
449                 }
450         }
451         if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
452                 struct g_concat_metadata md;
453
454                 /* Re-read metadata. */
455                 error = g_concat_read_metadata(cp, &md);
456                 if (error != 0)
457                         goto fail;
458
459                 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 ||
460                     strcmp(md.md_name, sc->sc_name) != 0 ||
461                     md.md_id != sc->sc_id) {
462                         G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name);
463                         goto fail;
464                 }
465         }
466
467         cp->private = disk;
468         disk->d_consumer = cp;
469         disk->d_softc = sc;
470         disk->d_start = 0;      /* not yet */
471         disk->d_end = 0;        /* not yet */
472
473         G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
474
475         g_concat_check_and_run(sc);
476
477         return (0);
478 fail:
479         if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
480                 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
481         g_detach(cp);
482         g_destroy_consumer(cp);
483         return (error);
484 }
485
486 static struct g_geom *
487 g_concat_create(struct g_class *mp, const struct g_concat_metadata *md,
488     u_int type)
489 {
490         struct g_concat_softc *sc;
491         struct g_geom *gp;
492         u_int no;
493
494         G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
495             md->md_id);
496
497         /* One disks is minimum. */
498         if (md->md_all < 1)
499                 return (NULL);
500
501         /* Check for duplicate unit */
502         LIST_FOREACH(gp, &mp->geom, geom) {
503                 sc = gp->softc;
504                 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
505                         G_CONCAT_DEBUG(0, "Device %s already configured.",
506                             gp->name);
507                         return (NULL);
508                 }
509         }
510         gp = g_new_geomf(mp, "%s", md->md_name);
511         gp->softc = NULL;       /* for a moment */
512
513         sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO);
514         gp->start = g_concat_start;
515         gp->spoiled = g_concat_orphan;
516         gp->orphan = g_concat_orphan;
517         gp->access = g_concat_access;
518         gp->dumpconf = g_concat_dumpconf;
519
520         sc->sc_id = md->md_id;
521         sc->sc_ndisks = md->md_all;
522         sc->sc_disks = malloc(sizeof(struct g_concat_disk) * sc->sc_ndisks,
523             M_CONCAT, M_WAITOK | M_ZERO);
524         for (no = 0; no < sc->sc_ndisks; no++)
525                 sc->sc_disks[no].d_consumer = NULL;
526         sc->sc_type = type;
527
528         gp->softc = sc;
529         sc->sc_geom = gp;
530         sc->sc_provider = NULL;
531
532         G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
533
534         return (gp);
535 }
536
537 static int
538 g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
539 {
540         struct g_provider *pp;
541         struct g_geom *gp;
542         u_int no;
543
544         g_topology_assert();
545
546         if (sc == NULL)
547                 return (ENXIO);
548
549         pp = sc->sc_provider;
550         if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
551                 if (force) {
552                         G_CONCAT_DEBUG(0, "Device %s is still open, so it "
553                             "can't be definitely removed.", pp->name);
554                 } else {
555                         G_CONCAT_DEBUG(1,
556                             "Device %s is still open (r%dw%de%d).", pp->name,
557                             pp->acr, pp->acw, pp->ace);
558                         return (EBUSY);
559                 }
560         }
561
562         for (no = 0; no < sc->sc_ndisks; no++) {
563                 if (sc->sc_disks[no].d_consumer != NULL)
564                         g_concat_remove_disk(&sc->sc_disks[no]);
565         }
566
567         gp = sc->sc_geom;
568         gp->softc = NULL;
569         KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
570             gp->name));
571         free(sc->sc_disks, M_CONCAT);
572         free(sc, M_CONCAT);
573
574         pp = LIST_FIRST(&gp->provider);
575         if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
576                 G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
577
578         g_wither_geom(gp, ENXIO);
579
580         return (0);
581 }
582
583 static int
584 g_concat_destroy_geom(struct gctl_req *req __unused,
585     struct g_class *mp __unused, struct g_geom *gp)
586 {
587         struct g_concat_softc *sc;
588
589         sc = gp->softc;
590         return (g_concat_destroy(sc, 0));
591 }
592
593 static struct g_geom *
594 g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
595 {
596         struct g_concat_metadata md;
597         struct g_concat_softc *sc;
598         struct g_consumer *cp;
599         struct g_geom *gp;
600         int error;
601
602         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
603         g_topology_assert();
604
605         /* Skip providers that are already open for writing. */
606         if (pp->acw > 0)
607                 return (NULL);
608
609         G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
610
611         gp = g_new_geomf(mp, "concat:taste");
612         gp->start = g_concat_start;
613         gp->access = g_concat_access;
614         gp->orphan = g_concat_orphan;
615         cp = g_new_consumer(gp);
616         g_attach(cp, pp);
617         error = g_concat_read_metadata(cp, &md);
618         g_detach(cp);
619         g_destroy_consumer(cp);
620         g_destroy_geom(gp);
621         if (error != 0)
622                 return (NULL);
623         gp = NULL;
624
625         if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
626                 return (NULL);
627         if (md.md_version > G_CONCAT_VERSION) {
628                 printf("geom_concat.ko module is too old to handle %s.\n",
629                     pp->name);
630                 return (NULL);
631         }
632         /*
633          * Backward compatibility:
634          */
635         /* There was no md_provider field in earlier versions of metadata. */
636         if (md.md_version < 3)
637                 bzero(md.md_provider, sizeof(md.md_provider));
638         /* There was no md_provsize field in earlier versions of metadata. */
639         if (md.md_version < 4)
640                 md.md_provsize = pp->mediasize;
641
642         if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
643                 return (NULL);
644         if (md.md_provsize != pp->mediasize)
645                 return (NULL);
646
647         /*
648          * Let's check if device already exists.
649          */
650         sc = NULL;
651         LIST_FOREACH(gp, &mp->geom, geom) {
652                 sc = gp->softc;
653                 if (sc == NULL)
654                         continue;
655                 if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
656                         continue;
657                 if (strcmp(md.md_name, sc->sc_name) != 0)
658                         continue;
659                 if (md.md_id != sc->sc_id)
660                         continue;
661                 break;
662         }
663         if (gp != NULL) {
664                 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
665                 error = g_concat_add_disk(sc, pp, md.md_no);
666                 if (error != 0) {
667                         G_CONCAT_DEBUG(0,
668                             "Cannot add disk %s to %s (error=%d).", pp->name,
669                             gp->name, error);
670                         return (NULL);
671                 }
672         } else {
673                 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
674                 if (gp == NULL) {
675                         G_CONCAT_DEBUG(0, "Cannot create device %s.",
676                             md.md_name);
677                         return (NULL);
678                 }
679                 sc = gp->softc;
680                 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
681                 error = g_concat_add_disk(sc, pp, md.md_no);
682                 if (error != 0) {
683                         G_CONCAT_DEBUG(0,
684                             "Cannot add disk %s to %s (error=%d).", pp->name,
685                             gp->name, error);
686                         g_concat_destroy(sc, 1);
687                         return (NULL);
688                 }
689         }
690
691         return (gp);
692 }
693
694 static void
695 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
696 {
697         u_int attached, no;
698         struct g_concat_metadata md;
699         struct g_provider *pp;
700         struct g_concat_softc *sc;
701         struct g_geom *gp;
702         struct sbuf *sb;
703         const char *name;
704         char param[16];
705         int *nargs;
706
707         g_topology_assert();
708         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
709         if (nargs == NULL) {
710                 gctl_error(req, "No '%s' argument.", "nargs");
711                 return;
712         }
713         if (*nargs < 2) {
714                 gctl_error(req, "Too few arguments.");
715                 return;
716         }
717
718         strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
719         md.md_version = G_CONCAT_VERSION;
720         name = gctl_get_asciiparam(req, "arg0");
721         if (name == NULL) {
722                 gctl_error(req, "No 'arg%u' argument.", 0);
723                 return;
724         }
725         strlcpy(md.md_name, name, sizeof(md.md_name));
726         md.md_id = arc4random();
727         md.md_no = 0;
728         md.md_all = *nargs - 1;
729         bzero(md.md_provider, sizeof(md.md_provider));
730         /* This field is not important here. */
731         md.md_provsize = 0;
732
733         /* Check all providers are valid */
734         for (no = 1; no < *nargs; no++) {
735                 snprintf(param, sizeof(param), "arg%u", no);
736                 name = gctl_get_asciiparam(req, param);
737                 if (name == NULL) {
738                         gctl_error(req, "No 'arg%u' argument.", no);
739                         return;
740                 }
741                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
742                         name += strlen("/dev/");
743                 pp = g_provider_by_name(name);
744                 if (pp == NULL) {
745                         G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
746                         gctl_error(req, "Disk %s is invalid.", name);
747                         return;
748                 }
749         }
750
751         gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
752         if (gp == NULL) {
753                 gctl_error(req, "Can't configure %s.", md.md_name);
754                 return;
755         }
756
757         sc = gp->softc;
758         sb = sbuf_new_auto();
759         sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
760         for (attached = 0, no = 1; no < *nargs; no++) {
761                 snprintf(param, sizeof(param), "arg%u", no);
762                 name = gctl_get_asciiparam(req, param);
763                 if (name == NULL) {
764                         gctl_error(req, "No 'arg%d' argument.", no);
765                         return;
766                 }
767                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
768                         name += strlen("/dev/");
769                 pp = g_provider_by_name(name);
770                 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
771                 if (g_concat_add_disk(sc, pp, no - 1) != 0) {
772                         G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
773                             no, pp->name, gp->name);
774                         sbuf_printf(sb, " %s", pp->name);
775                         continue;
776                 }
777                 attached++;
778         }
779         sbuf_finish(sb);
780         if (md.md_all != attached) {
781                 g_concat_destroy(gp->softc, 1);
782                 gctl_error(req, "%s", sbuf_data(sb));
783         }
784         sbuf_delete(sb);
785 }
786
787 static struct g_concat_softc *
788 g_concat_find_device(struct g_class *mp, const char *name)
789 {
790         struct g_concat_softc *sc;
791         struct g_geom *gp;
792
793         LIST_FOREACH(gp, &mp->geom, geom) {
794                 sc = gp->softc;
795                 if (sc == NULL)
796                         continue;
797                 if (strcmp(sc->sc_name, name) == 0)
798                         return (sc);
799         }
800         return (NULL);
801 }
802
803 static void
804 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
805 {
806         struct g_concat_softc *sc;
807         int *force, *nargs, error;
808         const char *name;
809         char param[16];
810         u_int i;
811
812         g_topology_assert();
813
814         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
815         if (nargs == NULL) {
816                 gctl_error(req, "No '%s' argument.", "nargs");
817                 return;
818         }
819         if (*nargs <= 0) {
820                 gctl_error(req, "Missing device(s).");
821                 return;
822         }
823         force = gctl_get_paraml(req, "force", sizeof(*force));
824         if (force == NULL) {
825                 gctl_error(req, "No '%s' argument.", "force");
826                 return;
827         }
828
829         for (i = 0; i < (u_int)*nargs; i++) {
830                 snprintf(param, sizeof(param), "arg%u", i);
831                 name = gctl_get_asciiparam(req, param);
832                 if (name == NULL) {
833                         gctl_error(req, "No 'arg%u' argument.", i);
834                         return;
835                 }
836                 sc = g_concat_find_device(mp, name);
837                 if (sc == NULL) {
838                         gctl_error(req, "No such device: %s.", name);
839                         return;
840                 }
841                 error = g_concat_destroy(sc, *force);
842                 if (error != 0) {
843                         gctl_error(req, "Cannot destroy device %s (error=%d).",
844                             sc->sc_name, error);
845                         return;
846                 }
847         }
848 }
849
850 static void
851 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
852 {
853         uint32_t *version;
854
855         g_topology_assert();
856
857         version = gctl_get_paraml(req, "version", sizeof(*version));
858         if (version == NULL) {
859                 gctl_error(req, "No '%s' argument.", "version");
860                 return;
861         }
862         if (*version != G_CONCAT_VERSION) {
863                 gctl_error(req, "Userland and kernel parts are out of sync.");
864                 return;
865         }
866
867         if (strcmp(verb, "create") == 0) {
868                 g_concat_ctl_create(req, mp);
869                 return;
870         } else if (strcmp(verb, "destroy") == 0 ||
871             strcmp(verb, "stop") == 0) {
872                 g_concat_ctl_destroy(req, mp);
873                 return;
874         }
875         gctl_error(req, "Unknown verb.");
876 }
877
878 static void
879 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
880     struct g_consumer *cp, struct g_provider *pp)
881 {
882         struct g_concat_softc *sc;
883
884         g_topology_assert();
885         sc = gp->softc;
886         if (sc == NULL)
887                 return;
888         if (pp != NULL) {
889                 /* Nothing here. */
890         } else if (cp != NULL) {
891                 struct g_concat_disk *disk;
892
893                 disk = cp->private;
894                 if (disk == NULL)
895                         return;
896                 sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
897                     (intmax_t)disk->d_end);
898                 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
899                     (intmax_t)disk->d_start);
900         } else {
901                 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
902                 sbuf_printf(sb, "%s<Type>", indent);
903                 switch (sc->sc_type) {
904                 case G_CONCAT_TYPE_AUTOMATIC:
905                         sbuf_printf(sb, "AUTOMATIC");
906                         break;
907                 case G_CONCAT_TYPE_MANUAL:
908                         sbuf_printf(sb, "MANUAL");
909                         break;
910                 default:
911                         sbuf_printf(sb, "UNKNOWN");
912                         break;
913                 }
914                 sbuf_printf(sb, "</Type>\n");
915                 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
916                     indent, sc->sc_ndisks, g_concat_nvalid(sc));
917                 sbuf_printf(sb, "%s<State>", indent);
918                 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
919                         sbuf_printf(sb, "UP");
920                 else
921                         sbuf_printf(sb, "DOWN");
922                 sbuf_printf(sb, "</State>\n");
923         }
924 }
925
926 DECLARE_GEOM_CLASS(g_concat_class, g_concat);