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