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