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