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
5*0209230bSgjelinek  * Common Development and Distribution License (the "License").
6*0209230bSgjelinek  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*0209230bSgjelinek  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <limits.h>
277c478bd9Sstevel@tonic-gate #include <pool.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <synch.h>
327c478bd9Sstevel@tonic-gate #include <thread.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/loadavg.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "dict.h"
397c478bd9Sstevel@tonic-gate #include "pool_internal.h"
407c478bd9Sstevel@tonic-gate #include "pool_impl.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * Atom structure, used to reference count string atoms.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate typedef struct {
467c478bd9Sstevel@tonic-gate 	char *a_string;				/* String atom */
477c478bd9Sstevel@tonic-gate 	uint_t a_count;				/* String reference count */
487c478bd9Sstevel@tonic-gate } atom_t;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * The _internal_lock is used to lock the state of libpool during
527c478bd9Sstevel@tonic-gate  * internal initialisation operations.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate mutex_t		_internal_lock;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int _libpool_debug = 0;			/* debugging messages */
577c478bd9Sstevel@tonic-gate static dict_hdl_t *_pv_atoms;			/* pool_value_t atoms */
587c478bd9Sstevel@tonic-gate static mutex_t _atom_lock;			/* atom table lock */
597c478bd9Sstevel@tonic-gate static int _libpool_internal_initialised = PO_FALSE;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Various useful constant strings which are often encountered
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate const char *c_a_dtype = "a-dtype";
657c478bd9Sstevel@tonic-gate const char *c_name = "name";
667c478bd9Sstevel@tonic-gate const char *c_type = "type";
677c478bd9Sstevel@tonic-gate const char *c_ref_id = "ref_id";
687c478bd9Sstevel@tonic-gate const char *c_max_prop = "max";
697c478bd9Sstevel@tonic-gate const char *c_min_prop = "min";
707c478bd9Sstevel@tonic-gate const char *c_size_prop = "size";
717c478bd9Sstevel@tonic-gate const char *c_sys_prop = "sys_id";
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * prop_is_type() checks the supplied property and returns PO_TRUE if the
757c478bd9Sstevel@tonic-gate  * property value is set for the property else PO_FALSE
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate static int prop_is_type(int, const pool_prop_t *);
787c478bd9Sstevel@tonic-gate static int resource_get_common(const pool_resource_t *, const char *,
797c478bd9Sstevel@tonic-gate     uint64_t *);
807c478bd9Sstevel@tonic-gate static int64_t elem_get_expected_int64(const pool_elem_t *, const char *);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * The following returns a malloc'ed string which must be free'd by the
847c478bd9Sstevel@tonic-gate  * caller.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate static char *elem_get_expected_string(const pool_elem_t *, const char *);
877c478bd9Sstevel@tonic-gate static int element_props_init(pool_prop_t *);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Each element class/sub-class has a set of properties and behaviours
917c478bd9Sstevel@tonic-gate  * which are used to create the element with appropriate property
927c478bd9Sstevel@tonic-gate  * values and to ensure correct property manipulations. The details
937c478bd9Sstevel@tonic-gate  * are all stored in the following arrays.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static int elem_name_init(pool_prop_t *);
977c478bd9Sstevel@tonic-gate static int elem_comment_init(pool_prop_t *);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static int pool_importance_init(pool_prop_t *);
1007c478bd9Sstevel@tonic-gate static int pool_active_init(pool_prop_t *);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static int res_max_init(pool_prop_t *);
1037c478bd9Sstevel@tonic-gate static int res_min_init(pool_prop_t *);
1047c478bd9Sstevel@tonic-gate static int res_size_init(pool_prop_t *);
1057c478bd9Sstevel@tonic-gate static int res_load_init(pool_prop_t *);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int pset_units_init(pool_prop_t *);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static int cpu_status_init(pool_prop_t *);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static int elem_no_set(pool_elem_t *, const pool_value_t *);
1127c478bd9Sstevel@tonic-gate static int elem_set_name(pool_elem_t *, const pool_value_t *);
1137c478bd9Sstevel@tonic-gate static int elem_get_type(const pool_elem_t *, pool_value_t *);
1147c478bd9Sstevel@tonic-gate static int elem_set_string(pool_elem_t *, const pool_value_t *);
1157c478bd9Sstevel@tonic-gate static int elem_set_bool(pool_elem_t *, const pool_value_t *);
1167c478bd9Sstevel@tonic-gate static int elem_set_uint(pool_elem_t *, const pool_value_t *);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static int system_set_allocate(pool_elem_t *, const pool_value_t *);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static int pool_set_scheduler(pool_elem_t *, const pool_value_t *);
1217c478bd9Sstevel@tonic-gate static int pool_set_active(pool_elem_t *, const pool_value_t *);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static int res_set_max(pool_elem_t *, const pool_value_t *);
1247c478bd9Sstevel@tonic-gate static int res_set_min(pool_elem_t *, const pool_value_t *);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static int cpu_set_status(pool_elem_t *, const pool_value_t *);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static const char *pool_elem_class_name[] = {
1297c478bd9Sstevel@tonic-gate 	"invalid",
1307c478bd9Sstevel@tonic-gate 	"system",
1317c478bd9Sstevel@tonic-gate 	"pool",
1327c478bd9Sstevel@tonic-gate 	"component resource",
1337c478bd9Sstevel@tonic-gate 	"aggregate resource",
1347c478bd9Sstevel@tonic-gate 	"component"
1357c478bd9Sstevel@tonic-gate };
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * This must be kept in sync with the pool_resource_elem_ctl array and
1397c478bd9Sstevel@tonic-gate  * the "enum pool_resource_elem_class" type.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate static const char *pool_resource_elem_class_name[] = {
1427c478bd9Sstevel@tonic-gate 	"invalid",
1437c478bd9Sstevel@tonic-gate 	"pset"
1447c478bd9Sstevel@tonic-gate };
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static const char *pool_component_elem_class_name[] = {
1477c478bd9Sstevel@tonic-gate 	"invalid",
1487c478bd9Sstevel@tonic-gate 	"cpu"
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static pool_prop_t system_props[] = {
1527c478bd9Sstevel@tonic-gate 	{ "system.name", POOL_VALUE_INITIALIZER, PP_STORED, NULL,
1537c478bd9Sstevel@tonic-gate 	    { NULL, elem_set_name } },
1547c478bd9Sstevel@tonic-gate 	{ "system.ref_id", POOL_VALUE_INITIALIZER,
1557c478bd9Sstevel@tonic-gate 	    PP_HIDDEN | PP_STORED | PP_READ, NULL, { NULL, elem_no_set } },
1567c478bd9Sstevel@tonic-gate 	{ "system.comment", POOL_VALUE_INITIALIZER, PP_STORED, NULL, NULL },
1577c478bd9Sstevel@tonic-gate 	{ "system.version", POOL_VALUE_INITIALIZER,
1587c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_READ, NULL, NULL },
1597c478bd9Sstevel@tonic-gate 	{ "system.bind-default", POOL_VALUE_INITIALIZER,
1607c478bd9Sstevel@tonic-gate 	    PP_STORED, NULL, NULL },
1617c478bd9Sstevel@tonic-gate 	{ "system.allocate-method", POOL_VALUE_INITIALIZER,
1627c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_OPTIONAL, NULL, { NULL, system_set_allocate } },
1637c478bd9Sstevel@tonic-gate 	{ "system.poold.log-level", POOL_VALUE_INITIALIZER,
1647c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_OPTIONAL, NULL, { NULL, elem_set_string } },
1657c478bd9Sstevel@tonic-gate 	{ "system.poold.log-location", POOL_VALUE_INITIALIZER,
1667c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_OPTIONAL, NULL, { NULL, elem_set_string } },
1677c478bd9Sstevel@tonic-gate 	{ "system.poold.monitor-interval", POOL_VALUE_INITIALIZER,
1687c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_OPTIONAL, NULL, { NULL, elem_set_uint } },
1697c478bd9Sstevel@tonic-gate 	{ "system.poold.history-file", POOL_VALUE_INITIALIZER,
1707c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_OPTIONAL, NULL, { NULL, elem_set_string } },
1717c478bd9Sstevel@tonic-gate 	{ "system.poold.objectives", POOL_VALUE_INITIALIZER,
1727c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_OPTIONAL, NULL, { NULL, elem_set_string } },
1737c478bd9Sstevel@tonic-gate 	NULL
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static pool_prop_t pool_props[] = {
1777c478bd9Sstevel@tonic-gate 	{ "pool.sys_id", POOL_VALUE_INITIALIZER,
1787c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_READ, NULL, NULL },
1797c478bd9Sstevel@tonic-gate 	{ "pool.name", POOL_VALUE_INITIALIZER,
1807c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_INIT, elem_name_init, { NULL, elem_set_name } },
1817c478bd9Sstevel@tonic-gate 	{ "pool.res", POOL_VALUE_INITIALIZER,
1827c478bd9Sstevel@tonic-gate 	    PP_HIDDEN | PP_STORED | PP_READ, NULL, { NULL, elem_no_set } },
1837c478bd9Sstevel@tonic-gate 	{ "pool.ref_id", POOL_VALUE_INITIALIZER,
1847c478bd9Sstevel@tonic-gate 	    PP_HIDDEN | PP_STORED | PP_READ, NULL, { NULL, elem_no_set } },
1857c478bd9Sstevel@tonic-gate 	{ "pool.active", POOL_VALUE_INITIALIZER, PP_STORED | PP_INIT,
1867c478bd9Sstevel@tonic-gate 	    pool_active_init, { NULL, pool_set_active } },
1877c478bd9Sstevel@tonic-gate 	{ "pool.default", POOL_VALUE_INITIALIZER,
1887c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_READ, NULL, NULL },
1897c478bd9Sstevel@tonic-gate 	{ "pool.scheduler", POOL_VALUE_INITIALIZER,
1907c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_OPTIONAL, NULL,
1917c478bd9Sstevel@tonic-gate 	    { NULL, pool_set_scheduler } },
1927c478bd9Sstevel@tonic-gate 	{ "pool.importance", POOL_VALUE_INITIALIZER, PP_STORED | PP_INIT,
1937c478bd9Sstevel@tonic-gate 	    pool_importance_init, NULL },
1947c478bd9Sstevel@tonic-gate 	{ "pool.comment", POOL_VALUE_INITIALIZER, PP_STORED | PP_INIT,
1957c478bd9Sstevel@tonic-gate 	    elem_comment_init, NULL },
1967c478bd9Sstevel@tonic-gate 	NULL
1977c478bd9Sstevel@tonic-gate };
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static pool_prop_t pset_props[] = {
2007c478bd9Sstevel@tonic-gate 	{ "type", POOL_VALUE_INITIALIZER, PP_HIDDEN | PP_STORED | PP_READ, NULL,
2017c478bd9Sstevel@tonic-gate 	    { elem_get_type, NULL }  },
2027c478bd9Sstevel@tonic-gate 	{ "pset.sys_id", POOL_VALUE_INITIALIZER,
2037c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_READ, NULL, NULL },
2047c478bd9Sstevel@tonic-gate 	{ "pset.name", POOL_VALUE_INITIALIZER,
2057c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_INIT, elem_name_init, { NULL, elem_set_name } },
2067c478bd9Sstevel@tonic-gate 	{ "pset.ref_id", POOL_VALUE_INITIALIZER,
2077c478bd9Sstevel@tonic-gate 	    PP_HIDDEN | PP_STORED | PP_READ, NULL, { NULL, elem_no_set } },
2087c478bd9Sstevel@tonic-gate 	{ "pset.default", POOL_VALUE_INITIALIZER,
2097c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_READ, NULL, NULL },
2107c478bd9Sstevel@tonic-gate 	{ "pset.min", POOL_VALUE_INITIALIZER, PP_STORED | PP_INIT, res_min_init,
2117c478bd9Sstevel@tonic-gate 	    { NULL, res_set_min } },
2127c478bd9Sstevel@tonic-gate 	{ "pset.max", POOL_VALUE_INITIALIZER, PP_STORED | PP_INIT, res_max_init,
2137c478bd9Sstevel@tonic-gate 	    { NULL, res_set_max } },
2147c478bd9Sstevel@tonic-gate 	{ "pset.units", POOL_VALUE_INITIALIZER,
2157c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_INIT, pset_units_init, NULL },
2167c478bd9Sstevel@tonic-gate 	{ "pset.load", POOL_VALUE_INITIALIZER, PP_READ | PP_INIT,
2177c478bd9Sstevel@tonic-gate 	    res_load_init, NULL },
2187c478bd9Sstevel@tonic-gate 	{ "pset.size", POOL_VALUE_INITIALIZER, PP_STORED | PP_INIT | PP_READ,
2197c478bd9Sstevel@tonic-gate 	    res_size_init, NULL },
2207c478bd9Sstevel@tonic-gate 	{ "pset.comment", POOL_VALUE_INITIALIZER, PP_STORED | PP_INIT,
2217c478bd9Sstevel@tonic-gate 	    elem_comment_init, NULL },
2227c478bd9Sstevel@tonic-gate 	{ "pset.poold.objectives", POOL_VALUE_INITIALIZER,
2237c478bd9Sstevel@tonic-gate 	    PP_STORED | PP_OPTIONAL, NULL, { NULL, elem_set_string } },
2247c478bd9Sstevel@tonic-gate 	NULL
2257c478bd9Sstevel@tonic-gate };
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static pool_prop_t cpu_props[] = {
2287c478bd9Sstevel@tonic-gate 	{ "type", POOL_VALUE_INITIALIZER, PP_HIDDEN | PP_STORED | PP_READ, NULL,
2297c478bd9Sstevel@tonic-gate 	    { elem_get_type, NULL }  },
2307c478bd9Sstevel@tonic-gate 	{ "cpu.sys_id", POOL_VALUE_INITIALIZER, PP_STORED | PP_READ, NULL,
2317c478bd9Sstevel@tonic-gate 	    NULL },
2327c478bd9Sstevel@tonic-gate 	{ "cpu.ref_id", POOL_VALUE_INITIALIZER,
2337c478bd9Sstevel@tonic-gate 	    PP_HIDDEN | PP_STORED | PP_READ, NULL, { NULL, elem_no_set } },
2347c478bd9Sstevel@tonic-gate 	{ "cpu.comment", POOL_VALUE_INITIALIZER, PP_STORED | PP_INIT,
2357c478bd9Sstevel@tonic-gate 	    elem_comment_init, NULL },
2367c478bd9Sstevel@tonic-gate 	{ "cpu.status", POOL_VALUE_INITIALIZER, PP_INIT, cpu_status_init,
2377c478bd9Sstevel@tonic-gate 	    { NULL, cpu_set_status } },
2387c478bd9Sstevel@tonic-gate 	{ "cpu.pinned", POOL_VALUE_INITIALIZER, PP_STORED | PP_OPTIONAL, NULL,
2397c478bd9Sstevel@tonic-gate 	    { NULL, elem_set_bool } },
2407c478bd9Sstevel@tonic-gate 	NULL
2417c478bd9Sstevel@tonic-gate };
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static pool_prop_t *pool_elem_ctl[] = {
2447c478bd9Sstevel@tonic-gate 	NULL,
2457c478bd9Sstevel@tonic-gate 	system_props,
2467c478bd9Sstevel@tonic-gate 	pool_props,
2477c478bd9Sstevel@tonic-gate 	NULL,
2487c478bd9Sstevel@tonic-gate 	NULL,
2497c478bd9Sstevel@tonic-gate 	NULL
2507c478bd9Sstevel@tonic-gate };
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * This must be kept in sync with the pool_resource_elem_class_name array and
2547c478bd9Sstevel@tonic-gate  * the "enum pool_resource_elem_class" type.
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate static pool_prop_t *pool_resource_elem_ctl[] = {
2577c478bd9Sstevel@tonic-gate 	NULL,
2587c478bd9Sstevel@tonic-gate 	pset_props
2597c478bd9Sstevel@tonic-gate };
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static pool_prop_t *pool_component_elem_ctl[] = {
2627c478bd9Sstevel@tonic-gate 	NULL,
2637c478bd9Sstevel@tonic-gate 	cpu_props
2647c478bd9Sstevel@tonic-gate };
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static void
atom_init(void)2677c478bd9Sstevel@tonic-gate atom_init(void)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&_atom_lock);
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * Initialize pool_value_t atom dictionary
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 	if (_pv_atoms == NULL)
2747c478bd9Sstevel@tonic-gate 		if ((_pv_atoms = dict_new((int (*)(const void *, const void *))
2757c478bd9Sstevel@tonic-gate 		    strcmp, (uint64_t (*)(const void *))hash_str)) == NULL)
2767c478bd9Sstevel@tonic-gate 			abort();
2777c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&_atom_lock);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * Initializer, called when the library is initialized.
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate void
internal_init(void)2847c478bd9Sstevel@tonic-gate internal_init(void)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&_internal_lock);
2877c478bd9Sstevel@tonic-gate 	if (_libpool_internal_initialised == PO_TRUE) {
2887c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&_internal_lock);
2897c478bd9Sstevel@tonic-gate 		return;
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	atom_init();
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Initialize all available property arrays.
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 	if (element_props_init(system_props) == PO_FAIL)
2967c478bd9Sstevel@tonic-gate 		abort();
2977c478bd9Sstevel@tonic-gate 	if (element_props_init(pool_props) == PO_FAIL)
2987c478bd9Sstevel@tonic-gate 		abort();
2997c478bd9Sstevel@tonic-gate 	if (element_props_init(pset_props) == PO_FAIL)
3007c478bd9Sstevel@tonic-gate 		abort();
3017c478bd9Sstevel@tonic-gate 	if (element_props_init(cpu_props) == PO_FAIL)
3027c478bd9Sstevel@tonic-gate 		abort();
3037c478bd9Sstevel@tonic-gate 	_libpool_internal_initialised = PO_TRUE;
3047c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&_internal_lock);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate static int
element_props_init(pool_prop_t * props)3097c478bd9Sstevel@tonic-gate element_props_init(pool_prop_t *props)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	int i;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	for (i = 0; props[i].pp_pname != NULL; i++) {
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * Initialise each of the properties
3167c478bd9Sstevel@tonic-gate 		 */
3177c478bd9Sstevel@tonic-gate 		if (pool_value_set_name(&props[i].pp_value,
3187c478bd9Sstevel@tonic-gate 		    props[i].pp_pname) != PO_SUCCESS) {
3197c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 		if (props[i].pp_init &&
3227c478bd9Sstevel@tonic-gate 		    props[i].pp_init(&props[i]) != PO_SUCCESS) {
3237c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * These functions intialise the properties of this plugin. The only reason
3327c478bd9Sstevel@tonic-gate  * they exist is because the ability to perform the static initialisation of
3337c478bd9Sstevel@tonic-gate  * union members properly was only introduced in the C99 standard. i.e. if you
3347c478bd9Sstevel@tonic-gate  * could do {.f = 1.0} like in the proposed C99 standard then that would
3357c478bd9Sstevel@tonic-gate  * be the preferred way to do this as it keeps the data in the array and
3367c478bd9Sstevel@tonic-gate  * minimises the scope for errors. However, until that time these functions
3377c478bd9Sstevel@tonic-gate  * are the best way to minimise the scope for errors and to maximise
3387c478bd9Sstevel@tonic-gate  * maintainability.
3397c478bd9Sstevel@tonic-gate  *
3407c478bd9Sstevel@tonic-gate  * There is one function for each property, and the initial value for each
3417c478bd9Sstevel@tonic-gate  * property is hard-coded into each function.
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate static int
elem_name_init(pool_prop_t * prop)3457c478bd9Sstevel@tonic-gate elem_name_init(pool_prop_t *prop)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	return (string_init(prop, "default"));
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate static int
elem_comment_init(pool_prop_t * prop)3517c478bd9Sstevel@tonic-gate elem_comment_init(pool_prop_t *prop)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	return (string_init(prop, ""));
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate static int
pool_importance_init(pool_prop_t * prop)3577c478bd9Sstevel@tonic-gate pool_importance_init(pool_prop_t *prop)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	return (int_init(prop, 1));
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate static int
pool_active_init(pool_prop_t * prop)3637c478bd9Sstevel@tonic-gate pool_active_init(pool_prop_t *prop)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	return (bool_init(prop, PO_TRUE));
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate static int
res_max_init(pool_prop_t * prop)3697c478bd9Sstevel@tonic-gate res_max_init(pool_prop_t *prop)
3707c478bd9Sstevel@tonic-gate {
3717c478bd9Sstevel@tonic-gate 	return (uint_init(prop, 0));
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static int
res_min_init(pool_prop_t * prop)3757c478bd9Sstevel@tonic-gate res_min_init(pool_prop_t *prop)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	return (uint_init(prop, 0));
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate static int
res_size_init(pool_prop_t * prop)3817c478bd9Sstevel@tonic-gate res_size_init(pool_prop_t *prop)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	return (uint_init(prop, 0));
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate static int
res_load_init(pool_prop_t * prop)3877c478bd9Sstevel@tonic-gate res_load_init(pool_prop_t *prop)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate 	return (uint_init(prop, 0));
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate static int
pset_units_init(pool_prop_t * prop)3937c478bd9Sstevel@tonic-gate pset_units_init(pool_prop_t *prop)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate 	return (string_init(prop, "population"));
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate static int
cpu_status_init(pool_prop_t * prop)3997c478bd9Sstevel@tonic-gate cpu_status_init(pool_prop_t *prop)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	return (string_init(prop, PS_ONLINE));
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * Individual property manipulation routines for use by the generic
4067c478bd9Sstevel@tonic-gate  * get/put property routines
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * Many properties cannot be modified. This function prevents property
4117c478bd9Sstevel@tonic-gate  * modification.
4127c478bd9Sstevel@tonic-gate  */
4137c478bd9Sstevel@tonic-gate /* ARGSUSED */
4147c478bd9Sstevel@tonic-gate static int
elem_no_set(pool_elem_t * elem,const pool_value_t * pval)4157c478bd9Sstevel@tonic-gate elem_no_set(pool_elem_t *elem, const pool_value_t *pval)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	return (PO_FAIL);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * Duplicate names for a pool or resource type are illegal.
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate static int
elem_set_name(pool_elem_t * elem,const pool_value_t * pval)4247c478bd9Sstevel@tonic-gate elem_set_name(pool_elem_t *elem, const pool_value_t *pval)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	const char *nm;
4277c478bd9Sstevel@tonic-gate 	pool_t *pool;
4287c478bd9Sstevel@tonic-gate 	pool_resource_t *res;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	if (pool_value_get_string(pval, &nm) != PO_SUCCESS) {
4317c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	if (!is_valid_name(nm)) {
4347c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
4357c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 	switch (pool_elem_class(elem)) {
4387c478bd9Sstevel@tonic-gate 	case PEC_SYSTEM:
4397c478bd9Sstevel@tonic-gate 		break;
4407c478bd9Sstevel@tonic-gate 	case PEC_POOL:
4417c478bd9Sstevel@tonic-gate 		pool = pool_get_pool(TO_CONF(elem), nm);
4427c478bd9Sstevel@tonic-gate 		if (pool != NULL && pool != pool_elem_pool(elem)) {
4437c478bd9Sstevel@tonic-gate 			pool_seterror(POE_PUTPROP);
4447c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 		break;
4477c478bd9Sstevel@tonic-gate 	case PEC_RES_COMP:
4487c478bd9Sstevel@tonic-gate 	case PEC_RES_AGG:
4497c478bd9Sstevel@tonic-gate 		res = pool_get_resource(TO_CONF(elem),
4507c478bd9Sstevel@tonic-gate 		    pool_elem_class_string(elem), nm);
4517c478bd9Sstevel@tonic-gate 		if (res != NULL && res != pool_elem_res(elem)) {
4527c478bd9Sstevel@tonic-gate 			pool_seterror(POE_PUTPROP);
4537c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 		break;
4567c478bd9Sstevel@tonic-gate 	default:
4577c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
4607c478bd9Sstevel@tonic-gate }
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate  * Ensure the type is a string.
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate /* ARGSUSED */
4667c478bd9Sstevel@tonic-gate static int
elem_set_string(pool_elem_t * elem,const pool_value_t * pval)4677c478bd9Sstevel@tonic-gate elem_set_string(pool_elem_t *elem, const pool_value_t *pval)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	if (pool_value_get_type(pval) == POC_STRING)
4707c478bd9Sstevel@tonic-gate 		return (PO_SUCCESS);
4717c478bd9Sstevel@tonic-gate 	else {
4727c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
4737c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * Ensure the type is a boolean.
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate /* ARGSUSED */
4817c478bd9Sstevel@tonic-gate static int
elem_set_bool(pool_elem_t * elem,const pool_value_t * pval)4827c478bd9Sstevel@tonic-gate elem_set_bool(pool_elem_t *elem, const pool_value_t *pval)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	if (pool_value_get_type(pval) == POC_BOOL)
4857c478bd9Sstevel@tonic-gate 		return (PO_SUCCESS);
4867c478bd9Sstevel@tonic-gate 	else {
4877c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
4887c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  * Ensure the type is an unsigned int.
4947c478bd9Sstevel@tonic-gate  */
4957c478bd9Sstevel@tonic-gate /* ARGSUSED */
4967c478bd9Sstevel@tonic-gate static int
elem_set_uint(pool_elem_t * elem,const pool_value_t * pval)4977c478bd9Sstevel@tonic-gate elem_set_uint(pool_elem_t *elem, const pool_value_t *pval)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	if (pool_value_get_type(pval) == POC_UINT)
5007c478bd9Sstevel@tonic-gate 		return (PO_SUCCESS);
5017c478bd9Sstevel@tonic-gate 	else {
5027c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
5037c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate /* ARGSUSED */
5087c478bd9Sstevel@tonic-gate int
system_set_allocate(pool_elem_t * elem,const pool_value_t * pval)5097c478bd9Sstevel@tonic-gate system_set_allocate(pool_elem_t *elem, const pool_value_t *pval)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	const char *sval;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (pool_value_get_string(pval, &sval) != PO_SUCCESS) {
5147c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
5157c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if (strcmp(POA_IMPORTANCE, sval) != 0 &&
5187c478bd9Sstevel@tonic-gate 	    strcmp(POA_SURPLUS_TO_DEFAULT, sval) != 0) {
5197c478bd9Sstevel@tonic-gate 			pool_seterror(POE_PUTPROP);
5207c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate /* ARGSUSED */
5267c478bd9Sstevel@tonic-gate int
pool_set_active(pool_elem_t * elem,const pool_value_t * pval)5277c478bd9Sstevel@tonic-gate pool_set_active(pool_elem_t *elem, const pool_value_t *pval)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	uchar_t bval;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (pool_value_get_type(pval) != POC_BOOL) {
5327c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
5337c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	(void) pool_value_get_bool(pval, &bval);
5367c478bd9Sstevel@tonic-gate 	if (bval != 1) {
5377c478bd9Sstevel@tonic-gate 		/*
5387c478bd9Sstevel@tonic-gate 		 * "active" must be true on pools for
5397c478bd9Sstevel@tonic-gate 		 * now.
5407c478bd9Sstevel@tonic-gate 		 */
5417c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
5427c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate /* ARGSUSED */
5487c478bd9Sstevel@tonic-gate int
pool_set_scheduler(pool_elem_t * elem,const pool_value_t * pval)5497c478bd9Sstevel@tonic-gate pool_set_scheduler(pool_elem_t *elem, const pool_value_t *pval)
5507c478bd9Sstevel@tonic-gate {
5517c478bd9Sstevel@tonic-gate 	pcinfo_t pcinfo;
5527c478bd9Sstevel@tonic-gate 	const char *sched;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (pool_value_get_string(pval, &sched) != 0) {
5557c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
5567c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	(void) strncpy(pcinfo.pc_clname, sched, PC_CLNMSZ);
5597c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, &pcinfo) == -1) {
5607c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
5617c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate static int
res_set_max(pool_elem_t * elem,const pool_value_t * pval)5677c478bd9Sstevel@tonic-gate res_set_max(pool_elem_t *elem, const pool_value_t *pval)
5687c478bd9Sstevel@tonic-gate {
5697c478bd9Sstevel@tonic-gate 	uint64_t min, max;
5707c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * max must be a uint
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 	if (pool_value_get_uint64(pval, &max) != PO_SUCCESS) {
5767c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
5777c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * max can't be less than min (if it exists)
5817c478bd9Sstevel@tonic-gate 	 */
5827c478bd9Sstevel@tonic-gate 	if (pool_get_ns_property(elem, c_min_prop, &val) == POC_INVAL)
5837c478bd9Sstevel@tonic-gate 		return (PO_SUCCESS);
5847c478bd9Sstevel@tonic-gate 	if (pool_value_get_uint64(&val, &min) != PO_SUCCESS) {
5857c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
5867c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	if (max < min) {
5897c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
5907c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * Ensure that changes to the max in a dynamic configuration
5947c478bd9Sstevel@tonic-gate 	 * are still valid.
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	if (conf_is_dynamic(TO_CONF(elem)) == PO_TRUE) {
5977c478bd9Sstevel@tonic-gate 		uint64_t oldmax;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		if (pool_get_ns_property(elem, c_max_prop, &val) == POC_INVAL) {
6007c478bd9Sstevel@tonic-gate 			pool_seterror(POE_PUTPROP);
6017c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 		if (pool_value_get_uint64(&val, &oldmax) != PO_SUCCESS) {
6047c478bd9Sstevel@tonic-gate 			pool_seterror(POE_PUTPROP);
6057c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		if (max < oldmax) {
6087c478bd9Sstevel@tonic-gate 			/*
6097c478bd9Sstevel@tonic-gate 			 * Ensure that the modified total max is >= size
6107c478bd9Sstevel@tonic-gate 			 * of all resources of this type
6117c478bd9Sstevel@tonic-gate 			 */
6127c478bd9Sstevel@tonic-gate 			return (pool_validate_resource(TO_CONF(elem),
6137c478bd9Sstevel@tonic-gate 				pool_elem_class_string(elem), c_max_prop,
6147c478bd9Sstevel@tonic-gate 				max - oldmax));
6157c478bd9Sstevel@tonic-gate 		}
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
6187c478bd9Sstevel@tonic-gate }
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate static int
res_set_min(pool_elem_t * elem,const pool_value_t * pval)6217c478bd9Sstevel@tonic-gate res_set_min(pool_elem_t *elem, const pool_value_t *pval)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	uint64_t min, max;
6247c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/*
6277c478bd9Sstevel@tonic-gate 	 * min must be a uint
6287c478bd9Sstevel@tonic-gate 	 */
6297c478bd9Sstevel@tonic-gate 	if (pool_value_get_uint64(pval, &min) != PO_SUCCESS) {
6307c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
6317c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 	/*
6347c478bd9Sstevel@tonic-gate 	 * min can't be more than max (if it exists)
6357c478bd9Sstevel@tonic-gate 	 */
6367c478bd9Sstevel@tonic-gate 	if (pool_get_ns_property(elem, c_max_prop, &val) == POC_INVAL)
6377c478bd9Sstevel@tonic-gate 		return (PO_SUCCESS);
6387c478bd9Sstevel@tonic-gate 	if (pool_value_get_uint64(&val, &max) != PO_SUCCESS) {
6397c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
6407c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 	if (min > max) {
6437c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
6447c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	switch (pool_resource_elem_class(elem)) {
6487c478bd9Sstevel@tonic-gate 	case PREC_PSET:
6497c478bd9Sstevel@tonic-gate 		if (resource_is_default(pool_elem_res(elem))) {
6507c478bd9Sstevel@tonic-gate 			if (min < 1) {
6517c478bd9Sstevel@tonic-gate 				pool_seterror(POE_PUTPROP);
6527c478bd9Sstevel@tonic-gate 				return (PO_FAIL);
6537c478bd9Sstevel@tonic-gate 			}
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 		break;
6567c478bd9Sstevel@tonic-gate 	default:
6577c478bd9Sstevel@tonic-gate 		break;
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * Ensure that changes to the min in a dynamic configuration
6627c478bd9Sstevel@tonic-gate 	 * are still valid.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	if (conf_is_dynamic(TO_CONF(elem)) == PO_TRUE) {
6657c478bd9Sstevel@tonic-gate 		uint64_t oldmin;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		if (pool_get_ns_property(elem, c_min_prop, &val) == POC_INVAL) {
6687c478bd9Sstevel@tonic-gate 			pool_seterror(POE_PUTPROP);
6697c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 		if (pool_value_get_uint64(&val, &oldmin) != PO_SUCCESS) {
6727c478bd9Sstevel@tonic-gate 			pool_seterror(POE_PUTPROP);
6737c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 		if (min > oldmin) {
6767c478bd9Sstevel@tonic-gate 			/*
6777c478bd9Sstevel@tonic-gate 			 * Ensure that the modified total min is <= size
6787c478bd9Sstevel@tonic-gate 			 * of all resources of this type
6797c478bd9Sstevel@tonic-gate 			 */
6807c478bd9Sstevel@tonic-gate 			return (pool_validate_resource(TO_CONF(elem),
6817c478bd9Sstevel@tonic-gate 				pool_elem_class_string(elem), c_min_prop,
6827c478bd9Sstevel@tonic-gate 				min - oldmin));
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /* ARGSUSED */
6897c478bd9Sstevel@tonic-gate int
cpu_set_status(pool_elem_t * elem,const pool_value_t * pval)6907c478bd9Sstevel@tonic-gate cpu_set_status(pool_elem_t *elem, const pool_value_t *pval)
6917c478bd9Sstevel@tonic-gate {
6927c478bd9Sstevel@tonic-gate 	const char *status;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if (pool_value_get_string(pval, &status) != 0) {
6957c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
6967c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (strcmp(PS_ONLINE, status) != 0 &&
7007c478bd9Sstevel@tonic-gate 	    strcmp(PS_OFFLINE, status) != 0 &&
7017c478bd9Sstevel@tonic-gate 	    strcmp(PS_NOINTR, status) != 0 &&
7027c478bd9Sstevel@tonic-gate 	    strcmp(PS_SPARE, status) != 0 &&
7037c478bd9Sstevel@tonic-gate 	    strcmp(PS_FAULTED, status) != 0) {
7047c478bd9Sstevel@tonic-gate 		pool_seterror(POE_PUTPROP);
7057c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate static int
elem_get_type(const pool_elem_t * elem,pool_value_t * pval)7117c478bd9Sstevel@tonic-gate elem_get_type(const pool_elem_t *elem, pool_value_t *pval)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	if (pool_value_set_string(pval, pool_elem_class_string(elem)) ==
7147c478bd9Sstevel@tonic-gate 	    PO_FAIL)
7157c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
7167c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate  * More general utilities
7217c478bd9Sstevel@tonic-gate  */
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate  * Is the supplied configuration the dynamic configuration
7247c478bd9Sstevel@tonic-gate  * Return: PO_TRUE/PO_FALSE
7257c478bd9Sstevel@tonic-gate  */
7267c478bd9Sstevel@tonic-gate int
conf_is_dynamic(const pool_conf_t * conf)7277c478bd9Sstevel@tonic-gate conf_is_dynamic(const pool_conf_t *conf)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	if (strcmp(pool_conf_location(conf), pool_dynamic_location()) == 0)
7307c478bd9Sstevel@tonic-gate 		return (PO_TRUE);
7317c478bd9Sstevel@tonic-gate 	return (PO_FALSE);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate /*
7357c478bd9Sstevel@tonic-gate  * uint_init() initialises the value of the supplied property with the
7367c478bd9Sstevel@tonic-gate  * supplied value.
7377c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS
7387c478bd9Sstevel@tonic-gate  */
7397c478bd9Sstevel@tonic-gate int
uint_init(pool_prop_t * prop,uint64_t val)7407c478bd9Sstevel@tonic-gate uint_init(pool_prop_t *prop, uint64_t val)
7417c478bd9Sstevel@tonic-gate {
7427c478bd9Sstevel@tonic-gate 	pool_value_set_uint64(&prop->pp_value, val);
7437c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate /*
7477c478bd9Sstevel@tonic-gate  * int_init() initialises the value of the supplied property with the
7487c478bd9Sstevel@tonic-gate  * supplied value.
7497c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS
7507c478bd9Sstevel@tonic-gate  */
7517c478bd9Sstevel@tonic-gate int
int_init(pool_prop_t * prop,int64_t val)7527c478bd9Sstevel@tonic-gate int_init(pool_prop_t *prop, int64_t val)
7537c478bd9Sstevel@tonic-gate {
7547c478bd9Sstevel@tonic-gate 	pool_value_set_int64(&prop->pp_value, val);
7557c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate /*
7597c478bd9Sstevel@tonic-gate  * double_init() initialises the value of the supplied property with the
7607c478bd9Sstevel@tonic-gate  * supplied value.
7617c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS
7627c478bd9Sstevel@tonic-gate  */
7637c478bd9Sstevel@tonic-gate int
double_init(pool_prop_t * prop,double val)7647c478bd9Sstevel@tonic-gate double_init(pool_prop_t *prop, double val)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	pool_value_set_double(&prop->pp_value, val);
7677c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate /*
7717c478bd9Sstevel@tonic-gate  * bool_init() initialises the value of the supplied property with the
7727c478bd9Sstevel@tonic-gate  * supplied value.
7737c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS
7747c478bd9Sstevel@tonic-gate  */
7757c478bd9Sstevel@tonic-gate int
bool_init(pool_prop_t * prop,uchar_t val)7767c478bd9Sstevel@tonic-gate bool_init(pool_prop_t *prop, uchar_t val)
7777c478bd9Sstevel@tonic-gate {
7787c478bd9Sstevel@tonic-gate 	pool_value_set_bool(&prop->pp_value, val);
7797c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate  * string_init() initialises the value of the supplied property with the
7847c478bd9Sstevel@tonic-gate  * supplied value.
7857c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate int
string_init(pool_prop_t * prop,const char * val)7887c478bd9Sstevel@tonic-gate string_init(pool_prop_t *prop, const char *val)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	return (pool_value_set_string(&prop->pp_value, val));
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate /*
7947c478bd9Sstevel@tonic-gate  * pool_get_provider_count() returns the count of registered providers.
7957c478bd9Sstevel@tonic-gate  *
7967c478bd9Sstevel@tonic-gate  * Returns count of registered providers
7977c478bd9Sstevel@tonic-gate  */
7987c478bd9Sstevel@tonic-gate uint_t
pool_get_provider_count(void)7997c478bd9Sstevel@tonic-gate pool_get_provider_count(void)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	uint_t count = 0;
8027c478bd9Sstevel@tonic-gate 	int i;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (pool_resource_elem_ctl) /
8057c478bd9Sstevel@tonic-gate 	    sizeof (pool_resource_elem_ctl[0]); i++) {
8067c478bd9Sstevel@tonic-gate 		if (pool_resource_elem_ctl[i] != NULL)
8077c478bd9Sstevel@tonic-gate 			count++;
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 	return (count);
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate /*
8137c478bd9Sstevel@tonic-gate  * Return all the props for a specified provider
8147c478bd9Sstevel@tonic-gate  */
8157c478bd9Sstevel@tonic-gate const pool_prop_t *
provider_get_props(const pool_elem_t * elem)8167c478bd9Sstevel@tonic-gate provider_get_props(const pool_elem_t *elem)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate 	const pool_prop_t *prop_list = NULL;
8197c478bd9Sstevel@tonic-gate 	pool_elem_class_t elem_class = pool_elem_class(elem);
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	switch (elem_class) {
8227c478bd9Sstevel@tonic-gate 	case PEC_SYSTEM:
8237c478bd9Sstevel@tonic-gate 	case PEC_POOL:
8247c478bd9Sstevel@tonic-gate 		prop_list = pool_elem_ctl[elem_class];
8257c478bd9Sstevel@tonic-gate 		break;
8267c478bd9Sstevel@tonic-gate 	case PEC_RES_AGG:
8277c478bd9Sstevel@tonic-gate 	case PEC_RES_COMP:
8287c478bd9Sstevel@tonic-gate 		prop_list = pool_resource_elem_ctl
8297c478bd9Sstevel@tonic-gate 		    [pool_resource_elem_class(elem)];
8307c478bd9Sstevel@tonic-gate 		break;
8317c478bd9Sstevel@tonic-gate 	case PEC_COMP:
8327c478bd9Sstevel@tonic-gate 		prop_list = pool_component_elem_ctl
8337c478bd9Sstevel@tonic-gate 		    [pool_component_elem_class(elem)];
8347c478bd9Sstevel@tonic-gate 		break;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	return (prop_list);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate  * provider_get_prop() return the pool_prop_t structure which
8417c478bd9Sstevel@tonic-gate  * describes the supplied property name for the supplied provider.
8427c478bd9Sstevel@tonic-gate  *
8437c478bd9Sstevel@tonic-gate  * Returns the property description or NULL if it doesn't exist.
8447c478bd9Sstevel@tonic-gate  */
8457c478bd9Sstevel@tonic-gate const pool_prop_t *
provider_get_prop(const pool_elem_t * elem,const char * name)8467c478bd9Sstevel@tonic-gate provider_get_prop(const pool_elem_t *elem, const char *name)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	int i;
8497c478bd9Sstevel@tonic-gate 	const pool_prop_t *prop_list;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	if ((prop_list = provider_get_props(elem)) == NULL)
8527c478bd9Sstevel@tonic-gate 		return (NULL);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	for (i = 0; prop_list[i].pp_pname != NULL; i++) {
8557c478bd9Sstevel@tonic-gate 		if (strcmp(name, prop_list[i].pp_pname) == 0) {
8567c478bd9Sstevel@tonic-gate 			return (&prop_list[i]);
8577c478bd9Sstevel@tonic-gate 		}
8587c478bd9Sstevel@tonic-gate 	}
8597c478bd9Sstevel@tonic-gate 	return (NULL);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate  * prop_is_type() checks the supplied property and returns PO_TRUE if the
8647c478bd9Sstevel@tonic-gate  * property value is 1 else PO_FALSE
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate static int
prop_is_type(int prop_type,const pool_prop_t * prop)8677c478bd9Sstevel@tonic-gate prop_is_type(int prop_type, const pool_prop_t *prop)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	return ((prop->pp_perms & prop_type) ? PO_TRUE : PO_FALSE);
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate  * prop_is_stored() returns PO_TRUE if the property is stored in the backing
8747c478bd9Sstevel@tonic-gate  * configuration and PO_FALSE else.
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate int
prop_is_stored(const pool_prop_t * prop)8777c478bd9Sstevel@tonic-gate prop_is_stored(const pool_prop_t *prop)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	return (prop_is_type(PP_STORED, prop));
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate /*
8837c478bd9Sstevel@tonic-gate  * prop_is_readonly() returns PO_TRUE if the property is a read-only property
8847c478bd9Sstevel@tonic-gate  * and PO_FALSE else.
8857c478bd9Sstevel@tonic-gate  */
8867c478bd9Sstevel@tonic-gate int
prop_is_readonly(const pool_prop_t * prop)8877c478bd9Sstevel@tonic-gate prop_is_readonly(const pool_prop_t *prop)
8887c478bd9Sstevel@tonic-gate {
8897c478bd9Sstevel@tonic-gate 	return (prop_is_type(PP_READ, prop));
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate /*
8937c478bd9Sstevel@tonic-gate  * prop_is_init() returns PO_TRUE if the property should be
8947c478bd9Sstevel@tonic-gate  * initialised when an element of this type is created and PO_FALSE
8957c478bd9Sstevel@tonic-gate  * else.
8967c478bd9Sstevel@tonic-gate  */
8977c478bd9Sstevel@tonic-gate int
prop_is_init(const pool_prop_t * prop)8987c478bd9Sstevel@tonic-gate prop_is_init(const pool_prop_t *prop)
8997c478bd9Sstevel@tonic-gate {
9007c478bd9Sstevel@tonic-gate 	return (prop_is_type(PP_INIT, prop));
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate /*
9047c478bd9Sstevel@tonic-gate  * prop_is_hidden() returns PO_TRUE if the property should be hidden
9057c478bd9Sstevel@tonic-gate  * from access by the external property access mechanisms.
9067c478bd9Sstevel@tonic-gate  */
9077c478bd9Sstevel@tonic-gate int
prop_is_hidden(const pool_prop_t * prop)9087c478bd9Sstevel@tonic-gate prop_is_hidden(const pool_prop_t *prop)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	return (prop_is_type(PP_HIDDEN, prop));
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate /*
9147c478bd9Sstevel@tonic-gate  * prop_is_optional() returns PO_TRUE if the property is optional and
9157c478bd9Sstevel@tonic-gate  * can be removed by external property access mechanisms.
9167c478bd9Sstevel@tonic-gate  */
9177c478bd9Sstevel@tonic-gate int
prop_is_optional(const pool_prop_t * prop)9187c478bd9Sstevel@tonic-gate prop_is_optional(const pool_prop_t *prop)
9197c478bd9Sstevel@tonic-gate {
9207c478bd9Sstevel@tonic-gate 	return (prop_is_type(PP_OPTIONAL, prop));
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate int
cpu_is_requested(pool_component_t * component)9247c478bd9Sstevel@tonic-gate cpu_is_requested(pool_component_t *component)
9257c478bd9Sstevel@tonic-gate {
9267c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
9277c478bd9Sstevel@tonic-gate 	uchar_t requested;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (pool_get_property(TO_CONF(TO_ELEM(component)), TO_ELEM(component),
9307c478bd9Sstevel@tonic-gate 	    "cpu.requested", &val) != POC_BOOL) {
9317c478bd9Sstevel@tonic-gate 		return (PO_FALSE);
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 	if (pool_value_get_bool(&val, &requested) != PO_SUCCESS) {
9347c478bd9Sstevel@tonic-gate 		return (PO_FALSE);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 	return ((int)requested);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate  * Common code for various resource get functions
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate static int
resource_get_common(const pool_resource_t * res,const char * name,uint64_t * uval)9437c478bd9Sstevel@tonic-gate resource_get_common(const pool_resource_t *res, const char *name,
9447c478bd9Sstevel@tonic-gate     uint64_t *uval)
9457c478bd9Sstevel@tonic-gate {
9467c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
9477c478bd9Sstevel@tonic-gate 	pool_value_class_t pvc;
9487c478bd9Sstevel@tonic-gate 	int retval = PO_SUCCESS;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	pvc = pool_get_ns_property(TO_ELEM(res), name, &val);
9517c478bd9Sstevel@tonic-gate 	if (pvc == POC_INVAL) {
9527c478bd9Sstevel@tonic-gate 		*uval = 0;
9537c478bd9Sstevel@tonic-gate #ifdef DEBUG
9547c478bd9Sstevel@tonic-gate 		dprintf("can't retrieve %s\n");
9557c478bd9Sstevel@tonic-gate 		pool_elem_dprintf(TO_ELEM(res));
9567c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
9577c478bd9Sstevel@tonic-gate 	} else if (pvc == POC_UINT) {
9587c478bd9Sstevel@tonic-gate 		retval = pool_value_get_uint64(&val, uval);
9597c478bd9Sstevel@tonic-gate 	}
9607c478bd9Sstevel@tonic-gate 	return (retval);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate /*
9647c478bd9Sstevel@tonic-gate  * resource_get_size() updates size with the size of the supplied resource.
9657c478bd9Sstevel@tonic-gate  *
9667c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
9677c478bd9Sstevel@tonic-gate  */
9687c478bd9Sstevel@tonic-gate int
resource_get_size(const pool_resource_t * res,uint64_t * size)9697c478bd9Sstevel@tonic-gate resource_get_size(const pool_resource_t *res, uint64_t *size)
9707c478bd9Sstevel@tonic-gate {
9717c478bd9Sstevel@tonic-gate 	return (resource_get_common(res, c_size_prop, size));
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate /*
9757c478bd9Sstevel@tonic-gate  * resource_get_pinned() updates pinned with the size of the
9767c478bd9Sstevel@tonic-gate  * pinned part of a supplied resource. Resource is not available for
9777c478bd9Sstevel@tonic-gate  * allocation if it is marked as "pinned".
9787c478bd9Sstevel@tonic-gate  *
9797c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
9807c478bd9Sstevel@tonic-gate  */
9817c478bd9Sstevel@tonic-gate int
resource_get_pinned(const pool_resource_t * res,uint64_t * pinned)9827c478bd9Sstevel@tonic-gate resource_get_pinned(const pool_resource_t *res, uint64_t *pinned)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	pool_value_t *props[] = { NULL, NULL };
9857c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
9867c478bd9Sstevel@tonic-gate 	pool_component_t **cs = NULL;
9877c478bd9Sstevel@tonic-gate 	uint_t ncompelem;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	props[0] = &val;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	pool_value_set_bool(props[0], PO_TRUE);
9927c478bd9Sstevel@tonic-gate 	if (pool_value_set_name(props[0], "cpu.pinned") != PO_SUCCESS)
9937c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	if ((cs = pool_query_resource_components(TO_CONF(TO_ELEM(res)), res,
9967c478bd9Sstevel@tonic-gate 	    &ncompelem, props)) != NULL) {
9977c478bd9Sstevel@tonic-gate 		*pinned = ncompelem;
9987c478bd9Sstevel@tonic-gate 		free(cs);
9997c478bd9Sstevel@tonic-gate 	} else
10007c478bd9Sstevel@tonic-gate 		*pinned = 0;
10017c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate  * resource_get_min() updates min with the minimum size of the supplied
10067c478bd9Sstevel@tonic-gate  * resource.
10077c478bd9Sstevel@tonic-gate  *
10087c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
10097c478bd9Sstevel@tonic-gate  */
10107c478bd9Sstevel@tonic-gate int
resource_get_min(const pool_resource_t * res,uint64_t * min)10117c478bd9Sstevel@tonic-gate resource_get_min(const pool_resource_t *res, uint64_t *min)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	return (resource_get_common(res, c_min_prop, min));
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate /*
10177c478bd9Sstevel@tonic-gate  * resource_get_max() updates max with the maximum size of the supplied
10187c478bd9Sstevel@tonic-gate  * resource.
10197c478bd9Sstevel@tonic-gate  *
10207c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
10217c478bd9Sstevel@tonic-gate  */
10227c478bd9Sstevel@tonic-gate int
resource_get_max(const pool_resource_t * res,uint64_t * max)10237c478bd9Sstevel@tonic-gate resource_get_max(const pool_resource_t *res, uint64_t *max)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	return (resource_get_common(res, c_max_prop, max));
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate /*
10297c478bd9Sstevel@tonic-gate  * TODO: This is pset specific
10307c478bd9Sstevel@tonic-gate  *
10317c478bd9Sstevel@tonic-gate  * get_default_resource() returns the default resource for type of the supplied
10327c478bd9Sstevel@tonic-gate  * resource.
10337c478bd9Sstevel@tonic-gate  *
10347c478bd9Sstevel@tonic-gate  * Returns A pointer to the default resource of the same type as the supplied
10357c478bd9Sstevel@tonic-gate  * resource.
10367c478bd9Sstevel@tonic-gate  */
10377c478bd9Sstevel@tonic-gate const pool_resource_t *
get_default_resource(const pool_resource_t * res)10387c478bd9Sstevel@tonic-gate get_default_resource(const pool_resource_t *res)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	return (resource_by_sysid(TO_CONF(TO_ELEM(res)), PS_NONE,
10417c478bd9Sstevel@tonic-gate 	    pool_elem_class_string(TO_ELEM(res))));
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * resource_is_default() returns 1 if the supplied resource is the default
10467c478bd9Sstevel@tonic-gate  * resource for it's type.
10477c478bd9Sstevel@tonic-gate  */
10487c478bd9Sstevel@tonic-gate int
resource_is_default(const pool_resource_t * res)10497c478bd9Sstevel@tonic-gate resource_is_default(const pool_resource_t *res)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	return (get_default_resource(res) == res);
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate /*
10567c478bd9Sstevel@tonic-gate  * resource_is_system() determines if the resource is a system resource.
10577c478bd9Sstevel@tonic-gate  */
10587c478bd9Sstevel@tonic-gate int
resource_is_system(const pool_resource_t * res)10597c478bd9Sstevel@tonic-gate resource_is_system(const pool_resource_t *res)
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate 	return (res->pr_is_system(res));
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate  * resource_can_associate() determines if it is possible to associate
10677c478bd9Sstevel@tonic-gate  * with the supplied resource.
10687c478bd9Sstevel@tonic-gate  */
10697c478bd9Sstevel@tonic-gate int
resource_can_associate(const pool_resource_t * res)10707c478bd9Sstevel@tonic-gate resource_can_associate(const pool_resource_t *res)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	return (res->pr_can_associate(res));
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate /*
10767c478bd9Sstevel@tonic-gate  * Common code to get an int64 property.
10777c478bd9Sstevel@tonic-gate  * Unfortunately (-1) is a valid psetid, so we'll return (-2) in case of
10787c478bd9Sstevel@tonic-gate  * error.
10797c478bd9Sstevel@tonic-gate  */
10807c478bd9Sstevel@tonic-gate static int64_t
elem_get_expected_int64(const pool_elem_t * elem,const char * name)10817c478bd9Sstevel@tonic-gate elem_get_expected_int64(const pool_elem_t *elem, const char *name)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	int64_t val64;
10847c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if (pool_get_ns_property(elem, name, &val) != POC_INT) {
10877c478bd9Sstevel@tonic-gate 		return (POOL_SYSID_BAD);
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 	(void) pool_value_get_int64(&val, &val64);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	return (val64);
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate /*
10957c478bd9Sstevel@tonic-gate  * The following returns a malloc'ed string which must be free'd by the
10967c478bd9Sstevel@tonic-gate  * caller.
10977c478bd9Sstevel@tonic-gate  */
10987c478bd9Sstevel@tonic-gate static char *
elem_get_expected_string(const pool_elem_t * elem,const char * name)10997c478bd9Sstevel@tonic-gate elem_get_expected_string(const pool_elem_t *elem, const char *name)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
11027c478bd9Sstevel@tonic-gate 	char *retval;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	if (pool_get_ns_property(elem, name, &val) != POC_STRING) {
11057c478bd9Sstevel@tonic-gate 		return (NULL);
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 	(void) pool_value_get_string(&val, (const char **)&retval);
11087c478bd9Sstevel@tonic-gate 	retval = strdup(retval);
11097c478bd9Sstevel@tonic-gate 	return (retval);
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate /*
11137c478bd9Sstevel@tonic-gate  * elem_get_sysid() returns the sys_id for the supplied elem.
11147c478bd9Sstevel@tonic-gate  */
11157c478bd9Sstevel@tonic-gate id_t
elem_get_sysid(const pool_elem_t * elem)11167c478bd9Sstevel@tonic-gate elem_get_sysid(const pool_elem_t *elem)
11177c478bd9Sstevel@tonic-gate {
11187c478bd9Sstevel@tonic-gate 	return ((id_t)elem_get_expected_int64(elem, c_sys_prop));
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate  * elem_get_name() returns the name for the supplied elem. Note that
11237c478bd9Sstevel@tonic-gate  * it is the caller's responsibility to free this memory.
11247c478bd9Sstevel@tonic-gate  */
11257c478bd9Sstevel@tonic-gate char *
elem_get_name(const pool_elem_t * elem)11267c478bd9Sstevel@tonic-gate elem_get_name(const pool_elem_t *elem)
11277c478bd9Sstevel@tonic-gate {
11287c478bd9Sstevel@tonic-gate 	return (elem_get_expected_string(elem, c_name));
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate /*
11327c478bd9Sstevel@tonic-gate  * elem_is_default() returns 1 if the supplied elem is the default
11337c478bd9Sstevel@tonic-gate  * elem for it's type.
11347c478bd9Sstevel@tonic-gate  */
11357c478bd9Sstevel@tonic-gate int
elem_is_default(const pool_elem_t * res)11367c478bd9Sstevel@tonic-gate elem_is_default(const pool_elem_t *res)
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	return (get_default_elem(res) == res);
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate 
1142*0209230bSgjelinek /*
1143*0209230bSgjelinek  * Return B_TRUE if the element has the 'temporary' property set.
1144*0209230bSgjelinek  */
1145*0209230bSgjelinek boolean_t
elem_is_tmp(const pool_elem_t * elem)1146*0209230bSgjelinek elem_is_tmp(const pool_elem_t *elem)
1147*0209230bSgjelinek {
1148*0209230bSgjelinek 	pool_value_t val = POOL_VALUE_INITIALIZER;
1149*0209230bSgjelinek 	uchar_t bval;
1150*0209230bSgjelinek 
1151*0209230bSgjelinek 	if (pool_get_ns_property(elem, "temporary", &val) != POC_BOOL)
1152*0209230bSgjelinek 		return (B_FALSE);
1153*0209230bSgjelinek 
1154*0209230bSgjelinek 	(void) pool_value_get_bool(&val, &bval);
1155*0209230bSgjelinek 
1156*0209230bSgjelinek 	return (bval != 0);
1157*0209230bSgjelinek }
1158*0209230bSgjelinek 
11597c478bd9Sstevel@tonic-gate /*
11607c478bd9Sstevel@tonic-gate  * get_default_elem() returns the default elem for type of the supplied
11617c478bd9Sstevel@tonic-gate  * elem.
11627c478bd9Sstevel@tonic-gate  *
11637c478bd9Sstevel@tonic-gate  * Returns A pointer to the default elem of the same type as the
11647c478bd9Sstevel@tonic-gate  * supplied elem or NULL on error. Trying to access the default elem
11657c478bd9Sstevel@tonic-gate  * for a type of element which doesn't support the notion of default
11667c478bd9Sstevel@tonic-gate  * is an error.
11677c478bd9Sstevel@tonic-gate  */
11687c478bd9Sstevel@tonic-gate const pool_elem_t *
get_default_elem(const pool_elem_t * pe)11697c478bd9Sstevel@tonic-gate get_default_elem(const pool_elem_t *pe)
11707c478bd9Sstevel@tonic-gate {
11717c478bd9Sstevel@tonic-gate 	pool_result_set_t *rs;
11727c478bd9Sstevel@tonic-gate 	pool_value_t *props[] = { NULL, NULL };
11737c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
11747c478bd9Sstevel@tonic-gate 	char_buf_t *cb;
11757c478bd9Sstevel@tonic-gate 	const pool_elem_t *pe_default;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	props[0] = &val;
11787c478bd9Sstevel@tonic-gate 	if ((cb = alloc_char_buf(CB_DEFAULT_LEN)) == NULL) {
11797c478bd9Sstevel@tonic-gate 		return (NULL);
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 	if (set_char_buf(cb, "%s.default", pool_elem_class_string(pe)) !=
11827c478bd9Sstevel@tonic-gate 	    PO_SUCCESS) {
11837c478bd9Sstevel@tonic-gate 		free_char_buf(cb);
11847c478bd9Sstevel@tonic-gate 		return (NULL);
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate 	if (pool_value_set_name(props[0], cb->cb_buf) != PO_SUCCESS) {
11877c478bd9Sstevel@tonic-gate 		free_char_buf(cb);
11887c478bd9Sstevel@tonic-gate 		return (NULL);
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 	free_char_buf(cb);
11917c478bd9Sstevel@tonic-gate 	pool_value_set_bool(props[0], PO_TRUE);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if ((rs = pool_exec_query(TO_CONF(pe), NULL, NULL,
11947c478bd9Sstevel@tonic-gate 	    PEC_QRY_ELEM(pe), props)) == NULL) {
11957c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
11967c478bd9Sstevel@tonic-gate 		return (NULL);
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 	if (pool_rs_count(rs) != 1) {
11997c478bd9Sstevel@tonic-gate 		(void) pool_rs_close(rs);
12007c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
12017c478bd9Sstevel@tonic-gate 		return (NULL);
12027c478bd9Sstevel@tonic-gate 	}
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	pe_default = rs->prs_next(rs);
12057c478bd9Sstevel@tonic-gate 	(void) pool_rs_close(rs);
12067c478bd9Sstevel@tonic-gate 	return (pe_default);
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate /*
12107c478bd9Sstevel@tonic-gate  * is_a_known_prefix() determines if the supplied prop_name is a known
12117c478bd9Sstevel@tonic-gate  * name for the supplied class.
12127c478bd9Sstevel@tonic-gate  *
12137c478bd9Sstevel@tonic-gate  * Returns a pointer to the prefix if it is found or NULL
12147c478bd9Sstevel@tonic-gate  */
12157c478bd9Sstevel@tonic-gate const char *
is_a_known_prefix(pool_elem_class_t class,const char * prop_name)12167c478bd9Sstevel@tonic-gate is_a_known_prefix(pool_elem_class_t class, const char *prop_name)
12177c478bd9Sstevel@tonic-gate {
12187c478bd9Sstevel@tonic-gate 	int i;
12197c478bd9Sstevel@tonic-gate 	int len;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	switch (class) {
12227c478bd9Sstevel@tonic-gate 	case PEC_SYSTEM:
12237c478bd9Sstevel@tonic-gate 	case PEC_POOL:
12247c478bd9Sstevel@tonic-gate 		len = strlen(pool_elem_class_name[class]);
12257c478bd9Sstevel@tonic-gate 		if (strncmp(prop_name, pool_elem_class_name[class], len) == 0 &&
12267c478bd9Sstevel@tonic-gate 		    prop_name[len] == '.' || strcmp(prop_name, c_type) == 0)
12277c478bd9Sstevel@tonic-gate 			return (pool_elem_class_name[class]);
12287c478bd9Sstevel@tonic-gate 		break;
12297c478bd9Sstevel@tonic-gate 	case PEC_RES_COMP:
12307c478bd9Sstevel@tonic-gate 	case PEC_RES_AGG:
12317c478bd9Sstevel@tonic-gate 		for (i = 0; i < sizeof (pool_resource_elem_class_name) /
12327c478bd9Sstevel@tonic-gate 		    sizeof (pool_resource_elem_class_name[0]); i++) {
12337c478bd9Sstevel@tonic-gate 			len = strlen(pool_resource_elem_class_name[i]);
12347c478bd9Sstevel@tonic-gate 			if (strncmp(prop_name,
12357c478bd9Sstevel@tonic-gate 			    pool_resource_elem_class_name[i], len) == 0 &&
12367c478bd9Sstevel@tonic-gate 			    prop_name[len] == '.' ||
12377c478bd9Sstevel@tonic-gate 			    strcmp(prop_name, c_type) == 0)
12387c478bd9Sstevel@tonic-gate 				return (pool_resource_elem_class_name[i]);
12397c478bd9Sstevel@tonic-gate 		}
12407c478bd9Sstevel@tonic-gate 		break;
12417c478bd9Sstevel@tonic-gate 	case PEC_COMP:
12427c478bd9Sstevel@tonic-gate 		for (i = 0; i < sizeof (pool_component_elem_class_name) /
12437c478bd9Sstevel@tonic-gate 		    sizeof (pool_component_elem_class_name[0]); i++) {
12447c478bd9Sstevel@tonic-gate 			len = strlen(pool_component_elem_class_name[i]);
12457c478bd9Sstevel@tonic-gate 			if (strncmp(prop_name,
12467c478bd9Sstevel@tonic-gate 			    pool_component_elem_class_name[i], len) == 0 &&
12477c478bd9Sstevel@tonic-gate 			    prop_name[len] == '.' ||
12487c478bd9Sstevel@tonic-gate 			    strcmp(prop_name, c_type) == 0)
12497c478bd9Sstevel@tonic-gate 				return (pool_component_elem_class_name[i]);
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 		break;
12527c478bd9Sstevel@tonic-gate 	default:
12537c478bd9Sstevel@tonic-gate 		break;
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 	return (NULL);
12567c478bd9Sstevel@tonic-gate }
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate const char *
pool_elem_class_string(const pool_elem_t * pe)12607c478bd9Sstevel@tonic-gate pool_elem_class_string(const pool_elem_t *pe)
12617c478bd9Sstevel@tonic-gate {
12627c478bd9Sstevel@tonic-gate 	switch (pool_elem_class(pe)) {
12637c478bd9Sstevel@tonic-gate 	case PEC_SYSTEM:
12647c478bd9Sstevel@tonic-gate 	case PEC_POOL:
12657c478bd9Sstevel@tonic-gate 		return (pool_elem_class_name[pool_elem_class(pe)]);
12667c478bd9Sstevel@tonic-gate 	case PEC_RES_COMP:
12677c478bd9Sstevel@tonic-gate 	case PEC_RES_AGG:
12687c478bd9Sstevel@tonic-gate 		return (pool_resource_elem_class_name
12697c478bd9Sstevel@tonic-gate 		    [pool_resource_elem_class(pe)]);
12707c478bd9Sstevel@tonic-gate 	case PEC_COMP:
12717c478bd9Sstevel@tonic-gate 		return (pool_component_elem_class_name
12727c478bd9Sstevel@tonic-gate 		    [pool_component_elem_class(pe)]);
12737c478bd9Sstevel@tonic-gate 	default:
12747c478bd9Sstevel@tonic-gate 		return (pool_elem_class_name[PEC_INVALID]);
12757c478bd9Sstevel@tonic-gate 	}
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate const char *
pool_resource_type_string(pool_resource_elem_class_t type)12797c478bd9Sstevel@tonic-gate pool_resource_type_string(pool_resource_elem_class_t type)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	return (pool_resource_elem_class_name[type]);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate const char *
pool_component_type_string(pool_component_elem_class_t type)12857c478bd9Sstevel@tonic-gate pool_component_type_string(pool_component_elem_class_t type)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	return (pool_component_elem_class_name[type]);
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate /*
12917c478bd9Sstevel@tonic-gate  * resource_by_sysid() finds a resource from it's supplied sysid and type.
12927c478bd9Sstevel@tonic-gate  *
12937c478bd9Sstevel@tonic-gate  * Returns a pointer to the resource or NULL if it doesn't exist.
12947c478bd9Sstevel@tonic-gate  */
12957c478bd9Sstevel@tonic-gate pool_resource_t *
resource_by_sysid(const pool_conf_t * conf,id_t sysid,const char * type)12967c478bd9Sstevel@tonic-gate resource_by_sysid(const pool_conf_t *conf, id_t sysid, const char *type)
12977c478bd9Sstevel@tonic-gate {
12987c478bd9Sstevel@tonic-gate 	pool_value_t *props[] = { NULL, NULL, NULL };
12997c478bd9Sstevel@tonic-gate 	pool_resource_t **resources = NULL;
13007c478bd9Sstevel@tonic-gate 	pool_resource_t *retval = NULL;
13017c478bd9Sstevel@tonic-gate 	uint_t nelem;
13027c478bd9Sstevel@tonic-gate 	char_buf_t *cb;
13037c478bd9Sstevel@tonic-gate 	pool_value_t val0 = POOL_VALUE_INITIALIZER;
13047c478bd9Sstevel@tonic-gate 	pool_value_t val1 = POOL_VALUE_INITIALIZER;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	props[0] = &val0;
13077c478bd9Sstevel@tonic-gate 	props[1] = &val1;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	if (pool_value_set_string(props[0], type) != PO_SUCCESS ||
13107c478bd9Sstevel@tonic-gate 	    pool_value_set_name(props[0], c_type) != PO_SUCCESS)
13117c478bd9Sstevel@tonic-gate 		return (NULL);
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	if ((cb = alloc_char_buf(CB_DEFAULT_LEN)) == NULL) {
13147c478bd9Sstevel@tonic-gate 		return (NULL);
13157c478bd9Sstevel@tonic-gate 	}
13167c478bd9Sstevel@tonic-gate 	if (set_char_buf(cb, "%s.sys_id", type) != PO_SUCCESS) {
13177c478bd9Sstevel@tonic-gate 		free_char_buf(cb);
13187c478bd9Sstevel@tonic-gate 		return (NULL);
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	if (pool_value_set_name(props[1], cb->cb_buf) != PO_SUCCESS) {
13217c478bd9Sstevel@tonic-gate 		free_char_buf(cb);
13227c478bd9Sstevel@tonic-gate 		return (NULL);
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 	free_char_buf(cb);
13257c478bd9Sstevel@tonic-gate 	pool_value_set_int64(props[1], sysid);
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	resources = pool_query_resources(conf, &nelem, props);
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	if (resources != NULL) {
13307c478bd9Sstevel@tonic-gate 		retval = resources[0];
13317c478bd9Sstevel@tonic-gate 		free(resources);
13327c478bd9Sstevel@tonic-gate 	}
13337c478bd9Sstevel@tonic-gate 	return (retval);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate pool_elem_class_t
pool_elem_class_from_string(const char * type)13377c478bd9Sstevel@tonic-gate pool_elem_class_from_string(const char *type)
13387c478bd9Sstevel@tonic-gate {
13397c478bd9Sstevel@tonic-gate 	int i;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (pool_elem_class_name) /
13427c478bd9Sstevel@tonic-gate 	    sizeof (pool_elem_class_name[0]); i++) {
13437c478bd9Sstevel@tonic-gate 		if (strcmp(pool_elem_class_name[i], type) == 0)
13447c478bd9Sstevel@tonic-gate 			break;
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 	if (i == sizeof (pool_elem_class_name) /
13477c478bd9Sstevel@tonic-gate 	    sizeof (pool_elem_class_name[0]))
13487c478bd9Sstevel@tonic-gate 		return (PEC_INVALID);
13497c478bd9Sstevel@tonic-gate 	return ((pool_elem_class_t)i);
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate pool_resource_elem_class_t
pool_resource_elem_class_from_string(const char * type)13537c478bd9Sstevel@tonic-gate pool_resource_elem_class_from_string(const char *type)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	int i;
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (pool_resource_elem_class_name) /
13587c478bd9Sstevel@tonic-gate 	    sizeof (pool_resource_elem_class_name[0]); i++) {
13597c478bd9Sstevel@tonic-gate 		if (strcmp(pool_resource_elem_class_name[i], type) == 0)
13607c478bd9Sstevel@tonic-gate 			break;
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate 	if (i == sizeof (pool_resource_elem_class_name) /
13637c478bd9Sstevel@tonic-gate 	    sizeof (pool_resource_elem_class_name[0]))
13647c478bd9Sstevel@tonic-gate 		return (PREC_INVALID);
13657c478bd9Sstevel@tonic-gate 	return ((pool_resource_elem_class_t)i);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate pool_component_elem_class_t
pool_component_elem_class_from_string(const char * type)13697c478bd9Sstevel@tonic-gate pool_component_elem_class_from_string(const char *type)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	int i;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (pool_component_elem_class_name) /
13747c478bd9Sstevel@tonic-gate 	    sizeof (pool_component_elem_class_name[0]); i++) {
13757c478bd9Sstevel@tonic-gate 		if (strcmp(pool_component_elem_class_name[i], type) == 0)
13767c478bd9Sstevel@tonic-gate 			break;
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 	if (i == sizeof (pool_component_elem_class_name) /
13797c478bd9Sstevel@tonic-gate 	    sizeof (pool_component_elem_class_name[0]))
13807c478bd9Sstevel@tonic-gate 		return (PCEC_INVALID);
13817c478bd9Sstevel@tonic-gate 	return ((pool_component_elem_class_t)i);
13827c478bd9Sstevel@tonic-gate }
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate /*
13857c478bd9Sstevel@tonic-gate  * pool_resource_type_list() populates the supplied array of pointers
13867c478bd9Sstevel@tonic-gate  * with the names of the available resource types on this system.
13877c478bd9Sstevel@tonic-gate  */
13887c478bd9Sstevel@tonic-gate int
pool_resource_type_list(const char ** types,uint_t * numtypes)13897c478bd9Sstevel@tonic-gate pool_resource_type_list(const char **types, uint_t *numtypes)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate 	int i, j;
13927c478bd9Sstevel@tonic-gate 	uint_t maxnum = *numtypes;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	*numtypes = pool_get_provider_count();
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	if (types) {
13977c478bd9Sstevel@tonic-gate 		for (i = 0, j = 0; i < sizeof (pool_resource_elem_ctl) /
13987c478bd9Sstevel@tonic-gate 		    sizeof (pool_resource_elem_ctl[0]) && j < maxnum; i++) {
13997c478bd9Sstevel@tonic-gate 			if (pool_resource_elem_ctl[i] != NULL)
14007c478bd9Sstevel@tonic-gate 				types[j++] = pool_resource_elem_class_name[i];
14017c478bd9Sstevel@tonic-gate 		}
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate /*
14077c478bd9Sstevel@tonic-gate  * Return the system element for the supplied conf.
14087c478bd9Sstevel@tonic-gate  * NULL is returned if an error is detected and the error code is updated
14097c478bd9Sstevel@tonic-gate  * to indicate the cause of the error.
14107c478bd9Sstevel@tonic-gate  */
14117c478bd9Sstevel@tonic-gate pool_system_t *
pool_conf_system(const pool_conf_t * conf)14127c478bd9Sstevel@tonic-gate pool_conf_system(const pool_conf_t *conf)
14137c478bd9Sstevel@tonic-gate {
14147c478bd9Sstevel@tonic-gate 	pool_elem_t *system;
14157c478bd9Sstevel@tonic-gate 	pool_result_set_t *rs;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	if ((rs = pool_exec_query(conf, NULL, NULL, PEC_QRY_SYSTEM, NULL)) ==
14187c478bd9Sstevel@tonic-gate 	    NULL) {
14197c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
14207c478bd9Sstevel@tonic-gate 		return (NULL);
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 	/* There should only be one system record */
14237c478bd9Sstevel@tonic-gate 	if (pool_rs_count(rs) != 1) {
14247c478bd9Sstevel@tonic-gate 		pool_seterror(POE_INVALID_CONF);
14257c478bd9Sstevel@tonic-gate 		(void) pool_rs_close(rs);
14267c478bd9Sstevel@tonic-gate 		return (NULL);
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 	system = rs->prs_next(rs);
14297c478bd9Sstevel@tonic-gate 	(void) pool_rs_close(rs);
14307c478bd9Sstevel@tonic-gate 	return (pool_elem_system(system));
14317c478bd9Sstevel@tonic-gate }
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate pool_system_t *
pool_elem_system(const pool_elem_t * pe)14347c478bd9Sstevel@tonic-gate pool_elem_system(const pool_elem_t *pe)
14357c478bd9Sstevel@tonic-gate {
14367c478bd9Sstevel@tonic-gate 	if (pe->pe_class != PEC_SYSTEM) {
14377c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
14387c478bd9Sstevel@tonic-gate 		return (NULL);
14397c478bd9Sstevel@tonic-gate 	}
14407c478bd9Sstevel@tonic-gate 	return ((pool_system_t *)pe);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate pool_t *
pool_elem_pool(const pool_elem_t * pe)14447c478bd9Sstevel@tonic-gate pool_elem_pool(const pool_elem_t *pe)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	if (pe->pe_class != PEC_POOL) {
14477c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
14487c478bd9Sstevel@tonic-gate 		return (NULL);
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	return ((pool_t *)pe);
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate pool_resource_t *
pool_elem_res(const pool_elem_t * pe)14547c478bd9Sstevel@tonic-gate pool_elem_res(const pool_elem_t *pe)
14557c478bd9Sstevel@tonic-gate {
14567c478bd9Sstevel@tonic-gate 	if (pe->pe_class != PEC_RES_COMP &&
14577c478bd9Sstevel@tonic-gate 	    pool_elem_class(pe) != PEC_RES_AGG) {
14587c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
14597c478bd9Sstevel@tonic-gate 		return (NULL);
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	return ((pool_resource_t *)pe);
14627c478bd9Sstevel@tonic-gate }
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate pool_component_t *
pool_elem_comp(const pool_elem_t * pe)14657c478bd9Sstevel@tonic-gate pool_elem_comp(const pool_elem_t *pe)
14667c478bd9Sstevel@tonic-gate {
14677c478bd9Sstevel@tonic-gate 	if (pe->pe_class != PEC_COMP) {
14687c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
14697c478bd9Sstevel@tonic-gate 		return (NULL);
14707c478bd9Sstevel@tonic-gate 	}
14717c478bd9Sstevel@tonic-gate 	return ((pool_component_t *)pe);
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate /*
14757c478bd9Sstevel@tonic-gate  * qsort_elem_compare() is used for qsort elemement comparison.
14767c478bd9Sstevel@tonic-gate  *
14777c478bd9Sstevel@tonic-gate  * Returns see qsort(3c)
14787c478bd9Sstevel@tonic-gate  */
14797c478bd9Sstevel@tonic-gate int
qsort_elem_compare(const void * a,const void * b)14807c478bd9Sstevel@tonic-gate qsort_elem_compare(const void *a, const void *b)
14817c478bd9Sstevel@tonic-gate {
14827c478bd9Sstevel@tonic-gate 	const pool_elem_t *e1 = *(const pool_elem_t **)a;
14837c478bd9Sstevel@tonic-gate 	const pool_elem_t *e2 = *(const pool_elem_t **)b;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	/*
14867c478bd9Sstevel@tonic-gate 	 * Special case for handling name changes on default elements
14877c478bd9Sstevel@tonic-gate 	 * If both elements are default elements then always return 0
14887c478bd9Sstevel@tonic-gate 	 */
14897c478bd9Sstevel@tonic-gate 	if (pool_elem_same_class(e1, e2) == PO_TRUE &&
14907c478bd9Sstevel@tonic-gate 	    (elem_is_default(e1) && elem_is_default(e2)))
14917c478bd9Sstevel@tonic-gate 			return (0);
14927c478bd9Sstevel@tonic-gate 	else
14937c478bd9Sstevel@tonic-gate 		return (pool_elem_compare_name(e1, e2));
14947c478bd9Sstevel@tonic-gate }
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate /*
14977c478bd9Sstevel@tonic-gate  * Dynamic character buffers.
14987c478bd9Sstevel@tonic-gate  */
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate /*
15017c478bd9Sstevel@tonic-gate  * Resize the supplied character buffer to the new size.
15027c478bd9Sstevel@tonic-gate  */
15037c478bd9Sstevel@tonic-gate static int
resize_char_buf(char_buf_t * cb,size_t size)15047c478bd9Sstevel@tonic-gate resize_char_buf(char_buf_t *cb, size_t size)
15057c478bd9Sstevel@tonic-gate {
15067c478bd9Sstevel@tonic-gate 	char *re_cb = NULL;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	if ((re_cb = realloc(cb->cb_buf, size)) == NULL) {
15097c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
15107c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
15117c478bd9Sstevel@tonic-gate 	}
15127c478bd9Sstevel@tonic-gate 	/* If inital allocation, make sure buffer is zeroed */
15137c478bd9Sstevel@tonic-gate 	if (cb->cb_buf == NULL)
15147c478bd9Sstevel@tonic-gate 		(void) memset(re_cb, 0, sizeof (re_cb));
15157c478bd9Sstevel@tonic-gate 	/* If resized smaller, make sure buffer NULL terminated */
15167c478bd9Sstevel@tonic-gate 	if (size < cb->cb_size)
15177c478bd9Sstevel@tonic-gate 		re_cb[size] = 0;
15187c478bd9Sstevel@tonic-gate 	cb->cb_buf = re_cb;
15197c478bd9Sstevel@tonic-gate 	cb->cb_size = size;
15207c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
15217c478bd9Sstevel@tonic-gate }
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate /*
15247c478bd9Sstevel@tonic-gate  * Allocate a new char_buf_t structure. If there isn't enough memory, return
15257c478bd9Sstevel@tonic-gate  * NULL. Initialise the new char_buf_t to 0 and then call resize_char_buf
15267c478bd9Sstevel@tonic-gate  * to initialise the character buffer. Return a pointer to the new
15277c478bd9Sstevel@tonic-gate  * char_buf_t if the operation succeeds.
15287c478bd9Sstevel@tonic-gate  */
15297c478bd9Sstevel@tonic-gate char_buf_t *
alloc_char_buf(size_t size)15307c478bd9Sstevel@tonic-gate alloc_char_buf(size_t size)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 	char_buf_t *cb;
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	if ((cb = malloc(sizeof (char_buf_t))) == NULL) {
15357c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
15367c478bd9Sstevel@tonic-gate 		return (NULL);
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 	(void) memset(cb, 0, sizeof (char_buf_t));
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	if (resize_char_buf(cb, size + 1) == PO_FAIL) {
15417c478bd9Sstevel@tonic-gate 		free(cb);
15427c478bd9Sstevel@tonic-gate 		return (NULL);
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate 	return (cb);
15457c478bd9Sstevel@tonic-gate }
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate /*
15487c478bd9Sstevel@tonic-gate  * Free the character buffer and then free the char_buf_t.
15497c478bd9Sstevel@tonic-gate  */
15507c478bd9Sstevel@tonic-gate void
free_char_buf(char_buf_t * cb)15517c478bd9Sstevel@tonic-gate free_char_buf(char_buf_t *cb)
15527c478bd9Sstevel@tonic-gate {
15537c478bd9Sstevel@tonic-gate 	free((void *)cb->cb_buf);
15547c478bd9Sstevel@tonic-gate 	free(cb);
15557c478bd9Sstevel@tonic-gate }
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate /*
15587c478bd9Sstevel@tonic-gate  * Set the character buffer to the supplied data. The user supplies a printf
15597c478bd9Sstevel@tonic-gate  * like format string and then an appropriate number of parameters for the
15607c478bd9Sstevel@tonic-gate  * specified format. The character buffer is automatically resized to fit
15617c478bd9Sstevel@tonic-gate  * the data as determined by resize_char_buf.
15627c478bd9Sstevel@tonic-gate  */
15637c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
15647c478bd9Sstevel@tonic-gate int
set_char_buf(char_buf_t * cb,const char * fmt,...)15657c478bd9Sstevel@tonic-gate set_char_buf(char_buf_t *cb, const char *fmt, ...)
15667c478bd9Sstevel@tonic-gate {
15677c478bd9Sstevel@tonic-gate 	va_list ap;
15687c478bd9Sstevel@tonic-gate 	int new_size;
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
15717c478bd9Sstevel@tonic-gate 	if ((new_size = vsnprintf(cb->cb_buf, cb->cb_size, fmt, ap)) >=
15727c478bd9Sstevel@tonic-gate 	    cb->cb_size) {
15737c478bd9Sstevel@tonic-gate 		if (resize_char_buf(cb, new_size + 1) != PO_SUCCESS) {
15747c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
15757c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
15767c478bd9Sstevel@tonic-gate 		}
15777c478bd9Sstevel@tonic-gate 		(void) vsnprintf(cb->cb_buf, cb->cb_size, fmt, ap);
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 	va_end(ap);
15807c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate /*
15847c478bd9Sstevel@tonic-gate  * Append the supplied data to the character buffer. The user supplies a printf
15857c478bd9Sstevel@tonic-gate  * like format string and then an appropriate number of parameters for the
15867c478bd9Sstevel@tonic-gate  * specified format. The character buffer is automatically resized to fit
15877c478bd9Sstevel@tonic-gate  * the data as determined by resize_char_buf.
15887c478bd9Sstevel@tonic-gate  */
15897c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
15907c478bd9Sstevel@tonic-gate int
append_char_buf(char_buf_t * cb,const char * fmt,...)15917c478bd9Sstevel@tonic-gate append_char_buf(char_buf_t *cb, const char *fmt, ...)
15927c478bd9Sstevel@tonic-gate {
15937c478bd9Sstevel@tonic-gate 	va_list ap;
15947c478bd9Sstevel@tonic-gate 	int new_len;
15957c478bd9Sstevel@tonic-gate 	char size_buf[1];
15967c478bd9Sstevel@tonic-gate 	int old_len = 0;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	if (cb->cb_buf != NULL)
15997c478bd9Sstevel@tonic-gate 		old_len = strlen(cb->cb_buf);
16007c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
16017c478bd9Sstevel@tonic-gate 	new_len = vsnprintf(size_buf, sizeof (size_buf), fmt, ap);
16027c478bd9Sstevel@tonic-gate 	if (new_len + old_len >= cb->cb_size) {
16037c478bd9Sstevel@tonic-gate 		if (resize_char_buf(cb, old_len + new_len + 1) !=
16047c478bd9Sstevel@tonic-gate 		    PO_SUCCESS) {
16057c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
16067c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
16077c478bd9Sstevel@tonic-gate 		}
16087c478bd9Sstevel@tonic-gate 	}
16097c478bd9Sstevel@tonic-gate 	/*
16107c478bd9Sstevel@tonic-gate 	 * Resized the buffer to the right size, now append the new data
16117c478bd9Sstevel@tonic-gate 	 */
16127c478bd9Sstevel@tonic-gate 	(void) vsnprintf(&cb->cb_buf[old_len], cb->cb_size - old_len, fmt, ap);
16137c478bd9Sstevel@tonic-gate 	va_end(ap);
16147c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate /*
16187c478bd9Sstevel@tonic-gate  * Return the class for the supplied elem.
16197c478bd9Sstevel@tonic-gate  * If the return is PEC_INVALID, the error code will be set to reflect cause.
16207c478bd9Sstevel@tonic-gate  */
16217c478bd9Sstevel@tonic-gate pool_elem_class_t
pool_elem_class(const pool_elem_t * elem)16227c478bd9Sstevel@tonic-gate pool_elem_class(const pool_elem_t *elem)
16237c478bd9Sstevel@tonic-gate {
16247c478bd9Sstevel@tonic-gate 	return (elem->pe_class);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate /*
16297c478bd9Sstevel@tonic-gate  * Return the resource class for the supplied elem.
16307c478bd9Sstevel@tonic-gate  */
16317c478bd9Sstevel@tonic-gate pool_resource_elem_class_t
pool_resource_elem_class(const pool_elem_t * elem)16327c478bd9Sstevel@tonic-gate pool_resource_elem_class(const pool_elem_t *elem)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	return (elem->pe_resource_class);
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate /*
16387c478bd9Sstevel@tonic-gate  * Return the component class for the supplied elem.
16397c478bd9Sstevel@tonic-gate  */
16407c478bd9Sstevel@tonic-gate pool_component_elem_class_t
pool_component_elem_class(const pool_elem_t * elem)16417c478bd9Sstevel@tonic-gate pool_component_elem_class(const pool_elem_t *elem)
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate 	return (elem->pe_component_class);
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate pool_elem_t *
pool_get_pair(const pool_elem_t * pe)16477c478bd9Sstevel@tonic-gate pool_get_pair(const pool_elem_t *pe)
16487c478bd9Sstevel@tonic-gate {
16497c478bd9Sstevel@tonic-gate 	return (pe->pe_pair);
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate void
pool_set_pair(pool_elem_t * pe1,pool_elem_t * pe2)16537c478bd9Sstevel@tonic-gate pool_set_pair(pool_elem_t *pe1, pool_elem_t *pe2)
16547c478bd9Sstevel@tonic-gate {
16557c478bd9Sstevel@tonic-gate 	pe1->pe_pair = pe2;
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate int
pool_validate_resource(const pool_conf_t * conf,const char * type,const char * prop,int64_t delta)16597c478bd9Sstevel@tonic-gate pool_validate_resource(const pool_conf_t *conf, const char *type,
16607c478bd9Sstevel@tonic-gate     const char *prop, int64_t delta)
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate 	pool_conf_t *dyn;
16637c478bd9Sstevel@tonic-gate 	uint_t nelem;
16647c478bd9Sstevel@tonic-gate 	uint64_t available, required, uval;
16657c478bd9Sstevel@tonic-gate 	int i;
16667c478bd9Sstevel@tonic-gate 	pool_resource_t **rl;
16677c478bd9Sstevel@tonic-gate 	pool_value_t val = POOL_VALUE_INITIALIZER;
16687c478bd9Sstevel@tonic-gate 	pool_value_t val1 = POOL_VALUE_INITIALIZER;
16697c478bd9Sstevel@tonic-gate 	pool_value_t *pvals[] = { NULL, NULL };
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 	if (strcmp(prop, c_min_prop) && strcmp(prop, c_max_prop)) {
16727c478bd9Sstevel@tonic-gate 		pool_seterror(POE_BADPARAM);
16737c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	pvals[0] = &val;
16777c478bd9Sstevel@tonic-gate 	(void) pool_value_set_string(&val, type);
16787c478bd9Sstevel@tonic-gate 	(void) pool_value_set_name(&val, c_type);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	/*
16817c478bd9Sstevel@tonic-gate 	 * Check that there are available resources on this
16827c478bd9Sstevel@tonic-gate 	 * system for this configuration to be applied. Find
16837c478bd9Sstevel@tonic-gate 	 * each resource type and then find all resources of
16847c478bd9Sstevel@tonic-gate 	 * each type and total ".min". Find all available
16857c478bd9Sstevel@tonic-gate 	 * resources and ensure >= total min.
16867c478bd9Sstevel@tonic-gate 	 */
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	available = 0;
16897c478bd9Sstevel@tonic-gate 	required = delta;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	if ((rl = (pool_query_resources(conf, &nelem, pvals))) == NULL)
16927c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++) {
16957c478bd9Sstevel@tonic-gate 		if (pool_get_ns_property(TO_ELEM(rl[i]), prop,
16967c478bd9Sstevel@tonic-gate 		    &val1) == POC_INVAL ||
16977c478bd9Sstevel@tonic-gate 		    pool_value_get_uint64(&val1, &uval) != PO_SUCCESS) {
16987c478bd9Sstevel@tonic-gate 			free(rl);
16997c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
17007c478bd9Sstevel@tonic-gate 		}
17017c478bd9Sstevel@tonic-gate 		/*
17027c478bd9Sstevel@tonic-gate 		 * Watch out for overflow
17037c478bd9Sstevel@tonic-gate 		 */
17047c478bd9Sstevel@tonic-gate 		if (required + uval < required) {
17057c478bd9Sstevel@tonic-gate 			required = UINT64_MAX;
17067c478bd9Sstevel@tonic-gate 			break;
17077c478bd9Sstevel@tonic-gate 		} else
17087c478bd9Sstevel@tonic-gate 			required += uval;
17097c478bd9Sstevel@tonic-gate 	}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	if (conf_is_dynamic(conf) == PO_TRUE) {
17127c478bd9Sstevel@tonic-gate 		dyn = (pool_conf_t *)conf;
17137c478bd9Sstevel@tonic-gate 	} else {
17147c478bd9Sstevel@tonic-gate 		free(rl);
17157c478bd9Sstevel@tonic-gate 		if ((dyn = pool_conf_alloc()) == NULL)
17167c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
17177c478bd9Sstevel@tonic-gate 		if (pool_conf_open(dyn, pool_dynamic_location(), PO_RDONLY) !=
17187c478bd9Sstevel@tonic-gate 		PO_SUCCESS) {
17197c478bd9Sstevel@tonic-gate 			pool_conf_free(dyn);
17207c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
17217c478bd9Sstevel@tonic-gate 		}
17227c478bd9Sstevel@tonic-gate 		if ((rl = (pool_query_resources(dyn, &nelem, pvals))) ==
17237c478bd9Sstevel@tonic-gate 		    NULL) {
17247c478bd9Sstevel@tonic-gate 			(void) pool_conf_close(dyn);
17257c478bd9Sstevel@tonic-gate 			pool_conf_free(dyn);
17267c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
17277c478bd9Sstevel@tonic-gate 		}
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++) {
17307c478bd9Sstevel@tonic-gate 		if (pool_get_ns_property(TO_ELEM(rl[i]), c_size_prop,
17317c478bd9Sstevel@tonic-gate 		    &val1) == POC_INVAL ||
17327c478bd9Sstevel@tonic-gate 		    pool_value_get_uint64(&val1, &uval) != PO_SUCCESS) {
17337c478bd9Sstevel@tonic-gate 			free(rl);
17347c478bd9Sstevel@tonic-gate 			if (conf != dyn) {
17357c478bd9Sstevel@tonic-gate 				(void) pool_conf_close(dyn);
17367c478bd9Sstevel@tonic-gate 				pool_conf_free(dyn);
17377c478bd9Sstevel@tonic-gate 			}
17387c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
17397c478bd9Sstevel@tonic-gate 		}
17407c478bd9Sstevel@tonic-gate 		available += uval;
17417c478bd9Sstevel@tonic-gate 	}
17427c478bd9Sstevel@tonic-gate 	free(rl);
17437c478bd9Sstevel@tonic-gate 	if (conf != dyn) {
17447c478bd9Sstevel@tonic-gate 		(void) pool_conf_close(dyn);
17457c478bd9Sstevel@tonic-gate 		pool_conf_free(dyn);
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 	if (strcmp(prop, c_min_prop) == 0) {
17487c478bd9Sstevel@tonic-gate 		if (available < required) {
17497c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
17507c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
17517c478bd9Sstevel@tonic-gate 		}
17527c478bd9Sstevel@tonic-gate 	} else {
17537c478bd9Sstevel@tonic-gate 		if (available > required) {
17547c478bd9Sstevel@tonic-gate 			pool_seterror(POE_INVALID_CONF);
17557c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
17567c478bd9Sstevel@tonic-gate 		}
17577c478bd9Sstevel@tonic-gate 	}
17587c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
17597c478bd9Sstevel@tonic-gate }
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate /*
17627c478bd9Sstevel@tonic-gate  * If _libpool_debug is set, printf the debug message to stderr with an
17637c478bd9Sstevel@tonic-gate  * appropriate prefix in front of it.
17647c478bd9Sstevel@tonic-gate  */
17657c478bd9Sstevel@tonic-gate void
do_dprintf(const char * format,va_list ap)17667c478bd9Sstevel@tonic-gate do_dprintf(const char *format, va_list ap)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate 	if (_libpool_debug) {
17697c478bd9Sstevel@tonic-gate 		(void) fputs("libpool DEBUG: ", stderr);
17707c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, format, ap);
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
17757c478bd9Sstevel@tonic-gate void
dprintf(const char * format,...)17767c478bd9Sstevel@tonic-gate dprintf(const char *format, ...)
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate 	if (_libpool_debug) {
17797c478bd9Sstevel@tonic-gate 		va_list alist;
17807c478bd9Sstevel@tonic-gate 		va_start(alist, format);
17817c478bd9Sstevel@tonic-gate 		do_dprintf(format, alist);
17827c478bd9Sstevel@tonic-gate 		va_end(alist);
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate }
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate /*
17877c478bd9Sstevel@tonic-gate  * log_alloc() allocates a new, empty transaction log.
17887c478bd9Sstevel@tonic-gate  *
17897c478bd9Sstevel@tonic-gate  * Returns a pointer to the new log or NULL on failure.
17907c478bd9Sstevel@tonic-gate  */
17917c478bd9Sstevel@tonic-gate log_t *
log_alloc(pool_conf_t * conf)17927c478bd9Sstevel@tonic-gate log_alloc(pool_conf_t *conf)
17937c478bd9Sstevel@tonic-gate {
17947c478bd9Sstevel@tonic-gate 	log_t *l;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	if ((l = calloc(1, sizeof (log_t))) == NULL) {
17977c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
17987c478bd9Sstevel@tonic-gate 		return (NULL);
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 	l->l_state = LS_DO;
18017c478bd9Sstevel@tonic-gate 	l->l_conf = conf;
18027c478bd9Sstevel@tonic-gate 	if ((l->l_sentinel = log_item_alloc(l, 0, NULL))
18037c478bd9Sstevel@tonic-gate 	    == NULL) {
18047c478bd9Sstevel@tonic-gate 		free(l);
18057c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
18067c478bd9Sstevel@tonic-gate 		return (NULL);
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 	l->l_sentinel->li_next = l->l_sentinel;
18097c478bd9Sstevel@tonic-gate 	l->l_sentinel->li_prev = l->l_sentinel;
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	return (l);
18127c478bd9Sstevel@tonic-gate }
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate /*
18157c478bd9Sstevel@tonic-gate  * log_free() reclaims the resources associated with a transaction log.
18167c478bd9Sstevel@tonic-gate  */
18177c478bd9Sstevel@tonic-gate void
log_free(log_t * l)18187c478bd9Sstevel@tonic-gate log_free(log_t *l)
18197c478bd9Sstevel@tonic-gate {
18207c478bd9Sstevel@tonic-gate 	(void) log_walk(l, log_item_free);
18217c478bd9Sstevel@tonic-gate 	(void) log_item_free(l->l_sentinel);
18227c478bd9Sstevel@tonic-gate 	free(l);
18237c478bd9Sstevel@tonic-gate }
18247c478bd9Sstevel@tonic-gate /*
18257c478bd9Sstevel@tonic-gate  * log_empty() removes all items from a transaction log. It is the
18267c478bd9Sstevel@tonic-gate  * users responsibility to ensure that any resources associated with
18277c478bd9Sstevel@tonic-gate  * an item are reclaimed before this function is invoked.
18287c478bd9Sstevel@tonic-gate  */
18297c478bd9Sstevel@tonic-gate void
log_empty(log_t * l)18307c478bd9Sstevel@tonic-gate log_empty(log_t *l)
18317c478bd9Sstevel@tonic-gate {
18327c478bd9Sstevel@tonic-gate 	(void) log_walk(l, log_item_free);
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate /*
18367c478bd9Sstevel@tonic-gate  * log_walk() visits each log item in turn and executes the supplied action
18377c478bd9Sstevel@tonic-gate  * using the item as a parameter. If no action is supplied, then the item
18387c478bd9Sstevel@tonic-gate  * uses it's own stored action.
18397c478bd9Sstevel@tonic-gate  *
18407c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
18417c478bd9Sstevel@tonic-gate  */
18427c478bd9Sstevel@tonic-gate int
log_walk(log_t * l,log_item_action_t action)18437c478bd9Sstevel@tonic-gate log_walk(log_t *l, log_item_action_t action)
18447c478bd9Sstevel@tonic-gate {
18457c478bd9Sstevel@tonic-gate 	log_item_t *li, *li_next;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	li = l->l_sentinel->li_next;
18487c478bd9Sstevel@tonic-gate 	while (li != l->l_sentinel) {
18497c478bd9Sstevel@tonic-gate 		li_next = li->li_next;
18507c478bd9Sstevel@tonic-gate 		if ((action(li)) != PO_SUCCESS)
18517c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
18527c478bd9Sstevel@tonic-gate 		li = li_next;
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate /*
18587c478bd9Sstevel@tonic-gate  * log_reverse_walk() visits each log item in turn (in reverse order)
18597c478bd9Sstevel@tonic-gate  * and executes the supplied action using the item as a parameter.
18607c478bd9Sstevel@tonic-gate  *
18617c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
18627c478bd9Sstevel@tonic-gate  */
18637c478bd9Sstevel@tonic-gate int
log_reverse_walk(log_t * l,log_item_action_t action)18647c478bd9Sstevel@tonic-gate log_reverse_walk(log_t *l, log_item_action_t action)
18657c478bd9Sstevel@tonic-gate {
18667c478bd9Sstevel@tonic-gate 	log_item_t *li, *li_prev;
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	li = l->l_sentinel->li_prev;
18697c478bd9Sstevel@tonic-gate 	while (li != l->l_sentinel) {
18707c478bd9Sstevel@tonic-gate 		li_prev = li->li_prev;
18717c478bd9Sstevel@tonic-gate 		if ((action(li)) != PO_SUCCESS)
18727c478bd9Sstevel@tonic-gate 			return (PO_FAIL);
18737c478bd9Sstevel@tonic-gate 		li = li_prev;
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
18767c478bd9Sstevel@tonic-gate }
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate /*
18797c478bd9Sstevel@tonic-gate  * log_size() returns the size of the log, i.e. the number of items pending in
18807c478bd9Sstevel@tonic-gate  * the log.
18817c478bd9Sstevel@tonic-gate  */
18827c478bd9Sstevel@tonic-gate uint_t
log_size(log_t * l)18837c478bd9Sstevel@tonic-gate log_size(log_t *l)
18847c478bd9Sstevel@tonic-gate {
18857c478bd9Sstevel@tonic-gate 	log_item_t *li;
18867c478bd9Sstevel@tonic-gate 	uint_t size = 0;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	for (li = l->l_sentinel->li_next; li != l->l_sentinel; li = li->li_next)
18897c478bd9Sstevel@tonic-gate 		size++;
18907c478bd9Sstevel@tonic-gate 	return (size);
18917c478bd9Sstevel@tonic-gate }
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate /*
18947c478bd9Sstevel@tonic-gate  * log_append() allocates a new log item to hold the supplied details and
18957c478bd9Sstevel@tonic-gate  * appends the newly created item to the supplied log.
18967c478bd9Sstevel@tonic-gate  *
18977c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS/PO_FAIL
18987c478bd9Sstevel@tonic-gate  */
18997c478bd9Sstevel@tonic-gate int
log_append(log_t * l,int op,void * details)19007c478bd9Sstevel@tonic-gate log_append(log_t *l, int op, void *details)
19017c478bd9Sstevel@tonic-gate {
19027c478bd9Sstevel@tonic-gate 	log_item_t *li;
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	if ((li = log_item_alloc(l, op, details)) == NULL) {
19057c478bd9Sstevel@tonic-gate 		l->l_state = LS_UNDO;
19067c478bd9Sstevel@tonic-gate 		return (PO_FAIL);
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 	/*
19097c478bd9Sstevel@tonic-gate 	 * Link it in
19107c478bd9Sstevel@tonic-gate 	 */
19117c478bd9Sstevel@tonic-gate 	li->li_prev = l->l_sentinel->li_prev;
19127c478bd9Sstevel@tonic-gate 	li->li_next = l->l_sentinel;
19137c478bd9Sstevel@tonic-gate 	l->l_sentinel->li_prev->li_next = li;
19147c478bd9Sstevel@tonic-gate 	l->l_sentinel->li_prev = li;
19157c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate /*
19197c478bd9Sstevel@tonic-gate  * log_item_alloc() allocates a new transaction log item. The item should be
19207c478bd9Sstevel@tonic-gate  * used to store details about a transaction which may need to be undone if
19217c478bd9Sstevel@tonic-gate  * commit processing fails.
19227c478bd9Sstevel@tonic-gate  *
19237c478bd9Sstevel@tonic-gate  * Returns a pointer to a new transaction log item or NULL.
19247c478bd9Sstevel@tonic-gate  */
19257c478bd9Sstevel@tonic-gate log_item_t *
log_item_alloc(log_t * l,int op,void * details)19267c478bd9Sstevel@tonic-gate log_item_alloc(log_t *l, int op, void *details)
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate 	log_item_t *li;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	if ((li = malloc(sizeof (log_item_t))) == NULL) {
19317c478bd9Sstevel@tonic-gate 		pool_seterror(POE_SYSTEM);
19327c478bd9Sstevel@tonic-gate 		return (NULL);
19337c478bd9Sstevel@tonic-gate 	}
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	(void) memset(li, 0, sizeof (log_item_t));
19367c478bd9Sstevel@tonic-gate 	li->li_log = l;
19377c478bd9Sstevel@tonic-gate 	li->li_op = op;
19387c478bd9Sstevel@tonic-gate 	li->li_details = details;
19397c478bd9Sstevel@tonic-gate 	li->li_state = LS_DO;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	return (li);
19427c478bd9Sstevel@tonic-gate }
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate /*
19457c478bd9Sstevel@tonic-gate  * log_item_free() reclaims the resources associated with a log_item_t.
19467c478bd9Sstevel@tonic-gate  */
19477c478bd9Sstevel@tonic-gate int
log_item_free(log_item_t * li)19487c478bd9Sstevel@tonic-gate log_item_free(log_item_t *li)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	li->li_prev->li_next = li->li_next;
19517c478bd9Sstevel@tonic-gate 	li->li_next->li_prev = li->li_prev;
19527c478bd9Sstevel@tonic-gate 	free(li);
19537c478bd9Sstevel@tonic-gate 	return (PO_SUCCESS);
19547c478bd9Sstevel@tonic-gate }
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate /*
19577c478bd9Sstevel@tonic-gate  * atom_string() checks the string table to see if a string is already
19587c478bd9Sstevel@tonic-gate  * stored. If it is, return a pointer to it. If not, duplicate the
19597c478bd9Sstevel@tonic-gate  * string and return a pointer to the duplicate.
19607c478bd9Sstevel@tonic-gate  */
19617c478bd9Sstevel@tonic-gate const char *
atom_string(const char * s)19627c478bd9Sstevel@tonic-gate atom_string(const char *s)
19637c478bd9Sstevel@tonic-gate {
19647c478bd9Sstevel@tonic-gate 	atom_t *atom;
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	/*
19677c478bd9Sstevel@tonic-gate 	 * atom_init() must have completed successfully
19687c478bd9Sstevel@tonic-gate 	 */
19697c478bd9Sstevel@tonic-gate 	atom_init();
19707c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&_atom_lock);
19717c478bd9Sstevel@tonic-gate 	if ((atom = dict_get(_pv_atoms, s)) == NULL) {
19727c478bd9Sstevel@tonic-gate 		if ((atom = calloc(1, sizeof (atom_t))) == NULL) {
19737c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
19747c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&_atom_lock);
19757c478bd9Sstevel@tonic-gate 			return (NULL);
19767c478bd9Sstevel@tonic-gate 		}
19777c478bd9Sstevel@tonic-gate 		if ((atom->a_string = strdup(s)) == NULL) {
19787c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&_atom_lock);
19797c478bd9Sstevel@tonic-gate 			free(atom);
19807c478bd9Sstevel@tonic-gate 			pool_seterror(POE_SYSTEM);
19817c478bd9Sstevel@tonic-gate 			return (NULL);
19827c478bd9Sstevel@tonic-gate 		}
19837c478bd9Sstevel@tonic-gate 		(void) dict_put(_pv_atoms, atom->a_string, atom);
19847c478bd9Sstevel@tonic-gate 	}
19857c478bd9Sstevel@tonic-gate 	atom->a_count++;
19867c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&_atom_lock);
19877c478bd9Sstevel@tonic-gate 	return (atom->a_string);
19887c478bd9Sstevel@tonic-gate }
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate /*
19917c478bd9Sstevel@tonic-gate  * atom_free() decrements the reference count for the supplied
19927c478bd9Sstevel@tonic-gate  * string. If the reference count reaches zero, then the atom is
19937c478bd9Sstevel@tonic-gate  * destroyed.
19947c478bd9Sstevel@tonic-gate  */
19957c478bd9Sstevel@tonic-gate void
atom_free(const char * s)19967c478bd9Sstevel@tonic-gate atom_free(const char *s)
19977c478bd9Sstevel@tonic-gate {
19987c478bd9Sstevel@tonic-gate 	atom_t *atom;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&_atom_lock);
20017c478bd9Sstevel@tonic-gate 	if ((atom = dict_get(_pv_atoms, s)) != NULL) {
20027c478bd9Sstevel@tonic-gate 		if (--atom->a_count == 0) {
20037c478bd9Sstevel@tonic-gate 			(void) dict_remove(_pv_atoms, s);
20047c478bd9Sstevel@tonic-gate 			free(atom->a_string);
20057c478bd9Sstevel@tonic-gate 			free(atom);
20067c478bd9Sstevel@tonic-gate 		}
20077c478bd9Sstevel@tonic-gate 	}
20087c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&_atom_lock);
20097c478bd9Sstevel@tonic-gate }
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate #ifdef DEBUG
20127c478bd9Sstevel@tonic-gate /*
20137c478bd9Sstevel@tonic-gate  * log_item_dprintf() prints the contents of the supplied log item using the
20147c478bd9Sstevel@tonic-gate  * pools dprintf() trace mechanism.
20157c478bd9Sstevel@tonic-gate  *
20167c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS
20177c478bd9Sstevel@tonic-gate  */
20187c478bd9Sstevel@tonic-gate void
log_item_dprintf(log_item_t * li)20197c478bd9Sstevel@tonic-gate log_item_dprintf(log_item_t *li)
20207c478bd9Sstevel@tonic-gate {
20217c478bd9Sstevel@tonic-gate 	dprintf("LOGDUMP: %d operation, %p\n", li->li_op, li->li_details);
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate /*
20257c478bd9Sstevel@tonic-gate  * log_item_dprintf() prints the contents of the supplied log item using the
20267c478bd9Sstevel@tonic-gate  * pools dprintf() trace mechanism.
20277c478bd9Sstevel@tonic-gate  *
20287c478bd9Sstevel@tonic-gate  * Returns PO_SUCCESS
20297c478bd9Sstevel@tonic-gate  */
20307c478bd9Sstevel@tonic-gate void
pool_elem_dprintf(const pool_elem_t * pe)20317c478bd9Sstevel@tonic-gate pool_elem_dprintf(const pool_elem_t *pe)
20327c478bd9Sstevel@tonic-gate {
20337c478bd9Sstevel@tonic-gate 	if (pool_elem_class(pe) != PEC_COMP) {
20347c478bd9Sstevel@tonic-gate 		const char *name = elem_get_name(pe);
20357c478bd9Sstevel@tonic-gate 		dprintf("element type: %s name: %s\n",
20367c478bd9Sstevel@tonic-gate 		    pool_elem_class_string(pe), name);
20377c478bd9Sstevel@tonic-gate 		free((void *)name);
20387c478bd9Sstevel@tonic-gate 	} else {
20397c478bd9Sstevel@tonic-gate 		id_t sys_id = elem_get_sysid(pe);
20407c478bd9Sstevel@tonic-gate 		dprintf("element type: %s sys_id: %d\n",
20417c478bd9Sstevel@tonic-gate 		    pool_elem_class_string(pe), sys_id);
20427c478bd9Sstevel@tonic-gate 	}
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
2045