xref: /onnv-gate/usr/src/cmd/lvm/metassist/layout/layout_stripe.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <string.h>
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <libintl.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "volume_error.h"
34*0Sstevel@tonic-gate #include "volume_devconfig.h"
35*0Sstevel@tonic-gate #include "volume_dlist.h"
36*0Sstevel@tonic-gate #include "volume_output.h"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include "layout_device_cache.h"
39*0Sstevel@tonic-gate #include "layout_device_util.h"
40*0Sstevel@tonic-gate #include "layout_discovery.h"
41*0Sstevel@tonic-gate #include "layout_dlist_util.h"
42*0Sstevel@tonic-gate #include "layout_messages.h"
43*0Sstevel@tonic-gate #include "layout_request.h"
44*0Sstevel@tonic-gate #include "layout_slice.h"
45*0Sstevel@tonic-gate #include "layout_svm_util.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #define	_LAYOUT_STRIPE_C
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate static int compose_stripe(
50*0Sstevel@tonic-gate 	devconfig_t	*request,
51*0Sstevel@tonic-gate 	uint64_t	nbytes,
52*0Sstevel@tonic-gate 	dlist_t		*disks,
53*0Sstevel@tonic-gate 	int		max,
54*0Sstevel@tonic-gate 	int		min,
55*0Sstevel@tonic-gate 	dlist_t		*othervols,
56*0Sstevel@tonic-gate 	devconfig_t	**stripe);
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate static int compose_stripe_within_hba(
59*0Sstevel@tonic-gate 	devconfig_t	*request,
60*0Sstevel@tonic-gate 	dlist_t		*hbas,
61*0Sstevel@tonic-gate 	uint64_t	nbytes,
62*0Sstevel@tonic-gate 	uint16_t	min,
63*0Sstevel@tonic-gate 	uint16_t	max,
64*0Sstevel@tonic-gate 	devconfig_t	**stripe);
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static int assemble_stripe(
67*0Sstevel@tonic-gate 	devconfig_t	*request,
68*0Sstevel@tonic-gate 	dlist_t		*comps,
69*0Sstevel@tonic-gate 	devconfig_t	**stripe);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static dlist_t *
72*0Sstevel@tonic-gate order_stripe_components_alternate_hbas(
73*0Sstevel@tonic-gate 	dlist_t *comps);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate static int compute_usable_stripe_capacity(
76*0Sstevel@tonic-gate 	dlist_t		*comps,
77*0Sstevel@tonic-gate 	uint64_t	ilace,
78*0Sstevel@tonic-gate 	uint64_t	*nbytes);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  * FUNCTION:	layout_stripe(devconfig_t *request, uint64_t nbytes,
82*0Sstevel@tonic-gate  *			dlist_t **results)
83*0Sstevel@tonic-gate  *
84*0Sstevel@tonic-gate  * INPUT:	request	- pointer to a devconfig_t of the current request
85*0Sstevel@tonic-gate  *		nbytes	- the desired capacity of the stripe
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  * OUPUT:	results	- pointer to a list of composed volumes
88*0Sstevel@tonic-gate  *
89*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
90*0Sstevel@tonic-gate  *			 !0 otherwise.
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  * PURPOSE:	Main layout driver for composing stripe volumes.
93*0Sstevel@tonic-gate  *
94*0Sstevel@tonic-gate  *		Attempts to construct a stripe of size nbytes.
95*0Sstevel@tonic-gate  *
96*0Sstevel@tonic-gate  *		Basic goal of all strategies is to build wide-thin stripes:
97*0Sstevel@tonic-gate  *		build widest stripe possible across as many HBAs as possible.
98*0Sstevel@tonic-gate  *
99*0Sstevel@tonic-gate  *		Several different layout strategies are tried in order
100*0Sstevel@tonic-gate  *		of preference until one succeeds or there are none left.
101*0Sstevel@tonic-gate  *
102*0Sstevel@tonic-gate  *		1 - stripe across similar HBAs
103*0Sstevel@tonic-gate  *		    . number of components is driven by # of HBAs
104*0Sstevel@tonic-gate  *		    . requires mincomp available HBAs
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  *		2 - stripe within a single HBA
107*0Sstevel@tonic-gate  *		    . number of components is driven by # of disks
108*0Sstevel@tonic-gate  *		    . requires at least 1 HBA with mincomp disks
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  *		3 - stripe across all available disks on similar HBAs
111*0Sstevel@tonic-gate  *		    . number of components is driven by # of disk
112*0Sstevel@tonic-gate  *		    . requires at least mincomp disks
113*0Sstevel@tonic-gate  *
114*0Sstevel@tonic-gate  *		4 - stripe across all available HBAs
115*0Sstevel@tonic-gate  *		    . number of components is driven by # of HBAs
116*0Sstevel@tonic-gate  *		    . requires at least mincomp HBAs
117*0Sstevel@tonic-gate  *
118*0Sstevel@tonic-gate  *		5 - stripe across all available disks on all HBAs
119*0Sstevel@tonic-gate  *		    . number of components is driven by # of disks
120*0Sstevel@tonic-gate  *		    . requires at least mincomp disks
121*0Sstevel@tonic-gate  *
122*0Sstevel@tonic-gate  *		Each strategy tries to compose a stripe with the
123*0Sstevel@tonic-gate  *		maximum number of components first then reduces the
124*0Sstevel@tonic-gate  *		number of components down to mincomp.
125*0Sstevel@tonic-gate  *
126*0Sstevel@tonic-gate  *		get allowed minimum number of stripe components
127*0Sstevel@tonic-gate  *		get allowed maximum number of stripe components
128*0Sstevel@tonic-gate  *		get available HBAs
129*0Sstevel@tonic-gate  *
130*0Sstevel@tonic-gate  *		group HBAs by characteristics
131*0Sstevel@tonic-gate  *		for (each HBA grouping) and (stripe not composed) {
132*0Sstevel@tonic-gate  *		    select next HBA group
133*0Sstevel@tonic-gate  *		    for (strategy[1,2,3]) and (stripe not composed) {
134*0Sstevel@tonic-gate  *			compose stripe using HBAs in group
135*0Sstevel@tonic-gate  *		    }
136*0Sstevel@tonic-gate  *		}
137*0Sstevel@tonic-gate  *
138*0Sstevel@tonic-gate  *		if (stripe not composed) {
139*0Sstevel@tonic-gate  *		    for (strategy[4,5]) and (stripe not composed) {
140*0Sstevel@tonic-gate  *			compose stripe using all HBAs
141*0Sstevel@tonic-gate  *		    }
142*0Sstevel@tonic-gate  *		}
143*0Sstevel@tonic-gate  *
144*0Sstevel@tonic-gate  *		if (stripe composed) {
145*0Sstevel@tonic-gate  *		    append composed stripe to results
146*0Sstevel@tonic-gate  *		}
147*0Sstevel@tonic-gate  *
148*0Sstevel@tonic-gate  */
149*0Sstevel@tonic-gate int
layout_stripe(devconfig_t * request,uint64_t nbytes,dlist_t ** results)150*0Sstevel@tonic-gate layout_stripe(
151*0Sstevel@tonic-gate 	devconfig_t	*request,
152*0Sstevel@tonic-gate 	uint64_t	nbytes,
153*0Sstevel@tonic-gate 	dlist_t		**results)
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	/*
156*0Sstevel@tonic-gate 	 * these enums define the # of strategies and the preference order
157*0Sstevel@tonic-gate 	 * in which they are tried
158*0Sstevel@tonic-gate 	 */
159*0Sstevel@tonic-gate 	typedef enum {
160*0Sstevel@tonic-gate 		STRIPE_ACROSS_SIMILAR_HBAS_DISK_PER = 0,
161*0Sstevel@tonic-gate 		STRIPE_WITHIN_SIMILAR_HBA,
162*0Sstevel@tonic-gate 		STRIPE_ACROSS_SIMILAR_HBAS,
163*0Sstevel@tonic-gate 		N_SIMILAR_HBA_STRATEGIES
164*0Sstevel@tonic-gate 	} similar_hba_strategy_order_t;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	typedef enum {
167*0Sstevel@tonic-gate 		STRIPE_ACROSS_ANY_HBAS_DISK_PER = 0,
168*0Sstevel@tonic-gate 		STRIPE_ACROSS_ANY_HBAS,
169*0Sstevel@tonic-gate 		N_ANY_HBA_STRATEGIES
170*0Sstevel@tonic-gate 	} any_hba_strategy_order_t;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	dlist_t		*usable_hbas = NULL;
174*0Sstevel@tonic-gate 	dlist_t		*similar_hba_groups = NULL;
175*0Sstevel@tonic-gate 	dlist_t		*iter = NULL;
176*0Sstevel@tonic-gate 	devconfig_t	*stripe = NULL;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	uint16_t	mincomp	= 0;
179*0Sstevel@tonic-gate 	uint16_t	maxcomp	= 0;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	int		error = 0;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	(error = get_usable_hbas(&usable_hbas));
184*0Sstevel@tonic-gate 	if (error != 0) {
185*0Sstevel@tonic-gate 	    return (error);
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	print_layout_volume_msg(devconfig_type_to_str(TYPE_STRIPE), nbytes);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	if (dlist_length(usable_hbas) == 0) {
191*0Sstevel@tonic-gate 	    print_no_hbas_msg();
192*0Sstevel@tonic-gate 	    volume_set_error(gettext("There are no usable HBAs."));
193*0Sstevel@tonic-gate 	    return (-1);
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	((error = group_similar_hbas(usable_hbas, &similar_hba_groups)) != 0) ||
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	/*
199*0Sstevel@tonic-gate 	 * determine the min/max number of stripe components
200*0Sstevel@tonic-gate 	 * based on the request, the diskset defaults or the
201*0Sstevel@tonic-gate 	 * global defaults.  These are absolute limits, the
202*0Sstevel@tonic-gate 	 * actual values are determined by the number of HBAs
203*0Sstevel@tonic-gate 	 * and/or disks available.
204*0Sstevel@tonic-gate 	 */
205*0Sstevel@tonic-gate 	(error = get_stripe_min_comp(request, &mincomp)) ||
206*0Sstevel@tonic-gate 	(error = get_stripe_max_comp(request, &maxcomp));
207*0Sstevel@tonic-gate 	if (error != 0) {
208*0Sstevel@tonic-gate 	    return (error);
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	for (iter = similar_hba_groups;
212*0Sstevel@tonic-gate 	    (error == 0) && (stripe == NULL) && (iter != NULL);
213*0Sstevel@tonic-gate 	    iter = iter->next) {
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	    dlist_t *hbas = (dlist_t *)iter->obj;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	    similar_hba_strategy_order_t order;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	    for (order = STRIPE_ACROSS_SIMILAR_HBAS_DISK_PER;
220*0Sstevel@tonic-gate 		(order < N_SIMILAR_HBA_STRATEGIES) &&
221*0Sstevel@tonic-gate 			(stripe == NULL) && (error == 0);
222*0Sstevel@tonic-gate 		order++) {
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		dlist_t *selhbas = NULL;
225*0Sstevel@tonic-gate 		dlist_t	*disks = NULL;
226*0Sstevel@tonic-gate 		int	n = 0;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		switch (order) {
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate 		case STRIPE_ACROSS_SIMILAR_HBAS_DISK_PER:
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 		    error = select_hbas_with_n_disks(
233*0Sstevel@tonic-gate 			    request, hbas, 1, &selhbas, &disks);
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		    if (error == 0) {
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate /* BEGIN CSTYLED */
238*0Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
239*0Sstevel@tonic-gate gettext("  -->Strategy 1: use 1 disk from %d-%d similar HBAs - stripe across HBAs\n"),
240*0Sstevel@tonic-gate 	mincomp, maxcomp);
241*0Sstevel@tonic-gate /* END CSTYLED */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 			if ((n = dlist_length(selhbas)) >= mincomp) {
244*0Sstevel@tonic-gate 			    n = ((n > maxcomp) ? maxcomp : n);
245*0Sstevel@tonic-gate 			    error = compose_stripe(
246*0Sstevel@tonic-gate 				    request, nbytes, disks, n,
247*0Sstevel@tonic-gate 				    mincomp, NULL, &stripe);
248*0Sstevel@tonic-gate 			} else {
249*0Sstevel@tonic-gate 			    print_insufficient_hbas_msg(n);
250*0Sstevel@tonic-gate 			}
251*0Sstevel@tonic-gate 		    }
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 		    break;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 		case STRIPE_WITHIN_SIMILAR_HBA:
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 		    error = select_hbas_with_n_disks(
258*0Sstevel@tonic-gate 			    request, hbas, mincomp, &selhbas, &disks);
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 		    if (error == 0) {
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate /* BEGIN CSTYLED */
263*0Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
264*0Sstevel@tonic-gate gettext("  -->Strategy 2: use %d-%d disks from any single HBA - stripe within HBA\n"),
265*0Sstevel@tonic-gate 	mincomp, maxcomp);
266*0Sstevel@tonic-gate /* END CSTYLED */
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 			if ((n = dlist_length(selhbas)) > 0) {
269*0Sstevel@tonic-gate 			    error = compose_stripe_within_hba(
270*0Sstevel@tonic-gate 				    request, selhbas, nbytes,
271*0Sstevel@tonic-gate 				    mincomp, maxcomp, &stripe);
272*0Sstevel@tonic-gate 			} else {
273*0Sstevel@tonic-gate 			    print_insufficient_disks_msg(n);
274*0Sstevel@tonic-gate 			}
275*0Sstevel@tonic-gate 		    }
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 		    break;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 		case STRIPE_ACROSS_SIMILAR_HBAS:
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 		    error = select_hbas_with_n_disks(
282*0Sstevel@tonic-gate 			    request, hbas, 1, &selhbas, &disks);
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 		    if (error == 0) {
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate /* BEGIN CSTYLED */
287*0Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
288*0Sstevel@tonic-gate gettext("  -->Strategy 3: use %d-%d disks from %d similar HBAs - stripe across HBAs\n"),
289*0Sstevel@tonic-gate 	mincomp, maxcomp, dlist_length(hbas));
290*0Sstevel@tonic-gate /* END CSTYLED */
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 			if ((n = dlist_length(selhbas)) > 0) {
293*0Sstevel@tonic-gate 			    if ((n = dlist_length(disks)) >= mincomp) {
294*0Sstevel@tonic-gate 				n = ((n > maxcomp) ? maxcomp : n);
295*0Sstevel@tonic-gate 				error = compose_stripe(
296*0Sstevel@tonic-gate 					request, nbytes, disks, n,
297*0Sstevel@tonic-gate 					mincomp, NULL, &stripe);
298*0Sstevel@tonic-gate 			    } else {
299*0Sstevel@tonic-gate 				print_insufficient_disks_msg(n);
300*0Sstevel@tonic-gate 			    }
301*0Sstevel@tonic-gate 			} else {
302*0Sstevel@tonic-gate 			    print_insufficient_hbas_msg(n);
303*0Sstevel@tonic-gate 			}
304*0Sstevel@tonic-gate 		    }
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 		    break;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 		default:
309*0Sstevel@tonic-gate 		    break;
310*0Sstevel@tonic-gate 		}
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 		dlist_free_items(disks, NULL);
313*0Sstevel@tonic-gate 		dlist_free_items(selhbas, NULL);
314*0Sstevel@tonic-gate 	    }
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	for (iter = similar_hba_groups; iter != NULL; iter = iter->next) {
318*0Sstevel@tonic-gate 	    dlist_free_items((dlist_t *)iter->obj, NULL);
319*0Sstevel@tonic-gate 	}
320*0Sstevel@tonic-gate 	dlist_free_items(similar_hba_groups, NULL);
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	/*
323*0Sstevel@tonic-gate 	 * if striping within similar HBA groups failed,
324*0Sstevel@tonic-gate 	 * try across all available HBAs
325*0Sstevel@tonic-gate 	 */
326*0Sstevel@tonic-gate 	if ((stripe == NULL) && (error == 0)) {
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	    any_hba_strategy_order_t order;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	    for (order = STRIPE_ACROSS_ANY_HBAS_DISK_PER;
331*0Sstevel@tonic-gate 		(order < N_ANY_HBA_STRATEGIES) &&
332*0Sstevel@tonic-gate 			(stripe == NULL) && (error == 0);
333*0Sstevel@tonic-gate 		order++) {
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 		dlist_t	*selhbas = NULL;
336*0Sstevel@tonic-gate 		dlist_t	*disks = NULL;
337*0Sstevel@tonic-gate 		int	n = 0;
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 		switch (order) {
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 		case STRIPE_ACROSS_ANY_HBAS_DISK_PER:
342*0Sstevel@tonic-gate 
343*0Sstevel@tonic-gate 		    error = select_hbas_with_n_disks(
344*0Sstevel@tonic-gate 			    request, usable_hbas, 1, &selhbas, &disks);
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 		    if (error == 0) {
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate /* BEGIN CSTYLED */
349*0Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
350*0Sstevel@tonic-gate gettext("  -->Strategy 4: use 1 disk from %d-%d available HBAs - stripe across any HBAs\n"),
351*0Sstevel@tonic-gate 	mincomp, maxcomp);
352*0Sstevel@tonic-gate /* END CSTYLED */
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 			if ((n = dlist_length(selhbas)) >= mincomp) {
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 			    n = ((n > maxcomp) ? maxcomp : n);
357*0Sstevel@tonic-gate 			    error = compose_stripe(
358*0Sstevel@tonic-gate 				    request, nbytes, disks, n,
359*0Sstevel@tonic-gate 				    mincomp, NULL, &stripe);
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 			} else {
362*0Sstevel@tonic-gate 			    print_insufficient_hbas_msg(n);
363*0Sstevel@tonic-gate 			}
364*0Sstevel@tonic-gate 		    }
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 		    break;
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 		case STRIPE_ACROSS_ANY_HBAS:
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 		    error = select_hbas_with_n_disks(
371*0Sstevel@tonic-gate 			    request, usable_hbas, 1, &selhbas, &disks);
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 		    if (error == 0) {
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate /* BEGIN CSTYLED */
376*0Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
377*0Sstevel@tonic-gate gettext("  -->Strategy 5: use %d-%d disks from %d available HBA - stripe across any HBAs\n"),
378*0Sstevel@tonic-gate 	mincomp, maxcomp, dlist_length(selhbas));
379*0Sstevel@tonic-gate /* END CSTYLED */
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 			if ((n = dlist_length(disks)) >= mincomp) {
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 			    n = ((n > maxcomp) ? maxcomp : n);
384*0Sstevel@tonic-gate 			    error = compose_stripe(
385*0Sstevel@tonic-gate 				    request, nbytes, disks, n,
386*0Sstevel@tonic-gate 				    mincomp, NULL, &stripe);
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 			} else {
389*0Sstevel@tonic-gate 			    print_insufficient_disks_msg(n);
390*0Sstevel@tonic-gate 			}
391*0Sstevel@tonic-gate 		    }
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 		    break;
394*0Sstevel@tonic-gate 		}
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 		dlist_free_items(disks, NULL);
397*0Sstevel@tonic-gate 		dlist_free_items(selhbas, NULL);
398*0Sstevel@tonic-gate 	    }
399*0Sstevel@tonic-gate 	}
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (stripe != NULL) {
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	    dlist_t *item = NULL;
404*0Sstevel@tonic-gate 	    if ((item = dlist_new_item(stripe)) == NULL) {
405*0Sstevel@tonic-gate 		error = ENOMEM;
406*0Sstevel@tonic-gate 	    } else {
407*0Sstevel@tonic-gate 		*results = dlist_append(item, *results, AT_TAIL);
408*0Sstevel@tonic-gate 		print_layout_success_msg();
409*0Sstevel@tonic-gate 	    }
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	} else if (error != 0) {
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 	    print_debug_failure_msg(
414*0Sstevel@tonic-gate 		    devconfig_type_to_str(TYPE_STRIPE),
415*0Sstevel@tonic-gate 		    get_error_string(error));
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 	} else {
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	    print_insufficient_resources_msg(
420*0Sstevel@tonic-gate 		    devconfig_type_to_str(TYPE_STRIPE));
421*0Sstevel@tonic-gate 	    error = -1;
422*0Sstevel@tonic-gate 	}
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 	return (error);
425*0Sstevel@tonic-gate }
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate /*
428*0Sstevel@tonic-gate  * FUNCTION:	populate_stripe(devconfig_t *request, uint64_t nbytes,
429*0Sstevel@tonic-gate  *			dlist_t *disks, uint16_t ncomp, dlist_t *othervols,
430*0Sstevel@tonic-gate  *			devconfig_t **stripe)
431*0Sstevel@tonic-gate  *
432*0Sstevel@tonic-gate  * INPUT:	request	- pointer to a request devconfig_t
433*0Sstevel@tonic-gate  *		nbytes	- desired stripe size
434*0Sstevel@tonic-gate  *		disks	- pointer to a list of availalb disks
435*0Sstevel@tonic-gate  *		ncomp	- number of components desired
436*0Sstevel@tonic-gate  *		othervols - pointer to a list of other volumes whose
437*0Sstevel@tonic-gate  *				composition may affect this stripe
438*0Sstevel@tonic-gate  *				(e.g., submirrors of the same mirror)
439*0Sstevel@tonic-gate  *
440*0Sstevel@tonic-gate  * OUTPUT:	stripe	- pointer to a devconfig_t to hold resulting stripe
441*0Sstevel@tonic-gate  *
442*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
443*0Sstevel@tonic-gate  *			 !0 otherwise.
444*0Sstevel@tonic-gate  *
445*0Sstevel@tonic-gate  * PURPOSE:	Helper to populate a stripe with the specified number of
446*0Sstevel@tonic-gate  *		components and aggregate capacity using slices on disks
447*0Sstevel@tonic-gate  *		in the input list.
448*0Sstevel@tonic-gate  *
449*0Sstevel@tonic-gate  *		If the othervols list is not empty, the slice components
450*0Sstevel@tonic-gate  *		chosen for the stripe must not on the same disks as any
451*0Sstevel@tonic-gate  *		of the other volumes.
452*0Sstevel@tonic-gate  *
453*0Sstevel@tonic-gate  *		If sufficient slice components can be found, the stripe
454*0Sstevel@tonic-gate  *		is assembled and returned.
455*0Sstevel@tonic-gate  */
456*0Sstevel@tonic-gate int
populate_stripe(devconfig_t * request,uint64_t nbytes,dlist_t * disks,uint16_t ncomp,dlist_t * othervols,devconfig_t ** stripe)457*0Sstevel@tonic-gate populate_stripe(
458*0Sstevel@tonic-gate 	devconfig_t	*request,
459*0Sstevel@tonic-gate 	uint64_t	nbytes,
460*0Sstevel@tonic-gate 	dlist_t		*disks,
461*0Sstevel@tonic-gate 	uint16_t	ncomp,
462*0Sstevel@tonic-gate 	dlist_t		*othervols,
463*0Sstevel@tonic-gate 	devconfig_t	**stripe)
464*0Sstevel@tonic-gate {
465*0Sstevel@tonic-gate 	uint16_t	npaths = 0;
466*0Sstevel@tonic-gate 	uint16_t	ncomps = 0;	/* number of components found */
467*0Sstevel@tonic-gate 	uint64_t	rsize = 0;	/* reqd component size */
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	dlist_t		*other_hbas = NULL;
470*0Sstevel@tonic-gate 	dlist_t		*other_disks = NULL;
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 	dlist_t		*slices = NULL;
473*0Sstevel@tonic-gate 	dlist_t		*comps = NULL;
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 	int		error = 0;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	*stripe = NULL;
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	((error = disks_get_avail_slices(request, disks, &slices)) != 0) ||
480*0Sstevel@tonic-gate 	(error = get_volume_npaths(request, &npaths));
481*0Sstevel@tonic-gate 	if (error != 0) {
482*0Sstevel@tonic-gate 	    return (error);
483*0Sstevel@tonic-gate 	}
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	print_populate_volume_ncomps_msg(
486*0Sstevel@tonic-gate 		devconfig_type_to_str(TYPE_STRIPE), nbytes, ncomp);
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 	if (slices == NULL) {
489*0Sstevel@tonic-gate 	    print_populate_no_slices_msg();
490*0Sstevel@tonic-gate 	    return (0);
491*0Sstevel@tonic-gate 	}
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	/* determine HBAs and disks used by othervols */
494*0Sstevel@tonic-gate 	error = get_hbas_and_disks_used_by_volumes(othervols,
495*0Sstevel@tonic-gate 		&other_hbas, &other_disks);
496*0Sstevel@tonic-gate 	if (error != 0) {
497*0Sstevel@tonic-gate 	    dlist_free_items(other_hbas, NULL);
498*0Sstevel@tonic-gate 	    dlist_free_items(other_disks, NULL);
499*0Sstevel@tonic-gate 	    return (error);
500*0Sstevel@tonic-gate 	}
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 	print_populate_choose_slices_msg();
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 	/*
505*0Sstevel@tonic-gate 	 * each stripe component needs to be this size.
506*0Sstevel@tonic-gate 	 * Note that the stripe interlace doesn't need to be
507*0Sstevel@tonic-gate 	 * taken into account in this computation because any
508*0Sstevel@tonic-gate 	 * slice selected as a stripe component will be oversized
509*0Sstevel@tonic-gate 	 * to account for interlace and cylinder rounding done
510*0Sstevel@tonic-gate 	 * by libmeta.
511*0Sstevel@tonic-gate 	 */
512*0Sstevel@tonic-gate 	rsize = nbytes / ncomp;
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate 	/*
515*0Sstevel@tonic-gate 	 * need to select 'ncomp' slices that are at least 'rsize'
516*0Sstevel@tonic-gate 	 * large in order to reach the desired capacity.
517*0Sstevel@tonic-gate 	 */
518*0Sstevel@tonic-gate 	ncomps = 0;
519*0Sstevel@tonic-gate 	while ((ncomps < ncomp) && (error == 0)) {
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	    devconfig_t	*comp = NULL;
522*0Sstevel@tonic-gate 	    dlist_t	*item = NULL;
523*0Sstevel@tonic-gate 	    dlist_t	*rmvd = NULL;
524*0Sstevel@tonic-gate 	    char	*cname = NULL;
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate 	    /* BEGIN CSTYLED */
527*0Sstevel@tonic-gate 	    /*
528*0Sstevel@tonic-gate 	     * 1st B_TRUE: require a different disk than those used by
529*0Sstevel@tonic-gate 	     *		comps and othervols
530*0Sstevel@tonic-gate 	     * 2nd B_TRUE: requested size is minimum acceptable
531*0Sstevel@tonic-gate 	     * 3rd B_TRUE: add an extra cylinder to the resulting slice, this is
532*0Sstevel@tonic-gate 	     *		necessary for Stripe components whose sizes get rounded
533*0Sstevel@tonic-gate 	     *		down to an interlace multiple and then down to a cylinder
534*0Sstevel@tonic-gate 	     *		boundary.
535*0Sstevel@tonic-gate 	     */
536*0Sstevel@tonic-gate 	    /* END CSTYLED */
537*0Sstevel@tonic-gate 	    error = choose_slice(rsize, npaths, slices, comps,
538*0Sstevel@tonic-gate 		    other_hbas, other_disks, B_TRUE, B_TRUE, B_TRUE, &comp);
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate 	    if ((error == 0) && (comp != NULL)) {
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 		++ncomps;
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 		item = dlist_new_item(comp);
545*0Sstevel@tonic-gate 		if (item == NULL) {
546*0Sstevel@tonic-gate 		    error = ENOMEM;
547*0Sstevel@tonic-gate 		} else {
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 		    /* add selected component to comp list */
550*0Sstevel@tonic-gate 		    comps = dlist_insert_ordered(
551*0Sstevel@tonic-gate 			    item,
552*0Sstevel@tonic-gate 			    comps,
553*0Sstevel@tonic-gate 			    ASCENDING,
554*0Sstevel@tonic-gate 			    compare_devconfig_sizes);
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 		    /* remove it from the available list */
557*0Sstevel@tonic-gate 		    slices = dlist_remove_equivalent_item(slices, (void *) comp,
558*0Sstevel@tonic-gate 			    compare_devconfig_and_descriptor_names, &rmvd);
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 		    if (rmvd != NULL) {
561*0Sstevel@tonic-gate 			free(rmvd);
562*0Sstevel@tonic-gate 		    }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 		    /* add the component slice to the used list */
565*0Sstevel@tonic-gate 		    if ((error = devconfig_get_name(comp, &cname)) == 0) {
566*0Sstevel@tonic-gate 			error = add_used_slice_by_name(cname);
567*0Sstevel@tonic-gate 		    }
568*0Sstevel@tonic-gate 		}
569*0Sstevel@tonic-gate 	    } else if (comp == NULL) {
570*0Sstevel@tonic-gate 		/* no possible slice */
571*0Sstevel@tonic-gate 		break;
572*0Sstevel@tonic-gate 	    }
573*0Sstevel@tonic-gate 	}
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate 	dlist_free_items(slices, NULL);
576*0Sstevel@tonic-gate 	dlist_free_items(other_hbas, NULL);
577*0Sstevel@tonic-gate 	dlist_free_items(other_disks, NULL);
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate 	if (ncomps == ncomp) {
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	    if ((error = assemble_stripe(request, comps, stripe)) == 0) {
582*0Sstevel@tonic-gate 		print_populate_success_msg();
583*0Sstevel@tonic-gate 	    } else {
584*0Sstevel@tonic-gate 		dlist_free_items(comps, free_devconfig_object);
585*0Sstevel@tonic-gate 	    }
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate 	} else if (error == 0) {
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	    if (ncomps > 0) {
590*0Sstevel@tonic-gate 		print_insufficient_components_msg(ncomps);
591*0Sstevel@tonic-gate 		dlist_free_items(comps, free_devconfig_object);
592*0Sstevel@tonic-gate 	    } else {
593*0Sstevel@tonic-gate 		print_populate_no_slices_msg();
594*0Sstevel@tonic-gate 	    }
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	}
597*0Sstevel@tonic-gate 	return (error);
598*0Sstevel@tonic-gate }
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate /*
601*0Sstevel@tonic-gate  * FUNCTION:	populate_explicit_stripe(devconfig_t *request,
602*0Sstevel@tonic-gate  *			dlist_t **results)
603*0Sstevel@tonic-gate  *
604*0Sstevel@tonic-gate  * INPUT:	request	- pointer to a request devconfig_t
605*0Sstevel@tonic-gate  *
606*0Sstevel@tonic-gate  * OUTPUT:	results	- pointer to a list of volume devconfig_t results
607*0Sstevel@tonic-gate  *
608*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
609*0Sstevel@tonic-gate  *			 !0 otherwise.
610*0Sstevel@tonic-gate  *
611*0Sstevel@tonic-gate  * PURPOSE:	Processes the input stripe request that specifies explicit
612*0Sstevel@tonic-gate  *		slice components.
613*0Sstevel@tonic-gate  *
614*0Sstevel@tonic-gate  *		The components have already been validated and reserved,
615*0Sstevel@tonic-gate  *		all that is required is to create devconfig_t structs
616*0Sstevel@tonic-gate  *		for each requested slice.
617*0Sstevel@tonic-gate  *
618*0Sstevel@tonic-gate  *		The net size of the stripe is determined by the slice
619*0Sstevel@tonic-gate  *		components.
620*0Sstevel@tonic-gate  *
621*0Sstevel@tonic-gate  *		The stripe devconfig_t is assembled and appended to the
622*0Sstevel@tonic-gate  *		results list.
623*0Sstevel@tonic-gate  *
624*0Sstevel@tonic-gate  *		This function is also called from
625*0Sstevel@tonic-gate  *		    layout_mirror.populate_explicit_mirror()
626*0Sstevel@tonic-gate  */
627*0Sstevel@tonic-gate int
populate_explicit_stripe(devconfig_t * request,dlist_t ** results)628*0Sstevel@tonic-gate populate_explicit_stripe(
629*0Sstevel@tonic-gate 	devconfig_t	*request,
630*0Sstevel@tonic-gate 	dlist_t		**results)
631*0Sstevel@tonic-gate {
632*0Sstevel@tonic-gate 	devconfig_t	*stripe = NULL;
633*0Sstevel@tonic-gate 	int		error = 0;
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	dlist_t		*comps = NULL;
636*0Sstevel@tonic-gate 	dlist_t		*iter = NULL;
637*0Sstevel@tonic-gate 	dlist_t		*item = NULL;
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 	print_layout_explicit_msg(devconfig_type_to_str(TYPE_STRIPE));
640*0Sstevel@tonic-gate 
641*0Sstevel@tonic-gate 	/* assemble components */
642*0Sstevel@tonic-gate 	iter = devconfig_get_components(request);
643*0Sstevel@tonic-gate 	for (; (iter != NULL) && (error == 0); iter = iter->next) {
644*0Sstevel@tonic-gate 	    devconfig_t	*rqst = (devconfig_t *)iter->obj;
645*0Sstevel@tonic-gate 	    dm_descriptor_t rqst_slice = NULL;
646*0Sstevel@tonic-gate 	    char	*rqst_name = NULL;
647*0Sstevel@tonic-gate 	    devconfig_t	*comp = NULL;
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 	    /* slice components have been validated */
650*0Sstevel@tonic-gate 	    /* turn each into a devconfig_t */
651*0Sstevel@tonic-gate 	    ((error = devconfig_get_name(rqst, &rqst_name)) != 0) ||
652*0Sstevel@tonic-gate 	    (error = slice_get_by_name(rqst_name, &rqst_slice)) ||
653*0Sstevel@tonic-gate 	    (error = create_devconfig_for_slice(rqst_slice, &comp));
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	    if (error == 0) {
656*0Sstevel@tonic-gate 
657*0Sstevel@tonic-gate 		print_layout_explicit_added_msg(rqst_name);
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 		item = dlist_new_item((void *)comp);
660*0Sstevel@tonic-gate 		if (item == NULL) {
661*0Sstevel@tonic-gate 		    error = ENOMEM;
662*0Sstevel@tonic-gate 		} else {
663*0Sstevel@tonic-gate 		    comps = dlist_append(item, comps, AT_TAIL);
664*0Sstevel@tonic-gate 		}
665*0Sstevel@tonic-gate 	    }
666*0Sstevel@tonic-gate 	}
667*0Sstevel@tonic-gate 
668*0Sstevel@tonic-gate 	if (error == 0) {
669*0Sstevel@tonic-gate 	    error = assemble_stripe(request, comps, &stripe);
670*0Sstevel@tonic-gate 	}
671*0Sstevel@tonic-gate 
672*0Sstevel@tonic-gate 	if (error == 0) {
673*0Sstevel@tonic-gate 	    if ((item = dlist_new_item(stripe)) == NULL) {
674*0Sstevel@tonic-gate 		error = ENOMEM;
675*0Sstevel@tonic-gate 	    } else {
676*0Sstevel@tonic-gate 		*results = dlist_append(item, *results, AT_TAIL);
677*0Sstevel@tonic-gate 		print_populate_success_msg();
678*0Sstevel@tonic-gate 	    }
679*0Sstevel@tonic-gate 	} else {
680*0Sstevel@tonic-gate 	    dlist_free_items(comps, free_devconfig);
681*0Sstevel@tonic-gate 	}
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 	return (error);
684*0Sstevel@tonic-gate }
685*0Sstevel@tonic-gate 
686*0Sstevel@tonic-gate /*
687*0Sstevel@tonic-gate  * FUNCTION:	compose_stripe(devconfig_t *request, uint64_t nbytes,
688*0Sstevel@tonic-gate  *			dlist_t *disks, uint16_t max, uint16_t min,
689*0Sstevel@tonic-gate  *			dlist_t *othervols, devconfig_t **stripe)
690*0Sstevel@tonic-gate  *
691*0Sstevel@tonic-gate  * INPUT:	request	- pointer to a request devconfig_t
692*0Sstevel@tonic-gate  *		nbytes	- desired stripe size
693*0Sstevel@tonic-gate  *		disks	- pointer to a list of availalb disks
694*0Sstevel@tonic-gate  *		max	- maximum number of components allowed
695*0Sstevel@tonic-gate  *		min	- minimum number of components allowed
696*0Sstevel@tonic-gate  *		othervols - pointer to a list of other volumes whose
697*0Sstevel@tonic-gate  *				composition may affect this stripe
698*0Sstevel@tonic-gate  *				(e.g., submirrors of the same mirror)
699*0Sstevel@tonic-gate  *
700*0Sstevel@tonic-gate  * OUTPUT:	stripe	- pointer to a devconfig_t to hold resulting stripe
701*0Sstevel@tonic-gate  *
702*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
703*0Sstevel@tonic-gate  *			 !0 otherwise.
704*0Sstevel@tonic-gate  *
705*0Sstevel@tonic-gate  * PURPOSE:	Attempt to compose a stripe of capacity nbytes, with
706*0Sstevel@tonic-gate  *		component slices chosen from the input list of disks.
707*0Sstevel@tonic-gate  *		The number of components in the stripe should be in the
708*0Sstevel@tonic-gate  *		range min <= N <= max, more components are preferred.
709*0Sstevel@tonic-gate  *
710*0Sstevel@tonic-gate  *		If a stripe can be composed, a pointer to it will be
711*0Sstevel@tonic-gate  *		returned in the stripe devconfig_t.
712*0Sstevel@tonic-gate  *
713*0Sstevel@tonic-gate  *		This is a loop wrapped around populate_stripe which
714*0Sstevel@tonic-gate  *		varies the number of components between 'max' and 'min'.
715*0Sstevel@tonic-gate  */
716*0Sstevel@tonic-gate static int
compose_stripe(devconfig_t * request,uint64_t nbytes,dlist_t * disks,int max,int min,dlist_t * othervols,devconfig_t ** stripe)717*0Sstevel@tonic-gate compose_stripe(
718*0Sstevel@tonic-gate 	devconfig_t	*request,
719*0Sstevel@tonic-gate 	uint64_t	nbytes,
720*0Sstevel@tonic-gate 	dlist_t		*disks,
721*0Sstevel@tonic-gate 	int		max,
722*0Sstevel@tonic-gate 	int		min,
723*0Sstevel@tonic-gate 	dlist_t		*othervols,
724*0Sstevel@tonic-gate 	devconfig_t	**stripe)
725*0Sstevel@tonic-gate {
726*0Sstevel@tonic-gate 	int		error = 0;
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 	*stripe = NULL;
729*0Sstevel@tonic-gate 
730*0Sstevel@tonic-gate 	for (; (error == 0) && (*stripe == NULL) && (max >= min); max--) {
731*0Sstevel@tonic-gate 	    error = populate_stripe(
732*0Sstevel@tonic-gate 		    request, nbytes, disks, max, othervols, stripe);
733*0Sstevel@tonic-gate 	}
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate 	return (error);
736*0Sstevel@tonic-gate }
737*0Sstevel@tonic-gate 
738*0Sstevel@tonic-gate /*
739*0Sstevel@tonic-gate  * FUNCTION:	compose_stripe_within_hba(devconfig_t *request,
740*0Sstevel@tonic-gate  *			dlist_t *hbas, uint64_t nbytes,
741*0Sstevel@tonic-gate  *			int maxcomp, int mincomp, dlist_t **stripe)
742*0Sstevel@tonic-gate  *
743*0Sstevel@tonic-gate  * INPUT:	request	- pointer to a devconfig_t of the current request
744*0Sstevel@tonic-gate  *		hbas	- pointer to a list of available HBAs
745*0Sstevel@tonic-gate  *		nbytes	- the desired capacity for the stripe
746*0Sstevel@tonic-gate  *		maxcomp - the maximum number of stripe components
747*0Sstevel@tonic-gate  *		mincomp - the minimum number of stripe components
748*0Sstevel@tonic-gate  *
749*0Sstevel@tonic-gate  * OUTPUT:	stripe	- pointer to a stripe devconfig_t result
750*0Sstevel@tonic-gate  *
751*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
752*0Sstevel@tonic-gate  *			 !0 otherwise.
753*0Sstevel@tonic-gate  *
754*0Sstevel@tonic-gate  * PURPOSE:	Layout function which compose a stripe of the desired size
755*0Sstevel@tonic-gate  *		using available disks within any single HBA from the input list.
756*0Sstevel@tonic-gate  *
757*0Sstevel@tonic-gate  *		The number of components within the composed stripe will be
758*0Sstevel@tonic-gate  *		in the range of min to max, preferring more components
759*0Sstevel@tonic-gate  *		over fewer.
760*0Sstevel@tonic-gate  *
761*0Sstevel@tonic-gate  * 		All input HBAs are expected to have at least mincomp
762*0Sstevel@tonic-gate  *		available disks and total space sufficient for the stripe.
763*0Sstevel@tonic-gate  *
764*0Sstevel@tonic-gate  *		If the stripe can be composed, a pointer to it is returned in
765*0Sstevel@tonic-gate  *		the stripe devconfig_t *.
766*0Sstevel@tonic-gate  *
767*0Sstevel@tonic-gate  *
768*0Sstevel@tonic-gate  *		while (more hbas and stripe not composed) {
769*0Sstevel@tonic-gate  *		    select HBA
770*0Sstevel@tonic-gate  *		    if (not enough available space on this HBA) {
771*0Sstevel@tonic-gate  *			continue;
772*0Sstevel@tonic-gate  *		    }
773*0Sstevel@tonic-gate  *		    get available disks for HBA
774*0Sstevel@tonic-gate  *		    use # disks as max # of stripe components
775*0Sstevel@tonic-gate  *		    try to compose stripe
776*0Sstevel@tonic-gate  *		}
777*0Sstevel@tonic-gate  *
778*0Sstevel@tonic-gate  */
779*0Sstevel@tonic-gate static int
compose_stripe_within_hba(devconfig_t * request,dlist_t * hbas,uint64_t nbytes,uint16_t min,uint16_t max,devconfig_t ** stripe)780*0Sstevel@tonic-gate compose_stripe_within_hba(
781*0Sstevel@tonic-gate 	devconfig_t	*request,
782*0Sstevel@tonic-gate 	dlist_t		*hbas,
783*0Sstevel@tonic-gate 	uint64_t	nbytes,
784*0Sstevel@tonic-gate 	uint16_t	min,
785*0Sstevel@tonic-gate 	uint16_t	max,
786*0Sstevel@tonic-gate 	devconfig_t	**stripe)
787*0Sstevel@tonic-gate {
788*0Sstevel@tonic-gate 	int		error = 0;
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	dlist_t		*iter = NULL;
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate 	*stripe = NULL;
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate 	for (iter = hbas;
795*0Sstevel@tonic-gate 	    (iter != NULL) && (error == 0) && (*stripe == NULL);
796*0Sstevel@tonic-gate 	    iter = iter->next) {
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	    dm_descriptor_t hba = (uintptr_t)iter->obj;
799*0Sstevel@tonic-gate 	    dlist_t	*disks = NULL;
800*0Sstevel@tonic-gate 	    uint64_t	space = 0;
801*0Sstevel@tonic-gate 	    uint16_t	ncomp = 0;
802*0Sstevel@tonic-gate 	    char	*name;
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	    ((error = get_display_name(hba, &name)) != 0) ||
805*0Sstevel@tonic-gate 	    (error = hba_get_avail_disks_and_space(request,
806*0Sstevel@tonic-gate 		    hba, &disks, &space));
807*0Sstevel@tonic-gate 
808*0Sstevel@tonic-gate 	    if (error == 0) {
809*0Sstevel@tonic-gate 		if (space >= nbytes) {
810*0Sstevel@tonic-gate 		    ncomp = dlist_length(disks);
811*0Sstevel@tonic-gate 		    ncomp = ((ncomp > max) ? max : ncomp);
812*0Sstevel@tonic-gate 		    error = compose_stripe(
813*0Sstevel@tonic-gate 			    request, nbytes, disks, ncomp,
814*0Sstevel@tonic-gate 			    min, NULL, stripe);
815*0Sstevel@tonic-gate 		} else {
816*0Sstevel@tonic-gate 		    print_hba_insufficient_space_msg(name, space);
817*0Sstevel@tonic-gate 		}
818*0Sstevel@tonic-gate 	    }
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	    dlist_free_items(disks, NULL);
821*0Sstevel@tonic-gate 	}
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate 	return (error);
824*0Sstevel@tonic-gate }
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate /*
827*0Sstevel@tonic-gate  * FUNCTION:	assemble_stripe(devconfig_t *request, dlist_t *comps,
828*0Sstevel@tonic-gate  *			devconfig_t **stripe)
829*0Sstevel@tonic-gate  *
830*0Sstevel@tonic-gate  * INPUT:	request	- pointer to a devconfig_t of the current request
831*0Sstevel@tonic-gate  *		comps	- pointer to a list of slice components
832*0Sstevel@tonic-gate  *
833*0Sstevel@tonic-gate  * OUPUT:	stripe	- pointer to a devconfig_t to hold final stripe
834*0Sstevel@tonic-gate  *
835*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
836*0Sstevel@tonic-gate  *			 !0 otherwise.
837*0Sstevel@tonic-gate  *
838*0Sstevel@tonic-gate  * PURPOSE:	Helper which creates and populates a stripe devconfig_t
839*0Sstevel@tonic-gate  *		struct using information from the input request and the
840*0Sstevel@tonic-gate  *		list of slice components.
841*0Sstevel@tonic-gate  *
842*0Sstevel@tonic-gate  *		Determines the name of the stripe either from the request
843*0Sstevel@tonic-gate  *		or from the default naming scheme.
844*0Sstevel@tonic-gate  *
845*0Sstevel@tonic-gate  *		Sets the interlace for the stripe if a value is specified
846*0Sstevel@tonic-gate  *		in the request.
847*0Sstevel@tonic-gate  *
848*0Sstevel@tonic-gate  *		Attaches the input list of components to the devconfig.
849*0Sstevel@tonic-gate  */
850*0Sstevel@tonic-gate static int
assemble_stripe(devconfig_t * request,dlist_t * comps,devconfig_t ** stripe)851*0Sstevel@tonic-gate assemble_stripe(
852*0Sstevel@tonic-gate 	devconfig_t	*request,
853*0Sstevel@tonic-gate 	dlist_t		*comps,
854*0Sstevel@tonic-gate 	devconfig_t	**stripe)
855*0Sstevel@tonic-gate {
856*0Sstevel@tonic-gate 	uint64_t ilace = 0;
857*0Sstevel@tonic-gate 	char	*name = NULL;
858*0Sstevel@tonic-gate 	int	error = 0;
859*0Sstevel@tonic-gate 
860*0Sstevel@tonic-gate 	if ((error = new_devconfig(stripe, TYPE_STRIPE)) == 0) {
861*0Sstevel@tonic-gate 	    /* set stripe name, use requested name if specified */
862*0Sstevel@tonic-gate 	    if ((error = devconfig_get_name(request, &name)) != 0) {
863*0Sstevel@tonic-gate 		if (error != ERR_ATTR_UNSET) {
864*0Sstevel@tonic-gate 		    volume_set_error(gettext("error getting requested name\n"));
865*0Sstevel@tonic-gate 		} else {
866*0Sstevel@tonic-gate 		    error = 0;
867*0Sstevel@tonic-gate 		}
868*0Sstevel@tonic-gate 	    }
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 	    if (error == 0) {
871*0Sstevel@tonic-gate 		if (name == NULL) {
872*0Sstevel@tonic-gate 		    if ((error = get_next_volume_name(&name,
873*0Sstevel@tonic-gate 			TYPE_STRIPE)) == 0) {
874*0Sstevel@tonic-gate 			error = devconfig_set_name(*stripe, name);
875*0Sstevel@tonic-gate 			free(name);
876*0Sstevel@tonic-gate 		    }
877*0Sstevel@tonic-gate 		} else {
878*0Sstevel@tonic-gate 		    error = devconfig_set_name(*stripe, name);
879*0Sstevel@tonic-gate 		}
880*0Sstevel@tonic-gate 	    }
881*0Sstevel@tonic-gate 	}
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 	if (error == 0) {
884*0Sstevel@tonic-gate 	    if ((error = get_stripe_interlace(request, &ilace)) == 0) {
885*0Sstevel@tonic-gate 		error = devconfig_set_stripe_interlace(*stripe, ilace);
886*0Sstevel@tonic-gate 	    } else if (error == ENOENT) {
887*0Sstevel@tonic-gate 		ilace = get_default_stripe_interlace();
888*0Sstevel@tonic-gate 		error = 0;
889*0Sstevel@tonic-gate 	    }
890*0Sstevel@tonic-gate 	}
891*0Sstevel@tonic-gate 
892*0Sstevel@tonic-gate 	if (error == 0) {
893*0Sstevel@tonic-gate 	    uint64_t	nbytes = 0;
894*0Sstevel@tonic-gate 	    if ((error = compute_usable_stripe_capacity(comps,
895*0Sstevel@tonic-gate 		ilace, &nbytes)) == 0) {
896*0Sstevel@tonic-gate 		error = devconfig_set_size_in_blocks(*stripe, nbytes/DEV_BSIZE);
897*0Sstevel@tonic-gate 	    }
898*0Sstevel@tonic-gate 	}
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 	if (error == 0) {
901*0Sstevel@tonic-gate 	    comps = order_stripe_components_alternate_hbas(comps);
902*0Sstevel@tonic-gate 	    devconfig_set_components(*stripe, comps);
903*0Sstevel@tonic-gate 	} else {
904*0Sstevel@tonic-gate 	    free_devconfig(*stripe);
905*0Sstevel@tonic-gate 	    *stripe = NULL;
906*0Sstevel@tonic-gate 	}
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 	return (error);
909*0Sstevel@tonic-gate }
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate /*
912*0Sstevel@tonic-gate  * Order the given stripe component list such that the number of
913*0Sstevel@tonic-gate  * slices on the same hba adjacent to each other in the list are
914*0Sstevel@tonic-gate  * minimized.
915*0Sstevel@tonic-gate  *
916*0Sstevel@tonic-gate  * @param       comps
917*0Sstevel@tonic-gate  *              the slice component list to order
918*0Sstevel@tonic-gate  *
919*0Sstevel@tonic-gate  * @return      the first element of the resulting list
920*0Sstevel@tonic-gate  */
921*0Sstevel@tonic-gate static dlist_t *
order_stripe_components_alternate_hbas(dlist_t * comps)922*0Sstevel@tonic-gate order_stripe_components_alternate_hbas(
923*0Sstevel@tonic-gate 	dlist_t *comps)
924*0Sstevel@tonic-gate {
925*0Sstevel@tonic-gate 	dlist_t *iter;
926*0Sstevel@tonic-gate 
927*0Sstevel@tonic-gate 	oprintf(OUTPUT_DEBUG,
928*0Sstevel@tonic-gate 	    gettext("Stripe components before ordering to alternate HBAs:\n"));
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 	for (iter = comps; iter != NULL; iter = iter->next) {
931*0Sstevel@tonic-gate 	    devconfig_t *slice = (devconfig_t *)(iter->obj);
932*0Sstevel@tonic-gate 	    char *name;
933*0Sstevel@tonic-gate 	    devconfig_get_name(slice, &name);
934*0Sstevel@tonic-gate 	    oprintf(OUTPUT_DEBUG, "  %s\n", name);
935*0Sstevel@tonic-gate 	}
936*0Sstevel@tonic-gate 
937*0Sstevel@tonic-gate 	return (dlist_separate_similar_elements(
938*0Sstevel@tonic-gate 	    comps, compare_slices_on_same_hba));
939*0Sstevel@tonic-gate }
940*0Sstevel@tonic-gate 
941*0Sstevel@tonic-gate /*
942*0Sstevel@tonic-gate  * FUNCTION:	compute_usable_stripe_capacity(dlist_t *comps, uint64_t ilace,
943*0Sstevel@tonic-gate  *			uint64_t *nbytes)
944*0Sstevel@tonic-gate  *
945*0Sstevel@tonic-gate  * INPUT:	comps	- pointer to a list of stripe components
946*0Sstevel@tonic-gate  *		ilace	- the expected stripe interlace in bytes
947*0Sstevel@tonic-gate  *
948*0Sstevel@tonic-gate  * OUPUT:	nbytes	- pointer to hold the computed capacity
949*0Sstevel@tonic-gate  *
950*0Sstevel@tonic-gate  * RETURNS:	int	- 0 on success
951*0Sstevel@tonic-gate  *			 !0 otherwise.
952*0Sstevel@tonic-gate  *
953*0Sstevel@tonic-gate  * PURPOSE:	Helper which computes the usable size of a stripe taking
954*0Sstevel@tonic-gate  *		into account the interlace and cylinder rounding that
955*0Sstevel@tonic-gate  *		libmeta uses: a stripe component's size is rounded down to
956*0Sstevel@tonic-gate  *		an integral multiple of the interlace and then rounded down
957*0Sstevel@tonic-gate  *		to a cylinder boundary on VTOC labeled disks.
958*0Sstevel@tonic-gate  *
959*0Sstevel@tonic-gate  *		(These libmeta computations are in the meta_stripe_attach()
960*0Sstevel@tonic-gate  *		 function of .../lib/lvm/libmeta/common/meta_stripe.c and
961*0Sstevel@tonic-gate  *		 meta_adjust_geom() in .../lib/lvm/libmeta/common/meta_init.c)
962*0Sstevel@tonic-gate  *
963*0Sstevel@tonic-gate  *		This function's implementation iterates the input list of
964*0Sstevel@tonic-gate  *		stripe component slices and determines the smallest usable
965*0Sstevel@tonic-gate  *		component capacity.
966*0Sstevel@tonic-gate  *
967*0Sstevel@tonic-gate  *		The usable stripe capacity is then that component capacity
968*0Sstevel@tonic-gate  *		times the number of components.
969*0Sstevel@tonic-gate  */
970*0Sstevel@tonic-gate static int
compute_usable_stripe_capacity(dlist_t * comps,uint64_t ilace,uint64_t * nbytes)971*0Sstevel@tonic-gate compute_usable_stripe_capacity(
972*0Sstevel@tonic-gate 	dlist_t		*comps,
973*0Sstevel@tonic-gate 	uint64_t	ilace,
974*0Sstevel@tonic-gate 	uint64_t	*nbytes)
975*0Sstevel@tonic-gate {
976*0Sstevel@tonic-gate 	uint64_t	bytes_per_component = 0;
977*0Sstevel@tonic-gate 	dlist_t		*iter;
978*0Sstevel@tonic-gate 	int		ncomps = 0;
979*0Sstevel@tonic-gate 	int		error = 0;
980*0Sstevel@tonic-gate 
981*0Sstevel@tonic-gate 	for (iter = comps; (iter != NULL) && (error == 0); iter = iter->next) {
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 	    devconfig_t		*comp = (devconfig_t *)iter->obj;
984*0Sstevel@tonic-gate 	    char		*comp_name = NULL;
985*0Sstevel@tonic-gate 	    uint64_t 		comp_nbytes = 0;
986*0Sstevel@tonic-gate 	    dm_descriptor_t	comp_disk;
987*0Sstevel@tonic-gate 	    boolean_t		comp_disk_efi = B_FALSE;
988*0Sstevel@tonic-gate 	    uint64_t 		comp_disk_bps = 0; /* disk bytes per sector */
989*0Sstevel@tonic-gate 
990*0Sstevel@tonic-gate 	    ((error = devconfig_get_size(comp, &comp_nbytes)) != 0) ||
991*0Sstevel@tonic-gate 	    (error = devconfig_get_name(comp, &comp_name)) ||
992*0Sstevel@tonic-gate 	    (error = get_disk_for_named_slice(comp_name, &comp_disk)) ||
993*0Sstevel@tonic-gate 	    (error = disk_get_blocksize(comp_disk, &comp_disk_bps)) ||
994*0Sstevel@tonic-gate 	    (error = disk_get_is_efi(comp_disk, &comp_disk_efi));
995*0Sstevel@tonic-gate 	    if (error == 0) {
996*0Sstevel@tonic-gate 
997*0Sstevel@tonic-gate 		if (comp_disk_efi == B_FALSE) {
998*0Sstevel@tonic-gate 		    uint64_t	nhead = 0;
999*0Sstevel@tonic-gate 		    uint64_t	nsect = 0;
1000*0Sstevel@tonic-gate 		    uint64_t	ncyls = 0;
1001*0Sstevel@tonic-gate 
1002*0Sstevel@tonic-gate 		    /* do cylinder and interlace rounding for non-EFI disks */
1003*0Sstevel@tonic-gate 		    ((error = disk_get_ncylinders(comp_disk, &ncyls)) != 0) ||
1004*0Sstevel@tonic-gate 		    (error = disk_get_nheads(comp_disk, &nhead)) ||
1005*0Sstevel@tonic-gate 		    (error = disk_get_nsectors(comp_disk, &nsect));
1006*0Sstevel@tonic-gate 		    if (error == 0) {
1007*0Sstevel@tonic-gate 			/* compute bytes per cyl */
1008*0Sstevel@tonic-gate 			uint64_t bpc = nhead * nsect * comp_disk_bps;
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 			/* round nbytes down to a multiple of interlace */
1011*0Sstevel@tonic-gate 			comp_nbytes = (comp_nbytes / ilace) * ilace;
1012*0Sstevel@tonic-gate 
1013*0Sstevel@tonic-gate 			/* round nbytes down to a cylinder boundary */
1014*0Sstevel@tonic-gate 			comp_nbytes = (comp_nbytes / bpc) * bpc;
1015*0Sstevel@tonic-gate 		    }
1016*0Sstevel@tonic-gate 		}
1017*0Sstevel@tonic-gate 
1018*0Sstevel@tonic-gate 		/* save smallest component size */
1019*0Sstevel@tonic-gate 		if ((bytes_per_component == 0) ||
1020*0Sstevel@tonic-gate 		    (comp_nbytes < bytes_per_component)) {
1021*0Sstevel@tonic-gate 		    bytes_per_component = comp_nbytes;
1022*0Sstevel@tonic-gate 		}
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate 		++ncomps;
1025*0Sstevel@tonic-gate 	    }
1026*0Sstevel@tonic-gate 	}
1027*0Sstevel@tonic-gate 
1028*0Sstevel@tonic-gate 	if (error == 0) {
1029*0Sstevel@tonic-gate 	    /* size of stripe = smallest component size * n components */
1030*0Sstevel@tonic-gate 	    *nbytes = (bytes_per_component * ncomps);
1031*0Sstevel@tonic-gate 	}
1032*0Sstevel@tonic-gate 
1033*0Sstevel@tonic-gate 	return (error);
1034*0Sstevel@tonic-gate }
1035