xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_ustat.c (revision d9638e547d8811f2c689977f8dd2a353938b61fd)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*d9638e54Smws 
237c478bd9Sstevel@tonic-gate /*
24*d9638e54Smws  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <fmd_ustat.h>
317c478bd9Sstevel@tonic-gate #include <fmd_alloc.h>
327c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
337c478bd9Sstevel@tonic-gate #include <fmd_string.h>
347c478bd9Sstevel@tonic-gate #include <fmd_error.h>
357c478bd9Sstevel@tonic-gate #include <fmd.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static fmd_ustat_chunk_t *
fmd_ustat_chunk_init(fmd_ustat_t * usp,fmd_stat_t * base,uint_t len)387c478bd9Sstevel@tonic-gate fmd_ustat_chunk_init(fmd_ustat_t *usp, fmd_stat_t *base, uint_t len)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	fmd_ustat_chunk_t *cp;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	cp = fmd_zalloc(sizeof (fmd_ustat_chunk_t), FMD_SLEEP);
437c478bd9Sstevel@tonic-gate 	cp->usc_base = base;
447c478bd9Sstevel@tonic-gate 	cp->usc_len = len;
457c478bd9Sstevel@tonic-gate 	cp->usc_refs = 1;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&usp->us_lock));
487c478bd9Sstevel@tonic-gate 	fmd_list_append(&usp->us_chunks, cp);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	return (cp);
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static void
fmd_ustat_chunk_hold(fmd_ustat_t * usp,fmd_ustat_chunk_t * cp)547c478bd9Sstevel@tonic-gate fmd_ustat_chunk_hold(fmd_ustat_t *usp, fmd_ustat_chunk_t *cp)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&usp->us_lock));
577c478bd9Sstevel@tonic-gate 	cp->usc_refs++;
587c478bd9Sstevel@tonic-gate 	ASSERT(cp->usc_refs != 0);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static void
fmd_ustat_chunk_rele(fmd_ustat_t * usp,fmd_ustat_chunk_t * cp)627c478bd9Sstevel@tonic-gate fmd_ustat_chunk_rele(fmd_ustat_t *usp, fmd_ustat_chunk_t *cp)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&usp->us_lock));
657c478bd9Sstevel@tonic-gate 	ASSERT(cp->usc_refs != 0);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	if (--cp->usc_refs == 0) {
687c478bd9Sstevel@tonic-gate 		/*
697c478bd9Sstevel@tonic-gate 		 * Note that any strings pointed to by FMD_TYPE_STRING stats
707c478bd9Sstevel@tonic-gate 		 * are freed one-by-one before releasing the chunk.  So here
717c478bd9Sstevel@tonic-gate 		 * we can just free the chunk and not worry about its content.
727c478bd9Sstevel@tonic-gate 		 */
737c478bd9Sstevel@tonic-gate 		fmd_free(cp->usc_base, sizeof (fmd_stat_t) * cp->usc_len);
747c478bd9Sstevel@tonic-gate 		fmd_list_delete(&usp->us_chunks, cp);
757c478bd9Sstevel@tonic-gate 		fmd_free(cp, sizeof (fmd_ustat_chunk_t));
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate fmd_ustat_t *
fmd_ustat_create(void)807c478bd9Sstevel@tonic-gate fmd_ustat_create(void)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	fmd_ustat_t *usp = fmd_zalloc(sizeof (fmd_ustat_t), FMD_SLEEP);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_init(&usp->us_lock, NULL);
857c478bd9Sstevel@tonic-gate 	usp->us_hashlen = fmd.d_str_buckets;
867c478bd9Sstevel@tonic-gate 	usp->us_hash = fmd_zalloc(sizeof (void *) * usp->us_hashlen, FMD_SLEEP);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	return (usp);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate void
fmd_ustat_destroy(fmd_ustat_t * usp)927c478bd9Sstevel@tonic-gate fmd_ustat_destroy(fmd_ustat_t *usp)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	fmd_ustat_elem_t *ep, *np;
957c478bd9Sstevel@tonic-gate 	uint_t i;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&usp->us_lock);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	for (i = 0; i < usp->us_hashlen; i++) {
1007c478bd9Sstevel@tonic-gate 		for (ep = usp->us_hash[i]; ep != NULL; ep = np) {
1017c478bd9Sstevel@tonic-gate 			if (ep->use_stat->fmds_type == FMD_TYPE_STRING)
1027c478bd9Sstevel@tonic-gate 				fmd_strfree(ep->use_stat->fmds_value.str);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 			if (ep->use_chunk != NULL)
1057c478bd9Sstevel@tonic-gate 				fmd_ustat_chunk_rele(usp, ep->use_chunk);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 			np = ep->use_next;
1087c478bd9Sstevel@tonic-gate 			fmd_free(ep, sizeof (fmd_ustat_elem_t));
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	ASSERT(usp->us_chunks.l_next == NULL);
1137c478bd9Sstevel@tonic-gate 	ASSERT(usp->us_chunks.l_prev == NULL);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	fmd_free(usp->us_hash, sizeof (void *) * usp->us_hashlen);
1167c478bd9Sstevel@tonic-gate 	fmd_free(usp, sizeof (fmd_ustat_t));
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate int
fmd_ustat_snapshot(fmd_ustat_t * usp,fmd_ustat_snap_t * uss)1207c478bd9Sstevel@tonic-gate fmd_ustat_snapshot(fmd_ustat_t *usp, fmd_ustat_snap_t *uss)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	const fmd_ustat_elem_t *ep;
1237c478bd9Sstevel@tonic-gate 	fmd_stat_t *sp;
1247c478bd9Sstevel@tonic-gate 	uint_t i;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&usp->us_lock);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	uss->uss_buf = sp = malloc(sizeof (fmd_stat_t) * usp->us_nelems);
1297c478bd9Sstevel@tonic-gate 	uss->uss_len = usp->us_nelems;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (uss->uss_buf == NULL) {
1327c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&usp->us_lock);
1337c478bd9Sstevel@tonic-gate 		return (fmd_set_errno(EFMD_STAT_NOMEM));
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	for (i = 0; i < usp->us_hashlen; i++) {
1377c478bd9Sstevel@tonic-gate 		for (ep = usp->us_hash[i]; ep != NULL; ep = ep->use_next) {
1387c478bd9Sstevel@tonic-gate 			bcopy(ep->use_stat, sp, sizeof (fmd_stat_t));
1397c478bd9Sstevel@tonic-gate 			if (sp->fmds_type == FMD_TYPE_STRING &&
1407c478bd9Sstevel@tonic-gate 			    sp->fmds_value.str != NULL)
1417c478bd9Sstevel@tonic-gate 				sp->fmds_value.str = strdup(sp->fmds_value.str);
1427c478bd9Sstevel@tonic-gate 			sp++;
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	ASSERT(sp == uss->uss_buf + uss->uss_len);
1477c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&usp->us_lock);
1487c478bd9Sstevel@tonic-gate 	return (0);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static void
fmd_ustat_delete_locked(fmd_ustat_t * usp,uint_t n,fmd_stat_t * sp,int strfree)1527c478bd9Sstevel@tonic-gate fmd_ustat_delete_locked(fmd_ustat_t *usp, uint_t n, fmd_stat_t *sp, int strfree)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&usp->us_lock));
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	for (; n-- != 0; sp++) {
1577c478bd9Sstevel@tonic-gate 		uint_t h = fmd_strhash(sp->fmds_name) % usp->us_hashlen;
1587c478bd9Sstevel@tonic-gate 		fmd_ustat_elem_t *ep, **pp = &usp->us_hash[h];
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		for (ep = *pp; ep != NULL; ep = ep->use_next) {
1617c478bd9Sstevel@tonic-gate 			if (strcmp(sp->fmds_name, ep->use_stat->fmds_name) != 0)
1627c478bd9Sstevel@tonic-gate 				pp = &ep->use_next;
1637c478bd9Sstevel@tonic-gate 			else
1647c478bd9Sstevel@tonic-gate 				break;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		if (ep == NULL)
1687c478bd9Sstevel@tonic-gate 			continue; /* silently ignore unregistered entries */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		if (strfree && ep->use_stat->fmds_type == FMD_TYPE_STRING)
1717c478bd9Sstevel@tonic-gate 			fmd_strfree(ep->use_stat->fmds_value.str);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		if (ep->use_chunk != NULL)
1747c478bd9Sstevel@tonic-gate 			fmd_ustat_chunk_rele(usp, ep->use_chunk);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		*pp = ep->use_next;
1777c478bd9Sstevel@tonic-gate 		fmd_free(ep, sizeof (fmd_ustat_elem_t));
1787c478bd9Sstevel@tonic-gate 		usp->us_nelems--;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate fmd_stat_t *
fmd_ustat_insert(fmd_ustat_t * usp,uint_t flags,uint_t n,fmd_stat_t * template,fmd_stat_t ** epp)1837c478bd9Sstevel@tonic-gate fmd_ustat_insert(fmd_ustat_t *usp, uint_t flags,
1847c478bd9Sstevel@tonic-gate     uint_t n, fmd_stat_t *template, fmd_stat_t **epp)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	fmd_stat_t *stats, *sp;
1877c478bd9Sstevel@tonic-gate 	fmd_ustat_chunk_t *cp;
1887c478bd9Sstevel@tonic-gate 	uint_t i;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	int checkid = flags & FMD_USTAT_VALIDATE;
1917c478bd9Sstevel@tonic-gate 	int has_str = 0;
1927c478bd9Sstevel@tonic-gate 	int err = 0;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if (flags & FMD_USTAT_ALLOC) {
1957c478bd9Sstevel@tonic-gate 		sp = stats = fmd_alloc(sizeof (fmd_stat_t) * n, FMD_SLEEP);
1967c478bd9Sstevel@tonic-gate 		bcopy(template, stats, sizeof (fmd_stat_t) * n);
1977c478bd9Sstevel@tonic-gate 	} else
1987c478bd9Sstevel@tonic-gate 		sp = stats = template;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&usp->us_lock);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (flags & FMD_USTAT_ALLOC)
2037c478bd9Sstevel@tonic-gate 		cp = fmd_ustat_chunk_init(usp, stats, n);
2047c478bd9Sstevel@tonic-gate 	else
2057c478bd9Sstevel@tonic-gate 		cp = NULL;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	for (i = 0; i < n; i++, sp++) {
2087c478bd9Sstevel@tonic-gate 		char *p, *q = sp->fmds_name + sizeof (sp->fmds_name);
2097c478bd9Sstevel@tonic-gate 		fmd_ustat_elem_t *ep;
2107c478bd9Sstevel@tonic-gate 		uint_t h;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		/*
2137c478bd9Sstevel@tonic-gate 		 * Since a module may be passing in this statistic and our
2147c478bd9Sstevel@tonic-gate 		 * names are represented by a fixed-size array, scan fmds_name
2157c478bd9Sstevel@tonic-gate 		 * to ensure it has a \0 somewhere before we attempt strcmps.
2167c478bd9Sstevel@tonic-gate 		 */
2177c478bd9Sstevel@tonic-gate 		for (p = sp->fmds_name; p < q; p++) {
2187c478bd9Sstevel@tonic-gate 			if (*p == '\0')
2197c478bd9Sstevel@tonic-gate 				break;
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		if (p == q)
2237c478bd9Sstevel@tonic-gate 			q[-1] = '\0'; /* nul-terminate for subsequent message */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		if (p == q || fmd_strbadid(sp->fmds_name, checkid) != NULL) {
2267c478bd9Sstevel@tonic-gate 			fmd_error(EFMD_STAT_BADNAME, "'%s' does not conform to "
2277c478bd9Sstevel@tonic-gate 			    "statistic naming rules\n", sp->fmds_name);
2287c478bd9Sstevel@tonic-gate 			err = fmd_set_errno(EFMD_STAT_BADNAME);
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		if (sp->fmds_type > FMD_TYPE_SIZE) {
2337c478bd9Sstevel@tonic-gate 			fmd_error(EFMD_STAT_BADTYPE, "'%s' statistic type %u "
2347c478bd9Sstevel@tonic-gate 			    "is not valid\n", sp->fmds_name, sp->fmds_type);
2357c478bd9Sstevel@tonic-gate 			err = fmd_set_errno(EFMD_STAT_BADTYPE);
2367c478bd9Sstevel@tonic-gate 			break;
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		if (sp->fmds_type == FMD_TYPE_STRING)
2407c478bd9Sstevel@tonic-gate 			has_str++; /* flag for second pass; see below */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		h = fmd_strhash(sp->fmds_name) % usp->us_hashlen;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		for (ep = usp->us_hash[h]; ep != NULL; ep = ep->use_next) {
2457c478bd9Sstevel@tonic-gate 			if (strcmp(sp->fmds_name, ep->use_stat->fmds_name) == 0)
2467c478bd9Sstevel@tonic-gate 				break;
2477c478bd9Sstevel@tonic-gate 		}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		if (ep != NULL) {
2507c478bd9Sstevel@tonic-gate 			fmd_error(EFMD_STAT_DUPNAME, "'%s' is already defined "
2517c478bd9Sstevel@tonic-gate 			    "as a statistic name\n", sp->fmds_name);
2527c478bd9Sstevel@tonic-gate 			err = fmd_set_errno(EFMD_STAT_DUPNAME);
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		ep = fmd_alloc(sizeof (fmd_ustat_elem_t), FMD_SLEEP);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		ep->use_next = usp->us_hash[h];
2597c478bd9Sstevel@tonic-gate 		usp->us_hash[h] = ep;
2607c478bd9Sstevel@tonic-gate 		ep->use_stat = sp;
2617c478bd9Sstevel@tonic-gate 		ep->use_chunk = cp;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		if (cp != NULL)
2647c478bd9Sstevel@tonic-gate 			fmd_ustat_chunk_hold(usp, cp);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		usp->us_nelems++;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * If an error occurred, delete all the stats inserted by successful
2717c478bd9Sstevel@tonic-gate 	 * iterations of the loop [0 .. i-1].  If 'epp' is non-NULL, store a
2727c478bd9Sstevel@tonic-gate 	 * copy of the input stat pointer that caused the error there.  When
2737c478bd9Sstevel@tonic-gate 	 * the delete is done, if we allocated a chunk, there should be only
2747c478bd9Sstevel@tonic-gate 	 * one reference remaining (from the initial fmd_ustat_chunk_init()).
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if (err != 0) {
2777c478bd9Sstevel@tonic-gate 		fmd_ustat_delete_locked(usp, i, stats, FMD_B_FALSE);
2787c478bd9Sstevel@tonic-gate 		ASSERT(cp == NULL || cp->usc_refs == 1);
2797c478bd9Sstevel@tonic-gate 		if (epp != NULL)
2807c478bd9Sstevel@tonic-gate 			*epp = template + i;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	} else if (has_str) {
2837c478bd9Sstevel@tonic-gate 		/*
2847c478bd9Sstevel@tonic-gate 		 * If no error occurred and one or more string stats are being
2857c478bd9Sstevel@tonic-gate 		 * inserted, make a second pass through 'stats' duplicating any
2867c478bd9Sstevel@tonic-gate 		 * initial strings so that fmd_stat_setstr() can alloc/free.
2877c478bd9Sstevel@tonic-gate 		 */
2887c478bd9Sstevel@tonic-gate 		for (sp = stats, i = 0; i < n; i++, sp++) {
2897c478bd9Sstevel@tonic-gate 			if (sp->fmds_type == FMD_TYPE_STRING &&
2907c478bd9Sstevel@tonic-gate 			    sp->fmds_value.str != NULL) {
2917c478bd9Sstevel@tonic-gate 				sp->fmds_value.str = fmd_strdup(
2927c478bd9Sstevel@tonic-gate 				    sp->fmds_value.str, FMD_SLEEP);
2937c478bd9Sstevel@tonic-gate 			}
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (cp != NULL)
2987c478bd9Sstevel@tonic-gate 		fmd_ustat_chunk_rele(usp, cp);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&usp->us_lock);
3017c478bd9Sstevel@tonic-gate 	return (err ? NULL : stats);
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate void
fmd_ustat_delete(fmd_ustat_t * usp,uint_t n,fmd_stat_t * sp)3057c478bd9Sstevel@tonic-gate fmd_ustat_delete(fmd_ustat_t *usp, uint_t n, fmd_stat_t *sp)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&usp->us_lock);
3087c478bd9Sstevel@tonic-gate 	fmd_ustat_delete_locked(usp, n, sp, FMD_B_TRUE);
3097c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&usp->us_lock);
3107c478bd9Sstevel@tonic-gate }
311*d9638e54Smws 
312*d9638e54Smws /*
313*d9638e54Smws  * Delete all statistics that are references to external memory (that is, all
314*d9638e54Smws  * statistics inserted with FMD_STAT_NOALLOC), i.e. a NULL ep->use_chunk.
315*d9638e54Smws  */
316*d9638e54Smws void
fmd_ustat_delete_references(fmd_ustat_t * usp)317*d9638e54Smws fmd_ustat_delete_references(fmd_ustat_t *usp)
318*d9638e54Smws {
319*d9638e54Smws 	fmd_ustat_elem_t *ep, **pp;
320*d9638e54Smws 	uint_t i;
321*d9638e54Smws 
322*d9638e54Smws 	(void) pthread_rwlock_wrlock(&usp->us_lock);
323*d9638e54Smws 
324*d9638e54Smws 	for (i = 0; i < usp->us_hashlen; i++) {
325*d9638e54Smws 		for (pp = &usp->us_hash[i], ep = *pp; ep != NULL; ep = *pp) {
326*d9638e54Smws 			if (ep->use_chunk != NULL) {
327*d9638e54Smws 				pp = &ep->use_next;
328*d9638e54Smws 				continue;
329*d9638e54Smws 			}
330*d9638e54Smws 
331*d9638e54Smws 			if (ep->use_stat->fmds_type == FMD_TYPE_STRING)
332*d9638e54Smws 				fmd_strfree(ep->use_stat->fmds_value.str);
333*d9638e54Smws 
334*d9638e54Smws 			*pp = ep->use_next;
335*d9638e54Smws 			fmd_free(ep, sizeof (fmd_ustat_elem_t));
336*d9638e54Smws 			usp->us_nelems--;
337*d9638e54Smws 		}
338*d9638e54Smws 	}
339*d9638e54Smws 
340*d9638e54Smws 	(void) pthread_rwlock_unlock(&usp->us_lock);
341*d9638e54Smws }
342