17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*b6c8bd52Sjeanm * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include "volume_error.h"
347c478bd9Sstevel@tonic-gate #include "volume_dlist.h"
357c478bd9Sstevel@tonic-gate #include "volume_output.h"
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate #include "layout_device_cache.h"
387c478bd9Sstevel@tonic-gate #include "layout_device_util.h"
397c478bd9Sstevel@tonic-gate #include "layout_discovery.h"
407c478bd9Sstevel@tonic-gate #include "layout_dlist_util.h"
417c478bd9Sstevel@tonic-gate #include "layout_messages.h"
427c478bd9Sstevel@tonic-gate #include "layout_request.h"
437c478bd9Sstevel@tonic-gate #include "layout_slice.h"
447c478bd9Sstevel@tonic-gate #include "layout_svm_util.h"
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #define _LAYOUT_HSP_C
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate static int layout_explicit_hsp(
497c478bd9Sstevel@tonic-gate devconfig_t *hsprequest,
507c478bd9Sstevel@tonic-gate dlist_t *devices,
517c478bd9Sstevel@tonic-gate devconfig_t **hsp);
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate static int layout_default_hsp(
547c478bd9Sstevel@tonic-gate devconfig_t *request,
557c478bd9Sstevel@tonic-gate dlist_t *devices,
567c478bd9Sstevel@tonic-gate devconfig_t **hsp);
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static int populate_hsp(
597c478bd9Sstevel@tonic-gate devconfig_t *request,
607c478bd9Sstevel@tonic-gate devconfig_t *hsp,
617c478bd9Sstevel@tonic-gate dlist_t *devices);
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate static int assemble_hsp(
647c478bd9Sstevel@tonic-gate devconfig_t *hsp,
657c478bd9Sstevel@tonic-gate dlist_t *newspares,
667c478bd9Sstevel@tonic-gate dlist_t *devices);
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate static int get_uniquely_sized_slices(
697c478bd9Sstevel@tonic-gate dlist_t *devices,
707c478bd9Sstevel@tonic-gate dlist_t **unique);
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate static int remove_undersized_slices(
737c478bd9Sstevel@tonic-gate dlist_t *unique,
747c478bd9Sstevel@tonic-gate dlist_t **avail);
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate static int find_spare_for_component(
777c478bd9Sstevel@tonic-gate devconfig_t *component,
787c478bd9Sstevel@tonic-gate dlist_t *all_spares,
797c478bd9Sstevel@tonic-gate dlist_t *hbas,
807c478bd9Sstevel@tonic-gate dlist_t *disks,
817c478bd9Sstevel@tonic-gate boolean_t *found);
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate static int choose_spare_for_component(
847c478bd9Sstevel@tonic-gate devconfig_t *comp,
857c478bd9Sstevel@tonic-gate dlist_t **all_spares,
867c478bd9Sstevel@tonic-gate dlist_t **new_spares,
877c478bd9Sstevel@tonic-gate dlist_t **avail,
887c478bd9Sstevel@tonic-gate dlist_t *used_hbas,
897c478bd9Sstevel@tonic-gate dlist_t *used_disks,
907c478bd9Sstevel@tonic-gate uint16_t npaths);
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * FUNCTION: layout_hsp(devconfig_t *request, devconfig_t hsprequest,
947c478bd9Sstevel@tonic-gate * dlist_t *devices, dlist_t **results)
957c478bd9Sstevel@tonic-gate *
967c478bd9Sstevel@tonic-gate * INPUT: request - pointer to the toplevel request devconfig_t
977c478bd9Sstevel@tonic-gate * hsp - pointer to the optional HSP request devconfig_t
987c478bd9Sstevel@tonic-gate * devices - pointer to a list of devices to be served by the HSP
997c478bd9Sstevel@tonic-gate *
1007c478bd9Sstevel@tonic-gate * OUTPUT: results - pointer to a list result devconfig_t, if the HSP
1017c478bd9Sstevel@tonic-gate * to service the input list of devices needs to be
1027c478bd9Sstevel@tonic-gate * created or modified, it will be appended to the list.
1037c478bd9Sstevel@tonic-gate *
1047c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
1057c478bd9Sstevel@tonic-gate * !0 otherwise.
1067c478bd9Sstevel@tonic-gate *
1077c478bd9Sstevel@tonic-gate * PURPOSE: Main layout driver for HSP, attempts to build/populate a
1087c478bd9Sstevel@tonic-gate * single HSP to service the list of devices.
1097c478bd9Sstevel@tonic-gate *
1107c478bd9Sstevel@tonic-gate * If the input hsprequest is NULL, use the default HSP scheme:
1117c478bd9Sstevel@tonic-gate * a. use the first HSP in the diskset
1127c478bd9Sstevel@tonic-gate * b. create an HSP if the diskset has none
1137c478bd9Sstevel@tonic-gate *
1147c478bd9Sstevel@tonic-gate * If the hsprequest is not NULL:
1157c478bd9Sstevel@tonic-gate * a. if the request names an HSP and it already exists, use it
1167c478bd9Sstevel@tonic-gate * b. if the request names an HSP and it does not exist, create it
1177c478bd9Sstevel@tonic-gate * c. if the request specifies components, use them
1187c478bd9Sstevel@tonic-gate */
1197c478bd9Sstevel@tonic-gate int
layout_hsp(devconfig_t * request,devconfig_t * hsprequest,dlist_t * devices,dlist_t ** results)1207c478bd9Sstevel@tonic-gate layout_hsp(
1217c478bd9Sstevel@tonic-gate devconfig_t *request,
1227c478bd9Sstevel@tonic-gate devconfig_t *hsprequest,
1237c478bd9Sstevel@tonic-gate dlist_t *devices,
1247c478bd9Sstevel@tonic-gate dlist_t **results)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate int error = 0;
1277c478bd9Sstevel@tonic-gate devconfig_t *hsp = NULL;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
1307c478bd9Sstevel@tonic-gate gettext(" ->Layout a %s\n"),
1317c478bd9Sstevel@tonic-gate devconfig_type_to_str(TYPE_HSP));
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate if (hsprequest == NULL) {
1347c478bd9Sstevel@tonic-gate error = layout_default_hsp(request, devices, &hsp);
1357c478bd9Sstevel@tonic-gate } else {
1367c478bd9Sstevel@tonic-gate error = layout_explicit_hsp(hsprequest, devices, &hsp);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate if (error != 0) {
1407c478bd9Sstevel@tonic-gate print_debug_failure_msg(devconfig_type_to_str(TYPE_HSP),
1417c478bd9Sstevel@tonic-gate get_error_string(error));
1427c478bd9Sstevel@tonic-gate } else if (hsp != NULL) {
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate if (devconfig_get_components(hsp) == NULL) {
1457c478bd9Sstevel@tonic-gate /* HSP is usable as it is */
1467c478bd9Sstevel@tonic-gate free_devconfig(hsp);
1477c478bd9Sstevel@tonic-gate hsp = NULL;
1487c478bd9Sstevel@tonic-gate } else {
1497c478bd9Sstevel@tonic-gate dlist_t *item = NULL;
1507c478bd9Sstevel@tonic-gate if ((item = dlist_new_item(hsp)) == NULL) {
1517c478bd9Sstevel@tonic-gate error = ENOMEM;
1527c478bd9Sstevel@tonic-gate } else {
1537c478bd9Sstevel@tonic-gate *results = dlist_append(item, *results, AT_TAIL);
1547c478bd9Sstevel@tonic-gate print_layout_success_msg();
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate return (error);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate * FUNCTION: layout_default_hsp(devconfig_t *request,
1647c478bd9Sstevel@tonic-gate * dlist_t *devices, devconfig_t **hsp)
1657c478bd9Sstevel@tonic-gate *
1667c478bd9Sstevel@tonic-gate * INPUT: request - pointer to the toplevel request devconfig_t
1677c478bd9Sstevel@tonic-gate * devices - pointer to a list of devices to be served by the HSP
1687c478bd9Sstevel@tonic-gate *
1697c478bd9Sstevel@tonic-gate * OUTPUT: hsp - pointer to a devconfig_t to hold the resulting HSP
1707c478bd9Sstevel@tonic-gate *
1717c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
1727c478bd9Sstevel@tonic-gate * !0 otherwise.
1737c478bd9Sstevel@tonic-gate *
1747c478bd9Sstevel@tonic-gate * PURPOSE: Layout driver for default HSP construction.
1757c478bd9Sstevel@tonic-gate *
1767c478bd9Sstevel@tonic-gate * a. use the first HSP in the diskset
1777c478bd9Sstevel@tonic-gate * b. create an HSP if the diskset has none
1787c478bd9Sstevel@tonic-gate * c. add spares to the HSP to service the list of input devices.
1797c478bd9Sstevel@tonic-gate */
1807c478bd9Sstevel@tonic-gate static int
layout_default_hsp(devconfig_t * request,dlist_t * devices,devconfig_t ** hsp)1817c478bd9Sstevel@tonic-gate layout_default_hsp(
1827c478bd9Sstevel@tonic-gate devconfig_t *request,
1837c478bd9Sstevel@tonic-gate dlist_t *devices,
1847c478bd9Sstevel@tonic-gate devconfig_t **hsp)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate char *dsname = get_request_diskset();
1877c478bd9Sstevel@tonic-gate char *hspname = NULL;
1887c478bd9Sstevel@tonic-gate boolean_t free_hspname = B_FALSE;
1897c478bd9Sstevel@tonic-gate devconfig_t *default_hsp = NULL;
1907c478bd9Sstevel@tonic-gate int error = 0;
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
1937c478bd9Sstevel@tonic-gate gettext(" -->Using default HSP scheme...\n"));
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate if ((error = get_default_hsp_name(request, &hspname)) != 0) {
1967c478bd9Sstevel@tonic-gate volume_set_error(
1977c478bd9Sstevel@tonic-gate gettext("error getting HSP name from defaults\n"));
1987c478bd9Sstevel@tonic-gate return (error);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate if (hspname != NULL) {
2027c478bd9Sstevel@tonic-gate if ((error = hsp_get_by_name(dsname, hspname, &default_hsp)) != 0) {
2037c478bd9Sstevel@tonic-gate volume_set_error(
2047c478bd9Sstevel@tonic-gate gettext("error getting default HSP by name\n"));
2057c478bd9Sstevel@tonic-gate return (error);
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate } else {
2087c478bd9Sstevel@tonic-gate /* no default HSP name, get diskset's default HSP */
2097c478bd9Sstevel@tonic-gate if ((error = hsp_get_default_for_diskset(dsname,
2107c478bd9Sstevel@tonic-gate &default_hsp)) != 0) {
2117c478bd9Sstevel@tonic-gate volume_set_error(
2127c478bd9Sstevel@tonic-gate gettext("error getting default HSP\n"));
2137c478bd9Sstevel@tonic-gate return (error);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate if (default_hsp == NULL) {
2177c478bd9Sstevel@tonic-gate /* no default HSP name, no default HSP, make one */
2187c478bd9Sstevel@tonic-gate if ((error = get_next_hsp_name(&hspname)) != 0) {
2197c478bd9Sstevel@tonic-gate volume_set_error(
2207c478bd9Sstevel@tonic-gate gettext("error making default HSP name\n"));
2217c478bd9Sstevel@tonic-gate return (error);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate free_hspname = B_TRUE;
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate if (default_hsp != NULL) {
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate /* Found existing default HSP, copy it */
2307c478bd9Sstevel@tonic-gate dlist_t *spares = devconfig_get_components(default_hsp);
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate ((error = devconfig_get_name(default_hsp, &hspname)) != 0) ||
2337c478bd9Sstevel@tonic-gate (error = new_devconfig(hsp, TYPE_HSP)) ||
2347c478bd9Sstevel@tonic-gate (error = devconfig_set_name(*hsp, hspname));
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate if (error == 0) {
2377c478bd9Sstevel@tonic-gate devconfig_set_components(*hsp, spares);
2387c478bd9Sstevel@tonic-gate devconfig_set_components(default_hsp, NULL);
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
2417c478bd9Sstevel@tonic-gate gettext(" --->Using %s from disk set %s...\n"),
2427c478bd9Sstevel@tonic-gate hspname, dsname);
2437c478bd9Sstevel@tonic-gate } else {
2447c478bd9Sstevel@tonic-gate free_devconfig(*hsp);
2457c478bd9Sstevel@tonic-gate *hsp = NULL;
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate } else {
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate /* no existing default HSP, make it */
2517c478bd9Sstevel@tonic-gate ((error = new_devconfig(hsp, TYPE_HSP)) != 0) ||
2527c478bd9Sstevel@tonic-gate (error = devconfig_set_name(*hsp, hspname));
2537c478bd9Sstevel@tonic-gate if (error == 0) {
2547c478bd9Sstevel@tonic-gate oprintf(OUTPUT_VERBOSE,
2557c478bd9Sstevel@tonic-gate gettext(" --->Created %s for disk set %s...\n "),
2567c478bd9Sstevel@tonic-gate hspname, dsname);
2577c478bd9Sstevel@tonic-gate } else {
2587c478bd9Sstevel@tonic-gate free_devconfig(*hsp);
2597c478bd9Sstevel@tonic-gate *hsp = NULL;
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate if (free_hspname == B_TRUE) {
2637c478bd9Sstevel@tonic-gate free(hspname);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate if (error == 0) {
2687c478bd9Sstevel@tonic-gate error = populate_hsp(request, *hsp, devices);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate return (error);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate * FUNCTION: layout_explicit_hsp(devconfig_t *hsprequest,
2767c478bd9Sstevel@tonic-gate * dlist_t *devices, devconfig_t **hsp)
2777c478bd9Sstevel@tonic-gate *
2787c478bd9Sstevel@tonic-gate * INPUT: hsprequest - pointer to the explicit HSP request devconfig_t
2797c478bd9Sstevel@tonic-gate * devices - pointer to a list of devices to be served by the HSP
2807c478bd9Sstevel@tonic-gate *
2817c478bd9Sstevel@tonic-gate * OUTPUT: hsp - pointer to a HSP devconfig_t to hold resulting HSP
2827c478bd9Sstevel@tonic-gate *
2837c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
2847c478bd9Sstevel@tonic-gate * !0 otherwise.
2857c478bd9Sstevel@tonic-gate *
2867c478bd9Sstevel@tonic-gate * PURPOSE: Layout driver for an explicit HSP request.
2877c478bd9Sstevel@tonic-gate *
2887c478bd9Sstevel@tonic-gate * a. if the request names an HSP and it already exists, use it
2897c478bd9Sstevel@tonic-gate * b. if the request names an HSP and it does not exist, create it
2907c478bd9Sstevel@tonic-gate * c. if the request specifies components, use them
2917c478bd9Sstevel@tonic-gate * otherwise, add new spares to handle the input list
2927c478bd9Sstevel@tonic-gate * of devices.
2937c478bd9Sstevel@tonic-gate */
2947c478bd9Sstevel@tonic-gate static int
layout_explicit_hsp(devconfig_t * hsprequest,dlist_t * devices,devconfig_t ** hsp)2957c478bd9Sstevel@tonic-gate layout_explicit_hsp(
2967c478bd9Sstevel@tonic-gate devconfig_t *hsprequest,
2977c478bd9Sstevel@tonic-gate dlist_t *devices,
2987c478bd9Sstevel@tonic-gate devconfig_t **hsp)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate char *dsname = get_request_diskset();
3017c478bd9Sstevel@tonic-gate char *hspname = NULL;
3027c478bd9Sstevel@tonic-gate dlist_t *rspares = NULL;
3037c478bd9Sstevel@tonic-gate int error = 0;
3047c478bd9Sstevel@tonic-gate
3057c478bd9Sstevel@tonic-gate oprintf(OUTPUT_VERBOSE,
3067c478bd9Sstevel@tonic-gate gettext(" --->Explicit HSP request...\n"));
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate (void) devconfig_get_name(hsprequest, &hspname);
3097c478bd9Sstevel@tonic-gate if (hspname != NULL) {
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate (void) hsp_get_by_name(dsname, hspname, hsp);
3127c478bd9Sstevel@tonic-gate if (*hsp != NULL) {
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate oprintf(OUTPUT_VERBOSE,
3157c478bd9Sstevel@tonic-gate gettext(" --->Using %s...\n"),
3167c478bd9Sstevel@tonic-gate hspname);
3177c478bd9Sstevel@tonic-gate } else {
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate /* named HSP doesn't exist, create it */
3207c478bd9Sstevel@tonic-gate ((error = new_devconfig(hsp, TYPE_HSP)) != 0) ||
3217c478bd9Sstevel@tonic-gate (error = devconfig_set_name(*hsp, hspname));
3227c478bd9Sstevel@tonic-gate if (error == 0) {
3237c478bd9Sstevel@tonic-gate oprintf(OUTPUT_VERBOSE,
3247c478bd9Sstevel@tonic-gate gettext(" --->%s does not exist, "
3257c478bd9Sstevel@tonic-gate "created...\n"), hspname);
3267c478bd9Sstevel@tonic-gate } else {
3277c478bd9Sstevel@tonic-gate free_devconfig(*hsp);
3287c478bd9Sstevel@tonic-gate *hsp = NULL;
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate free(hspname);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate if (error == 0) {
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate /* does the hsprequest specify spares? */
3377c478bd9Sstevel@tonic-gate rspares = devconfig_get_components(hsprequest);
3387c478bd9Sstevel@tonic-gate if (rspares != NULL) {
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate /* put requested spares into HSP */
3417c478bd9Sstevel@tonic-gate dlist_t *list = NULL;
3427c478bd9Sstevel@tonic-gate dlist_t *iter = NULL;
3437c478bd9Sstevel@tonic-gate
3447c478bd9Sstevel@tonic-gate for (iter = rspares;
3457c478bd9Sstevel@tonic-gate (iter != NULL) && (error == 0);
3467c478bd9Sstevel@tonic-gate iter = iter->next) {
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate dlist_t *item = NULL;
3497c478bd9Sstevel@tonic-gate if ((dlist_new_item(iter->obj)) == NULL) {
3507c478bd9Sstevel@tonic-gate error = ENOMEM;
3517c478bd9Sstevel@tonic-gate } else {
3527c478bd9Sstevel@tonic-gate list = dlist_append(item, list, AT_TAIL);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate
3567c478bd9Sstevel@tonic-gate if (error == 0) {
3577c478bd9Sstevel@tonic-gate error = assemble_hsp(*hsp, rspares, devices);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate } else {
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate /* select new spares */
3637c478bd9Sstevel@tonic-gate error = populate_hsp(hsprequest, *hsp, devices);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate return (error);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate * FUNCTION: populate_hsp(devconfig_t *request, devconfig_t *hsp,
3727c478bd9Sstevel@tonic-gate * dlist_t *devices)
3737c478bd9Sstevel@tonic-gate *
3747c478bd9Sstevel@tonic-gate * INPUT: request - pointer to a request devconfig_t
3757c478bd9Sstevel@tonic-gate * hsp - pointer to a HSP devconfig_t
3767c478bd9Sstevel@tonic-gate * devices - pointer to a list of devices to be served by the HSP
3777c478bd9Sstevel@tonic-gate *
3787c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
3797c478bd9Sstevel@tonic-gate * !0 otherwise.
3807c478bd9Sstevel@tonic-gate *
3817c478bd9Sstevel@tonic-gate * PURPOSE: Processes the input HSP request and add spares sufficient
3827c478bd9Sstevel@tonic-gate * to service the input list of devices.
3837c478bd9Sstevel@tonic-gate *
3847c478bd9Sstevel@tonic-gate * Determine the available HBAs, disks, and slices.
3857c478bd9Sstevel@tonic-gate * Sort thru the input list of devices and determine
3867c478bd9Sstevel@tonic-gate * the unique component sizes which need to be spared.
3877c478bd9Sstevel@tonic-gate * Filter the available slices and remove those that are
3887c478bd9Sstevel@tonic-gate * too small to serve as spares.
3897c478bd9Sstevel@tonic-gate *
3907c478bd9Sstevel@tonic-gate * Iterate each device and its components and see if the
3917c478bd9Sstevel@tonic-gate * HSP currently has a sufficient spare, if not, try
3927c478bd9Sstevel@tonic-gate * to select one from the available slices.
3937c478bd9Sstevel@tonic-gate *
3947c478bd9Sstevel@tonic-gate * If a spare cannot be found for any device component,
3957c478bd9Sstevel@tonic-gate * the HSP layout process stops.
3967c478bd9Sstevel@tonic-gate *
3977c478bd9Sstevel@tonic-gate * If spares are found for all device components, add
3987c478bd9Sstevel@tonic-gate * any required new ones to the HSP.
3997c478bd9Sstevel@tonic-gate */
4007c478bd9Sstevel@tonic-gate static int
populate_hsp(devconfig_t * request,devconfig_t * hsp,dlist_t * devices)4017c478bd9Sstevel@tonic-gate populate_hsp(
4027c478bd9Sstevel@tonic-gate devconfig_t *request,
4037c478bd9Sstevel@tonic-gate devconfig_t *hsp,
4047c478bd9Sstevel@tonic-gate dlist_t *devices)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate int error = 0;
4077c478bd9Sstevel@tonic-gate uint16_t npaths = 0;
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate dlist_t *usable_hbas = NULL;
4107c478bd9Sstevel@tonic-gate dlist_t *sel_hbas = NULL;
4117c478bd9Sstevel@tonic-gate dlist_t *disks = NULL;
4127c478bd9Sstevel@tonic-gate dlist_t *iter = NULL;
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate dlist_t *avail = NULL; /* available slices */
4157c478bd9Sstevel@tonic-gate dlist_t *slices = NULL; /* avail slices of sufficient size */
4167c478bd9Sstevel@tonic-gate dlist_t *unique = NULL; /* volume slices that need spares */
4177c478bd9Sstevel@tonic-gate dlist_t *curspares = NULL; /* current spares in the HSP */
4187c478bd9Sstevel@tonic-gate dlist_t *newspares = NULL; /* slices to add to HSP */
4197c478bd9Sstevel@tonic-gate dlist_t *allspares = NULL; /* current and new spares */
4207c478bd9Sstevel@tonic-gate
4217c478bd9Sstevel@tonic-gate ((error = get_usable_hbas(&usable_hbas)) != 0) ||
4227c478bd9Sstevel@tonic-gate (error = select_hbas_with_n_disks(request, usable_hbas, 1, &sel_hbas,
4237c478bd9Sstevel@tonic-gate &disks)) ||
4247c478bd9Sstevel@tonic-gate (error = disks_get_avail_slices(request, disks, &avail)) ||
4257c478bd9Sstevel@tonic-gate (error = get_volume_npaths(request, &npaths));
4267c478bd9Sstevel@tonic-gate if (error != 0) {
4277c478bd9Sstevel@tonic-gate dlist_free_items(sel_hbas, NULL);
4287c478bd9Sstevel@tonic-gate dlist_free_items(disks, NULL);
4297c478bd9Sstevel@tonic-gate dlist_free_items(avail, NULL);
4307c478bd9Sstevel@tonic-gate return (error);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate if (disks == NULL || dlist_length(disks) == 0) {
4347c478bd9Sstevel@tonic-gate /* all disks have been consumed by the devices */
4357c478bd9Sstevel@tonic-gate volume_set_error(
4367c478bd9Sstevel@tonic-gate gettext(" no available disks to populate HSP\n"));
4377c478bd9Sstevel@tonic-gate dlist_free_items(sel_hbas, NULL);
4387c478bd9Sstevel@tonic-gate dlist_free_items(avail, NULL);
4397c478bd9Sstevel@tonic-gate return (-1);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate
4427c478bd9Sstevel@tonic-gate if (avail == NULL || dlist_length(avail) == 0) {
4437c478bd9Sstevel@tonic-gate /* all slices have been consumed by the devices */
4447c478bd9Sstevel@tonic-gate volume_set_error(
4457c478bd9Sstevel@tonic-gate gettext(" no available slices to populate HSP\n"));
4467c478bd9Sstevel@tonic-gate dlist_free_items(sel_hbas, NULL);
4477c478bd9Sstevel@tonic-gate dlist_free_items(disks, NULL);
4487c478bd9Sstevel@tonic-gate return (-1);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate dlist_free_items(sel_hbas, NULL);
4527c478bd9Sstevel@tonic-gate dlist_free_items(disks, NULL);
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate /* build list of slices needing to be spared */
4557c478bd9Sstevel@tonic-gate ((error = get_uniquely_sized_slices(devices, &unique)) != 0) ||
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate /* and list of slices of sufficient size to spare for them */
4587c478bd9Sstevel@tonic-gate (error = remove_undersized_slices(unique, &avail));
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate if (error != 0) {
4617c478bd9Sstevel@tonic-gate dlist_free_items(avail, NULL);
4627c478bd9Sstevel@tonic-gate dlist_free_items(unique, NULL);
4637c478bd9Sstevel@tonic-gate dlist_free_items(slices, NULL);
4647c478bd9Sstevel@tonic-gate return (error);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate /* get spares currently in the HSP */
4687c478bd9Sstevel@tonic-gate curspares = devconfig_get_components(hsp);
4697c478bd9Sstevel@tonic-gate
4707c478bd9Sstevel@tonic-gate /* clone current spares list */
4717c478bd9Sstevel@tonic-gate for (iter = curspares;
4727c478bd9Sstevel@tonic-gate (iter != NULL) && (error == 0);
4737c478bd9Sstevel@tonic-gate iter = iter->next) {
4747c478bd9Sstevel@tonic-gate dlist_t *item = dlist_new_item(iter->obj);
4757c478bd9Sstevel@tonic-gate if (item == NULL) {
4767c478bd9Sstevel@tonic-gate error = ENOMEM;
4777c478bd9Sstevel@tonic-gate } else {
4787c478bd9Sstevel@tonic-gate allspares = dlist_append(item, allspares, AT_TAIL);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate if (error != 0) {
4837c478bd9Sstevel@tonic-gate dlist_free_items(avail, NULL);
4847c478bd9Sstevel@tonic-gate dlist_free_items(unique, NULL);
4857c478bd9Sstevel@tonic-gate dlist_free_items(slices, NULL);
4867c478bd9Sstevel@tonic-gate dlist_free_items(allspares, NULL);
4877c478bd9Sstevel@tonic-gate return (error);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate * examine device component slices and see if the HSP already
4927c478bd9Sstevel@tonic-gate * has a suitable spare. If not, select the best available
4937c478bd9Sstevel@tonic-gate * of the same (or larger) size
4947c478bd9Sstevel@tonic-gate */
4957c478bd9Sstevel@tonic-gate for (iter = devices;
4967c478bd9Sstevel@tonic-gate (iter != NULL) && (error == 0);
4977c478bd9Sstevel@tonic-gate iter = iter->next) {
4987c478bd9Sstevel@tonic-gate
4997c478bd9Sstevel@tonic-gate devconfig_t *device = (devconfig_t *)iter->obj;
5007c478bd9Sstevel@tonic-gate dlist_t *components = devconfig_get_components(device);
5017c478bd9Sstevel@tonic-gate dlist_t *hbas = NULL;
5027c478bd9Sstevel@tonic-gate dlist_t *disks = NULL;
5037c478bd9Sstevel@tonic-gate dlist_t *iter1;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate error = get_hbas_and_disks_used_by_volume(device, &hbas, &disks);
5067c478bd9Sstevel@tonic-gate for (iter1 = components; (iter1 != NULL) && (error == 0);
5077c478bd9Sstevel@tonic-gate iter1 = iter1->next) {
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate devconfig_t *comp = (devconfig_t *)iter1->obj;
5107c478bd9Sstevel@tonic-gate boolean_t found = B_FALSE;
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate if ((error = find_spare_for_component(
5137c478bd9Sstevel@tonic-gate comp, allspares, hbas, disks, &found)) == 0) {
5147c478bd9Sstevel@tonic-gate if (found != B_TRUE) {
5157c478bd9Sstevel@tonic-gate error = choose_spare_for_component(
5167c478bd9Sstevel@tonic-gate comp, &allspares, &newspares,
5177c478bd9Sstevel@tonic-gate &avail, hbas, disks, npaths);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate dlist_free_items(disks, NULL);
5227c478bd9Sstevel@tonic-gate dlist_free_items(hbas, NULL);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate
5257c478bd9Sstevel@tonic-gate if (error == 0) {
5267c478bd9Sstevel@tonic-gate /* existing spares are no longer needed */
5277c478bd9Sstevel@tonic-gate dlist_free_items(curspares, free_devconfig_object);
5287c478bd9Sstevel@tonic-gate curspares = NULL;
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate error = assemble_hsp(hsp, newspares, devices);
5317c478bd9Sstevel@tonic-gate } else {
5327c478bd9Sstevel@tonic-gate dlist_free_items(newspares, free_devconfig_object);
5337c478bd9Sstevel@tonic-gate newspares = NULL;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate dlist_free_items(avail, NULL);
5377c478bd9Sstevel@tonic-gate dlist_free_items(slices, NULL);
5387c478bd9Sstevel@tonic-gate dlist_free_items(unique, NULL);
5397c478bd9Sstevel@tonic-gate dlist_free_items(allspares, NULL);
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate return (error);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate * FUNCTION: assemble_hsp(devconfig_t *hsp, dlist_t *newspares,
5467c478bd9Sstevel@tonic-gate * dlist_t *devices)
5477c478bd9Sstevel@tonic-gate *
5487c478bd9Sstevel@tonic-gate * INPUT: request - pointer to a HSP devconfig_t
5497c478bd9Sstevel@tonic-gate * newspare - pointer to a list of new spares for the HSP
5507c478bd9Sstevel@tonic-gate * devices - pointer to a list of devices to be served by the HSP
5517c478bd9Sstevel@tonic-gate *
5527c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
5537c478bd9Sstevel@tonic-gate * !0 otherwise.
5547c478bd9Sstevel@tonic-gate *
5557c478bd9Sstevel@tonic-gate * PURPOSE: Final assembly of an HSP. Attach new spare components
5567c478bd9Sstevel@tonic-gate * and associate the HSP with each device in the input list.
5577c478bd9Sstevel@tonic-gate */
5587c478bd9Sstevel@tonic-gate static int
assemble_hsp(devconfig_t * hsp,dlist_t * newspares,dlist_t * devices)5597c478bd9Sstevel@tonic-gate assemble_hsp(
5607c478bd9Sstevel@tonic-gate devconfig_t *hsp,
5617c478bd9Sstevel@tonic-gate dlist_t *newspares,
5627c478bd9Sstevel@tonic-gate dlist_t *devices)
5637c478bd9Sstevel@tonic-gate {
5647c478bd9Sstevel@tonic-gate dlist_t *iter;
5657c478bd9Sstevel@tonic-gate char *hspname = NULL;
5667c478bd9Sstevel@tonic-gate int error = 0;
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate /* add new spares to HSP */
5697c478bd9Sstevel@tonic-gate (void) devconfig_set_components(hsp, newspares);
5707c478bd9Sstevel@tonic-gate (void) devconfig_get_name(hsp, &hspname);
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate /* associate HSP with each of the devices */
5737c478bd9Sstevel@tonic-gate for (iter = devices;
5747c478bd9Sstevel@tonic-gate (iter != NULL) && (error == 0);
5757c478bd9Sstevel@tonic-gate iter = iter->next) {
5767c478bd9Sstevel@tonic-gate
5777c478bd9Sstevel@tonic-gate devconfig_t *dev = iter->obj;
5787c478bd9Sstevel@tonic-gate devconfig_t *hspcomp = NULL;
5797c478bd9Sstevel@tonic-gate dlist_t *item = NULL;
5807c478bd9Sstevel@tonic-gate char *devname = NULL;
5817c478bd9Sstevel@tonic-gate
5827c478bd9Sstevel@tonic-gate ((error = devconfig_get_name(dev, &devname)) != 0) ||
5837c478bd9Sstevel@tonic-gate (error = new_devconfig(&hspcomp, TYPE_HSP)) ||
5847c478bd9Sstevel@tonic-gate (error = devconfig_set_name(hspcomp, hspname));
5857c478bd9Sstevel@tonic-gate
5867c478bd9Sstevel@tonic-gate if (error != 0) {
5877c478bd9Sstevel@tonic-gate
5887c478bd9Sstevel@tonic-gate free_devconfig(hspcomp);
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate } else if ((item = dlist_new_item(hspcomp)) == NULL) {
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate free_devconfig(hspcomp);
5937c478bd9Sstevel@tonic-gate error = ENOMEM;
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate } else {
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate dlist_t *comps = devconfig_get_components(dev);
5987c478bd9Sstevel@tonic-gate comps = dlist_append(comps, item, AT_TAIL);
5997c478bd9Sstevel@tonic-gate (void) devconfig_set_components(dev, comps);
6007c478bd9Sstevel@tonic-gate
6017c478bd9Sstevel@tonic-gate oprintf(OUTPUT_VERBOSE,
6027c478bd9Sstevel@tonic-gate gettext(" --->volume %s will use HSP %s\n"),
6037c478bd9Sstevel@tonic-gate devname, hspname);
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate
6077c478bd9Sstevel@tonic-gate return (error);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate * FUNCTION: get_uniquely_sized_slices(dlist_t *devices,
6127c478bd9Sstevel@tonic-gate * dlist_t **unique)
6137c478bd9Sstevel@tonic-gate *
6147c478bd9Sstevel@tonic-gate * INPUT: devices - pointer to a list of devconfig_t devices
6157c478bd9Sstevel@tonic-gate *
6167c478bd9Sstevel@tonic-gate * OUTPUT: unique - pointer to a list of uniquely size slices
6177c478bd9Sstevel@tonic-gate * from the input list of devices.
6187c478bd9Sstevel@tonic-gate *
6197c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
6207c478bd9Sstevel@tonic-gate * !0 otherwise.
6217c478bd9Sstevel@tonic-gate *
6227c478bd9Sstevel@tonic-gate * PURPOSE: Examine each device's slice components and build a list
6237c478bd9Sstevel@tonic-gate * of uniquely sized slices.
6247c478bd9Sstevel@tonic-gate */
6257c478bd9Sstevel@tonic-gate static int
get_uniquely_sized_slices(dlist_t * devices,dlist_t ** unique)6267c478bd9Sstevel@tonic-gate get_uniquely_sized_slices(
6277c478bd9Sstevel@tonic-gate dlist_t *devices,
6287c478bd9Sstevel@tonic-gate dlist_t **unique)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate int error = 0;
6317c478bd9Sstevel@tonic-gate dlist_t *iter = NULL;
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate for (iter = devices;
6347c478bd9Sstevel@tonic-gate (iter != NULL) && (error == 0);
6357c478bd9Sstevel@tonic-gate iter = iter->next) {
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate dlist_t *iter1;
6387c478bd9Sstevel@tonic-gate for (iter1 = devconfig_get_components((devconfig_t *)iter->obj);
6397c478bd9Sstevel@tonic-gate (iter1 != NULL) && (error == 0);
6407c478bd9Sstevel@tonic-gate iter1 = iter1->next) {
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate devconfig_t *comp = (devconfig_t *)iter1->obj;
6437c478bd9Sstevel@tonic-gate if (dlist_contains(*unique, comp,
6447c478bd9Sstevel@tonic-gate compare_devconfig_sizes) != B_TRUE) {
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate dlist_t *item = NULL;
6477c478bd9Sstevel@tonic-gate if ((item = dlist_new_item(comp)) == NULL) {
6487c478bd9Sstevel@tonic-gate error = ENOMEM;
6497c478bd9Sstevel@tonic-gate } else {
6507c478bd9Sstevel@tonic-gate *unique = dlist_insert_ordered(item, *unique,
6517c478bd9Sstevel@tonic-gate ASCENDING, compare_devconfig_sizes);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate
6577c478bd9Sstevel@tonic-gate return (error);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate * FUNCTION: remove_undersized_slices(dlist_t *unique,
6627c478bd9Sstevel@tonic-gate * dlist_t **avail)
6637c478bd9Sstevel@tonic-gate *
6647c478bd9Sstevel@tonic-gate * INPUT: avail - pointer to a list of available slices
6657c478bd9Sstevel@tonic-gate * unique - pointer to a list of uniquely size slices
6667c478bd9Sstevel@tonic-gate *
6677c478bd9Sstevel@tonic-gate * OUTPUT: avail - pointer to an updated list of available slices
6687c478bd9Sstevel@tonic-gate * that are at least as large as slices in the
6697c478bd9Sstevel@tonic-gate * unique list.
6707c478bd9Sstevel@tonic-gate *
6717c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
6727c478bd9Sstevel@tonic-gate * !0 otherwise.
6737c478bd9Sstevel@tonic-gate *
6747c478bd9Sstevel@tonic-gate * PURPOSE: filter available slices and remove those that aren't
6757c478bd9Sstevel@tonic-gate * large enough for the device components which need spares.
6767c478bd9Sstevel@tonic-gate *
6777c478bd9Sstevel@tonic-gate * For each uniquely sized slice, find all available slices
6787c478bd9Sstevel@tonic-gate * that are larger and add them to the filtered list.
6797c478bd9Sstevel@tonic-gate */
6807c478bd9Sstevel@tonic-gate static int
remove_undersized_slices(dlist_t * unique,dlist_t ** avail)6817c478bd9Sstevel@tonic-gate remove_undersized_slices(
6827c478bd9Sstevel@tonic-gate dlist_t *unique,
6837c478bd9Sstevel@tonic-gate dlist_t **avail)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate dlist_t *filtered = NULL;
6867c478bd9Sstevel@tonic-gate dlist_t *iter = NULL;
6877c478bd9Sstevel@tonic-gate int error = 0;
6887c478bd9Sstevel@tonic-gate
6897c478bd9Sstevel@tonic-gate for (iter = unique;
6907c478bd9Sstevel@tonic-gate (iter != NULL) && (error == 0);
6917c478bd9Sstevel@tonic-gate iter = iter->next) {
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate devconfig_t *uslice = (devconfig_t *)iter->obj;
6947c478bd9Sstevel@tonic-gate uint64_t usize = 0;
6957c478bd9Sstevel@tonic-gate dlist_t *iter2 = NULL;
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate error = devconfig_get_size(uslice, &usize);
6987c478bd9Sstevel@tonic-gate
6997c478bd9Sstevel@tonic-gate for (iter2 = *avail;
7007c478bd9Sstevel@tonic-gate (iter2 != NULL) && (error == 0);
7017c478bd9Sstevel@tonic-gate iter2 = iter2->next) {
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate dm_descriptor_t aslice = (uintptr_t)iter2->obj;
7047c478bd9Sstevel@tonic-gate uint64_t asize = 0;
7057c478bd9Sstevel@tonic-gate
7067c478bd9Sstevel@tonic-gate error = slice_get_size(aslice, &asize);
7077c478bd9Sstevel@tonic-gate if (asize >= usize) {
7087c478bd9Sstevel@tonic-gate
7097c478bd9Sstevel@tonic-gate /* this slice is large enough */
7107c478bd9Sstevel@tonic-gate dlist_t *item = NULL;
711*b6c8bd52Sjeanm if ((item = dlist_new_item((void *)(uintptr_t)aslice)) ==
712*b6c8bd52Sjeanm NULL) {
7137c478bd9Sstevel@tonic-gate error = ENOMEM;
7147c478bd9Sstevel@tonic-gate } else {
7157c478bd9Sstevel@tonic-gate filtered = dlist_insert_ordered(item, filtered,
7167c478bd9Sstevel@tonic-gate ASCENDING, compare_slice_sizes);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate
7237c478bd9Sstevel@tonic-gate if (error == 0) {
7247c478bd9Sstevel@tonic-gate dlist_free_items(*avail, NULL);
7257c478bd9Sstevel@tonic-gate *avail = filtered;
7267c478bd9Sstevel@tonic-gate } else {
7277c478bd9Sstevel@tonic-gate dlist_free_items(filtered, NULL);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate
7307c478bd9Sstevel@tonic-gate return (error);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate * FUNCTION: find_spare_for_component(devconfig_t *component,
7357c478bd9Sstevel@tonic-gate * dlist_t *all_spares, dlist_t *hbas, dlist_t *disks,
7367c478bd9Sstevel@tonic-gate * boolean_t *found)
7377c478bd9Sstevel@tonic-gate *
7387c478bd9Sstevel@tonic-gate * INPUT: comp - pointer to a devconfig_t slice compenent that
7397c478bd9Sstevel@tonic-gate * needs to be spared
7407c478bd9Sstevel@tonic-gate * all_spares - pointer to a list of spares currently
7417c478bd9Sstevel@tonic-gate * in the pool or that will be added
7427c478bd9Sstevel@tonic-gate * hbas - pointer to a list of HBAs the component's
7437c478bd9Sstevel@tonic-gate * parent device utilizes
7447c478bd9Sstevel@tonic-gate * disks - pointer to a list of disks the component's
7457c478bd9Sstevel@tonic-gate * parent device utilizes
7467c478bd9Sstevel@tonic-gate *
7477c478bd9Sstevel@tonic-gate * OUTPUT: found - pointer to a boolean_t to hold the result.
7487c478bd9Sstevel@tonic-gate *
7497c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
7507c478bd9Sstevel@tonic-gate * !0 otherwise.
7517c478bd9Sstevel@tonic-gate *
7527c478bd9Sstevel@tonic-gate * PURPOSE: Find a spare for the input component.
7537c478bd9Sstevel@tonic-gate *
7547c478bd9Sstevel@tonic-gate * Searches the input list of spares to see if one is
7557c478bd9Sstevel@tonic-gate * sufficient.
7567c478bd9Sstevel@tonic-gate *
7577c478bd9Sstevel@tonic-gate * A suffcient spare is one that is large enough to spare
7587c478bd9Sstevel@tonic-gate * for the input component and not on the same disk as any
7597c478bd9Sstevel@tonic-gate * of the components in the parent device.
7607c478bd9Sstevel@tonic-gate *
7617c478bd9Sstevel@tonic-gate * The optimal spare would be on a different controller/HBA
7627c478bd9Sstevel@tonic-gate * as the component and any of the components in the parent
7637c478bd9Sstevel@tonic-gate * device. We settle for sufficient.
7647c478bd9Sstevel@tonic-gate */
7657c478bd9Sstevel@tonic-gate static int
find_spare_for_component(devconfig_t * component,dlist_t * all_spares,dlist_t * hbas,dlist_t * disks,boolean_t * found)7667c478bd9Sstevel@tonic-gate find_spare_for_component(
7677c478bd9Sstevel@tonic-gate devconfig_t *component,
7687c478bd9Sstevel@tonic-gate dlist_t *all_spares,
7697c478bd9Sstevel@tonic-gate dlist_t *hbas,
7707c478bd9Sstevel@tonic-gate dlist_t *disks,
7717c478bd9Sstevel@tonic-gate boolean_t *found)
7727c478bd9Sstevel@tonic-gate {
7737c478bd9Sstevel@tonic-gate dlist_t *iter = NULL;
7747c478bd9Sstevel@tonic-gate uint64_t csize = 0;
7757c478bd9Sstevel@tonic-gate int error = 0;
7767c478bd9Sstevel@tonic-gate
7777c478bd9Sstevel@tonic-gate *found = B_FALSE;
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate (void) devconfig_get_size(component, &csize);
7807c478bd9Sstevel@tonic-gate
7817c478bd9Sstevel@tonic-gate for (iter = all_spares;
7827c478bd9Sstevel@tonic-gate (iter != NULL) && (*found == B_FALSE) && (error == 0);
7837c478bd9Sstevel@tonic-gate iter = iter->next) {
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate devconfig_t *spare = (devconfig_t *)iter->obj;
7867c478bd9Sstevel@tonic-gate char *spname = NULL;
7877c478bd9Sstevel@tonic-gate uint64_t spsize = 0;
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate if (((error = devconfig_get_name(spare, &spname)) != 0) ||
7907c478bd9Sstevel@tonic-gate ((error = devconfig_get_size(spare, &spsize)) != 0)) {
7917c478bd9Sstevel@tonic-gate continue;
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate
7947c478bd9Sstevel@tonic-gate if (spsize >= csize) {
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate dm_descriptor_t disk = NULL;
7977c478bd9Sstevel@tonic-gate
7987c478bd9Sstevel@tonic-gate /* see if spare's disk is independent of the volume */
7997c478bd9Sstevel@tonic-gate error = get_disk_for_named_slice(spname, &disk);
800*b6c8bd52Sjeanm if ((error == 0) && (dlist_contains(disks,
801*b6c8bd52Sjeanm (void *)(uintptr_t)disk, compare_descriptor_names) ==
802*b6c8bd52Sjeanm B_FALSE)) {
8037c478bd9Sstevel@tonic-gate *found = B_TRUE;
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate
8087c478bd9Sstevel@tonic-gate if ((*found == B_TRUE) && (get_max_verbosity() >= OUTPUT_DEBUG)) {
8097c478bd9Sstevel@tonic-gate char *cname = NULL;
8107c478bd9Sstevel@tonic-gate (void) devconfig_get_name(component, &cname);
8117c478bd9Sstevel@tonic-gate oprintf(OUTPUT_DEBUG,
8127c478bd9Sstevel@tonic-gate gettext(" found existing spare for: %s (%llu)\n"),
8137c478bd9Sstevel@tonic-gate cname, csize);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate
8167c478bd9Sstevel@tonic-gate return (error);
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate
8197c478bd9Sstevel@tonic-gate /*
8207c478bd9Sstevel@tonic-gate * FUNCTION: choose_spare_for_component(devconfig_t *component,
8217c478bd9Sstevel@tonic-gate * dlist_t *all_spares, dlist_t **new_spares,
8227c478bd9Sstevel@tonic-gate * dlist_t avail, uint16_t npaths, dlist_t *used_hbas,
8237c478bd9Sstevel@tonic-gate * dlist_t *used_disks)
8247c478bd9Sstevel@tonic-gate *
8257c478bd9Sstevel@tonic-gate * INPUT: comp - pointer to a devconfig_t slice compenent that
8267c478bd9Sstevel@tonic-gate * needs to be spared
8277c478bd9Sstevel@tonic-gate * all_spares - pointer to a list of spares currently
8287c478bd9Sstevel@tonic-gate * in the pool and those to be added
8297c478bd9Sstevel@tonic-gate * new_spares - pointer to a list of spares that need to
8307c478bd9Sstevel@tonic-gate * be added to the pool
8317c478bd9Sstevel@tonic-gate * avail - list of available slices
8327c478bd9Sstevel@tonic-gate * npaths - required number of paths for the spare
8337c478bd9Sstevel@tonic-gate * used_hbas - list of HBAs used by the component's parent
8347c478bd9Sstevel@tonic-gate * used_disks - list of disks used by the component's parent
8357c478bd9Sstevel@tonic-gate *
8367c478bd9Sstevel@tonic-gate * OUTPUT: all_spares - the possibly updated list of all spares
8377c478bd9Sstevel@tonic-gate * new_spares - the possibly updated list of spares which
8387c478bd9Sstevel@tonic-gate * need to be added to the pool.
8397c478bd9Sstevel@tonic-gate *
8407c478bd9Sstevel@tonic-gate * RETURNS: int - 0 on success
8417c478bd9Sstevel@tonic-gate * !0 otherwise.
8427c478bd9Sstevel@tonic-gate *
8437c478bd9Sstevel@tonic-gate * PURPOSE: Find a new spare for the input component.
8447c478bd9Sstevel@tonic-gate *
8457c478bd9Sstevel@tonic-gate * Select a spare from the available slice list and add
8467c478bd9Sstevel@tonic-gate * it to the new_spares list.
8477c478bd9Sstevel@tonic-gate *
8487c478bd9Sstevel@tonic-gate * The spare slice chosen should be on a unique HBA and
8497c478bd9Sstevel@tonic-gate * disk relative to the input lists of used HBAs and disks
8507c478bd9Sstevel@tonic-gate * and any spares in the pool.
8517c478bd9Sstevel@tonic-gate */
8527c478bd9Sstevel@tonic-gate static int
choose_spare_for_component(devconfig_t * component,dlist_t ** all_spares,dlist_t ** new_spares,dlist_t ** avail,dlist_t * used_hbas,dlist_t * used_disks,uint16_t npaths)8537c478bd9Sstevel@tonic-gate choose_spare_for_component(
8547c478bd9Sstevel@tonic-gate devconfig_t *component,
8557c478bd9Sstevel@tonic-gate dlist_t **all_spares,
8567c478bd9Sstevel@tonic-gate dlist_t **new_spares,
8577c478bd9Sstevel@tonic-gate dlist_t **avail,
8587c478bd9Sstevel@tonic-gate dlist_t *used_hbas,
8597c478bd9Sstevel@tonic-gate dlist_t *used_disks,
8607c478bd9Sstevel@tonic-gate uint16_t npaths)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate devconfig_t *spare = NULL;
8637c478bd9Sstevel@tonic-gate uint64_t csize = 0;
8647c478bd9Sstevel@tonic-gate int error = 0;
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate (void) devconfig_get_size(component, &csize);
8677c478bd9Sstevel@tonic-gate
8687c478bd9Sstevel@tonic-gate if (get_max_verbosity() >= OUTPUT_DEBUG) {
8697c478bd9Sstevel@tonic-gate char *cname = NULL;
8707c478bd9Sstevel@tonic-gate (void) devconfig_get_name(component, &cname);
8717c478bd9Sstevel@tonic-gate oprintf(OUTPUT_DEBUG,
8727c478bd9Sstevel@tonic-gate gettext(" select new spare for: %s (%llu)\n"),
8737c478bd9Sstevel@tonic-gate cname, csize);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate * find a spare for the input component.
8787c478bd9Sstevel@tonic-gate * select the best one from the available list that
8797c478bd9Sstevel@tonic-gate * is on a unique disk.
8807c478bd9Sstevel@tonic-gate */
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate /*
8837c478bd9Sstevel@tonic-gate * 1st B_TRUE: require a different disk than those used by
8847c478bd9Sstevel@tonic-gate * all spares and devices
8857c478bd9Sstevel@tonic-gate * 2nd B_TRUE: requested size is the minimum acceptable
8867c478bd9Sstevel@tonic-gate * 1st B_FALSE: do not add an extra cylinder when resizing slice,
8877c478bd9Sstevel@tonic-gate * this is only necessary for Stripe components whose
8887c478bd9Sstevel@tonic-gate * sizes get rounded down to an interlace multiple and
8897c478bd9Sstevel@tonic-gate * then down to a cylinder boundary.
8907c478bd9Sstevel@tonic-gate */
8917c478bd9Sstevel@tonic-gate error = choose_slice(csize, npaths, *avail, *all_spares,
8927c478bd9Sstevel@tonic-gate used_hbas, used_disks, B_TRUE, B_TRUE, B_FALSE, &spare);
8937c478bd9Sstevel@tonic-gate
8947c478bd9Sstevel@tonic-gate if ((error == 0) && (spare == NULL)) {
8957c478bd9Sstevel@tonic-gate /* can't find one on a unique disk, try again on any disk */
8967c478bd9Sstevel@tonic-gate
8977c478bd9Sstevel@tonic-gate /* BEGIN CSTYLED */
8987c478bd9Sstevel@tonic-gate /*
8997c478bd9Sstevel@tonic-gate * 1st B_FALSE: don't require a different disk than those used
9007c478bd9Sstevel@tonic-gate * by all spares and devices
9017c478bd9Sstevel@tonic-gate * 2nd B_TRUE: requested size is still the minimum acceptable
9027c478bd9Sstevel@tonic-gate * 2nd B_FALSE: do not add an extra cylinder when resizing slice
9037c478bd9Sstevel@tonic-gate * this is only necessary for Stripe components whose
9047c478bd9Sstevel@tonic-gate * sizes get rounded down to an interlace multiple and
9057c478bd9Sstevel@tonic-gate * then down to a cylinder boundary.
9067c478bd9Sstevel@tonic-gate */
9077c478bd9Sstevel@tonic-gate /* END CSTYLED */
9087c478bd9Sstevel@tonic-gate error = choose_slice(
9097c478bd9Sstevel@tonic-gate csize, npaths, *avail, *all_spares, used_hbas,
9107c478bd9Sstevel@tonic-gate used_disks, B_FALSE, B_TRUE, B_FALSE, &spare);
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate
9137c478bd9Sstevel@tonic-gate if ((error == 0) && (spare != NULL)) {
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate dlist_t *rmvd = NULL;
9167c478bd9Sstevel@tonic-gate dlist_t *item = NULL;
9177c478bd9Sstevel@tonic-gate char *spname = NULL;
9187c478bd9Sstevel@tonic-gate
9197c478bd9Sstevel@tonic-gate if ((item = dlist_new_item(spare)) == NULL) {
9207c478bd9Sstevel@tonic-gate error = ENOMEM;
9217c478bd9Sstevel@tonic-gate } else {
9227c478bd9Sstevel@tonic-gate
9237c478bd9Sstevel@tonic-gate /* add spare to the all spares list */
9247c478bd9Sstevel@tonic-gate *all_spares = dlist_append(item, *all_spares, AT_HEAD);
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate if ((item = dlist_new_item(spare)) == NULL) {
9277c478bd9Sstevel@tonic-gate error = ENOMEM;
9287c478bd9Sstevel@tonic-gate } else {
9297c478bd9Sstevel@tonic-gate
9307c478bd9Sstevel@tonic-gate /* add spare to the new spares list */
9317c478bd9Sstevel@tonic-gate *new_spares = dlist_insert_ordered(
9327c478bd9Sstevel@tonic-gate item, *new_spares, ASCENDING,
9337c478bd9Sstevel@tonic-gate compare_devconfig_sizes);
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate /* remove it from the available list */
9367c478bd9Sstevel@tonic-gate *avail = dlist_remove_equivalent_item(*avail, spare,
9377c478bd9Sstevel@tonic-gate compare_devconfig_and_descriptor_names,
9387c478bd9Sstevel@tonic-gate &rmvd);
9397c478bd9Sstevel@tonic-gate
9407c478bd9Sstevel@tonic-gate if (rmvd != NULL) {
9417c478bd9Sstevel@tonic-gate free(rmvd);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate
9447c478bd9Sstevel@tonic-gate /* add the spare to the used slice list */
9457c478bd9Sstevel@tonic-gate error = devconfig_get_name(spare, &spname);
9467c478bd9Sstevel@tonic-gate if (error == 0) {
9477c478bd9Sstevel@tonic-gate error = add_used_slice_by_name(spname);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate
9527c478bd9Sstevel@tonic-gate } else {
9537c478bd9Sstevel@tonic-gate
9547c478bd9Sstevel@tonic-gate /* no spare, give up on layout */
9557c478bd9Sstevel@tonic-gate oprintf(OUTPUT_TERSE,
9567c478bd9Sstevel@tonic-gate gettext(" <---Failed: insufficient suitable spares\n"));
9577c478bd9Sstevel@tonic-gate
9587c478bd9Sstevel@tonic-gate volume_set_error(
9597c478bd9Sstevel@tonic-gate gettext("failed to find sufficient spares for HSP\n"));
9607c478bd9Sstevel@tonic-gate
9617c478bd9Sstevel@tonic-gate error = -1;
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate
9647c478bd9Sstevel@tonic-gate return (error);
9657c478bd9Sstevel@tonic-gate }
966