]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/geom/stripe/g_stripe.c
MFC: r227309 (partial)
[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         u_int no;
165
166         KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
167         sc = (struct g_stripe_softc *)cp->private;
168         KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
169         no = cp->index;
170
171         G_STRIPE_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
172             sc->sc_name);
173
174         sc->sc_disks[no] = NULL;
175         if (sc->sc_provider != NULL) {
176                 sc->sc_provider->flags |= G_PF_WITHER;
177                 g_orphan_provider(sc->sc_provider, ENXIO);
178                 sc->sc_provider = NULL;
179                 G_STRIPE_DEBUG(0, "Device %s removed.", sc->sc_name);
180         }
181
182         if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
183                 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
184         g_detach(cp);
185         g_destroy_consumer(cp);
186 }
187
188 static void
189 g_stripe_orphan(struct g_consumer *cp)
190 {
191         struct g_stripe_softc *sc;
192         struct g_geom *gp;
193
194         g_topology_assert();
195         gp = cp->geom;
196         sc = gp->softc;
197         if (sc == NULL)
198                 return;
199
200         g_stripe_remove_disk(cp);
201         /* If there are no valid disks anymore, remove device. */
202         if (g_stripe_nvalid(sc) == 0)
203                 g_stripe_destroy(sc, 1);
204 }
205
206 static int
207 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
208 {
209         struct g_consumer *cp1, *cp2;
210         struct g_stripe_softc *sc;
211         struct g_geom *gp;
212         int error;
213
214         gp = pp->geom;
215         sc = gp->softc;
216
217         if (sc == NULL) {
218                 /*
219                  * It looks like geom is being withered.
220                  * In that case we allow only negative requests.
221                  */
222                 KASSERT(dr <= 0 && dw <= 0 && de <= 0,
223                     ("Positive access request (device=%s).", pp->name));
224                 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 &&
225                     (pp->ace + de) == 0) {
226                         G_STRIPE_DEBUG(0, "Device %s definitely destroyed.",
227                             gp->name);
228                 }
229                 return (0);
230         }
231
232         /* On first open, grab an extra "exclusive" bit */
233         if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
234                 de++;
235         /* ... and let go of it on last close */
236         if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
237                 de--;
238
239         error = ENXIO;
240         LIST_FOREACH(cp1, &gp->consumer, consumer) {
241                 error = g_access(cp1, dr, dw, de);
242                 if (error == 0)
243                         continue;
244                 /*
245                  * If we fail here, backout all previous changes.
246                  */
247                 LIST_FOREACH(cp2, &gp->consumer, consumer) {
248                         if (cp1 == cp2)
249                                 return (error);
250                         g_access(cp2, -dr, -dw, -de);
251                 }
252                 /* NOTREACHED */
253         }
254
255         return (error);
256 }
257
258 static void
259 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
260     off_t length, int mode)
261 {
262         u_int stripesize;
263         size_t len;
264
265         stripesize = sc->sc_stripesize;
266         len = (size_t)(stripesize - (offset & (stripesize - 1)));
267         do {
268                 bcopy(src, dst, len);
269                 if (mode) {
270                         dst += len + stripesize * (sc->sc_ndisks - 1);
271                         src += len;
272                 } else {
273                         dst += len;
274                         src += len + stripesize * (sc->sc_ndisks - 1);
275                 }
276                 length -= len;
277                 KASSERT(length >= 0,
278                     ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
279                     (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
280                 if (length > stripesize)
281                         len = stripesize;
282                 else
283                         len = length;
284         } while (length > 0);
285 }
286
287 static void
288 g_stripe_done(struct bio *bp)
289 {
290         struct g_stripe_softc *sc;
291         struct bio *pbp;
292
293         pbp = bp->bio_parent;
294         sc = pbp->bio_to->geom->softc;
295         if (pbp->bio_error == 0)
296                 pbp->bio_error = bp->bio_error;
297         pbp->bio_completed += bp->bio_completed;
298         if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
299                 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
300                     bp->bio_length, 1);
301                 bp->bio_data = bp->bio_caller1;
302                 bp->bio_caller1 = NULL;
303         }
304         g_destroy_bio(bp);
305         pbp->bio_inbed++;
306         if (pbp->bio_children == pbp->bio_inbed) {
307                 if (pbp->bio_driver1 != NULL)
308                         uma_zfree(g_stripe_zone, pbp->bio_driver1);
309                 g_io_deliver(pbp, pbp->bio_error);
310         }
311 }
312
313 static int
314 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
315 {
316         TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
317         u_int nparts = 0, stripesize;
318         struct g_stripe_softc *sc;
319         char *addr, *data = NULL;
320         struct bio *cbp;
321         int error;
322
323         sc = bp->bio_to->geom->softc;
324
325         addr = bp->bio_data;
326         stripesize = sc->sc_stripesize;
327
328         cbp = g_clone_bio(bp);
329         if (cbp == NULL) {
330                 error = ENOMEM;
331                 goto failure;
332         }
333         TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
334         nparts++;
335         /*
336          * Fill in the component buf structure.
337          */
338         cbp->bio_done = g_stripe_done;
339         cbp->bio_offset = offset;
340         cbp->bio_data = addr;
341         cbp->bio_caller1 = NULL;
342         cbp->bio_length = length;
343         cbp->bio_caller2 = sc->sc_disks[no];
344
345         /* offset -= offset % stripesize; */
346         offset -= offset & (stripesize - 1);
347         addr += length;
348         length = bp->bio_length - length;
349         for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
350                 if (no > sc->sc_ndisks - 1) {
351                         no = 0;
352                         offset += stripesize;
353                 }
354                 if (nparts >= sc->sc_ndisks) {
355                         cbp = TAILQ_NEXT(cbp, bio_queue);
356                         if (cbp == NULL)
357                                 cbp = TAILQ_FIRST(&queue);
358                         nparts++;
359                         /*
360                          * Update bio structure.
361                          */
362                         /*
363                          * MIN() is in case when
364                          * (bp->bio_length % sc->sc_stripesize) != 0.
365                          */
366                         cbp->bio_length += MIN(stripesize, length);
367                         if (cbp->bio_caller1 == NULL) {
368                                 cbp->bio_caller1 = cbp->bio_data;
369                                 cbp->bio_data = NULL;
370                                 if (data == NULL) {
371                                         data = uma_zalloc(g_stripe_zone,
372                                             M_NOWAIT);
373                                         if (data == NULL) {
374                                                 error = ENOMEM;
375                                                 goto failure;
376                                         }
377                                 }
378                         }
379                 } else {
380                         cbp = g_clone_bio(bp);
381                         if (cbp == NULL) {
382                                 error = ENOMEM;
383                                 goto failure;
384                         }
385                         TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
386                         nparts++;
387                         /*
388                          * Fill in the component buf structure.
389                          */
390                         cbp->bio_done = g_stripe_done;
391                         cbp->bio_offset = offset;
392                         cbp->bio_data = addr;
393                         cbp->bio_caller1 = NULL;
394                         /*
395                          * MIN() is in case when
396                          * (bp->bio_length % sc->sc_stripesize) != 0.
397                          */
398                         cbp->bio_length = MIN(stripesize, length);
399                         cbp->bio_caller2 = sc->sc_disks[no];
400                 }
401         }
402         if (data != NULL)
403                 bp->bio_driver1 = data;
404         /*
405          * Fire off all allocated requests!
406          */
407         while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
408                 struct g_consumer *cp;
409
410                 TAILQ_REMOVE(&queue, cbp, bio_queue);
411                 cp = cbp->bio_caller2;
412                 cbp->bio_caller2 = NULL;
413                 cbp->bio_to = cp->provider;
414                 if (cbp->bio_caller1 != NULL) {
415                         cbp->bio_data = data;
416                         if (bp->bio_cmd == BIO_WRITE) {
417                                 g_stripe_copy(sc, cbp->bio_caller1, data,
418                                     cbp->bio_offset, cbp->bio_length, 0);
419                         }
420                         data += cbp->bio_length;
421                 }
422                 G_STRIPE_LOGREQ(cbp, "Sending request.");
423                 g_io_request(cbp, cp);
424         }
425         return (0);
426 failure:
427         if (data != NULL)
428                 uma_zfree(g_stripe_zone, data);
429         while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
430                 TAILQ_REMOVE(&queue, cbp, bio_queue);
431                 if (cbp->bio_caller1 != NULL) {
432                         cbp->bio_data = cbp->bio_caller1;
433                         cbp->bio_caller1 = NULL;
434                 }
435                 bp->bio_children--;
436                 g_destroy_bio(cbp);
437         }
438         return (error);
439 }
440
441 static int
442 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
443 {
444         TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
445         struct g_stripe_softc *sc;
446         uint32_t stripesize;
447         struct bio *cbp;
448         char *addr;
449         int error;
450
451         sc = bp->bio_to->geom->softc;
452
453         addr = bp->bio_data;
454         stripesize = sc->sc_stripesize;
455
456         cbp = g_clone_bio(bp);
457         if (cbp == NULL) {
458                 error = ENOMEM;
459                 goto failure;
460         }
461         TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
462         /*
463          * Fill in the component buf structure.
464          */
465         cbp->bio_done = g_std_done;
466         cbp->bio_offset = offset;
467         cbp->bio_data = addr;
468         cbp->bio_length = length;
469         cbp->bio_caller2 = sc->sc_disks[no];
470
471         /* offset -= offset % stripesize; */
472         offset -= offset & (stripesize - 1);
473         addr += length;
474         length = bp->bio_length - length;
475         for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
476                 if (no > sc->sc_ndisks - 1) {
477                         no = 0;
478                         offset += stripesize;
479                 }
480                 cbp = g_clone_bio(bp);
481                 if (cbp == NULL) {
482                         error = ENOMEM;
483                         goto failure;
484                 }
485                 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
486
487                 /*
488                  * Fill in the component buf structure.
489                  */
490                 cbp->bio_done = g_std_done;
491                 cbp->bio_offset = offset;
492                 cbp->bio_data = addr;
493                 /*
494                  * MIN() is in case when
495                  * (bp->bio_length % sc->sc_stripesize) != 0.
496                  */
497                 cbp->bio_length = MIN(stripesize, length);
498
499                 cbp->bio_caller2 = sc->sc_disks[no];
500         }
501         /*
502          * Fire off all allocated requests!
503          */
504         while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
505                 struct g_consumer *cp;
506
507                 TAILQ_REMOVE(&queue, cbp, bio_queue);
508                 cp = cbp->bio_caller2;
509                 cbp->bio_caller2 = NULL;
510                 cbp->bio_to = cp->provider;
511                 G_STRIPE_LOGREQ(cbp, "Sending request.");
512                 g_io_request(cbp, cp);
513         }
514         return (0);
515 failure:
516         while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
517                 TAILQ_REMOVE(&queue, cbp, bio_queue);
518                 bp->bio_children--;
519                 g_destroy_bio(cbp);
520         }
521         return (error);
522 }
523
524 static void
525 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
526 {
527         struct bio_queue_head queue;
528         struct g_consumer *cp;
529         struct bio *cbp;
530         u_int no;
531
532         bioq_init(&queue);
533         for (no = 0; no < sc->sc_ndisks; no++) {
534                 cbp = g_clone_bio(bp);
535                 if (cbp == NULL) {
536                         for (cbp = bioq_first(&queue); cbp != NULL;
537                             cbp = bioq_first(&queue)) {
538                                 bioq_remove(&queue, cbp);
539                                 g_destroy_bio(cbp);
540                         }
541                         if (bp->bio_error == 0)
542                                 bp->bio_error = ENOMEM;
543                         g_io_deliver(bp, bp->bio_error);
544                         return;
545                 }
546                 bioq_insert_tail(&queue, cbp);
547                 cbp->bio_done = g_std_done;
548                 cbp->bio_caller1 = sc->sc_disks[no];
549                 cbp->bio_to = sc->sc_disks[no]->provider;
550         }
551         for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
552                 bioq_remove(&queue, cbp);
553                 G_STRIPE_LOGREQ(cbp, "Sending request.");
554                 cp = cbp->bio_caller1;
555                 cbp->bio_caller1 = NULL;
556                 g_io_request(cbp, cp);
557         }
558 }
559
560 static void
561 g_stripe_start(struct bio *bp)
562 {
563         off_t offset, start, length, nstripe;
564         struct g_stripe_softc *sc;
565         u_int no, stripesize;
566         int error, fast = 0;
567
568         sc = bp->bio_to->geom->softc;
569         /*
570          * If sc == NULL, provider's error should be set and g_stripe_start()
571          * should not be called at all.
572          */
573         KASSERT(sc != NULL,
574             ("Provider's error should be set (error=%d)(device=%s).",
575             bp->bio_to->error, bp->bio_to->name));
576
577         G_STRIPE_LOGREQ(bp, "Request received.");
578
579         switch (bp->bio_cmd) {
580         case BIO_READ:
581         case BIO_WRITE:
582         case BIO_DELETE:
583                 break;
584         case BIO_FLUSH:
585                 g_stripe_flush(sc, bp);
586                 return;
587         case BIO_GETATTR:
588                 /* To which provider it should be delivered? */
589         default:
590                 g_io_deliver(bp, EOPNOTSUPP);
591                 return;
592         }
593
594         stripesize = sc->sc_stripesize;
595
596         /*
597          * Calculations are quite messy, but fast I hope.
598          */
599
600         /* Stripe number. */
601         /* nstripe = bp->bio_offset / stripesize; */
602         nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
603         /* Disk number. */
604         no = nstripe % sc->sc_ndisks;
605         /* Start position in stripe. */
606         /* start = bp->bio_offset % stripesize; */
607         start = bp->bio_offset & (stripesize - 1);
608         /* Start position in disk. */
609         /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
610         offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
611         /* Length of data to operate. */
612         length = MIN(bp->bio_length, stripesize - start);
613
614         /*
615          * Do use "fast" mode when:
616          * 1. "Fast" mode is ON.
617          * and
618          * 2. Request size is less than or equal to MAXPHYS,
619          *    which should always be true.
620          * and
621          * 3. Request size is bigger than stripesize * ndisks. If it isn't,
622          *    there will be no need to send more than one I/O request to
623          *    a provider, so there is nothing to optmize.
624          */
625         if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
626             bp->bio_length >= stripesize * sc->sc_ndisks) {
627                 fast = 1;
628         }
629         error = 0;
630         if (fast) {
631                 error = g_stripe_start_fast(bp, no, offset, length);
632                 if (error != 0)
633                         g_stripe_fast_failed++;
634         }
635         /*
636          * Do use "economic" when:
637          * 1. "Economic" mode is ON.
638          * or
639          * 2. "Fast" mode failed. It can only fail if there is no memory.
640          */
641         if (!fast || error != 0)
642                 error = g_stripe_start_economic(bp, no, offset, length);
643         if (error != 0) {
644                 if (bp->bio_error == 0)
645                         bp->bio_error = error;
646                 g_io_deliver(bp, bp->bio_error);
647         }
648 }
649
650 static void
651 g_stripe_check_and_run(struct g_stripe_softc *sc)
652 {
653         off_t mediasize, ms;
654         u_int no, sectorsize = 0;
655
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_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         /* Metadata corrupted? */
727         if (no >= sc->sc_ndisks)
728                 return (EINVAL);
729
730         /* Check if disk is not already attached. */
731         if (sc->sc_disks[no] != NULL)
732                 return (EEXIST);
733
734         gp = sc->sc_geom;
735         fcp = LIST_FIRST(&gp->consumer);
736
737         cp = g_new_consumer(gp);
738         error = g_attach(cp, pp);
739         if (error != 0) {
740                 g_destroy_consumer(cp);
741                 return (error);
742         }
743
744         if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
745                 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
746                 if (error != 0) {
747                         g_detach(cp);
748                         g_destroy_consumer(cp);
749                         return (error);
750                 }
751         }
752         if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
753                 struct g_stripe_metadata md;
754
755                 /* Reread metadata. */
756                 error = g_stripe_read_metadata(cp, &md);
757                 if (error != 0)
758                         goto fail;
759
760                 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
761                     strcmp(md.md_name, sc->sc_name) != 0 ||
762                     md.md_id != sc->sc_id) {
763                         G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
764                         goto fail;
765                 }
766         }
767
768         cp->private = sc;
769         cp->index = no;
770         sc->sc_disks[no] = cp;
771
772         G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
773
774         g_stripe_check_and_run(sc);
775
776         return (0);
777 fail:
778         if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
779                 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
780         g_detach(cp);
781         g_destroy_consumer(cp);
782         return (error);
783 }
784
785 static struct g_geom *
786 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
787     u_int type)
788 {
789         struct g_stripe_softc *sc;
790         struct g_geom *gp;
791         u_int no;
792
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_geom *gp;
855         u_int no;
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         for (no = 0; no < sc->sc_ndisks; no++) {
876                 if (sc->sc_disks[no] != NULL)
877                         g_stripe_remove_disk(sc->sc_disks[no]);
878         }
879
880         gp = sc->sc_geom;
881         gp->softc = NULL;
882         KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
883             gp->name));
884         free(sc->sc_disks, M_STRIPE);
885         free(sc, M_STRIPE);
886
887         pp = LIST_FIRST(&gp->provider);
888         if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
889                 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
890
891         g_wither_geom(gp, ENXIO);
892
893         return (0);
894 }
895
896 static int
897 g_stripe_destroy_geom(struct gctl_req *req __unused,
898     struct g_class *mp __unused, struct g_geom *gp)
899 {
900         struct g_stripe_softc *sc;
901
902         sc = gp->softc;
903         return (g_stripe_destroy(sc, 0));
904 }
905
906 static struct g_geom *
907 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
908 {
909         struct g_stripe_metadata md;
910         struct g_stripe_softc *sc;
911         struct g_consumer *cp;
912         struct g_geom *gp;
913         int error;
914
915         g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
916         g_topology_assert();
917
918         /* Skip providers that are already open for writing. */
919         if (pp->acw > 0)
920                 return (NULL);
921
922         G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
923
924         gp = g_new_geomf(mp, "stripe:taste");
925         gp->start = g_stripe_start;
926         gp->access = g_stripe_access;
927         gp->orphan = g_stripe_orphan;
928         cp = g_new_consumer(gp);
929         g_attach(cp, pp);
930         error = g_stripe_read_metadata(cp, &md);
931         g_detach(cp);
932         g_destroy_consumer(cp);
933         g_destroy_geom(gp);
934         if (error != 0)
935                 return (NULL);
936         gp = NULL;
937
938         if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
939                 return (NULL);
940         if (md.md_version > G_STRIPE_VERSION) {
941                 printf("geom_stripe.ko module is too old to handle %s.\n",
942                     pp->name);
943                 return (NULL);
944         }
945         /*
946          * Backward compatibility:
947          */
948         /* There was no md_provider field in earlier versions of metadata. */
949         if (md.md_version < 2)
950                 bzero(md.md_provider, sizeof(md.md_provider));
951         /* There was no md_provsize field in earlier versions of metadata. */
952         if (md.md_version < 3)
953                 md.md_provsize = pp->mediasize;
954
955         if (md.md_provider[0] != '\0' &&
956             !g_compare_names(md.md_provider, pp->name))
957                 return (NULL);
958         if (md.md_provsize != pp->mediasize)
959                 return (NULL);
960
961         /*
962          * Let's check if device already exists.
963          */
964         sc = NULL;
965         LIST_FOREACH(gp, &mp->geom, geom) {
966                 sc = gp->softc;
967                 if (sc == NULL)
968                         continue;
969                 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
970                         continue;
971                 if (strcmp(md.md_name, sc->sc_name) != 0)
972                         continue;
973                 if (md.md_id != sc->sc_id)
974                         continue;
975                 break;
976         }
977         if (gp != NULL) {
978                 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
979                 error = g_stripe_add_disk(sc, pp, md.md_no);
980                 if (error != 0) {
981                         G_STRIPE_DEBUG(0,
982                             "Cannot add disk %s to %s (error=%d).", pp->name,
983                             gp->name, error);
984                         return (NULL);
985                 }
986         } else {
987                 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
988                 if (gp == NULL) {
989                         G_STRIPE_DEBUG(0, "Cannot create device %s.",
990                             md.md_name);
991                         return (NULL);
992                 }
993                 sc = gp->softc;
994                 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
995                 error = g_stripe_add_disk(sc, pp, md.md_no);
996                 if (error != 0) {
997                         G_STRIPE_DEBUG(0,
998                             "Cannot add disk %s to %s (error=%d).", pp->name,
999                             gp->name, error);
1000                         g_stripe_destroy(sc, 1);
1001                         return (NULL);
1002                 }
1003         }
1004
1005         return (gp);
1006 }
1007
1008 static void
1009 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1010 {
1011         u_int attached, no;
1012         struct g_stripe_metadata md;
1013         struct g_provider *pp;
1014         struct g_stripe_softc *sc;
1015         struct g_geom *gp;
1016         struct sbuf *sb;
1017         intmax_t *stripesize;
1018         const char *name;
1019         char param[16];
1020         int *nargs;
1021
1022         g_topology_assert();
1023         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1024         if (nargs == NULL) {
1025                 gctl_error(req, "No '%s' argument.", "nargs");
1026                 return;
1027         }
1028         if (*nargs <= 2) {
1029                 gctl_error(req, "Too few arguments.");
1030                 return;
1031         }
1032
1033         strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1034         md.md_version = G_STRIPE_VERSION;
1035         name = gctl_get_asciiparam(req, "arg0");
1036         if (name == NULL) {
1037                 gctl_error(req, "No 'arg%u' argument.", 0);
1038                 return;
1039         }
1040         strlcpy(md.md_name, name, sizeof(md.md_name));
1041         md.md_id = arc4random();
1042         md.md_no = 0;
1043         md.md_all = *nargs - 1;
1044         stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1045         if (stripesize == NULL) {
1046                 gctl_error(req, "No '%s' argument.", "stripesize");
1047                 return;
1048         }
1049         md.md_stripesize = *stripesize;
1050         bzero(md.md_provider, sizeof(md.md_provider));
1051         /* This field is not important here. */
1052         md.md_provsize = 0;
1053
1054         /* Check all providers are valid */
1055         for (no = 1; no < *nargs; no++) {
1056                 snprintf(param, sizeof(param), "arg%u", no);
1057                 name = gctl_get_asciiparam(req, param);
1058                 if (name == NULL) {
1059                         gctl_error(req, "No 'arg%u' argument.", no);
1060                         return;
1061                 }
1062                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1063                         name += strlen("/dev/");
1064                 pp = g_provider_by_name(name);
1065                 if (pp == NULL) {
1066                         G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1067                         gctl_error(req, "Disk %s is invalid.", name);
1068                         return;
1069                 }
1070         }
1071
1072         gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1073         if (gp == NULL) {
1074                 gctl_error(req, "Can't configure %s.", md.md_name);
1075                 return;
1076         }
1077
1078         sc = gp->softc;
1079         sb = sbuf_new_auto();
1080         sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1081         for (attached = 0, no = 1; no < *nargs; no++) {
1082                 snprintf(param, sizeof(param), "arg%u", no);
1083                 name = gctl_get_asciiparam(req, param);
1084                 if (name == NULL) {
1085                         gctl_error(req, "No 'arg%u' argument.", no);
1086                         continue;
1087                 }
1088                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1089                         name += strlen("/dev/");
1090                 pp = g_provider_by_name(name);
1091                 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1092                 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1093                         G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1094                             no, pp->name, gp->name);
1095                         sbuf_printf(sb, " %s", pp->name);
1096                         continue;
1097                 }
1098                 attached++;
1099         }
1100         sbuf_finish(sb);
1101         if (md.md_all != attached) {
1102                 g_stripe_destroy(gp->softc, 1);
1103                 gctl_error(req, "%s", sbuf_data(sb));
1104         }
1105         sbuf_delete(sb);
1106 }
1107
1108 static struct g_stripe_softc *
1109 g_stripe_find_device(struct g_class *mp, const char *name)
1110 {
1111         struct g_stripe_softc *sc;
1112         struct g_geom *gp;
1113
1114         LIST_FOREACH(gp, &mp->geom, geom) {
1115                 sc = gp->softc;
1116                 if (sc == NULL)
1117                         continue;
1118                 if (strcmp(sc->sc_name, name) == 0)
1119                         return (sc);
1120         }
1121         return (NULL);
1122 }
1123
1124 static void
1125 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1126 {
1127         struct g_stripe_softc *sc;
1128         int *force, *nargs, error;
1129         const char *name;
1130         char param[16];
1131         u_int i;
1132
1133         g_topology_assert();
1134
1135         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1136         if (nargs == NULL) {
1137                 gctl_error(req, "No '%s' argument.", "nargs");
1138                 return;
1139         }
1140         if (*nargs <= 0) {
1141                 gctl_error(req, "Missing device(s).");
1142                 return;
1143         }
1144         force = gctl_get_paraml(req, "force", sizeof(*force));
1145         if (force == NULL) {
1146                 gctl_error(req, "No '%s' argument.", "force");
1147                 return;
1148         }
1149
1150         for (i = 0; i < (u_int)*nargs; i++) {
1151                 snprintf(param, sizeof(param), "arg%u", i);
1152                 name = gctl_get_asciiparam(req, param);
1153                 if (name == NULL) {
1154                         gctl_error(req, "No 'arg%u' argument.", i);
1155                         return;
1156                 }
1157                 sc = g_stripe_find_device(mp, name);
1158                 if (sc == NULL) {
1159                         gctl_error(req, "No such device: %s.", name);
1160                         return;
1161                 }
1162                 error = g_stripe_destroy(sc, *force);
1163                 if (error != 0) {
1164                         gctl_error(req, "Cannot destroy device %s (error=%d).",
1165                             sc->sc_name, error);
1166                         return;
1167                 }
1168         }
1169 }
1170
1171 static void
1172 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1173 {
1174         uint32_t *version;
1175
1176         g_topology_assert();
1177
1178         version = gctl_get_paraml(req, "version", sizeof(*version));
1179         if (version == NULL) {
1180                 gctl_error(req, "No '%s' argument.", "version");
1181                 return;
1182         }
1183         if (*version != G_STRIPE_VERSION) {
1184                 gctl_error(req, "Userland and kernel parts are out of sync.");
1185                 return;
1186         }
1187
1188         if (strcmp(verb, "create") == 0) {
1189                 g_stripe_ctl_create(req, mp);
1190                 return;
1191         } else if (strcmp(verb, "destroy") == 0 ||
1192             strcmp(verb, "stop") == 0) {
1193                 g_stripe_ctl_destroy(req, mp);
1194                 return;
1195         }
1196
1197         gctl_error(req, "Unknown verb.");
1198 }
1199
1200 static void
1201 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1202     struct g_consumer *cp, struct g_provider *pp)
1203 {
1204         struct g_stripe_softc *sc;
1205
1206         sc = gp->softc;
1207         if (sc == NULL)
1208                 return;
1209         if (pp != NULL) {
1210                 /* Nothing here. */
1211         } else if (cp != NULL) {
1212                 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1213                     (u_int)cp->index);
1214         } else {
1215                 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1216                 sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1217                     (u_int)sc->sc_stripesize);
1218                 sbuf_printf(sb, "%s<Type>", indent);
1219                 switch (sc->sc_type) {
1220                 case G_STRIPE_TYPE_AUTOMATIC:
1221                         sbuf_printf(sb, "AUTOMATIC");
1222                         break;
1223                 case G_STRIPE_TYPE_MANUAL:
1224                         sbuf_printf(sb, "MANUAL");
1225                         break;
1226                 default:
1227                         sbuf_printf(sb, "UNKNOWN");
1228                         break;
1229                 }
1230                 sbuf_printf(sb, "</Type>\n");
1231                 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1232                     indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1233                 sbuf_printf(sb, "%s<State>", indent);
1234                 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1235                         sbuf_printf(sb, "UP");
1236                 else
1237                         sbuf_printf(sb, "DOWN");
1238                 sbuf_printf(sb, "</State>\n");
1239         }
1240 }
1241
1242 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);