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