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