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