1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/lock.h>
34 #include <sys/mutex.h>
35 #include <sys/sx.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/geom_dbg.h>
42 #include <geom/concat/g_concat.h>
43
44 FEATURE(geom_concat, "GEOM concatenation support");
45
46 static MALLOC_DEFINE(M_CONCAT, "concat_data", "GEOM_CONCAT Data");
47
48 SYSCTL_DECL(_kern_geom);
49 static SYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
50 "GEOM_CONCAT stuff");
51 static u_int g_concat_debug = 0;
52 SYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RWTUN, &g_concat_debug, 0,
53 "Debug level");
54
55 static int g_concat_destroy(struct g_concat_softc *sc, boolean_t force);
56 static int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp,
57 struct g_geom *gp);
58
59 static g_taste_t g_concat_taste;
60 static g_ctl_req_t g_concat_config;
61 static g_dumpconf_t g_concat_dumpconf;
62
63 struct g_class g_concat_class = {
64 .name = G_CONCAT_CLASS_NAME,
65 .version = G_VERSION,
66 .ctlreq = g_concat_config,
67 .taste = g_concat_taste,
68 .destroy_geom = g_concat_destroy_geom
69 };
70
71 /*
72 * Greatest Common Divisor.
73 */
74 static u_int
gcd(u_int a,u_int b)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
lcm(u_int a,u_int b)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
g_concat_nvalid(struct g_concat_softc * sc)101 g_concat_nvalid(struct g_concat_softc *sc)
102 {
103 u_int no;
104 struct g_concat_disk *disk;
105
106 sx_assert(&sc->sc_disks_lock, SA_LOCKED);
107
108 no = 0;
109 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
110 if (disk->d_consumer != NULL)
111 no++;
112 }
113
114 return (no);
115 }
116
117 static void
g_concat_remove_disk(struct g_concat_disk * disk)118 g_concat_remove_disk(struct g_concat_disk *disk)
119 {
120 struct g_consumer *cp;
121 struct g_concat_softc *sc;
122
123 g_topology_assert();
124 KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__));
125 sc = disk->d_softc;
126 cp = disk->d_consumer;
127
128 if (!disk->d_removed) {
129 G_CONCAT_DEBUG(0, "Disk %s removed from %s.",
130 cp->provider->name, sc->sc_name);
131 disk->d_removed = 1;
132 }
133
134 if (sc->sc_provider != NULL) {
135 G_CONCAT_DEBUG(0, "Device %s deactivated.",
136 sc->sc_provider->name);
137 g_wither_provider(sc->sc_provider, ENXIO);
138 sc->sc_provider = NULL;
139 }
140
141 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
142 return;
143 disk->d_consumer = NULL;
144 g_detach(cp);
145 g_destroy_consumer(cp);
146 /* If there are no valid disks anymore, remove device. */
147 if (LIST_EMPTY(&sc->sc_geom->consumer))
148 g_concat_destroy(sc, 1);
149 }
150
151 static void
g_concat_orphan(struct g_consumer * cp)152 g_concat_orphan(struct g_consumer *cp)
153 {
154 struct g_concat_softc *sc;
155 struct g_concat_disk *disk;
156 struct g_geom *gp;
157
158 g_topology_assert();
159 gp = cp->geom;
160 sc = gp->softc;
161 if (sc == NULL)
162 return;
163
164 disk = cp->private;
165 if (disk == NULL) /* Possible? */
166 return;
167 g_concat_remove_disk(disk);
168 }
169
170 static int
g_concat_access(struct g_provider * pp,int dr,int dw,int de)171 g_concat_access(struct g_provider *pp, int dr, int dw, int de)
172 {
173 struct g_consumer *cp1, *cp2, *tmp;
174 struct g_concat_disk *disk;
175 struct g_geom *gp;
176 struct g_concat_softc *sc;
177 int error;
178
179 g_topology_assert();
180 gp = pp->geom;
181 sc = gp->softc;
182
183 /* On first open, grab an extra "exclusive" bit */
184 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
185 de++;
186 /* ... and let go of it on last close */
187 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
188 de--;
189
190 sx_slock(&sc->sc_disks_lock);
191 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
192 error = g_access(cp1, dr, dw, de);
193 if (error != 0)
194 goto fail;
195 disk = cp1->private;
196 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
197 disk->d_removed) {
198 g_concat_remove_disk(disk); /* May destroy geom. */
199 }
200 }
201 sx_sunlock(&sc->sc_disks_lock);
202 return (0);
203
204 fail:
205 sx_sunlock(&sc->sc_disks_lock);
206 LIST_FOREACH(cp2, &gp->consumer, consumer) {
207 if (cp1 == cp2)
208 break;
209 g_access(cp2, -dr, -dw, -de);
210 }
211 return (error);
212 }
213
214 static void
g_concat_candelete(struct bio * bp)215 g_concat_candelete(struct bio *bp)
216 {
217 struct g_concat_softc *sc;
218 struct g_concat_disk *disk;
219 int val;
220
221 sc = bp->bio_to->geom->softc;
222 sx_assert(&sc->sc_disks_lock, SX_LOCKED);
223 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
224 if (!disk->d_removed && disk->d_candelete)
225 break;
226 }
227 val = disk != NULL;
228 g_handleattr(bp, "GEOM::candelete", &val, sizeof(val));
229 }
230
231 static void
g_concat_kernel_dump(struct bio * bp)232 g_concat_kernel_dump(struct bio *bp)
233 {
234 struct g_concat_softc *sc;
235 struct g_concat_disk *disk;
236 struct bio *cbp;
237 struct g_kerneldump *gkd;
238
239 sc = bp->bio_to->geom->softc;
240 gkd = (struct g_kerneldump *)bp->bio_data;
241 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
242 if (disk->d_start <= gkd->offset &&
243 disk->d_end > gkd->offset)
244 break;
245 }
246 if (disk == NULL) {
247 g_io_deliver(bp, EOPNOTSUPP);
248 return;
249 }
250
251 gkd->offset -= disk->d_start;
252 if (gkd->length > disk->d_end - disk->d_start - gkd->offset)
253 gkd->length = disk->d_end - disk->d_start - gkd->offset;
254 cbp = g_clone_bio(bp);
255 if (cbp == NULL) {
256 g_io_deliver(bp, ENOMEM);
257 return;
258 }
259 cbp->bio_done = g_std_done;
260 g_io_request(cbp, disk->d_consumer);
261 G_CONCAT_DEBUG(1, "Kernel dump will go to %s.",
262 disk->d_consumer->provider->name);
263 }
264
265 static void
g_concat_done(struct bio * bp)266 g_concat_done(struct bio *bp)
267 {
268 struct g_concat_softc *sc;
269 struct bio *pbp;
270
271 pbp = bp->bio_parent;
272 sc = pbp->bio_to->geom->softc;
273 mtx_lock(&sc->sc_completion_lock);
274 if (pbp->bio_error == 0)
275 pbp->bio_error = bp->bio_error;
276 pbp->bio_completed += bp->bio_completed;
277 pbp->bio_inbed++;
278 if (pbp->bio_children == pbp->bio_inbed) {
279 mtx_unlock(&sc->sc_completion_lock);
280 g_io_deliver(pbp, pbp->bio_error);
281 } else
282 mtx_unlock(&sc->sc_completion_lock);
283 g_destroy_bio(bp);
284 }
285
286 /*
287 * Called for both BIO_FLUSH and BIO_SPEEDUP. Just pass the call down
288 */
289 static void
g_concat_passdown(struct g_concat_softc * sc,struct bio * bp)290 g_concat_passdown(struct g_concat_softc *sc, struct bio *bp)
291 {
292 struct bio_queue_head queue;
293 struct g_consumer *cp;
294 struct bio *cbp;
295 struct g_concat_disk *disk;
296
297 sx_assert(&sc->sc_disks_lock, SX_LOCKED);
298
299 bioq_init(&queue);
300 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
301 cbp = g_clone_bio(bp);
302 if (cbp == NULL) {
303 while ((cbp = bioq_takefirst(&queue)) != NULL)
304 g_destroy_bio(cbp);
305 if (bp->bio_error == 0)
306 bp->bio_error = ENOMEM;
307 g_io_deliver(bp, bp->bio_error);
308 return;
309 }
310 bioq_insert_tail(&queue, cbp);
311 cbp->bio_done = g_concat_done;
312 cbp->bio_caller1 = disk->d_consumer;
313 cbp->bio_to = disk->d_consumer->provider;
314 }
315 while ((cbp = bioq_takefirst(&queue)) != NULL) {
316 G_CONCAT_LOGREQ(cbp, "Sending request.");
317 cp = cbp->bio_caller1;
318 cbp->bio_caller1 = NULL;
319 g_io_request(cbp, cp);
320 }
321 }
322
323 static void
g_concat_start(struct bio * bp)324 g_concat_start(struct bio *bp)
325 {
326 struct bio_queue_head queue;
327 struct g_concat_softc *sc;
328 struct g_concat_disk *disk;
329 struct g_provider *pp;
330 off_t offset, end, length, off, len;
331 struct bio *cbp;
332 char *addr;
333
334 pp = bp->bio_to;
335 sc = pp->geom->softc;
336 /*
337 * If sc == NULL, provider's error should be set and g_concat_start()
338 * should not be called at all.
339 */
340 KASSERT(sc != NULL,
341 ("Provider's error should be set (error=%d)(device=%s).",
342 bp->bio_to->error, bp->bio_to->name));
343
344 G_CONCAT_LOGREQ(bp, "Request received.");
345 sx_slock(&sc->sc_disks_lock);
346
347 switch (bp->bio_cmd) {
348 case BIO_READ:
349 case BIO_WRITE:
350 case BIO_DELETE:
351 break;
352 case BIO_SPEEDUP:
353 case BIO_FLUSH:
354 g_concat_passdown(sc, bp);
355 goto end;
356 case BIO_GETATTR:
357 if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
358 g_concat_kernel_dump(bp);
359 goto end;
360 } else if (strcmp("GEOM::candelete", bp->bio_attribute) == 0) {
361 g_concat_candelete(bp);
362 goto end;
363 }
364 /* To which provider it should be delivered? */
365 /* FALLTHROUGH */
366 default:
367 g_io_deliver(bp, EOPNOTSUPP);
368 goto end;
369 }
370
371 offset = bp->bio_offset;
372 length = bp->bio_length;
373 if ((bp->bio_flags & BIO_UNMAPPED) != 0)
374 addr = NULL;
375 else
376 addr = bp->bio_data;
377 end = offset + length;
378
379 bioq_init(&queue);
380 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
381 if (disk->d_end <= offset)
382 continue;
383 if (disk->d_start >= end)
384 break;
385
386 off = offset - disk->d_start;
387 len = MIN(length, disk->d_end - offset);
388 length -= len;
389 offset += len;
390
391 cbp = g_clone_bio(bp);
392 if (cbp == NULL) {
393 while ((cbp = bioq_takefirst(&queue)) != NULL)
394 g_destroy_bio(cbp);
395 if (bp->bio_error == 0)
396 bp->bio_error = ENOMEM;
397 g_io_deliver(bp, bp->bio_error);
398 goto end;
399 }
400 bioq_insert_tail(&queue, cbp);
401 /*
402 * Fill in the component buf structure.
403 */
404 if (len == bp->bio_length)
405 cbp->bio_done = g_std_done;
406 else
407 cbp->bio_done = g_concat_done;
408 cbp->bio_offset = off;
409 cbp->bio_length = len;
410 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
411 cbp->bio_ma_offset += (uintptr_t)addr;
412 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
413 cbp->bio_ma_offset %= PAGE_SIZE;
414 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
415 cbp->bio_length) / PAGE_SIZE;
416 } else
417 cbp->bio_data = addr;
418 addr += len;
419 cbp->bio_to = disk->d_consumer->provider;
420 cbp->bio_caller1 = disk;
421
422 if (length == 0)
423 break;
424 }
425 KASSERT(length == 0,
426 ("Length is still greater than 0 (class=%s, name=%s).",
427 bp->bio_to->geom->class->name, bp->bio_to->geom->name));
428 while ((cbp = bioq_takefirst(&queue)) != NULL) {
429 G_CONCAT_LOGREQ(cbp, "Sending request.");
430 disk = cbp->bio_caller1;
431 cbp->bio_caller1 = NULL;
432 g_io_request(cbp, disk->d_consumer);
433 }
434 end:
435 sx_sunlock(&sc->sc_disks_lock);
436 }
437
438 static void
g_concat_check_and_run(struct g_concat_softc * sc)439 g_concat_check_and_run(struct g_concat_softc *sc)
440 {
441 struct g_concat_disk *disk;
442 struct g_provider *dp, *pp;
443 u_int sectorsize = 0;
444 off_t start;
445 int error;
446
447 g_topology_assert();
448 if (g_concat_nvalid(sc) != sc->sc_ndisks)
449 return;
450
451 pp = g_new_providerf(sc->sc_geom, "concat/%s", sc->sc_name);
452 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE |
453 G_PF_ACCEPT_UNMAPPED;
454 start = 0;
455 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
456 dp = disk->d_consumer->provider;
457 disk->d_start = start;
458 disk->d_end = disk->d_start + dp->mediasize;
459 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC)
460 disk->d_end -= dp->sectorsize;
461 start = disk->d_end;
462 error = g_access(disk->d_consumer, 1, 0, 0);
463 if (error == 0) {
464 error = g_getattr("GEOM::candelete", disk->d_consumer,
465 &disk->d_candelete);
466 if (error != 0)
467 disk->d_candelete = 0;
468 (void)g_access(disk->d_consumer, -1, 0, 0);
469 } else
470 G_CONCAT_DEBUG(1, "Failed to access disk %s, error %d.",
471 dp->name, error);
472 if (disk == TAILQ_FIRST(&sc->sc_disks))
473 sectorsize = dp->sectorsize;
474 else
475 sectorsize = lcm(sectorsize, dp->sectorsize);
476
477 /* A provider underneath us doesn't support unmapped */
478 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
479 G_CONCAT_DEBUG(1, "Cancelling unmapped "
480 "because of %s.", dp->name);
481 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
482 }
483 }
484 pp->sectorsize = sectorsize;
485 /* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
486 pp->mediasize = start;
487 dp = TAILQ_FIRST(&sc->sc_disks)->d_consumer->provider;
488 pp->stripesize = dp->stripesize;
489 pp->stripeoffset = dp->stripeoffset;
490 sc->sc_provider = pp;
491 g_error_provider(pp, 0);
492
493 G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
494 }
495
496 static int
g_concat_read_metadata(struct g_consumer * cp,struct g_concat_metadata * md)497 g_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md)
498 {
499 struct g_provider *pp;
500 u_char *buf;
501 int error;
502
503 g_topology_assert();
504
505 error = g_access(cp, 1, 0, 0);
506 if (error != 0)
507 return (error);
508 pp = cp->provider;
509 g_topology_unlock();
510 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
511 &error);
512 g_topology_lock();
513 g_access(cp, -1, 0, 0);
514 if (buf == NULL)
515 return (error);
516
517 /* Decode metadata. */
518 concat_metadata_decode(buf, md);
519 g_free(buf);
520
521 return (0);
522 }
523
524 /*
525 * Add disk to given device.
526 */
527 static int
g_concat_add_disk(struct g_concat_softc * sc,struct g_provider * pp,u_int no)528 g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no)
529 {
530 struct g_concat_disk *disk;
531 struct g_consumer *cp, *fcp;
532 struct g_geom *gp;
533 int error;
534
535 g_topology_assert();
536
537 sx_slock(&sc->sc_disks_lock);
538
539 /* Metadata corrupted? */
540 if (no >= sc->sc_ndisks) {
541 sx_sunlock(&sc->sc_disks_lock);
542 return (EINVAL);
543 }
544
545 for (disk = TAILQ_FIRST(&sc->sc_disks); no > 0; no--) {
546 disk = TAILQ_NEXT(disk, d_next);
547 }
548
549 /* Check if disk is not already attached. */
550 if (disk->d_consumer != NULL) {
551 sx_sunlock(&sc->sc_disks_lock);
552 return (EEXIST);
553 }
554
555 gp = sc->sc_geom;
556 fcp = LIST_FIRST(&gp->consumer);
557
558 cp = g_new_consumer(gp);
559 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
560 error = g_attach(cp, pp);
561 if (error != 0) {
562 sx_sunlock(&sc->sc_disks_lock);
563 g_destroy_consumer(cp);
564 return (error);
565 }
566
567 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
568 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
569 if (error != 0) {
570 sx_sunlock(&sc->sc_disks_lock);
571 g_detach(cp);
572 g_destroy_consumer(cp);
573 return (error);
574 }
575 }
576 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
577 struct g_concat_metadata md;
578
579 // temporarily give up the lock to avoid lock order violation
580 // due to topology unlock in g_concat_read_metadata
581 sx_sunlock(&sc->sc_disks_lock);
582 /* Re-read metadata. */
583 error = g_concat_read_metadata(cp, &md);
584 sx_slock(&sc->sc_disks_lock);
585
586 if (error != 0)
587 goto fail;
588
589 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 ||
590 strcmp(md.md_name, sc->sc_name) != 0 ||
591 md.md_id != sc->sc_id) {
592 G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name);
593 error = EINVAL;
594 goto fail;
595 }
596
597 disk->d_hardcoded = md.md_provider[0] != '\0';
598 } else {
599 disk->d_hardcoded = false;
600 }
601
602 cp->private = disk;
603 disk->d_consumer = cp;
604 disk->d_softc = sc;
605 disk->d_start = 0; /* not yet */
606 disk->d_end = 0; /* not yet */
607 disk->d_removed = 0;
608
609 G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
610
611 g_concat_check_and_run(sc);
612 sx_sunlock(&sc->sc_disks_lock); // need lock for check_and_run
613
614 return (0);
615 fail:
616 sx_sunlock(&sc->sc_disks_lock);
617 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
618 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
619 g_detach(cp);
620 g_destroy_consumer(cp);
621 return (error);
622 }
623
624 static struct g_geom *
g_concat_create(struct g_class * mp,const struct g_concat_metadata * md,u_int type)625 g_concat_create(struct g_class *mp, const struct g_concat_metadata *md,
626 u_int type)
627 {
628 struct g_concat_softc *sc;
629 struct g_concat_disk *disk;
630 struct g_geom *gp;
631 u_int no;
632
633 G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
634 md->md_id);
635
636 /* One disks is minimum. */
637 if (md->md_all < 1)
638 return (NULL);
639
640 /* Check for duplicate unit */
641 LIST_FOREACH(gp, &mp->geom, geom) {
642 sc = gp->softc;
643 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
644 G_CONCAT_DEBUG(0, "Device %s already configured.",
645 gp->name);
646 return (NULL);
647 }
648 }
649 gp = g_new_geom(mp, md->md_name);
650 sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO);
651 gp->start = g_concat_start;
652 gp->spoiled = g_concat_orphan;
653 gp->orphan = g_concat_orphan;
654 gp->access = g_concat_access;
655 gp->dumpconf = g_concat_dumpconf;
656
657 sc->sc_id = md->md_id;
658 sc->sc_ndisks = md->md_all;
659 TAILQ_INIT(&sc->sc_disks);
660 for (no = 0; no < sc->sc_ndisks; no++) {
661 disk = malloc(sizeof(*disk), M_CONCAT, M_WAITOK | M_ZERO);
662 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
663 }
664 sc->sc_type = type;
665 mtx_init(&sc->sc_completion_lock, "gconcat lock", NULL, MTX_DEF);
666 sx_init(&sc->sc_disks_lock, "gconcat append lock");
667
668 gp->softc = sc;
669 sc->sc_geom = gp;
670 sc->sc_provider = NULL;
671
672 G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
673
674 return (gp);
675 }
676
677 static int
g_concat_destroy(struct g_concat_softc * sc,boolean_t force)678 g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
679 {
680 struct g_provider *pp;
681 struct g_consumer *cp, *cp1;
682 struct g_geom *gp;
683 struct g_concat_disk *disk;
684
685 g_topology_assert();
686
687 if (sc == NULL)
688 return (ENXIO);
689
690 pp = sc->sc_provider;
691 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
692 if (force) {
693 G_CONCAT_DEBUG(0, "Device %s is still open, so it "
694 "can't be definitely removed.", pp->name);
695 } else {
696 G_CONCAT_DEBUG(1,
697 "Device %s is still open (r%dw%de%d).", pp->name,
698 pp->acr, pp->acw, pp->ace);
699 return (EBUSY);
700 }
701 }
702
703 gp = sc->sc_geom;
704 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
705 g_concat_remove_disk(cp->private);
706 if (cp1 == NULL)
707 return (0); /* Recursion happened. */
708 }
709 if (!LIST_EMPTY(&gp->consumer))
710 return (EINPROGRESS);
711
712 gp->softc = NULL;
713 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
714 gp->name));
715 while ((disk = TAILQ_FIRST(&sc->sc_disks)) != NULL) {
716 TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
717 free(disk, M_CONCAT);
718 }
719 mtx_destroy(&sc->sc_completion_lock);
720 sx_destroy(&sc->sc_disks_lock);
721 free(sc, M_CONCAT);
722
723 G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
724 g_wither_geom(gp, ENXIO);
725 return (0);
726 }
727
728 static int
g_concat_destroy_geom(struct gctl_req * req __unused,struct g_class * mp __unused,struct g_geom * gp)729 g_concat_destroy_geom(struct gctl_req *req __unused,
730 struct g_class *mp __unused, struct g_geom *gp)
731 {
732 struct g_concat_softc *sc;
733
734 sc = gp->softc;
735 return (g_concat_destroy(sc, 0));
736 }
737
738 static struct g_geom *
g_concat_taste(struct g_class * mp,struct g_provider * pp,int flags __unused)739 g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
740 {
741 struct g_concat_metadata md;
742 struct g_concat_softc *sc;
743 struct g_consumer *cp;
744 struct g_geom *gp;
745 int error;
746
747 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
748 g_topology_assert();
749
750 /* Skip providers that are already open for writing. */
751 if (pp->acw > 0)
752 return (NULL);
753
754 G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
755
756 gp = g_new_geom(mp, "concat:taste");
757 gp->start = g_concat_start;
758 gp->access = g_concat_access;
759 gp->orphan = g_concat_orphan;
760 cp = g_new_consumer(gp);
761 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
762 error = g_attach(cp, pp);
763 if (error == 0) {
764 error = g_concat_read_metadata(cp, &md);
765 g_detach(cp);
766 }
767 g_destroy_consumer(cp);
768 g_destroy_geom(gp);
769 if (error != 0)
770 return (NULL);
771 gp = NULL;
772
773 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
774 return (NULL);
775 if (md.md_version > G_CONCAT_VERSION) {
776 printf("geom_concat.ko module is too old to handle %s.\n",
777 pp->name);
778 return (NULL);
779 }
780 /*
781 * Backward compatibility:
782 */
783 /* There was no md_provider field in earlier versions of metadata. */
784 if (md.md_version < 3)
785 bzero(md.md_provider, sizeof(md.md_provider));
786 /* There was no md_provsize field in earlier versions of metadata. */
787 if (md.md_version < 4)
788 md.md_provsize = pp->mediasize;
789
790 if (md.md_provider[0] != '\0' &&
791 !g_compare_names(md.md_provider, pp->name))
792 return (NULL);
793 if (md.md_provsize != pp->mediasize)
794 return (NULL);
795
796 /*
797 * Let's check if device already exists.
798 */
799 sc = NULL;
800 LIST_FOREACH(gp, &mp->geom, geom) {
801 sc = gp->softc;
802 if (sc == NULL)
803 continue;
804 if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
805 continue;
806 if (strcmp(md.md_name, sc->sc_name) != 0)
807 continue;
808 if (md.md_id != sc->sc_id)
809 continue;
810 break;
811 }
812 if (gp != NULL) {
813 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
814 error = g_concat_add_disk(sc, pp, md.md_no);
815 if (error != 0) {
816 G_CONCAT_DEBUG(0,
817 "Cannot add disk %s to %s (error=%d).", pp->name,
818 gp->name, error);
819 return (NULL);
820 }
821 } else {
822 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
823 if (gp == NULL) {
824 G_CONCAT_DEBUG(0, "Cannot create device %s.",
825 md.md_name);
826 return (NULL);
827 }
828 sc = gp->softc;
829 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
830 error = g_concat_add_disk(sc, pp, md.md_no);
831 if (error != 0) {
832 G_CONCAT_DEBUG(0,
833 "Cannot add disk %s to %s (error=%d).", pp->name,
834 gp->name, error);
835 g_concat_destroy(sc, 1);
836 return (NULL);
837 }
838 }
839
840 return (gp);
841 }
842
843 static void
g_concat_ctl_create(struct gctl_req * req,struct g_class * mp)844 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
845 {
846 u_int attached, no;
847 struct g_concat_metadata md;
848 struct g_provider *pp;
849 struct g_concat_softc *sc;
850 struct g_geom *gp;
851 struct sbuf *sb;
852 const char *name;
853 char param[16];
854 int *nargs;
855
856 g_topology_assert();
857 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
858 if (nargs == NULL) {
859 gctl_error(req, "No '%s' argument.", "nargs");
860 return;
861 }
862 if (*nargs < 2) {
863 gctl_error(req, "Too few arguments.");
864 return;
865 }
866
867 bzero(&md, sizeof(md));
868 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
869 md.md_version = G_CONCAT_VERSION;
870 name = gctl_get_asciiparam(req, "arg0");
871 if (name == NULL) {
872 gctl_error(req, "No 'arg%u' argument.", 0);
873 return;
874 }
875 strlcpy(md.md_name, name, sizeof(md.md_name));
876 md.md_id = arc4random();
877 md.md_no = 0;
878 md.md_all = *nargs - 1;
879 /* This field is not important here. */
880 md.md_provsize = 0;
881
882 /* Check all providers are valid */
883 for (no = 1; no < *nargs; no++) {
884 snprintf(param, sizeof(param), "arg%u", no);
885 pp = gctl_get_provider(req, param);
886 if (pp == NULL)
887 return;
888 }
889
890 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
891 if (gp == NULL) {
892 gctl_error(req, "Can't configure %s.", md.md_name);
893 return;
894 }
895
896 sc = gp->softc;
897 sb = sbuf_new_auto();
898 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
899 for (attached = 0, no = 1; no < *nargs; no++) {
900 snprintf(param, sizeof(param), "arg%u", no);
901 pp = gctl_get_provider(req, param);
902 if (pp == NULL) {
903 name = gctl_get_asciiparam(req, param);
904 MPASS(name != NULL);
905 sbuf_printf(sb, " %s", name);
906 continue;
907 }
908 if (g_concat_add_disk(sc, pp, no - 1) != 0) {
909 G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
910 no, pp->name, gp->name);
911 sbuf_printf(sb, " %s", pp->name);
912 continue;
913 }
914 attached++;
915 }
916 sbuf_finish(sb);
917 if (md.md_all != attached) {
918 g_concat_destroy(gp->softc, 1);
919 gctl_error(req, "%s", sbuf_data(sb));
920 }
921 sbuf_delete(sb);
922 }
923
924 static struct g_concat_softc *
g_concat_find_device(struct g_class * mp,const char * name)925 g_concat_find_device(struct g_class *mp, const char *name)
926 {
927 struct g_concat_softc *sc;
928 struct g_geom *gp;
929
930 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
931 name += strlen(_PATH_DEV);
932
933 LIST_FOREACH(gp, &mp->geom, geom) {
934 sc = gp->softc;
935 if (sc == NULL)
936 continue;
937 if (strcmp(sc->sc_name, name) == 0)
938 return (sc);
939 }
940 return (NULL);
941 }
942
943 static void
g_concat_ctl_destroy(struct gctl_req * req,struct g_class * mp)944 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
945 {
946 struct g_concat_softc *sc;
947 int *force, *nargs, error;
948 const char *name;
949 char param[16];
950 u_int i;
951
952 g_topology_assert();
953
954 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
955 if (nargs == NULL) {
956 gctl_error(req, "No '%s' argument.", "nargs");
957 return;
958 }
959 if (*nargs <= 0) {
960 gctl_error(req, "Missing device(s).");
961 return;
962 }
963 force = gctl_get_paraml(req, "force", sizeof(*force));
964 if (force == NULL) {
965 gctl_error(req, "No '%s' argument.", "force");
966 return;
967 }
968
969 for (i = 0; i < (u_int)*nargs; i++) {
970 snprintf(param, sizeof(param), "arg%u", i);
971 name = gctl_get_asciiparam(req, param);
972 if (name == NULL) {
973 gctl_error(req, "No 'arg%u' argument.", i);
974 return;
975 }
976 sc = g_concat_find_device(mp, name);
977 if (sc == NULL) {
978 gctl_error(req, "No such device: %s.", name);
979 return;
980 }
981 error = g_concat_destroy(sc, *force);
982 if (error != 0) {
983 gctl_error(req, "Cannot destroy device %s (error=%d).",
984 sc->sc_name, error);
985 return;
986 }
987 }
988 }
989
990 static struct g_concat_disk *
g_concat_find_disk(struct g_concat_softc * sc,const char * name)991 g_concat_find_disk(struct g_concat_softc *sc, const char *name)
992 {
993 struct g_concat_disk *disk;
994
995 sx_assert(&sc->sc_disks_lock, SX_LOCKED);
996 if (strncmp(name, "/dev/", 5) == 0)
997 name += 5;
998 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
999 if (disk->d_consumer == NULL)
1000 continue;
1001 if (disk->d_consumer->provider == NULL)
1002 continue;
1003 if (strcmp(disk->d_consumer->provider->name, name) == 0)
1004 return (disk);
1005 }
1006 return (NULL);
1007 }
1008
1009 static void
g_concat_write_metadata(struct gctl_req * req,struct g_concat_softc * sc)1010 g_concat_write_metadata(struct gctl_req *req, struct g_concat_softc *sc)
1011 {
1012 u_int no = 0;
1013 struct g_concat_disk *disk;
1014 struct g_concat_metadata md;
1015 struct g_provider *pp;
1016 u_char *sector;
1017 int error;
1018
1019 bzero(&md, sizeof(md));
1020 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
1021 md.md_version = G_CONCAT_VERSION;
1022 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
1023 md.md_id = sc->sc_id;
1024 md.md_all = sc->sc_ndisks;
1025 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1026 pp = disk->d_consumer->provider;
1027
1028 md.md_no = no;
1029 if (disk->d_hardcoded)
1030 strlcpy(md.md_provider, pp->name,
1031 sizeof(md.md_provider));
1032 md.md_provsize = disk->d_consumer->provider->mediasize;
1033
1034 sector = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1035 concat_metadata_encode(&md, sector);
1036 error = g_access(disk->d_consumer, 0, 1, 0);
1037 if (error == 0) {
1038 error = g_write_data(disk->d_consumer,
1039 pp->mediasize - pp->sectorsize, sector,
1040 pp->sectorsize);
1041 (void)g_access(disk->d_consumer, 0, -1, 0);
1042 }
1043 g_free(sector);
1044 if (error != 0)
1045 gctl_error(req, "Cannot store metadata on %s: %d",
1046 pp->name, error);
1047
1048 no++;
1049 }
1050 }
1051
1052 static void
g_concat_ctl_append(struct gctl_req * req,struct g_class * mp)1053 g_concat_ctl_append(struct gctl_req *req, struct g_class *mp)
1054 {
1055 struct g_concat_softc *sc;
1056 struct g_consumer *cp, *fcp;
1057 struct g_provider *pp;
1058 struct g_geom *gp;
1059 const char *name, *cname;
1060 struct g_concat_disk *disk;
1061 int *nargs, *hardcode;
1062 int error;
1063 int disk_candelete;
1064
1065 g_topology_assert();
1066
1067 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1068 if (nargs == NULL) {
1069 gctl_error(req, "No '%s' argument.", "nargs");
1070 return;
1071 }
1072 if (*nargs != 2) {
1073 gctl_error(req, "Invalid number of arguments.");
1074 return;
1075 }
1076 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
1077 if (hardcode == NULL) {
1078 gctl_error(req, "No '%s' argument.", "hardcode");
1079 return;
1080 }
1081
1082 cname = gctl_get_asciiparam(req, "arg0");
1083 if (cname == NULL) {
1084 gctl_error(req, "No 'arg%u' argument.", 0);
1085 return;
1086 }
1087 sc = g_concat_find_device(mp, cname);
1088 if (sc == NULL) {
1089 gctl_error(req, "No such device: %s.", cname);
1090 return;
1091 }
1092 if (sc->sc_provider == NULL) {
1093 /*
1094 * this won't race with g_concat_remove_disk as both
1095 * are holding the topology lock
1096 */
1097 gctl_error(req, "Device not active, can't append: %s.", cname);
1098 return;
1099 }
1100 G_CONCAT_DEBUG(1, "Appending to %s:", cname);
1101 sx_xlock(&sc->sc_disks_lock);
1102 gp = sc->sc_geom;
1103 fcp = LIST_FIRST(&gp->consumer);
1104
1105 name = gctl_get_asciiparam(req, "arg1");
1106 if (name == NULL) {
1107 gctl_error(req, "No 'arg%u' argument.", 1);
1108 goto fail;
1109 }
1110 pp = g_provider_by_name(name);
1111 if (pp == NULL) {
1112 G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
1113 gctl_error(req, "Disk %s is invalid.", name);
1114 goto fail;
1115 }
1116 G_CONCAT_DEBUG(1, "Appending %s to this", name);
1117
1118 if (g_concat_find_disk(sc, name) != NULL) {
1119 gctl_error(req, "Disk %s already appended.", name);
1120 goto fail;
1121 }
1122
1123 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
1124 gctl_error(req, "Providers sectorsize mismatch: %u vs %u",
1125 sc->sc_provider->sectorsize, pp->sectorsize);
1126 goto fail;
1127 }
1128
1129 cp = g_new_consumer(gp);
1130 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1131 error = g_attach(cp, pp);
1132 if (error != 0) {
1133 g_destroy_consumer(cp);
1134 gctl_error(req, "Cannot open device %s (error=%d).",
1135 name, error);
1136 goto fail;
1137 }
1138
1139 error = g_access(cp, 1, 0, 0);
1140 if (error == 0) {
1141 error = g_getattr("GEOM::candelete", cp, &disk_candelete);
1142 if (error != 0)
1143 disk_candelete = 0;
1144 (void)g_access(cp, -1, 0, 0);
1145 } else
1146 G_CONCAT_DEBUG(1, "Failed to access disk %s, error %d.", name, error);
1147
1148 /* invoke g_access exactly as deep as all the other members currently are */
1149 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
1150 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
1151 if (error != 0) {
1152 g_detach(cp);
1153 g_destroy_consumer(cp);
1154 gctl_error(req, "Failed to access disk %s (error=%d).", name, error);
1155 goto fail;
1156 }
1157 }
1158
1159 disk = malloc(sizeof(*disk), M_CONCAT, M_WAITOK | M_ZERO);
1160 disk->d_consumer = cp;
1161 disk->d_softc = sc;
1162 disk->d_start = TAILQ_LAST(&sc->sc_disks, g_concat_disks)->d_end;
1163 disk->d_end = disk->d_start + cp->provider->mediasize;
1164 disk->d_candelete = disk_candelete;
1165 disk->d_removed = 0;
1166 disk->d_hardcoded = *hardcode;
1167 cp->private = disk;
1168 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
1169 sc->sc_ndisks++;
1170
1171 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
1172 /* last sector is for metadata */
1173 disk->d_end -= cp->provider->sectorsize;
1174
1175 /* update metadata on all parts */
1176 g_concat_write_metadata(req, sc);
1177 }
1178
1179 g_resize_provider(sc->sc_provider, disk->d_end);
1180
1181 fail:
1182 sx_xunlock(&sc->sc_disks_lock);
1183 }
1184
1185 static void
g_concat_config(struct gctl_req * req,struct g_class * mp,const char * verb)1186 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1187 {
1188 uint32_t *version;
1189
1190 g_topology_assert();
1191
1192 version = gctl_get_paraml(req, "version", sizeof(*version));
1193 if (version == NULL) {
1194 gctl_error(req, "No '%s' argument.", "version");
1195 return;
1196 }
1197 if (*version != G_CONCAT_VERSION) {
1198 gctl_error(req, "Userland and kernel parts are out of sync.");
1199 return;
1200 }
1201
1202 if (strcmp(verb, "create") == 0) {
1203 g_concat_ctl_create(req, mp);
1204 return;
1205 } else if (strcmp(verb, "destroy") == 0 ||
1206 strcmp(verb, "stop") == 0) {
1207 g_concat_ctl_destroy(req, mp);
1208 return;
1209 } else if (strcmp(verb, "append") == 0) {
1210 g_concat_ctl_append(req, mp);
1211 return;
1212 }
1213 gctl_error(req, "Unknown verb.");
1214 }
1215
1216 static void
g_concat_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp,struct g_provider * pp)1217 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1218 struct g_consumer *cp, struct g_provider *pp)
1219 {
1220 struct g_concat_softc *sc;
1221
1222 g_topology_assert();
1223 sc = gp->softc;
1224 if (sc == NULL)
1225 return;
1226
1227 sx_slock(&sc->sc_disks_lock);
1228 if (pp != NULL) {
1229 /* Nothing here. */
1230 } else if (cp != NULL) {
1231 struct g_concat_disk *disk;
1232
1233 disk = cp->private;
1234 if (disk == NULL)
1235 goto end;
1236 sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
1237 (intmax_t)disk->d_end);
1238 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
1239 (intmax_t)disk->d_start);
1240 } else {
1241 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1242 sbuf_printf(sb, "%s<Type>", indent);
1243 switch (sc->sc_type) {
1244 case G_CONCAT_TYPE_AUTOMATIC:
1245 sbuf_cat(sb, "AUTOMATIC");
1246 break;
1247 case G_CONCAT_TYPE_MANUAL:
1248 sbuf_cat(sb, "MANUAL");
1249 break;
1250 default:
1251 sbuf_cat(sb, "UNKNOWN");
1252 break;
1253 }
1254 sbuf_cat(sb, "</Type>\n");
1255 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1256 indent, sc->sc_ndisks, g_concat_nvalid(sc));
1257 sbuf_printf(sb, "%s<State>", indent);
1258 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1259 sbuf_cat(sb, "UP");
1260 else
1261 sbuf_cat(sb, "DOWN");
1262 sbuf_cat(sb, "</State>\n");
1263 }
1264 end:
1265 sx_sunlock(&sc->sc_disks_lock);
1266 }
1267
1268 DECLARE_GEOM_CLASS(g_concat_class, g_concat);
1269 MODULE_VERSION(geom_concat, 0);
1270