]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/geom/stripe/g_stripe.c
Adapt merge of r361581 to 9-stable to unbreak kernel compilation.
[FreeBSD/stable/9.git] / sys / geom / stripe / g_stripe.c
1 /*-
2  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/bio.h>
37 #include <sys/sbuf.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <vm/uma.h>
41 #include <geom/geom.h>
42 #include <geom/stripe/g_stripe.h>
43
44 FEATURE(geom_stripe, "GEOM striping support");
45
46 static MALLOC_DEFINE(M_STRIPE, "stripe_data", "GEOM_STRIPE Data");
47
48 static uma_zone_t g_stripe_zone;
49
50 static int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force);
51 static int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp,
52     struct g_geom *gp);
53
54 static g_taste_t g_stripe_taste;
55 static g_ctl_req_t g_stripe_config;
56 static g_dumpconf_t g_stripe_dumpconf;
57 static g_init_t g_stripe_init;
58 static g_fini_t g_stripe_fini;
59
60 struct g_class g_stripe_class = {
61         .name = G_STRIPE_CLASS_NAME,
62         .version = G_VERSION,
63         .ctlreq = g_stripe_config,
64         .taste = g_stripe_taste,
65         .destroy_geom = g_stripe_destroy_geom,
66         .init = g_stripe_init,
67         .fini = g_stripe_fini
68 };
69
70 SYSCTL_DECL(_kern_geom);
71 static SYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0,
72     "GEOM_STRIPE stuff");
73 static u_int g_stripe_debug = 0;
74 TUNABLE_INT("kern.geom.stripe.debug", &g_stripe_debug);
75 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RW, &g_stripe_debug, 0,
76     "Debug level");
77 static int g_stripe_fast = 0;
78 TUNABLE_INT("kern.geom.stripe.fast", &g_stripe_fast);
79 static int
80 g_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS)
81 {
82         int error, fast;
83
84         fast = g_stripe_fast;
85         error = sysctl_handle_int(oidp, &fast, 0, req);
86         if (error == 0 && req->newptr != NULL)
87                 g_stripe_fast = fast;
88         return (error);
89 }
90 SYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RW,
91     NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode");
92 static u_int g_stripe_maxmem = MAXPHYS * 100;
93 TUNABLE_INT("kern.geom.stripe.maxmem", &g_stripe_maxmem);
94 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RD, &g_stripe_maxmem,
95     0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
96 static u_int g_stripe_fast_failed = 0;
97 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
98     &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
99
100 /*
101  * Greatest Common Divisor.
102  */
103 static u_int
104 gcd(u_int a, u_int b)
105 {
106         u_int c;
107
108         while (b != 0) {
109                 c = a;
110                 a = b;
111                 b = (c % b);
112         }
113         return (a);
114 }
115
116 /*
117  * Least Common Multiple.
118  */
119 static u_int
120 lcm(u_int a, u_int b)
121 {
122
123         return ((a * b) / gcd(a, b));
124 }
125
126 static void
127 g_stripe_init(struct g_class *mp __unused)
128 {
129
130         g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL,
131             NULL, NULL, 0, 0);
132         g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS;
133         uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS);
134 }
135
136 static void
137 g_stripe_fini(struct g_class *mp __unused)
138 {
139
140         uma_zdestroy(g_stripe_zone);
141 }
142
143 /*
144  * Return the number of valid disks.
145  */
146 static u_int
147 g_stripe_nvalid(struct g_stripe_softc *sc)
148 {
149         u_int i, no;
150
151         no = 0;
152         for (i = 0; i < sc->sc_ndisks; i++) {
153                 if (sc->sc_disks[i] != NULL)
154                         no++;
155         }
156
157         return (no);
158 }
159
160 static void
161 g_stripe_remove_disk(struct g_consumer *cp)
162 {
163         struct g_stripe_softc *sc;
164
165         g_topology_assert();
166         KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
167         sc = (struct g_stripe_softc *)cp->geom->softc;
168         KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
169
170         if (cp->private == NULL) {
171                 G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
172                     cp->provider->name, sc->sc_name);
173                 cp->private = (void *)(uintptr_t)-1;
174         }
175
176         if (sc->sc_provider != NULL) {
177                 sc->sc_provider->flags |= G_PF_WITHER;
178                 G_STRIPE_DEBUG(0, "Device %s deactivated.",
179                     sc->sc_provider->name);
180                 g_orphan_provider(sc->sc_provider, ENXIO);
181                 sc->sc_provider = NULL;
182         }
183
184         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
185                 return;
186         sc->sc_disks[cp->index] = NULL;
187         cp->index = 0;
188         g_detach(cp);
189         g_destroy_consumer(cp);
190         /* If there are no valid disks anymore, remove device. */
191         if (LIST_EMPTY(&sc->sc_geom->consumer))
192                 g_stripe_destroy(sc, 1);
193 }
194
195 static void
196 g_stripe_orphan(struct g_consumer *cp)
197 {
198         struct g_stripe_softc *sc;
199         struct g_geom *gp;
200
201         g_topology_assert();
202         gp = cp->geom;
203         sc = gp->softc;
204         if (sc == NULL)
205                 return;
206
207         g_stripe_remove_disk(cp);
208 }
209
210 static int
211 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
212 {
213         struct g_consumer *cp1, *cp2, *tmp;
214         struct g_stripe_softc *sc;
215         struct g_geom *gp;
216         int error;
217
218         g_topology_assert();
219         gp = pp->geom;
220         sc = gp->softc;
221         KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
222
223         /* On first open, grab an extra "exclusive" bit */
224         if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
225                 de++;
226         /* ... and let go of it on last close */
227         if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
228                 de--;
229
230         LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
231                 error = g_access(cp1, dr, dw, de);
232                 if (error != 0)
233                         goto fail;
234                 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
235                     cp1->private != NULL) {
236                         g_stripe_remove_disk(cp1); /* May destroy geom. */
237                 }
238         }
239         return (0);
240
241 fail:
242         LIST_FOREACH(cp2, &gp->consumer, consumer) {
243                 if (cp1 == cp2)
244                         break;
245                 g_access(cp2, -dr, -dw, -de);
246         }
247         return (error);
248 }
249
250 static void
251 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
252     off_t length, int mode)
253 {
254         u_int stripesize;
255         size_t len;
256
257         stripesize = sc->sc_stripesize;
258         len = (size_t)(stripesize - (offset & (stripesize - 1)));
259         do {
260                 bcopy(src, dst, len);
261                 if (mode) {
262                         dst += len + stripesize * (sc->sc_ndisks - 1);
263                         src += len;
264                 } else {
265                         dst += len;
266                         src += len + stripesize * (sc->sc_ndisks - 1);
267                 }
268                 length -= len;
269                 KASSERT(length >= 0,
270                     ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
271                     (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
272                 if (length > stripesize)
273                         len = stripesize;
274                 else
275                         len = length;
276         } while (length > 0);
277 }
278
279 static void
280 g_stripe_done(struct bio *bp)
281 {
282         struct g_stripe_softc *sc;
283         struct bio *pbp;
284
285         pbp = bp->bio_parent;
286         sc = pbp->bio_to->geom->softc;
287         if (pbp->bio_error == 0)
288                 pbp->bio_error = bp->bio_error;
289         pbp->bio_completed += bp->bio_completed;
290         if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
291                 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
292                     bp->bio_length, 1);
293                 bp->bio_data = bp->bio_caller1;
294                 bp->bio_caller1 = NULL;
295         }
296         g_destroy_bio(bp);
297         pbp->bio_inbed++;
298         if (pbp->bio_children == pbp->bio_inbed) {
299                 if (pbp->bio_driver1 != NULL)
300                         uma_zfree(g_stripe_zone, pbp->bio_driver1);
301                 g_io_deliver(pbp, pbp->bio_error);
302         }
303 }
304
305 static int
306 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
307 {
308         TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
309         u_int nparts = 0, stripesize;
310         struct g_stripe_softc *sc;
311         char *addr, *data = NULL;
312         struct bio *cbp;
313         int error;
314
315         sc = bp->bio_to->geom->softc;
316
317         addr = bp->bio_data;
318         stripesize = sc->sc_stripesize;
319
320         cbp = g_clone_bio(bp);
321         if (cbp == NULL) {
322                 error = ENOMEM;
323                 goto failure;
324         }
325         TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
326         nparts++;
327         /*
328          * Fill in the component buf structure.
329          */
330         cbp->bio_done = g_stripe_done;
331         cbp->bio_offset = offset;
332         cbp->bio_data = addr;
333         cbp->bio_caller1 = NULL;
334         cbp->bio_length = length;
335         cbp->bio_caller2 = sc->sc_disks[no];
336
337         /* offset -= offset % stripesize; */
338         offset -= offset & (stripesize - 1);
339         addr += length;
340         length = bp->bio_length - length;
341         for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
342                 if (no > sc->sc_ndisks - 1) {
343                         no = 0;
344                         offset += stripesize;
345                 }
346                 if (nparts >= sc->sc_ndisks) {
347                         cbp = TAILQ_NEXT(cbp, bio_queue);
348                         if (cbp == NULL)
349                                 cbp = TAILQ_FIRST(&queue);
350                         nparts++;
351                         /*
352                          * Update bio structure.
353                          */
354                         /*
355                          * MIN() is in case when
356                          * (bp->bio_length % sc->sc_stripesize) != 0.
357                          */
358                         cbp->bio_length += MIN(stripesize, length);
359                         if (cbp->bio_caller1 == NULL) {
360                                 cbp->bio_caller1 = cbp->bio_data;
361                                 cbp->bio_data = NULL;
362                                 if (data == NULL) {
363                                         data = uma_zalloc(g_stripe_zone,
364                                             M_NOWAIT);
365                                         if (data == NULL) {
366                                                 error = ENOMEM;
367                                                 goto failure;
368                                         }
369                                 }
370                         }
371                 } else {
372                         cbp = g_clone_bio(bp);
373                         if (cbp == NULL) {
374                                 error = ENOMEM;
375                                 goto failure;
376                         }
377                         TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
378                         nparts++;
379                         /*
380                          * Fill in the component buf structure.
381                          */
382                         cbp->bio_done = g_stripe_done;
383                         cbp->bio_offset = offset;
384                         cbp->bio_data = addr;
385                         cbp->bio_caller1 = NULL;
386                         /*
387                          * MIN() is in case when
388                          * (bp->bio_length % sc->sc_stripesize) != 0.
389                          */
390                         cbp->bio_length = MIN(stripesize, length);
391                         cbp->bio_caller2 = sc->sc_disks[no];
392                 }
393         }
394         if (data != NULL)
395                 bp->bio_driver1 = data;
396         /*
397          * Fire off all allocated requests!
398          */
399         while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
400                 struct g_consumer *cp;
401
402                 TAILQ_REMOVE(&queue, cbp, bio_queue);
403                 cp = cbp->bio_caller2;
404                 cbp->bio_caller2 = NULL;
405                 cbp->bio_to = cp->provider;
406                 if (cbp->bio_caller1 != NULL) {
407                         cbp->bio_data = data;
408                         if (bp->bio_cmd == BIO_WRITE) {
409                                 g_stripe_copy(sc, cbp->bio_caller1, data,
410                                     cbp->bio_offset, cbp->bio_length, 0);
411                         }
412                         data += cbp->bio_length;
413                 }
414                 G_STRIPE_LOGREQ(cbp, "Sending request.");
415                 g_io_request(cbp, cp);
416         }
417         return (0);
418 failure:
419         if (data != NULL)
420                 uma_zfree(g_stripe_zone, data);
421         while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
422                 TAILQ_REMOVE(&queue, cbp, bio_queue);
423                 if (cbp->bio_caller1 != NULL) {
424                         cbp->bio_data = cbp->bio_caller1;
425                         cbp->bio_caller1 = NULL;
426                 }
427                 bp->bio_children--;
428                 g_destroy_bio(cbp);
429         }
430         return (error);
431 }
432
433 static int
434 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
435 {
436         TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
437         struct g_stripe_softc *sc;
438         uint32_t stripesize;
439         struct bio *cbp;
440         char *addr;
441         int error;
442
443         sc = bp->bio_to->geom->softc;
444
445         addr = bp->bio_data;
446         stripesize = sc->sc_stripesize;
447
448         cbp = g_clone_bio(bp);
449         if (cbp == NULL) {
450                 error = ENOMEM;
451                 goto failure;
452         }
453         TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
454         /*
455          * Fill in the component buf structure.
456          */
457         cbp->bio_done = g_std_done;
458         cbp->bio_offset = offset;
459         cbp->bio_data = addr;
460         cbp->bio_length = length;
461         cbp->bio_caller2 = sc->sc_disks[no];
462
463         /* offset -= offset % stripesize; */
464         offset -= offset & (stripesize - 1);
465         if (bp->bio_cmd != BIO_DELETE)
466                 addr += length;
467         length = bp->bio_length - length;
468         for (no++; length > 0; no++, length -= stripesize) {
469                 if (no > sc->sc_ndisks - 1) {
470                         no = 0;
471                         offset += stripesize;
472                 }
473                 cbp = g_clone_bio(bp);
474                 if (cbp == NULL) {
475                         error = ENOMEM;
476                         goto failure;
477                 }
478                 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
479
480                 /*
481                  * Fill in the component buf structure.
482                  */
483                 cbp->bio_done = g_std_done;
484                 cbp->bio_offset = offset;
485                 cbp->bio_data = addr;
486                 /*
487                  * MIN() is in case when
488                  * (bp->bio_length % sc->sc_stripesize) != 0.
489                  */
490                 cbp->bio_length = MIN(stripesize, length);
491
492                 cbp->bio_caller2 = sc->sc_disks[no];
493
494                 if (bp->bio_cmd != BIO_DELETE)
495                         addr += stripesize;
496         }
497         /*
498          * Fire off all allocated requests!
499          */
500         while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
501                 struct g_consumer *cp;
502
503                 TAILQ_REMOVE(&queue, cbp, bio_queue);
504                 cp = cbp->bio_caller2;
505                 cbp->bio_caller2 = NULL;
506                 cbp->bio_to = cp->provider;
507                 G_STRIPE_LOGREQ(cbp, "Sending request.");
508                 g_io_request(cbp, cp);
509         }
510         return (0);
511 failure:
512         while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
513                 TAILQ_REMOVE(&queue, cbp, bio_queue);
514                 bp->bio_children--;
515                 g_destroy_bio(cbp);
516         }
517         return (error);
518 }
519
520 static void
521 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
522 {
523         struct bio_queue_head queue;
524         struct g_consumer *cp;
525         struct bio *cbp;
526         u_int no;
527
528         bioq_init(&queue);
529         for (no = 0; no < sc->sc_ndisks; no++) {
530                 cbp = g_clone_bio(bp);
531                 if (cbp == NULL) {
532                         for (cbp = bioq_first(&queue); cbp != NULL;
533                             cbp = bioq_first(&queue)) {
534                                 bioq_remove(&queue, cbp);
535                                 g_destroy_bio(cbp);
536                         }
537                         if (bp->bio_error == 0)
538                                 bp->bio_error = ENOMEM;
539                         g_io_deliver(bp, bp->bio_error);
540                         return;
541                 }
542                 bioq_insert_tail(&queue, cbp);
543                 cbp->bio_done = g_std_done;
544                 cbp->bio_caller1 = sc->sc_disks[no];
545                 cbp->bio_to = sc->sc_disks[no]->provider;
546         }
547         for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
548                 bioq_remove(&queue, cbp);
549                 G_STRIPE_LOGREQ(cbp, "Sending request.");
550                 cp = cbp->bio_caller1;
551                 cbp->bio_caller1 = NULL;
552                 g_io_request(cbp, cp);
553         }
554 }
555
556 static void
557 g_stripe_start(struct bio *bp)
558 {
559         off_t offset, start, length, nstripe;
560         struct g_stripe_softc *sc;
561         u_int no, stripesize;
562         int error, fast = 0;
563
564         sc = bp->bio_to->geom->softc;
565         /*
566          * If sc == NULL, provider's error should be set and g_stripe_start()
567          * should not be called at all.
568          */
569         KASSERT(sc != NULL,
570             ("Provider's error should be set (error=%d)(device=%s).",
571             bp->bio_to->error, bp->bio_to->name));
572
573         G_STRIPE_LOGREQ(bp, "Request received.");
574
575         switch (bp->bio_cmd) {
576         case BIO_READ:
577         case BIO_WRITE:
578         case BIO_DELETE:
579                 break;
580         case BIO_FLUSH:
581                 g_stripe_flush(sc, bp);
582                 return;
583         case BIO_GETATTR:
584                 /* To which provider it should be delivered? */
585         default:
586                 g_io_deliver(bp, EOPNOTSUPP);
587                 return;
588         }
589
590         stripesize = sc->sc_stripesize;
591
592         /*
593          * Calculations are quite messy, but fast I hope.
594          */
595
596         /* Stripe number. */
597         /* nstripe = bp->bio_offset / stripesize; */
598         nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
599         /* Disk number. */
600         no = nstripe % sc->sc_ndisks;
601         /* Start position in stripe. */
602         /* start = bp->bio_offset % stripesize; */
603         start = bp->bio_offset & (stripesize - 1);
604         /* Start position in disk. */
605         /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
606         offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
607         /* Length of data to operate. */
608         length = MIN(bp->bio_length, stripesize - start);
609
610         /*
611          * Do use "fast" mode when:
612          * 1. "Fast" mode is ON.
613          * and
614          * 2. Request size is less than or equal to MAXPHYS,
615          *    which should always be true.
616          * and
617          * 3. Request size is bigger than stripesize * ndisks. If it isn't,
618          *    there will be no need to send more than one I/O request to
619          *    a provider, so there is nothing to optmize.
620          * and
621          * 5. It is not a BIO_DELETE.
622          */
623         if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
624             bp->bio_length >= stripesize * sc->sc_ndisks &&
625             bp->bio_cmd != BIO_DELETE) {
626                 fast = 1;
627         }
628         error = 0;
629         if (fast) {
630                 error = g_stripe_start_fast(bp, no, offset, length);
631                 if (error != 0)
632                         g_stripe_fast_failed++;
633         }
634         /*
635          * Do use "economic" when:
636          * 1. "Economic" mode is ON.
637          * or
638          * 2. "Fast" mode failed. It can only fail if there is no memory.
639          */
640         if (!fast || error != 0)
641                 error = g_stripe_start_economic(bp, no, offset, length);
642         if (error != 0) {
643                 if (bp->bio_error == 0)
644                         bp->bio_error = error;
645                 g_io_deliver(bp, bp->bio_error);
646         }
647 }
648
649 static void
650 g_stripe_check_and_run(struct g_stripe_softc *sc)
651 {
652         off_t mediasize, ms;
653         u_int no, sectorsize = 0;
654
655         g_topology_assert();
656         if (g_stripe_nvalid(sc) != sc->sc_ndisks)
657                 return;
658
659         sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
660             sc->sc_name);
661         /*
662          * Find the smallest disk.
663          */
664         mediasize = sc->sc_disks[0]->provider->mediasize;
665         if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
666                 mediasize -= sc->sc_disks[0]->provider->sectorsize;
667         mediasize -= mediasize % sc->sc_stripesize;
668         sectorsize = sc->sc_disks[0]->provider->sectorsize;
669         for (no = 1; no < sc->sc_ndisks; no++) {
670                 ms = sc->sc_disks[no]->provider->mediasize;
671                 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
672                         ms -= sc->sc_disks[no]->provider->sectorsize;
673                 ms -= ms % sc->sc_stripesize;
674                 if (ms < mediasize)
675                         mediasize = ms;
676                 sectorsize = lcm(sectorsize,
677                     sc->sc_disks[no]->provider->sectorsize);
678         }
679         sc->sc_provider->sectorsize = sectorsize;
680         sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
681         sc->sc_provider->stripesize = sc->sc_stripesize;
682         sc->sc_provider->stripeoffset = 0;
683         g_error_provider(sc->sc_provider, 0);
684
685         G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
686 }
687
688 static int
689 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
690 {
691         struct g_provider *pp;
692         u_char *buf;
693         int error;
694
695         g_topology_assert();
696
697         error = g_access(cp, 1, 0, 0);
698         if (error != 0)
699                 return (error);
700         pp = cp->provider;
701         g_topology_unlock();
702         buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
703             &error);
704         g_topology_lock();
705         g_access(cp, -1, 0, 0);
706         if (buf == NULL)
707                 return (error);
708
709         /* Decode metadata. */
710         stripe_metadata_decode(buf, md);
711         g_free(buf);
712
713         return (0);
714 }
715
716 /*
717  * Add disk to given device.
718  */
719 static int
720 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
721 {
722         struct g_consumer *cp, *fcp;
723         struct g_geom *gp;
724         int error;
725
726         g_topology_assert();
727         /* Metadata corrupted? */
728         if (no >= sc->sc_ndisks)
729                 return (EINVAL);
730
731         /* Check if disk is not already attached. */
732         if (sc->sc_disks[no] != NULL)
733                 return (EEXIST);
734
735         gp = sc->sc_geom;
736         fcp = LIST_FIRST(&gp->consumer);
737
738         cp = g_new_consumer(gp);
739         cp->private = NULL;
740         cp->index = no;
741         error = g_attach(cp, pp);
742         if (error != 0) {
743                 g_destroy_consumer(cp);
744                 return (error);
745         }
746
747         if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
748                 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
749                 if (error != 0) {
750                         g_detach(cp);
751                         g_destroy_consumer(cp);
752                         return (error);
753                 }
754         }
755         if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
756                 struct g_stripe_metadata md;
757
758                 /* Reread metadata. */
759                 error = g_stripe_read_metadata(cp, &md);
760                 if (error != 0)
761                         goto fail;
762
763                 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
764                     strcmp(md.md_name, sc->sc_name) != 0 ||
765                     md.md_id != sc->sc_id) {
766                         G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
767                         goto fail;
768                 }
769         }
770
771         sc->sc_disks[no] = cp;
772         G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
773         g_stripe_check_and_run(sc);
774
775         return (0);
776 fail:
777         if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
778                 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
779         g_detach(cp);
780         g_destroy_consumer(cp);
781         return (error);
782 }
783
784 static struct g_geom *
785 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
786     u_int type)
787 {
788         struct g_stripe_softc *sc;
789         struct g_geom *gp;
790         u_int no;
791
792         g_topology_assert();
793         G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
794             md->md_id);
795
796         /* Two disks is minimum. */
797         if (md->md_all < 2) {
798                 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
799                 return (NULL);
800         }
801 #if 0
802         /* Stripe size have to be grater than or equal to sector size. */
803         if (md->md_stripesize < sectorsize) {
804                 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
805                 return (NULL);
806         }
807 #endif
808         /* Stripe size have to be power of 2. */
809         if (!powerof2(md->md_stripesize)) {
810                 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
811                 return (NULL);
812         }
813
814         /* Check for duplicate unit */
815         LIST_FOREACH(gp, &mp->geom, geom) {
816                 sc = gp->softc;
817                 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
818                         G_STRIPE_DEBUG(0, "Device %s already configured.",
819                             sc->sc_name);
820                         return (NULL);
821                 }
822         }
823         gp = g_new_geomf(mp, "%s", md->md_name);
824         sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
825         gp->start = g_stripe_start;
826         gp->spoiled = g_stripe_orphan;
827         gp->orphan = g_stripe_orphan;
828         gp->access = g_stripe_access;
829         gp->dumpconf = g_stripe_dumpconf;
830
831         sc->sc_id = md->md_id;
832         sc->sc_stripesize = md->md_stripesize;
833         sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
834         sc->sc_ndisks = md->md_all;
835         sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
836             M_STRIPE, M_WAITOK | M_ZERO);
837         for (no = 0; no < sc->sc_ndisks; no++)
838                 sc->sc_disks[no] = NULL;
839         sc->sc_type = type;
840
841         gp->softc = sc;
842         sc->sc_geom = gp;
843         sc->sc_provider = NULL;
844
845         G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
846
847         return (gp);
848 }
849
850 static int
851 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
852 {
853         struct g_provider *pp;
854         struct g_consumer *cp, *cp1;
855         struct g_geom *gp;
856
857         g_topology_assert();
858
859         if (sc == NULL)
860                 return (ENXIO);
861
862         pp = sc->sc_provider;
863         if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
864                 if (force) {
865                         G_STRIPE_DEBUG(0, "Device %s is still open, so it "
866                             "can't be definitely removed.", pp->name);
867                 } else {
868                         G_STRIPE_DEBUG(1,
869                             "Device %s is still open (r%dw%de%d).", pp->name,
870                             pp->acr, pp->acw, pp->ace);
871                         return (EBUSY);
872                 }
873         }
874
875         gp = sc->sc_geom;
876         LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
877                 g_stripe_remove_disk(cp);
878                 if (cp1 == NULL)
879                         return (0);     /* Recursion happened. */
880         }
881         if (!LIST_EMPTY(&gp->consumer))
882                 return (EINPROGRESS);
883
884         gp->softc = NULL;
885         KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
886             gp->name));
887         free(sc->sc_disks, M_STRIPE);
888         free(sc, M_STRIPE);
889         G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
890         g_wither_geom(gp, ENXIO);
891         return (0);
892 }
893
894 static int
895 g_stripe_destroy_geom(struct gctl_req *req __unused,
896     struct g_class *mp __unused, struct g_geom *gp)
897 {
898         struct g_stripe_softc *sc;
899
900         sc = gp->softc;
901         return (g_stripe_destroy(sc, 0));
902 }
903
904 static struct g_geom *
905 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
906 {
907         struct g_stripe_metadata md;
908         struct g_stripe_softc *sc;
909         struct g_consumer *cp;
910         struct g_geom *gp;
911         int error;
912
913         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
914         g_topology_assert();
915
916         /* Skip providers that are already open for writing. */
917         if (pp->acw > 0)
918                 return (NULL);
919
920         G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
921
922         gp = g_new_geomf(mp, "stripe:taste");
923         gp->start = g_stripe_start;
924         gp->access = g_stripe_access;
925         gp->orphan = g_stripe_orphan;
926         cp = g_new_consumer(gp);
927         g_attach(cp, pp);
928         error = g_stripe_read_metadata(cp, &md);
929         g_detach(cp);
930         g_destroy_consumer(cp);
931         g_destroy_geom(gp);
932         if (error != 0)
933                 return (NULL);
934         gp = NULL;
935
936         if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
937                 return (NULL);
938         if (md.md_version > G_STRIPE_VERSION) {
939                 printf("geom_stripe.ko module is too old to handle %s.\n",
940                     pp->name);
941                 return (NULL);
942         }
943         /*
944          * Backward compatibility:
945          */
946         /* There was no md_provider field in earlier versions of metadata. */
947         if (md.md_version < 2)
948                 bzero(md.md_provider, sizeof(md.md_provider));
949         /* There was no md_provsize field in earlier versions of metadata. */
950         if (md.md_version < 3)
951                 md.md_provsize = pp->mediasize;
952
953         if (md.md_provider[0] != '\0' &&
954             !g_compare_names(md.md_provider, pp->name))
955                 return (NULL);
956         if (md.md_provsize != pp->mediasize)
957                 return (NULL);
958
959         /*
960          * Let's check if device already exists.
961          */
962         sc = NULL;
963         LIST_FOREACH(gp, &mp->geom, geom) {
964                 sc = gp->softc;
965                 if (sc == NULL)
966                         continue;
967                 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
968                         continue;
969                 if (strcmp(md.md_name, sc->sc_name) != 0)
970                         continue;
971                 if (md.md_id != sc->sc_id)
972                         continue;
973                 break;
974         }
975         if (gp != NULL) {
976                 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
977                 error = g_stripe_add_disk(sc, pp, md.md_no);
978                 if (error != 0) {
979                         G_STRIPE_DEBUG(0,
980                             "Cannot add disk %s to %s (error=%d).", pp->name,
981                             gp->name, error);
982                         return (NULL);
983                 }
984         } else {
985                 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
986                 if (gp == NULL) {
987                         G_STRIPE_DEBUG(0, "Cannot create device %s.",
988                             md.md_name);
989                         return (NULL);
990                 }
991                 sc = gp->softc;
992                 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
993                 error = g_stripe_add_disk(sc, pp, md.md_no);
994                 if (error != 0) {
995                         G_STRIPE_DEBUG(0,
996                             "Cannot add disk %s to %s (error=%d).", pp->name,
997                             gp->name, error);
998                         g_stripe_destroy(sc, 1);
999                         return (NULL);
1000                 }
1001         }
1002
1003         return (gp);
1004 }
1005
1006 static void
1007 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1008 {
1009         u_int attached, no;
1010         struct g_stripe_metadata md;
1011         struct g_provider *pp;
1012         struct g_stripe_softc *sc;
1013         struct g_geom *gp;
1014         struct sbuf *sb;
1015         intmax_t *stripesize;
1016         const char *name;
1017         char param[16];
1018         int *nargs;
1019
1020         g_topology_assert();
1021         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1022         if (nargs == NULL) {
1023                 gctl_error(req, "No '%s' argument.", "nargs");
1024                 return;
1025         }
1026         if (*nargs <= 2) {
1027                 gctl_error(req, "Too few arguments.");
1028                 return;
1029         }
1030
1031         strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1032         md.md_version = G_STRIPE_VERSION;
1033         name = gctl_get_asciiparam(req, "arg0");
1034         if (name == NULL) {
1035                 gctl_error(req, "No 'arg%u' argument.", 0);
1036                 return;
1037         }
1038         strlcpy(md.md_name, name, sizeof(md.md_name));
1039         md.md_id = arc4random();
1040         md.md_no = 0;
1041         md.md_all = *nargs - 1;
1042         stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1043         if (stripesize == NULL) {
1044                 gctl_error(req, "No '%s' argument.", "stripesize");
1045                 return;
1046         }
1047         md.md_stripesize = *stripesize;
1048         bzero(md.md_provider, sizeof(md.md_provider));
1049         /* This field is not important here. */
1050         md.md_provsize = 0;
1051
1052         /* Check all providers are valid */
1053         for (no = 1; no < *nargs; no++) {
1054                 snprintf(param, sizeof(param), "arg%u", no);
1055                 name = gctl_get_asciiparam(req, param);
1056                 if (name == NULL) {
1057                         gctl_error(req, "No 'arg%u' argument.", no);
1058                         return;
1059                 }
1060                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1061                         name += strlen("/dev/");
1062                 pp = g_provider_by_name(name);
1063                 if (pp == NULL) {
1064                         G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1065                         gctl_error(req, "Disk %s is invalid.", name);
1066                         return;
1067                 }
1068         }
1069
1070         gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1071         if (gp == NULL) {
1072                 gctl_error(req, "Can't configure %s.", md.md_name);
1073                 return;
1074         }
1075
1076         sc = gp->softc;
1077         sb = sbuf_new_auto();
1078         sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1079         for (attached = 0, no = 1; no < *nargs; no++) {
1080                 snprintf(param, sizeof(param), "arg%u", no);
1081                 name = gctl_get_asciiparam(req, param);
1082                 if (name == NULL) {
1083                         gctl_error(req, "No 'arg%u' argument.", no);
1084                         continue;
1085                 }
1086                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1087                         name += strlen("/dev/");
1088                 pp = g_provider_by_name(name);
1089                 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1090                 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1091                         G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1092                             no, pp->name, gp->name);
1093                         sbuf_printf(sb, " %s", pp->name);
1094                         continue;
1095                 }
1096                 attached++;
1097         }
1098         sbuf_finish(sb);
1099         if (md.md_all != attached) {
1100                 g_stripe_destroy(gp->softc, 1);
1101                 gctl_error(req, "%s", sbuf_data(sb));
1102         }
1103         sbuf_delete(sb);
1104 }
1105
1106 static struct g_stripe_softc *
1107 g_stripe_find_device(struct g_class *mp, const char *name)
1108 {
1109         struct g_stripe_softc *sc;
1110         struct g_geom *gp;
1111
1112         LIST_FOREACH(gp, &mp->geom, geom) {
1113                 sc = gp->softc;
1114                 if (sc == NULL)
1115                         continue;
1116                 if (strcmp(sc->sc_name, name) == 0)
1117                         return (sc);
1118         }
1119         return (NULL);
1120 }
1121
1122 static void
1123 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1124 {
1125         struct g_stripe_softc *sc;
1126         int *force, *nargs, error;
1127         const char *name;
1128         char param[16];
1129         u_int i;
1130
1131         g_topology_assert();
1132
1133         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1134         if (nargs == NULL) {
1135                 gctl_error(req, "No '%s' argument.", "nargs");
1136                 return;
1137         }
1138         if (*nargs <= 0) {
1139                 gctl_error(req, "Missing device(s).");
1140                 return;
1141         }
1142         force = gctl_get_paraml(req, "force", sizeof(*force));
1143         if (force == NULL) {
1144                 gctl_error(req, "No '%s' argument.", "force");
1145                 return;
1146         }
1147
1148         for (i = 0; i < (u_int)*nargs; i++) {
1149                 snprintf(param, sizeof(param), "arg%u", i);
1150                 name = gctl_get_asciiparam(req, param);
1151                 if (name == NULL) {
1152                         gctl_error(req, "No 'arg%u' argument.", i);
1153                         return;
1154                 }
1155                 sc = g_stripe_find_device(mp, name);
1156                 if (sc == NULL) {
1157                         gctl_error(req, "No such device: %s.", name);
1158                         return;
1159                 }
1160                 error = g_stripe_destroy(sc, *force);
1161                 if (error != 0) {
1162                         gctl_error(req, "Cannot destroy device %s (error=%d).",
1163                             sc->sc_name, error);
1164                         return;
1165                 }
1166         }
1167 }
1168
1169 static void
1170 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1171 {
1172         uint32_t *version;
1173
1174         g_topology_assert();
1175
1176         version = gctl_get_paraml(req, "version", sizeof(*version));
1177         if (version == NULL) {
1178                 gctl_error(req, "No '%s' argument.", "version");
1179                 return;
1180         }
1181         if (*version != G_STRIPE_VERSION) {
1182                 gctl_error(req, "Userland and kernel parts are out of sync.");
1183                 return;
1184         }
1185
1186         if (strcmp(verb, "create") == 0) {
1187                 g_stripe_ctl_create(req, mp);
1188                 return;
1189         } else if (strcmp(verb, "destroy") == 0 ||
1190             strcmp(verb, "stop") == 0) {
1191                 g_stripe_ctl_destroy(req, mp);
1192                 return;
1193         }
1194
1195         gctl_error(req, "Unknown verb.");
1196 }
1197
1198 static void
1199 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1200     struct g_consumer *cp, struct g_provider *pp)
1201 {
1202         struct g_stripe_softc *sc;
1203
1204         sc = gp->softc;
1205         if (sc == NULL)
1206                 return;
1207         if (pp != NULL) {
1208                 /* Nothing here. */
1209         } else if (cp != NULL) {
1210                 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1211                     (u_int)cp->index);
1212         } else {
1213                 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1214                 sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1215                     (u_int)sc->sc_stripesize);
1216                 sbuf_printf(sb, "%s<Type>", indent);
1217                 switch (sc->sc_type) {
1218                 case G_STRIPE_TYPE_AUTOMATIC:
1219                         sbuf_printf(sb, "AUTOMATIC");
1220                         break;
1221                 case G_STRIPE_TYPE_MANUAL:
1222                         sbuf_printf(sb, "MANUAL");
1223                         break;
1224                 default:
1225                         sbuf_printf(sb, "UNKNOWN");
1226                         break;
1227                 }
1228                 sbuf_printf(sb, "</Type>\n");
1229                 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1230                     indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1231                 sbuf_printf(sb, "%s<State>", indent);
1232                 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1233                         sbuf_printf(sb, "UP");
1234                 else
1235                         sbuf_printf(sb, "DOWN");
1236                 sbuf_printf(sb, "</State>\n");
1237         }
1238 }
1239
1240 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);