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