xref: /titanic_44/usr/src/uts/common/io/kstat.c (revision d4c279d363fa7ce2d1eb5f1c3b1db380eb09af48)
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
519397407SSherry Moore  * Common Development and Distribution License (the "License").
619397407SSherry Moore  * 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 /*
2219397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
245c712b3eSHans Rosenfeld  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * kernel statistics driver
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/time.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
387c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
397c478bd9Sstevel@tonic-gate #include <sys/proc.h>
407c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
417c478bd9Sstevel@tonic-gate #include <sys/uio.h>
427c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
437c478bd9Sstevel@tonic-gate #include <sys/cred.h>
447c478bd9Sstevel@tonic-gate #include <sys/mman.h>
457c478bd9Sstevel@tonic-gate #include <sys/errno.h>
467c478bd9Sstevel@tonic-gate #include <sys/ioccom.h>
477c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/conf.h>
507c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
527c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
537c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
547c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
557c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
567c478bd9Sstevel@tonic-gate #include <sys/policy.h>
577c478bd9Sstevel@tonic-gate #include <sys/zone.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static dev_info_t *kstat_devi;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static int
read_kstat_data(int * rvalp,void * user_ksp,int flag)627c478bd9Sstevel@tonic-gate read_kstat_data(int *rvalp, void *user_ksp, int flag)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	kstat_t user_kstat, *ksp;
657c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
667c478bd9Sstevel@tonic-gate 	kstat32_t user_kstat32;
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 	void *kbuf = NULL;
697c478bd9Sstevel@tonic-gate 	size_t kbufsize, ubufsize, copysize;
707c478bd9Sstevel@tonic-gate 	int error = 0;
717c478bd9Sstevel@tonic-gate 	uint_t model;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	switch (model = ddi_model_convert_from(flag & FMODELS)) {
747c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
757c478bd9Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
767c478bd9Sstevel@tonic-gate 		if (copyin(user_ksp, &user_kstat32, sizeof (kstat32_t)) != 0)
777c478bd9Sstevel@tonic-gate 			return (EFAULT);
787c478bd9Sstevel@tonic-gate 		user_kstat.ks_kid = user_kstat32.ks_kid;
797c478bd9Sstevel@tonic-gate 		user_kstat.ks_data = (void *)(uintptr_t)user_kstat32.ks_data;
807c478bd9Sstevel@tonic-gate 		user_kstat.ks_data_size = (size_t)user_kstat32.ks_data_size;
817c478bd9Sstevel@tonic-gate 		break;
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate 	default:
847c478bd9Sstevel@tonic-gate 	case DDI_MODEL_NONE:
857c478bd9Sstevel@tonic-gate 		if (copyin(user_ksp, &user_kstat, sizeof (kstat_t)) != 0)
867c478bd9Sstevel@tonic-gate 			return (EFAULT);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	ksp = kstat_hold_bykid(user_kstat.ks_kid, getzoneid());
907c478bd9Sstevel@tonic-gate 	if (ksp == NULL) {
917c478bd9Sstevel@tonic-gate 		/*
927c478bd9Sstevel@tonic-gate 		 * There is no kstat with the specified KID
937c478bd9Sstevel@tonic-gate 		 */
947c478bd9Sstevel@tonic-gate 		return (ENXIO);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_INVALID) {
977c478bd9Sstevel@tonic-gate 		/*
987c478bd9Sstevel@tonic-gate 		 * The kstat exists, but is momentarily in some
997c478bd9Sstevel@tonic-gate 		 * indeterminate state (e.g. the data section is not
1007c478bd9Sstevel@tonic-gate 		 * yet initialized).  Try again in a few milliseconds.
1017c478bd9Sstevel@tonic-gate 		 */
1027c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
1037c478bd9Sstevel@tonic-gate 		return (EAGAIN);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/*
1077c478bd9Sstevel@tonic-gate 	 * If it's a fixed-size kstat, allocate the buffer now, so we
1087c478bd9Sstevel@tonic-gate 	 * don't have to do it under the kstat's data lock.  (If it's a
109*d4c279d3SHans Rosenfeld 	 * var-size kstat or one with long strings, we don't know the size
110*d4c279d3SHans Rosenfeld 	 * until after the update routine is called, so we can't do this
111*d4c279d3SHans Rosenfeld 	 * optimization.)
1127c478bd9Sstevel@tonic-gate 	 * The allocator relies on this behavior to prevent recursive
1137c478bd9Sstevel@tonic-gate 	 * mutex_enter in its (fixed-size) kstat update routine.
1147c478bd9Sstevel@tonic-gate 	 * It's a zalloc to prevent unintentional exposure of random
1157c478bd9Sstevel@tonic-gate 	 * juicy morsels of (old) kernel data.
1167c478bd9Sstevel@tonic-gate 	 */
117*d4c279d3SHans Rosenfeld 	if (!(ksp->ks_flags & (KSTAT_FLAG_VAR_SIZE | KSTAT_FLAG_LONGSTRINGS))) {
1187c478bd9Sstevel@tonic-gate 		kbufsize = ksp->ks_data_size;
1197c478bd9Sstevel@tonic-gate 		kbuf = kmem_zalloc(kbufsize + 1, KM_NOSLEEP);
1207c478bd9Sstevel@tonic-gate 		if (kbuf == NULL) {
1217c478bd9Sstevel@tonic-gate 			kstat_rele(ksp);
1227c478bd9Sstevel@tonic-gate 			return (EAGAIN);
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	KSTAT_ENTER(ksp);
1267c478bd9Sstevel@tonic-gate 	if ((error = KSTAT_UPDATE(ksp, KSTAT_READ)) != 0) {
1277c478bd9Sstevel@tonic-gate 		KSTAT_EXIT(ksp);
1287c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
1297c478bd9Sstevel@tonic-gate 		if (kbuf != NULL)
1307c478bd9Sstevel@tonic-gate 			kmem_free(kbuf, kbufsize + 1);
1317c478bd9Sstevel@tonic-gate 		return (error);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	kbufsize = ksp->ks_data_size;
1357c478bd9Sstevel@tonic-gate 	ubufsize = user_kstat.ks_data_size;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (ubufsize < kbufsize) {
1387c478bd9Sstevel@tonic-gate 		error = ENOMEM;
1397c478bd9Sstevel@tonic-gate 	} else {
1407c478bd9Sstevel@tonic-gate 		if (kbuf == NULL)
1417c478bd9Sstevel@tonic-gate 			kbuf = kmem_zalloc(kbufsize + 1, KM_NOSLEEP);
1427c478bd9Sstevel@tonic-gate 		if (kbuf == NULL) {
1437c478bd9Sstevel@tonic-gate 			error = EAGAIN;
1447c478bd9Sstevel@tonic-gate 		} else {
1457c478bd9Sstevel@tonic-gate 			error = KSTAT_SNAPSHOT(ksp, kbuf, KSTAT_READ);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * The following info must be returned to user level,
1517c478bd9Sstevel@tonic-gate 	 * even if the the update or snapshot failed.  This allows
1527c478bd9Sstevel@tonic-gate 	 * kstat readers to get a handle on variable-size kstats,
1537c478bd9Sstevel@tonic-gate 	 * detect dormant kstats, etc.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	user_kstat.ks_ndata	= ksp->ks_ndata;
1567c478bd9Sstevel@tonic-gate 	user_kstat.ks_data_size	= kbufsize;
1577c478bd9Sstevel@tonic-gate 	user_kstat.ks_flags	= ksp->ks_flags;
1587c478bd9Sstevel@tonic-gate 	user_kstat.ks_snaptime	= ksp->ks_snaptime;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	*rvalp = kstat_chain_id;
1617c478bd9Sstevel@tonic-gate 	KSTAT_EXIT(ksp);
1627c478bd9Sstevel@tonic-gate 	kstat_rele(ksp);
1637c478bd9Sstevel@tonic-gate 
1645c712b3eSHans Rosenfeld 	if (kbuf == NULL)
1655c712b3eSHans Rosenfeld 		goto out;
1665c712b3eSHans Rosenfeld 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * Copy the buffer containing the kstat back to userland.
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	copysize = kbufsize;
1715c712b3eSHans Rosenfeld 
1725c712b3eSHans Rosenfeld 	switch (model) {
1735c712b3eSHans Rosenfeld 	int i;
1747c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1757c478bd9Sstevel@tonic-gate 	kstat32_t *k32;
1767c478bd9Sstevel@tonic-gate 	kstat_t *k;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		if (ksp->ks_type == KSTAT_TYPE_NAMED) {
1817c478bd9Sstevel@tonic-gate 			kstat_named_t *kn = kbuf;
182*d4c279d3SHans Rosenfeld 			char *strbuf = (char *)((kstat_named_t *)kn +
183*d4c279d3SHans Rosenfeld 			    ksp->ks_ndata);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 			for (i = 0; i < user_kstat.ks_ndata; kn++, i++)
1867c478bd9Sstevel@tonic-gate 				switch (kn->data_type) {
1877c478bd9Sstevel@tonic-gate 				/*
1885c712b3eSHans Rosenfeld 				 * Named statistics have fields of type 'long'.
1895c712b3eSHans Rosenfeld 				 * For a 32-bit application looking at a 64-bit
1905c712b3eSHans Rosenfeld 				 * kernel, forcibly truncate these 64-bit
1917c478bd9Sstevel@tonic-gate 				 * quantities to 32-bit values.
1927c478bd9Sstevel@tonic-gate 				 */
1937c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_LONG:
1945c712b3eSHans Rosenfeld 					kn->value.i32 = (int32_t)kn->value.l;
1955c712b3eSHans Rosenfeld 					kn->data_type = KSTAT_DATA_INT32;
1967c478bd9Sstevel@tonic-gate 					break;
1977c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_ULONG:
1985c712b3eSHans Rosenfeld 					kn->value.ui32 = (uint32_t)kn->value.ul;
1995c712b3eSHans Rosenfeld 					kn->data_type = KSTAT_DATA_UINT32;
2007c478bd9Sstevel@tonic-gate 					break;
2017c478bd9Sstevel@tonic-gate 				/*
2025c712b3eSHans Rosenfeld 				 * Long strings must be massaged before being
2035c712b3eSHans Rosenfeld 				 * copied out to userland.  Do that here.
2047c478bd9Sstevel@tonic-gate 				 */
2057c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_STRING:
2065c712b3eSHans Rosenfeld 					if (KSTAT_NAMED_STR_PTR(kn) == NULL)
2077c478bd9Sstevel@tonic-gate 						break;
2087c478bd9Sstevel@tonic-gate 					/*
209*d4c279d3SHans Rosenfeld 					 * If the string lies outside of kbuf
210*d4c279d3SHans Rosenfeld 					 * copy it there and update the pointer.
211*d4c279d3SHans Rosenfeld 					 */
212*d4c279d3SHans Rosenfeld 					if (KSTAT_NAMED_STR_PTR(kn) <
213*d4c279d3SHans Rosenfeld 					    (char *)kbuf ||
214*d4c279d3SHans Rosenfeld 					    KSTAT_NAMED_STR_PTR(kn) +
215*d4c279d3SHans Rosenfeld 					    KSTAT_NAMED_STR_BUFLEN(kn) >
216*d4c279d3SHans Rosenfeld 					    (char *)kbuf + kbufsize + 1) {
217*d4c279d3SHans Rosenfeld 						bcopy(KSTAT_NAMED_STR_PTR(kn),
218*d4c279d3SHans Rosenfeld 						    strbuf,
219*d4c279d3SHans Rosenfeld 						    KSTAT_NAMED_STR_BUFLEN(kn));
220*d4c279d3SHans Rosenfeld 
221*d4c279d3SHans Rosenfeld 						KSTAT_NAMED_STR_PTR(kn) =
222*d4c279d3SHans Rosenfeld 						    strbuf;
223*d4c279d3SHans Rosenfeld 						strbuf +=
224*d4c279d3SHans Rosenfeld 						    KSTAT_NAMED_STR_BUFLEN(kn);
225*d4c279d3SHans Rosenfeld 						ASSERT(strbuf <=
226*d4c279d3SHans Rosenfeld 						    (char *)kbuf +
227*d4c279d3SHans Rosenfeld 						    kbufsize + 1);
228*d4c279d3SHans Rosenfeld 					}
229*d4c279d3SHans Rosenfeld 					/*
2305c712b3eSHans Rosenfeld 					 * The offsets within the buffers are
2315c712b3eSHans Rosenfeld 					 * the same, so add the offset to the
2325c712b3eSHans Rosenfeld 					 * beginning of the new buffer to fix
2335c712b3eSHans Rosenfeld 					 * the pointer.
2347c478bd9Sstevel@tonic-gate 					 */
2357c478bd9Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(kn) =
2367c478bd9Sstevel@tonic-gate 					    (char *)user_kstat.ks_data +
2377c478bd9Sstevel@tonic-gate 					    (KSTAT_NAMED_STR_PTR(kn) -
2387c478bd9Sstevel@tonic-gate 					    (char *)kbuf);
2397c478bd9Sstevel@tonic-gate 					/*
2405c712b3eSHans Rosenfeld 					 * Make sure the string pointer lies
2415c712b3eSHans Rosenfeld 					 * within the allocated buffer.
2427c478bd9Sstevel@tonic-gate 					 */
2437c478bd9Sstevel@tonic-gate 					ASSERT(KSTAT_NAMED_STR_PTR(kn) +
2445c712b3eSHans Rosenfeld 					    KSTAT_NAMED_STR_BUFLEN(kn) <=
2455c712b3eSHans Rosenfeld 					    ((char *)user_kstat.ks_data +
2467c478bd9Sstevel@tonic-gate 					    ubufsize));
2475c712b3eSHans Rosenfeld 					ASSERT(KSTAT_NAMED_STR_PTR(kn) >=
2485c712b3eSHans Rosenfeld 					    (char *)((kstat_named_t *)
2497c478bd9Sstevel@tonic-gate 					    user_kstat.ks_data +
2507c478bd9Sstevel@tonic-gate 					    user_kstat.ks_ndata));
2517c478bd9Sstevel@tonic-gate 					/*
2527c478bd9Sstevel@tonic-gate 					 * Cast 64-bit ptr to 32-bit.
2537c478bd9Sstevel@tonic-gate 					 */
254a1b5e537Sbmc 					kn->value.str.addr.ptr32 =
2557c478bd9Sstevel@tonic-gate 					    (caddr32_t)(uintptr_t)
2567c478bd9Sstevel@tonic-gate 					    KSTAT_NAMED_STR_PTR(kn);
2577c478bd9Sstevel@tonic-gate 					break;
2587c478bd9Sstevel@tonic-gate 				default:
2597c478bd9Sstevel@tonic-gate 					break;
2607c478bd9Sstevel@tonic-gate 				}
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		if (user_kstat.ks_kid != 0)
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		/*
2677c478bd9Sstevel@tonic-gate 		 * This is the special case of the kstat header
2687c478bd9Sstevel@tonic-gate 		 * list for the entire system.  Reshape the
2697c478bd9Sstevel@tonic-gate 		 * array in place, then copy it out.
2707c478bd9Sstevel@tonic-gate 		 */
2717c478bd9Sstevel@tonic-gate 		k32 = kbuf;
2727c478bd9Sstevel@tonic-gate 		k = kbuf;
2737c478bd9Sstevel@tonic-gate 		for (i = 0; i < user_kstat.ks_ndata; k32++, k++, i++) {
2747c478bd9Sstevel@tonic-gate 			k32->ks_crtime		= k->ks_crtime;
2757c478bd9Sstevel@tonic-gate 			k32->ks_next		= 0;
2767c478bd9Sstevel@tonic-gate 			k32->ks_kid		= k->ks_kid;
2777c478bd9Sstevel@tonic-gate 			(void) strcpy(k32->ks_module, k->ks_module);
2787c478bd9Sstevel@tonic-gate 			k32->ks_resv		= k->ks_resv;
2797c478bd9Sstevel@tonic-gate 			k32->ks_instance	= k->ks_instance;
2807c478bd9Sstevel@tonic-gate 			(void) strcpy(k32->ks_name, k->ks_name);
2817c478bd9Sstevel@tonic-gate 			k32->ks_type		= k->ks_type;
2827c478bd9Sstevel@tonic-gate 			(void) strcpy(k32->ks_class, k->ks_class);
2837c478bd9Sstevel@tonic-gate 			k32->ks_flags		= k->ks_flags;
2847c478bd9Sstevel@tonic-gate 			k32->ks_data		= 0;
2857c478bd9Sstevel@tonic-gate 			k32->ks_ndata		= k->ks_ndata;
2867c478bd9Sstevel@tonic-gate 			if (k->ks_data_size > UINT32_MAX) {
2877c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
2887c478bd9Sstevel@tonic-gate 				break;
2897c478bd9Sstevel@tonic-gate 			}
2907c478bd9Sstevel@tonic-gate 			k32->ks_data_size = (size32_t)k->ks_data_size;
2917c478bd9Sstevel@tonic-gate 			k32->ks_snaptime	= k->ks_snaptime;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		/*
2957c478bd9Sstevel@tonic-gate 		 * XXX	In this case we copy less data than is
2967c478bd9Sstevel@tonic-gate 		 *	claimed in the header.
2977c478bd9Sstevel@tonic-gate 		 */
2987c478bd9Sstevel@tonic-gate 		copysize = user_kstat.ks_ndata * sizeof (kstat32_t);
2997c478bd9Sstevel@tonic-gate 		break;
3007c478bd9Sstevel@tonic-gate #endif	/* _MULTI_DATAMODEL */
3017c478bd9Sstevel@tonic-gate 	default:
3027c478bd9Sstevel@tonic-gate 	case DDI_MODEL_NONE:
3037c478bd9Sstevel@tonic-gate 		if (ksp->ks_type == KSTAT_TYPE_NAMED) {
3047c478bd9Sstevel@tonic-gate 			kstat_named_t *kn = kbuf;
305*d4c279d3SHans Rosenfeld 			char *strbuf = (char *)((kstat_named_t *)kn +
306*d4c279d3SHans Rosenfeld 			    ksp->ks_ndata);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 			for (i = 0; i < user_kstat.ks_ndata; kn++, i++)
3097c478bd9Sstevel@tonic-gate 				switch (kn->data_type) {
3107c478bd9Sstevel@tonic-gate #ifdef _LP64
3117c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_LONG:
3127c478bd9Sstevel@tonic-gate 					kn->data_type =
3137c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_INT64;
3147c478bd9Sstevel@tonic-gate 					break;
3157c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_ULONG:
3167c478bd9Sstevel@tonic-gate 					kn->data_type =
3177c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_UINT64;
3187c478bd9Sstevel@tonic-gate 					break;
3197c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
3207c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_STRING:
3215c712b3eSHans Rosenfeld 					if (KSTAT_NAMED_STR_PTR(kn) == NULL)
3227c478bd9Sstevel@tonic-gate 						break;
323*d4c279d3SHans Rosenfeld 					/*
324*d4c279d3SHans Rosenfeld 					 * If the string lies outside of kbuf
325*d4c279d3SHans Rosenfeld 					 * copy it there and update the pointer.
326*d4c279d3SHans Rosenfeld 					 */
327*d4c279d3SHans Rosenfeld 					if (KSTAT_NAMED_STR_PTR(kn) <
328*d4c279d3SHans Rosenfeld 					    (char *)kbuf ||
329*d4c279d3SHans Rosenfeld 					    KSTAT_NAMED_STR_PTR(kn) +
330*d4c279d3SHans Rosenfeld 					    KSTAT_NAMED_STR_BUFLEN(kn) >
331*d4c279d3SHans Rosenfeld 					    (char *)kbuf + kbufsize + 1) {
332*d4c279d3SHans Rosenfeld 						bcopy(KSTAT_NAMED_STR_PTR(kn),
333*d4c279d3SHans Rosenfeld 						    strbuf,
334*d4c279d3SHans Rosenfeld 						    KSTAT_NAMED_STR_BUFLEN(kn));
335*d4c279d3SHans Rosenfeld 
336*d4c279d3SHans Rosenfeld 						KSTAT_NAMED_STR_PTR(kn) =
337*d4c279d3SHans Rosenfeld 						    strbuf;
338*d4c279d3SHans Rosenfeld 						strbuf +=
339*d4c279d3SHans Rosenfeld 						    KSTAT_NAMED_STR_BUFLEN(kn);
340*d4c279d3SHans Rosenfeld 						ASSERT(strbuf <=
341*d4c279d3SHans Rosenfeld 						    (char *)kbuf +
342*d4c279d3SHans Rosenfeld 						    kbufsize + 1);
343*d4c279d3SHans Rosenfeld 					}
344*d4c279d3SHans Rosenfeld 
3457c478bd9Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(kn) =
3467c478bd9Sstevel@tonic-gate 					    (char *)user_kstat.ks_data +
3477c478bd9Sstevel@tonic-gate 					    (KSTAT_NAMED_STR_PTR(kn) -
3487c478bd9Sstevel@tonic-gate 					    (char *)kbuf);
3497c478bd9Sstevel@tonic-gate 					ASSERT(KSTAT_NAMED_STR_PTR(kn) +
3505c712b3eSHans Rosenfeld 					    KSTAT_NAMED_STR_BUFLEN(kn) <=
3515c712b3eSHans Rosenfeld 					    ((char *)user_kstat.ks_data +
3527c478bd9Sstevel@tonic-gate 					    ubufsize));
3535c712b3eSHans Rosenfeld 					ASSERT(KSTAT_NAMED_STR_PTR(kn) >=
3545c712b3eSHans Rosenfeld 					    (char *)((kstat_named_t *)
3557c478bd9Sstevel@tonic-gate 					    user_kstat.ks_data +
3567c478bd9Sstevel@tonic-gate 					    user_kstat.ks_ndata));
3577c478bd9Sstevel@tonic-gate 					break;
3587c478bd9Sstevel@tonic-gate 				default:
3597c478bd9Sstevel@tonic-gate 					break;
3607c478bd9Sstevel@tonic-gate 				}
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 		break;
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (error == 0 &&
3667c478bd9Sstevel@tonic-gate 	    copyout(kbuf, user_kstat.ks_data, copysize))
3677c478bd9Sstevel@tonic-gate 		error = EFAULT;
3687c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, kbufsize + 1);
3697c478bd9Sstevel@tonic-gate 
3705c712b3eSHans Rosenfeld out:
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * We have modified the ks_ndata, ks_data_size, ks_flags, and
3737c478bd9Sstevel@tonic-gate 	 * ks_snaptime fields of the user kstat; now copy it back to userland.
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	switch (model) {
3767c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
3777c478bd9Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
3787c478bd9Sstevel@tonic-gate 		if (kbufsize > UINT32_MAX) {
3797c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 		user_kstat32.ks_ndata		= user_kstat.ks_ndata;
3837c478bd9Sstevel@tonic-gate 		user_kstat32.ks_data_size	= (size32_t)kbufsize;
3847c478bd9Sstevel@tonic-gate 		user_kstat32.ks_flags		= user_kstat.ks_flags;
3857c478bd9Sstevel@tonic-gate 		user_kstat32.ks_snaptime	= user_kstat.ks_snaptime;
3867c478bd9Sstevel@tonic-gate 		if (copyout(&user_kstat32, user_ksp, sizeof (kstat32_t)) &&
3877c478bd9Sstevel@tonic-gate 		    error == 0)
3887c478bd9Sstevel@tonic-gate 			error = EFAULT;
3897c478bd9Sstevel@tonic-gate 		break;
3907c478bd9Sstevel@tonic-gate #endif
3917c478bd9Sstevel@tonic-gate 	default:
3927c478bd9Sstevel@tonic-gate 	case DDI_MODEL_NONE:
3937c478bd9Sstevel@tonic-gate 		if (copyout(&user_kstat, user_ksp, sizeof (kstat_t)) &&
3947c478bd9Sstevel@tonic-gate 		    error == 0)
3957c478bd9Sstevel@tonic-gate 			error = EFAULT;
3967c478bd9Sstevel@tonic-gate 		break;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	return (error);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate static int
write_kstat_data(int * rvalp,void * user_ksp,int flag,cred_t * cred)4037c478bd9Sstevel@tonic-gate write_kstat_data(int *rvalp, void *user_ksp, int flag, cred_t *cred)
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	kstat_t user_kstat, *ksp;
4067c478bd9Sstevel@tonic-gate 	void *buf = NULL;
4077c478bd9Sstevel@tonic-gate 	size_t bufsize;
4087c478bd9Sstevel@tonic-gate 	int error = 0;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (secpolicy_sys_config(cred, B_FALSE) != 0)
4117c478bd9Sstevel@tonic-gate 		return (EPERM);
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	switch (ddi_model_convert_from(flag & FMODELS)) {
4147c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
4157c478bd9Sstevel@tonic-gate 		kstat32_t user_kstat32;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
4187c478bd9Sstevel@tonic-gate 		if (copyin(user_ksp, &user_kstat32, sizeof (kstat32_t)))
4197c478bd9Sstevel@tonic-gate 			return (EFAULT);
4207c478bd9Sstevel@tonic-gate 		/*
4217c478bd9Sstevel@tonic-gate 		 * These are the only fields we actually look at.
4227c478bd9Sstevel@tonic-gate 		 */
4237c478bd9Sstevel@tonic-gate 		user_kstat.ks_kid = user_kstat32.ks_kid;
4247c478bd9Sstevel@tonic-gate 		user_kstat.ks_data = (void *)(uintptr_t)user_kstat32.ks_data;
4257c478bd9Sstevel@tonic-gate 		user_kstat.ks_data_size = (size_t)user_kstat32.ks_data_size;
4267c478bd9Sstevel@tonic-gate 		user_kstat.ks_ndata = user_kstat32.ks_ndata;
4277c478bd9Sstevel@tonic-gate 		break;
4287c478bd9Sstevel@tonic-gate #endif
4297c478bd9Sstevel@tonic-gate 	default:
4307c478bd9Sstevel@tonic-gate 	case DDI_MODEL_NONE:
4317c478bd9Sstevel@tonic-gate 		if (copyin(user_ksp, &user_kstat, sizeof (kstat_t)))
4327c478bd9Sstevel@tonic-gate 			return (EFAULT);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	bufsize = user_kstat.ks_data_size;
4367c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(bufsize + 1, KM_NOSLEEP);
4377c478bd9Sstevel@tonic-gate 	if (buf == NULL)
4387c478bd9Sstevel@tonic-gate 		return (EAGAIN);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if (copyin(user_kstat.ks_data, buf, bufsize)) {
4417c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4427c478bd9Sstevel@tonic-gate 		return (EFAULT);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	ksp = kstat_hold_bykid(user_kstat.ks_kid, getzoneid());
4467c478bd9Sstevel@tonic-gate 	if (ksp == NULL) {
4477c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4487c478bd9Sstevel@tonic-gate 		return (ENXIO);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_INVALID) {
4517c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
4527c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4537c478bd9Sstevel@tonic-gate 		return (EAGAIN);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	if (!(ksp->ks_flags & KSTAT_FLAG_WRITABLE)) {
4567c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
4577c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4587c478bd9Sstevel@tonic-gate 		return (EACCES);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4625c712b3eSHans Rosenfeld 	 * With KSTAT_FLAG_VAR_SIZE, one must call the kstat's update callback
4637c478bd9Sstevel@tonic-gate 	 * routine to ensure ks_data_size is up to date.
4647c478bd9Sstevel@tonic-gate 	 * In this case it makes sense to do it anyhow, as it will be shortly
4657c478bd9Sstevel@tonic-gate 	 * followed by a KSTAT_SNAPSHOT().
4667c478bd9Sstevel@tonic-gate 	 */
4677c478bd9Sstevel@tonic-gate 	KSTAT_ENTER(ksp);
4687c478bd9Sstevel@tonic-gate 	error = KSTAT_UPDATE(ksp, KSTAT_READ);
4697c478bd9Sstevel@tonic-gate 	if (error || user_kstat.ks_data_size != ksp->ks_data_size ||
4707c478bd9Sstevel@tonic-gate 	    user_kstat.ks_ndata != ksp->ks_ndata) {
4717c478bd9Sstevel@tonic-gate 		KSTAT_EXIT(ksp);
4727c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
4737c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4747c478bd9Sstevel@tonic-gate 		return (error ? error : EINVAL);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * We have to ensure that we don't accidentally change the type of
4797c478bd9Sstevel@tonic-gate 	 * existing kstat_named statistics when writing over them.
4807c478bd9Sstevel@tonic-gate 	 * Since read_kstat_data() modifies some of the types on their way
4817c478bd9Sstevel@tonic-gate 	 * out, we need to be sure to handle these types seperately.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (ksp->ks_type == KSTAT_TYPE_NAMED) {
4847c478bd9Sstevel@tonic-gate 		void *kbuf;
4857c478bd9Sstevel@tonic-gate 		kstat_named_t *kold;
4867c478bd9Sstevel@tonic-gate 		kstat_named_t *knew = buf;
4877c478bd9Sstevel@tonic-gate 		int i;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
4907c478bd9Sstevel@tonic-gate 		int model = ddi_model_convert_from(flag & FMODELS);
4917c478bd9Sstevel@tonic-gate #endif
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		/*
4947c478bd9Sstevel@tonic-gate 		 * Since ksp->ks_data may be NULL, we need to take a snapshot
4957c478bd9Sstevel@tonic-gate 		 * of the published data to look at the types.
4967c478bd9Sstevel@tonic-gate 		 */
4977c478bd9Sstevel@tonic-gate 		kbuf = kmem_alloc(bufsize + 1, KM_NOSLEEP);
4987c478bd9Sstevel@tonic-gate 		if (kbuf == NULL) {
4997c478bd9Sstevel@tonic-gate 			KSTAT_EXIT(ksp);
5007c478bd9Sstevel@tonic-gate 			kstat_rele(ksp);
5017c478bd9Sstevel@tonic-gate 			kmem_free(buf, bufsize + 1);
5027c478bd9Sstevel@tonic-gate 			return (EAGAIN);
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 		error = KSTAT_SNAPSHOT(ksp, kbuf, KSTAT_READ);
5057c478bd9Sstevel@tonic-gate 		if (error) {
5067c478bd9Sstevel@tonic-gate 			KSTAT_EXIT(ksp);
5077c478bd9Sstevel@tonic-gate 			kstat_rele(ksp);
5087c478bd9Sstevel@tonic-gate 			kmem_free(kbuf, bufsize + 1);
5097c478bd9Sstevel@tonic-gate 			kmem_free(buf, bufsize + 1);
5107c478bd9Sstevel@tonic-gate 			return (error);
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 		kold = kbuf;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		/*
5157c478bd9Sstevel@tonic-gate 		 * read_kstat_data() changes the types of
5167c478bd9Sstevel@tonic-gate 		 * KSTAT_DATA_LONG / KSTAT_DATA_ULONG, so we need to
5177c478bd9Sstevel@tonic-gate 		 * make sure that these (modified) types are considered
5187c478bd9Sstevel@tonic-gate 		 * valid.
5197c478bd9Sstevel@tonic-gate 		 */
5207c478bd9Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, kold++, knew++) {
5217c478bd9Sstevel@tonic-gate 			switch (kold->data_type) {
5227c478bd9Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
5237c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_LONG:
5247c478bd9Sstevel@tonic-gate 				switch (model) {
5257c478bd9Sstevel@tonic-gate 				case DDI_MODEL_ILP32:
5267c478bd9Sstevel@tonic-gate 					if (knew->data_type ==
5277c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_INT32) {
5287c478bd9Sstevel@tonic-gate 						knew->value.l =
5297c478bd9Sstevel@tonic-gate 						    (long)knew->value.i32;
5307c478bd9Sstevel@tonic-gate 						knew->data_type =
5317c478bd9Sstevel@tonic-gate 						    KSTAT_DATA_LONG;
5327c478bd9Sstevel@tonic-gate 					}
5337c478bd9Sstevel@tonic-gate 					break;
5347c478bd9Sstevel@tonic-gate 				default:
5357c478bd9Sstevel@tonic-gate 				case DDI_MODEL_NONE:
5367c478bd9Sstevel@tonic-gate #ifdef _LP64
5377c478bd9Sstevel@tonic-gate 					if (knew->data_type ==
5387c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_INT64) {
5397c478bd9Sstevel@tonic-gate 						knew->value.l =
5407c478bd9Sstevel@tonic-gate 						    (long)knew->value.i64;
5417c478bd9Sstevel@tonic-gate 						knew->data_type =
5427c478bd9Sstevel@tonic-gate 						    KSTAT_DATA_LONG;
5437c478bd9Sstevel@tonic-gate 					}
5447c478bd9Sstevel@tonic-gate #endif /* _LP64 */
5457c478bd9Sstevel@tonic-gate 					break;
5467c478bd9Sstevel@tonic-gate 				}
5477c478bd9Sstevel@tonic-gate 				break;
5487c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
5497c478bd9Sstevel@tonic-gate 				switch (model) {
5507c478bd9Sstevel@tonic-gate 				case DDI_MODEL_ILP32:
5517c478bd9Sstevel@tonic-gate 					if (knew->data_type ==
5527c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_UINT32) {
5537c478bd9Sstevel@tonic-gate 						knew->value.ul =
5547c478bd9Sstevel@tonic-gate 						    (ulong_t)knew->value.ui32;
5557c478bd9Sstevel@tonic-gate 						knew->data_type =
5567c478bd9Sstevel@tonic-gate 						    KSTAT_DATA_ULONG;
5577c478bd9Sstevel@tonic-gate 					}
5587c478bd9Sstevel@tonic-gate 					break;
5597c478bd9Sstevel@tonic-gate 				default:
5607c478bd9Sstevel@tonic-gate 				case DDI_MODEL_NONE:
5617c478bd9Sstevel@tonic-gate #ifdef _LP64
5627c478bd9Sstevel@tonic-gate 					if (knew->data_type ==
5637c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_UINT64) {
5647c478bd9Sstevel@tonic-gate 						knew->value.ul =
5657c478bd9Sstevel@tonic-gate 						    (ulong_t)knew->value.ui64;
5667c478bd9Sstevel@tonic-gate 						knew->data_type =
5677c478bd9Sstevel@tonic-gate 						    KSTAT_DATA_ULONG;
5687c478bd9Sstevel@tonic-gate 					}
5697c478bd9Sstevel@tonic-gate #endif /* _LP64 */
5707c478bd9Sstevel@tonic-gate 					break;
5717c478bd9Sstevel@tonic-gate 				}
5727c478bd9Sstevel@tonic-gate 				break;
5737c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
5747c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_STRING:
5757c478bd9Sstevel@tonic-gate 				if (knew->data_type != KSTAT_DATA_STRING) {
5767c478bd9Sstevel@tonic-gate 					KSTAT_EXIT(ksp);
5777c478bd9Sstevel@tonic-gate 					kstat_rele(ksp);
5787c478bd9Sstevel@tonic-gate 					kmem_free(kbuf, bufsize + 1);
5797c478bd9Sstevel@tonic-gate 					kmem_free(buf, bufsize + 1);
5807c478bd9Sstevel@tonic-gate 					return (EINVAL);
5817c478bd9Sstevel@tonic-gate 				}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
5847c478bd9Sstevel@tonic-gate 				if (model == DDI_MODEL_ILP32)
5857c478bd9Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(knew) =
5867c478bd9Sstevel@tonic-gate 					    (char *)(uintptr_t)
587a1b5e537Sbmc 						knew->value.str.addr.ptr32;
5887c478bd9Sstevel@tonic-gate #endif
5897c478bd9Sstevel@tonic-gate 				/*
5907c478bd9Sstevel@tonic-gate 				 * Nothing special for NULL
5917c478bd9Sstevel@tonic-gate 				 */
5927c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(knew) == NULL)
5937c478bd9Sstevel@tonic-gate 					break;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 				/*
5967c478bd9Sstevel@tonic-gate 				 * Check to see that the pointers all point
5977c478bd9Sstevel@tonic-gate 				 * to within the buffer and after the array
5987c478bd9Sstevel@tonic-gate 				 * of kstat_named_t's.
5997c478bd9Sstevel@tonic-gate 				 */
6007c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(knew) <
6017c478bd9Sstevel@tonic-gate 				    (char *)
6027c478bd9Sstevel@tonic-gate 				    ((kstat_named_t *)user_kstat.ks_data +
6037c478bd9Sstevel@tonic-gate 				    ksp->ks_ndata)) {
6047c478bd9Sstevel@tonic-gate 					KSTAT_EXIT(ksp);
6057c478bd9Sstevel@tonic-gate 					kstat_rele(ksp);
6067c478bd9Sstevel@tonic-gate 					kmem_free(kbuf, bufsize + 1);
6077c478bd9Sstevel@tonic-gate 					kmem_free(buf, bufsize + 1);
6087c478bd9Sstevel@tonic-gate 					return (EINVAL);
6097c478bd9Sstevel@tonic-gate 				}
6107c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(knew) +
6117c478bd9Sstevel@tonic-gate 				    KSTAT_NAMED_STR_BUFLEN(knew) >
6127c478bd9Sstevel@tonic-gate 				    ((char *)user_kstat.ks_data +
6137c478bd9Sstevel@tonic-gate 				    ksp->ks_data_size)) {
6147c478bd9Sstevel@tonic-gate 					KSTAT_EXIT(ksp);
6157c478bd9Sstevel@tonic-gate 					kstat_rele(ksp);
6167c478bd9Sstevel@tonic-gate 					kmem_free(kbuf, bufsize + 1);
6177c478bd9Sstevel@tonic-gate 					kmem_free(buf, bufsize + 1);
6187c478bd9Sstevel@tonic-gate 					return (EINVAL);
6197c478bd9Sstevel@tonic-gate 				}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 				/*
6227c478bd9Sstevel@tonic-gate 				 * Update the pointers within the buffer
6237c478bd9Sstevel@tonic-gate 				 */
6247c478bd9Sstevel@tonic-gate 				KSTAT_NAMED_STR_PTR(knew) =
6257c478bd9Sstevel@tonic-gate 				    (char *)buf +
6267c478bd9Sstevel@tonic-gate 				    (KSTAT_NAMED_STR_PTR(knew) -
6277c478bd9Sstevel@tonic-gate 				    (char *)user_kstat.ks_data);
6287c478bd9Sstevel@tonic-gate 				break;
6297c478bd9Sstevel@tonic-gate 			default:
6307c478bd9Sstevel@tonic-gate 				break;
6317c478bd9Sstevel@tonic-gate 			}
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 		kold = kbuf;
6357c478bd9Sstevel@tonic-gate 		knew = buf;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		/*
6387c478bd9Sstevel@tonic-gate 		 * Now make sure the types are what we expected them to be.
6397c478bd9Sstevel@tonic-gate 		 */
6407c478bd9Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, kold++, knew++)
6417c478bd9Sstevel@tonic-gate 			if (kold->data_type != knew->data_type) {
6427c478bd9Sstevel@tonic-gate 				KSTAT_EXIT(ksp);
6437c478bd9Sstevel@tonic-gate 				kstat_rele(ksp);
6447c478bd9Sstevel@tonic-gate 				kmem_free(kbuf, bufsize + 1);
6457c478bd9Sstevel@tonic-gate 				kmem_free(buf, bufsize + 1);
6467c478bd9Sstevel@tonic-gate 				return (EINVAL);
6477c478bd9Sstevel@tonic-gate 			}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		kmem_free(kbuf, bufsize + 1);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	error = KSTAT_SNAPSHOT(ksp, buf, KSTAT_WRITE);
6537c478bd9Sstevel@tonic-gate 	if (!error)
6547c478bd9Sstevel@tonic-gate 		error = KSTAT_UPDATE(ksp, KSTAT_WRITE);
6557c478bd9Sstevel@tonic-gate 	*rvalp = kstat_chain_id;
6567c478bd9Sstevel@tonic-gate 	KSTAT_EXIT(ksp);
6577c478bd9Sstevel@tonic-gate 	kstat_rele(ksp);
6587c478bd9Sstevel@tonic-gate 	kmem_free(buf, bufsize + 1);
6597c478bd9Sstevel@tonic-gate 	return (error);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6637c478bd9Sstevel@tonic-gate static int
kstat_ioctl(dev_t dev,int cmd,intptr_t data,int flag,cred_t * cr,int * rvalp)6647c478bd9Sstevel@tonic-gate kstat_ioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cr, int *rvalp)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	int rc = 0;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	switch (cmd) {
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	case KSTAT_IOC_CHAIN_ID:
6717c478bd9Sstevel@tonic-gate 		*rvalp = kstat_chain_id;
6727c478bd9Sstevel@tonic-gate 		break;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	case KSTAT_IOC_READ:
6757c478bd9Sstevel@tonic-gate 		rc = read_kstat_data(rvalp, (void *)data, flag);
6767c478bd9Sstevel@tonic-gate 		break;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	case KSTAT_IOC_WRITE:
6797c478bd9Sstevel@tonic-gate 		rc = write_kstat_data(rvalp, (void *)data, flag, cr);
6807c478bd9Sstevel@tonic-gate 		break;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	default:
6837c478bd9Sstevel@tonic-gate 		/* invalid request */
6847c478bd9Sstevel@tonic-gate 		rc = EINVAL;
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 	return (rc);
6877c478bd9Sstevel@tonic-gate }
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate /* ARGSUSED */
6907c478bd9Sstevel@tonic-gate static int
kstat_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)6917c478bd9Sstevel@tonic-gate kstat_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
6927c478bd9Sstevel@tonic-gate 	void **result)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	switch (infocmd) {
6957c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6967c478bd9Sstevel@tonic-gate 		*result = kstat_devi;
6977c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6987c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
6997c478bd9Sstevel@tonic-gate 		*result = NULL;
7007c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate static int
kstat_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)7067c478bd9Sstevel@tonic-gate kstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
7097c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "kstat", S_IFCHR,
7127c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
7137c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
7147c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 	kstat_devi = devi;
7177c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate static int
kstat_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)7217c478bd9Sstevel@tonic-gate kstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
7247c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
7277c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate static struct cb_ops kstat_cb_ops = {
7317c478bd9Sstevel@tonic-gate 	nulldev,		/* open */
7327c478bd9Sstevel@tonic-gate 	nulldev,		/* close */
7337c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
7347c478bd9Sstevel@tonic-gate 	nodev,			/* print */
7357c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
7367c478bd9Sstevel@tonic-gate 	nodev,			/* read */
7377c478bd9Sstevel@tonic-gate 	nodev,			/* write */
7387c478bd9Sstevel@tonic-gate 	kstat_ioctl,		/* ioctl */
7397c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
7407c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
7417c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
7427c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
7437c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
7447c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
7457c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
7467c478bd9Sstevel@tonic-gate };
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate static struct dev_ops kstat_ops = {
7497c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
7507c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
7517c478bd9Sstevel@tonic-gate 	kstat_info,		/* get_dev_info */
7527c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
7537c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
7547c478bd9Sstevel@tonic-gate 	kstat_attach,		/* attach */
7557c478bd9Sstevel@tonic-gate 	kstat_detach,		/* detach */
7567c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
7577c478bd9Sstevel@tonic-gate 	&kstat_cb_ops,		/* driver operations */
75819397407SSherry Moore 	(struct bus_ops *)0,	/* no bus operations */
75919397407SSherry Moore 	NULL,			/* power */
76019397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
7617c478bd9Sstevel@tonic-gate };
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
76419397407SSherry Moore 	&mod_driverops, "kernel statistics driver", &kstat_ops,
7657c478bd9Sstevel@tonic-gate };
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
7687c478bd9Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
7697c478bd9Sstevel@tonic-gate };
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate int
_init(void)7727c478bd9Sstevel@tonic-gate _init(void)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate int
_fini(void)7787c478bd9Sstevel@tonic-gate _fini(void)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)7847c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
7877c478bd9Sstevel@tonic-gate }
788