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