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