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